11111
This commit is contained in:
277
Assets/00.StaryEvoTools/Runtime/HybridLoad/Fsm/ResStartState.cs
Normal file
277
Assets/00.StaryEvoTools/Runtime/HybridLoad/Fsm/ResStartState.cs
Normal file
@@ -0,0 +1,277 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Main;
|
||||
using Newtonsoft.Json;
|
||||
using Stary.Evo.UIFarme;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
using UnityEngine.Networking;
|
||||
using YooAsset;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public class ResStartState : AbstractFSMIStateAsync
|
||||
{
|
||||
public ResStartState(IFsmSystemAsync system) : base(system)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public override async UniTask OnEnterAsync()
|
||||
{
|
||||
Debug.Log("UnityEvo:启动开始资源初始化...");
|
||||
|
||||
|
||||
//初始化读取资源配置表
|
||||
HotfixMainResDomain hotfixMainResDomain = Resources.Load<HotfixMainResDomain>("HotfixMainResDomain");
|
||||
if (hotfixMainResDomain != null)
|
||||
{
|
||||
AppConfig.IpConfig = hotfixMainResDomain.hotfixMainResDomainEntity.ipconfig;
|
||||
AppConfig.UserName = hotfixMainResDomain.hotfixMainResDomainEntity.username;
|
||||
AppConfig.PassWord = hotfixMainResDomain.hotfixMainResDomainEntity.password;
|
||||
AppConfig.ProductName =Application.identifier;
|
||||
AppConfig.MainDomainVersion = hotfixMainResDomain.hotfixMainResDomainEntity.mainDomainVersion;
|
||||
}
|
||||
|
||||
Debug.Log($"UnityEvo:读取资源配置表成功...{AppConfig.IpConfig}{AppConfig.UserName}{AppConfig.PassWord}");
|
||||
// 初始化资源系统
|
||||
YooAssets.Initialize();
|
||||
|
||||
|
||||
//自定义网络请求器
|
||||
// 设置自定义请求委托
|
||||
//YooAssets.SetDownloadSystemUnityWebRequest(NasWebRequester);
|
||||
|
||||
|
||||
//初始化资源加载模块
|
||||
// 增加包存在性检查
|
||||
var package = YooAssets.TryGetPackage(AppConfig.PackageDomainName);
|
||||
if (package == null)
|
||||
{
|
||||
Debug.LogWarning($"UnityEvo:资源包 {AppConfig.PackageDomainName} 不存在,正在尝试创建...");
|
||||
package = YooAssets.CreatePackage(AppConfig.PackageDomainName);
|
||||
}
|
||||
|
||||
YooAssets.SetDefaultPackage(package);
|
||||
|
||||
// 初始化资源包
|
||||
#if EDITOR_SIMULATEMODE
|
||||
await EDITOR_SIMULATEMODE(package);
|
||||
FsmSystem.SetCurState(nameof(ResUpdateServerState));
|
||||
#elif OFFLINE_PLAYMODE
|
||||
await OFFLINE_PLAYMODE(package);
|
||||
FsmSystem.SetCurState(nameof(ResUpdateLocalState));
|
||||
#elif HOST_PLAYMODE
|
||||
// if (package.PackageName.Equals("Main"))
|
||||
// {
|
||||
// await OFFLINE_PLAYMODE(package);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
//登录
|
||||
string url = AppConfig.IpConfig + "/Authentication/login";
|
||||
bool isLogin = await WebRequestSystem.Login(url, AppConfig.UserName, AppConfig.PassWord);
|
||||
if (isLogin)
|
||||
await HOST_PLAYMODE(package);
|
||||
else
|
||||
await Chche_PLAYMODE(package);
|
||||
// }
|
||||
|
||||
FsmSystem.SetCurState(nameof(ResUpdateLocalState));
|
||||
|
||||
#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
|
||||
}
|
||||
|
||||
public override UniTask OnEnterAsync<T>(T param)
|
||||
{
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public override UniTask OnEnterAsync<T1, T2>(T1 param1, T2 param2)
|
||||
{
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public override void OnUpdate()
|
||||
{
|
||||
base.OnUpdate();
|
||||
}
|
||||
|
||||
|
||||
public override UniTask OnExitAsync()
|
||||
{
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
private async UniTask EDITOR_SIMULATEMODE(ResourcePackage package)
|
||||
{
|
||||
var initParams=YooAssetFileSystem.EditorSimulateInitializeParameter();
|
||||
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 Chche_PLAYMODE(ResourcePackage package)
|
||||
{
|
||||
Debug.Log("UnityEvo:网络连接不通畅,切换缓存加载!");
|
||||
var initParams=YooAssetFileSystem.hostInitializeParameter();
|
||||
var initOperation = package.InitializeAsync(initParams);
|
||||
await initOperation;
|
||||
|
||||
var operation = package.RequestPackageVersionAsync();
|
||||
await operation;
|
||||
if (operation.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
PlayerPrefs.SetString($"{AppConfig.PackageDomainName}_GAME_VERSION", operation.PackageVersion);
|
||||
Debug.Log("UnityEvo:从本地缓存中加载资源包,初始化获取版本号成功!");
|
||||
}else
|
||||
{
|
||||
Debug.LogError($"UnityEvo:从本地缓存中加载资源包,初始化获取版本号失败!");
|
||||
}
|
||||
|
||||
if (initOperation.Status == EOperationStatus.Succeed)
|
||||
Debug.Log("UnityEvo:从本地缓存中资源包,初始化成功!");
|
||||
else
|
||||
Debug.LogError($"UnityEvo:从本地缓存中资源包,初始化失败:{initOperation.Error}");
|
||||
}
|
||||
private async UniTask OFFLINE_PLAYMODE(ResourcePackage package)
|
||||
{
|
||||
var initParams=YooAssetFileSystem.OfflineInitializeParameter();
|
||||
var initOperation = package.InitializeAsync(initParams);
|
||||
await initOperation;
|
||||
|
||||
var operation = package.RequestPackageVersionAsync();
|
||||
await operation;
|
||||
if (operation.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
PlayerPrefs.SetString($"{AppConfig.PackageDomainName}_GAME_VERSION", operation.PackageVersion);
|
||||
Debug.Log("UnityEvo:从本地加载资源包,初始化获取版本号成功!");
|
||||
}else
|
||||
{
|
||||
Debug.LogError($"UnityEvo:从本地加载资源包,初始化获取版本号失败!");
|
||||
}
|
||||
|
||||
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;
|
||||
AppConfig.Platform = buildTarget.ToString();
|
||||
#else
|
||||
AppConfig.Platform = Application.platform.ToString();
|
||||
#endif
|
||||
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)
|
||||
{
|
||||
ResDmainResponse resDmainResponse =
|
||||
JsonConvert.DeserializeObject<ResDmainResponse>(resDmainMessageEntity.data.ToString());
|
||||
//获取当前版本
|
||||
var oldVersion = PlayerPrefs.GetString($"{AppConfig.PackageDomainName}_GAME_VERSION");
|
||||
//版本不一致,开始下载
|
||||
if (resDmainResponse.PackageVersion != oldVersion)
|
||||
{
|
||||
await Download(resDmainResponse.DocumentFileId);
|
||||
PlayerPrefs.SetString($"{AppConfig.PackageDomainName}_GAME_VERSION",
|
||||
resDmainResponse.PackageVersion);
|
||||
}
|
||||
else //版本一致,加载缓存资源
|
||||
{
|
||||
Debug.Log($"UnityEvo:资源版本一致,自动跳过更新...");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"UnityEvo:获取资源版本失败: 【{resDmainMessageEntity.message}】");
|
||||
}
|
||||
|
||||
var initParams=YooAssetFileSystem.hostInitializeParameter();
|
||||
// initParameters.CacheFileSystemParameters = cacheFileSystemParams;
|
||||
|
||||
var initOperation = package.InitializeAsync(initParams);
|
||||
|
||||
await initOperation;
|
||||
|
||||
if (initOperation.Status == EOperationStatus.Succeed)
|
||||
Debug.Log("UnityEvo:从远程加载资源包,初始化成功!");
|
||||
else
|
||||
Debug.LogError($"UnityEvo:从远程加载资源包,初始化失败:{initOperation.Error}");
|
||||
}
|
||||
|
||||
public async UniTask Download(string fileId)
|
||||
{
|
||||
// 在任意MonoBehaviour或DomainBase派生类中
|
||||
string loadPath = $"{Application.persistentDataPath}/DownloadedContent/{AppConfig.PackageDomainName}";
|
||||
if (Directory.Exists(loadPath))
|
||||
{
|
||||
Directory.Delete(loadPath, true);
|
||||
}
|
||||
FsmLoadSystem loadSystem =FsmSystem as FsmLoadSystem;
|
||||
if (loadSystem.ProgressBarPanel == null)
|
||||
{
|
||||
loadSystem.ProgressBarPanel =Object.Instantiate(Resources.Load<GameObject>("ProgressBarPanel"),
|
||||
Camera.main.transform).GetOrAddComponent<ProgressBarPanel>();
|
||||
}
|
||||
await ZipTool.DownloadAndUnzipAsync(fileId, loadPath, DownLoadProgress, UnzipProgress);
|
||||
}
|
||||
|
||||
private void DownLoadProgress(float progress)
|
||||
{
|
||||
Debug.Log($"下载进度:{progress:P0}");
|
||||
FsmLoadSystem loadSystem =FsmSystem as FsmLoadSystem;
|
||||
loadSystem.ProgressBarPanel.SetProgressBarValue("下载中", progress);
|
||||
}
|
||||
|
||||
private void UnzipProgress(float progress)
|
||||
{
|
||||
Debug.Log($"解压进度:{progress:P0}");
|
||||
FsmLoadSystem loadSystem =FsmSystem as FsmLoadSystem;
|
||||
loadSystem.ProgressBarPanel.SetProgressBarValue("解压中", progress);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user