247 lines
10 KiB
C#
247 lines
10 KiB
C#
|
|
using System;
|
|||
|
|
using Cysharp.Threading.Tasks;
|
|||
|
|
using Newtonsoft.Json;
|
|||
|
|
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()
|
|||
|
|
{
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
Debug.Log($"UnityEvo:读取资源配置表成功...{ AppConfig.IPCONFIG}{AppConfig.USERNAME}{AppConfig.PASSWORD}");
|
|||
|
|
// 初始化资源系统
|
|||
|
|
YooAssets.Initialize();
|
|||
|
|
|
|||
|
|
|
|||
|
|
//自定义网络请求器
|
|||
|
|
// 设置自定义请求委托
|
|||
|
|
YooAssets.SetDownloadSystemUnityWebRequest(NasWebRequester);
|
|||
|
|
|
|||
|
|
|
|||
|
|
//初始化资源加载模块
|
|||
|
|
// 增加包存在性检查
|
|||
|
|
var package = YooAssets.TryGetPackage(AppConfig.ASSETPACKGENAME);
|
|||
|
|
if (package == null)
|
|||
|
|
{
|
|||
|
|
Debug.LogWarning($"UnityEvo:资源包 {AppConfig.ASSETPACKGENAME} 不存在,正在尝试创建...");
|
|||
|
|
package = YooAssets.CreatePackage(AppConfig.ASSETPACKGENAME);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
YooAssets.SetDefaultPackage(package);
|
|||
|
|
|
|||
|
|
// 初始化资源包
|
|||
|
|
#if EDITOR_SIMULATEMODE
|
|||
|
|
await EDITOR_SIMULATEMODE(package);
|
|||
|
|
AppConfig.IPCONFIGVideo = "http://192.168.31.100:9527/";
|
|||
|
|
#elif OFFLINE_PLAYMODE
|
|||
|
|
await OFFLINE_PLAYMODE(package);
|
|||
|
|
AppConfig.IPCONFIGVideo = Application.streamingAssetsPath + "/";
|
|||
|
|
|
|||
|
|
#elif HOST_PLAYMODE
|
|||
|
|
if (package.PackageName.Equals("Main"))
|
|||
|
|
{
|
|||
|
|
await OFFLINE_PLAYMODE(package);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
await HOST_PLAYMODE(package);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
AppConfig.IPCONFIGVideo = "http://192.168.31.100:9527/";
|
|||
|
|
#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
|
|||
|
|
|
|||
|
|
FsmSystem.SetCurState(nameof(ResUpdateServerState));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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 buildResult = EditorSimulateModeHelper.SimulateBuild(AppConfig.ASSETPACKGENAME);
|
|||
|
|
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;
|
|||
|
|
string platformName = buildTarget.ToString();
|
|||
|
|
#else
|
|||
|
|
string platformName = Application.platform.ToString();
|
|||
|
|
#endif
|
|||
|
|
Debug.Log($"目标平台标识: {platformName}");
|
|||
|
|
//从服务器加载配置列表
|
|||
|
|
WebRequestSystem webRequestSystem = new WebRequestSystem();
|
|||
|
|
string URL =
|
|||
|
|
$"{AppConfig.IPCONFIG}/HybridclrConfigData/{AppConfig.ASSETPACKGENAME}/{platformName}/{Application.productName}.json";
|
|||
|
|
Debug.Log("UnityEvo: 服务器配置文件地址:" + URL);
|
|||
|
|
string text = await webRequestSystem.Get(URL, GetAuthorization(AppConfig.USERNAME, AppConfig.PASSWORD));
|
|||
|
|
if (!string.IsNullOrEmpty(text))
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
Debug.Log("UnityEvo: 服务器配置文件读取成功:" + text);
|
|||
|
|
text = text.Trim('\uFEFF'); // 移除 BOM 字符
|
|||
|
|
HotfixMainResDomainEntity hotfixMainResDomainEntity =
|
|||
|
|
JsonConvert.DeserializeObject<HotfixMainResDomainEntity>(text);
|
|||
|
|
if (hotfixMainResDomainEntity != null &&
|
|||
|
|
hotfixMainResDomainEntity.domain == AppConfig.ASSETPACKGENAME)
|
|||
|
|
{
|
|||
|
|
AppConfig.IPCONFIG = hotfixMainResDomainEntity.ipconfig;
|
|||
|
|
AppConfig.PATHCONFIG = hotfixMainResDomainEntity.pathconfig;
|
|||
|
|
AppConfig.ASSETPACKGENAME = hotfixMainResDomainEntity.domain;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw new Exception("服务器配置文件中Domain与指定domain不匹配,进程中断,请排查:" + AppConfig.ASSETPACKGENAME);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (JsonException e)
|
|||
|
|
{
|
|||
|
|
Debug.LogError($"UnityEvo:JSON解析失败: {e.Message}\n原始内容: {text}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.LogError("UnityEvo:从服务器获取的配置数据为空");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
string defaultHostServer = $"{AppConfig.IPCONFIG}/{AppConfig.PATHCONFIG}";
|
|||
|
|
string fallbackHostServer = $"{AppConfig.IPCONFIG}/{AppConfig.PATHCONFIG}";
|
|||
|
|
|
|||
|
|
Debug.Log($"UnityEvo:正在初始化远程资源包,主服务器:{defaultHostServer}");
|
|||
|
|
// 在初始化下载器前添加证书验证配置
|
|||
|
|
|
|||
|
|
|
|||
|
|
var initParameters = new HostPlayModeParameters();
|
|||
|
|
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
|
|||
|
|
var cacheFileSystemParams = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices);
|
|||
|
|
cacheFileSystemParams.AddParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, true);
|
|||
|
|
|
|||
|
|
var buildinFileSystemParams = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
|
|||
|
|
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, true);
|
|||
|
|
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.COPY_BUILDIN_PACKAGE_MANIFEST, true);
|
|||
|
|
initParameters.BuildinFileSystemParameters = null;
|
|||
|
|
initParameters.CacheFileSystemParameters = cacheFileSystemParams;
|
|||
|
|
|
|||
|
|
var initOperation = package.InitializeAsync(initParameters);
|
|||
|
|
|
|||
|
|
await initOperation;
|
|||
|
|
|
|||
|
|
if (initOperation.Status == EOperationStatus.Succeed)
|
|||
|
|
Debug.Log("UnityEvo:从远程加载资源包,初始化成功!");
|
|||
|
|
else
|
|||
|
|
Debug.LogError($"UnityEvo:从远程加载资源包,初始化失败:{initOperation.Error}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 自定义网络请求器需要登录
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="url"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
private UnityWebRequest NasWebRequester(string url)
|
|||
|
|
{
|
|||
|
|
var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
|
|||
|
|
var authorization = GetAuthorization(AppConfig.USERNAME, AppConfig.PASSWORD);
|
|||
|
|
request.SetRequestHeader("AUTHORIZATION", authorization);
|
|||
|
|
return request;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GetAuthorization(string userName, string password)
|
|||
|
|
{
|
|||
|
|
string auth = userName + ":" + password;
|
|||
|
|
var bytes = System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth);
|
|||
|
|
return "Basic " + System.Convert.ToBase64String(bytes);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|