using System.IO;
using UnityEngine;
using YooAsset;
namespace AlicizaX.Resource.Runtime
{
///
/// 远端资源地址查询服务类
///
class RemoteServices : IRemoteServices
{
private readonly string _defaultHostServer;
private readonly string _fallbackHostServer;
public RemoteServices(string defaultHostServer, string fallbackHostServer)
{
_defaultHostServer = defaultHostServer;
_fallbackHostServer = fallbackHostServer;
}
string IRemoteServices.GetRemoteMainURL(string fileName)
{
return $"{_defaultHostServer}/{fileName}";
}
string IRemoteServices.GetRemoteFallbackURL(string fileName)
{
return $"{_fallbackHostServer}/{fileName}";
}
}
///
/// 资源文件流加载解密类
///
class FileStreamDecryption : IDecryptionServices
{
DecryptResult IDecryptionServices.LoadAssetBundle(DecryptFileInfo fileInfo)
{
BundleStream bundleStream = new BundleStream(fileInfo.FileLoadPath, FileMode.Open, FileAccess.Read, FileShare.Read);
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = bundleStream;
decryptResult.Result = AssetBundle.LoadFromStream(bundleStream, 0, GetManagedReadBufferSize());
return decryptResult;
}
// AssetBundle解密方法
DecryptResult IDecryptionServices.LoadAssetBundleAsync(DecryptFileInfo fileInfo)
{
BundleStream bundleStream = new BundleStream(fileInfo.FileLoadPath, FileMode.Open, FileAccess.Read, FileShare.Read);
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = bundleStream;
decryptResult.CreateRequest = AssetBundle.LoadFromStreamAsync(bundleStream, 0, GetManagedReadBufferSize());
return decryptResult;
}
public byte[] ReadFileData(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
public string ReadFileText(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
private static uint GetManagedReadBufferSize()
{
return 1024;
}
}
///
/// 资源文件偏移加载解密类
///
class FileOffsetDecryption : IDecryptionServices
{
DecryptResult IDecryptionServices.LoadAssetBundle(DecryptFileInfo fileInfo)
{
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = null;
decryptResult.Result = AssetBundle.LoadFromFile(fileInfo.FileLoadPath, 0, GetFileOffset());
return decryptResult;
}
// AssetBundle解密方法
DecryptResult IDecryptionServices.LoadAssetBundleAsync(DecryptFileInfo fileInfo)
{
DecryptResult decryptResult = new DecryptResult();
decryptResult.ManagedStream = null;
decryptResult.CreateRequest = AssetBundle.LoadFromFileAsync(fileInfo.FileLoadPath, 0, GetFileOffset());
return decryptResult;
}
public byte[] ReadFileData(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
public string ReadFileText(DecryptFileInfo fileInfo)
{
throw new System.NotImplementedException();
}
private static ulong GetFileOffset()
{
return 32;
}
}
///
/// 资源文件解密流
///
public class BundleStream : FileStream
{
public const byte KEY = 64;
public BundleStream(string path, FileMode mode, FileAccess access, FileShare share) : base(path, mode, access, share)
{
}
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;
}
}
}