Compare commits

..

No commits in common. "151e978e5b3fb2df20a2ef96c01b6c1c0a96b230" and "07086b68c8c49e3b8784e875bf8eb6e2e06bf133" have entirely different histories.

21 changed files with 121 additions and 469 deletions

View File

@ -47,11 +47,6 @@ namespace YooAsset
/// 自定义参数:跨域下载服务接口 /// 自定义参数:跨域下载服务接口
/// </summary> /// </summary>
public IRemoteServices RemoteServices { private set; get; } = null; public IRemoteServices RemoteServices { private set; get; } = null;
/// <summary>
/// 自定义参数:解密方法类
/// </summary>
public IWebDecryptionServices DecryptionServices { private set; get; }
#endregion #endregion
@ -113,10 +108,6 @@ namespace YooAsset
{ {
RemoteServices = (IRemoteServices)value; RemoteServices = (IRemoteServices)value;
} }
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{
DecryptionServices = (IWebDecryptionServices)value;
}
else else
{ {
YooLogger.Warning($"Invalid parameter : {name}"); YooLogger.Warning($"Invalid parameter : {name}");

View File

@ -1,4 +1,6 @@
 
using UnityEngine;
namespace YooAsset namespace YooAsset
{ {
internal class DWRFSLoadAssetBundleOperation : FSLoadBundleOperation internal class DWRFSLoadAssetBundleOperation : FSLoadBundleOperation
@ -6,13 +8,13 @@ namespace YooAsset
private enum ESteps private enum ESteps
{ {
None, None,
DownloadAssetBundle, DownloadFile,
Done, Done,
} }
private readonly DefaultWebRemoteFileSystem _fileSystem; private readonly DefaultWebRemoteFileSystem _fileSystem;
private readonly PackageBundle _bundle; private readonly PackageBundle _bundle;
private DownloadAssetBundleOperation _downloadAssetBundleOp; private DownloadHandlerAssetBundleOperation _downloadhanlderAssetBundleOp;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
@ -23,47 +25,38 @@ namespace YooAsset
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
_steps = ESteps.DownloadAssetBundle; _steps = ESteps.DownloadFile;
} }
internal override void InternalOnUpdate() internal override void InternalOnUpdate()
{ {
if (_steps == ESteps.None || _steps == ESteps.Done) if (_steps == ESteps.None || _steps == ESteps.Done)
return; return;
if (_steps == ESteps.DownloadAssetBundle) if (_steps == ESteps.DownloadFile)
{ {
if (_downloadAssetBundleOp == null) if (_downloadhanlderAssetBundleOp == null)
{ {
DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60); DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60);
downloadParam.MainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName); downloadParam.MainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName);
downloadParam.FallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName); downloadParam.FallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName);
_downloadhanlderAssetBundleOp = new DownloadHandlerAssetBundleOperation(_fileSystem.DisableUnityWebCache, _bundle, downloadParam);
if (_bundle.Encrypted) OperationSystem.StartOperation(_fileSystem.PackageName, _downloadhanlderAssetBundleOp);
{
_downloadAssetBundleOp = new DownloadWebEncryptAssetBundleOperation(_fileSystem.DecryptionServices, _bundle, downloadParam);
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp);
}
else
{
_downloadAssetBundleOp = new DownloadWebNormalAssetBundleOperation(_fileSystem.DisableUnityWebCache, _bundle, downloadParam);
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp);
}
} }
DownloadProgress = _downloadAssetBundleOp.DownloadProgress; DownloadProgress = _downloadhanlderAssetBundleOp.DownloadProgress;
DownloadedBytes = _downloadAssetBundleOp.DownloadedBytes; DownloadedBytes = _downloadhanlderAssetBundleOp.DownloadedBytes;
Progress = _downloadAssetBundleOp.Progress; Progress = _downloadhanlderAssetBundleOp.Progress;
if (_downloadAssetBundleOp.IsDone == false) if (_downloadhanlderAssetBundleOp.IsDone == false)
return; return;
if (_downloadAssetBundleOp.Status == EOperationStatus.Succeed) if (_downloadhanlderAssetBundleOp.Status == EOperationStatus.Succeed)
{ {
var assetBundle = _downloadAssetBundleOp.Result; var assetBundle = _downloadhanlderAssetBundleOp.Result;
if(assetBundle == null) if(assetBundle == null)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !"; Error = $"{nameof(DownloadHandlerAssetBundleOperation)} loaded asset bundle is null !";
} }
else else
{ {
@ -76,7 +69,7 @@ namespace YooAsset
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = _downloadAssetBundleOp.Error; Error = _downloadhanlderAssetBundleOp.Error;
} }
} }
} }
@ -92,10 +85,10 @@ namespace YooAsset
} }
public override void AbortDownloadOperation() public override void AbortDownloadOperation()
{ {
if (_steps == ESteps.DownloadAssetBundle) if (_steps == ESteps.DownloadFile)
{ {
if (_downloadAssetBundleOp != null) if (_downloadhanlderAssetBundleOp != null)
_downloadAssetBundleOp.SetAbort(); _downloadhanlderAssetBundleOp.SetAbort();
} }
} }
} }

