refactor the runtime code

重构了运行时代码,支持全新的文件系统。
pull/326/head
何冠峰 2024-07-04 20:36:26 +08:00
parent 2987d356b6
commit ff02da5c54
313 changed files with 9889 additions and 7234 deletions

View File

@ -26,7 +26,7 @@ namespace YooAsset.Editor
{
EncryptFileInfo fileInfo = new EncryptFileInfo();
fileInfo.BundleName = bundleInfo.BundleName;
fileInfo.FilePath = $"{pipelineOutputDirectory}/{bundleInfo.BundleName}";
fileInfo.FileLoadPath = $"{pipelineOutputDirectory}/{bundleInfo.BundleName}";
var encryptResult = encryptionServices.Encrypt(fileInfo);
if (encryptResult.Encrypted)
{

View File

@ -189,7 +189,7 @@ namespace YooAsset.Editor
// Status
StyleColor textColor;
if (bundleInfo.Status == BundleLoaderBase.EStatus.Failed.ToString())
if (bundleInfo.Status == BundleFileLoader.EStatus.Failed.ToString())
textColor = new StyleColor(Color.yellow);
else
textColor = label1.style.color;

View File

@ -1,19 +0,0 @@

namespace YooAsset
{
public class CacheFileInfo
{
public string RemoteFileName { private set; get; }
public string FilePath { private set; get; }
public string FileCRC { private set; get; }
public long FileSize { private set; get; }
public CacheFileInfo(string remoteFileName, string filePath, string fileCRC, long fileSize)
{
RemoteFileName = remoteFileName;
FilePath = filePath;
FileCRC = fileCRC;
FileSize = fileSize;
}
}
}

View File

@ -1,132 +0,0 @@
using System;
using System.IO;
namespace YooAsset
{
internal static class CacheHelper
{
/// <summary>
/// 禁用Unity缓存系统在WebGL平台
/// </summary>
public static bool DisableUnityCacheOnWebGL = false;
#region 资源信息文件相关
private static readonly BufferWriter SharedBuffer = new BufferWriter(1024);
/// <summary>
/// 写入资源包信息
/// </summary>
public static void WriteInfoToFile(string filePath, string dataFileCRC, long dataFileSize)
{
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
SharedBuffer.Clear();
SharedBuffer.WriteUTF8(dataFileCRC);
SharedBuffer.WriteInt64(dataFileSize);
SharedBuffer.WriteToStream(fs);
fs.Flush();
}
}
/// <summary>
/// 读取资源包信息
/// </summary>
public static void ReadInfoFromFile(string filePath, out string dataFileCRC, out long dataFileSize)
{
byte[] binaryData = FileUtility.ReadAllBytes(filePath);
BufferReader buffer = new BufferReader(binaryData);
dataFileCRC = buffer.ReadUTF8();
dataFileSize = buffer.ReadInt64();
}
#endregion
#region 资源文件验证相关
/// <summary>
/// 验证缓存文件(子线程内操作)
/// </summary>
public static EVerifyResult VerifyingCacheFile(VerifyCacheFileElement element, EVerifyLevel verifyLevel)
{
try
{
if (verifyLevel == EVerifyLevel.Low)
{
if (File.Exists(element.InfoFilePath) == false)
return EVerifyResult.InfoFileNotExisted;
if (File.Exists(element.DataFilePath) == false)
return EVerifyResult.DataFileNotExisted;
return EVerifyResult.Succeed;
}
else
{
if (File.Exists(element.InfoFilePath) == false)
return EVerifyResult.InfoFileNotExisted;
// 解析信息文件获取验证数据
CacheHelper.ReadInfoFromFile(element.InfoFilePath, out element.DataFileCRC, out element.DataFileSize);
}
}
catch (Exception)
{
return EVerifyResult.Exception;
}
return VerifyingInternal(element.DataFilePath, element.DataFileSize, element.DataFileCRC, verifyLevel);
}
/// <summary>
/// 验证下载文件(子线程内操作)
/// </summary>
public static EVerifyResult VerifyingTempFile(VerifyTempFileElement element)
{
return VerifyingInternal(element.TempDataFilePath, element.FileSize, element.FileCRC, EVerifyLevel.High);
}
/// <summary>
/// 验证记录文件(主线程内操作)
/// </summary>
public static EVerifyResult VerifyingRecordFile(CacheManager cache, string cacheGUID)
{
var wrapper = cache.TryGetWrapper(cacheGUID);
if (wrapper == null)
return EVerifyResult.CacheNotFound;
EVerifyResult result = VerifyingInternal(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EVerifyLevel.High);
return result;
}
private static EVerifyResult VerifyingInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel)
{
try
{
if (File.Exists(filePath) == false)
return EVerifyResult.DataFileNotExisted;
// 先验证文件大小
long size = FileUtility.GetFileSize(filePath);
if (size < fileSize)
return EVerifyResult.FileNotComplete;
else if (size > fileSize)
return EVerifyResult.FileOverflow;
// 再验证文件CRC
if (verifyLevel == EVerifyLevel.High)
{
string crc = HashUtility.FileCRC32Safely(filePath);
if (crc == fileCRC)
return EVerifyResult.Succeed;
else
return EVerifyResult.FileCrcError;
}
else
{
return EVerifyResult.Succeed;
}
}
catch (Exception)
{
return EVerifyResult.Exception;
}
}
#endregion
}
}

View File

@ -1,136 +0,0 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
internal class CacheManager
{
internal class RecordWrapper
{
public string InfoFilePath { private set; get; }
public string DataFilePath { private set; get; }
public string DataFileCRC { private set; get; }
public long DataFileSize { private set; get; }
public RecordWrapper(string infoFilePath, string dataFilePath, string dataFileCRC, long dataFileSize)
{
InfoFilePath = infoFilePath;
DataFilePath = dataFilePath;
DataFileCRC = dataFileCRC;
DataFileSize = dataFileSize;
}
}
private readonly Dictionary<string, RecordWrapper> _wrappers = new Dictionary<string, RecordWrapper>();
/// <summary>
/// 所属包裹
/// </summary>
public readonly string PackageName;
/// <summary>
/// 验证级别
/// </summary>
public readonly EVerifyLevel BootVerifyLevel;
public CacheManager(string packageName, EVerifyLevel bootVerifyLevel)
{
PackageName = packageName;
BootVerifyLevel = bootVerifyLevel;
}
/// <summary>
/// 清空所有数据
/// </summary>
public void ClearAll()
{
_wrappers.Clear();
}
/// <summary>
/// 查询缓存记录
/// </summary>
public bool IsCached(string cacheGUID)
{
return _wrappers.ContainsKey(cacheGUID);
}
/// <summary>
/// 记录验证结果
/// </summary>
public void Record(string cacheGUID, RecordWrapper wrapper)
{
if (_wrappers.ContainsKey(cacheGUID) == false)
{
_wrappers.Add(cacheGUID, wrapper);
}
else
{
throw new Exception("Should never get here !");
}
}
/// <summary>
/// 丢弃验证结果并删除缓存文件
/// </summary>
public void Discard(string cacheGUID)
{
var wrapper = TryGetWrapper(cacheGUID);
if (wrapper != null)
{
try
{
string dataFilePath = wrapper.DataFilePath;
FileInfo fileInfo = new FileInfo(dataFilePath);
if (fileInfo.Exists)
fileInfo.Directory.Delete(true);
}
catch (Exception e)
{
YooLogger.Error($"Failed to delete cache file ! {e.Message}");
}
}
if (_wrappers.ContainsKey(cacheGUID))
{
_wrappers.Remove(cacheGUID);
}
}
/// <summary>
/// 获取记录对象
/// </summary>
public RecordWrapper TryGetWrapper(string cacheGUID)
{
if (_wrappers.TryGetValue(cacheGUID, out RecordWrapper value))
return value;
else
return null;
}
/// <summary>
/// 获取缓存文件总数
/// </summary>
public int GetAllCachedFilesCount()
{
return _wrappers.Count;
}
/// <summary>
/// 获取缓存GUID集合
/// </summary>
public List<string> GetAllCachedGUIDs()
{
List<string> keys = new List<string>(_wrappers.Keys.Count);
var keyCollection = _wrappers.Keys;
foreach (var key in keyCollection)
{
keys.Add(key);
}
return keys;
}
}
}

View File

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

View File

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

View File

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

View File

@ -1,99 +0,0 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
public class GetAllCacheFileInfosOperation : AsyncOperationBase
{
private enum ESteps
{
None,
TryLoadCacheManifest,
GetCacheFileInfos,
Done,
}
private readonly PersistentManager _persistent;
private readonly CacheManager _cache;
private readonly string _packageVersion;
private LoadCacheManifestOperation _tryLoadCacheManifestOp;
private PackageManifest _manifest;
private ESteps _steps = ESteps.None;
private List<CacheFileInfo> _cacheFileInfos;
/// <summary>
/// 搜索结果
/// </summary>
public List<CacheFileInfo> Result
{
get { return _cacheFileInfos; }
}
internal GetAllCacheFileInfosOperation(PersistentManager persistent, CacheManager cache, string packageVersion)
{
_persistent = persistent;
_cache = cache;
_packageVersion = packageVersion;
}
internal override void InternalOnStart()
{
_steps = ESteps.TryLoadCacheManifest;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.TryLoadCacheManifest)
{
if (_tryLoadCacheManifestOp == null)
{
_tryLoadCacheManifestOp = new LoadCacheManifestOperation(_persistent, _packageVersion);
OperationSystem.StartOperation(_cache.PackageName, _tryLoadCacheManifestOp);
}
if (_tryLoadCacheManifestOp.IsDone == false)
return;
if (_tryLoadCacheManifestOp.Status == EOperationStatus.Succeed)
{
_manifest = _tryLoadCacheManifestOp.Manifest;
_steps = ESteps.GetCacheFileInfos;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _tryLoadCacheManifestOp.Error;
}
}
if (_steps == ESteps.GetCacheFileInfos)
{
var allCachedGUIDs = _cache.GetAllCachedGUIDs();
_cacheFileInfos = new List<CacheFileInfo>(allCachedGUIDs.Count);
for (int i = 0; i < allCachedGUIDs.Count; i++)
{
var cachedGUID = allCachedGUIDs[i];
var wrapper = _cache.TryGetWrapper(cachedGUID);
if (wrapper != null)
{
if (_manifest.TryGetPackageBundleByCacheGUID(cachedGUID, out var packageBundle))
{
var cacheFileInfo = new CacheFileInfo(packageBundle.FileName, wrapper.DataFilePath, wrapper.DataFileCRC, wrapper.DataFileSize);
_cacheFileInfos.Add(cacheFileInfo);
}
}
}
// 注意:总是返回成功
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
}

View File

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

View File

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

View File

@ -1,254 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace YooAsset
{
internal abstract class VerifyCacheFilesOperation : AsyncOperationBase
{
public static VerifyCacheFilesOperation CreateOperation(CacheManager cache, List<VerifyCacheFileElement> elements)
{
#if UNITY_WEBGL
var operation = new VerifyCacheFilesWithoutThreadOperation(cache, elements);
#else
var operation = new VerifyCacheFilesWithThreadOperation(cache, elements);
#endif
return operation;
}
}
/// <summary>
/// 本地缓存文件验证(线程版)
/// </summary>
internal class VerifyCacheFilesWithThreadOperation : VerifyCacheFilesOperation
{
private enum ESteps
{
None,
InitVerify,
UpdateVerify,
Done,
}
private readonly ThreadSyncContext _syncContext = new ThreadSyncContext();
private readonly CacheManager _cache;
private List<VerifyCacheFileElement> _waitingList;
private List<VerifyCacheFileElement> _verifyingList;
private int _verifyMaxNum;
private int _verifyTotalCount;
private float _verifyStartTime;
private int _succeedCount;
private int _failedCount;
private ESteps _steps = ESteps.None;
public VerifyCacheFilesWithThreadOperation(CacheManager cache, List<VerifyCacheFileElement> elements)
{
_cache = cache;
_waitingList = elements;
}
internal override void InternalOnStart()
{
_steps = ESteps.InitVerify;
_verifyStartTime = UnityEngine.Time.realtimeSinceStartup;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.InitVerify)
{
int fileCount = _waitingList.Count;
// 设置同时验证的最大数
ThreadPool.GetMaxThreads(out int workerThreads, out int ioThreads);
YooLogger.Log($"Work threads : {workerThreads}, IO threads : {ioThreads}");
_verifyMaxNum = Math.Min(workerThreads, ioThreads);
_verifyTotalCount = fileCount;
if (_verifyMaxNum < 1)
_verifyMaxNum = 1;
_verifyingList = new List<VerifyCacheFileElement>(_verifyMaxNum);
_steps = ESteps.UpdateVerify;
}
if (_steps == ESteps.UpdateVerify)
{
_syncContext.Update();
Progress = GetProgress();
if (_waitingList.Count == 0 && _verifyingList.Count == 0)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime;
YooLogger.Log($"Verify cache files elapsed time {costTime:f1} seconds");
}
for (int i = _waitingList.Count - 1; i >= 0; i--)
{
if (OperationSystem.IsBusy)
break;
if (_verifyingList.Count >= _verifyMaxNum)
break;
var element = _waitingList[i];
if (BeginVerifyFileWithThread(element))
{
_waitingList.RemoveAt(i);
_verifyingList.Add(element);
}
else
{
YooLogger.Warning("The thread pool is failed queued.");
break;
}
}
}
}
private float GetProgress()
{
if (_verifyTotalCount == 0)
return 1f;
return (float)(_succeedCount + _failedCount) / _verifyTotalCount;
}
private bool BeginVerifyFileWithThread(VerifyCacheFileElement element)
{
return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), element);
}
private void VerifyInThread(object obj)
{
VerifyCacheFileElement element = (VerifyCacheFileElement)obj;
element.Result = CacheHelper.VerifyingCacheFile(element, _cache.BootVerifyLevel);
_syncContext.Post(VerifyCallback, element);
}
private void VerifyCallback(object obj)
{
VerifyCacheFileElement element = (VerifyCacheFileElement)obj;
_verifyingList.Remove(element);
if (element.Result == EVerifyResult.Succeed)
{
_succeedCount++;
var wrapper = new CacheManager.RecordWrapper(element.InfoFilePath, element.DataFilePath, element.DataFileCRC, element.DataFileSize);
_cache.Record(element.CacheGUID, wrapper);
}
else
{
_failedCount++;
YooLogger.Warning($"Failed verify file and delete files : {element.FileRootPath}");
element.DeleteFiles();
}
}
}
/// <summary>
/// 本地缓存文件验证(非线程版)
/// </summary>
internal class VerifyCacheFilesWithoutThreadOperation : VerifyCacheFilesOperation
{
private enum ESteps
{
None,
InitVerify,
UpdateVerify,
Done,
}
private readonly CacheManager _cache;
private List<VerifyCacheFileElement> _waitingList;
private List<VerifyCacheFileElement> _verifyingList;
private int _verifyMaxNum;
private int _verifyTotalCount;
private float _verifyStartTime;
private int _succeedCount;
private int _failedCount;
private ESteps _steps = ESteps.None;
public VerifyCacheFilesWithoutThreadOperation(CacheManager cache, List<VerifyCacheFileElement> elements)
{
_cache = cache;
_waitingList = elements;
}
internal override void InternalOnStart()
{
_steps = ESteps.InitVerify;
_verifyStartTime = UnityEngine.Time.realtimeSinceStartup;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.InitVerify)
{
int fileCount = _waitingList.Count;
// 设置同时验证的最大数
_verifyMaxNum = fileCount;
_verifyTotalCount = fileCount;
_verifyingList = new List<VerifyCacheFileElement>(_verifyMaxNum);
_steps = ESteps.UpdateVerify;
}
if (_steps == ESteps.UpdateVerify)
{
Progress = GetProgress();
if (_waitingList.Count == 0 && _verifyingList.Count == 0)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime;
YooLogger.Log($"Package verify elapsed time {costTime:f1} seconds");
}
for (int i = _waitingList.Count - 1; i >= 0; i--)
{
if (OperationSystem.IsBusy)
break;
if (_verifyingList.Count >= _verifyMaxNum)
break;
var element = _waitingList[i];
BeginVerifyFileWithoutThread(element);
_waitingList.RemoveAt(i);
_verifyingList.Add(element);
}
// 主线程内验证,可以清空列表
_verifyingList.Clear();
}
}
private float GetProgress()
{
if (_verifyTotalCount == 0)
return 1f;
return (float)(_succeedCount + _failedCount) / _verifyTotalCount;
}
private void BeginVerifyFileWithoutThread(VerifyCacheFileElement element)
{
element.Result = CacheHelper.VerifyingCacheFile(element, _cache.BootVerifyLevel);
if (element.Result == EVerifyResult.Succeed)
{
_succeedCount++;
var wrapper = new CacheManager.RecordWrapper(element.InfoFilePath, element.DataFilePath, element.DataFileCRC, element.DataFileSize);
_cache.Record(element.CacheGUID, wrapper);
}
else
{
_failedCount++;
YooLogger.Warning($"Failed verify file and delete files : {element.FileRootPath}");
element.DeleteFiles();
}
}
}
}

