mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-26 02:30:18 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化操作
|
||||
/// </summary>
|
||||
public abstract class InitializationOperation : AsyncOperationBase
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编辑器下模拟运行的初始化操作
|
||||
/// </summary>
|
||||
internal class EditorModeInitializationOperation : InitializationOperation
|
||||
{
|
||||
internal override void Start()
|
||||
{
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
internal override void Update()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 离线模式的初始化操作
|
||||
/// </summary>
|
||||
internal class OfflinePlayModeInitializationOperation : InitializationOperation
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
Idle,
|
||||
LoadAppManifest,
|
||||
CheckAppManifest,
|
||||
Done,
|
||||
}
|
||||
|
||||
private OfflinePlayModeImpl _impl;
|
||||
private ESteps _steps = ESteps.Idle;
|
||||
private UnityWebRequester _downloader;
|
||||
private string _downloadURL;
|
||||
|
||||
internal OfflinePlayModeInitializationOperation(OfflinePlayModeImpl impl)
|
||||
{
|
||||
_impl = impl;
|
||||
}
|
||||
internal override void Start()
|
||||
{
|
||||
_steps = ESteps.LoadAppManifest;
|
||||
}
|
||||
internal override void Update()
|
||||
{
|
||||
if (_steps == ESteps.Idle)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.LoadAppManifest)
|
||||
{
|
||||
string filePath = AssetPathHelper.MakeStreamingLoadPath(ResourceSettingData.Setting.PatchManifestFileName);
|
||||
_downloadURL = AssetPathHelper.ConvertToWWWPath(filePath);
|
||||
_downloader = new UnityWebRequester();
|
||||
_downloader.SendRequest(_downloadURL);
|
||||
_steps = ESteps.CheckAppManifest;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.CheckAppManifest)
|
||||
{
|
||||
if (_downloader.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (_downloader.HasError())
|
||||
{
|
||||
Error = _downloader.GetError();
|
||||
Status = EOperationStatus.Failed;
|
||||
_downloader.Dispose();
|
||||
_steps = ESteps.Done;
|
||||
throw new System.Exception($"Fatal error : Failed load application patch manifest file : {_downloadURL}");
|
||||
}
|
||||
|
||||
// 解析APP里的补丁清单
|
||||
_impl.AppPatchManifest = PatchManifest.Deserialize(_downloader.GetText());
|
||||
_downloader.Dispose();
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 网络模式的初始化操作
|
||||
/// </summary>
|
||||
internal class HostPlayModeInitializationOperation : InitializationOperation
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
Idle,
|
||||
InitCache,
|
||||
LoadAppManifest,
|
||||
CheckAppManifest,
|
||||
LoadSandboxManifest,
|
||||
Done,
|
||||
}
|
||||
|
||||
private HostPlayModeImpl _impl;
|
||||
private ESteps _steps = ESteps.Idle;
|
||||
private UnityWebRequester _downloader;
|
||||
private string _downloadURL;
|
||||
|
||||
internal HostPlayModeInitializationOperation(HostPlayModeImpl impl)
|
||||
{
|
||||
_impl = impl;
|
||||
}
|
||||
internal override void Start()
|
||||
{
|
||||
_steps = ESteps.InitCache;
|
||||
}
|
||||
internal override void Update()
|
||||
{
|
||||
if (_steps == ESteps.Idle)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.InitCache)
|
||||
{
|
||||
// 每次启动时比对APP版本号是否一致
|
||||
PatchCache cache = PatchCache.LoadCache();
|
||||
if (cache.CacheAppVersion != Application.version)
|
||||
{
|
||||
Logger.Warning($"Cache is dirty ! Cache app version is {cache.CacheAppVersion}, Current app version is {Application.version}");
|
||||
|
||||
// 注意:在覆盖安装的时候,会保留APP沙盒目录,可以选择清空缓存目录
|
||||
if (_impl.ClearCacheWhenDirty)
|
||||
{
|
||||
Logger.Warning("Clear cache files.");
|
||||
PatchHelper.DeleteSandboxCacheFolder();
|
||||
}
|
||||
|
||||
// 删除清单文件
|
||||
PatchHelper.DeleteSandboxPatchManifestFile();
|
||||
// 更新缓存文件
|
||||
PatchCache.UpdateCache();
|
||||
}
|
||||
_steps = ESteps.LoadAppManifest;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.LoadAppManifest)
|
||||
{
|
||||
// 加载APP内的补丁清单
|
||||
Logger.Log($"Load application patch manifest.");
|
||||
string filePath = AssetPathHelper.MakeStreamingLoadPath(ResourceSettingData.Setting.PatchManifestFileName);
|
||||
_downloadURL = AssetPathHelper.ConvertToWWWPath(filePath);
|
||||
_downloader = new UnityWebRequester();
|
||||
_downloader.SendRequest(_downloadURL);
|
||||
_steps = ESteps.CheckAppManifest;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.CheckAppManifest)
|
||||
{
|
||||
if (_downloader.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (_downloader.HasError())
|
||||
{
|
||||
Error = _downloader.GetError();
|
||||
Status = EOperationStatus.Failed;
|
||||
_downloader.Dispose();
|
||||
_steps = ESteps.Done;
|
||||
throw new System.Exception($"Fatal error : Failed load application patch manifest file : {_downloadURL}");
|
||||
}
|
||||
|
||||
// 解析补丁清单
|
||||
string jsonData = _downloader.GetText();
|
||||
_impl.AppPatchManifest = PatchManifest.Deserialize(jsonData);
|
||||
_impl.LocalPatchManifest = _impl.AppPatchManifest;
|
||||
_downloader.Dispose();
|
||||
_steps = ESteps.LoadSandboxManifest;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.LoadSandboxManifest)
|
||||
{
|
||||
// 加载沙盒内的补丁清单
|
||||
if (PatchHelper.CheckSandboxPatchManifestFileExist())
|
||||
{
|
||||
Logger.Log($"Load sandbox patch manifest.");
|
||||
string filePath = AssetPathHelper.MakePersistentLoadPath(ResourceSettingData.Setting.PatchManifestFileName);
|
||||
string jsonData = File.ReadAllText(filePath);
|
||||
_impl.LocalPatchManifest = PatchManifest.Deserialize(jsonData);
|
||||
}
|
||||
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a44ad228b117d0047ab80e7a442459b4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,308 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 更新清单操作
|
||||
/// </summary>
|
||||
public abstract class UpdateManifestOperation : AsyncOperationBase
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编辑器下模拟运行的更新清单操作
|
||||
/// </summary>
|
||||
internal class EditorModeUpdateManifestOperation : UpdateManifestOperation
|
||||
{
|
||||
internal override void Start()
|
||||
{
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
internal override void Update()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 离线模式的更新清单操作
|
||||
/// </summary>
|
||||
internal class OfflinePlayModeUpdateManifestOperation : UpdateManifestOperation
|
||||
{
|
||||
internal override void Start()
|
||||
{
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
internal override void Update()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 网络模式的更新清单操作
|
||||
/// </summary>
|
||||
internal class HostPlayModeUpdateManifestOperation : UpdateManifestOperation
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
Idle,
|
||||
LoadWebManifestHash,
|
||||
CheckWebManifestHash,
|
||||
LoadWebManifest,
|
||||
CheckWebManifest,
|
||||
InitPrepareCache,
|
||||
UpdatePrepareCache,
|
||||
Done,
|
||||
}
|
||||
|
||||
private static int RequestCount = 0;
|
||||
|
||||
private readonly HostPlayModeImpl _impl;
|
||||
private readonly int _updateResourceVersion;
|
||||
private readonly int _timeout;
|
||||
private ESteps _steps = ESteps.Idle;
|
||||
private UnityWebRequester _downloaderHash;
|
||||
private UnityWebRequester _downloaderManifest;
|
||||
private float _verifyTime;
|
||||
|
||||
public HostPlayModeUpdateManifestOperation(HostPlayModeImpl impl, int updateResourceVersion, int timeout)
|
||||
{
|
||||
_impl = impl;
|
||||
_updateResourceVersion = updateResourceVersion;
|
||||
_timeout = timeout;
|
||||
}
|
||||
internal override void Start()
|
||||
{
|
||||
RequestCount++;
|
||||
_steps = ESteps.LoadWebManifestHash;
|
||||
|
||||
if (_impl.IgnoreResourceVersion && _updateResourceVersion > 0)
|
||||
{
|
||||
Logger.Warning($"Update resource version {_updateResourceVersion} is invalid when ignore resource version.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Log($"Update patch manifest : update resource version is {_updateResourceVersion}");
|
||||
}
|
||||
}
|
||||
internal override void Update()
|
||||
{
|
||||
if (_steps == ESteps.Idle)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.LoadWebManifestHash)
|
||||
{
|
||||
string webURL = GetPatchManifestRequestURL(_updateResourceVersion, ResourceSettingData.Setting.PatchManifestHashFileName);
|
||||
Logger.Log($"Beginning to request patch manifest hash : {webURL}");
|
||||
_downloaderHash = new UnityWebRequester();
|
||||
_downloaderHash.SendRequest(webURL, _timeout);
|
||||
_steps = ESteps.CheckWebManifestHash;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.CheckWebManifestHash)
|
||||
{
|
||||
if (_downloaderHash.IsDone() == false)
|
||||
return;
|
||||
|
||||
// Check fatal
|
||||
if (_downloaderHash.HasError())
|
||||
{
|
||||
Error = _downloaderHash.GetError();
|
||||
Status = EOperationStatus.Failed;
|
||||
_downloaderHash.Dispose();
|
||||
_steps = ESteps.Done;
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取补丁清单文件的哈希值
|
||||
string webManifestHash = _downloaderHash.GetText();
|
||||
_downloaderHash.Dispose();
|
||||
|
||||
// 如果补丁清单文件的哈希值相同
|
||||
string currentFileHash = PatchHelper.GetSandboxPatchManifestFileHash();
|
||||
if (currentFileHash == webManifestHash)
|
||||
{
|
||||
Logger.Log($"Patch manifest file hash is not change : {webManifestHash}");
|
||||
_steps = ESteps.InitPrepareCache;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Log($"Patch manifest hash is change : {webManifestHash} -> {currentFileHash}");
|
||||
_steps = ESteps.LoadWebManifest;
|
||||
}
|
||||
}
|
||||
|
||||
if (_steps == ESteps.LoadWebManifest)
|
||||
{
|
||||
string webURL = GetPatchManifestRequestURL(_updateResourceVersion, ResourceSettingData.Setting.PatchManifestFileName);
|
||||
Logger.Log($"Beginning to request patch manifest : {webURL}");
|
||||
_downloaderManifest = new UnityWebRequester();
|
||||
_downloaderManifest.SendRequest(webURL, _timeout);
|
||||
_steps = ESteps.CheckWebManifest;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.CheckWebManifest)
|
||||
{
|
||||
if (_downloaderManifest.IsDone() == false)
|
||||
return;
|
||||
|
||||
// Check fatal
|
||||
if (_downloaderManifest.HasError())
|
||||
{
|
||||
Error = _downloaderManifest.GetError();
|
||||
Status = EOperationStatus.Failed;
|
||||
_downloaderManifest.Dispose();
|
||||
_steps = ESteps.Done;
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析补丁清单
|
||||
ParseAndSaveRemotePatchManifest(_downloaderManifest.GetText());
|
||||
_downloaderManifest.Dispose();
|
||||
_steps = ESteps.InitPrepareCache;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.InitPrepareCache)
|
||||
{
|
||||
InitPrepareCache();
|
||||
_verifyTime = UnityEngine.Time.realtimeSinceStartup;
|
||||
_steps = ESteps.UpdatePrepareCache;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.UpdatePrepareCache)
|
||||
{
|
||||
if (UpdatePrepareCache())
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyTime;
|
||||
Logger.Log($"Verify files total time : {costTime}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string GetPatchManifestRequestURL(int updateResourceVersion, string fileName)
|
||||
{
|
||||
string url;
|
||||
|
||||
// 轮流返回请求地址
|
||||
if (RequestCount % 2 == 0)
|
||||
url = _impl.GetPatchDownloadFallbackURL(updateResourceVersion, fileName);
|
||||
else
|
||||
url = _impl.GetPatchDownloadMainURL(updateResourceVersion, fileName);
|
||||
|
||||
// 注意:在URL末尾添加时间戳
|
||||
if (_impl.IgnoreResourceVersion)
|
||||
url = $"{url}?{System.DateTime.UtcNow.Ticks}";
|
||||
|
||||
return url;
|
||||
}
|
||||
private void ParseAndSaveRemotePatchManifest(string content)
|
||||
{
|
||||
_impl.LocalPatchManifest = PatchManifest.Deserialize(content);
|
||||
|
||||
// 注意:这里会覆盖掉沙盒内的补丁清单文件
|
||||
Logger.Log("Save remote patch manifest file.");
|
||||
string savePath = AssetPathHelper.MakePersistentLoadPath(ResourceSettingData.Setting.PatchManifestFileName);
|
||||
PatchManifest.Serialize(savePath, _impl.LocalPatchManifest);
|
||||
}
|
||||
|
||||
#region 多线程相关
|
||||
private class ThreadInfo
|
||||
{
|
||||
public bool Result = false;
|
||||
public string FilePath { private set; get; }
|
||||
public PatchBundle Bundle { private set; get; }
|
||||
public ThreadInfo(string filePath, PatchBundle bundle)
|
||||
{
|
||||
FilePath = filePath;
|
||||
Bundle = bundle;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly List<PatchBundle> _cacheList = new List<PatchBundle>(1000);
|
||||
private readonly List<PatchBundle> _verifyList = new List<PatchBundle>(100);
|
||||
private readonly ThreadSyncContext _syncContext = new ThreadSyncContext();
|
||||
private const int VerifyMaxCount = 32;
|
||||
|
||||
private void InitPrepareCache()
|
||||
{
|
||||
// 遍历所有文件然后验证并缓存合法文件
|
||||
foreach (var patchBundle in _impl.LocalPatchManifest.BundleList)
|
||||
{
|
||||
// 忽略缓存文件
|
||||
if (DownloadSystem.ContainsVerifyFile(patchBundle.Hash))
|
||||
continue;
|
||||
|
||||
// 忽略APP资源
|
||||
// 注意:如果是APP资源并且哈希值相同,则不需要下载
|
||||
if (_impl.AppPatchManifest.Bundles.TryGetValue(patchBundle.BundleName, out PatchBundle appPatchBundle))
|
||||
{
|
||||
if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash)
|
||||
continue;
|
||||
}
|
||||
|
||||
// 查看文件是否存在
|
||||
string filePath = PatchHelper.MakeSandboxCacheFilePath(patchBundle.Hash);
|
||||
if (File.Exists(filePath) == false)
|
||||
continue;
|
||||
|
||||
_cacheList.Add(patchBundle);
|
||||
}
|
||||
}
|
||||
private bool UpdatePrepareCache()
|
||||
{
|
||||
_syncContext.Update();
|
||||
|
||||
if (_cacheList.Count == 0 && _verifyList.Count == 0)
|
||||
return true;
|
||||
|
||||
if (_verifyList.Count >= VerifyMaxCount)
|
||||
return false;
|
||||
|
||||
for (int i = _cacheList.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (_verifyList.Count >= VerifyMaxCount)
|
||||
break;
|
||||
|
||||
var patchBundle = _cacheList[i];
|
||||
if (RunThread(patchBundle))
|
||||
{
|
||||
_cacheList.RemoveAt(i);
|
||||
_verifyList.Add(patchBundle);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Warning("Failed to run verify thread.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
private bool RunThread(PatchBundle patchBundle)
|
||||
{
|
||||
string filePath = PatchHelper.MakeSandboxCacheFilePath(patchBundle.Hash);
|
||||
ThreadInfo info = new ThreadInfo(filePath, patchBundle);
|
||||
return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyFile), info);
|
||||
}
|
||||
private void VerifyFile(object infoObj)
|
||||
{
|
||||
// 验证沙盒内的文件
|
||||
ThreadInfo info = (ThreadInfo)infoObj;
|
||||
info.Result = DownloadSystem.CheckContentIntegrity(info.FilePath, info.Bundle.SizeBytes, info.Bundle.CRC);
|
||||
_syncContext.Post(VerifyCallback, info);
|
||||
}
|
||||
private void VerifyCallback(object obj)
|
||||
{
|
||||
ThreadInfo info = (ThreadInfo)obj;
|
||||
if (info.Result)
|
||||
DownloadSystem.CacheVerifyFile(info.Bundle.Hash, info.Bundle.BundleName);
|
||||
_verifyList.Remove(info.Bundle);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 250c5da7ab03e724f8c328de5238e433
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user