View File

@ -56,11 +56,6 @@ namespace YooAsset
/// 禁用Unity的网络缓存 /// 禁用Unity的网络缓存
/// </summary> /// </summary>
public bool DisableUnityWebCache { private set; get; } = false; public bool DisableUnityWebCache { private set; get; } = false;
/// <summary>
/// 自定义参数:解密方法类
/// </summary>
public IWebDecryptionServices DecryptionServices { private set; get; }
#endregion #endregion
@ -118,10 +113,6 @@ namespace YooAsset
{ {
DisableUnityWebCache = (bool)value; DisableUnityWebCache = (bool)value;
} }
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{
DecryptionServices = (IWebDecryptionServices)value;
}
else else
{ {
YooLogger.Warning($"Invalid parameter : {name}"); YooLogger.Warning($"Invalid parameter : {name}");

View File

@ -6,13 +6,13 @@ namespace YooAsset
private enum ESteps private enum ESteps
{ {
None, None,
DownloadAssetBundle, DownloadFile,
Done, Done,
} }
private readonly DefaultWebServerFileSystem _fileSystem; private readonly DefaultWebServerFileSystem _fileSystem;
private readonly PackageBundle _bundle; private readonly PackageBundle _bundle;
private DownloadAssetBundleOperation _downloadAssetBundleOp; private DownloadHandlerAssetBundleOperation _downloadhanlderAssetBundleOp;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
@ -23,48 +23,39 @@ namespace YooAsset
} }
internal override void InternalOnStart() internal override void InternalOnStart()
{ {
_steps = ESteps.DownloadAssetBundle; _steps = ESteps.DownloadFile;
} }
internal override void InternalOnUpdate() internal override void InternalOnUpdate()
{ {
if (_steps == ESteps.None || _steps == ESteps.Done) if (_steps == ESteps.None || _steps == ESteps.Done)
return; return;
if (_steps == ESteps.DownloadAssetBundle) if (_steps == ESteps.DownloadFile)
{ {
if (_downloadAssetBundleOp == null) if (_downloadhanlderAssetBundleOp == null)
{ {
DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60); DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60);
string fileLoadPath = _fileSystem.GetWebFileLoadPath(_bundle); string fileLoadPath = _fileSystem.GetWebFileLoadPath(_bundle);
downloadParam.MainURL = DownloadSystemHelper.ConvertToWWWPath(fileLoadPath); downloadParam.MainURL = DownloadSystemHelper.ConvertToWWWPath(fileLoadPath);
downloadParam.FallbackURL = downloadParam.MainURL; downloadParam.FallbackURL = downloadParam.MainURL;
_downloadhanlderAssetBundleOp = new DownloadHandlerAssetBundleOperation(_fileSystem.DisableUnityWebCache, _bundle, downloadParam);
if (_bundle.Encrypted) OperationSystem.StartOperation(_fileSystem.PackageName, _downloadhanlderAssetBundleOp);
{
_downloadAssetBundleOp = new DownloadWebEncryptAssetBundleOperation(_fileSystem.DecryptionServices, _bundle, downloadParam);
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp);
}
else
{
_downloadAssetBundleOp = new DownloadWebNormalAssetBundleOperation(_fileSystem.DisableUnityWebCache, _bundle, downloadParam);
OperationSystem.StartOperation(_fileSystem.PackageName, _downloadAssetBundleOp);
}
} }
DownloadProgress = _downloadAssetBundleOp.DownloadProgress; DownloadProgress = _downloadhanlderAssetBundleOp.DownloadProgress;
DownloadedBytes = _downloadAssetBundleOp.DownloadedBytes; DownloadedBytes = _downloadhanlderAssetBundleOp.DownloadedBytes;
Progress = _downloadAssetBundleOp.Progress; Progress = _downloadhanlderAssetBundleOp.Progress;
if (_downloadAssetBundleOp.IsDone == false) if (_downloadhanlderAssetBundleOp.IsDone == false)
return; return;
if (_downloadAssetBundleOp.Status == EOperationStatus.Succeed) if (_downloadhanlderAssetBundleOp.Status == EOperationStatus.Succeed)
{ {
var assetBundle = _downloadAssetBundleOp.Result; var assetBundle = _downloadhanlderAssetBundleOp.Result;
if (assetBundle == null) if (assetBundle == null)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !"; Error = $"{nameof(DownloadHandlerAssetBundleOperation)} loaded asset bundle is null !";
} }
else else
{ {
@ -77,7 +68,7 @@ namespace YooAsset
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = _downloadAssetBundleOp.Error; Error = _downloadhanlderAssetBundleOp.Error;
} }
} }
} }
@ -93,10 +84,10 @@ namespace YooAsset
} }
public override void AbortDownloadOperation() public override void AbortDownloadOperation()
{ {
if (_steps == ESteps.DownloadAssetBundle) if (_steps == ESteps.DownloadFile)
{ {
if (_downloadAssetBundleOp != null) if (_downloadhanlderAssetBundleOp != null)
_downloadAssetBundleOp.SetAbort(); _downloadhanlderAssetBundleOp.SetAbort();
} }
} }
} }

