Update runtime logic

pull/40/head
hevinci 2022-09-27 21:11:55 +08:00
parent e1801a5fba
commit af37bb6e54
13 changed files with 176 additions and 307 deletions

View File

@ -1,60 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace YooAsset
{
[Serializable]
internal sealed class CacheData
{
/// <summary>
/// 缓存的APP内置版本
/// </summary>
public string CacheAppVersion = string.Empty;
/// <summary>
/// 读取缓存文件
/// 注意:如果文件不存在则创建新的缓存文件
/// </summary>
public static CacheData LoadCache()
{
string filePath = GetCacheDataFilePath();
if (File.Exists(filePath))
{
string jsonData = FileUtility.ReadFile(filePath);
var cacheData = JsonUtility.FromJson<CacheData>(jsonData);
YooLogger.Log($"Load cache data : {cacheData.CacheAppVersion}");
return cacheData;
}
else
{
YooLogger.Log($"Create cache data : {Application.version}");
CacheData cacheData = new CacheData();
cacheData.CacheAppVersion = Application.version;
string jsonData = JsonUtility.ToJson(cacheData);
FileUtility.CreateFile(filePath, jsonData);
return cacheData;
}
}
/// <summary>
/// 更新缓存文件
/// </summary>
public static void UpdateCache()
{
YooLogger.Log($"Update cache data to disk : {Application.version}");
CacheData cacheData = new CacheData();
cacheData.CacheAppVersion = Application.version;
string filePath = GetCacheDataFilePath();
string jsonData = JsonUtility.ToJson(cacheData);
FileUtility.CreateFile(filePath, jsonData);
}
private static string GetCacheDataFilePath()
{
return PathHelper.MakePersistentLoadPath("CacheData.bytes");
}
}
}

View File

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

View File

