AlicizaX/Client/Packages/com.alicizax.unity.resource/Runtime/Resource/ResourceModule.Services.cs

138 lines
4.2 KiB
C#
Raw Normal View History

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