View File

@ -110,11 +110,10 @@ namespace YooAsset
/// 创建默认的WebServer文件系统参数 /// 创建默认的WebServer文件系统参数
/// </summary> /// </summary>
/// <param name="disableUnityWebCache">禁用Unity的网络缓存</param> /// <param name="disableUnityWebCache">禁用Unity的网络缓存</param>
public static FileSystemParameters CreateDefaultWebServerFileSystemParameters(IWebDecryptionServices decryptionServices = null, bool disableUnityWebCache = false) public static FileSystemParameters CreateDefaultWebServerFileSystemParameters(bool disableUnityWebCache = false)
{ {
string fileSystemClass = typeof(DefaultWebServerFileSystem).FullName; string fileSystemClass = typeof(DefaultWebServerFileSystem).FullName;
var fileSystemParams = new FileSystemParameters(fileSystemClass, null); var fileSystemParams = new FileSystemParameters(fileSystemClass, null);
fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
fileSystemParams.AddParameter(FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE, disableUnityWebCache); fileSystemParams.AddParameter(FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE, disableUnityWebCache);
return fileSystemParams; return fileSystemParams;
} }
@ -124,12 +123,11 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="remoteServices">远端资源地址查询服务类</param> /// <param name="remoteServices">远端资源地址查询服务类</param>
/// <param name="disableUnityWebCache">禁用Unity的网络缓存</param> /// <param name="disableUnityWebCache">禁用Unity的网络缓存</param>
public static FileSystemParameters CreateDefaultWebRemoteFileSystemParameters(IRemoteServices remoteServices, IWebDecryptionServices decryptionServices = null, bool disableUnityWebCache = false) public static FileSystemParameters CreateDefaultWebRemoteFileSystemParameters(IRemoteServices remoteServices, bool disableUnityWebCache = false)
{ {
string fileSystemClass = typeof(DefaultWebRemoteFileSystem).FullName; string fileSystemClass = typeof(DefaultWebRemoteFileSystem).FullName;
var fileSystemParams = new FileSystemParameters(fileSystemClass, null); var fileSystemParams = new FileSystemParameters(fileSystemClass, null);
fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices); fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
fileSystemParams.AddParameter(FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE, disableUnityWebCache); fileSystemParams.AddParameter(FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE, disableUnityWebCache);
return fileSystemParams; return fileSystemParams;
} }