@ -89,12 +89,13 @@ namespace YooAsset
} }
private readonly OfflinePlayModeImpl _impl; private readonly OfflinePlayModeImpl _impl;
private readonly AppManifestLoader _appManifestLoader = new AppManifestLoader(); private AppManifestLoader _appManifestLoader;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
internal OfflinePlayModeInitializationOperation(OfflinePlayModeImpl impl) internal OfflinePlayModeInitializationOperation(OfflinePlayModeImpl impl, string buildinPackageName)
{ {
_impl = impl; _impl = impl;
_appManifestLoader = new AppManifestLoader(buildinPackageName);
} }
internal override void Start() internal override void Start()
{ {
@ -136,51 +137,32 @@ namespace YooAsset
private enum ESteps private enum ESteps
{ {
None, None,
InitCache,
LoadManifest, LoadManifest,
CopyManifest, CopyManifest,
Done, Done,
} }
private readonly HostPlayModeImpl _impl; private readonly HostPlayModeImpl _impl;
private readonly AppManifestLoader _appManifestLoader = new AppManifestLoader(); private readonly string _buildinPackageName;
private readonly AppManifestCopyer _appManifestCopyer = new AppManifestCopyer(); private AppManifestLoader _appManifestLoader;
private AppManifestCopyer _appManifestCopyer;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
internal HostPlayModeInitializationOperation(HostPlayModeImpl impl) internal HostPlayModeInitializationOperation(HostPlayModeImpl impl, string buildinPackageName)
{ {
_impl = impl; _impl = impl;
_buildinPackageName = buildinPackageName;
_appManifestLoader = new AppManifestLoader(buildinPackageName);
} }
internal override void Start() internal override void Start()
{ {
_steps = ESteps.InitCache; _steps = ESteps.LoadManifest;
} }
internal override void Update() internal override void Update()
{ {
if (_steps == ESteps.None || _steps == ESteps.Done) if (_steps == ESteps.None || _steps == ESteps.Done)
return; return;
if (_steps == ESteps.InitCache)
{
// 每次启动时比对APP版本号是否一致
CacheData cacheData = CacheData.LoadCache();
if (cacheData.CacheAppVersion != Application.version)
{
YooLogger.Warning($"Cache is dirty ! Cache application version is {cacheData.CacheAppVersion}, Current application version is {Application.version}");
// 注意在覆盖安装的时候会保留APP沙盒目录可以选择清空缓存目录
if (_impl.ClearCacheWhenDirty)
{
YooLogger.Warning("Clear cache files.");
SandboxHelper.DeleteCacheFolder();
}
// 更新缓存文件
CacheData.UpdateCache();
}
_steps = ESteps.LoadManifest;
}
if (_steps == ESteps.LoadManifest) if (_steps == ESteps.LoadManifest)
{ {
_appManifestLoader.Update(); _appManifestLoader.Update();
@ -198,7 +180,7 @@ namespace YooAsset
{ {
_impl.SetAppPatchManifest(_appManifestLoader.Result); _impl.SetAppPatchManifest(_appManifestLoader.Result);
_impl.SetLocalPatchManifest(_appManifestLoader.Result); _impl.SetLocalPatchManifest(_appManifestLoader.Result);
_appManifestCopyer.Init(_appManifestLoader.StaticVersion); _appManifestCopyer = new AppManifestCopyer(_buildinPackageName, _appManifestLoader.BuildinPackageCRC);
_steps = ESteps.CopyManifest; _steps = ESteps.CopyManifest;
} }
} }
@ -239,6 +221,7 @@ namespace YooAsset
Done, Done,
} }
private string _buildinPackageName;
private ESteps _steps = ESteps.LoadStaticVersion; private ESteps _steps = ESteps.LoadStaticVersion;
private UnityWebDataRequester _downloader1; private UnityWebDataRequester _downloader1;
private UnityWebDataRequester _downloader2; private UnityWebDataRequester _downloader2;
@ -254,9 +237,15 @@ namespace YooAsset
public PatchManifest Result { private set; get; } public PatchManifest Result { private set; get; }
/// <summary> /// <summary>
/// 内置补丁清单版本号 /// 内置补丁清单CRC
/// </summary> /// </summary>
public int StaticVersion { private set; get; } public string BuildinPackageCRC { private set; get; }
public AppManifestLoader(string buildinPackageName)
{
_buildinPackageName = buildinPackageName;
}
/// <summary> /// <summary>
/// 是否已经完成 /// 是否已经完成
@ -287,7 +276,8 @@ namespace YooAsset
if (_steps == ESteps.LoadStaticVersion) if (_steps == ESteps.LoadStaticVersion)
{ {
YooLogger.Log($"Load application static version."); YooLogger.Log($"Load application static version.");
string filePath = PathHelper.MakeStreamingLoadPath(YooAssetSettings.VersionFileName); string fileName = YooAssetSettingsData.GetStaticVersionFileName(_buildinPackageName);
string filePath = PathHelper.MakeStreamingLoadPath(fileName);
string url = PathHelper.ConvertToWWWPath(filePath); string url = PathHelper.ConvertToWWWPath(filePath);
_downloader1 = new UnityWebDataRequester(); _downloader1 = new UnityWebDataRequester();
_downloader1.SendRequest(url); _downloader1.SendRequest(url);
@ -306,7 +296,7 @@ namespace YooAsset
} }
else else
{ {
StaticVersion = int.Parse(_downloader1.GetText()); BuildinPackageCRC = _downloader1.GetText();
_steps = ESteps.LoadAppManifest; _steps = ESteps.LoadAppManifest;
} }
_downloader1.Dispose(); _downloader1.Dispose();
@ -315,7 +305,8 @@ namespace YooAsset
if (_steps == ESteps.LoadAppManifest) if (_steps == ESteps.LoadAppManifest)
{ {
YooLogger.Log($"Load application patch manifest."); YooLogger.Log($"Load application patch manifest.");
string filePath = PathHelper.MakeStreamingLoadPath(YooAssetSettingsData.GetPatchManifestFileName(StaticVersion)); string fileName = YooAssetSettingsData.GetPatchManifestFileName(_buildinPackageName, BuildinPackageCRC);
string filePath = PathHelper.MakeStreamingLoadPath(fileName);
string url = PathHelper.ConvertToWWWPath(filePath); string url = PathHelper.ConvertToWWWPath(filePath);
_downloader2 = new UnityWebDataRequester(); _downloader2 = new UnityWebDataRequester();
_downloader2.SendRequest(url); _downloader2.SendRequest(url);
@ -355,10 +346,10 @@ namespace YooAsset
Done, Done,
} }
private string _buildinPackageName;
private string _buildinPackageCRC;
private ESteps _steps = ESteps.CopyAppManifest; private ESteps _steps = ESteps.CopyAppManifest;
private UnityWebFileRequester _downloader1; private UnityWebFileRequester _downloader1;
private int _staticVersion;
/// <summary> /// <summary>
/// 错误日志 /// 错误日志
@ -370,20 +361,11 @@ namespace YooAsset
/// </summary> /// </summary>
public bool Result { private set; get; } public bool Result { private set; get; }
/// <summary>
/// 是否已经完成
/// </summary>
public bool IsDone()
{
return _steps == ESteps.Done;
}
/// <summary> public AppManifestCopyer(string buildinPackageName, string buildinPackageCRC)
/// 初始化流程
/// </summary>
public void Init(int staticVersion)
{ {
_staticVersion = staticVersion; _buildinPackageName = buildinPackageName;
_buildinPackageCRC = buildinPackageCRC;
} }
/// <summary> /// <summary>
@ -396,7 +378,8 @@ namespace YooAsset
if (_steps == ESteps.CopyAppManifest) if (_steps == ESteps.CopyAppManifest)
{ {
string destFilePath = PathHelper.MakePersistentLoadPath(YooAssetSettingsData.GetPatchManifestFileName(_staticVersion)); string fileName = YooAssetSettingsData.GetPatchManifestFileName(_buildinPackageName, _buildinPackageCRC);
string destFilePath = PathHelper.MakePersistentLoadPath(fileName);
if (File.Exists(destFilePath)) if (File.Exists(destFilePath))
{ {
Result = true; Result = true;
@ -406,7 +389,7 @@ namespace YooAsset
else else
{ {
YooLogger.Log($"Copy application patch manifest."); YooLogger.Log($"Copy application patch manifest.");
string sourceFilePath = PathHelper.MakeStreamingLoadPath(YooAssetSettingsData.GetPatchManifestFileName(_staticVersion)); string sourceFilePath = PathHelper.MakeStreamingLoadPath(fileName);
string url = PathHelper.ConvertToWWWPath(sourceFilePath); string url = PathHelper.ConvertToWWWPath(sourceFilePath);
_downloader1 = new UnityWebFileRequester(); _downloader1 = new UnityWebFileRequester();
_downloader1.SendRequest(url, destFilePath); _downloader1.SendRequest(url, destFilePath);
@ -433,5 +416,13 @@ namespace YooAsset
_downloader1.Dispose(); _downloader1.Dispose();
} }
} }
/// <summary>
/// 是否已经完成
/// </summary>
public bool IsDone()
{
return _steps == ESteps.Done;
}
} }
} }

View File

