【m】更新后台

This commit is contained in:
2025-05-30 14:50:46 +08:00
parent cbd48e8411
commit 078f080fcc
68 changed files with 764 additions and 342 deletions

View File

@@ -8,24 +8,24 @@ namespace Stary.Evo
/// <summary>
/// package name
/// </summary>
public static string ASSETPACKGENAME;
public static string PackageDomainName { get; set; }
/// <summary>
/// 热更资源路径
/// </summary>
public static string PATHCONFIG;
public static string IPCONFIG;
public static string USERNAME;
public static string PASSWORD;
public static string IPCONFIGVideo;
public static string IpConfig { get; set; }
public static string UserName { get; set; }
public static string PassWord { get; set; }
public static string ProductName { get; set; }
public static string Platform { get; set; }
public static string MainDomainVersion { get; set; }
/// <summary>
/// 主场景main实例物体
/// </summary>
private static GameObject _MainBaseModel;
/// <summary>
/// 赋值默认的实例
/// </summary>

View File

@@ -32,7 +32,7 @@ namespace Stary.Evo
{
DomainConfig domainConfig = param as DomainConfig;
Debug.Log("UnityEvo:热更脚本...");
var package = YooAssets.GetPackage(AppConfig.ASSETPACKGENAME);
var package = YooAssets.GetPackage(AppConfig.PackageDomainName);
// Editor环境下HotUpdate.dll.bytes已经被自动加载不需要加载重复加载反而会出问题。
@@ -44,7 +44,7 @@ namespace Stary.Evo
Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies()
.First(a => a.GetName().Name == $"HotUpdate_{AppConfig.ASSETPACKGENAME}");
#else
string hotUpdateAssemblyName = $"HotUpdate_{AppConfig.ASSETPACKGENAME}";
string hotUpdateAssemblyName = $"HotUpdate_{AppConfig.PackageDomainName}";
Type assemblyType = IsAssetLoaded(fullClassName);
@@ -78,9 +78,9 @@ namespace Stary.Evo
LoadMetadataForAOTAssemblies();
// 加载热更dll
var hotfixDll = package.LoadAssetAsync<TextAsset>($"Android_HotUpdate_{AppConfig.ASSETPACKGENAME}.dll");
var hotfixDll = package.LoadAssetAsync<TextAsset>($"Android_HotUpdate_{AppConfig.PackageDomainName}.dll");
await hotfixDll;
var hotfixPdb = package.LoadAssetAsync<TextAsset>($"Android_HotUpdate_{AppConfig.ASSETPACKGENAME}.pdb");
var hotfixPdb = package.LoadAssetAsync<TextAsset>($"Android_HotUpdate_{AppConfig.PackageDomainName}.pdb");
await hotfixPdb;
Assembly hotUpdateAss = null;
if (hotfixDll.Status == EOperationStatus.Succeed)
@@ -100,7 +100,7 @@ namespace Stary.Evo
}
else
{
Debug.LogError($"UnityEvo:Android_HotUpdate_{AppConfig.ASSETPACKGENAME}.dll加载失败");
Debug.LogError($"UnityEvo:Android_HotUpdate_{AppConfig.PackageDomainName}.dll加载失败");
}

View File