View File

@ -1,14 +0,0 @@
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
internal abstract class DownloadAssetBundleOperation : DefaultDownloadFileOperation
{
internal DownloadAssetBundleOperation(PackageBundle bundle, DownloadParam param) : base(bundle, param)
{
}
public AssetBundle Result;
}
}

View File

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

View File

@ -3,13 +3,16 @@ using UnityEngine.Networking;
namespace YooAsset namespace YooAsset
{ {
internal class DownloadWebNormalAssetBundleOperation : DownloadAssetBundleOperation internal class DownloadHandlerAssetBundleOperation : DefaultDownloadFileOperation
{ {
private readonly bool _disableUnityWebCache; private readonly bool _disableUnityWebCache;
private DownloadHandlerAssetBundle _downloadhandler; private DownloadHandlerAssetBundle _downloadhandler;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
internal DownloadWebNormalAssetBundleOperation(bool disableUnityWebCache, PackageBundle bundle, DownloadParam param) : base(bundle, param) public AssetBundle Result { private set; get; }
internal DownloadHandlerAssetBundleOperation(bool disableUnityWebCache, PackageBundle bundle, DownloadParam param) : base(bundle, param)
{ {
_disableUnityWebCache = disableUnityWebCache; _disableUnityWebCache = disableUnityWebCache;
} }
@ -52,12 +55,12 @@ namespace YooAsset
// 检查网络错误 // 检查网络错误
if (CheckRequestResult()) if (CheckRequestResult())
{ {
AssetBundle assetBundle = _downloadhandler.assetBundle; var assetBundle = _downloadhandler.assetBundle;
if (assetBundle == null) if (assetBundle == null)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Download handler asset bundle object is null !"; Error = "Download handler asset bundle object is null !";
Status = EOperationStatus.Failed;
} }
else else
{ {
@ -103,7 +106,7 @@ namespace YooAsset
private void CreateWebRequest() private void CreateWebRequest()
{ {
_downloadhandler = CreateWebDownloadHandler(); _downloadhandler = CreateDownloadHandler();
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL); _webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.downloadHandler = _downloadhandler; _webRequest.downloadHandler = _downloadhandler;
_webRequest.disposeDownloadHandlerOnDispose = true; _webRequest.disposeDownloadHandlerOnDispose = true;
@ -118,7 +121,7 @@ namespace YooAsset
_webRequest = null; _webRequest = null;
} }
} }
private DownloadHandlerAssetBundle CreateWebDownloadHandler() private DownloadHandlerAssetBundle CreateDownloadHandler()
{ {
if (_disableUnityWebCache) if (_disableUnityWebCache)
{ {

View File

@ -1,144 +0,0 @@
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
internal class DownloadWebEncryptAssetBundleOperation : DownloadAssetBundleOperation
{
private readonly IWebDecryptionServices _decryptionServices;
private DownloadHandlerBuffer _downloadhandler;
private ESteps _steps = ESteps.None;
internal DownloadWebEncryptAssetBundleOperation(IWebDecryptionServices decryptionServices, PackageBundle bundle, DownloadParam param) : base(bundle, param)
{
_decryptionServices = decryptionServices;
}
internal override void InternalOnStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
// 获取请求地址
_requestURL = GetRequestURL();
// 重置变量
ResetRequestFiled();
// 创建下载器
CreateWebRequest();
_steps = ESteps.CheckRequest;
}
// 检测下载结果
if (_steps == ESteps.CheckRequest)
{
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = DownloadProgress;
if (_webRequest.isDone == false)
{
CheckRequestTimeout();
return;
}
// 检查网络错误
if (CheckRequestResult())
{
if (_decryptionServices == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The {nameof(IWebDecryptionServices)} is null !";
YooLogger.Error(Error);
return;
}
AssetBundle assetBundle = LoadEncryptedAssetBundle(_downloadhandler.data);
if (assetBundle == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Download handler asset bundle object is null !";
}
else
{
_steps = ESteps.Done;
Result = assetBundle;
Status = EOperationStatus.Succeed;
}
}
else
{
_steps = ESteps.TryAgain;
}
// 注意:最终释放请求器
DisposeWebRequest();
}
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
if (FailedTryAgain <= 0)
{
Status = EOperationStatus.Failed;
_steps = ESteps.Done;
YooLogger.Error(Error);
return;
}
_tryAgainTimer += Time.unscaledDeltaTime;
if (_tryAgainTimer > 1f)
{
FailedTryAgain--;
_steps = ESteps.CreateRequest;
YooLogger.Warning(Error);
}
}
}
internal override void InternalOnAbort()
{
_steps = ESteps.Done;
DisposeWebRequest();
}
private void CreateWebRequest()
{
_downloadhandler = new DownloadHandlerBuffer();
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.downloadHandler = _downloadhandler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
/// <summary>
/// 加载加密资源文件
/// </summary>
private AssetBundle LoadEncryptedAssetBundle(byte[] fileData)
{
var fileInfo = new WebDecryptFileInfo();
fileInfo.BundleName = Bundle.BundleName;
fileInfo.FileLoadCRC = Bundle.UnityCRC;
fileInfo.FileData = fileData;
var decryptResult = _decryptionServices.LoadAssetBundle(fileInfo);
return decryptResult.Result;
}
}
}

View File

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

View File

@ -8,12 +8,12 @@ namespace YooAsset
/// <summary> /// <summary>
/// 释放所有资源句柄,防止卸载过程中触发完成回调! /// 释放所有资源句柄,防止卸载过程中触发完成回调!
/// </summary> /// </summary>
public bool ReleaseAllHandles = false; public bool ReleaseAllHandles = true;
/// <summary> /// <summary>
/// 卸载过程中锁定加载操作,防止新的任务请求! /// 卸载过程中锁定加载操作,防止新的任务请求!
/// </summary> /// </summary>
public bool LockLoadOperation = false; public bool LockLoadOperation = true;
} }
public sealed class UnloadAllAssetsOperation : AsyncOperationBase public sealed class UnloadAllAssetsOperation : AsyncOperationBase

View File

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

View File

@ -3,7 +3,6 @@ using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
using YooAsset; using YooAsset;
using TTSDK; using TTSDK;
using WeChatWASM;
internal class TTFSLoadBundleOperation : FSLoadBundleOperation internal class TTFSLoadBundleOperation : FSLoadBundleOperation
{ {
@ -50,22 +49,7 @@ internal class TTFSLoadBundleOperation : FSLoadBundleOperation
if (CheckRequestResult()) if (CheckRequestResult())
{ {
if (_bundle.Encrypted && _fileSystem.DecryptionServices == null) var assetBundle = (_webRequest.downloadHandler as DownloadHandlerTTAssetBundle).assetBundle;
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The {nameof(IWebDecryptionServices)} is null !";
YooLogger.Error(Error);
return;
}
AssetBundle assetBundle;
var downloadHanlder = _webRequest.downloadHandler as DownloadHandlerTTAssetBundle;
if (_bundle.Encrypted)
assetBundle = _fileSystem.LoadEncryptedAssetBundle(_bundle, downloadHanlder.data);
else
assetBundle = downloadHanlder.assetBundle;
if (assetBundle == null) if (assetBundle == null)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;

View File

@ -8,20 +8,11 @@ using System;
public static class TiktokFileSystemCreater public static class TiktokFileSystemCreater
{ {
public static FileSystemParameters CreateByteGameFileSystemParameters(string packageRoot, IRemoteServices remoteServices) public static FileSystemParameters CreateByteGameFileSystemParameters(IRemoteServices remoteServices, string packageRoot)
{ {
string fileSystemClass = $"{nameof(TiktokFileSystem)},YooAsset.RuntimeExtension"; string fileSystemClass = $"{nameof(TiktokFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot); var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices); fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices);
return fileSystemParams;
}
public static FileSystemParameters CreateByteGameFileSystemParameters(string packageRoot, IRemoteServices remoteServices, IWebDecryptionServices decryptionServices)
{
string fileSystemClass = $"{nameof(TiktokFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
return fileSystemParams; return fileSystemParams;
} }
} }
@ -98,11 +89,6 @@ internal class TiktokFileSystem : IFileSystem
/// 自定义参数:远程服务接口 /// 自定义参数:远程服务接口
/// </summary> /// </summary>
public IRemoteServices RemoteServices { private set; get; } = null; public IRemoteServices RemoteServices { private set; get; } = null;
/// <summary>
/// 自定义参数:解密方法类
/// </summary>
public IWebDecryptionServices DecryptionServices { private set; get; }
#endregion #endregion
@ -160,14 +146,10 @@ internal class TiktokFileSystem : IFileSystem
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == FileSystemParametersDefine.REMOTE_SERVICES) if (name == "REMOTE_SERVICES")
{ {
RemoteServices = (IRemoteServices)value; RemoteServices = (IRemoteServices)value;
} }
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{
DecryptionServices = (IWebDecryptionServices)value;
}
else else
{ {
YooLogger.Warning($"Invalid parameter : {name}"); YooLogger.Warning($"Invalid parameter : {name}");
@ -268,19 +250,6 @@ internal class TiktokFileSystem : IFileSystem
} }
return filePath; return filePath;
} }
/// <summary>
/// 加载加密资源文件
/// </summary>
public AssetBundle LoadEncryptedAssetBundle(PackageBundle bundle, byte[] fileData)
{
WebDecryptFileInfo fileInfo = new WebDecryptFileInfo();
fileInfo.BundleName = bundle.BundleName;
fileInfo.FileLoadCRC = bundle.UnityCRC;
fileInfo.FileData = fileData;
var decryptResult = DecryptionServices.LoadAssetBundle(fileInfo);
return decryptResult.Result;
}
#endregion #endregion
} }
#endif #endif