View File

@ -1,141 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace YooAsset
{
internal abstract class VerifyTempFileOperation : AsyncOperationBase
{
public EVerifyResult VerifyResult { protected set; get; }
public static VerifyTempFileOperation CreateOperation(VerifyTempFileElement element)
{
#if UNITY_WEBGL
var operation = new VerifyTempFileWithoutThreadOperation(element);
#else
var operation = new VerifyTempFileWithThreadOperation(element);
#endif
return operation;
}
}
/// <summary>
/// 下载文件验证(线程版)
/// </summary>
internal class VerifyTempFileWithThreadOperation : VerifyTempFileOperation
{
private enum ESteps
{
None,
VerifyFile,
Waiting,
Done,
}
private readonly VerifyTempFileElement _element;
private ESteps _steps = ESteps.None;
public VerifyTempFileWithThreadOperation(VerifyTempFileElement element)
{
_element = element;
}
internal override void InternalOnStart()
{
_steps = ESteps.VerifyFile;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.VerifyFile)
{
if (BeginVerifyFileWithThread(_element))
{
_steps = ESteps.Waiting;
}
}
if (_steps == ESteps.Waiting)
{
int result = _element.Result;
if (result == 0)
return;
VerifyResult = (EVerifyResult)result;
if (VerifyResult == EVerifyResult.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed verify file : {_element.TempDataFilePath} ! ErrorCode : {VerifyResult}";
}
}
}
private bool BeginVerifyFileWithThread(VerifyTempFileElement element)
{
return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), element);
}
private void VerifyInThread(object obj)
{
VerifyTempFileElement element = (VerifyTempFileElement)obj;
int result = (int)CacheHelper.VerifyingTempFile(element);
element.Result = result;
}
}
/// <summary>
/// 下载文件验证(非线程版)
/// </summary>
internal class VerifyTempFileWithoutThreadOperation : VerifyTempFileOperation
{
private enum ESteps
{
None,
VerifyFile,
Done,
}
private readonly VerifyTempFileElement _element;
private ESteps _steps = ESteps.None;
public VerifyTempFileWithoutThreadOperation(VerifyTempFileElement element)
{
_element = element;
}
internal override void InternalOnStart()
{
_steps = ESteps.VerifyFile;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.VerifyFile)
{
_element.Result = (int)CacheHelper.VerifyingTempFile(_element);
VerifyResult = (EVerifyResult)_element.Result;
if (VerifyResult == EVerifyResult.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed verify file : {_element.TempDataFilePath} ! ErrorCode : {VerifyResult}";
}
}
}
}
}

View File

@ -1,74 +0,0 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
internal class PackageCachingOperation : AsyncOperationBase
{
private enum ESteps
{
None,
FindCacheFiles,
VerifyCacheFiles,
Done,
}
private readonly PersistentManager _persistent;
private readonly CacheManager _cache;
private FindCacheFilesOperation _findCacheFilesOp;
private VerifyCacheFilesOperation _verifyCacheFilesOp;
private ESteps _steps = ESteps.None;
public PackageCachingOperation(PersistentManager persistent, CacheManager cache)
{
_persistent = persistent;
_cache = cache;
}
internal override void InternalOnStart()
{
_steps = ESteps.FindCacheFiles;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.FindCacheFiles)
{
if (_findCacheFilesOp == null)
{
_findCacheFilesOp = new FindCacheFilesOperation(_persistent, _cache);
OperationSystem.StartOperation(_cache.PackageName, _findCacheFilesOp);
}
Progress = _findCacheFilesOp.Progress;
if (_findCacheFilesOp.IsDone == false)
return;
_steps = ESteps.VerifyCacheFiles;
}
if (_steps == ESteps.VerifyCacheFiles)
{
if (_verifyCacheFilesOp == null)
{
_verifyCacheFilesOp = VerifyCacheFilesOperation.CreateOperation(_cache, _findCacheFilesOp.VerifyElements);
OperationSystem.StartOperation(_cache.PackageName, _verifyCacheFilesOp);
}
Progress = _verifyCacheFilesOp.Progress;
if (_verifyCacheFilesOp.IsDone == false)
return;
// 注意:总是返回成功
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
int totalCount = _cache.GetAllCachedFilesCount();
YooLogger.Log($"Package '{_cache.PackageName}' cached files count : {totalCount}");
}
}
}
}

View File

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

View File

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

View File

@ -1,217 +0,0 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
internal class PersistentManager
{
private readonly Dictionary<string, string> _cachedDataFilePaths = new Dictionary<string, string>(10000);
private readonly Dictionary<string, string> _cachedInfoFilePaths = new Dictionary<string, string>(10000);
private readonly Dictionary<string, string> _tempDataFilePaths = new Dictionary<string, string>(10000);
private readonly Dictionary<string, string> _buildinFilePaths = new Dictionary<string, string>(10000);
/// <summary>
/// 所属包裹
/// </summary>
public readonly string PackageName;
public string BuildinRoot { private set; get; }
public string BuildinPackageRoot { private set; get; }
public string SandboxRoot { private set; get; }
public string SandboxPackageRoot { private set; get; }
public string SandboxCacheFilesRoot { private set; get; }
public string SandboxManifestFilesRoot { private set; get; }
public string SandboxAppFootPrintFilePath { private set; get; }
public bool AppendFileExtension { private set; get; }
public PersistentManager(string packageName)
{
PackageName = packageName;
}
/// <summary>
/// 初始化
/// </summary>
public void Initialize(string buildinRoot, string sandboxRoot, bool appendFileExtension)
{
if (string.IsNullOrEmpty(buildinRoot))
BuildinRoot = CreateDefaultBuildinRoot();
else
BuildinRoot = buildinRoot;
if (string.IsNullOrEmpty(sandboxRoot))
SandboxRoot = CreateDefaultSandboxRoot();
else
SandboxRoot = sandboxRoot;
BuildinPackageRoot = PathUtility.Combine(BuildinRoot, PackageName);
SandboxPackageRoot = PathUtility.Combine(SandboxRoot, PackageName);
SandboxCacheFilesRoot = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.CacheFilesFolderName);
SandboxManifestFilesRoot = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.ManifestFolderName);
SandboxAppFootPrintFilePath = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.AppFootPrintFileName);
AppendFileExtension = appendFileExtension;
}
private static string CreateDefaultBuildinRoot()
{
string path = PathUtility.Combine(UnityEngine.Application.streamingAssetsPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#if UNITY_OPENHARMONY
return $"file://{path}";
#else
return path;
#endif
}
private static string CreateDefaultSandboxRoot()
{
#if UNITY_EDITOR
// 注意:为了方便调试查看,编辑器下把存储目录放到项目里。
string projectPath = Path.GetDirectoryName(UnityEngine.Application.dataPath);
projectPath = PathUtility.RegularPath(projectPath);
return PathUtility.Combine(projectPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#elif UNITY_STANDALONE
return PathUtility.Combine(UnityEngine.Application.dataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#else
return PathUtility.Combine(UnityEngine.Application.persistentDataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#endif
}
public string GetCachedDataFilePath(PackageBundle bundle)
{
if (_cachedDataFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false)
{
string folderName = bundle.FileHash.Substring(0, 2);
filePath = PathUtility.Combine(SandboxCacheFilesRoot, folderName, bundle.CacheGUID, YooAssetSettings.CacheBundleDataFileName);
if (AppendFileExtension)
filePath += bundle.FileExtension;
_cachedDataFilePaths.Add(bundle.CacheGUID, filePath);
}
return filePath;
}
public string GetCachedInfoFilePath(PackageBundle bundle)
{
if (_cachedInfoFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false)
{
string folderName = bundle.FileHash.Substring(0, 2);
filePath = PathUtility.Combine(SandboxCacheFilesRoot, folderName, bundle.CacheGUID, YooAssetSettings.CacheBundleInfoFileName);
_cachedInfoFilePaths.Add(bundle.CacheGUID, filePath);
}
return filePath;
}
public string GetTempDataFilePath(PackageBundle bundle)
{
if (_tempDataFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false)
{
string cachedDataFilePath = GetCachedDataFilePath(bundle);
filePath = $"{cachedDataFilePath}.temp";
_tempDataFilePaths.Add(bundle.CacheGUID, filePath);
}
return filePath;
}
public string GetBuildinFilePath(PackageBundle bundle)
{
if (_buildinFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false)
{
filePath = PathUtility.Combine(BuildinPackageRoot, bundle.FileName);
_buildinFilePaths.Add(bundle.CacheGUID, filePath);
}
return filePath;
}
/// <summary>
/// 删除沙盒里的包裹目录
/// </summary>
public void DeleteSandboxPackageFolder()
{
if (Directory.Exists(SandboxPackageRoot))
Directory.Delete(SandboxPackageRoot, true);
}
/// <summary>
/// 删除沙盒内的缓存文件夹
/// </summary>
public void DeleteSandboxCacheFilesFolder()
{
if (Directory.Exists(SandboxCacheFilesRoot))
Directory.Delete(SandboxCacheFilesRoot, true);
}
/// <summary>
/// 删除沙盒内的清单文件夹
/// </summary>
public void DeleteSandboxManifestFilesFolder()
{
if (Directory.Exists(SandboxManifestFilesRoot))
Directory.Delete(SandboxManifestFilesRoot, true);
}
/// <summary>
/// 获取沙盒内包裹的清单文件的路径
/// </summary>
public string GetSandboxPackageManifestFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion);
return PathUtility.Combine(SandboxManifestFilesRoot, fileName);
}
/// <summary>
/// 获取沙盒内包裹的哈希文件的路径
/// </summary>
public string GetSandboxPackageHashFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion);
return PathUtility.Combine(SandboxManifestFilesRoot, fileName);
}
/// <summary>
/// 获取沙盒内包裹的版本文件的路径
/// </summary>
public string GetSandboxPackageVersionFilePath()
{
string fileName = YooAssetSettingsData.GetPackageVersionFileName(PackageName);
return PathUtility.Combine(SandboxManifestFilesRoot, fileName);
}
/// <summary>
/// 保存沙盒内默认的包裹版本
/// </summary>
public void SaveSandboxPackageVersionFile(string version)
{
YooLogger.Log($"Save package version : {version}");
string filePath = GetSandboxPackageVersionFilePath();
FileUtility.WriteAllText(filePath, version);
}
/// <summary>
/// 获取APP内包裹的清单文件的路径
/// </summary>
public string GetBuildinPackageManifestFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion);
return PathUtility.Combine(BuildinPackageRoot, fileName);
}
/// <summary>
/// 获取APP内包裹的哈希文件的路径
/// </summary>
public string GetBuildinPackageHashFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion);
return PathUtility.Combine(BuildinPackageRoot, fileName);
}
/// <summary>
/// 获取APP内包裹的版本文件的路径
/// </summary>
public string GetBuildinPackageVersionFilePath()
{
string fileName = YooAssetSettingsData.GetPackageVersionFileName(PackageName);
return PathUtility.Combine(BuildinPackageRoot, fileName);
}
}
}

View File

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

View File

@ -1,60 +0,0 @@
using System.IO;
namespace YooAsset
{
/// <summary>
/// 缓存文件验证元素
/// </summary>
internal class VerifyCacheFileElement
{
public string PackageName { private set; get; }
public string CacheGUID { private set; get; }
public string FileRootPath { private set; get; }
public string DataFilePath { private set; get; }
public string InfoFilePath { private set; get; }
public EVerifyResult Result;
public string DataFileCRC;
public long DataFileSize;
public VerifyCacheFileElement(string packageName, string cacheGUID, string fileRootPath, string dataFilePath, string infoFilePath)
{
PackageName = packageName;
CacheGUID = cacheGUID;
FileRootPath = fileRootPath;
DataFilePath = dataFilePath;
InfoFilePath = infoFilePath;
}
public void DeleteFiles()
{
try
{
Directory.Delete(FileRootPath, true);
}
catch (System.Exception e)
{
YooLogger.Warning($"Failed delete cache bundle folder : {e}");
}
}
}
/// <summary>
/// 下载文件验证元素
/// </summary>
internal class VerifyTempFileElement
{
public string TempDataFilePath { private set; get; }
public string FileCRC { private set; get; }
public long FileSize { private set; get; }
public int Result = 0; // 注意:原子操作对象
public VerifyTempFileElement(string tempDataFilePath, string fileCRC, long fileSize)
{
TempDataFilePath = tempDataFilePath;
FileCRC = fileCRC;
FileSize = fileSize;
}
}
}

View File

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

View File

@ -1,41 +0,0 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine.Networking;
namespace YooAsset
{
/// <summary>
/// 自定义下载器的请求委托
/// </summary>
public delegate UnityWebRequest DownloadRequestDelegate(string url);
internal static class DownloadHelper
{
/// <summary>
/// 下载失败后清理文件的HTTP错误码
/// </summary>
public static List<long> ClearFileResponseCodes { set; get; }
/// <summary>
/// 自定义下载器的请求委托
/// </summary>
public static DownloadRequestDelegate RequestDelegate = null;
/// <summary>
/// 创建一个新的网络请求
/// </summary>
public static UnityWebRequest NewRequest(string requestURL)
{
UnityWebRequest webRequest;
if (RequestDelegate != null)
webRequest = RequestDelegate.Invoke(requestURL);
else
webRequest = new UnityWebRequest(requestURL, UnityWebRequest.kHttpVerbGET);
return webRequest;
}
}
}

View File

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

View File

@ -1,150 +0,0 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
namespace YooAsset
{
/// <summary>
/// 1. 保证每一时刻资源文件只存在一个下载器
/// 2. 保证下载器下载完成后立刻验证并缓存
/// 3. 保证资源文件不会被重复下载
/// </summary>
internal class DownloadManager
{
private readonly Dictionary<string, DownloaderBase> _downloaders = new Dictionary<string, DownloaderBase>(1000);
private readonly List<string> _removeList = new List<string>(1000);
private uint _breakpointResumeFileSize;
/// <summary>
/// 所属包裹
/// </summary>
public readonly string PackageName;
public DownloadManager(string packageName)
{
PackageName = packageName;
}
/// <summary>
/// 初始化
/// </summary>
public void Initialize(uint breakpointResumeFileSize)
{
_breakpointResumeFileSize = breakpointResumeFileSize;
}
/// <summary>
/// 更新下载器
/// </summary>
public void Update()
{
// 更新下载器
_removeList.Clear();
foreach (var valuePair in _downloaders)
{
var downloader = valuePair.Value;
downloader.Update();
if (downloader.IsDone())
{
_removeList.Add(valuePair.Key);
}
}
// 移除下载器
foreach (var key in _removeList)
{
_downloaders.Remove(key);
}
}
/// <summary>
/// 销毁所有下载器
/// </summary>
public void DestroyAll()
{
foreach (var valuePair in _downloaders)
{
var downloader = valuePair.Value;
downloader.Abort();
}
_downloaders.Clear();
_removeList.Clear();
}
/// <summary>
/// 创建下载器
/// 注意:只有第一次请求的参数才有效
/// </summary>
public DownloaderBase CreateDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60)
{
// 查询存在的下载器
if (_downloaders.TryGetValue(bundleInfo.CachedDataFilePath, out var downloader))
{
downloader.Reference();
return downloader;
}
// 如果资源已经缓存
if (bundleInfo.IsCached())
{
var completedDownloader = new CompletedDownloader(bundleInfo);
return completedDownloader;
}
// 创建新的下载器
DownloaderBase newDownloader = null;
YooLogger.Log($"Beginning to download bundle : {bundleInfo.Bundle.BundleName} URL : {bundleInfo.RemoteMainURL}");
#if UNITY_WEBGL
if (bundleInfo.Bundle.Buildpipeline == EDefaultBuildPipeline.RawFileBuildPipeline.ToString())
{
FileUtility.CreateFileDirectory(bundleInfo.CachedDataFilePath);
System.Type requesterType = typeof(FileGeneralRequest);
newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
}
else
{
System.Type requesterType = typeof(AssetBundleWebRequest);
newDownloader = new WebDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
}
#else
FileUtility.CreateFileDirectory(bundleInfo.CachedDataFilePath);
bool resumeDownload = bundleInfo.Bundle.FileSize >= _breakpointResumeFileSize;
if (resumeDownload)
{
System.Type requesterType = typeof(FileResumeRequest);
newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
}
else
{
System.Type requesterType = typeof(FileGeneralRequest);
newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
}
#endif
// 返回新创建的下载器
_downloaders.Add(bundleInfo.CachedDataFilePath, newDownloader);
newDownloader.Reference();
return newDownloader;
}
/// <summary>
/// 停止不再使用的下载器
/// </summary>
public void AbortUnusedDownloader()
{
foreach (var valuePair in _downloaders)
{
var downloader = valuePair.Value;
if (downloader.RefCount <= 0)
{
downloader.Abort();
}
}
}
}
}