@@ -1,6 +1,9 @@
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;
@@ -16,38 +19,39 @@ namespace Stary.Evo
}
public override async UniTask OnEnterAsync()
{
Debug.Log("UnityEvo:启动开始资源初始化...");
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.IpConfig = hotfixMainResDomain.hotfixMainResDomainEntity.ipconfig;
AppConfig.UserName = hotfixMainResDomain.hotfixMainResDomainEntity.username;
AppConfig.PassWord = hotfixMainResDomain.hotfixMainResDomainEntity.password;
AppConfig.ProductName = hotfixMainResDomain.hotfixMainResDomainEntity.productName;
AppConfig.MainDomainVersion = hotfixMainResDomain.hotfixMainResDomainEntity.mainDomainVersion;
}
Debug.Log($"UnityEvo:读取资源配置表成功...{ AppConfig.IPCONFIG}{AppConfig.USERNAME}{AppConfig.PASSWORD}");
Debug.Log($"UnityEvo:读取资源配置表成功...{AppConfig.IpConfig}{AppConfig.UserName}{AppConfig.PassWord}");
// 初始化资源系统
YooAssets.Initialize();
//自定义网络请求器
// 设置自定义请求委托
YooAssets.SetDownloadSystemUnityWebRequest(NasWebRequester);
//YooAssets.SetDownloadSystemUnityWebRequest(NasWebRequester);
//初始化资源加载模块
// 增加包存在性检查
var package = YooAssets.TryGetPackage(AppConfig.ASSETPACKGENAME);
var package = YooAssets.TryGetPackage(AppConfig.PackageDomainName);
if (package == null)
{
Debug.LogWarning($"UnityEvo:资源包 {AppConfig.ASSETPACKGENAME} 不存在,正在尝试创建...");
package = YooAssets.CreatePackage(AppConfig.ASSETPACKGENAME);
Debug.LogWarning($"UnityEvo:资源包 {AppConfig.PackageDomainName} 不存在,正在尝试创建...");
package = YooAssets.CreatePackage(AppConfig.PackageDomainName);
}
YooAssets.SetDefaultPackage(package);
@@ -55,11 +59,10 @@ namespace Stary.Evo
// 初始化资源包
#if EDITOR_SIMULATEMODE
await EDITOR_SIMULATEMODE(package);
AppConfig.IPCONFIGVideo = "http://192.168.31.100:9527/";
FsmSystem.SetCurState(nameof(ResUpdateServerState));
#elif OFFLINE_PLAYMODE
await OFFLINE_PLAYMODE(package);
AppConfig.IPCONFIGVideo = Application.streamingAssetsPath + "/";
FsmSystem.SetCurState(nameof(ResUpdateLocalState));
#elif HOST_PLAYMODE
if (package.PackageName.Equals("Main"))
{
@@ -67,10 +70,17 @@ namespace Stary.Evo
}
else
{
await HOST_PLAYMODE(package);
//登录
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);
}
AppConfig.IPCONFIGVideo = "http://192.168.31.100:9527/";
FsmSystem.SetCurState(nameof(ResUpdateLocalState));
#elif WEB_PLAYMODE
// IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
// var webServerFileSystemParams = FileSystemParameters.CreateDefaultWebServerFileSystemParameters();
@@ -91,24 +101,23 @@ namespace Stary.Evo
Debug.LogError($"UnityEvo:暂不支持");
#endif
FsmSystem.SetCurState(nameof(ResUpdateServerState));
}
public override UniTask OnEnterAsync<T>(T param)
public override UniTask OnEnterAsync<T>(T param)
{
return UniTask.CompletedTask;
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()
{
@@ -117,7 +126,7 @@ namespace Stary.Evo
private async UniTask EDITOR_SIMULATEMODE(ResourcePackage package)
{
var buildResult = EditorSimulateModeHelper.SimulateBuild(AppConfig.ASSETPACKGENAME);
var buildResult = EditorSimulateModeHelper.SimulateBuild(AppConfig.PackageDomainName);
var packageRoot = buildResult.PackageRootDirectory;
var editorFileSystemParameters = FileSystemParameters.CreateDefaultEditorFileSystemParameters(packageRoot);
var initParams = new EditorSimulateModeParameters();
@@ -154,65 +163,52 @@ namespace Stary.Evo
// 新增平台判断代码
#if UNITY_EDITOR
BuildTarget buildTarget = UnityEditor.EditorUserBuildSettings.activeBuildTarget;
string platformName = buildTarget.ToString();
AppConfig.Platform = buildTarget.ToString();
#else
string platformName = Application.platform.ToString();
AppConfig.Platform = 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))
Debug.Log($"目标平台标识: {AppConfig.Platform}");
// 请求资源版本
string url = $"{AppConfig.IpConfig}/ResDomain/GetResDomainByDomain";
var resDmainRequst = new ResDmainRequst()
{
try
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)
{
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);
}
await Download(resDmainResponse.DocumentFileId);
PlayerPrefs.SetString($"{AppConfig.PackageDomainName}_GAME_VERSION",
resDmainResponse.PackageVersion);
}
catch (JsonException e)
else //版本一致,加载缓存资源
{
Debug.LogError($"UnityEvo:JSON解析失败: {e.Message}\n原始内容: {text}");
Debug.Log($"UnityEvo:资源版本一致,自动跳过更新...");
}
}
else
{
Debug.LogError("UnityEvo:从服务器获取的配置数据为空");
Debug.LogError($"UnityEvo:获取资源版本失败: 【{resDmainMessageEntity.message}】");
}
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();
var initParameters = new OfflinePlayModeParameters();
var buildinFileSystemParams = FileSystemParameters.CreateDefaultBuildinFileSystemParameters(null,
$"{Application.persistentDataPath}/DownloadedContent/{AppConfig.PackageDomainName}");
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, true);
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.COPY_BUILDIN_PACKAGE_MANIFEST, true);
initParameters.BuildinFileSystemParameters = null;
initParameters.CacheFileSystemParameters = cacheFileSystemParams;
initParameters.BuildinFileSystemParameters = buildinFileSystemParams;
// initParameters.CacheFileSystemParameters = cacheFileSystemParams;
var initOperation = package.InitializeAsync(initParameters);
@@ -224,24 +220,32 @@ namespace Stary.Evo
Debug.LogError($"UnityEvo:从远程加载资源包,初始化失败:{initOperation.Error}");
}
/// <summary>
/// 自定义网络请求器需要登录
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private UnityWebRequest NasWebRequester(string url)
public async UniTask Download(string fileId)
{
var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
var authorization = GetAuthorization(AppConfig.USERNAME, AppConfig.PASSWORD);
request.SetRequestHeader("AUTHORIZATION", authorization);
return request;
// 在任意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 string GetAuthorization(string userName, string password)
private void DownLoadProgress(float progress)
{
string auth = userName + ":" + password;
var bytes = System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth);
return "Basic " + System.Convert.ToBase64String(bytes);
Debug.Log($"下载进度:{progress:P0}");
MainArchitecture.Interface.GetSystem<IPanelSystem>()
.SendPanelEvent(ProgressBarPanel.ProgressBarType.Add, "下载中", progress);
}
private void UnzipProgress(float progress)
{
Debug.Log($"解压进度:{progress:P0}");
MainArchitecture.Interface.GetSystem<IPanelSystem>()
.SendPanelEvent(ProgressBarPanel.ProgressBarType.Add, "解压中", progress);
}
}
}