@ -52,8 +52,7 @@ namespace YooAsset
private enum ESteps private enum ESteps
{ {
None, None,
LoadWebManifestHash, CheckManifestHash,
CheckWebManifestHash,
LoadWebManifest, LoadWebManifest,
CheckWebManifest, CheckWebManifest,
InitVerifyingCache, InitVerifyingCache,
@ -63,18 +62,19 @@ namespace YooAsset
private static int RequestCount = 0; private static int RequestCount = 0;
private readonly HostPlayModeImpl _impl; private readonly HostPlayModeImpl _impl;
private readonly int _resourceVersion; private readonly string _packageName;
private readonly string _packageCRC;
private readonly int _timeout; private readonly int _timeout;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
private UnityWebDataRequester _downloader1; private UnityWebDataRequester _downloader;
private UnityWebDataRequester _downloader2;
private PatchCacheVerifier _patchCacheVerifier; private PatchCacheVerifier _patchCacheVerifier;
private float _verifyTime; private float _verifyTime;
internal HostPlayModeUpdateManifestOperation(HostPlayModeImpl impl, int resourceVersion, int timeout) internal HostPlayModeUpdateManifestOperation(HostPlayModeImpl impl, string packageName, string packageCRC, int timeout)
{ {
_impl = impl; _impl = impl;
_resourceVersion = resourceVersion; _packageName = packageName;
_packageCRC = packageCRC;
_timeout = timeout; _timeout = timeout;
#if UNITY_WEBGL #if UNITY_WEBGL
@ -86,82 +86,59 @@ namespace YooAsset
internal override void Start() internal override void Start()
{ {
RequestCount++; RequestCount++;
_steps = ESteps.LoadWebManifestHash; _steps = ESteps.CheckManifestHash;
} }
internal override void Update() internal override void Update()
{ {
if (_steps == ESteps.None || _steps == ESteps.Done) if (_steps == ESteps.None || _steps == ESteps.Done)
return; return;
if (_steps == ESteps.LoadWebManifestHash) if (_steps == ESteps.CheckManifestHash)
{ {
string webURL = GetPatchManifestRequestURL(YooAssetSettingsData.GetPatchManifestHashFileName(_resourceVersion)); string cachedManifestCRC = GetSandboxPatchManifestFileHash(_packageName, _packageCRC);
YooLogger.Log($"Beginning to request patch manifest hash : {webURL}");
_downloader1 = new UnityWebDataRequester();
_downloader1.SendRequest(webURL, _timeout);
_steps = ESteps.CheckWebManifestHash;
}
if (_steps == ESteps.CheckWebManifestHash) // 如果补丁清单文件的哈希值相同
{ if (cachedManifestCRC == _packageCRC)
if (_downloader1.IsDone() == false)
return;
// Check error
if (_downloader1.HasError())
{ {
_steps = ESteps.Done; YooLogger.Log($"Patch manifest file hash is not change : {_packageCRC}");
Status = EOperationStatus.Failed; LoadSandboxPatchManifest(_packageName, _packageCRC);
Error = _downloader1.GetError(); FoundNewManifest = false;
_steps = ESteps.InitVerifyingCache;
} }
else else
{ {
string webManifestHash = _downloader1.GetText(); YooLogger.Log($"Patch manifest hash is change : {cachedManifestCRC} -> {_packageCRC}");
string cachedManifestHash = GetSandboxPatchManifestFileHash(_resourceVersion); FoundNewManifest = true;
_steps = ESteps.LoadWebManifest;
// 如果补丁清单文件的哈希值相同
if (cachedManifestHash == webManifestHash)
{
YooLogger.Log($"Patch manifest file hash is not change : {webManifestHash}");
LoadSandboxPatchManifest(_resourceVersion);
FoundNewManifest = false;
_steps = ESteps.InitVerifyingCache;
}
else
{
YooLogger.Log($"Patch manifest hash is change : {webManifestHash} -> {cachedManifestHash}");
FoundNewManifest = true;
_steps = ESteps.LoadWebManifest;
}
} }
_downloader1.Dispose();
} }
if (_steps == ESteps.LoadWebManifest) if (_steps == ESteps.LoadWebManifest)
{ {
string webURL = GetPatchManifestRequestURL(YooAssetSettingsData.GetPatchManifestFileName(_resourceVersion)); string fileName = YooAssetSettingsData.GetPatchManifestFileName(_packageName, _packageCRC);
string webURL = GetPatchManifestRequestURL(fileName);
YooLogger.Log($"Beginning to request patch manifest : {webURL}"); YooLogger.Log($"Beginning to request patch manifest : {webURL}");
_downloader2 = new UnityWebDataRequester(); _downloader = new UnityWebDataRequester();
_downloader2.SendRequest(webURL, _timeout); _downloader.SendRequest(webURL, _timeout);
_steps = ESteps.CheckWebManifest; _steps = ESteps.CheckWebManifest;
} }
if (_steps == ESteps.CheckWebManifest) if (_steps == ESteps.CheckWebManifest)
{ {
if (_downloader2.IsDone() == false) if (_downloader.IsDone() == false)
return; return;
// Check error // Check error
if (_downloader2.HasError()) if (_downloader.HasError())
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = _downloader2.GetError(); Error = _downloader.GetError();
} }
else else
{ {
// 解析补丁清单 // 解析补丁清单
if (ParseAndSaveRemotePatchManifest(_resourceVersion, _downloader2.GetText())) if (ParseAndSaveRemotePatchManifest(_packageName, _packageCRC, _downloader.GetText()))
{ {
_steps = ESteps.InitVerifyingCache; _steps = ESteps.InitVerifyingCache;
} }
@ -169,10 +146,10 @@ namespace YooAsset
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"URL : {_downloader2.URL} Error : remote patch manifest content is invalid"; Error = $"URL : {_downloader.URL} Error : remote patch manifest content is invalid";
} }
} }
_downloader2.Dispose(); _downloader.Dispose();
} }
if (_steps == ESteps.InitVerifyingCache) if (_steps == ESteps.InitVerifyingCache)
@ -210,7 +187,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 解析并保存远端请求的补丁清单 /// 解析并保存远端请求的补丁清单
/// </summary> /// </summary>
private bool ParseAndSaveRemotePatchManifest(int updateResourceVersion, string content) private bool ParseAndSaveRemotePatchManifest(string packageName, string packageCRC, string content)
{ {
try try
{ {
@ -218,7 +195,8 @@ namespace YooAsset
_impl.SetLocalPatchManifest(remotePatchManifest); _impl.SetLocalPatchManifest(remotePatchManifest);
YooLogger.Log("Save remote patch manifest file."); YooLogger.Log("Save remote patch manifest file.");
string savePath = PathHelper.MakePersistentLoadPath(YooAssetSettingsData.GetPatchManifestFileName(updateResourceVersion)); string fileName = YooAssetSettingsData.GetPatchManifestFileName(packageName, packageCRC);
string savePath = PathHelper.MakePersistentLoadPath(fileName);
PatchManifest.Serialize(savePath, remotePatchManifest); PatchManifest.Serialize(savePath, remotePatchManifest);
return true; return true;
} }
@ -233,10 +211,11 @@ namespace YooAsset
/// 加载沙盒内的补丁清单 /// 加载沙盒内的补丁清单
/// 注意:在加载本地补丁清单之前,已经验证过文件的哈希值 /// 注意:在加载本地补丁清单之前,已经验证过文件的哈希值
/// </summary> /// </summary>
private void LoadSandboxPatchManifest(int updateResourceVersion) private void LoadSandboxPatchManifest(string packageName, string packageCRC)
{ {
YooLogger.Log("Load sandbox patch manifest file."); YooLogger.Log("Load sandbox patch manifest file.");
string filePath = PathHelper.MakePersistentLoadPath(YooAssetSettingsData.GetPatchManifestFileName(updateResourceVersion)); string fileName = YooAssetSettingsData.GetPatchManifestFileName(packageName, packageCRC);
string filePath = PathHelper.MakePersistentLoadPath(fileName);
string jsonData = File.ReadAllText(filePath); string jsonData = File.ReadAllText(filePath);
var sandboxPatchManifest = PatchManifest.Deserialize(jsonData); var sandboxPatchManifest = PatchManifest.Deserialize(jsonData);
_impl.SetLocalPatchManifest(sandboxPatchManifest); _impl.SetLocalPatchManifest(sandboxPatchManifest);
@ -246,11 +225,12 @@ namespace YooAsset
/// 获取沙盒内补丁清单文件的哈希值 /// 获取沙盒内补丁清单文件的哈希值
/// 注意:如果沙盒内补丁清单文件不存在,返回空字符串 /// 注意:如果沙盒内补丁清单文件不存在,返回空字符串
/// </summary> /// </summary>
private string GetSandboxPatchManifestFileHash(int updateResourceVersion) private string GetSandboxPatchManifestFileHash(string packageName, string packageCRC)
{ {
string filePath = PathHelper.MakePersistentLoadPath(YooAssetSettingsData.GetPatchManifestFileName(updateResourceVersion)); string fileName = YooAssetSettingsData.GetPatchManifestFileName(packageName, packageCRC);
string filePath = PathHelper.MakePersistentLoadPath(fileName);
if (File.Exists(filePath)) if (File.Exists(filePath))
return HashUtility.FileMD5(filePath); return HashUtility.FileCRC32(filePath);
else else
return string.Empty; return string.Empty;
} }
@ -271,15 +251,17 @@ namespace YooAsset
} }
private readonly HostPlayModeImpl _impl; private readonly HostPlayModeImpl _impl;
private readonly int _resourceVersion; private readonly string _packageName;
private readonly string _packageCRC;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
private PatchCacheVerifier _patchCacheVerifier; private PatchCacheVerifier _patchCacheVerifier;
private float _verifyTime; private float _verifyTime;
internal HostPlayModeWeaklyUpdateManifestOperation(HostPlayModeImpl impl, int resourceVersion) internal HostPlayModeWeaklyUpdateManifestOperation(HostPlayModeImpl impl, string packageName, string packageCRC)
{ {
_impl = impl; _impl = impl;
_resourceVersion = resourceVersion; _packageName = packageName;
_packageCRC = packageCRC;
#if UNITY_WEBGL #if UNITY_WEBGL
_patchCacheVerifier = new PatchCacheVerifierWithoutThread(); _patchCacheVerifier = new PatchCacheVerifierWithoutThread();
@ -298,7 +280,7 @@ namespace YooAsset
if (_steps == ESteps.LoadSandboxManifestHash) if (_steps == ESteps.LoadSandboxManifestHash)
{ {
LoadSandboxPatchManifest(_resourceVersion); LoadSandboxPatchManifest(_packageName, _packageCRC);
_steps = ESteps.InitVerifyingCache; _steps = ESteps.InitVerifyingCache;
} }
@ -313,7 +295,7 @@ namespace YooAsset
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"The resource version {_resourceVersion} content is not complete !"; Error = $"The package resource {_packageName}_{_packageCRC} content is not complete !";
} }
} }
@ -328,7 +310,7 @@ namespace YooAsset
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"The resource version {_resourceVersion} content has verify failed file !"; Error = $"The package resource {_packageName}_{_packageCRC} content has verify failed file !";
} }
else else
{ {
@ -343,9 +325,10 @@ namespace YooAsset
/// 加载沙盒内的补丁清单 /// 加载沙盒内的补丁清单
/// 注意:在加载本地补丁清单之前,未验证过文件的哈希值 /// 注意:在加载本地补丁清单之前,未验证过文件的哈希值
/// </summary> /// </summary>
private void LoadSandboxPatchManifest(int updateResourceVersion) private void LoadSandboxPatchManifest(string packageName, string packageCRC)
{ {
string filePath = PathHelper.MakePersistentLoadPath(YooAssetSettingsData.GetPatchManifestFileName(updateResourceVersion)); string fileName = YooAssetSettingsData.GetPatchManifestFileName(packageName, packageCRC);
string filePath = PathHelper.MakePersistentLoadPath(fileName);
if (File.Exists(filePath)) if (File.Exists(filePath))
{ {
YooLogger.Log("Load sandbox patch manifest file."); YooLogger.Log("Load sandbox patch manifest file.");

View File

@ -78,21 +78,23 @@ namespace YooAsset
private static int RequestCount = 0; private static int RequestCount = 0;
private readonly HostPlayModeImpl _impl; private readonly HostPlayModeImpl _impl;
private readonly int _resourceVersion; private readonly string _packageName;
private readonly string _packageCRC;
private readonly int _timeout; private readonly int _timeout;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
private UnityWebDataRequester _downloader; private UnityWebDataRequester _downloader;
private PatchManifest _remotePatchManifest; private PatchManifest _remotePatchManifest;
internal HostPlayModeUpdatePackageOperation(HostPlayModeImpl impl, int resourceVersion, int timeout) internal HostPlayModeUpdatePackageOperation(HostPlayModeImpl impl, string packageName, string packageCRC, int timeout)
{ {
_impl = impl; _impl = impl;
_resourceVersion = resourceVersion; _packageName = packageName;
_packageCRC = packageCRC;
_timeout = timeout; _timeout = timeout;
} }
internal override void Start() internal override void Start()
{ {
RequestCount++; RequestCount++;
_steps = ESteps.LoadWebManifest; _steps = ESteps.LoadWebManifest;
} }
internal override void Update() internal override void Update()
@ -102,7 +104,8 @@ namespace YooAsset
if (_steps == ESteps.LoadWebManifest) if (_steps == ESteps.LoadWebManifest)
{ {
string webURL = GetPatchManifestRequestURL(YooAssetSettingsData.GetPatchManifestFileName(_resourceVersion)); string fileName = YooAssetSettingsData.GetPatchManifestFileName(_packageName, _packageCRC);
string webURL = GetPatchManifestRequestURL(fileName);
YooLogger.Log($"Beginning to request patch manifest : {webURL}"); YooLogger.Log($"Beginning to request patch manifest : {webURL}");
_downloader = new UnityWebDataRequester(); _downloader = new UnityWebDataRequester();
_downloader.SendRequest(webURL, _timeout); _downloader.SendRequest(webURL, _timeout);

View File

@ -9,9 +9,9 @@ namespace YooAsset
public abstract class UpdateStaticVersionOperation : AsyncOperationBase public abstract class UpdateStaticVersionOperation : AsyncOperationBase
{ {
/// <summary> /// <summary>
/// 资源版本号 /// 包裹文件的哈希值
/// </summary> /// </summary>
public int ResourceVersion { protected set; get; } = 0; public string PackageCRC { protected set; get; } = string.Empty;
} }
/// <summary> /// <summary>
@ -57,13 +57,15 @@ namespace YooAsset
private static int RequestCount = 0; private static int RequestCount = 0;
private readonly HostPlayModeImpl _impl; private readonly HostPlayModeImpl _impl;
private readonly string _packageName;
private readonly int _timeout; private readonly int _timeout;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
private UnityWebDataRequester _downloader; private UnityWebDataRequester _downloader;
internal HostPlayModeUpdateStaticVersionOperation(HostPlayModeImpl impl, int timeout) internal HostPlayModeUpdateStaticVersionOperation(HostPlayModeImpl impl, string packageName, int timeout)
{ {
_impl = impl; _impl = impl;
_packageName = packageName;
_timeout = timeout; _timeout = timeout;
} }
internal override void Start() internal override void Start()
@ -78,7 +80,8 @@ namespace YooAsset
if (_steps == ESteps.LoadStaticVersion) if (_steps == ESteps.LoadStaticVersion)
{ {
string webURL = GetStaticVersionRequestURL(YooAssetSettings.VersionFileName); string versionFileName = YooAssetSettingsData.GetStaticVersionFileName(_packageName);
string webURL = GetStaticVersionRequestURL(versionFileName);
YooLogger.Log($"Beginning to request static version : {webURL}"); YooLogger.Log($"Beginning to request static version : {webURL}");
_downloader = new UnityWebDataRequester(); _downloader = new UnityWebDataRequester();
_downloader.SendRequest(webURL, _timeout); _downloader.SendRequest(webURL, _timeout);
@ -99,17 +102,18 @@ namespace YooAsset
} }
else else
{ {
if (int.TryParse(_downloader.GetText(), out int value)) string packageCRC = _downloader.GetText();
{ if(string.IsNullOrEmpty(packageCRC))
ResourceVersion = value;
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"URL : {_downloader.URL} Error : static version content is invalid."; Error = $"URL : {_downloader.URL} Error : static version content is empty.";
}
else
{
PackageCRC = packageCRC;
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
} }
} }
_downloader.Dispose(); _downloader.Dispose();

View File

@ -17,11 +17,6 @@ namespace YooAsset
/// </summary> /// </summary>
public string FileVersion; public string FileVersion;
/// <summary>
/// 资源版本号
/// </summary>
public int ResourceVersion;
/// <summary> /// <summary>
/// 启用可寻址资源定位 /// 启用可寻址资源定位
/// </summary> /// </summary>
@ -32,6 +27,11 @@ namespace YooAsset
/// </summary> /// </summary>
public int OutputNameStyle; public int OutputNameStyle;
/// <summary>
/// 资源包裹名称
/// </summary>
public string PackageName;
/// <summary> /// <summary>
/// 内置资源的标签列表(首包资源) /// 内置资源的标签列表(首包资源)
/// </summary> /// </summary>

View File

@ -20,16 +20,6 @@ namespace YooAsset
return operation; return operation;
} }
/// <summary>
/// 获取资源版本号
/// </summary>
public int GetResourceVersion()
{
if (_simulatePatchManifest == null)
return 0;
return _simulatePatchManifest.ResourceVersion;
}
// 设置资源清单 // 设置资源清单
internal void SetSimulatePatchManifest(PatchManifest patchManifest) internal void SetSimulatePatchManifest(PatchManifest patchManifest)
{ {

View File

@ -13,26 +13,19 @@ namespace YooAsset
// 参数相关 // 参数相关
private bool _locationToLower; private bool _locationToLower;
private bool _clearCacheWhenDirty;
private string _defaultHostServer; private string _defaultHostServer;
private string _fallbackHostServer; private string _fallbackHostServer;
public bool ClearCacheWhenDirty
{
get { return _clearCacheWhenDirty; }
}
/// <summary> /// <summary>
/// 异步初始化 /// 异步初始化
/// </summary> /// </summary>
public InitializationOperation InitializeAsync(bool locationToLower, bool clearCacheWhenDirty, string defaultHostServer, string fallbackHostServer) public InitializationOperation InitializeAsync(bool locationToLower, string buildinPackageName, string defaultHostServer, string fallbackHostServer)
{ {
_locationToLower = locationToLower; _locationToLower = locationToLower;
_clearCacheWhenDirty = clearCacheWhenDirty;
_defaultHostServer = defaultHostServer; _defaultHostServer = defaultHostServer;
_fallbackHostServer = fallbackHostServer; _fallbackHostServer = fallbackHostServer;
var operation = new HostPlayModeInitializationOperation(this); var operation = new HostPlayModeInitializationOperation(this, buildinPackageName);
OperationSystem.StartOperation(operation); OperationSystem.StartOperation(operation);
return operation; return operation;
} }
@ -40,9 +33,9 @@ namespace YooAsset
/// <summary> /// <summary>
/// 异步更新资源版本号 /// 异步更新资源版本号
/// </summary> /// </summary>
public UpdateStaticVersionOperation UpdateStaticVersionAsync(int timeout) public UpdateStaticVersionOperation UpdateStaticVersionAsync(string packageName, int timeout)
{ {
var operation = new HostPlayModeUpdateStaticVersionOperation(this, timeout); var operation = new HostPlayModeUpdateStaticVersionOperation(this, packageName, timeout);
OperationSystem.StartOperation(operation); OperationSystem.StartOperation(operation);
return operation; return operation;
} }
@ -50,9 +43,9 @@ namespace YooAsset
/// <summary> /// <summary>
/// 异步更新补丁清单 /// 异步更新补丁清单
/// </summary> /// </summary>
public UpdateManifestOperation UpdatePatchManifestAsync(int resourceVersion, int timeout) public UpdateManifestOperation UpdatePatchManifestAsync(string packageName, string packageCRC, int timeout)
{ {
var operation = new HostPlayModeUpdateManifestOperation(this, resourceVersion, timeout); var operation = new HostPlayModeUpdateManifestOperation(this, packageName, packageCRC, timeout);
OperationSystem.StartOperation(operation); OperationSystem.StartOperation(operation);
return operation; return operation;
} }
@ -60,9 +53,9 @@ namespace YooAsset
/// <summary> /// <summary>
/// 异步更新补丁清单(弱联网) /// 异步更新补丁清单(弱联网)
/// </summary> /// </summary>
public UpdateManifestOperation WeaklyUpdatePatchManifestAsync(int resourceVersion) public UpdateManifestOperation WeaklyUpdatePatchManifestAsync(string packageName, string packageCRC)
{ {
var operation = new HostPlayModeWeaklyUpdateManifestOperation(this, resourceVersion); var operation = new HostPlayModeWeaklyUpdateManifestOperation(this, packageName, packageCRC);
OperationSystem.StartOperation(operation); OperationSystem.StartOperation(operation);
return operation; return operation;
} }
@ -70,23 +63,13 @@ namespace YooAsset
/// <summary> /// <summary>
/// 异步更新资源包裹 /// 异步更新资源包裹
/// </summary> /// </summary>
public UpdatePackageOperation UpdatePackageAsync(int resourceVersion, int timeout) public UpdatePackageOperation UpdatePackageAsync(string packageName, string packageCRC, int timeout)
{ {
var operation = new HostPlayModeUpdatePackageOperation(this, resourceVersion, timeout); var operation = new HostPlayModeUpdatePackageOperation(this, packageName, packageCRC, timeout);
OperationSystem.StartOperation(operation); OperationSystem.StartOperation(operation);
return operation; return operation;
} }
/// <summary>
/// 获取资源版本号
/// </summary>
public int GetResourceVersion()
{
if (LocalPatchManifest == null)
return 0;
return LocalPatchManifest.ResourceVersion;
}
/// <summary> /// <summary>
/// 获取未被使用的缓存文件路径集合 /// 获取未被使用的缓存文件路径集合
/// </summary> /// </summary>

View File

@ -12,24 +12,14 @@ namespace YooAsset
/// <summary> /// <summary>
/// 异步初始化 /// 异步初始化
/// </summary> /// </summary>
public InitializationOperation InitializeAsync(bool locationToLower) public InitializationOperation InitializeAsync(bool locationToLower, string buildinPackageName)
{ {
_locationToLower = locationToLower; _locationToLower = locationToLower;
var operation = new OfflinePlayModeInitializationOperation(this); var operation = new OfflinePlayModeInitializationOperation(this, buildinPackageName);
OperationSystem.StartOperation(operation); OperationSystem.StartOperation(operation);
return operation; return operation;
} }
/// <summary>
/// 获取资源版本号
/// </summary>
public int GetResourceVersion()
{
if (_appPatchManifest == null)
return 0;
return _appPatchManifest.ResourceVersion;
}
// 设置资源清单 // 设置资源清单
internal void SetAppPatchManifest(PatchManifest patchManifest) internal void SetAppPatchManifest(PatchManifest patchManifest)
{ {

View File

@ -24,7 +24,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 补丁清单文件版本 /// 补丁清单文件版本
/// </summary> /// </summary>
public const string PatchManifestFileVersion = "1.2.2"; public const string PatchManifestFileVersion = "1.3.0";
/// <summary> /// <summary>
/// 构建输出文件夹名称 /// 构建输出文件夹名称
@ -39,7 +39,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 静态版本文件 /// 静态版本文件
/// </summary> /// </summary>
public const string VersionFileName = "StaticVersion.bytes"; public const string VersionFileName = "StaticVersion";
/// <summary> /// <summary>
/// Unity着色器资源包名称 /// Unity着色器资源包名称

View File

@ -35,25 +35,33 @@ namespace YooAsset
/// <summary> /// <summary>
/// 获取构建报告文件名 /// 获取构建报告文件名
/// </summary> /// </summary>
public static string GetReportFileName(int resourceVersion) public static string GetReportFileName(string packageName, string packageCRC)
{ {
return $"{YooAssetSettings.ReportFileName}_{resourceVersion}.json"; return $"{YooAssetSettings.ReportFileName}_{packageName}_{packageCRC}.json";
} }
/// <summary> /// <summary>
/// 获取补丁清单文件完整名称 /// 获取补丁清单文件完整名称
/// </summary> /// </summary>
public static string GetPatchManifestFileName(int resourceVersion) public static string GetPatchManifestFileName(string packageName, string packageCRC)
{ {
return $"{Setting.PatchManifestFileName}_{resourceVersion}.bytes"; return $"{Setting.PatchManifestFileName}_{packageName}_{packageCRC}.bytes";
} }
/// <summary> /// <summary>
/// 获取补丁清单哈希文件完整名称 /// 获取补丁清单文件临时名称
/// </summary> /// </summary>
public static string GetPatchManifestHashFileName(int resourceVersion) public static string GetPatchManifestTempFileName(string packageName)
{ {
return $"{Setting.PatchManifestFileName}_{resourceVersion}.hash"; return $"{Setting.PatchManifestFileName}_{packageName}.temp";
}
/// <summary>
/// 获取静态版本文件名称
/// </summary>
public static string GetStaticVersionFileName(string packageName)
{
return $"{YooAssetSettings.VersionFileName}_{packageName}.bytes";
} }
/// <summary> /// <summary>

View File

@ -40,6 +40,11 @@ namespace YooAsset
/// </summary> /// </summary>
public bool LocationToLower = false; public bool LocationToLower = false;
/// <summary>
/// 内置的资源包裹名称
/// </summary>
public string BuildinPackageName = string.Empty;
/// <summary> /// <summary>
/// 资源定位服务接口 /// 资源定位服务接口
/// </summary> /// </summary>
@ -146,16 +151,19 @@ namespace YooAsset
if (parameters == null) if (parameters == null)
throw new Exception($"YooAsset create parameters is null."); throw new Exception($"YooAsset create parameters is null.");
if (string.IsNullOrEmpty(parameters.BuildinPackageName))
throw new Exception($"{nameof(parameters.BuildinPackageName)} is empty.");
if (parameters.LocationServices == null) if (parameters.LocationServices == null)
throw new Exception($"{nameof(IBundleServices)} is null."); throw new Exception($"{nameof(IBundleServices)} is null.");
else
_locationServices = parameters.LocationServices;
#if !UNITY_EDITOR #if !UNITY_EDITOR
if (parameters is EditorSimulateModeParameters) if (parameters is EditorSimulateModeParameters)
throw new Exception($"Editor simulate mode only support unity editor."); throw new Exception($"Editor simulate mode only support unity editor.");
#endif #endif
_locationServices = parameters.LocationServices;
// 创建驱动器 // 创建驱动器
if (_isInitialize == false) if (_isInitialize == false)
{ {
@ -228,7 +236,7 @@ namespace YooAsset
_offlinePlayModeImpl = new OfflinePlayModeImpl(); _offlinePlayModeImpl = new OfflinePlayModeImpl();
_bundleServices = _offlinePlayModeImpl; _bundleServices = _offlinePlayModeImpl;
AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
initializeOperation = _offlinePlayModeImpl.InitializeAsync(parameters.LocationToLower); initializeOperation = _offlinePlayModeImpl.InitializeAsync(parameters.LocationToLower, parameters.BuildinPackageName);
} }
else if (_playMode == EPlayMode.HostPlayMode) else if (_playMode == EPlayMode.HostPlayMode)
{ {
@ -238,7 +246,7 @@ namespace YooAsset
var hostPlayModeParameters = parameters as HostPlayModeParameters; var hostPlayModeParameters = parameters as HostPlayModeParameters;
initializeOperation = _hostPlayModeImpl.InitializeAsync( initializeOperation = _hostPlayModeImpl.InitializeAsync(
hostPlayModeParameters.LocationToLower, hostPlayModeParameters.LocationToLower,
hostPlayModeParameters.ClearCacheWhenDirty, hostPlayModeParameters.BuildinPackageName,
hostPlayModeParameters.DefaultHostServer, hostPlayModeParameters.DefaultHostServer,
hostPlayModeParameters.FallbackHostServer); hostPlayModeParameters.FallbackHostServer);
} }
@ -260,8 +268,9 @@ namespace YooAsset
/// <summary> /// <summary>
/// 向网络端请求静态资源版本 /// 向网络端请求静态资源版本
/// </summary> /// </summary>
/// <param name="packageName">更新的资源包裹名称</param>
/// <param name="timeout">超时时间默认值60秒</param> /// <param name="timeout">超时时间默认值60秒</param>
public static UpdateStaticVersionOperation UpdateStaticVersionAsync(int timeout = 60) public static UpdateStaticVersionOperation UpdateStaticVersionAsync(string packageName, int timeout = 60)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode) if (_playMode == EPlayMode.EditorSimulateMode)
@ -278,7 +287,7 @@ namespace YooAsset
} }
else if (_playMode == EPlayMode.HostPlayMode) else if (_playMode == EPlayMode.HostPlayMode)
{ {
return _hostPlayModeImpl.UpdateStaticVersionAsync(timeout); return _hostPlayModeImpl.UpdateStaticVersionAsync(packageName, timeout);
} }
else else
{ {
@ -289,9 +298,10 @@ namespace YooAsset
/// <summary> /// <summary>
/// 向网络端请求并更新补丁清单 /// 向网络端请求并更新补丁清单
/// </summary> /// </summary>
/// <param name="resourceVersion">更新的资源版本</param> /// <param name="packageName">更新的资源包裹名称</param>
/// <param name="packageCRC">更新的资源包裹版本</param>
/// <param name="timeout">超时时间默认值60秒</param> /// <param name="timeout">超时时间默认值60秒</param>
public static UpdateManifestOperation UpdateManifestAsync(int resourceVersion, int timeout = 60) public static UpdateManifestOperation UpdateManifestAsync(string packageName, string packageCRC, int timeout = 60)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
DebugCheckUpdateManifest(); DebugCheckUpdateManifest();
@ -309,7 +319,7 @@ namespace YooAsset
} }
else if (_playMode == EPlayMode.HostPlayMode) else if (_playMode == EPlayMode.HostPlayMode)
{ {
return _hostPlayModeImpl.UpdatePatchManifestAsync(resourceVersion, timeout); return _hostPlayModeImpl.UpdatePatchManifestAsync(packageName, packageCRC, timeout);
} }
else else
{ {
@ -321,8 +331,9 @@ namespace YooAsset
/// 弱联网情况下加载补丁清单 /// 弱联网情况下加载补丁清单
/// 注意:当指定版本内容验证失败后会返回失败。 /// 注意:当指定版本内容验证失败后会返回失败。
/// </summary> /// </summary>
/// <param name="resourceVersion">指定的资源版本</param> /// <param name="packageName">指定的资源包裹名称</param>
public static UpdateManifestOperation WeaklyUpdateManifestAsync(int resourceVersion) /// <param name="packageCRC">指定的资源包裹版本</param>
public static UpdateManifestOperation WeaklyUpdateManifestAsync(string packageName, string packageCRC)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode) if (_playMode == EPlayMode.EditorSimulateMode)
@ -339,7 +350,7 @@ namespace YooAsset
} }
else if (_playMode == EPlayMode.HostPlayMode) else if (_playMode == EPlayMode.HostPlayMode)
{ {
return _hostPlayModeImpl.WeaklyUpdatePatchManifestAsync(resourceVersion); return _hostPlayModeImpl.WeaklyUpdatePatchManifestAsync(packageName, packageCRC);
} }
else else
{ {
@ -356,30 +367,6 @@ namespace YooAsset
OperationSystem.StartOperation(operation); OperationSystem.StartOperation(operation);
} }
/// <summary>
/// 获取资源版本号
/// </summary>
public static int GetResourceVersion()
{
DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode)
{
return _editorSimulateModeImpl.GetResourceVersion();
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
return _offlinePlayModeImpl.GetResourceVersion();
}
else if (_playMode == EPlayMode.HostPlayMode)
{
return _hostPlayModeImpl.GetResourceVersion();
}
else
{
throw new NotImplementedException();
}
}
/// <summary> /// <summary>
/// 资源回收(卸载引用计数为零的资源) /// 资源回收(卸载引用计数为零的资源)
/// </summary> /// </summary>
@ -985,9 +972,10 @@ namespace YooAsset
/// <summary> /// <summary>
/// 创建资源包裹下载器,用于下载更新指定资源版本所有的资源包文件 /// 创建资源包裹下载器,用于下载更新指定资源版本所有的资源包文件
/// </summary> /// </summary>
/// <param name="resourceVersion">指定更新的资源版本</param> /// <param name="packageName">指定更新的资源包裹名称</param>
/// <param name="packageCRC">指定更新的资源包裹版本</param>
/// <param name="timeout">超时时间</param> /// <param name="timeout">超时时间</param>
public static UpdatePackageOperation UpdatePackageAsync(int resourceVersion, int timeout = 60) public static UpdatePackageOperation UpdatePackageAsync(string packageName, string packageCRC, int timeout = 60)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode) if (_playMode == EPlayMode.EditorSimulateMode)
@ -1004,7 +992,7 @@ namespace YooAsset
} }
else if (_playMode == EPlayMode.HostPlayMode) else if (_playMode == EPlayMode.HostPlayMode)
{ {
return _hostPlayModeImpl.UpdatePackageAsync(resourceVersion, timeout); return _hostPlayModeImpl.UpdatePackageAsync(packageName, packageCRC, timeout);
} }
else else
{ {