Update basic sample

pull/51/head
hevinci 2022-11-02 22:04:11 +08:00
parent a805e0639e
commit c7116ad165
7 changed files with 131 additions and 39 deletions

View File

@ -0,0 +1,65 @@
using System;
using System.IO;
using System.Text;
using YooAsset;
public class EncryptionNone : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
EncryptResult result = new EncryptResult();
result.LoadMethod = EBundleLoadMethod.Normal;
return result;
}
}
public class FileOffsetEncryption : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
if(fileInfo.BundleName.Contains("gameres_music"))
{
int offset = 32;
byte[] fileData = File.ReadAllBytes(fileInfo.FilePath);
var encryptedData = new byte[fileData.Length + offset];
Buffer.BlockCopy(fileData, 0, encryptedData, offset, fileData.Length);
EncryptResult result = new EncryptResult();
result.LoadMethod = EBundleLoadMethod.LoadFromFileOffset;
result.EncryptedData = encryptedData;
return result;
}
else
{
EncryptResult result = new EncryptResult();
result.LoadMethod = EBundleLoadMethod.Normal;
return result;
}
}
}
public class FileStreamEncryption : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
if (fileInfo.BundleName.Contains("gameres_music"))
{
var fileData = File.ReadAllBytes(fileInfo.FilePath);
for (int i = 0; i < fileData.Length; i++)
{
fileData[i] ^= BundleStream.KEY;
}
EncryptResult result = new EncryptResult();
result.LoadMethod = EBundleLoadMethod.LoadFromStream;
result.EncryptedData = fileData;
return result;
}
else
{
EncryptResult result = new EncryptResult();
result.LoadMethod = EBundleLoadMethod.Normal;
return result;
}
}
}

View File

@ -1,37 +0,0 @@
using System;
using YooAsset.Editor;
public class EncryptionNone : IEncryptionServices
{
bool IEncryptionServices.Check(string bundleName)
{
return false;
}
byte[] IEncryptionServices.Encrypt(byte[] fileData)
{
throw new System.NotImplementedException();
}
}
public class GameEncryption : IEncryptionServices
{
/// <summary>
/// 检测资源包是否需要加密
/// </summary>
bool IEncryptionServices.Check(string bundleName)
{
// 对配置表进行加密
return bundleName.Contains("assets/gameres/config/");
}
/// <summary>
/// 对数据进行加密,并返回加密后的数据
/// </summary>
byte[] IEncryptionServices.Encrypt(byte[] fileData)
{
int offset = 32;
var temper = new byte[fileData.Length + offset];
Buffer.BlockCopy(fileData, 0, temper, offset, fileData.Length);
return temper;
}
}

View File

@ -3,6 +3,7 @@ using System.Collections;
using UnityEngine; using UnityEngine;
using YooAsset; using YooAsset;
using Better.StreamingAssets; using Better.StreamingAssets;
using System.IO;
public class BootScene : MonoBehaviour public class BootScene : MonoBehaviour
{ {
@ -61,6 +62,7 @@ public class BootScene : MonoBehaviour
if (PlayMode == EPlayMode.OfflinePlayMode) if (PlayMode == EPlayMode.OfflinePlayMode)
{ {
var createParameters = new OfflinePlayModeParameters(); var createParameters = new OfflinePlayModeParameters();
createParameters.DecryptionServices = new BundleDecryptionServices();
yield return defaultPackage.InitializeAsync(createParameters); yield return defaultPackage.InitializeAsync(createParameters);
} }
@ -68,6 +70,7 @@ public class BootScene : MonoBehaviour
if (PlayMode == EPlayMode.HostPlayMode) if (PlayMode == EPlayMode.HostPlayMode)
{ {
var createParameters = new HostPlayModeParameters(); var createParameters = new HostPlayModeParameters();
createParameters.DecryptionServices = new BundleDecryptionServices();
createParameters.QueryServices = new QueryStreamingAssetsFileServices(); createParameters.QueryServices = new QueryStreamingAssetsFileServices();
createParameters.DefaultHostServer = GetHostServerURL(); createParameters.DefaultHostServer = GetHostServerURL();
createParameters.FallbackHostServer = GetHostServerURL(); createParameters.FallbackHostServer = GetHostServerURL();
@ -113,4 +116,27 @@ public class BootScene : MonoBehaviour
return BetterStreamingAssets.FileExists($"{buildinFolderName}/{fileName}"); return BetterStreamingAssets.FileExists($"{buildinFolderName}/{fileName}");
} }
} }
private class BundleDecryptionServices : IDecryptionServices
{
public ulong LoadFromFileOffset(DecryptFileInfo fileInfo)
{
return 32;
}
public byte[] LoadFromMemory(DecryptFileInfo fileInfo)
{
throw new NotImplementedException();
}
public FileStream LoadFromStream(DecryptFileInfo fileInfo)
{
BundleStream bundleStream = new BundleStream(fileInfo.FilePath, FileMode.Open);
return bundleStream;
}
public uint GetManagedReadBufferSize()
{
return 1024;
}
}
} }

View File

@ -0,0 +1,27 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class BundleStream : FileStream
{
public const byte KEY = 64;
public BundleStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) : base(path, mode, access, share, bufferSize, useAsync)
{
}
public BundleStream(string path, FileMode mode) : base(path, mode)
{
}
public override int Read(byte[] array, int offset, int count)
{
var index = base.Read(array, offset, count);
for (int i = 0; i < array.Length; i++)
{
array[i] ^= KEY;
}
return index;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7da6bc93686626a4cb248691891073ac
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -13,10 +13,10 @@ MonoBehaviour:
m_Name: AssetBundleBuilderSetting m_Name: AssetBundleBuilderSetting
m_EditorClassIdentifier: m_EditorClassIdentifier:
BuildPipeline: 0 BuildPipeline: 0
BuildMode: 1 BuildMode: 0
BuildPackage: DefaultPackage BuildPackage: DefaultPackage
CompressOption: 2 CompressOption: 2
OutputNameStyle: 1 OutputNameStyle: 1
CopyBuildinFileOption: 1 CopyBuildinFileOption: 1
CopyBuildinFileTags: CopyBuildinFileTags:
EncyptionClassName: EncryptionNone EncyptionClassName: FileStreamEncryption