View File

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

View File

@ -1,8 +1,25 @@

using UnityEngine.Networking;
namespace YooAsset
{
internal static class PersistentHelper
/// <summary>
/// 自定义下载器的请求委托
/// </summary>
public delegate UnityWebRequest UnityWebRequestDelegate(string url);
internal class DownloadSystemHelper
{
public static UnityWebRequestDelegate UnityWebRequestCreater = null;
public static UnityWebRequest NewUnityWebRequestGet(string requestURL)
{
UnityWebRequest webRequest;
if (UnityWebRequestCreater != null)
webRequest = UnityWebRequestCreater.Invoke(requestURL);
else
webRequest = new UnityWebRequest(requestURL, UnityWebRequest.kHttpVerbGET);
return webRequest;
}
/// <summary>
/// 获取WWW加载本地资源的路径
/// </summary>

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 02ad351eb8539da47a0c789e2f8c468f
guid: 5bacfa8c42e283a45a2d21cbb93d6e5d
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -1,28 +0,0 @@
using UnityEngine;
namespace YooAsset
{
internal sealed class CompletedDownloader : DownloaderBase
{
public CompletedDownloader(BundleInfo bundleInfo) : base(bundleInfo, null, 0, 0)
{
DownloadProgress = 1f;
DownloadedBytes = (ulong)bundleInfo.Bundle.FileSize;
_status = EStatus.Succeed;
}
public override void SendRequest(params object[] param)
{
}
public override void Update()
{
}
public override void Abort()
{
}
public override AssetBundle GetAssetBundle()
{
throw new System.NotImplementedException();
}
}
}

View File

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

View File

@ -1,182 +0,0 @@
using System;
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
internal abstract class DownloaderBase
{
public enum EStatus
{
None = 0,
Succeed,
Failed
}
protected readonly BundleInfo _bundleInfo;
protected readonly System.Type _requesterType;
protected readonly int _timeout;
protected int _failedTryAgain;
protected IWebRequester _requester;
protected EStatus _status = EStatus.None;
protected string _lastestNetError = string.Empty;
protected long _lastestHttpCode = 0;
// 请求次数
protected int _requestCount = 0;
protected string _requestURL;
// 超时相关
protected bool _isAbort = false;
protected ulong _latestDownloadBytes;
protected float _latestDownloadRealtime;
protected float _tryAgainTimer;
/// <summary>
/// 是否等待异步结束
/// 警告只能用于解压APP内部资源
/// </summary>
public bool WaitForAsyncComplete = false;
/// <summary>
/// 下载进度0f~1f
/// </summary>
public float DownloadProgress { protected set; get; }
/// <summary>
/// 已经下载的总字节数
/// </summary>
public ulong DownloadedBytes { protected set; get; }
/// <summary>
/// 引用计数
/// </summary>
public int RefCount { private set; get; }
public DownloaderBase(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout)
{
_bundleInfo = bundleInfo;
_requesterType = requesterType;
_failedTryAgain = failedTryAgain;
_timeout = timeout;
}
public abstract void SendRequest(params object[] args);
public abstract void Update();
public abstract void Abort();
public abstract AssetBundle GetAssetBundle();
/// <summary>
/// 引用(引用计数递加)
/// </summary>
public void Reference()
{
RefCount++;
}
/// <summary>
/// 释放(引用计数递减)
/// </summary>
public void Release()
{
RefCount--;
}
/// <summary>
/// 检测下载器是否已经完成(无论成功或失败)
/// </summary>
public bool IsDone()
{
return _status == EStatus.Succeed || _status == EStatus.Failed;
}
/// <summary>
/// 下载过程是否发生错误
/// </summary>
public bool HasError()
{
return _status == EStatus.Failed;
}
/// <summary>
/// 按照错误级别打印错误
/// </summary>
public void ReportError()
{
YooLogger.Error(GetLastError());
}
/// <summary>
/// 按照警告级别打印错误
/// </summary>
public void ReportWarning()
{
YooLogger.Warning(GetLastError());
}
/// <summary>
/// 获取最近发生的错误信息
/// </summary>
public string GetLastError()
{
return $"Failed to download : {_requestURL} Error : {_lastestNetError} Code : {_lastestHttpCode}";
}
/// <summary>
/// 获取下载文件的大小
/// </summary>
/// <returns></returns>
public long GetDownloadFileSize()
{
return _bundleInfo.Bundle.FileSize;
}
/// <summary>
/// 获取下载的资源包名称
/// </summary>
public string GetDownloadBundleName()
{
return _bundleInfo.Bundle.BundleName;
}
/// <summary>
/// 获取网络请求地址
/// </summary>
protected string GetRequestURL()
{
// 轮流返回请求地址
_requestCount++;
if (_requestCount % 2 == 0)
return _bundleInfo.RemoteFallbackURL;
else
return _bundleInfo.RemoteMainURL;
}
/// <summary>
/// 超时判定方法
/// </summary>
protected void CheckTimeout()
{
// 注意:在连续时间段内无新增下载数据及判定为超时
if (_isAbort == false)
{
if (_latestDownloadBytes != DownloadedBytes)
{
_latestDownloadBytes = DownloadedBytes;
_latestDownloadRealtime = Time.realtimeSinceStartup;
}
float offset = Time.realtimeSinceStartup - _latestDownloadRealtime;
if (offset > _timeout)
{
YooLogger.Warning($"Web file request timeout : {_requestURL}");
if (_requester != null)
_requester.Abort();
_isAbort = true;
}
}
}
}
}

View File

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

View File

@ -1,216 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 文件下载器
/// </summary>
internal sealed class FileDownloader : DownloaderBase
{
private enum ESteps
{
None,
PrepareDownload,
CreateDownloader,
CheckDownload,
VerifyTempFile,
WaitingVerifyTempFile,
CachingFile,
TryAgain,
Done,
}
private VerifyTempFileOperation _verifyFileOp = null;
private ESteps _steps = ESteps.None;
public FileDownloader(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout) : base(bundleInfo, requesterType, failedTryAgain, timeout)
{
}
public override void SendRequest(params object[] args)
{
if (_steps == ESteps.None)
{
_steps = ESteps.PrepareDownload;
}
}
public override void Update()
{
if (_steps == ESteps.None)
return;
if (IsDone())
return;
// 准备下载
if (_steps == ESteps.PrepareDownload)
{
// 获取请求地址
_requestURL = GetRequestURL();
// 重置变量
DownloadProgress = 0f;
DownloadedBytes = 0;
// 重置变量
_isAbort = false;
_latestDownloadBytes = 0;
_latestDownloadRealtime = Time.realtimeSinceStartup;
// 重置计时器
if (_tryAgainTimer > 0f)
YooLogger.Warning($"Try again download : {_requestURL}");
_tryAgainTimer = 0f;
_steps = ESteps.CreateDownloader;
}
// 创建下载器
if (_steps == ESteps.CreateDownloader)
{
_requester = (IWebRequester)Activator.CreateInstance(_requesterType);
_requester.Create(_requestURL, _bundleInfo);
_steps = ESteps.CheckDownload;
}
// 检测下载结果
if (_steps == ESteps.CheckDownload)
{
_requester.Update();
DownloadedBytes = _requester.DownloadedBytes;
DownloadProgress = _requester.DownloadProgress;
if (_requester.IsDone() == false)
{
CheckTimeout();
return;
}
_lastestNetError = _requester.RequestNetError;
_lastestHttpCode = _requester.RequestHttpCode;
if (_requester.Status != ERequestStatus.Success)
{
_steps = ESteps.TryAgain;
}
else
{
_steps = ESteps.VerifyTempFile;
}
}
// 验证下载文件
if (_steps == ESteps.VerifyTempFile)
{
VerifyTempFileElement element = new VerifyTempFileElement(_bundleInfo.TempDataFilePath, _bundleInfo.Bundle.FileCRC, _bundleInfo.Bundle.FileSize);
_verifyFileOp = VerifyTempFileOperation.CreateOperation(element);
OperationSystem.StartOperation(_bundleInfo.Bundle.PackageName, _verifyFileOp);
_steps = ESteps.WaitingVerifyTempFile;
}
// 等待验证完成
if (_steps == ESteps.WaitingVerifyTempFile)
{
if (WaitForAsyncComplete)
_verifyFileOp.InternalOnUpdate();
if (_verifyFileOp.IsDone == false)
return;
if (_verifyFileOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.CachingFile;
}
else
{
string tempFilePath = _bundleInfo.TempDataFilePath;
if (File.Exists(tempFilePath))
File.Delete(tempFilePath);
_lastestNetError = _verifyFileOp.Error;
_steps = ESteps.TryAgain;
}
}
// 缓存下载文件
if (_steps == ESteps.CachingFile)
{
try
{
CachingFile();
_status = EStatus.Succeed;
_steps = ESteps.Done;
}
catch (Exception e)
{
_lastestNetError = e.Message;
_steps = ESteps.TryAgain;
}
}
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
if (_failedTryAgain <= 0)
{
ReportError();
_status = EStatus.Failed;
_steps = ESteps.Done;
return;
}
_tryAgainTimer += Time.unscaledDeltaTime;
if (_tryAgainTimer > 1f)
{
_failedTryAgain--;
_steps = ESteps.PrepareDownload;
ReportWarning();
}
}
}
public override void Abort()
{
if (_requester != null)
_requester.Abort();
if (IsDone() == false)
{
_status = EStatus.Failed;
_steps = ESteps.Done;
_lastestNetError = "user abort";
_lastestHttpCode = 0;
}
}
public override AssetBundle GetAssetBundle()
{
throw new NotImplementedException();
}
/// <summary>
/// 缓存下载文件
/// </summary>
private void CachingFile()
{
string tempFilePath = _bundleInfo.TempDataFilePath;
string infoFilePath = _bundleInfo.CachedInfoFilePath;
string dataFilePath = _bundleInfo.CachedDataFilePath;
string dataFileCRC = _bundleInfo.Bundle.FileCRC;
long dataFileSize = _bundleInfo.Bundle.FileSize;
if (File.Exists(infoFilePath))
File.Delete(infoFilePath);
if (File.Exists(dataFilePath))
File.Delete(dataFilePath);
// 移动临时文件路径
FileInfo fileInfo = new FileInfo(tempFilePath);
fileInfo.MoveTo(dataFilePath);
// 写入信息文件记录验证数据
CacheHelper.WriteInfoToFile(infoFilePath, dataFileCRC, dataFileSize);
// 记录缓存文件
_bundleInfo.CacheRecord();
}
}
}

View File

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

View File

@ -1,140 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace YooAsset
{
internal sealed class WebDownloader : DownloaderBase
{
private enum ESteps
{
None,
PrepareDownload,
CreateDownloader,
CheckDownload,
TryAgain,
Done,
}
private ESteps _steps = ESteps.None;
private bool _getAssetBundle = false;
public WebDownloader(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout) : base(bundleInfo, requesterType, failedTryAgain, timeout)
{
}
public override void SendRequest(params object[] args)
{
if (_steps == ESteps.None)
{
if (args.Length > 0)
{
_getAssetBundle = (bool)args[0];
}
_steps = ESteps.PrepareDownload;
}
}
public override void Update()
{
if (_steps == ESteps.None)
return;
if (IsDone())
return;
// 创建下载器
if (_steps == ESteps.PrepareDownload)
{
// 获取请求地址
_requestURL = GetRequestURL();
// 重置变量
DownloadProgress = 0f;
DownloadedBytes = 0;
// 重置变量
_isAbort = false;
_latestDownloadBytes = 0;
_latestDownloadRealtime = Time.realtimeSinceStartup;
// 重置计时器
if (_tryAgainTimer > 0f)
YooLogger.Warning($"Try again download : {_requestURL}");
_tryAgainTimer = 0f;
_steps = ESteps.CreateDownloader;
}
// 创建下载器
if (_steps == ESteps.CreateDownloader)
{
_requester = (IWebRequester)Activator.CreateInstance(_requesterType);
_requester.Create(_requestURL, _bundleInfo, _getAssetBundle);
_steps = ESteps.CheckDownload;
}
// 检测下载结果
if (_steps == ESteps.CheckDownload)
{
_requester.Update();
DownloadedBytes = _requester.DownloadedBytes;
DownloadProgress = _requester.DownloadProgress;
if (_requester.IsDone() == false)
{
CheckTimeout();
return;
}
_lastestNetError = _requester.RequestNetError;
_lastestHttpCode = _requester.RequestHttpCode;
if (_requester.Status != ERequestStatus.Success)
{
_steps = ESteps.TryAgain;
}
else
{
_status = EStatus.Succeed;
_steps = ESteps.Done;
}
}
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
if (_failedTryAgain <= 0)
{
ReportError();
_status = EStatus.Failed;
_steps = ESteps.Done;
return;
}
_tryAgainTimer += Time.unscaledDeltaTime;
if (_tryAgainTimer > 1f)
{
_failedTryAgain--;
_steps = ESteps.PrepareDownload;
ReportWarning();
YooLogger.Warning($"Try again download : {_requestURL}");
}
}
}
public override void Abort()
{
if (_requester != null)
_requester.Abort();
if (IsDone() == false)
{
_status = EStatus.Failed;
_steps = ESteps.Done;
_lastestNetError = "user abort";
_lastestHttpCode = 0;
}
}
public override AssetBundle GetAssetBundle()
{
return (AssetBundle)_requester.GetRequestObject();
}
}
}

View File

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

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d29a9623b2b346e439b7a7e37fafa781
guid: a8f060786f8775b4a82cc3f55d9135e2
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: a93a516506b2b5c4492fdefe26eb1175
guid: 4630dac2050606043bb146325fdce6ad
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@ -0,0 +1,77 @@
using UnityEngine.Networking;
using UnityEngine;
namespace YooAsset
{
internal class UnityWebDataRequestOperation : UnityWebRequestOperation
{
private UnityWebRequestAsyncOperation _requestOperation;
/// <summary>
/// 请求结果
/// </summary>
public byte[] Result { private set; get; }
internal UnityWebDataRequestOperation(string url, int timeout = 60) : base(url, timeout)
{
}
internal override void InternalOnStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CreateRequest)
{
_latestDownloadBytes = 0;
_latestDownloadRealtime = Time.realtimeSinceStartup;
CreateWebRequest();
_steps = ESteps.Download;
}
if (_steps == ESteps.Download)
{
Progress = _requestOperation.progress;
if (_requestOperation.isDone == false)
{
CheckRequestTimeout();
return;
}
if (CheckRequestResult())
{
_steps = ESteps.Done;
Result = _webRequest.downloadHandler.data;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
}
// 注意:最终释放请求器
DisposeRequest();
}
}
internal override void InternalOnAbort()
{
_steps = ESteps.Done;
DisposeRequest();
}
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
DownloadHandlerBuffer handler = new DownloadHandlerBuffer();
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_requestOperation = _webRequest.SendWebRequest();
}
}
}

View File

@ -0,0 +1,73 @@
using UnityEngine.Networking;
using UnityEngine;
namespace YooAsset
{
internal class UnityWebFileRequestOperation : UnityWebRequestOperation
{
private UnityWebRequestAsyncOperation _requestOperation;
private readonly string _fileSavePath;
internal UnityWebFileRequestOperation(string url, string fileSavePath, int timeout = 60) : base(url, timeout)
{
_fileSavePath = fileSavePath;
}
internal override void InternalOnStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CreateRequest)
{
_latestDownloadBytes = 0;
_latestDownloadRealtime = Time.realtimeSinceStartup;
CreateWebRequest();
_steps = ESteps.Download;
}
if (_steps == ESteps.Download)
{
Progress = _requestOperation.progress;
if (_requestOperation.isDone == false)
{
CheckRequestTimeout();
return;
}
if (CheckRequestResult())
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
}
// 注意:最终释放请求器
DisposeRequest();
}
}
internal override void InternalOnAbort()
{
_steps = ESteps.Done;
DisposeRequest();
}
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
DownloadHandlerFile handler = new DownloadHandlerFile(_fileSavePath);
handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_requestOperation = _webRequest.SendWebRequest();
}
}
}

View File

