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 ³õʼ»¯Ïà¹Ø
|
||
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)
|
||
{
|
||
// ×¢Ò⣺ÉèÖòÎÊýCOPY_BUILDIN_PACKAGE_MANIFEST£¬¿ÉÒÔ³õʼ»¯µÄʱºò¿½±´ÄÚÖÃÇåµ¥µ½É³ºÐĿ¼
|
||
buildinFileSystemParams = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
|
||
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.COPY_BUILDIN_PACKAGE_MANIFEST, true);
|
||
}
|
||
string defaultHostServer = GetHostServerURL();
|
||
string fallbackHostServer = GetHostServerURL();
|
||
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
|
||
// ×¢Ò⣺ÉèÖòÎÊýINSTALL_CLEAR_MODE£¬¿ÉÒÔ½â¾ö¸²¸Ç°²×°µÄʱºò½«¿½±´µÄÄÚÖÃÇåµ¥ÎļþÇåÀíµÄÎÊÌâ¡£
|
||
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"; //×¢Ò⣺Èç¹ûÓÐ×ÓĿ¼£¬ÇëÐ޸Ĵ˴¦£¡
|
||
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 ³õʼ»¯Ïà¹Ø
|
||
#region °æ±¾ºÍ×ÊÔ´Çåµ¥Ïà¹Ø
|
||
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 °æ±¾ºÍ×ÊÔ´Çåµ¥Ïà¹Ø
|
||
#region ÏÂÔØÏà¹Ø
|
||
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 ÏÂÔØÏà¹Ø
|
||
}
|