View File

@ -53,9 +53,7 @@ internal class WXFSClearUnusedBundleFilesAsync : FSClearCacheFilesOperation
string bundleGUID = Path.GetFileNameWithoutExtension(fileStat.path); string bundleGUID = Path.GetFileNameWithoutExtension(fileStat.path);
if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false) if (_manifest.TryGetPackageBundleByBundleGUID(bundleGUID, out PackageBundle value) == false)
{ {
string fullPath = WX.GetCachePath(fileStat.path); _unusedCacheFiles.Add(fileStat.path);
if (_unusedCacheFiles.Contains(fullPath) == false)
_unusedCacheFiles.Add(fullPath);
} }
} }

View File

@ -49,22 +49,7 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation
if (CheckRequestResult()) if (CheckRequestResult())
{ {
if (_bundle.Encrypted && _fileSystem.DecryptionServices == null) var assetBundle = (_webRequest.downloadHandler as DownloadHandlerWXAssetBundle).assetBundle;
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The {nameof(IWebDecryptionServices)} is null !";
YooLogger.Error(Error);
return;
}
AssetBundle assetBundle;
var downloadHanlder = _webRequest.downloadHandler as DownloadHandlerWXAssetBundle;
if (_bundle.Encrypted)
assetBundle = _fileSystem.LoadEncryptedAssetBundle(_bundle, downloadHanlder.data);
else
assetBundle = downloadHanlder.assetBundle;
if (assetBundle == null) if (assetBundle == null)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;

View File

@ -8,19 +8,11 @@ using WeChatWASM;
public static class WechatFileSystemCreater public static class WechatFileSystemCreater
{ {
public static FileSystemParameters CreateWechatFileSystemParameters(string packageRoot, IRemoteServices remoteServices) public static FileSystemParameters CreateWechatFileSystemParameters(IRemoteServices remoteServices, string packageRoot)
{ {
string fileSystemClass = $"{nameof(WechatFileSystem)},YooAsset.RuntimeExtension"; string fileSystemClass = $"{nameof(WechatFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot); var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices); fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices);
return fileSystemParams;
}
public static FileSystemParameters CreateWechatFileSystemParameters(string packageRoot, IRemoteServices remoteServices, IWebDecryptionServices decryptionServices)
{
string fileSystemClass = $"{nameof(WechatFileSystem)},YooAsset.RuntimeExtension";
var fileSystemParams = new FileSystemParameters(fileSystemClass, packageRoot);
fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
return fileSystemParams; return fileSystemParams;
} }
} }
@ -69,7 +61,7 @@ internal class WechatFileSystem : IFileSystem
/// 包裹名称 /// 包裹名称
/// </summary> /// </summary>
public string PackageName { private set; get; } public string PackageName { private set; get; }
private readonly string _packageRoot = YooAssetSettingsData.Setting.DefaultYooFolderName; private readonly string _packageRoot = YooAssetSettingsData.Setting.DefaultYooFolderName;
/// <summary> /// <summary>
@ -99,11 +91,6 @@ internal class WechatFileSystem : IFileSystem
/// 自定义参数:远程服务接口 /// 自定义参数:远程服务接口
/// </summary> /// </summary>
public IRemoteServices RemoteServices { private set; get; } = null; public IRemoteServices RemoteServices { private set; get; } = null;
/// <summary>
/// 自定义参数:解密方法类
/// </summary>
public IWebDecryptionServices DecryptionServices { private set; get; }
#endregion #endregion
@ -177,14 +164,10 @@ internal class WechatFileSystem : IFileSystem
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == FileSystemParametersDefine.REMOTE_SERVICES) if (name == "REMOTE_SERVICES")
{ {
RemoteServices = (IRemoteServices)value; RemoteServices = (IRemoteServices)value;
} }
else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{
DecryptionServices = (IWebDecryptionServices)value;
}
else else
{ {
YooLogger.Warning($"Invalid parameter : {name}"); YooLogger.Warning($"Invalid parameter : {name}");
@ -200,11 +183,6 @@ internal class WechatFileSystem : IFileSystem
throw new System.Exception("请配置微信小游戏缓存根目录!"); throw new System.Exception("请配置微信小游戏缓存根目录!");
} }
if (_wxCacheRoot.StartsWith(WX.PluginCachePath) == false)
{
_wxCacheRoot = PathUtility.Combine(WX.PluginCachePath, _wxCacheRoot);
}
// 注意CDN服务未启用的情况下使用微信WEB服务器 // 注意CDN服务未启用的情况下使用微信WEB服务器
if (RemoteServices == null) if (RemoteServices == null)
{ {
@ -286,19 +264,6 @@ internal class WechatFileSystem : IFileSystem
} }
return filePath; return filePath;
} }
/// <summary>
/// 加载加密资源文件
/// </summary>
public AssetBundle LoadEncryptedAssetBundle(PackageBundle bundle, byte[] fileData)
{
WebDecryptFileInfo fileInfo = new WebDecryptFileInfo();
fileInfo.BundleName = bundle.BundleName;
fileInfo.FileLoadCRC = bundle.UnityCRC;
fileInfo.FileData = fileData;
var decryptResult = DecryptionServices.LoadAssetBundle(fileInfo);
return decryptResult.Result;
}
#endregion #endregion
} }
#endif #endif

