2025-05-23 18:26:47 +08:00
|
|
|
|
using System;
|
2025-05-30 14:50:46 +08:00
|
|
|
|
using System.IO;
|
2025-05-23 18:26:47 +08:00
|
|
|
|
using Cysharp.Threading.Tasks;
|
2025-05-30 14:50:46 +08:00
|
|
|
|
using Main;
|
2025-05-23 18:26:47 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2025-05-30 14:50:46 +08:00
|
|
|
|
using Stary.Evo.UIFarme;
|
2025-05-23 18:26:47 +08:00
|
|
|
|
using UnityEditor;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.Assertions;
|
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
using YooAsset;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Stary.Evo
|
|
|
|
|
|
{
|
|
|
|
|
|
public class ResStartState : AbstractFSMIState
|
|
|
|
|
|
{
|
|
|
|
|
|
public ResStartState(IFsmSystem system) : base(system)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override async UniTask OnEnterAsync()
|
|
|
|
|
|
{
|
2025-05-30 14:50:46 +08:00
|
|
|
|
Debug.Log("UnityEvo:启动开始资源初始化...");
|
2025-05-23 18:26:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//初始化读取资源配置表
|
|
|
|
|
|
HotfixMainResDomain hotfixMainResDomain = Resources.Load<HotfixMainResDomain>("HotfixMainResDomain");
|
|
|
|
|
|
if (hotfixMainResDomain != null)
|
|
|
|
|
|
{
|
2025-05-30 14:50:46 +08:00
|
|
|
|
AppConfig.IpConfig = hotfixMainResDomain.hotfixMainResDomainEntity.ipconfig;
|
|
|
|
|
|
AppConfig.UserName = hotfixMainResDomain.hotfixMainResDomainEntity.username;
|
|
|
|
|
|
AppConfig.PassWord = hotfixMainResDomain.hotfixMainResDomainEntity.password;
|
|
|
|
|
|
AppConfig.ProductName = hotfixMainResDomain.hotfixMainResDomainEntity.productName;
|
|
|
|
|
|
AppConfig.MainDomainVersion = hotfixMainResDomain.hotfixMainResDomainEntity.mainDomainVersion;
|
2025-05-23 18:26:47 +08:00
|
|
|
|
}
|
2025-05-30 14:50:46 +08:00
|
|
|
|
|
|
|
|
|
|
Debug.Log($"UnityEvo:读取资源配置表成功...{AppConfig.IpConfig}{AppConfig.UserName}{AppConfig.PassWord}");
|
2025-05-23 18:26:47 +08:00
|
|
|
|
// 初始化资源系统
|
|
|
|
|
|
YooAssets.Initialize();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//自定义网络请求器
|
|
|
|
|
|
// 设置自定义请求委托
|
2025-05-30 14:50:46 +08:00
|
|
|
|
//YooAssets.SetDownloadSystemUnityWebRequest(NasWebRequester);
|
2025-05-23 18:26:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//初始化资源加载模块
|
|
|
|
|
|
// 增加包存在性检查
|
2025-05-30 14:50:46 +08:00
|
|
|
|
var package = YooAssets.TryGetPackage(AppConfig.PackageDomainName);
|
2025-05-23 18:26:47 +08:00
|
|
|
|
if (package == null)
|
|
|
|
|
|
{
|
2025-05-30 14:50:46 +08:00
|
|
|
|
Debug.LogWarning($"UnityEvo:资源包 {AppConfig.PackageDomainName} 不存在,正在尝试创建...");
|
|
|
|
|
|
package = YooAssets.CreatePackage(AppConfig.PackageDomainName);
|
2025-05-23 18:26:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
YooAssets.SetDefaultPackage(package);
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化资源包
|
|
|
|
|
|
#if EDITOR_SIMULATEMODE
|
|
|
|
|
|
await EDITOR_SIMULATEMODE(package);
|
2025-05-30 14:50:46 +08:00
|
|
|
|
FsmSystem.SetCurState(nameof(ResUpdateServerState));
|
2025-05-23 18:26:47 +08:00
|
|
|
|
#elif OFFLINE_PLAYMODE
|
|
|
|
|
|
await OFFLINE_PLAYMODE(package);
|
2025-05-30 14:50:46 +08:00
|
|
|
|
FsmSystem.SetCurState(nameof(ResUpdateLocalState));
|
2025-05-23 18:26:47 +08:00
|
|
|
|
#elif HOST_PLAYMODE
|
|
|
|
|
|
if (package.PackageName.Equals("Main"))
|
|
|
|
|
|
{
|
|
|
|
|
|
await OFFLINE_PLAYMODE(package);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-05-30 14:50:46 +08:00
|
|
|
|
//登录
|
|
|
|
|
|
string url = AppConfig.IpConfig + "/Authentication/login";
|
|
|
|
|
|
bool isLogin = await WebRequestSystem.Login(url, AppConfig.UserName, AppConfig.PassWord);
|
|
|
|
|
|
if (isLogin)
|
|
|
|
|
|
await HOST_PLAYMODE(package);
|
|
|
|
|
|
else
|
|
|
|
|
|
await OFFLINE_PLAYMODE(package);
|
2025-05-23 18:26:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-30 14:50:46 +08:00
|
|
|
|
FsmSystem.SetCurState(nameof(ResUpdateLocalState));
|
|
|
|
|
|
|
2025-05-23 18:26:47 +08:00
|
|
|
|
#elif WEB_PLAYMODE
|
|
|
|
|
|
// IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
|
|
|
|
|
|
// var webServerFileSystemParams = FileSystemParameters.CreateDefaultWebServerFileSystemParameters();
|
|
|
|
|
|
// var webRemoteFileSystemParams =
|
|
|
|
|
|
// FileSystemParameters.CreateDefaultWebRemoteFileSystemParameters(remoteServices); //支持跨域下载
|
|
|
|
|
|
//
|
|
|
|
|
|
// var initParameters = new WebPlayModeParameters();
|
|
|
|
|
|
// initParameters.WebServerFileSystemParameters = webServerFileSystemParams;
|
|
|
|
|
|
// initParameters.WebRemoteFileSystemParameters = webRemoteFileSystemParams;
|
|
|
|
|
|
//
|
|
|
|
|
|
// var initOperation = package.InitializeAsync(initParameters);
|
|
|
|
|
|
// await initOperation;
|
|
|
|
|
|
//
|
|
|
|
|
|
// if (initOperation.Status == EOperationStatus.Succeed)
|
|
|
|
|
|
// Debug.Log("UnityEvo:资源包初始化成功!");
|
|
|
|
|
|
// else
|
|
|
|
|
|
// Debug.LogError($"UnityEvo:资源包初始化失败:{initOperation.Error}");
|
|
|
|
|
|
|
|
|
|
|
|
Debug.LogError($"UnityEvo:暂不支持");
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-30 14:50:46 +08:00
|
|
|
|
public override UniTask OnEnterAsync<T>(T param)
|
2025-05-23 18:26:47 +08:00
|
|
|
|
{
|
2025-05-30 14:50:46 +08:00
|
|
|
|
return UniTask.CompletedTask;
|
2025-05-23 18:26:47 +08:00
|
|
|
|
}
|
2025-05-30 14:50:46 +08:00
|
|
|
|
|
2025-05-23 18:26:47 +08:00
|
|
|
|
public override UniTask OnEnterAsync<T1, T2>(T1 param1, T2 param2)
|
|
|
|
|
|
{
|
|
|
|
|
|
return UniTask.CompletedTask;
|
|
|
|
|
|
}
|
2025-05-30 14:50:46 +08:00
|
|
|
|
|
2025-05-23 18:26:47 +08:00
|
|
|
|
public override void OnUpdate()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnUpdate();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override UniTask OnExitAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
return UniTask.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async UniTask EDITOR_SIMULATEMODE(ResourcePackage package)
|
|
|
|
|
|
{
|
2025-05-30 14:50:46 +08:00
|
|
|
|
var buildResult = EditorSimulateModeHelper.SimulateBuild(AppConfig.PackageDomainName);
|
2025-05-23 18:26:47 +08:00
|
|
|
|
var packageRoot = buildResult.PackageRootDirectory;
|
|
|
|
|
|
var editorFileSystemParameters = FileSystemParameters.CreateDefaultEditorFileSystemParameters(packageRoot);
|
|
|
|
|
|
var initParams = new EditorSimulateModeParameters();
|
|
|
|
|
|
initParams.EditorFileSystemParameters = editorFileSystemParameters;
|
|
|
|
|
|
var initialization = package.InitializeAsync(initParams);
|
|
|
|
|
|
await initialization;
|
|
|
|
|
|
if (initialization.Status == EOperationStatus.Succeed)
|
|
|
|
|
|
{
|
|
|
|
|
|
Assert.AreEqual(EOperationStatus.Succeed, initialization.Status);
|
|
|
|
|
|
Debug.Log("UnityEvo:资源包初始化成功!");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError($"UnityEvo:资源包初始化失败:{initialization.Error}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async UniTask OFFLINE_PLAYMODE(ResourcePackage package)
|
|
|
|
|
|
{
|
|
|
|
|
|
var buildinFileSystemParams = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
|
|
|
|
|
|
var initParameters = new OfflinePlayModeParameters();
|
|
|
|
|
|
initParameters.BuildinFileSystemParameters = buildinFileSystemParams;
|
|
|
|
|
|
var initOperation = package.InitializeAsync(initParameters);
|
|
|
|
|
|
await initOperation;
|
|
|
|
|
|
|
|
|
|
|
|
if (initOperation.Status == EOperationStatus.Succeed)
|
|
|
|
|
|
Debug.Log("UnityEvo:从本地加载资源包,初始化成功!");
|
|
|
|
|
|
else
|
|
|
|
|
|
Debug.LogError($"UnityEvo:从本地加载资源包,初始化失败:{initOperation.Error}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async UniTask HOST_PLAYMODE(ResourcePackage package)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 新增平台判断代码
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
BuildTarget buildTarget = UnityEditor.EditorUserBuildSettings.activeBuildTarget;
|
2025-05-30 14:50:46 +08:00
|
|
|
|
AppConfig.Platform = buildTarget.ToString();
|
2025-05-23 18:26:47 +08:00
|
|
|
|
#else
|
2025-05-30 14:50:46 +08:00
|
|
|
|
AppConfig.Platform = Application.platform.ToString();
|
2025-05-23 18:26:47 +08:00
|
|
|
|
#endif
|
2025-05-30 14:50:46 +08:00
|
|
|
|
Debug.Log($"目标平台标识: {AppConfig.Platform}");
|
|
|
|
|
|
|
|
|
|
|
|
// 请求资源版本
|
|
|
|
|
|
string url = $"{AppConfig.IpConfig}/ResDomain/GetResDomainByDomain";
|
|
|
|
|
|
var resDmainRequst = new ResDmainRequst()
|
|
|
|
|
|
{
|
|
|
|
|
|
ProductName = AppConfig.ProductName,
|
|
|
|
|
|
DomainName = AppConfig.PackageDomainName,
|
|
|
|
|
|
Platform = AppConfig.Platform,
|
|
|
|
|
|
};
|
|
|
|
|
|
//获取服务器版本
|
|
|
|
|
|
var resDmainMessageEntity = await WebRequestSystem.Post(url, JsonConvert.SerializeObject(resDmainRequst));
|
|
|
|
|
|
if (resDmainMessageEntity.code == 200)
|
2025-05-23 18:26:47 +08:00
|
|
|
|
{
|
2025-05-30 14:50:46 +08:00
|
|
|
|
ResDmainResponse resDmainResponse =
|
|
|
|
|
|
JsonConvert.DeserializeObject<ResDmainResponse>(resDmainMessageEntity.data.ToString());
|
|
|
|
|
|
//获取当前版本
|
|
|
|
|
|
var oldVersion = PlayerPrefs.GetString($"{AppConfig.PackageDomainName}_GAME_VERSION");
|
|
|
|
|
|
//版本不一致,开始下载
|
|
|
|
|
|
if (resDmainResponse.PackageVersion != oldVersion)
|
2025-05-23 18:26:47 +08:00
|
|
|
|
{
|
2025-05-30 14:50:46 +08:00
|
|
|
|
await Download(resDmainResponse.DocumentFileId);
|
|
|
|
|
|
PlayerPrefs.SetString($"{AppConfig.PackageDomainName}_GAME_VERSION",
|
|
|
|
|
|
resDmainResponse.PackageVersion);
|
2025-05-23 18:26:47 +08:00
|
|
|
|
}
|
2025-05-30 14:50:46 +08:00
|
|
|
|
else //版本一致,加载缓存资源
|
2025-05-23 18:26:47 +08:00
|
|
|
|
{
|
2025-05-30 14:50:46 +08:00
|
|
|
|
Debug.Log($"UnityEvo:资源版本一致,自动跳过更新...");
|
2025-05-23 18:26:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-05-30 14:50:46 +08:00
|
|
|
|
Debug.LogError($"UnityEvo:获取资源版本失败: 【{resDmainMessageEntity.message}】");
|
2025-05-23 18:26:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-30 14:50:46 +08:00
|
|
|
|
var initParameters = new OfflinePlayModeParameters();
|
|
|
|
|
|
var buildinFileSystemParams = FileSystemParameters.CreateDefaultBuildinFileSystemParameters(null,
|
|
|
|
|
|
$"{Application.persistentDataPath}/DownloadedContent/{AppConfig.PackageDomainName}");
|
2025-05-23 18:26:47 +08:00
|
|
|
|
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, true);
|
|
|
|
|
|
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.COPY_BUILDIN_PACKAGE_MANIFEST, true);
|
2025-05-30 14:50:46 +08:00
|
|
|
|
initParameters.BuildinFileSystemParameters = buildinFileSystemParams;
|
|
|
|
|
|
// initParameters.CacheFileSystemParameters = cacheFileSystemParams;
|
2025-05-23 18:26:47 +08:00
|
|
|
|
|
|
|
|
|
|
var initOperation = package.InitializeAsync(initParameters);
|
|
|
|
|
|
|
|
|
|
|
|
await initOperation;
|
|
|
|
|
|
|
|
|
|
|
|
if (initOperation.Status == EOperationStatus.Succeed)
|
|
|
|
|
|
Debug.Log("UnityEvo:从远程加载资源包,初始化成功!");
|
|
|
|
|
|
else
|
|
|
|
|
|
Debug.LogError($"UnityEvo:从远程加载资源包,初始化失败:{initOperation.Error}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-30 14:50:46 +08:00
|
|
|
|
public async UniTask Download(string fileId)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 在任意MonoBehaviour或DomainBase派生类中
|
|
|
|
|
|
string loadPath = $"{Application.persistentDataPath}/DownloadedContent/{AppConfig.PackageDomainName}";
|
|
|
|
|
|
if (Directory.Exists(loadPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.Delete(loadPath, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await MainArchitecture.Interface.GetSystem<IPanelSystem>()
|
|
|
|
|
|
.PushQueue<ProgressBarPanel>(Camera.main.transform, "Main");
|
|
|
|
|
|
await ZipTool.DownloadAndUnzipAsync(fileId, loadPath, DownLoadProgress, UnzipProgress);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void DownLoadProgress(float progress)
|
2025-05-23 18:26:47 +08:00
|
|
|
|
{
|
2025-05-30 14:50:46 +08:00
|
|
|
|
Debug.Log($"下载进度:{progress:P0}");
|
|
|
|
|
|
MainArchitecture.Interface.GetSystem<IPanelSystem>()
|
|
|
|
|
|
.SendPanelEvent(ProgressBarPanel.ProgressBarType.Add, "下载中", progress);
|
2025-05-23 18:26:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-30 14:50:46 +08:00
|
|
|
|
private void UnzipProgress(float progress)
|
2025-05-23 18:26:47 +08:00
|
|
|
|
{
|
2025-05-30 14:50:46 +08:00
|
|
|
|
Debug.Log($"解压进度:{progress:P0}");
|
|
|
|
|
|
MainArchitecture.Interface.GetSystem<IPanelSystem>()
|
|
|
|
|
|
.SendPanelEvent(ProgressBarPanel.ProgressBarType.Add, "解压中", progress);
|
2025-05-23 18:26:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|