Update InitializationOperation.cs

pull/18/head
hevinci 2022-06-29 11:59:39 +08:00
parent 161fdff09a
commit 5b0c189d75
1 changed files with 134 additions and 18 deletions

View File

@ -137,12 +137,14 @@ namespace YooAsset
{
None,
InitCache,
Update,
LoadManifest,
CopyManifest,
Done,
}
private readonly HostPlayModeImpl _impl;
private readonly AppManifestLoader _appManifestLoader = new AppManifestLoader();
private readonly AppManifestCopyer _appManifestCopyer = new AppManifestCopyer();
private ESteps _steps = ESteps.None;
internal HostPlayModeInitializationOperation(HostPlayModeImpl impl)
@ -176,10 +178,10 @@ namespace YooAsset
// 更新缓存文件
CacheData.UpdateCache();
}
_steps = ESteps.Update;
_steps = ESteps.LoadManifest;
}
if (_steps == ESteps.Update)
if (_steps == ESteps.LoadManifest)
{
_appManifestLoader.Update();
Progress = _appManifestLoader.Progress();
@ -194,10 +196,29 @@ namespace YooAsset
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
_impl.SetAppPatchManifest(_appManifestLoader.Result);
_impl.SetLocalPatchManifest(_appManifestLoader.Result);
_appManifestCopyer.Init(_appManifestLoader.StaticVersion);
_steps = ESteps.CopyManifest;
}
}
if (_steps == ESteps.CopyManifest)
{
_appManifestCopyer.Update();
if (_appManifestCopyer.IsDone() == false)
return;
if (_appManifestCopyer.Result == false)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _appManifestCopyer.Error;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
@ -215,14 +236,12 @@ namespace YooAsset
CheckStaticVersion,
LoadAppManifest,
CheckAppManifest,
Succeed,
Failed,
Done,
}
private ESteps _steps = ESteps.LoadStaticVersion;
private UnityWebDataRequester _downloader1;
private UnityWebDataRequester _downloader2;
private int _staticVersion = 0;
/// <summary>
/// 错误日志
@ -230,19 +249,21 @@ namespace YooAsset
public string Error { private set; get; }
/// <summary>
/// 补丁清单
/// 加载结果
/// </summary>
public PatchManifest Result { private set; get; }
/// <summary>
/// 内置补丁清单版本号
/// </summary>
public int StaticVersion { private set; get; }
/// <summary>
/// 是否已经完成
/// </summary>
public bool IsDone()
{
if (_steps == ESteps.Succeed || _steps == ESteps.Failed)
return true;
else
return false;
return _steps == ESteps.Done;
}
/// <summary>
@ -255,6 +276,9 @@ namespace YooAsset
return _downloader2.Progress();
}
/// <summary>
/// 更新流程
/// </summary>
public void Update()
{
if (IsDone())
@ -278,11 +302,11 @@ namespace YooAsset
if (_downloader1.HasError())
{
Error = _downloader1.GetError();
_steps = ESteps.Failed;
_steps = ESteps.Done;
}
else
{
_staticVersion = int.Parse(_downloader1.GetText());
StaticVersion = int.Parse(_downloader1.GetText());
_steps = ESteps.LoadAppManifest;
}
_downloader1.Dispose();
@ -291,7 +315,7 @@ namespace YooAsset
if (_steps == ESteps.LoadAppManifest)
{
YooLogger.Log($"Load application patch manifest.");
string filePath = PathHelper.MakeStreamingLoadPath(YooAssetSettingsData.GetPatchManifestFileName(_staticVersion));
string filePath = PathHelper.MakeStreamingLoadPath(YooAssetSettingsData.GetPatchManifestFileName(StaticVersion));
string url = PathHelper.ConvertToWWWPath(filePath);
_downloader2 = new UnityWebDataRequester();
_downloader2.SendRequest(url);
@ -306,16 +330,108 @@ namespace YooAsset
if (_downloader2.HasError())
{
Error = _downloader2.GetError();
_steps = ESteps.Failed;
_steps = ESteps.Done;
}
else
{
// 解析APP里的补丁清单
Result = PatchManifest.Deserialize(_downloader2.GetText());
_steps = ESteps.Succeed;
_steps = ESteps.Done;
}
_downloader2.Dispose();
}
}
}
/// <summary>
/// 内置补丁清单复制器
/// </summary>
internal class AppManifestCopyer
{
private enum ESteps
{
CopyAppManifest,
CheckAppManifest,
Done,
}
private ESteps _steps = ESteps.CopyAppManifest;
private UnityWebFileRequester _downloader1;
private int _staticVersion;
/// <summary>
/// 错误日志
/// </summary>
public string Error { private set; get; }
/// <summary>
/// 拷贝结果
/// </summary>
public bool Result { private set; get; }
/// <summary>
/// 是否已经完成
/// </summary>
public bool IsDone()
{
return _steps == ESteps.Done;
}
/// <summary>
/// 初始化流程
/// </summary>
public void Init(int staticVersion)
{
_staticVersion = staticVersion;
}
/// <summary>
/// 更新流程
/// </summary>
public void Update()
{
if (IsDone())
return;
if (_steps == ESteps.CopyAppManifest)
{
string destFilePath = PathHelper.MakePersistentLoadPath(YooAssetSettingsData.GetPatchManifestFileName(_staticVersion));
if (File.Exists(destFilePath))
{
Result = true;
_steps = ESteps.Done;
return;
}
else
{
YooLogger.Log($"Copy application patch manifest.");
string sourceFilePath = PathHelper.MakeStreamingLoadPath(YooAssetSettingsData.GetPatchManifestFileName(_staticVersion));
string url = PathHelper.ConvertToWWWPath(sourceFilePath);
_downloader1 = new UnityWebFileRequester();
_downloader1.SendRequest(url, destFilePath);
_steps = ESteps.CheckAppManifest;
}
}
if (_steps == ESteps.CheckAppManifest)
{
if (_downloader1.IsDone() == false)
return;
if (_downloader1.HasError())
{
Result = false;
Error = _downloader1.GetError();
_steps = ESteps.Done;
}
else
{
Result = true;
_steps = ESteps.Done;
}
_downloader1.Dispose();
}
}
}
}