View File

@ -74,11 +74,10 @@ internal class FsmInitializePackage : IStateNode
#if UNITY_WEBGL && WEIXINMINIGAME && !UNITY_EDITOR #if UNITY_WEBGL && WEIXINMINIGAME && !UNITY_EDITOR
string defaultHostServer = GetHostServerURL(); string defaultHostServer = GetHostServerURL();
string fallbackHostServer = GetHostServerURL(); string fallbackHostServer = GetHostServerURL();
string packageRoot = $"{WeChatWASM.WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE"; //注意:如果有子目录,请修改此处!
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer); IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
createParameters.WebServerFileSystemParameters = WechatFileSystemCreater.CreateWechatFileSystemParameters(packageRoot, remoteServices); createParameters.WebServerFileSystemParameters = WechatFileSystemCreater.CreateWechatFileSystemParameters(remoteServices);
#else #else
createParameters.WebServerFileSystemParameters = FileSystemParameters.CreateDefaultWebServerFileSystemParameters(new WebDecryption()); createParameters.WebServerFileSystemParameters = FileSystemParameters.CreateDefaultWebServerFileSystemParameters();
#endif #endif
initializationOperation = package.InitializeAsync(createParameters); initializationOperation = package.InitializeAsync(createParameters);
} }
@ -149,24 +148,4 @@ internal class FsmInitializePackage : IStateNode
return $"{_fallbackHostServer}/{fileName}"; return $"{_fallbackHostServer}/{fileName}";
} }
} }
private class WebDecryption : IWebDecryptionServices
{
public const byte KEY = 64;
public WebDecryptResult LoadAssetBundle(WebDecryptFileInfo fileInfo)
{
byte[] copyData = new byte[fileInfo.FileData.Length];
Buffer.BlockCopy(fileInfo.FileData, 0, copyData, 0, fileInfo.FileData.Length);
for (int i = 0; i < copyData.Length; i++)
{
copyData[i] ^= KEY;
}
WebDecryptResult decryptResult = new WebDecryptResult();
decryptResult.Result = AssetBundle.LoadFromMemory(copyData);
return decryptResult;
}
}
} }