View File

@@ -20,20 +20,40 @@ namespace Stary.Evo
public override async UniTask OnEnterAsync()
{
//初始化读取资源配置表
var package = YooAssets.GetPackage(AppConfig.ASSETPACKGENAME);
var package = YooAssets.GetPackage(AppConfig.PackageDomainName);
//更新失败
Debug.Log($"UnityEvo:切换为加载本地缓存资源...");
// 获取上次成功记录的版本
string packageVersion = PlayerPrefs.GetString("GAME_VERSION", string.Empty);
if (string.IsNullOrEmpty(packageVersion))
string packageVersion = "";
if (package.PackageName.Equals("Main"))
{
Debug.Log($"UnityEvo:没有找到本地版本记录,需要更新资源!");
return;
var operation = package.RequestPackageVersionAsync();
await operation;
if (operation.Status == EOperationStatus.Succeed)
{
//更新成功
packageVersion = operation.PackageVersion;
Debug.Log($"Unity:【Main】Request package Version : {packageVersion}");
}
else
{
//更新失败
Debug.LogError("Unity:【Main】"+operation.Error);
}
}
else
{
// 获取上次成功记录的版本
packageVersion = PlayerPrefs.GetString($"{AppConfig.PackageDomainName}_GAME_VERSION", string.Empty);
if (string.IsNullOrEmpty(packageVersion))
{
Debug.Log($"UnityEvo:没有找到本地版本记录,需要更新资源!");
return;
}
}
Debug.Log($"UnityEvo:获取资源版本 Version : 【{packageVersion}】");
Debug.Log($"UnityEvo:开始加载本地资源...");
@@ -116,7 +136,7 @@ namespace Stary.Evo
{
int downloadingMaxNum = 1;
int failedTryAgain = 1;
var package = YooAssets.GetPackage(AppConfig.ASSETPACKGENAME);
var package = YooAssets.GetPackage(AppConfig.PackageDomainName);
var downloader = package.CreateResourceDownloader(downloadingMaxNum, failedTryAgain);
// 在正常开始游戏之前,还需要验证本地清单内容的完整性。

View File

@@ -20,7 +20,7 @@ namespace Stary.Evo
// //初始化读取资源配置表
var package = YooAssets.GetPackage(AppConfig.ASSETPACKGENAME);
var package = YooAssets.GetPackage(AppConfig.PackageDomainName);
// 请求资源版本
var requetVersionOp = package.RequestPackageVersionAsync();
await requetVersionOp;
@@ -29,7 +29,7 @@ namespace Stary.Evo
{
//更新成功
packageVersion = requetVersionOp.PackageVersion;
PlayerPrefs.SetString("GAME_VERSION", packageVersion);
PlayerPrefs.SetString($"{AppConfig.PackageDomainName}_GAME_VERSION", packageVersion);
Debug.Log($"UnityEvo:获取资源版本 Version : 【{packageVersion}】");
Debug.Log($"UnityEvo:开始加载服务器资源...");
}
@@ -120,46 +120,34 @@ namespace Stary.Evo
public async UniTask Download()
{
// 在任意MonoBehaviour或DomainBase派生类中
string[] andUnzipAsyncPath= await ZipTool.DownloadAndUnzipAsync(
$"{AppConfig.IPCONFIG}/XOSMOPlug_inLibrary/{AppConfig.ASSETPACKGENAME}/Android/1.0.5/X_02_01_1.0.5.zip",
Application.persistentDataPath + "/DownloadedContent",
progress => Debug.Log($"下载进度:{progress:P0}"),
progress => Debug.Log($"解压进度:{progress:P0}")
);
var package = YooAssets.GetPackage(AppConfig.ASSETPACKGENAME);
var downloader = package.CreateResourceImporter(andUnzipAsyncPath, 3, 3);
int downloadingMaxNum = 10;
int failedTryAgain = 3;
var package = YooAssets.GetPackage(AppConfig.PackageDomainName);
var downloader = package.CreateResourceDownloader(downloadingMaxNum, failedTryAgain);
//没有需要下载的资源
if (downloader.TotalDownloadCount == 0)
{
Debug.Log("UnityEvo:没有需要下载的资源,跳过更新");
return;
}
//需要下载的文件总数和总大小
int totalDownloadCount = downloader.TotalDownloadCount;
long totalDownloadBytes = downloader.TotalDownloadBytes;
Debug.Log($"UnityEvo:需要下载的资源的个数【{totalDownloadCount}】,需要下载的资源的总大小{totalDownloadBytes/1024} MB");
//===================================适应新版本YooAsset插件的修改===================================
//注册回调方法
downloader.DownloadErrorCallback = OnDownloadErrorFunction;
downloader.DownloadUpdateCallback = OnDownloadProgressUpdateFunction;
downloader.DownloadFinishCallback = OnDownloadOverFunction;
downloader.DownloadFileBeginCallback = OnStartDownloadFileFunction;
//===================================适应新版本YooAsset插件的修改===================================
// int downloadingMaxNum = 10;
// int failedTryAgain = 3;
// var package = YooAssets.GetPackage(AppConfig.ASSETPACKGENAME);
// var downloader = package.CreateResourceDownloader(downloadingMaxNum, failedTryAgain);
// //没有需要下载的资源
// if (downloader.TotalDownloadCount == 0)
// {
// Debug.Log("UnityEvo:没有需要下载的资源,跳过更新");
// return;
// }
//
// //需要下载的文件总数和总大小
// int totalDownloadCount = downloader.TotalDownloadCount;
// long totalDownloadBytes = downloader.TotalDownloadBytes;
// Debug.Log($"UnityEvo:需要下载的资源的个数【{totalDownloadCount}】,需要下载的资源的总大小{totalDownloadBytes/1024} MB");
// //===================================适应新版本YooAsset插件的修改===================================
// //注册回调方法
// downloader.DownloadErrorCallback = OnDownloadErrorFunction;
// downloader.DownloadUpdateCallback = OnDownloadProgressUpdateFunction;
// downloader.DownloadFinishCallback = OnDownloadOverFunction;
// downloader.DownloadFileBeginCallback = OnStartDownloadFileFunction;
// //===================================适应新版本YooAsset插件的修改===================================
//
// //开启下载
// downloader.BeginDownload();
await downloader;
//开启下载
downloader.BeginDownload();
await downloader;
//检测下载结果
if (downloader.Status == EOperationStatus.Succeed)

View File

@@ -30,20 +30,20 @@ namespace Stary.Evo
domain = "Main";
#endif
_fsmSystem.SetOpenDomainType(OpenDomainType.DEFAULT);
AppConfig.ASSETPACKGENAME = domain;
AppConfig.PackageDomainName = domain;
_fsmSystem.SetCurState(nameof(ResStartState));
}
public void OpenDomain()
{
AppConfig.ASSETPACKGENAME = domain;
AppConfig.PackageDomainName = domain;
_fsmSystem.SetCurState(nameof(ResStartState));
}
public void OpenDomain(string domain, OpenDomainType openDomainType)
{
_fsmSystem.SetOpenDomainType(openDomainType);
AppConfig.ASSETPACKGENAME = domain;
AppConfig.PackageDomainName = domain;
_fsmSystem.SetCurState(nameof(ResStartState));
}

View File

@@ -0,0 +1,7 @@
namespace Stary.Evo
{
public class WebRequestTools
{
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 48091c7012fc4cd88a54c096febdcdd0
timeCreated: 1748318581