231 lines
9.5 KiB
C#
231 lines
9.5 KiB
C#
|
|
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>
|
|||
|
|
}
|