This commit is contained in:
2025-09-17 18:56:28 +08:00
commit 54c72710a5
5244 changed files with 5717609 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Machine;
using YooAsset;
internal class FsmClearCacheBundle : IStateNode
{
private StateMachine _machine;
PatchOperationData data;
void IStateNode.OnCreate(StateMachine machine)
{
_machine = machine;
}
void IStateNode.OnEnter()
{
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
var packageName = data.packageName;
var package = YooAssets.GetPackage(packageName);
var operation = package.ClearCacheFilesAsync(EFileClearMode.ClearUnusedBundleFiles);
operation.Completed += Operation_Completed;
}
void IStateNode.OnUpdate()
{
}
void IStateNode.OnExit()
{
}
private void Operation_Completed(YooAsset.AsyncOperationBase obj)
{
_machine.ChangeState<FsmStartGame>();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1d83ce785f1442a40a1ad0f837073b69
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,68 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Machine;
using YooAsset;
public class FsmCreateDownloader : IStateNode
{
private StateMachine _machine;
PatchOperationData data;
void IStateNode.OnCreate(StateMachine machine)
{
_machine = machine;
}
void IStateNode.OnEnter()
{
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
Debug.Log($"创建资源下载器{data.packageName}");
CreateDownloader();
}
void IStateNode.OnUpdate()
{
}
void IStateNode.OnExit()
{
}
void CreateDownloader()
{
var packageName = data.packageName;
var package = YooAssets.GetPackage(packageName);
var downloader = package.CreateResourceDownloader(10, 3);
downloader.DownloadErrorCallback = data.downloadError;
downloader.DownloadFinishCallback = data.downloadFinish;
downloader.DownloadUpdateCallback = data.downloadUpdate;
var curVersion = PlayerPrefs.GetString($"{packageName}_Version", string.Empty);
var packageVersion = (string)_machine.GetBlackboardValue($"{packageName}_Version");
_machine.SetBlackboardValue($"{packageName}_Downloader", downloader);
if (downloader.TotalDownloadCount == 0)
{
Debug.Log("Not found any download files !");
_machine.ChangeState<FsmStartGame>();
}
else
{
if (data.autoDownload)
{
_machine.ChangeState<FsmDownloadPackageFiles>();
}
else
{
MessageBox.Show()
.SetTitle(packageName)
.SetContent($"发现资源更新\n{curVersion}=>{packageVersion}: {downloader.TotalDownloadBytes / 1024f / 1024f:F1}MB")
.AddButton("下载", (box) => { _machine.ChangeState<FsmDownloadPackageFiles>(); })
.AddButton("取消", (box) =>
{
downloader.CancelDownload();
data.useLocal = true;
_machine.SetBlackboardValue("PatchOperationData", data);
_machine.ChangeState<FsmRequestPackageVersion>();
})
.AddButton("退出", (box) => { Application.Quit(); });
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8b104741ebcb72946abe282c1da73360
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,44 @@
using System.Collections;
using UnityEngine;
using UniFramework.Machine;
using YooAsset;
public class FsmDownloadPackageFiles : IStateNode
{
private StateMachine _machine;
PatchOperationData data;
void IStateNode.OnCreate(StateMachine machine)
{
_machine = machine;
}
void IStateNode.OnEnter()
{
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
Debug.Log($"{data.packageName}开始下载资源文件");
GameManager.Inst.StartCoroutine(BeginDownload());
}
void IStateNode.OnUpdate()
{
}
void IStateNode.OnExit()
{
}
private IEnumerator BeginDownload()
{
var downloader = (ResourceDownloaderOperation)_machine.GetBlackboardValue($"{data.packageName}_Downloader");
downloader.BeginDownload();
yield return downloader;
// 检测下载结果
if (downloader.Status != EOperationStatus.Succeed)
{
MessageBox.Show()
.SetTitle(data.packageName)
.SetContent($"下载失败{downloader.Error}")
.AddButton("退出", (box) => { Application.Quit(); });
yield break;
}
_machine.ChangeState<FsmDownloadPackageOver>();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a3fae22040fe2d541ab4582c7c1c71fb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,34 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Machine;
using YooAsset;
internal class FsmDownloadPackageOver : IStateNode
{
private StateMachine _machine;
PatchOperationData data;
void IStateNode.OnCreate(StateMachine machine)
{
_machine = machine;
}
void IStateNode.OnEnter()
{
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
Debug.Log($"{data.packageName}下载完成");
//// 注意:下载完成之后再保存本地版本
//if (data.playMode != EPlayMode.EditorSimulateMode)
//{
// var packageVersion = (string)_machine.GetBlackboardValue($"{data.packageName}_Version");
// PlayerPrefs.SetString($"{data.packageName}_Version", packageVersion);
// Debug.Log($"{data.packageName} 更新流程完毕 保存版本{packageVersion}");
//}
_machine.ChangeState<FsmClearCacheBundle>();
}
void IStateNode.OnUpdate()
{
}
void IStateNode.OnExit()
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c96c57c65f74bb3469dc71b13ef78c84
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,163 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Machine;
using YooAsset;
internal class FsmInitializePackage : IStateNode
{
private StateMachine _machine;
PatchOperationData data;
void IStateNode.OnCreate(StateMachine machine)
{
_machine = machine;
}
void IStateNode.OnEnter()
{
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
Debug.Log($"初始化{data.packageName},下载路径:{GetHostServerURL(data.packageName)}");
GameManager.Inst.StartCoroutine(InitPackage());
}
void IStateNode.OnUpdate()
{
}
void IStateNode.OnExit()
{
}
private IEnumerator InitPackage()
{
var playMode = data.playMode;
var packageName = data.packageName;
// 创建资源包裹类
var package = YooAssets.TryGetPackage(packageName);
if (package == null)
package = YooAssets.CreatePackage(packageName);
// 编辑器下的模拟模式
InitializationOperation initializationOperation = null;
if (playMode == EPlayMode.EditorSimulateMode)
{
var buildResult = EditorSimulateModeHelper.SimulateBuild(packageName);
var packageRoot = buildResult.PackageRootDirectory;
var createParameters = new EditorSimulateModeParameters();
createParameters.EditorFileSystemParameters = FileSystemParameters.CreateDefaultEditorFileSystemParameters(packageRoot);
initializationOperation = package.InitializeAsync(createParameters);
}
// 单机运行模式
if (playMode == EPlayMode.OfflinePlayMode)
{
var createParameters = new OfflinePlayModeParameters();
createParameters.BuildinFileSystemParameters = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
initializationOperation = package.InitializeAsync(createParameters);
}
// 联机运行模式
if (playMode == EPlayMode.HostPlayMode)
{
// 注意设置参数COPY_BUILDIN_PACKAGE_MANIFEST可以初始化的时候拷贝内置清单到沙盒目录
var buildinFileSystemParams = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.COPY_BUILDIN_PACKAGE_MANIFEST, true);
string defaultHostServer = GetHostServerURL(packageName);
string fallbackHostServer = GetHostServerURL(packageName);
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
// 注意设置参数INSTALL_CLEAR_MODE可以解决覆盖安装的时候将拷贝的内置清单文件清理的问题。
var cacheFileSystemParams = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices);
cacheFileSystemParams.AddParameter(FileSystemParametersDefine.INSTALL_CLEAR_MODE, EOverwriteInstallClearMode.None);
var createParameters = new HostPlayModeParameters();
createParameters.BuildinFileSystemParameters = packageName == "Preload" ? buildinFileSystemParams : null;
createParameters.CacheFileSystemParameters = cacheFileSystemParams;
initializationOperation = package.InitializeAsync(createParameters);
}
// WebGL运行模式
if (playMode == EPlayMode.WebPlayMode)
{
#if UNITY_WEBGL && WEIXINMINIGAME && !UNITY_EDITOR
var createParameters = new WebPlayModeParameters();
string defaultHostServer = GetHostServerURL(packageName);
string fallbackHostServer = GetHostServerURL(packageName);
string packageRoot = $"{WeChatWASM.WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE"; //注意:如果有子目录,请修改此处!
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
createParameters.WebServerFileSystemParameters = WechatFileSystemCreater.CreateFileSystemParameters(packageRoot, remoteServices);
initializationOperation = package.InitializeAsync(createParameters);
#else
var createParameters = new WebPlayModeParameters();
createParameters.WebServerFileSystemParameters = FileSystemParameters.CreateDefaultWebServerFileSystemParameters();
initializationOperation = package.InitializeAsync(createParameters);
#endif
}
yield return initializationOperation;
// 如果初始化失败弹出提示界面
if (initializationOperation.Status != EOperationStatus.Succeed)
{
MessageBox.Show()
.SetTitle(packageName)
.SetContent($"{initializationOperation.Error}")
.AddButton("退出", (box) => { Application.Quit(); });
}
else
{
_machine.ChangeState<FsmRequestPackageVersion>();
}
}
/// <summary>
/// 获取资源服务器地址
/// </summary>
public string GetHostServerURL(string packageName)
{
string hostServerIP = $"http://localhost:8080/{Application.productName}";
if (GameManager.Inst.ServerAddress != "" && GameManager.Inst.ServerAddress != null)
hostServerIP = $"{GameManager.Inst.ServerAddress}/{Application.productName}";
string appVersion = "v1";
#if UNITY_EDITOR
if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.Android)
return $"{hostServerIP}/CDN/Android/{packageName}/{appVersion}";
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.iOS)
return $"{hostServerIP}/CDN/IPhone/{packageName}/{appVersion}";
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.WebGL)
return $"{hostServerIP}/CDN/WebGL/{packageName}/{appVersion}";
else
return $"{hostServerIP}/CDN/PC/{packageName}/{appVersion}";
#else
if (Application.platform == RuntimePlatform.Android)
return $"{hostServerIP}/CDN/Android/{packageName}/{appVersion}";
else if (Application.platform == RuntimePlatform.IPhonePlayer)
return $"{hostServerIP}/CDN/IPhone/{packageName}/{appVersion}";
else if (Application.platform == RuntimePlatform.WebGLPlayer)
return $"{hostServerIP}/CDN/WebGL/{packageName}/{appVersion}";
else
return $"{hostServerIP}/CDN/PC/{packageName}/{appVersion}";
#endif
}
/// <summary>
/// 远端资源地址查询服务类
/// </summary>
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}";
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9c67e6e479b68e345afcdf325775c079
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,60 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Machine;
using YooAsset;
internal class FsmRequestPackageVersion : IStateNode
{
private StateMachine _machine;
PatchOperationData data;
void IStateNode.OnCreate(StateMachine machine)
{
_machine = machine;
}
void IStateNode.OnEnter()
{
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
Debug.Log($"更新版本信息{data.packageName}");
GameManager.Inst.StartCoroutine(UpdatePackageVersion());
}
void IStateNode.OnUpdate()
{
}
void IStateNode.OnExit()
{
}
private IEnumerator UpdatePackageVersion()
{
var packageName = data.packageName;
var package = YooAssets.GetPackage(packageName);
var operation = package.RequestPackageVersionAsync(true,10);
var curVersion = PlayerPrefs.GetString($"{packageName}_Version", string.Empty);
yield return operation;
if (operation.Status != EOperationStatus.Succeed || data.useLocal)
{
Debug.LogWarning(operation.Error);
if (string.IsNullOrEmpty(curVersion))
{
MessageBox.Show()
.SetTitle(packageName)
.SetContent(operation.Error)
.AddButton("退出", (box) => { Application.Quit(); });
}
else
{
Debug.Log($"{packageName}获取上次成功记录的版本{curVersion}");
_machine.SetBlackboardValue($"{packageName}_Version", curVersion);
_machine.ChangeState<FsmUpdatePackageManifest>();
}
}
else
{
Debug.Log($"{packageName}获取远端资源版本成功{operation.PackageVersion}");
_machine.SetBlackboardValue($"{packageName}_Version", operation.PackageVersion);
_machine.ChangeState<FsmUpdatePackageManifest>();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f5ea76dfe0fc52b47a00a3bf68a54d09
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Machine;
using YooAsset;
internal class FsmStartGame : IStateNode
{
private PatchOperation _owner;
private StateMachine _machine;
PatchOperationData data;
void IStateNode.OnCreate(StateMachine machine)
{
_owner = machine.Owner as PatchOperation;
_machine = machine;
}
void IStateNode.OnEnter()
{
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
if (data.playMode != EPlayMode.EditorSimulateMode)
{
var packageVersion = (string)_machine.GetBlackboardValue($"{data.packageName}_Version");
PlayerPrefs.SetString($"{data.packageName}_Version", packageVersion);
Debug.Log($"{data.packageName} 更新流程完毕 保存版本{packageVersion}");
}
_owner.SetFinish();
}
void IStateNode.OnUpdate()
{
}
void IStateNode.OnExit()
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6cf80cfbed474c34b892eaeda7fcb054
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,51 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Machine;
using YooAsset;
public class FsmUpdatePackageManifest : IStateNode
{
private StateMachine _machine;
PatchOperationData data;
void IStateNode.OnCreate(StateMachine machine)
{
_machine = machine;
}
void IStateNode.OnEnter()
{
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
Debug.Log($"更新资源清单{data.packageName}");
GameManager.Inst.StartCoroutine(UpdateManifest());
}
void IStateNode.OnUpdate()
{
}
void IStateNode.OnExit()
{
}
private IEnumerator UpdateManifest()
{
var packageName = data.packageName;
var packageVersion = (string)_machine.GetBlackboardValue($"{packageName}_Version");
var package = YooAssets.GetPackage(packageName);
var operation = package.UpdatePackageManifestAsync(packageVersion);
yield return operation;
if (operation.Status != EOperationStatus.Succeed)
{
Debug.LogWarning(operation.Error);
MessageBox.Show()
.SetTitle(packageName)
.SetContent(operation.Error)
.AddButton("退出", (box) => { Application.Quit(); });
yield break;
}
else
{
Debug.Log($"{packageName}资源清单更新成功");
_machine.ChangeState<FsmCreateDownloader>();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 13faaa12de67f5e4db31cd8a44563089
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: