123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- namespace YooAsset
- {
-
-
-
- public abstract class InitializationOperation : AsyncOperationBase
- {
- }
-
-
-
- internal sealed class EditorSimulateModeInitializationOperation : InitializationOperation
- {
- private enum ESteps
- {
- None,
- Builder,
- Load,
- Done,
- }
- private readonly EditorSimulateModeImpl _impl;
- private string _simulatePatchManifestPath;
- private ESteps _steps = ESteps.None;
- internal EditorSimulateModeInitializationOperation(EditorSimulateModeImpl impl, string simulatePatchManifestPath)
- {
- _impl = impl;
- _simulatePatchManifestPath = simulatePatchManifestPath;
- }
- internal override void Start()
- {
- if (string.IsNullOrEmpty(_simulatePatchManifestPath))
- _steps = ESteps.Builder;
- else
- _steps = ESteps.Load;
- }
- internal override void Update()
- {
- if (_steps == ESteps.Builder)
- {
- _simulatePatchManifestPath = EditorSimulateModeHelper.SimulateBuild();
- if (string.IsNullOrEmpty(_simulatePatchManifestPath))
- {
- _steps = ESteps.Done;
- Status = EOperationStatus.Failed;
- Error = "Simulate build failed, see the detail info on the console window.";
- return;
- }
- _steps = ESteps.Load;
- }
- if (_steps == ESteps.Load)
- {
- if (File.Exists(_simulatePatchManifestPath) == false)
- {
- _steps = ESteps.Done;
- Status = EOperationStatus.Failed;
- Error = $"Manifest file not found : {_simulatePatchManifestPath}";
- return;
- }
- YooLogger.Log($"Load manifest file : {_simulatePatchManifestPath}");
- string jsonContent = FileUtility.ReadFile(_simulatePatchManifestPath);
- var simulatePatchManifest = PatchManifest.Deserialize(jsonContent);
- _impl.SetSimulatePatchManifest(simulatePatchManifest);
- _steps = ESteps.Done;
- Status = EOperationStatus.Succeed;
- }
- }
- }
-
-
-
- internal sealed class OfflinePlayModeInitializationOperation : InitializationOperation
- {
- private enum ESteps
- {
- None,
- Update,
- Done,
- }
- private readonly OfflinePlayModeImpl _impl;
- private readonly AppManifestLoader _appManifestLoader = new AppManifestLoader();
- private ESteps _steps = ESteps.None;
- internal OfflinePlayModeInitializationOperation(OfflinePlayModeImpl impl)
- {
- _impl = impl;
- }
- internal override void Start()
- {
- _steps = ESteps.Update;
- }
- internal override void Update()
- {
- if (_steps == ESteps.None || _steps == ESteps.Done)
- return;
- if (_steps == ESteps.Update)
- {
- _appManifestLoader.Update();
- Progress = _appManifestLoader.Progress();
- if (_appManifestLoader.IsDone() == false)
- return;
- if (_appManifestLoader.Result == null)
- {
- _steps = ESteps.Done;
- Status = EOperationStatus.Failed;
- Error = _appManifestLoader.Error;
- }
- else
- {
- _steps = ESteps.Done;
- Status = EOperationStatus.Succeed;
- _impl.SetAppPatchManifest(_appManifestLoader.Result);
- }
- }
- }
- }
-
-
-
- internal sealed class HostPlayModeInitializationOperation : InitializationOperation
- {
- private enum ESteps
- {
- None,
- InitCache,
- 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)
- {
- _impl = impl;
- }
- internal override void Start()
- {
- _steps = ESteps.InitCache;
- }
- internal override void Update()
- {
- if (_steps == ESteps.None || _steps == ESteps.Done)
- return;
- if (_steps == ESteps.InitCache)
- {
-
- 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}");
-
- if (_impl.ClearCacheWhenDirty)
- {
- YooLogger.Warning("Clear cache files.");
- SandboxHelper.DeleteCacheFolder();
- }
-
- CacheData.UpdateCache();
- }
- _steps = ESteps.LoadManifest;
- }
- if (_steps == ESteps.LoadManifest)
- {
- _appManifestLoader.Update();
- Progress = _appManifestLoader.Progress();
- if (_appManifestLoader.IsDone() == false)
- return;
- if (_appManifestLoader.Result == null)
- {
- _steps = ESteps.Done;
- Status = EOperationStatus.Failed;
- Error = _appManifestLoader.Error;
- }
- else
- {
- _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;
- }
- }
- }
- }
-
-
-
- internal class AppManifestLoader
- {
- private enum ESteps
- {
- LoadStaticVersion,
- CheckStaticVersion,
- LoadAppManifest,
- CheckAppManifest,
- Done,
- }
- private ESteps _steps = ESteps.LoadStaticVersion;
- private UnityWebDataRequester _downloader1;
- private UnityWebDataRequester _downloader2;
-
-
-
- public string Error { private set; get; }
-
-
-
- public PatchManifest Result { private set; get; }
-
-
-
- public int StaticVersion { private set; get; }
-
-
-
- public bool IsDone()
- {
- return _steps == ESteps.Done;
- }
-
-
-
- public float Progress()
- {
- if (_downloader2 == null)
- return 0;
- return _downloader2.Progress();
- }
-
-
-
- public void Update()
- {
- if (IsDone())
- return;
- if (_steps == ESteps.LoadStaticVersion)
- {
- YooLogger.Log($"Load application static version.");
- string filePath = PathHelper.MakeStreamingLoadPath(YooAssetSettings.VersionFileName);
- string url = PathHelper.ConvertToWWWPath(filePath);
- _downloader1 = new UnityWebDataRequester();
- _downloader1.SendRequest(url);
- _steps = ESteps.CheckStaticVersion;
- }
- if (_steps == ESteps.CheckStaticVersion)
- {
- if (_downloader1.IsDone() == false)
- return;
- if (_downloader1.HasError())
- {
- Error = _downloader1.GetError();
- _steps = ESteps.Done;
- }
- else
- {
- StaticVersion = int.Parse(_downloader1.GetText());
- _steps = ESteps.LoadAppManifest;
- }
- _downloader1.Dispose();
- }
- if (_steps == ESteps.LoadAppManifest)
- {
- YooLogger.Log($"Load application patch manifest.");
- string filePath = PathHelper.MakeStreamingLoadPath(YooAssetSettingsData.GetPatchManifestFileName(StaticVersion));
- string url = PathHelper.ConvertToWWWPath(filePath);
- _downloader2 = new UnityWebDataRequester();
- _downloader2.SendRequest(url);
- _steps = ESteps.CheckAppManifest;
- }
- if (_steps == ESteps.CheckAppManifest)
- {
- if (_downloader2.IsDone() == false)
- return;
- if (_downloader2.HasError())
- {
- Error = _downloader2.GetError();
- _steps = ESteps.Done;
- }
- else
- {
-
- Result = PatchManifest.Deserialize(_downloader2.GetText());
- _steps = ESteps.Done;
- }
- _downloader2.Dispose();
- }
- }
- }
-
-
-
- internal class AppManifestCopyer
- {
- private enum ESteps
- {
- CopyAppManifest,
- CheckAppManifest,
- Done,
- }
- private ESteps _steps = ESteps.CopyAppManifest;
- private UnityWebFileRequester _downloader1;
- private int _staticVersion;
-
-
-
- public string Error { private set; get; }
-
-
-
- public bool Result { private set; get; }
-
-
-
- public bool IsDone()
- {
- return _steps == ESteps.Done;
- }
-
-
-
- public void Init(int staticVersion)
- {
- _staticVersion = staticVersion;
- }
-
-
-
- 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();
- }
- }
- }
- }
|