132 lines
4.8 KiB
C#
132 lines
4.8 KiB
C#
|
|
using Cysharp.Threading.Tasks;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using YooAsset;
|
|||
|
|
|
|||
|
|
public class PreloadOperation
|
|||
|
|
{
|
|||
|
|
PatchOperationData data;
|
|||
|
|
PatchOperation operation;
|
|||
|
|
public PreloadOperation(EPlayMode playMode)
|
|||
|
|
{
|
|||
|
|
data = new PatchOperationData();
|
|||
|
|
data.packageName = "Preload";
|
|||
|
|
data.playMode = playMode;
|
|||
|
|
data.useBuildinFileSystem = true;
|
|||
|
|
data.downloadingMaxNum = 10;
|
|||
|
|
data.failedTryAgain = 3;
|
|||
|
|
|
|||
|
|
operation = new PatchOperation(data);
|
|||
|
|
}
|
|||
|
|
public async UniTask Execute()
|
|||
|
|
{
|
|||
|
|
#if !UNITY_EDITOR
|
|||
|
|
CheckIsOffline();
|
|||
|
|
#endif
|
|||
|
|
InitializationOperation initializationOperation = await operation.InitializePackage();
|
|||
|
|
//<2F><>ʼ<EFBFBD><CABC>ʧ<EFBFBD><CAA7>
|
|||
|
|
if (initializationOperation.Status != EOperationStatus.Succeed)
|
|||
|
|
{
|
|||
|
|
MessageBox.Show()
|
|||
|
|
.SetTitle(operation.data.packageName)
|
|||
|
|
.SetContent($"{initializationOperation.Error}")
|
|||
|
|
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
Debug.Log($"<22><>ʼ<EFBFBD><CABC><EFBFBD>ɹ<EFBFBD>{operation.data.packageName}");
|
|||
|
|
var version = await GetBestPackageVersion();
|
|||
|
|
#if UNITY_EDITOR
|
|||
|
|
var PackageVersionOperation = await operation.RequestPackageVersion();
|
|||
|
|
version = PackageVersionOperation.PackageVersion;
|
|||
|
|
#endif
|
|||
|
|
//<2F><>ȡ<EFBFBD>汾ʧ<E6B1BE><CAA7>
|
|||
|
|
if (version == null)
|
|||
|
|
{
|
|||
|
|
MessageBox.Show()
|
|||
|
|
.SetTitle(operation.data.packageName)
|
|||
|
|
.SetContent("<22><>ȡ<EFBFBD>汾ʧ<E6B1BE><CAA7>")
|
|||
|
|
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
operation.packageVersion = version;
|
|||
|
|
Debug.Log($"<22><>ȡ<EFBFBD>汾<EFBFBD>ɹ<EFBFBD>{operation.data.packageName}<7D><>{version}");
|
|||
|
|
UpdatePackageManifestOperation updatePackageManifest = await operation.UpdatePackageManifest();
|
|||
|
|
//<2F><>ȡ<EFBFBD><C8A1>Դ<EFBFBD>嵥ʧ<E5B5A5><CAA7>
|
|||
|
|
if (updatePackageManifest.Status != EOperationStatus.Succeed)
|
|||
|
|
{
|
|||
|
|
MessageBox.Show()
|
|||
|
|
.SetTitle(operation.data.packageName)
|
|||
|
|
.SetContent(updatePackageManifest.Error)
|
|||
|
|
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
await LoadAndShowPatchWindow();
|
|||
|
|
_ = UpdatePreloadPackage();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
void CheckIsOffline()
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(operation.GetCachedPackageVersion()))
|
|||
|
|
{
|
|||
|
|
operation.data.playMode = EPlayMode.OfflinePlayMode;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
async Task<string> GetBestPackageVersion()
|
|||
|
|
{
|
|||
|
|
string cachedVersion = operation.GetCachedPackageVersion();
|
|||
|
|
if (!string.IsNullOrEmpty(cachedVersion))
|
|||
|
|
{
|
|||
|
|
return cachedVersion;
|
|||
|
|
}
|
|||
|
|
string buildinVersion = await operation.GetBuildinPackageVersion();
|
|||
|
|
if (!string.IsNullOrEmpty(buildinVersion))
|
|||
|
|
{
|
|||
|
|
return buildinVersion;
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
private async UniTask LoadAndShowPatchWindow()
|
|||
|
|
{
|
|||
|
|
var assetHandle = operation.package.LoadAssetAsync<GameObject>("PatchWindow");
|
|||
|
|
await assetHandle.ToUniTask();
|
|||
|
|
if (assetHandle.Status == EOperationStatus.Succeed)
|
|||
|
|
GameObject.Instantiate(assetHandle.AssetObject, GameManager.Inst.MainUICanvas.transform);
|
|||
|
|
}
|
|||
|
|
private async UniTask UpdatePreloadPackage()
|
|||
|
|
{
|
|||
|
|
var PackageVersionOperation = await operation.RequestPackageVersion();
|
|||
|
|
if (PackageVersionOperation.Status != EOperationStatus.Succeed)
|
|||
|
|
{
|
|||
|
|
Debug.Log($"{operation.data.packageName}<7D><>̨<EFBFBD><CCA8><EFBFBD>°汾ʧ<E6B1BE>ܣ<EFBFBD>{PackageVersionOperation.Error}");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
operation.packageVersion = PackageVersionOperation.PackageVersion;
|
|||
|
|
var PackageManifestOperation = await operation.UpdatePackageManifest();
|
|||
|
|
if (PackageManifestOperation.Status != EOperationStatus.Succeed)
|
|||
|
|
{
|
|||
|
|
Debug.Log($"{operation.data.packageName}<7D><>̨<EFBFBD><CCA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>嵥ʧ<E5B5A5>ܣ<EFBFBD>{PackageManifestOperation.Error}");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
var DownloaderOperation = operation.CreateDownloader();
|
|||
|
|
if(DownloaderOperation.TotalDownloadCount == 0)
|
|||
|
|
{
|
|||
|
|
operation.SaveVersionToCache();
|
|||
|
|
Debug.Log($"<22><>̨<EFBFBD><CCA8><EFBFBD><EFBFBD>{operation.data.packageName}<7D><><EFBFBD>ɣ<EFBFBD><C9A3>汾<EFBFBD><E6B1BE>{operation.packageVersion}");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (!await operation.DownloadPackageFiles())
|
|||
|
|
{
|
|||
|
|
Debug.Log($"{operation.data.packageName}<7D><>̨<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
var ClearCacheFilesOperation = await operation.ClearCacheBundle();
|
|||
|
|
if (ClearCacheFilesOperation.Status != EOperationStatus.Succeed)
|
|||
|
|
{
|
|||
|
|
Debug.Log($"{operation.data.packageName}<7D><>̨<EFBFBD><CCA8><EFBFBD><EFBFBD>δʹ<CEB4>û<EFBFBD><C3BB><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>{ClearCacheFilesOperation.Error}");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
operation.SaveVersionToCache();
|
|||
|
|
Debug.Log($"<22><>̨<EFBFBD><CCA8><EFBFBD><EFBFBD>{operation.data.packageName}<7D><><EFBFBD>ɣ<EFBFBD><C9A3>汾<EFBFBD><E6B1BE>{operation.packageVersion}");
|
|||
|
|
}
|
|||
|
|
}
|