This commit is contained in:
2025-11-03 00:24:36 +08:00
parent 23eecc60c8
commit 3ba6a928cd
1030 changed files with 218646 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
using Cysharp.Threading.Tasks;
using UnityEngine;
using YooAsset;
public class MainOperation
{
PatchOperationData data;
PatchOperation operation;
public MainOperation(EPlayMode playMode)
{
data = new PatchOperationData();
data.packageName = "Main";
data.playMode = playMode;
data.useBuildinFileSystem = false;
data.downloadingMaxNum = 10;
data.failedTryAgain = 3;
operation = new PatchOperation(data);
}
public async UniTask Execute()
{
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;
}
var PackageVersionOperation = await operation.RequestPackageVersion();
if (PackageVersionOperation.Status != EOperationStatus.Succeed)
{
MessageBox.Show()
.SetTitle(operation.data.packageName)
.SetContent($"{PackageVersionOperation.Error}")
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
return;
}
operation.packageVersion = PackageVersionOperation.PackageVersion;
var PackageManifestOperation = await operation.UpdatePackageManifest();
if (PackageManifestOperation.Status != EOperationStatus.Succeed)
{
MessageBox.Show()
.SetTitle(operation.data.packageName)
.SetContent($"{PackageManifestOperation.Error}")
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
return;
}
var DownloaderOperation = operation.CreateDownloader();
if (DownloaderOperation.TotalDownloadCount == 0)
{
operation.SaveVersionToCache();
return;
}
if (!await operation.DownloadPackageFiles())
{
MessageBox.Show()
.SetTitle(operation.data.packageName)
.SetContent($"{DownloaderOperation.Error}")
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
return;
}
var ClearCacheFilesOperation = await operation.ClearCacheBundle();
if (ClearCacheFilesOperation.Status != EOperationStatus.Succeed)
{
MessageBox.Show()
.SetTitle(operation.data.packageName)
.SetContent($"{ClearCacheFilesOperation.Error}")
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
return;
}
operation.SaveVersionToCache();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 573021b0c5d8ec74f96d2a20ee3a7165

View File

@@ -0,0 +1,18 @@
using UnityEngine;
using Cysharp.Threading.Tasks;
using YooAsset;
public class PatchManager : Singleton<PatchManager>
{
public async UniTask<bool> StartOperation(EPlayMode playMode)
{
YooAssets.Initialize();
PreloadOperation _preloadOperation = new PreloadOperation(playMode);
await _preloadOperation.Execute();
MainOperation _mainOperation = new MainOperation(playMode);
await _mainOperation.Execute();
return true;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 625284a2d721ab646904f46c84f37a8a

View File

@@ -0,0 +1,230 @@
using Cysharp.Threading.Tasks;
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using YooAsset;
using static YooAsset.DownloaderOperation;
public class PatchOperationData
{
public string packageName;
public EPlayMode playMode;
public bool useBuildinFileSystem;
public DownloadError downloadError;
public DownloaderFinish downloadFinish;
public DownloadUpdate downloadUpdate;
public int downloadingMaxNum = 10;
public int failedTryAgain = 3;
}
public class PatchOperation
{
public PatchOperationData data;
public ResourcePackage package;
public ResourceDownloaderOperation downloader;
public string packageVersion;
public string PkgVersionKey;
public PatchOperation(PatchOperationData data)
{
this.data = data;
PkgVersionKey = $"{Application.productName}_{data.packageName}";
}
#region <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public async UniTask<InitializationOperation> InitializePackage()
{
package = YooAssets.TryGetPackage(data.packageName);
if (package == null)
package = YooAssets.CreatePackage(data.packageName);
InitializationOperation initializationOperation = null;
switch (data.playMode)
{
case EPlayMode.EditorSimulateMode:
initializationOperation = InitializeEditorMode(package);
break;
case EPlayMode.OfflinePlayMode:
initializationOperation = InitializeOfflineMode(package);
break;
case EPlayMode.HostPlayMode:
initializationOperation = InitializeHostMode(package);
break;
case EPlayMode.WebPlayMode:
initializationOperation = InitializeWebPlayMode(package);
break;
case EPlayMode.CustomPlayMode:
break;
default:
break;
}
await initializationOperation.ToUniTask();
return initializationOperation;
}
private InitializationOperation InitializeEditorMode(ResourcePackage package)
{
var buildResult = EditorSimulateModeHelper.SimulateBuild(data.packageName);
var packageRoot = buildResult.PackageRootDirectory;
var createParameters = new EditorSimulateModeParameters();
createParameters.EditorFileSystemParameters = FileSystemParameters.CreateDefaultEditorFileSystemParameters(packageRoot);
return package.InitializeAsync(createParameters);
}
private InitializationOperation InitializeOfflineMode(ResourcePackage package)
{
var createParameters = new OfflinePlayModeParameters();
createParameters.BuildinFileSystemParameters = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
return package.InitializeAsync(createParameters);
}
private InitializationOperation InitializeHostMode(ResourcePackage package)
{
FileSystemParameters buildinFileSystemParams = null;
if (data.useBuildinFileSystem)
{
// ע<><EFBFBD><E2A3BA><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>COPY_BUILDIN_PACKAGE_MANIFEST<53><54><EFBFBD><EFBFBD><EFBFBD>Գ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʱ<EFBFBD>򿽱<EFBFBD><F2BFBDB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5B5A5>ɳ<EFBFBD><C9B3>Ŀ¼
buildinFileSystemParams = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.COPY_BUILDIN_PACKAGE_MANIFEST, true);
}
string defaultHostServer = GetHostServerURL();
string fallbackHostServer = GetHostServerURL();
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
// ע<><EFBFBD><E2A3BA><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>INSTALL_CLEAR_MODE<44><45><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>װ<EFBFBD><D7B0>ʱ<EFBFBD>򽫿<EFBFBD><F2BDABBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
var cacheFileSystemParams = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices);
cacheFileSystemParams.AddParameter(FileSystemParametersDefine.INSTALL_CLEAR_MODE, EOverwriteInstallClearMode.ClearAllManifestFiles);
var createParameters = new HostPlayModeParameters();
createParameters.BuildinFileSystemParameters = buildinFileSystemParams;
createParameters.CacheFileSystemParameters = cacheFileSystemParams;
return package.InitializeAsync(createParameters);
}
private InitializationOperation InitializeWebPlayMode(ResourcePackage package)
{
#if UNITY_WEBGL && WEIXINMINIGAME && !UNITY_EDITOR
var createParameters = new WebPlayModeParameters();
string defaultHostServer = GetHostServerURL();
string fallbackHostServer = GetHostServerURL();
string packageRoot = $"{WeChatWASM.WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE"; //ע<><EFBFBD><E2A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD>޸Ĵ˴<C4B4><CBB4><EFBFBD>
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
createParameters.WebServerFileSystemParameters = WechatFileSystemCreater.CreateFileSystemParameters(packageRoot, remoteServices);
return package.InitializeAsync(createParameters);
#else
var createParameters = new WebPlayModeParameters();
createParameters.WebServerFileSystemParameters = FileSystemParameters.CreateDefaultWebServerFileSystemParameters();
return package.InitializeAsync(createParameters);
#endif
}
private string GetHostServerURL()
{
string hostServerIP = $"https://home.gtuantuan.online:9444/{Application.productName}";
string appVersion = "v1";
#if UNITY_EDITOR
if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.Android)
// return $"{hostServerIP}/CDN/Android/{packageName}/{appVersion}";
return $"{hostServerIP}/CDN/PC/{data.packageName}/{appVersion}";
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.iOS)
return $"{hostServerIP}/CDN/IPhone/{data.packageName}/{appVersion}";
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.WebGL)
return $"{hostServerIP}/CDN/WebGL/{data.packageName}/{appVersion}";
else
return $"{hostServerIP}/CDN/PC/{data.packageName}/{appVersion}";
#else
if (Application.platform == RuntimePlatform.Android)
return $"{hostServerIP}/CDN/Android/{data.packageName}/{appVersion}";
else if (Application.platform == RuntimePlatform.IPhonePlayer)
return $"{hostServerIP}/CDN/IPhone/{data.packageName}/{appVersion}";
else if (Application.platform == RuntimePlatform.WebGLPlayer)
return $"{hostServerIP}/CDN/WebGL/{data.packageName}/{appVersion}";
else
return $"{hostServerIP}/CDN/PC/{data.packageName}/{appVersion}";
#endif
}
private class RemoteServices : IRemoteServices
{
private readonly string _defaultHostServer;
private readonly string _fallbackHostServer;
public RemoteServices(string defaultHostServer, string fallbackHostServer)
{
_defaultHostServer = defaultHostServer;
_fallbackHostServer = fallbackHostServer;
}
string IRemoteServices.GetRemoteMainURL(string fileName)
{
return $"{_defaultHostServer}/{fileName}";
}
string IRemoteServices.GetRemoteFallbackURL(string fileName)
{
return $"{_fallbackHostServer}/{fileName}";
}
}
#endregion <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public async UniTask<RequestPackageVersionOperation> RequestPackageVersion()
{
var operation = package.RequestPackageVersionAsync(true, 1);
await operation.ToUniTask();
return operation;
}
public async Task<string> GetBuildinPackageVersion()
{
var operation = new GetBuildinPackageVersionOperation(data.packageName);
YooAssets.StartOperation(operation);
await operation;
if (operation.Status == EOperationStatus.Succeed)
{
return operation.PackageVersion;
}
else
{
return null;
}
}
public string GetCachedPackageVersion()
{
if (PlayerPrefs.HasKey(PkgVersionKey))
{
return PlayerPrefs.GetString(PkgVersionKey);
}
return null;
}
public void SaveVersionToCache()
{
#if !UNITY_EDITOR
PlayerPrefs.SetString(PkgVersionKey, packageVersion);
PlayerPrefs.Save();
#endif
}
public async UniTask<UpdatePackageManifestOperation> UpdatePackageManifest()
{
var operation = package.UpdatePackageManifestAsync(packageVersion,5);
await operation.ToUniTask();
return operation;
}
#endregion <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public ResourceDownloaderOperation CreateDownloader()
{
downloader = package.CreateResourceDownloader(data.downloadingMaxNum, data.failedTryAgain);
downloader.DownloadErrorCallback = data.downloadError;
downloader.DownloadFinishCallback = data.downloadFinish;
downloader.DownloadUpdateCallback = data.downloadUpdate;
return downloader;
}
public async UniTask<bool> DownloadPackageFiles()
{
if (downloader.TotalDownloadCount == 0)
return true;
Debug.Log($"{data.packageName} DownloadPackageFiles {downloader.TotalDownloadCount}");
downloader.BeginDownload();
await downloader.ToUniTask();
return downloader.Status == EOperationStatus.Succeed;
}
public async UniTask<ClearCacheFilesOperation> ClearCacheBundle()
{
var operation = package.ClearCacheFilesAsync(EFileClearMode.ClearUnusedBundleFiles);
await operation.ToUniTask();
return operation;
}
#endregion <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c2a463a1d7227c3478645bd706515e73

View File

@@ -0,0 +1,131 @@
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}");
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4532eb0a1a183db43aa88ef78d4d9ee8