mirror of https://github.com/tuyoogame/YooAsset
Update basic sample
parent
a805e0639e
commit
c7116ad165
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7da6bc93686626a4cb248691891073ac
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue