diff --git a/Assets/YooAsset/Samples~/Basic Sample/GameScript/Editor/Encryption.cs b/Assets/YooAsset/Samples~/Basic Sample/GameScript/Editor/Encryption.cs new file mode 100644 index 0000000..b69a63f --- /dev/null +++ b/Assets/YooAsset/Samples~/Basic Sample/GameScript/Editor/Encryption.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Basic Sample/GameScript/Editor/GameServices.cs.meta b/Assets/YooAsset/Samples~/Basic Sample/GameScript/Editor/Encryption.cs.meta similarity index 100% rename from Assets/YooAsset/Samples~/Basic Sample/GameScript/Editor/GameServices.cs.meta rename to Assets/YooAsset/Samples~/Basic Sample/GameScript/Editor/Encryption.cs.meta diff --git a/Assets/YooAsset/Samples~/Basic Sample/GameScript/Editor/GameServices.cs b/Assets/YooAsset/Samples~/Basic Sample/GameScript/Editor/GameServices.cs deleted file mode 100644 index 9405b19..0000000 --- a/Assets/YooAsset/Samples~/Basic Sample/GameScript/Editor/GameServices.cs +++ /dev/null @@ -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 -{ - /// - /// 检测资源包是否需要加密 - /// - bool IEncryptionServices.Check(string bundleName) - { - // 对配置表进行加密 - return bundleName.Contains("assets/gameres/config/"); - } - - /// - /// 对数据进行加密,并返回加密后的数据 - /// - 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; - } -} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Basic Sample/GameScript/Runtime/BootScene.cs b/Assets/YooAsset/Samples~/Basic Sample/GameScript/Runtime/BootScene.cs index c5257dc..409ead5 100644 --- a/Assets/YooAsset/Samples~/Basic Sample/GameScript/Runtime/BootScene.cs +++ b/Assets/YooAsset/Samples~/Basic Sample/GameScript/Runtime/BootScene.cs @@ -3,6 +3,7 @@ using System.Collections; using UnityEngine; using YooAsset; using Better.StreamingAssets; +using System.IO; public class BootScene : MonoBehaviour { @@ -61,6 +62,7 @@ public class BootScene : MonoBehaviour if (PlayMode == EPlayMode.OfflinePlayMode) { var createParameters = new OfflinePlayModeParameters(); + createParameters.DecryptionServices = new BundleDecryptionServices(); yield return defaultPackage.InitializeAsync(createParameters); } @@ -68,6 +70,7 @@ public class BootScene : MonoBehaviour if (PlayMode == EPlayMode.HostPlayMode) { var createParameters = new HostPlayModeParameters(); + createParameters.DecryptionServices = new BundleDecryptionServices(); createParameters.QueryServices = new QueryStreamingAssetsFileServices(); createParameters.DefaultHostServer = GetHostServerURL(); createParameters.FallbackHostServer = GetHostServerURL(); @@ -113,4 +116,27 @@ public class BootScene : MonoBehaviour 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; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Basic Sample/GameScript/Runtime/FileStream.cs b/Assets/YooAsset/Samples~/Basic Sample/GameScript/Runtime/FileStream.cs new file mode 100644 index 0000000..29cd987 --- /dev/null +++ b/Assets/YooAsset/Samples~/Basic Sample/GameScript/Runtime/FileStream.cs @@ -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; + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Basic Sample/GameScript/Runtime/FileStream.cs.meta b/Assets/YooAsset/Samples~/Basic Sample/GameScript/Runtime/FileStream.cs.meta new file mode 100644 index 0000000..a7df7a0 --- /dev/null +++ b/Assets/YooAsset/Samples~/Basic Sample/GameScript/Runtime/FileStream.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7da6bc93686626a4cb248691891073ac +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/Basic Sample/YooAssetSetting/AssetBundleBuilderSetting.asset b/Assets/YooAsset/Samples~/Basic Sample/YooAssetSetting/AssetBundleBuilderSetting.asset index d93ed67..0e5d053 100644 --- a/Assets/YooAsset/Samples~/Basic Sample/YooAssetSetting/AssetBundleBuilderSetting.asset +++ b/Assets/YooAsset/Samples~/Basic Sample/YooAssetSetting/AssetBundleBuilderSetting.asset @@ -13,10 +13,10 @@ MonoBehaviour: m_Name: AssetBundleBuilderSetting m_EditorClassIdentifier: BuildPipeline: 0 - BuildMode: 1 + BuildMode: 0 BuildPackage: DefaultPackage CompressOption: 2 OutputNameStyle: 1 CopyBuildinFileOption: 1 CopyBuildinFileTags: - EncyptionClassName: EncryptionNone + EncyptionClassName: FileStreamEncryption