@ -6,94 +6,53 @@ using UnityEngine;
namespace YooAsset
{
internal abstract class UnityWebRequesterBase
internal abstract class UnityWebRequestOperation : AsyncOperationBase
{
protected enum ESteps
{
None,
CreateRequest,
Download,
Done,
}
protected UnityWebRequest _webRequest;
protected UnityWebRequestAsyncOperation _operationHandle;
protected readonly string _requestURL;
protected ESteps _steps = ESteps.None;
// 超时相关
private float _timeout;
protected readonly float _timeout;
protected ulong _latestDownloadBytes;
protected float _latestDownloadRealtime;
private bool _isAbort = false;
private ulong _latestDownloadBytes;
private float _latestDownloadRealtime;
/// <summary>
/// 请求URL地址
/// </summary>
public string URL { protected set; get; }
protected void ResetTimeout(float timeout)
public string URL
{
get { return _requestURL; }
}
internal UnityWebRequestOperation(string url, int timeout)
{
_requestURL = url;
_timeout = timeout;
_latestDownloadBytes = 0;
_latestDownloadRealtime = Time.realtimeSinceStartup;
}
/// <summary>
/// 释放下载器
/// </summary>
public void Dispose()
protected void DisposeRequest()
{
if (_webRequest != null)
{
_webRequest.Dispose();
_webRequest = null;
_operationHandle = null;
}
}
/// <summary>
/// 是否完毕(无论成功失败)
/// </summary>
public bool IsDone()
{
if (_operationHandle == null)
return false;
return _operationHandle.isDone;
}
/// <summary>
/// 下载进度
/// </summary>
public float Progress()
{
if (_operationHandle == null)
return 0;
return _operationHandle.progress;
}
/// <summary>
/// 下载是否发生错误
/// </summary>
public bool HasError()
{
#if UNITY_2020_3_OR_NEWER
return _webRequest.result != UnityWebRequest.Result.Success;
#else
if (_webRequest.isNetworkError || _webRequest.isHttpError)
return true;
else
return false;
#endif
}
/// <summary>
/// 获取错误信息
/// </summary>
public string GetError()
{
if (_webRequest != null)
{
return $"URL : {URL} Error : {_webRequest.error}";
}
return string.Empty;
}
/// <summary>
/// 检测超时
/// </summary>
public void CheckTimeout()
protected void CheckRequestTimeout()
{
// 注意:在连续时间段内无新增下载数据及判定为超时
if (_isAbort == false)
@ -112,5 +71,33 @@ namespace YooAsset
}
}
}
/// <summary>
/// 检测请求结果
/// </summary>
protected bool CheckRequestResult()
{
#if UNITY_2020_3_OR_NEWER
if (_webRequest.result != UnityWebRequest.Result.Success)
{
Error = $"URL : {_requestURL} Error : {_webRequest.error}";
return false;
}
else
{
return true;
}
#else
if (_webRequest.isNetworkError || _webRequest.isHttpError)
{
Error = $"URL : {_requestURL} Error : {_webRequest.error}";
return false;
}
else
{
return true;
}
#endif
}
}
}

View File

@ -0,0 +1,77 @@
using UnityEngine.Networking;
using UnityEngine;
namespace YooAsset
{
internal class UnityWebTextRequestOperation : UnityWebRequestOperation
{
private UnityWebRequestAsyncOperation _requestOperation;
/// <summary>
/// 请求结果
/// </summary>
public string Result { private set; get; }
internal UnityWebTextRequestOperation(string url, int timeout = 60) : base(url, timeout)
{
}
internal override void InternalOnStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CreateRequest)
{
_latestDownloadBytes = 0;
_latestDownloadRealtime = Time.realtimeSinceStartup;
CreateWebRequest();
_steps = ESteps.Download;
}
if (_steps == ESteps.Download)
{
Progress = _requestOperation.progress;
if (_requestOperation.isDone == false)
{
CheckRequestTimeout();
return;
}
if (CheckRequestResult())
{
_steps = ESteps.Done;
Result = _webRequest.downloadHandler.text;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
}
// 注意:最终释放请求器
DisposeRequest();
}
}
internal override void InternalOnAbort()
{
_steps = ESteps.Done;
DisposeRequest();
}
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
DownloadHandlerBuffer handler = new DownloadHandlerBuffer();
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_requestOperation = _webRequest.SendWebRequest();
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 8616c7550a7890141af598898a12df1b
guid: a488de5dcd6f4c448a47c4b574d5c9bc
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 8a97534752300584a9b8c60c04c6a6a8
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,145 +0,0 @@
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
internal class AssetBundleWebRequest : IWebRequester
{
private UnityWebRequest _webRequest;
private DownloadHandlerAssetBundle _downloadhandler;
private AssetBundle _cacheAssetBundle;
private bool _getAssetBundle = false;
public ERequestStatus Status { private set; get; } = ERequestStatus.None;
public float DownloadProgress { private set; get; }
public ulong DownloadedBytes { private set; get; }
public string RequestNetError { private set; get; }
public long RequestHttpCode { private set; get; }
public AssetBundleWebRequest() { }
public void Create(string requestURL, BundleInfo bundleInfo, params object[] args)
{
if (Status != ERequestStatus.None)
throw new System.Exception("Should never get here !");
if (args.Length == 0)
throw new System.Exception("Not found param value");
// 解析附加参数
_getAssetBundle = (bool)args[0];
// 创建下载器
_webRequest = DownloadHelper.NewRequest(requestURL);
if (CacheHelper.DisableUnityCacheOnWebGL)
{
uint crc = bundleInfo.Bundle.UnityCRC;
_downloadhandler = new DownloadHandlerAssetBundle(requestURL, crc);
}
else
{
uint crc = bundleInfo.Bundle.UnityCRC;
var hash = Hash128.Parse(bundleInfo.Bundle.FileHash);
_downloadhandler = new DownloadHandlerAssetBundle(requestURL, hash, crc);
}
#if UNITY_2020_3_OR_NEWER
_downloadhandler.autoLoadAssetBundle = false;
#endif
_webRequest.downloadHandler = _downloadhandler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
Status = ERequestStatus.InProgress;
}
public void Update()
{
if (Status == ERequestStatus.None)
return;
if (IsDone())
return;
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = _webRequest.downloadedBytes;
if (_webRequest.isDone == false)
return;
// 检查网络错误
#if UNITY_2020_3_OR_NEWER
RequestHttpCode = _webRequest.responseCode;
if (_webRequest.result != UnityWebRequest.Result.Success)
{
RequestNetError = _webRequest.error;
Status = ERequestStatus.Error;
}
else
{
Status = ERequestStatus.Success;
}
#else
RequestHttpCode = _webRequest.responseCode;
if (_webRequest.isNetworkError || _webRequest.isHttpError)
{
RequestNetError = _webRequest.error;
Status = ERequestStatus.Error;
}
else
{
Status = ERequestStatus.Success;
}
#endif
// 缓存加载的AssetBundle对象
if (Status == ERequestStatus.Success)
{
if (_getAssetBundle)
{
_cacheAssetBundle = _downloadhandler.assetBundle;
if (_cacheAssetBundle == null)
{
RequestNetError = "assetBundle is null";
Status = ERequestStatus.Error;
}
}
}
// 最终释放下载器
DisposeWebRequest();
}
public void Abort()
{
// 如果下载任务还未开始
if (Status == ERequestStatus.None)
{
RequestNetError = "user cancel";
Status = ERequestStatus.Error;
}
else
{
// 注意:为了防止同一个文件强制停止之后立马创建新的请求,应该让进行中的请求自然终止。
if (_webRequest != null)
{
if (_webRequest.isDone == false)
_webRequest.Abort(); // If in progress, halts the UnityWebRequest as soon as possible.
}
}
}
public bool IsDone()
{
if (Status == ERequestStatus.Success || Status == ERequestStatus.Error)
return true;
else
return false;
}
public object GetRequestObject()
{
return _cacheAssetBundle;
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
_webRequest.Dispose();
_webRequest = null;
}
}
}
}

View File

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

View File