View File

@ -4,7 +4,6 @@ using System.Text;
using UnityEngine; using UnityEngine;
using YooAsset; using YooAsset;
#region 文件流
/// <summary> /// <summary>
/// 资源文件解密流 /// 资源文件解密流
/// </summary> /// </summary>
@ -30,35 +29,6 @@ public class BundleStream : FileStream
} }
} }
/// <summary>
/// 文件流加密方式
/// </summary>
public class FileStreamEncryption : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
if (fileInfo.BundleName.Contains("_gameres_audio"))
{
var fileData = File.ReadAllBytes(fileInfo.FileLoadPath);
for (int i = 0; i < fileData.Length; i++)
{
fileData[i] ^= BundleStream.KEY;
}
EncryptResult result = new EncryptResult();
result.Encrypted = true;
result.EncryptedData = fileData;
return result;
}
else
{
EncryptResult result = new EncryptResult();
result.Encrypted = false;
return result;
}
}
}
/// <summary> /// <summary>
/// 资源文件流加载解密类 /// 资源文件流加载解密类
/// </summary> /// </summary>
@ -111,37 +81,6 @@ public class FileStreamDecryption : IDecryptionServices
return 1024; return 1024;
} }
} }
#endregion
#region 文件偏移
/// <summary>
/// 文件偏移加密方式
/// </summary>
public class FileOffsetEncryption : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
// 注意:只对音频资源包加密
if (fileInfo.BundleName.Contains("_gameres_audio"))
{
int offset = 32;
byte[] fileData = File.ReadAllBytes(fileInfo.FileLoadPath);
var encryptedData = new byte[fileData.Length + offset];
Buffer.BlockCopy(fileData, 0, encryptedData, offset, fileData.Length);
EncryptResult result = new EncryptResult();
result.Encrypted = true;
result.EncryptedData = encryptedData;
return result;
}
else
{
EncryptResult result = new EncryptResult();
result.Encrypted = false;
return result;
}
}
}
/// <summary> /// <summary>
/// 资源文件偏移加载解密类 /// 资源文件偏移加载解密类
@ -193,4 +132,61 @@ public class FileOffsetDecryption : IDecryptionServices
return 32; return 32;
} }
} }
#endregion
/// <summary>
/// 文件偏移加密方式
/// </summary>
public class FileOffsetEncryption : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
// 注意:只对音频资源包加密
if (fileInfo.BundleName.Contains("_gameres_audio"))
{
int offset = 32;
byte[] fileData = File.ReadAllBytes(fileInfo.FileLoadPath);
var encryptedData = new byte[fileData.Length + offset];
Buffer.BlockCopy(fileData, 0, encryptedData, offset, fileData.Length);
EncryptResult result = new EncryptResult();
result.Encrypted = true;
result.EncryptedData = encryptedData;
return result;
}
else
{
EncryptResult result = new EncryptResult();
result.Encrypted = false;
return result;
}
}
}
/// <summary>
/// 文件流加密方式
/// </summary>
public class FileStreamEncryption : IEncryptionServices
{
public EncryptResult Encrypt(EncryptFileInfo fileInfo)
{
if (fileInfo.BundleName.Contains("_gameres_audio"))
{
var fileData = File.ReadAllBytes(fileInfo.FileLoadPath);
for (int i = 0; i < fileData.Length; i++)
{
fileData[i] ^= BundleStream.KEY;
}
EncryptResult result = new EncryptResult();
result.Encrypted = true;
result.EncryptedData = fileData;
return result;
}
else
{
EncryptResult result = new EncryptResult();
result.Encrypted = false;
return result;
}
}
}