Initial commit

This commit is contained in:
hevinci
2022-03-01 10:44:12 +08:00
parent 1d54164bfb
commit e66853fd46
237 changed files with 14462 additions and 2 deletions

View File

@@ -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;
}
}
}
}