@ -1,108 +0,0 @@
using System.IO;
using UnityEngine.Networking;
namespace YooAsset
{
internal class FileGeneralRequest : IWebRequester
{
private UnityWebRequest _webRequest;
public ERequestStatus Status { private set; get; } = ERequestStatus.None;
public float DownloadProgress { private set; get; }
public ulong DownloadedBytes { private set; get; }
public string RequestNetError { private set; get; }
public long RequestHttpCode { private set; get; }
public FileGeneralRequest() { }
public void Create(string requestURL, BundleInfo bundleInfo, params object[] args)
{
if (Status != ERequestStatus.None)
throw new System.Exception("Should never get here !");
string tempFilePath = bundleInfo.TempDataFilePath;
// 删除临时文件
if (File.Exists(tempFilePath))
File.Delete(tempFilePath);
// 创建下载器
_webRequest = DownloadHelper.NewRequest(requestURL);
DownloadHandlerFile handler = new DownloadHandlerFile(tempFilePath);
handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
Status = ERequestStatus.InProgress;
}
public void Update()
{
if (Status == ERequestStatus.None)
return;
if (IsDone())
return;
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = _webRequest.downloadedBytes;
if (_webRequest.isDone == false)
return;
// 检查网络错误
#if UNITY_2020_3_OR_NEWER
RequestHttpCode = _webRequest.responseCode;
if (_webRequest.result != UnityWebRequest.Result.Success)
{
RequestNetError = _webRequest.error;
Status = ERequestStatus.Error;
}
else
{
Status = ERequestStatus.Success;
}
#else
RequestHttpCode = _webRequest.responseCode;
if (_webRequest.isNetworkError || _webRequest.isHttpError)
{
RequestNetError = _webRequest.error;
Status = ERequestStatus.Error;
}
else
{
Status = ERequestStatus.Success;
}
#endif
// 最终释放下载器
DisposeWebRequest();
}
public void Abort()
{
DisposeWebRequest();
if (IsDone() == false)
{
RequestNetError = "user abort";
RequestHttpCode = 0;
Status = ERequestStatus.Error;
}
}
public bool IsDone()
{
if (Status == ERequestStatus.Success || Status == ERequestStatus.Error)
return true;
else
return false;
}
public object GetRequestObject()
{
throw new System.NotImplementedException();
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
}
}

View File

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

View File

@ -1,151 +0,0 @@
using System.IO;
using UnityEngine.Networking;
namespace YooAsset
{
internal class FileResumeRequest : IWebRequester
{
private string _tempFilePath;
private UnityWebRequest _webRequest;
private DownloadHandlerFileRange _downloadHandle;
private ulong _fileOriginLength = 0;
public ERequestStatus Status { private set; get; } = ERequestStatus.None;
public float DownloadProgress { private set; get; }
public ulong DownloadedBytes { private set; get; }
public string RequestNetError { private set; get; }
public long RequestHttpCode { private set; get; }
public FileResumeRequest() { }
public void Create(string requestURL, BundleInfo bundleInfo, params object[] args)
{
if (Status != ERequestStatus.None)
throw new System.Exception("Should never get here !");
_tempFilePath = bundleInfo.TempDataFilePath;
long fileBytes = bundleInfo.Bundle.FileSize;
// 获取下载的起始位置
long fileLength = -1;
if (File.Exists(_tempFilePath))
{
FileInfo fileInfo = new FileInfo(_tempFilePath);
fileLength = fileInfo.Length;
_fileOriginLength = (ulong)fileLength;
DownloadedBytes = _fileOriginLength;
}
// 检测下载起始位置是否有效
if (fileLength >= fileBytes)
{
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
}
// 创建下载器
_webRequest = DownloadHelper.NewRequest(requestURL);
#if UNITY_2019_4_OR_NEWER
var handler = new DownloadHandlerFile(_tempFilePath, true);
handler.removeFileOnAbort = false;
#else
var handler = new DownloadHandlerFileRange(tempFilePath, _bundleInfo.Bundle.FileSize, _webRequest);
_downloadHandle = handler;
#endif
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
if (fileLength > 0)
_webRequest.SetRequestHeader("Range", $"bytes={fileLength}-");
_webRequest.SendWebRequest();
Status = ERequestStatus.InProgress;
}
public void Update()
{
if (Status == ERequestStatus.None)
return;
if (IsDone())
return;
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = _fileOriginLength + _webRequest.downloadedBytes;
if (_webRequest.isDone == false)
return;
// 检查网络错误
#if UNITY_2020_3_OR_NEWER
RequestHttpCode = _webRequest.responseCode;
if (_webRequest.result != UnityWebRequest.Result.Success)
{
RequestNetError = _webRequest.error;
Status = ERequestStatus.Error;
}
else
{
Status = ERequestStatus.Success;
}
#else
RequestHttpCode = _webRequest.responseCode;
if (_webRequest.isNetworkError || _webRequest.isHttpError)
{
RequestNetError = _webRequest.error;
Status = ERequestStatus.Error;
}
else
{
Status = ERequestStatus.Success;
}
#endif
// 注意:下载断点续传文件发生特殊错误码之后删除文件
if (Status == ERequestStatus.Error)
{
if (DownloadHelper.ClearFileResponseCodes != null)
{
if (DownloadHelper.ClearFileResponseCodes.Contains(RequestHttpCode))
{
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
}
}
}
// 最终释放下载器
DisposeWebRequest();
}
public void Abort()
{
DisposeWebRequest();
if (IsDone() == false)
{
RequestNetError = "user abort";
RequestHttpCode = 0;
Status = ERequestStatus.Error;
}
}
public bool IsDone()
{
if (Status == ERequestStatus.Success || Status == ERequestStatus.Error)
return true;
else
return false;
}
public object GetRequestObject()
{
throw new System.NotImplementedException();
}
private void DisposeWebRequest()
{
if (_downloadHandle != null)
{
_downloadHandle.Cleanup();
_downloadHandle = null;
}
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
}
}

View File

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

View File

@ -1,65 +0,0 @@

namespace YooAsset
{
internal enum ERequestStatus
{
None,
InProgress,
Error,
Success,
}
internal interface IWebRequester
{
/// <summary>
/// 任务状态
/// </summary>
ERequestStatus Status { get; }
/// <summary>
/// 下载进度0f~1f
/// </summary>
float DownloadProgress { get; }
/// <summary>
/// 已经下载的总字节数
/// </summary>
ulong DownloadedBytes { get; }
/// <summary>
/// 返回的网络错误
/// </summary>
string RequestNetError { get; }
/// <summary>
/// 返回的HTTP CODE
/// </summary>
long RequestHttpCode { get; }
/// <summary>
/// 创建任务
/// </summary>
void Create(string url, BundleInfo bundleInfo, params object[] args);
/// <summary>
/// 更新任务
/// </summary>
void Update();
/// <summary>
/// 终止任务
/// </summary>
void Abort();
/// <summary>
/// 是否已经完成(无论成功或失败)
/// </summary>
bool IsDone();
/// <summary>
/// 获取请求的对象
/// </summary>
object GetRequestObject();
}
}

View File

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

View File

@ -1,51 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine;
namespace YooAsset
{
internal class UnityWebDataRequester : UnityWebRequesterBase
{
/// <summary>
/// 发送GET请求
/// </summary>
public void SendRequest(string url, int timeout = 60)
{
if (_webRequest == null)
{
URL = url;
ResetTimeout(timeout);
_webRequest = DownloadHelper.NewRequest(URL);
DownloadHandlerBuffer handler = new DownloadHandlerBuffer();
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_operationHandle = _webRequest.SendWebRequest();
}
}
/// <summary>
/// 获取下载的字节数据
/// </summary>
public byte[] GetData()
{
if (_webRequest != null && IsDone())
return _webRequest.downloadHandler.data;
else
return null;
}
/// <summary>
/// 获取下载的文本数据
/// </summary>
public string GetText()
{
if (_webRequest != null && IsDone())
return _webRequest.downloadHandler.text;
else
return null;
}
}
}

View File

@ -1,30 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine;
namespace YooAsset
{
internal class UnityWebFileRequester : UnityWebRequesterBase
{
/// <summary>
/// 发送GET请求
/// </summary>
public void SendRequest(string url, string fileSavePath, int timeout = 60)
{
if (_webRequest == null)
{
URL = url;
ResetTimeout(timeout);
_webRequest = DownloadHelper.NewRequest(URL);
DownloadHandlerFile handler = new DownloadHandlerFile(fileSavePath);
handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_operationHandle = _webRequest.SendWebRequest();
}
}
}
}

View File

@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace YooAsset
{
public class RequestHelper
internal class WebRequestCounter
{
/// <summary>
/// 记录网络请求失败事件的次数

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 96a75a20111d6124696665e7aac3564c
guid: d7ac605758b151748980942c6d563fed
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: a82eeb6a47cd02c4cb38e851c8ed8784
guid: 6f2f046660639e54cb2c40610189a91c
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 内置资源清单目录
/// </summary>
internal class DefaultBuildinFileCatalog : ScriptableObject
{
[Serializable]
public class FileWrapper
{
public string BundleGUID;
public string FileName;
public FileWrapper(string bundleGUID, string fileName)
{
BundleGUID = bundleGUID;
FileName = fileName;
}
}
/// <summary>
/// 包裹名称
/// </summary>
public string PackageName;
/// <summary>
/// 包裹版本
/// </summary>
public string PackageVersion;
/// <summary>
/// 文件列表
/// </summary>
public List<FileWrapper> Wrappers = new List<FileWrapper>();
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: e4a97c06e069c1146a881fcb359f9b4b
guid: b05c83971e3dca94f9fa460d396385e5
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -0,0 +1,335 @@
using System;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 内置文件系统
/// </summary>
internal class DefaultBuildinFileSystem : IFileSystem
{
public class FileWrapper
{
public string FileName { private set; get; }
public FileWrapper(string fileName)
{
FileName = fileName;
}
}
protected readonly Dictionary<string, FileWrapper> _wrappers = new Dictionary<string, FileWrapper>(10000);
protected readonly Dictionary<string, Stream> _loadedStream = new Dictionary<string, Stream>(10000);
protected readonly Dictionary<string, string> _buildinFilePaths = new Dictionary<string, string>(10000);
protected string _packageRoot;
/// <summary>
/// 解压文件系统
/// </summary>
public IFileSystem UnpackFileSystem { private set; get; }
/// <summary>
/// 包裹名称
/// </summary>
public string PackageName { private set; get; }
/// <summary>
/// 文件访问权限
/// </summary>
public EFileAccess FileSystemAccess
{
get
{
return EFileAccess.Read;
}
}
/// <summary>
/// 文件根目录
/// </summary>
public string FileRoot
{
get
{
return _packageRoot;
}
}
/// <summary>
/// 文件数量
/// </summary>
public int FileCount
{
get
{
return _wrappers.Count;
}
}
#region 自定义参数
/// <summary>
/// 自定义参数:初始化的时候缓存文件校验级别
/// </summary>
public EFileVerifyLevel FileVerifyLevel { private set; get; } = EFileVerifyLevel.Middle;
/// <summary>
/// 自定义参数:数据文件追加文件格式
/// </summary>
public bool AppendFileExtension { private set; get; } = false;
/// <summary>
/// 自定义参数:原生文件构建管线
/// </summary>
public bool RawFileBuildPipeline { private set; get; } = false;
#endregion
public DefaultBuildinFileSystem()
{
}
public virtual FSInitializeFileSystemOperation InitializeFileSystemAsync()
{
#if UNITY_EDITOR
var operation = new DBFSInitializeInEditorPlayModeOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
#else
var operation = new DBFSInitializeOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
#endif
}
public virtual FSLoadPackageManifestOperation LoadPackageManifestAsync(params object[] args)
{
var operation = new DBFSLoadPackageManifestOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSRequestPackageVersionOperation RequestPackageVersionAsync(params object[] args)
{
throw new System.NotImplementedException();
}
public virtual FSClearAllBundleFilesOperation ClearAllBundleFilesAsync(params object[] args)
{
var operation = new DBFSClearAllBundleFilesOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(params object[] args)
{
PackageManifest manifest = args[0] as PackageManifest;
var operation = new DBFSClearUnusedBundleFilesOperation(this, manifest);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSDownloadFileOperation DownloadFileAsync(params object[] args)
{
PackageBundle bundle = args[0] as PackageBundle;
int failedTryAgain = (int)args[2];
int timeout = (int)args[3];
string buidlinFilePath = GetBuildinFileLoadPath(bundle);
return UnpackFileSystem.DownloadFileAsync(bundle, buidlinFilePath, failedTryAgain, timeout);
}
public virtual void SetParameter(string name, object value)
{
if (name == "FILE_VERIFY_LEVEL")
{
FileVerifyLevel = (EFileVerifyLevel)value;
}
else if (name == "APPEND_FILE_EXTENSION")
{
AppendFileExtension = (bool)value;
}
else if (name == "RAW_FILE_BUILD_PIPELINE")
{
RawFileBuildPipeline = (bool)value;
}
else
{
YooLogger.Warning($"Invalid parameter : {name}");
}
}
public virtual void OnCreate(string packageName, string rootDirectory)
{
PackageName = packageName;
if (string.IsNullOrEmpty(rootDirectory))
rootDirectory = GetDefaultRoot();
_packageRoot = PathUtility.Combine(rootDirectory, packageName);
// 创建解压文件系统
UnpackFileSystem = new DefaultUnpackFileSystem();
UnpackFileSystem.SetParameter("FILE_VERIFY_LEVEL", FileVerifyLevel);
UnpackFileSystem.SetParameter("APPEND_FILE_EXTENSION", AppendFileExtension);
UnpackFileSystem.SetParameter("RAW_FILE_BUILD_PIPELINE", RawFileBuildPipeline);
UnpackFileSystem.OnCreate(packageName, null);
}
public virtual void OnUpdate()
{
}
public virtual bool Belong(PackageBundle bundle)
{
return _wrappers.ContainsKey(bundle.BundleGUID);
}
public virtual bool Belong(string bundleGUID)
{
return _wrappers.ContainsKey(bundleGUID);
}
public virtual bool Exists(PackageBundle bundle)
{
return _wrappers.ContainsKey(bundle.BundleGUID);
}
public virtual bool Exists(string bundleGUID)
{
return _wrappers.ContainsKey(bundleGUID);
}
public virtual bool CheckNeedDownload(PackageBundle bundle)
{
return false;
}
public virtual bool CheckNeedUnpack(PackageBundle bundle)
{
if (Belong(bundle) == false)
return false;
#if UNITY_ANDROID
return RawFileBuildPipeline || bundle.Encrypted;
#else
return false;
#endif
}
public virtual bool CheckNeedImport(PackageBundle bundle)
{
return false;
}
public virtual bool WriteFile(PackageBundle bundle, string copyPath)
{
return UnpackFileSystem.WriteFile(bundle, copyPath);
}
public virtual bool DeleteFile(PackageBundle bundle)
{
return UnpackFileSystem.DeleteFile(bundle);
}
public virtual bool DeleteFile(string bundleGUID)
{
return UnpackFileSystem.DeleteFile(bundleGUID);
}
public virtual EFileVerifyResult VerifyFile(PackageBundle bundle)
{
return UnpackFileSystem.VerifyFile(bundle);
}
public virtual byte[] ReadFileBytes(PackageBundle bundle)
{
return UnpackFileSystem.ReadFileBytes(bundle);
}
public virtual string ReadFileText(PackageBundle bundle)
{
return UnpackFileSystem.ReadFileText(bundle);
}
public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
{
if (RawFileBuildPipeline)
{
var operation = new DBFSLoadRawBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
else
{
var operation = new DBFSLoadAssetBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
}
public virtual void UnloadBundleFile(PackageBundle bundle, object result)
{
AssetBundle assetBundle = result as AssetBundle;
if (assetBundle == null)
return;
if (UnpackFileSystem.Exists(bundle))
{
UnpackFileSystem.UnloadBundleFile(bundle, assetBundle);
}
else
{
if (assetBundle != null)
assetBundle.Unload(true);
if (_loadedStream.TryGetValue(bundle.BundleGUID, out Stream managedStream))
{
managedStream.Close();
managedStream.Dispose();
_loadedStream.Remove(bundle.BundleGUID);
}
}
}
#region 内部方法
protected string GetDefaultRoot()
{
string path = PathUtility.Combine(UnityEngine.Application.streamingAssetsPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#if UNITY_OPENHARMONY
return $"file://{path}";
#else
return path;
#endif
}
public string GetBuildinFileLoadPath(PackageBundle bundle)
{
if (_buildinFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{
filePath = PathUtility.Combine(_packageRoot, bundle.FileName);
_buildinFilePaths.Add(bundle.BundleGUID, filePath);
}
return filePath;
}
public string GetBuildinCatalogFileLoadPath()
{
string fileName = Path.GetFileNameWithoutExtension(DefaultBuildinFileSystemDefine.BuildinCatalogFileName);
return PathUtility.Combine(YooAssetSettingsData.Setting.DefaultYooFolderName, PackageName, fileName);
}
public string GetBuildinPackageVersionFilePath()
{
string fileName = YooAssetSettingsData.GetPackageVersionFileName(PackageName);
return PathUtility.Combine(FileRoot, fileName);
}
public string GetBuildinPackageHashFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion);
return PathUtility.Combine(FileRoot, fileName);
}
public string GetBuildinPackageManifestFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion);
return PathUtility.Combine(FileRoot, fileName);
}
/// <summary>
/// 记录缓存信息
/// </summary>
public bool Record(string bundleGUID, FileWrapper wrapper)
{
if (_wrappers.ContainsKey(bundleGUID))
{
YooLogger.Error($"{nameof(DefaultBuildinFileSystem)} has element : {bundleGUID}");
return false;
}
_wrappers.Add(bundleGUID, wrapper);
return true;
}
#endregion
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 08f3e92fdbd5d56459d8882be1f54f60
guid: 98465b987635afc479022ec358477491
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -0,0 +1,114 @@
#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace YooAsset
{
internal class DefaultBuildinFileSystemBuild : UnityEditor.Build.IPreprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
/// <summary>
/// 在构建应用程序前自动生成内置资源清单。
/// 原理搜索StreamingAssets目录下的所有资源文件然后将这些文件信息写入内置资源清单并存储在Resources目录下。
/// </summary>
public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
{
string savePath = $"Assets/Resources/{YooAssetSettingsData.Setting.DefaultYooFolderName}";
DirectoryInfo saveDirectory = new DirectoryInfo(savePath);
if (saveDirectory.Exists)
saveDirectory.Delete(true);
string rootPath = $"{Application.dataPath}/StreamingAssets/{YooAssetSettingsData.Setting.DefaultYooFolderName}";
DirectoryInfo rootDirectory = new DirectoryInfo(rootPath);
if (rootDirectory.Exists == false)
{
Debug.LogWarning($"Not found buildin root folder : {rootPath}");
return;
}
// 搜索所有Package目录
DirectoryInfo[] subDirectories = rootDirectory.GetDirectories();
foreach (var subDirectory in subDirectories)
{
CreateBuildinManifest(subDirectory.Name, subDirectory.FullName);
}
}
private void CreateBuildinManifest(string packageName, string pacakgeDirectory)
{
// 获取资源清单版本
string packageVersion;
{
string versionFileName = YooAssetSettingsData.GetPackageVersionFileName(packageName);
string versionFilePath = $"{pacakgeDirectory}/{versionFileName}";
if (File.Exists(versionFilePath) == false)
{
Debug.LogWarning($"Not found package version file : {versionFilePath}");
return;
}
packageVersion = FileUtility.ReadAllText(versionFilePath);
}
// 加载资源清单文件
PackageManifest packageManifest;
{
string manifestFileName = YooAssetSettingsData.GetManifestBinaryFileName(packageName, packageVersion);
string manifestFilePath = $"{pacakgeDirectory}/{manifestFileName}";
if (File.Exists(manifestFilePath) == false)
{
Debug.LogWarning($"Not found package manifest file : {manifestFilePath}");
return;
}
var binaryData = FileUtility.ReadAllBytes(manifestFilePath);
packageManifest = ManifestTools.DeserializeFromBinary(binaryData);
}
// 获取文件名映射关系
Dictionary<string, string> fileMapping = new Dictionary<string, string>();
{
foreach (var packageBundle in packageManifest.BundleList)
{
fileMapping.Add(packageBundle.FileName, packageBundle.BundleGUID);
}
}
// 创建内置清单实例
var buildinFileCatalog = ScriptableObject.CreateInstance<DefaultBuildinFileCatalog>();
buildinFileCatalog.PackageName = packageName;
buildinFileCatalog.PackageVersion = packageVersion;
// 记录所有内置资源文件
DirectoryInfo rootDirectory = new DirectoryInfo(pacakgeDirectory);
FileInfo[] fileInfos = rootDirectory.GetFiles();
foreach (var fileInfo in fileInfos)
{
if (fileInfo.Extension == ".meta" || fileInfo.Extension == ".version" ||
fileInfo.Extension == ".hash" || fileInfo.Extension == ".bytes")
continue;
string fileName = fileInfo.Name;
if (fileMapping.TryGetValue(fileName, out string bundleGUID))
{
var wrapper = new DefaultBuildinFileCatalog.FileWrapper(bundleGUID, fileName);
buildinFileCatalog.Wrappers.Add(wrapper);
}
else
{
Debug.LogWarning($"Failed mapping file : {fileName}");
}
}
string saveFilePath = $"Assets/Resources/{YooAssetSettingsData.Setting.DefaultYooFolderName}/{packageName}/{DefaultBuildinFileSystemDefine.BuildinCatalogFileName}";
FileUtility.CreateFileDirectory(saveFilePath);
UnityEditor.AssetDatabase.CreateAsset(buildinFileCatalog, saveFilePath);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
Debug.Log($"一共记录{buildinFileCatalog.Wrappers.Count}个内置资源文件,内置资源目录文件保存成功 : {saveFilePath}");
}
}
}
#endif

View File

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

View File

@ -0,0 +1,11 @@

namespace YooAsset
{
internal class DefaultBuildinFileSystemDefine
{
/// <summary>
/// 内置清单文件名称
/// </summary>
public const string BuildinCatalogFileName = "BuildinCatalog.asset";
}
}

View File

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

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 06f6a75331ed07a4a9e5e8f46dcf157e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,45 @@

namespace YooAsset
{
internal sealed class DBFSClearAllBundleFilesOperation : FSClearAllBundleFilesOperation
{
private enum ESteps
{
None,
ClearUnpackFileSystem,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private FSClearAllBundleFilesOperation _unpackClearAllBundleFilesOp;
private ESteps _steps = ESteps.None;
internal DBFSClearAllBundleFilesOperation(DefaultBuildinFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.ClearUnpackFileSystem;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.ClearUnpackFileSystem)
{
if (_unpackClearAllBundleFilesOp == null)
_unpackClearAllBundleFilesOp = _fileSystem.UnpackFileSystem.ClearAllBundleFilesAsync();
Progress = _unpackClearAllBundleFilesOp.Progress;
if (_unpackClearAllBundleFilesOp.IsDone == false)
return;
_steps = ESteps.Done;
Status = _unpackClearAllBundleFilesOp.Status;
Error = _unpackClearAllBundleFilesOp.Error;
}
}
}
}

View File

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

View File

@ -0,0 +1,47 @@

namespace YooAsset
{
internal sealed class DBFSClearUnusedBundleFilesOperation : FSClearUnusedBundleFilesOperation
{
private enum ESteps
{
None,
ClearUnpackFileSystem,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private readonly PackageManifest _manifest;
private FSClearUnusedBundleFilesOperation _unpackClearUnusedBundleFilesOp;
private ESteps _steps = ESteps.None;
internal DBFSClearUnusedBundleFilesOperation(DefaultBuildinFileSystem fileSystem, PackageManifest manifest)
{
_fileSystem = fileSystem;
_manifest = manifest;
}
internal override void InternalOnStart()
{
_steps = ESteps.ClearUnpackFileSystem;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.ClearUnpackFileSystem)
{
if (_unpackClearUnusedBundleFilesOp == null)
_unpackClearUnusedBundleFilesOp = _fileSystem.UnpackFileSystem.ClearUnusedBundleFilesAsync(_manifest);
Progress = _unpackClearUnusedBundleFilesOp.Progress;
if (_unpackClearUnusedBundleFilesOp.IsDone == false)
return;
_steps = ESteps.Done;
Status = _unpackClearUnusedBundleFilesOp.Status;
Error = _unpackClearUnusedBundleFilesOp.Error;
}
}
}
}

View File

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

View File

@ -0,0 +1,186 @@
using System;
using System.IO;
namespace YooAsset
{
internal class DBFSInitializeOperation : FSInitializeFileSystemOperation
{
private enum ESteps
{
None,
InitUnpackFileSystem,
LoadCatalogFile,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private FSInitializeFileSystemOperation _initUnpackFIleSystemOp;
private LoadBuildinCatalogFileOperation _loadCatalogFileOp;
private ESteps _steps = ESteps.None;
internal DBFSInitializeOperation(DefaultBuildinFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.InitUnpackFileSystem;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.InitUnpackFileSystem)
{
if (_initUnpackFIleSystemOp == null)
_initUnpackFIleSystemOp = _fileSystem.UnpackFileSystem.InitializeFileSystemAsync();
Progress = _initUnpackFIleSystemOp.Progress;
if (_initUnpackFIleSystemOp.IsDone == false)
return;
if (_initUnpackFIleSystemOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadCatalogFile;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _initUnpackFIleSystemOp.Error;
}
}
if (_steps == ESteps.LoadCatalogFile)
{
if (_loadCatalogFileOp == null)
{
_loadCatalogFileOp = new LoadBuildinCatalogFileOperation(_fileSystem);
OperationSystem.StartOperation(_fileSystem.PackageName, _loadCatalogFileOp);
}
if (_loadCatalogFileOp.IsDone == false)
return;
if (_loadCatalogFileOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadCatalogFileOp.Error;
}
}
}
}
/// <summary>
/// 在编辑器下离线模式的兼容性初始化
/// </summary>
internal sealed class DBFSInitializeInEditorPlayModeOperation : FSInitializeFileSystemOperation
{
private enum ESteps
{
None,
InitUnpackFileSystem,
LoadPackageManifest,
RecordFiles,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private FSInitializeFileSystemOperation _initUnpackFIleSystemOp;
private DBFSLoadPackageManifestOperation _loadPackageManifestOp;
private ESteps _steps = ESteps.None;
internal DBFSInitializeInEditorPlayModeOperation(DefaultBuildinFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.InitUnpackFileSystem;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.InitUnpackFileSystem)
{
if (_initUnpackFIleSystemOp == null)
_initUnpackFIleSystemOp = _fileSystem.UnpackFileSystem.InitializeFileSystemAsync();
Progress = _initUnpackFIleSystemOp.Progress;
if (_initUnpackFIleSystemOp.IsDone == false)
return;
if (_initUnpackFIleSystemOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadPackageManifest;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _initUnpackFIleSystemOp.Error;
}
}
if (_steps == ESteps.LoadPackageManifest)
{
if (_loadPackageManifestOp == null)
{
_loadPackageManifestOp = new DBFSLoadPackageManifestOperation(_fileSystem);
OperationSystem.StartOperation(_fileSystem.PackageName, _loadPackageManifestOp);
}
if (_loadPackageManifestOp.IsDone == false)
return;
if (_loadPackageManifestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.RecordFiles;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadPackageManifestOp.Error;
}
}
if (_steps == ESteps.RecordFiles)
{
PackageManifest manifest = _loadPackageManifestOp.Result;
string pacakgeDirectory = _fileSystem.FileRoot;
DirectoryInfo rootDirectory = new DirectoryInfo(pacakgeDirectory);
FileInfo[] fileInfos = rootDirectory.GetFiles();
foreach (var fileInfo in fileInfos)
{
if (fileInfo.Extension == ".meta" || fileInfo.Extension == ".version" ||
fileInfo.Extension == ".hash" || fileInfo.Extension == ".bytes")
continue;
string fileName = fileInfo.Name;
if (manifest.TryGetPackageBundleByFileName(fileName, out PackageBundle value))
{
var fileWrapper = new DefaultBuildinFileSystem.FileWrapper(fileName);
_fileSystem.Record(value.BundleGUID, fileWrapper);
}
else
{
YooLogger.Warning($"Failed mapping file : {fileName}");
}
}
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
}

View File

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

View File

@ -0,0 +1,334 @@
using System.IO;
using UnityEngine;
namespace YooAsset
{
internal class DBFSLoadAssetBundleOperation : FSLoadBundleOperation
{
private enum ESteps
{
None,
UnpackAssetBundleFile,
LoadUnpackAssetBundle,
LoadBuidlinAssetBundle,
CheckLoadBuildinResult,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private FSLoadBundleOperation _loadUnpackBundleOp;
private FSDownloadFileOperation _unpackBundleOp;
private AssetBundleCreateRequest _createRequest;
private bool _isWaitForAsyncComplete = false;
private ESteps _steps = ESteps.None;
internal DBFSLoadAssetBundleOperation(DefaultBuildinFileSystem fileSystem, PackageBundle bundle)
{
_fileSystem = fileSystem;
_bundle = bundle;
}
internal override void InternalOnStart()
{
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
if (_fileSystem.CheckNeedUnpack(_bundle))
{
_steps = ESteps.UnpackAssetBundleFile;
}
else
{
_steps = ESteps.LoadBuidlinAssetBundle;
}
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.UnpackAssetBundleFile)
{
if (_unpackBundleOp == null)
{
int failedTryAgain = 0;
int timeout = int.MaxValue;
_unpackBundleOp = _fileSystem.DownloadFileAsync(_bundle, null, failedTryAgain, timeout);
}
if (_unpackBundleOp.IsDone == false)
return;
if (_unpackBundleOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadUnpackAssetBundle;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _unpackBundleOp.Error;
}
}
if (_steps == ESteps.LoadUnpackAssetBundle)
{
if (_loadUnpackBundleOp == null)
_loadUnpackBundleOp = _fileSystem.UnpackFileSystem.LoadBundleFile(_bundle);
if (_loadUnpackBundleOp.IsDone == false)
return;
if (_loadUnpackBundleOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Result = _loadUnpackBundleOp.Result;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadUnpackBundleOp.Error;
}
}
if (_steps == ESteps.LoadBuidlinAssetBundle)
{
string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
if (_isWaitForAsyncComplete)
{
Result = AssetBundle.LoadFromFile(filePath);
}
else
{
_createRequest = AssetBundle.LoadFromFileAsync(filePath);
}
_steps = ESteps.CheckLoadBuildinResult;
}
if (_steps == ESteps.CheckLoadBuildinResult)
{
if (_createRequest != null)
{
if (_isWaitForAsyncComplete)
{
// 强制挂起主线程(注意:该操作会很耗时)
YooLogger.Warning("Suspend the main thread to load unity bundle.");
Result = _createRequest.assetBundle;
}
else
{
if (_createRequest.isDone == false)
return;
Result = _createRequest.assetBundle;
}
}
if (Result != null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load buildin asset bundle file : {_bundle.BundleName}";
}
}
}
public override void WaitForAsyncComplete()
{
_isWaitForAsyncComplete = true;
while (true)
{
if (_unpackBundleOp != null)
{
if (_unpackBundleOp.IsDone == false)
_unpackBundleOp.WaitForAsyncComplete();
}
if (_loadUnpackBundleOp != null)
{
if (_loadUnpackBundleOp.IsDone == false)
_loadUnpackBundleOp.WaitForAsyncComplete();
}
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}
}
public override void AbortDownloadOperation()
{
if (_steps == ESteps.UnpackAssetBundleFile)
{
if (_unpackBundleOp != null)
_unpackBundleOp.SetAbort();
}
}
}
internal class DBFSLoadRawBundleOperation : FSLoadBundleOperation
{
private enum ESteps
{
None,
UnpackRawBundleFile,
LoadUnpackRawBundle,
LoadBuildinRawBundle,
CheckLoadBuildinResult,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private readonly PackageBundle _bundle;
private FSLoadBundleOperation _loadUnpackBundleOp;
private FSDownloadFileOperation _unpackBundleOp;
private ESteps _steps = ESteps.None;
internal DBFSLoadRawBundleOperation(DefaultBuildinFileSystem fileSystem, PackageBundle bundle)
{
_fileSystem = fileSystem;
_bundle = bundle;
}
internal override void InternalOnStart()
{
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
if (_fileSystem.CheckNeedUnpack(_bundle))
{
_steps = ESteps.UnpackRawBundleFile;
}
else
{
_steps = ESteps.LoadBuildinRawBundle;
}
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.UnpackRawBundleFile)
{
if (_unpackBundleOp == null)
{
int failedTryAgain = 0;
int timeout = int.MaxValue;
_unpackBundleOp = _fileSystem.DownloadFileAsync(_bundle, null, failedTryAgain, timeout);
}
if (_unpackBundleOp.IsDone == false)
return;
if (_unpackBundleOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadUnpackRawBundle;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _unpackBundleOp.Error;
}
}
if (_steps == ESteps.LoadUnpackRawBundle)
{
if (_loadUnpackBundleOp == null)
_loadUnpackBundleOp = _fileSystem.UnpackFileSystem.LoadBundleFile(_bundle);
if (_loadUnpackBundleOp.IsDone == false)
return;
if (_loadUnpackBundleOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Result = _loadUnpackBundleOp.Result;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadUnpackBundleOp.Error;
}
}
if (_steps == ESteps.LoadBuildinRawBundle)
{
string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
Result = filePath;
_steps = ESteps.CheckLoadBuildinResult;
}
if (_steps == ESteps.CheckLoadBuildinResult)
{
if (Result != null)
{
string filePath = Result as string;
if (File.Exists(filePath))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Can not found buildin raw bundle file : {filePath}";
}
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load buildin raw bundle file : {_bundle.BundleName}";
}
}
}
public override void WaitForAsyncComplete()
{
while (true)
{
if (_unpackBundleOp != null)
{
if (_unpackBundleOp.IsDone == false)
_unpackBundleOp.WaitForAsyncComplete();
}
if (_loadUnpackBundleOp != null)
{
if (_loadUnpackBundleOp.IsDone == false)
_loadUnpackBundleOp.WaitForAsyncComplete();
}
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}
}
public override void AbortDownloadOperation()
{
if (_steps == ESteps.UnpackRawBundleFile)
{
if (_unpackBundleOp != null)
_unpackBundleOp.SetAbort();
}
}
}
}

View File

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

View File

@ -0,0 +1,110 @@

namespace YooAsset
{
internal class DBFSLoadPackageManifestOperation : FSLoadPackageManifestOperation
{
private enum ESteps
{
None,
RequestBuildinPackageVersion,
RequestBuildinPackageHash,
LoadBuildinPackageManifest,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private RequestBuildinPackageVersionOperation _requestBuildinPackageVersionOp;
private RequestBuildinPackageHashOperation _requestBuildinPackageHashOp;
private LoadBuildinPackageManifestOperation _loadBuildinPackageManifestOp;
private ESteps _steps = ESteps.None;
public DBFSLoadPackageManifestOperation(DefaultBuildinFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.RequestBuildinPackageVersion;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RequestBuildinPackageVersion)
{
if (_requestBuildinPackageVersionOp == null)
{
_requestBuildinPackageVersionOp = new RequestBuildinPackageVersionOperation(_fileSystem);
OperationSystem.StartOperation(_fileSystem.PackageName, _requestBuildinPackageVersionOp);
}
if (_requestBuildinPackageVersionOp.IsDone == false)
return;
if (_requestBuildinPackageVersionOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.RequestBuildinPackageHash;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _requestBuildinPackageVersionOp.Error;
}
}
if (_steps == ESteps.RequestBuildinPackageHash)
{
if (_requestBuildinPackageHashOp == null)
{
string packageVersion = _requestBuildinPackageVersionOp.PackageVersion;
_requestBuildinPackageHashOp = new RequestBuildinPackageHashOperation(_fileSystem, packageVersion);
OperationSystem.StartOperation(_fileSystem.PackageName, _requestBuildinPackageHashOp);
}
if (_requestBuildinPackageHashOp.IsDone == false)
return;
if (_requestBuildinPackageHashOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadBuildinPackageManifest;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _requestBuildinPackageHashOp.Error;
}
}
if (_steps == ESteps.LoadBuildinPackageManifest)
{
if (_loadBuildinPackageManifestOp == null)
{
string packageVersion = _requestBuildinPackageVersionOp.PackageVersion;
string packageHash = _requestBuildinPackageHashOp.PackageHash;
_loadBuildinPackageManifestOp = new LoadBuildinPackageManifestOperation(_fileSystem, packageVersion, packageHash);
OperationSystem.StartOperation(_fileSystem.PackageName, _loadBuildinPackageManifestOp);
}
if (_loadBuildinPackageManifestOp.IsDone == false)
return;
if (_loadBuildinPackageManifestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Result = _loadBuildinPackageManifestOp.Manifest;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadBuildinPackageManifestOp.Error;
}
}
}
}
}

View File

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

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: be1c19353672bb34ca3e4ddcb462402f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,63 @@
using UnityEngine;
namespace YooAsset
{
internal sealed class LoadBuildinCatalogFileOperation : AsyncOperationBase
{
private enum ESteps
{
None,
LoadCatalog,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private ESteps _steps = ESteps.None;
internal LoadBuildinCatalogFileOperation(DefaultBuildinFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.LoadCatalog;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.LoadCatalog)
{
string catalogFilePath = _fileSystem.GetBuildinCatalogFileLoadPath();
var catalog = Resources.Load<DefaultBuildinFileCatalog>(catalogFilePath);
if (catalog == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load catalog file : {catalogFilePath}";
return;
}
if (catalog.PackageName != _fileSystem.PackageName)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The catalog file package name {catalog.PackageName} cannot match the file system package name {_fileSystem.PackageName}";
return;
}
foreach (var wrapper in catalog.Wrappers)
{
var fileWrapper = new DefaultBuildinFileSystem.FileWrapper(wrapper.FileName);
_fileSystem.Record(wrapper.BundleGUID, fileWrapper);
}
YooLogger.Log($"Package '{_fileSystem.PackageName}' catalog files count : {catalog.Wrappers.Count}");
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
}

View File

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

View File

@ -0,0 +1,110 @@

namespace YooAsset
{
internal class LoadBuildinPackageManifestOperation : AsyncOperationBase
{
private enum ESteps
{
None,
RequestFileData,
VerifyFileData,
LoadManifest,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private readonly string _packageVersion;
private readonly string _packageHash;
private UnityWebDataRequestOperation _webDataRequestOp;
private DeserializeManifestOperation _deserializer;
private ESteps _steps = ESteps.None;
/// <summary>
/// 加载的清单实例
/// </summary>
public PackageManifest Manifest { private set; get; }
internal LoadBuildinPackageManifestOperation(DefaultBuildinFileSystem fileSystem, string packageVersion, string packageHash)
{
_fileSystem = fileSystem;
_packageVersion = packageVersion;
_packageHash = packageHash;
}
internal override void InternalOnStart()
{
_steps = ESteps.RequestFileData;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RequestFileData)
{
if (_webDataRequestOp == null)
{
string filePath = _fileSystem.GetBuildinPackageManifestFilePath(_packageVersion);
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webDataRequestOp = new UnityWebDataRequestOperation(url);
OperationSystem.StartOperation(_fileSystem.PackageName, _webDataRequestOp);
}
if (_webDataRequestOp.IsDone == false)
return;
if (_webDataRequestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.VerifyFileData;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _webDataRequestOp.Error;
}
}
if (_steps == ESteps.VerifyFileData)
{
string fileHash = HashUtility.BytesMD5(_webDataRequestOp.Result);
if (fileHash == _packageHash)
{
_steps = ESteps.LoadManifest;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Failed to verify buildin package manifest file!";
}
}
if (_steps == ESteps.LoadManifest)
{
if (_deserializer == null)
{
_deserializer = new DeserializeManifestOperation(_webDataRequestOp.Result);
OperationSystem.StartOperation(_fileSystem.PackageName, _deserializer);
}
Progress = _deserializer.Progress;
if (_deserializer.IsDone == false)
return;
if (_deserializer.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Manifest = _deserializer.Manifest;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _deserializer.Error;
}
}
}
}
}

View File

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

View File

@ -0,0 +1,75 @@

namespace YooAsset
{
internal class RequestBuildinPackageHashOperation : AsyncOperationBase
{
private enum ESteps
{
None,
RequestPackageHash,
Done,
}
private readonly DefaultBuildinFileSystem _fileSystem;
private readonly string _packageVersion;
private UnityWebTextRequestOperation _webTextRequestOp;
private ESteps _steps = ESteps.None;
/// <summary>
/// 包裹哈希值
/// </summary>
public string PackageHash { private set; get; }
internal RequestBuildinPackageHashOperation(DefaultBuildinFileSystem fileSystem, string packageVersion)
{
_fileSystem = fileSystem;
_packageVersion = packageVersion;
}
internal override void InternalOnStart()
{
_steps = ESteps.RequestPackageHash;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.RequestPackageHash)
{
if (_webTextRequestOp == null)
{
string filePath = _fileSystem.GetBuildinPackageHashFilePath(_packageVersion);
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webTextRequestOp = new UnityWebTextRequestOperation(url);
OperationSystem.StartOperation(_fileSystem.PackageName, _webTextRequestOp);
}
if (_webTextRequestOp.IsDone == false)
return;
if (_webTextRequestOp.Status == EOperationStatus.Succeed)
{
PackageHash = _webTextRequestOp.Result;
if (string.IsNullOrEmpty(PackageHash))
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Buildin package hash file content is empty !";
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _webTextRequestOp.Error;
}
}
}
}
}

View File

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

View File

@ -1,17 +1,17 @@

namespace YooAsset
{
internal class QueryBuildinPackageVersionOperation : AsyncOperationBase
internal class RequestBuildinPackageVersionOperation : AsyncOperationBase
{
private enum ESteps
{
None,
LoadBuildinPackageVersionFile,
RequestPackageVersion,
Done,
}
private readonly PersistentManager _persistent;
private UnityWebDataRequester _downloader;
private readonly DefaultBuildinFileSystem _fileSystem;
private UnityWebTextRequestOperation _webTextRequestOp;
private ESteps _steps = ESteps.None;
/// <summary>
@ -20,41 +20,35 @@ namespace YooAsset
public string PackageVersion { private set; get; }
public QueryBuildinPackageVersionOperation(PersistentManager persistent)
internal RequestBuildinPackageVersionOperation(DefaultBuildinFileSystem fileSystem)
{
_persistent = persistent;
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.LoadBuildinPackageVersionFile;
_steps = ESteps.RequestPackageVersion;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.LoadBuildinPackageVersionFile)
if (_steps == ESteps.RequestPackageVersion)
{
if (_downloader == null)
if (_webTextRequestOp == null)
{
string filePath = _persistent.GetBuildinPackageVersionFilePath();
string url = PersistentHelper.ConvertToWWWPath(filePath);
_downloader = new UnityWebDataRequester();
_downloader.SendRequest(url);
string filePath = _fileSystem.GetBuildinPackageVersionFilePath();
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webTextRequestOp = new UnityWebTextRequestOperation(url);
OperationSystem.StartOperation(_fileSystem.PackageName, _webTextRequestOp);
}
if (_downloader.IsDone() == false)
if (_webTextRequestOp.IsDone == false)
return;
if (_downloader.HasError())
if (_webTextRequestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _downloader.GetError();
}
else
{
PackageVersion = _downloader.GetText();
PackageVersion = _webTextRequestOp.Result;
if (string.IsNullOrEmpty(PackageVersion))
{
_steps = ESteps.Done;
@ -67,8 +61,12 @@ namespace YooAsset
Status = EOperationStatus.Succeed;
}
}
_downloader.Dispose();
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _webTextRequestOp.Error;
}
}
}
}

View File

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

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b03f54c14ce3f6041870eaa9317d2e64
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,537 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 缓存文件系统
/// 说明正在进行的下载器会在ResourcePackage销毁的时候执行Abort操作
/// </summary>
internal class DefaultCacheFileSystem : IFileSystem
{
public class FileWrapper
{
public string InfoFilePath { private set; get; }
public string DataFilePath { private set; get; }
public string DataFileCRC { private set; get; }
public long DataFileSize { private set; get; }
public FileWrapper(string infoFilePath, string dataFilePath, string dataFileCRC, long dataFileSize)
{
InfoFilePath = infoFilePath;
DataFilePath = dataFilePath;
DataFileCRC = dataFileCRC;
DataFileSize = dataFileSize;
}
}
protected readonly Dictionary<string, DefaultDownloadFileOperation> _downloaders = new Dictionary<string, DefaultDownloadFileOperation>(1000);
protected readonly Dictionary<string, FileWrapper> _wrappers = new Dictionary<string, FileWrapper>(10000);
protected readonly Dictionary<string, Stream> _loadedStream = new Dictionary<string, Stream>(10000);
protected readonly Dictionary<string, string> _dataFilePaths = new Dictionary<string, string>(10000);
protected readonly Dictionary<string, string> _infoFilePaths = new Dictionary<string, string>(10000);
protected readonly Dictionary<string, string> _tempFilePaths = new Dictionary<string, string>(10000);
protected readonly List<string> _removeList = new List<string>(1000);
protected string _packageRoot;
protected string _saveFileRoot;
protected string _tempFileRoot;
protected string _manifestFileRoot;
/// <summary>
/// 包裹名称
/// </summary>
public string PackageName { private set; get; }
/// <summary>
/// 文件访问权限
/// </summary>
public EFileAccess FileSystemAccess
{
get
{
return EFileAccess.ReadWrite;
}
}
/// <summary>
/// 文件根目录
/// </summary>
public string FileRoot
{
get
{
return _packageRoot;
}
}
/// <summary>
/// 文件数量
/// </summary>
public int FileCount
{
get
{
return _wrappers.Count;
}
}
#region 自定义参数
/// <summary>
/// 自定义参数:远程服务接口
/// </summary>
public IRemoteServices RemoteServices { private set; get; } = null;
/// <summary>
/// 自定义参数:初始化的时候缓存文件校验级别
/// </summary>
public EFileVerifyLevel FileVerifyLevel { private set; get; } = EFileVerifyLevel.Middle;
/// <summary>
/// 自定义参数:数据文件追加文件格式
/// </summary>
public bool AppendFileExtension { private set; get; } = false;
/// <summary>
/// 自定义参数:原生文件构建管线
/// </summary>
public bool RawFileBuildPipeline { private set; get; } = false;
/// <summary>
/// 自定义参数:启用断点续传的最小尺寸
/// </summary>
public long ResumeDownloadMinimumSize { private set; get; } = long.MaxValue;
/// <summary>
/// 自定义参数:断点续传下载器关注的错误码
/// </summary>
public List<long> ResumeDownloadResponseCodes { private set; get; } = null;
#endregion
public DefaultCacheFileSystem()
{
}
public virtual FSInitializeFileSystemOperation InitializeFileSystemAsync()
{
var operation = new DCFSInitializeOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSLoadPackageManifestOperation LoadPackageManifestAsync(params object[] args)
{
string packageVersion = args[0] as string;
int timeout = (int)args[1];
var operation = new DCFSLoadPackageManifestOperation(this, packageVersion, timeout);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSRequestPackageVersionOperation RequestPackageVersionAsync(params object[] args)
{
bool appendTimeTicks = (bool)args[0];
int timeout = (int)args[1];
var operation = new DCFSRequestPackageVersionOperation(this, appendTimeTicks, timeout);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSClearAllBundleFilesOperation ClearAllBundleFilesAsync(params object[] args)
{
var operation = new DCFSClearAllBundleFilesOperation(this);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSClearUnusedBundleFilesOperation ClearUnusedBundleFilesAsync(params object[] args)
{
PackageManifest manifest = args[0] as PackageManifest;
var operation = new DCFSClearUnusedBundleFilesOperation(this, manifest);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
public virtual FSDownloadFileOperation DownloadFileAsync(params object[] args)
{
PackageBundle bundle = args[0] as PackageBundle;
string localCopyFilePath = (string)args[1];
int failedTryAgain = (int)args[2];
int timeout = (int)args[3];
// 查询旧的下载器
if (_downloaders.TryGetValue(bundle.BundleGUID, out var oldDownloader))
{
oldDownloader.Reference();
return oldDownloader;
}
// 创建新的下载器
{
string mainURL;
string fallbackURL;
if (string.IsNullOrEmpty(localCopyFilePath))
{
mainURL = RemoteServices.GetRemoteMainURL(bundle.FileName);
fallbackURL = RemoteServices.GetRemoteFallbackURL(bundle.FileName);
}
else
{
// 注意:把本地文件路径指定为远端下载地址
mainURL = DownloadSystemHelper.ConvertToWWWPath(localCopyFilePath);
fallbackURL = mainURL;
}
if (bundle.FileSize >= ResumeDownloadMinimumSize)
{
var newDownloader = new DCFSDownloadResumeFileOperation(this, bundle, mainURL, fallbackURL, failedTryAgain, timeout);
newDownloader.Reference();
_downloaders.Add(bundle.BundleGUID, newDownloader);
OperationSystem.StartOperation(PackageName, newDownloader);
return newDownloader;
}
else
{
var newDownloader = new DCFSDownloadNormalFileOperation(this, bundle, mainURL, fallbackURL, failedTryAgain, timeout);
newDownloader.Reference();
_downloaders.Add(bundle.BundleGUID, newDownloader);
OperationSystem.StartOperation(PackageName, newDownloader);
return newDownloader;
}
}
}
public virtual void SetParameter(string name, object value)
{
if (name == "REMOTE_SERVICES")
{
RemoteServices = (IRemoteServices)value;
}
else if (name == "FILE_VERIFY_LEVEL")
{
FileVerifyLevel = (EFileVerifyLevel)value;
}
else if (name == "APPEND_FILE_EXTENSION")
{
AppendFileExtension = (bool)value;
}
else if (name == "RAW_FILE_BUILD_PIPELINE")
{
RawFileBuildPipeline = (bool)value;
}
else if (name == "RESUME_DOWNLOAD_MINMUM_SIZE")
{
ResumeDownloadMinimumSize = (long)value;
}
else if (name == "RESUME_DOWNLOAD_RESPONSE_CODES")
{
ResumeDownloadResponseCodes = (List<long>)value;
}
else
{
YooLogger.Warning($"Invalid parameter : {name}");
}
}
public virtual void OnCreate(string packageName, string rootDirectory)
{
PackageName = packageName;
if (string.IsNullOrEmpty(rootDirectory))
rootDirectory = GetDefaultRoot();
_packageRoot = PathUtility.Combine(rootDirectory, packageName);
_saveFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.SaveFilesFolderName);
_tempFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.TempFilesFolderName);
_manifestFileRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.ManifestFilesFolderName);
}
public virtual void OnUpdate()
{
_removeList.Clear();
foreach (var valuePair in _downloaders)
{
var downloader = valuePair.Value;
// 注意:主动终止引用计数为零的下载任务
if (downloader.RefCount <= 0)
{
_removeList.Add(valuePair.Key);
downloader.SetAbort();
continue;
}
if (downloader.IsDone)
_removeList.Add(valuePair.Key);
}
foreach (var key in _removeList)
{
_downloaders.Remove(key);
}
}
public virtual bool Belong(PackageBundle bundle)
{
// 注意:缓存文件系统保底加载!
return true;
}
public virtual bool Belong(string bundleGUID)
{
// 注意:缓存文件系统保底加载!
return true;
}
public virtual bool Exists(PackageBundle packageBundle)
{
return _wrappers.ContainsKey(packageBundle.BundleGUID);
}
public virtual bool Exists(string bundleGUID)
{
return _wrappers.ContainsKey(bundleGUID);
}
public virtual bool CheckNeedDownload(PackageBundle bundle)
{
if (Belong(bundle) == false)
return false;
return Exists(bundle) == false;
}
public virtual bool CheckNeedUnpack(PackageBundle bundle)
{
return false;
}
public virtual bool CheckNeedImport(PackageBundle bundle)
{
if (Belong(bundle) == false)
return false;
return Exists(bundle) == false;
}
public virtual bool WriteFile(PackageBundle bundle, string copyPath)
{
if (_wrappers.ContainsKey(bundle.BundleGUID))
{
throw new Exception("Should never get here !");
}
string infoFilePath = GetInfoFilePath(bundle);
string dataFilePath = GetDataFilePath(bundle);
try
{
if (File.Exists(infoFilePath))
File.Delete(infoFilePath);
if (File.Exists(dataFilePath))
File.Delete(dataFilePath);
FileUtility.CreateFileDirectory(dataFilePath);
// 拷贝数据文件
FileInfo fileInfo = new FileInfo(copyPath);
fileInfo.CopyTo(dataFilePath);
// 写入文件信息
WriteInfoFile(infoFilePath, bundle.FileCRC, bundle.FileSize);
}
catch (Exception e)
{
YooLogger.Error($"Failed to write cache file ! {e.Message}");
return false;
}
FileWrapper wrapper = new FileWrapper(infoFilePath, dataFilePath, bundle.FileCRC, bundle.FileSize);
return Record(bundle.BundleGUID, wrapper);
}
public virtual bool DeleteFile(PackageBundle bundle)
{
return DeleteFile(bundle.BundleGUID);
}
public virtual bool DeleteFile(string bundleGUID)
{
if (_wrappers.TryGetValue(bundleGUID, out FileWrapper wrapper))
{
try
{
string dataFilePath = wrapper.DataFilePath;
FileInfo fileInfo = new FileInfo(dataFilePath);
if (fileInfo.Exists)
fileInfo.Directory.Delete(true);
_wrappers.Remove(bundleGUID);
return true;
}
catch (Exception e)
{
YooLogger.Error($"Failed to delete cache file ! {e.Message}");
return false;
}
}
else
{
return false;
}
}
public virtual EFileVerifyResult VerifyFile(PackageBundle bundle)
{
if (_wrappers.TryGetValue(bundle.BundleGUID, out FileWrapper wrapper) == false)
return EFileVerifyResult.CacheNotFound;
EFileVerifyResult result = FileSystemHelper.FileVerify(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EFileVerifyLevel.High);
return result;
}
public virtual byte[] ReadFileBytes(PackageBundle bundle)
{
throw new System.NotImplementedException();
}
public virtual string ReadFileText(PackageBundle bundle)
{
throw new System.NotImplementedException();
}
public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
{
if (RawFileBuildPipeline)
{
var operation = new DCFSLoadRawBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
else
{
var operation = new DCFSLoadAssetBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
}
public virtual void UnloadBundleFile(PackageBundle bundle, object result)
{
AssetBundle assetBundle = result as AssetBundle;
if (assetBundle == null)
return;
if (assetBundle != null)
assetBundle.Unload(true);
if (_loadedStream.TryGetValue(bundle.BundleGUID, out Stream managedStream))
{
managedStream.Close();
managedStream.Dispose();
_loadedStream.Remove(bundle.BundleGUID);
}
}
#region 内部方法
private readonly BufferWriter _sharedBuffer = new BufferWriter(1024);
public void WriteInfoFile(string filePath, string dataFileCRC, long dataFileSize)
{
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
_sharedBuffer.Clear();
_sharedBuffer.WriteUTF8(dataFileCRC);
_sharedBuffer.WriteInt64(dataFileSize);
_sharedBuffer.WriteToStream(fs);
fs.Flush();
}
}
public void ReadInfoFile(string filePath, out string dataFileCRC, out long dataFileSize)
{
byte[] binaryData = FileUtility.ReadAllBytes(filePath);
BufferReader buffer = new BufferReader(binaryData);
dataFileCRC = buffer.ReadUTF8();
dataFileSize = buffer.ReadInt64();
}
protected string GetDefaultRoot()
{
#if UNITY_EDITOR
// 注意:为了方便调试查看,编辑器下把存储目录放到项目里。
string projectPath = Path.GetDirectoryName(UnityEngine.Application.dataPath);
projectPath = PathUtility.RegularPath(projectPath);
return PathUtility.Combine(projectPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#elif UNITY_STANDALONE
return PathUtility.Combine(UnityEngine.Application.dataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#else
return PathUtility.Combine(UnityEngine.Application.persistentDataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
#endif
}
protected string GetDataFilePath(PackageBundle bundle)
{
if (_dataFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{
string folderName = bundle.FileHash.Substring(0, 2);
filePath = PathUtility.Combine(_saveFileRoot, folderName, bundle.BundleGUID, DefaultCacheFileSystemDefine.SaveBundleDataFileName);
if (AppendFileExtension)
filePath += bundle.FileExtension;
_dataFilePaths.Add(bundle.BundleGUID, filePath);
}
return filePath;
}
protected string GetInfoFilePath(PackageBundle bundle)
{
if (_infoFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{
string folderName = bundle.FileHash.Substring(0, 2);
filePath = PathUtility.Combine(_saveFileRoot, folderName, bundle.BundleGUID, DefaultCacheFileSystemDefine.SaveBundleInfoFileName);
_infoFilePaths.Add(bundle.BundleGUID, filePath);
}
return filePath;
}
public string GetTempFilePath(PackageBundle bundle)
{
if (_tempFilePaths.TryGetValue(bundle.BundleGUID, out string filePath) == false)
{
filePath = PathUtility.Combine(_tempFileRoot, bundle.BundleGUID);
_tempFilePaths.Add(bundle.BundleGUID, filePath);
}
return filePath;
}
public string GetFileLoadPath(PackageBundle bundle)
{
return GetDataFilePath(bundle);
}
public string GetCacheFilesRoot()
{
return _saveFileRoot;
}
public string GetCachePackageHashFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion);
return PathUtility.Combine(_manifestFileRoot, fileName);
}
public string GetCachePackageManifestFilePath(string packageVersion)
{
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion);
return PathUtility.Combine(_manifestFileRoot, fileName);
}
public string GetSandboxAppFootPrintFilePath()
{
return PathUtility.Combine(_manifestFileRoot, DefaultCacheFileSystemDefine.AppFootPrintFileName);
}
public void DeleteAllManifestFiles()
{
if (Directory.Exists(_manifestFileRoot))
{
Directory.Delete(_manifestFileRoot, true);
}
}
public List<string> GetAllCachedBundleGUIDs()
{
return _wrappers.Keys.ToList();
}
/// <summary>
/// 记录缓存信息
/// </summary>
public bool Record(string bundleGUID, FileWrapper wrapper)
{
if (Exists(bundleGUID))
{
YooLogger.Error($"{nameof(DefaultCacheFileSystem)} has element : {bundleGUID}");
return false;
}
_wrappers.Add(bundleGUID, wrapper);
return true;
}
#endregion
}
}

View File

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

View File

@ -0,0 +1,36 @@

namespace YooAsset
{
internal class DefaultCacheFileSystemDefine
{
/// <summary>
/// 保存的数据文件名称
/// </summary>
public const string SaveBundleDataFileName = "__data";
/// <summary>
/// 保存的信息文件名称
/// </summary>
public const string SaveBundleInfoFileName = "__info";
/// <summary>
/// 保存的资源文件的文件夹名称
/// </summary>
public const string SaveFilesFolderName = "CacheFiles";
/// <summary>
/// 下载的临时文件的文件夹名称
/// </summary>
public const string TempFilesFolderName = "CacheTempFiles";
/// <summary>
/// 保存的清单文件的文件夹名称
/// </summary>
public const string ManifestFilesFolderName = "ManifestFiles";
/// <summary>
/// 记录应用程序版本的文件名称
/// </summary>
public const string AppFootPrintFileName = "ApplicationFootPrint.bytes";
}
}

View File

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

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 09e4cebb2073ab945ac09e9099b327ad
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,13 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace YooAsset
{
/// <summary>
/// 清理本地包裹所有的缓存文件
/// </summary>
public sealed class ClearAllCacheFilesOperation : AsyncOperationBase
internal sealed class DCFSClearAllBundleFilesOperation : FSClearAllBundleFilesOperation
{
private enum ESteps
{
@ -17,14 +13,15 @@ namespace YooAsset
Done,
}
private readonly CacheManager _cache;
private List<string> _allCacheGUIDs;
private readonly DefaultCacheFileSystem _fileSystem;
private List<string> _allBundleGUIDs;
private int _fileTotalCount = 0;
private ESteps _steps = ESteps.None;
internal ClearAllCacheFilesOperation(CacheManager cache)
internal DCFSClearAllBundleFilesOperation(DefaultCacheFileSystem fileSystem)
{
_cache = cache;
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
@ -37,20 +34,19 @@ namespace YooAsset
if (_steps == ESteps.GetAllCacheFiles)
{
_allCacheGUIDs = _cache.GetAllCachedGUIDs();
_fileTotalCount = _allCacheGUIDs.Count;
_allBundleGUIDs = _fileSystem.GetAllCachedBundleGUIDs();
_fileTotalCount = _allBundleGUIDs.Count;
YooLogger.Log($"Found all cache file count : {_fileTotalCount}");
_steps = ESteps.ClearAllCacheFiles;
}
if (_steps == ESteps.ClearAllCacheFiles)
{
for (int i = _allCacheGUIDs.Count - 1; i >= 0; i--)
for (int i = _allBundleGUIDs.Count - 1; i >= 0; i--)
{
string cacheGUID = _allCacheGUIDs[i];
_cache.Discard(cacheGUID);
_allCacheGUIDs.RemoveAt(i);
string bundleGUID = _allBundleGUIDs[i];
_fileSystem.DeleteFile(bundleGUID);
_allBundleGUIDs.RemoveAt(i);
if (OperationSystem.IsBusy)
break;
}
@ -58,9 +54,9 @@ namespace YooAsset
if (_fileTotalCount == 0)
Progress = 1.0f;
else
Progress = 1.0f - (_allCacheGUIDs.Count / _fileTotalCount);
Progress = 1.0f - (_allBundleGUIDs.Count / _fileTotalCount);
if (_allCacheGUIDs.Count == 0)
if (_allBundleGUIDs.Count == 0)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;

View File

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

View File

@ -1,13 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace YooAsset
{
/// <summary>
/// 清理本地包裹未使用的缓存文件
/// </summary>
public sealed class ClearUnusedCacheFilesOperation : AsyncOperationBase
internal sealed class DCFSClearUnusedBundleFilesOperation : FSClearUnusedBundleFilesOperation
{
private enum ESteps
{
@ -17,16 +13,17 @@ namespace YooAsset
Done,
}
private readonly ResourcePackage _package;
private readonly CacheManager _cache;
private List<string> _unusedCacheGUIDs;
private readonly DefaultCacheFileSystem _fileSystem;
private readonly PackageManifest _manifest;
private List<string> _unusedBundleGUIDs;
private int _unusedFileTotalCount = 0;
private ESteps _steps = ESteps.None;
internal ClearUnusedCacheFilesOperation(ResourcePackage package, CacheManager cache)
internal DCFSClearUnusedBundleFilesOperation(DefaultCacheFileSystem fileSystem, PackageManifest manifest)
{
_package = package;
_cache = cache;
_fileSystem = fileSystem;
_manifest = manifest;
}
internal override void InternalOnStart()
{
@ -39,20 +36,19 @@ namespace YooAsset
if (_steps == ESteps.GetUnusedCacheFiles)
{
_unusedCacheGUIDs = GetUnusedCacheGUIDs();
_unusedFileTotalCount = _unusedCacheGUIDs.Count;
YooLogger.Log($"Found unused cache file count : {_unusedFileTotalCount}");
_steps = ESteps.ClearUnusedCacheFiles;
_unusedBundleGUIDs = GetUnusedBundleGUIDs();
_unusedFileTotalCount = _unusedBundleGUIDs.Count;
YooLogger.Log($"Found unused cache file count : {_unusedFileTotalCount}");
}
if (_steps == ESteps.ClearUnusedCacheFiles)
{
for (int i = _unusedCacheGUIDs.Count - 1; i >= 0; i--)
for (int i = _unusedBundleGUIDs.Count - 1; i >= 0; i--)
{
string cacheGUID = _unusedCacheGUIDs[i];
_cache.Discard(cacheGUID);
_unusedCacheGUIDs.RemoveAt(i);
string bundleGUID = _unusedBundleGUIDs[i];
_fileSystem.DeleteFile(bundleGUID);
_unusedBundleGUIDs.RemoveAt(i);
if (OperationSystem.IsBusy)
break;
}
@ -60,9 +56,9 @@ namespace YooAsset
if (_unusedFileTotalCount == 0)
Progress = 1.0f;
else
Progress = 1.0f - (_unusedCacheGUIDs.Count / _unusedFileTotalCount);
Progress = 1.0f - (_unusedBundleGUIDs.Count / _unusedFileTotalCount);
if (_unusedCacheGUIDs.Count == 0)
if (_unusedBundleGUIDs.Count == 0)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
@ -70,15 +66,15 @@ namespace YooAsset
}
}
private List<string> GetUnusedCacheGUIDs()
private List<string> GetUnusedBundleGUIDs()
{
var allCacheGUIDs = _cache.GetAllCachedGUIDs();
List<string> result = new List<string>(allCacheGUIDs.Count);
foreach (var cacheGUID in allCacheGUIDs)
var allBundleGUIDs = _fileSystem.GetAllCachedBundleGUIDs();
List<string> result = new List<string>(allBundleGUIDs.Count);
foreach (var bundleGUID in allBundleGUIDs)
{
if (_package.IsIncludeBundleFile(cacheGUID) == false)
if (_manifest.IsIncludeBundleFile(bundleGUID) == false)
{
result.Add(cacheGUID);
result.Add(bundleGUID);
}
}
return result;

View File

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

View File

@ -0,0 +1,432 @@
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
internal sealed class DCFSDownloadNormalFileOperation : DefaultDownloadFileOperation
{
private readonly DefaultCacheFileSystem _fileSystem;
private VerifyTempFileOperation _verifyOperation;
private string _fileSavePath;
private ESteps _steps = ESteps.None;
internal DCFSDownloadNormalFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, string mainURL, string fallbackURL, int failedTryAgain, int timeout)
: base(bundle, mainURL, fallbackURL, failedTryAgain, timeout)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_fileSavePath = _fileSystem.GetTempFilePath(Bundle);
// 注意:检测文件是否存在
if (_fileSystem.Exists(Bundle))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.CreateRequest;
}
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
FileUtility.CreateFileDirectory(_fileSavePath);
// 获取请求地址
_requestURL = GetRequestURL();
// 重置变量
_isAbort = false;
_latestDownloadBytes = 0;
_latestDownloadRealtime = Time.realtimeSinceStartup;
DownloadProgress = 0f;
DownloadedBytes = 0;
// 重置计时器
if (_tryAgainTimer > 0f)
YooLogger.Warning($"Try again download : {_requestURL}");
_tryAgainTimer = 0f;
// 删除临时文件
if (File.Exists(_fileSavePath))
File.Delete(_fileSavePath);
// 创建下载器
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())
_steps = ESteps.VerifyTempFile;
else
_steps = ESteps.TryAgain;
// 注意:最终释放请求器
DisposeWebRequest();
}
// 验证下载文件
if (_steps == ESteps.VerifyTempFile)
{
var element = new TempFileElement(_fileSavePath, Bundle.FileCRC, Bundle.FileSize);
_verifyOperation = new VerifyTempFileOperation(element);
OperationSystem.StartOperation(_fileSystem.PackageName, _verifyOperation);
_steps = ESteps.CheckVerifyTempFile;
}
// 等待验证完成
if (_steps == ESteps.CheckVerifyTempFile)
{
// 注意:同步解压文件更新
_verifyOperation.InternalOnUpdate();
if (_verifyOperation.IsDone == false)
return;
if (_verifyOperation.Status == EOperationStatus.Succeed)
{
if (_fileSystem.WriteFile(Bundle, _fileSavePath))
{
Status = EOperationStatus.Succeed;
_steps = ESteps.Done;
}
else
{
Error = $"{_fileSystem.GetType().FullName} write file failed !";
Status = EOperationStatus.Failed;
_steps = ESteps.Done;
YooLogger.Error(Error);
}
}
else
{
Error = _verifyOperation.Error;
_steps = ESteps.TryAgain;
}
// 注意:验证完成后直接删除文件
if (File.Exists(_fileSavePath))
File.Delete(_fileSavePath);
}
// 重新尝试下载
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();
}
public override void WaitForAsyncComplete()
{
while (true)
{
// 文件验证
if (_verifyOperation != null)
{
if (_verifyOperation.IsDone == false)
_verifyOperation.WaitForAsyncComplete();
}
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}
}
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
DownloadHandlerFile handler = new DownloadHandlerFile(_fileSavePath);
handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
}
internal sealed class DCFSDownloadResumeFileOperation : DefaultDownloadFileOperation
{
private readonly DefaultCacheFileSystem _fileSystem;
private DownloadHandlerFileRange _downloadHandle;
private VerifyTempFileOperation _verifyOperation;
private long _fileOriginLength = 0;
private string _fileSavePath;
private ESteps _steps = ESteps.None;
internal DCFSDownloadResumeFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, string mainURL, string fallbackURL, int failedTryAgain, int timeout)
: base(bundle, mainURL, fallbackURL, failedTryAgain, timeout)
{
_fileSystem = fileSystem;
}
internal override void InternalOnStart()
{
_fileSavePath = _fileSystem.GetTempFilePath(Bundle);
// 注意:检测文件是否存在
if (_fileSystem.Exists(Bundle))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.CreateRequest;
}
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
FileUtility.CreateFileDirectory(_fileSavePath);
// 获取请求地址
_requestURL = GetRequestURL();
// 重置变量
_isAbort = false;
_latestDownloadBytes = 0;
_latestDownloadRealtime = Time.realtimeSinceStartup;
_fileOriginLength = 0;
DownloadProgress = 0f;
DownloadedBytes = 0;
// 重置计时器
if (_tryAgainTimer > 0f)
YooLogger.Warning($"Try again download : {_requestURL}");
_tryAgainTimer = 0f;
// 获取下载起始位置
long fileBeginLength = -1;
if (File.Exists(_fileSavePath))
{
FileInfo fileInfo = new FileInfo(_fileSavePath);
fileBeginLength = fileInfo.Length;
_fileOriginLength = fileBeginLength;
DownloadedBytes = _fileOriginLength;
}
// 检测下载起始位置
if (fileBeginLength >= Bundle.FileSize)
{
// 删除临时文件
if (File.Exists(_fileSavePath))
File.Delete(_fileSavePath);
}
// 创建下载器
CreateWebRequest(fileBeginLength);
_steps = ESteps.CheckRequest;
}
// 检测下载结果
if (_steps == ESteps.CheckRequest)
{
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = _fileOriginLength + (long)_webRequest.downloadedBytes;
if (_webRequest.isDone == false)
{
CheckRequestTimeout();
return;
}
// 检查网络错误
if (CheckRequestResult())
_steps = ESteps.VerifyTempFile;
else
_steps = ESteps.TryAgain;
// 在遇到特殊错误的时候删除文件
ClearTempFileWhenError();
// 注意:最终释放请求器
DisposeWebRequest();
}
// 验证下载文件
if (_steps == ESteps.VerifyTempFile)
{
var element = new TempFileElement(_fileSavePath, Bundle.FileCRC, Bundle.FileSize);
_verifyOperation = new VerifyTempFileOperation(element);
OperationSystem.StartOperation(_fileSystem.PackageName, _verifyOperation);
_steps = ESteps.CheckVerifyTempFile;
}
// 等待验证完成
if (_steps == ESteps.CheckVerifyTempFile)
{
if (_verifyOperation.IsDone == false)
return;
if (_verifyOperation.Status == EOperationStatus.Succeed)
{
if (_fileSystem.WriteFile(Bundle, _fileSavePath))
{
Status = EOperationStatus.Succeed;
_steps = ESteps.Done;
}
else
{
Error = $"{_fileSystem.GetType().FullName} write file failed : {_fileSavePath}";
Status = EOperationStatus.Failed;
_steps = ESteps.Done;
}
}
else
{
Error = _verifyOperation.Error;
_steps = ESteps.TryAgain;
}
// 注意:验证完成后直接删除文件
if (File.Exists(_fileSavePath))
File.Delete(_fileSavePath);
}
// 重新尝试下载
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();
}
public override void WaitForAsyncComplete()
{
while (true)
{
// 文件验证
if (_verifyOperation != null)
{
if (_verifyOperation.IsDone == false)
_verifyOperation.WaitForAsyncComplete();
}
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}
}
private void CreateWebRequest(long beginLength)
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
#if UNITY_2019_4_OR_NEWER
var handler = new DownloadHandlerFile(_fileSavePath, true);
handler.removeFileOnAbort = false;
#else
var handler = new DownloadHandlerFileRange(FileSavePath, Bundle.FileSize, _webRequest);
_downloadHandle = handler;
#endif
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
if (beginLength > 0)
_webRequest.SetRequestHeader("Range", $"bytes={beginLength}-");
_webRequest.SendWebRequest();
}
private void DisposeWebRequest()
{
if (_downloadHandle != null)
{
_downloadHandle.Cleanup();
_downloadHandle = null;
}
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
private void ClearTempFileWhenError()
{
if (_fileSystem.ResumeDownloadResponseCodes == null)
return;
//说明:如果遇到以下错误返回码,验证失败直接删除文件
if (_fileSystem.ResumeDownloadResponseCodes.Contains(HttpCode))
{
if (File.Exists(_fileSavePath))
File.Delete(_fileSavePath);
}
}
}
}

View File

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

View File

@ -0,0 +1,87 @@
using System.IO;
using UnityEngine;
namespace YooAsset
{
internal class DCFSInitializeOperation : FSInitializeFileSystemOperation
{
private enum ESteps
{
None,
CheckAppFootPrint,
SearchCacheFiles,
VerifyCacheFiles,
Done,
}
private readonly DefaultCacheFileSystem _fileSytem;
private SearchCacheFilesOperation _searchCacheFilesOp;
private VerifyCacheFilesOperation _verifyCacheFilesOp;
private ESteps _steps = ESteps.None;
internal DCFSInitializeOperation(DefaultCacheFileSystem fileSystem)
{
_fileSytem = fileSystem;
}
internal override void InternalOnStart()
{
_steps = ESteps.CheckAppFootPrint;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CheckAppFootPrint)
{
var appFootPrint = new ApplicationFootPrint(_fileSytem);
appFootPrint.Load(_fileSytem.PackageName);
// 如果水印发生变化,则说明覆盖安装后首次打开游戏
if (appFootPrint.IsDirty())
{
_fileSytem.DeleteAllManifestFiles();
appFootPrint.Coverage(_fileSytem.PackageName);
YooLogger.Log("Delete manifest files when application foot print dirty !");
}
_steps = ESteps.SearchCacheFiles;
}
if (_steps == ESteps.SearchCacheFiles)
{
if (_searchCacheFilesOp == null)
{
_searchCacheFilesOp = new SearchCacheFilesOperation(_fileSytem);
OperationSystem.StartOperation(_fileSytem.PackageName, _searchCacheFilesOp);
}
Progress = _searchCacheFilesOp.Progress;
if (_searchCacheFilesOp.IsDone == false)
return;
_steps = ESteps.VerifyCacheFiles;
}
if (_steps == ESteps.VerifyCacheFiles)
{
if (_verifyCacheFilesOp == null)
{
_verifyCacheFilesOp = new VerifyCacheFilesOperation(_fileSytem, _searchCacheFilesOp.Result);
OperationSystem.StartOperation(_fileSytem.PackageName, _verifyCacheFilesOp);
}
Progress = _verifyCacheFilesOp.Progress;
if (_verifyCacheFilesOp.IsDone == false)
return;
// 注意:总是返回成功
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
YooLogger.Log($"Package '{_fileSytem.PackageName}' cached files count : {_fileSytem.FileCount}");
}
}
}
}

Some files were not shown because too many files have changed in this diff Show More