Files
RedHotRoast-ios/Assets/Scripts/System/Network/ConfigSystem.cs
T

123 lines
4.2 KiB
C#
Raw Normal View History

2026-04-22 09:52:55 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using SGModule.Common.Extensions;
using SGModule.Common.Helper;
using SGModule.ConfigLoader;
using SGModule.NetKit;
using UnityEngine;
2026-04-22 09:52:55 +08:00
namespace RedHotRoast {
public class ConfigSystem : BaseSystem {
2026-04-22 09:52:55 +08:00
public static string web_through_str;
public ConfigSystem(bool isAutoInit = true) {
if (isAutoInit) {
2026-04-22 09:52:55 +08:00
Init();
}
}
public sealed override void Init() {
2026-04-22 09:52:55 +08:00
base.Init();
AddListener();
}
private void AddListener() {
NetworkDispatcher.Instance.AddListener(NetworkMsg.GetConfig, OnGetConfig);
2026-04-22 09:52:55 +08:00
}
private void RemoveListener() {
NetworkDispatcher.Instance.RemoveListener(NetworkMsg.GetConfig, OnGetConfig);
2026-04-22 09:52:55 +08:00
}
private void OnGetConfig(object obj) {
TrackKit.TrackLoginFunnel(LoginFunnelEventType.LoadBegin); //加载开始打点
var loginModel = LoginKit.Instance.LoginModel;
ConfigLoader.Instance.Init(new ConfigInitOptions {
Setting = loginModel.Setting,
CdnUrl = loginModel.CdnURL,
OnComplete = state => {
Debug.Log($"配置加载状态{state}");
if (state == ConfigLoaderState.Successful) {
ReloadConfig();
2026-04-22 09:52:55 +08:00
}
},
OnError = (errorName, message) => {
Debug.LogError($"配置解析错误 {errorName} 错误信息:{message}");
},
OnHandleUnmarkedConfig = ParseGameConfig
});
2026-04-22 09:52:55 +08:00
}
/// <summary>
/// 重新加载配置
/// </summary>
/// <param name="json"></param>
private void ReloadConfig() {
TrackKit.TrackLoginFunnel(LoginFunnelEventType.LoadFinish); //加载完成打点
2026-04-22 09:52:55 +08:00
TextureHelper.imgUrl = LoginKit.Instance.LoginModel.CdnURL + "/" + ConfigSystem.GetCommonConf().ResVersion + "/";
LiveVideoManager.videoBaseUrl = LoginKit.Instance.LoginModel.CdnURL + "/" + ConfigSystem.GetCommonConf().ResVersion + "/";
2026-04-22 09:52:55 +08:00
// ParseGameConfig();
UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.NetLoadingUI_Close);
AppDispatcher.Instance.Dispatch(AppMsg.LoginInit);
CtrlDispatcher.Instance.Dispatch(CtrlMsg.Game_StartBefore);
SaveingPotHelper.ResetHistory();
2026-04-22 09:52:55 +08:00
}
#region
2026-04-22 09:52:55 +08:00
private void ParseGameConfig() {
var gameConfigModel = new GameConfigModel();
foreach (var key in ConfigLoader.Instance.GetJsonKeys()) {
if (!key.StartsWith("GameBoard")) {
continue;
2026-04-22 09:52:55 +08:00
}
// 提取 boardIndex
var boardIndex = 1;
var parts = key.Split('_');
if (parts.Length > 1 && int.TryParse(parts[1], out var parsed)) {
boardIndex = parsed;
}
2026-04-22 09:52:55 +08:00
// 获取 json 并反序列化
if (ConfigLoader.Instance.TryGetJsonValue(key, out var gameboardJson)) {
try {
var gameBoards = gameboardJson.As<List<GameBoard>>();
if (gameBoards != null) {
gameConfigModel.game_conf[boardIndex] = gameBoards;
}
else {
Log.ConfigLoader.Warning($"GameBoard 配置 {key} 反序列化为空");
2026-04-22 09:52:55 +08:00
}
}
catch (Exception ex) {
Log.ConfigLoader.Error($"GameBoard 配置 {key} 反序列化失败: {ex.Message}");
}
2026-04-22 09:52:55 +08:00
}
}
ConfigLoader.Instance.AddConfig(gameConfigModel);
2026-04-22 09:52:55 +08:00
}
#endregion
public static CommonModel GetCommonConf() {
return ConfigLoader.Instance.GetConfig<CommonModel>();
}
public static List<T> GetConfig<T>() where T : class {
return ConfigLoader.Instance.GetConfig<List<T>>() ?? new List<T>();
2026-04-22 09:52:55 +08:00
}
public override void Dispose() {
2026-04-22 09:52:55 +08:00
base.Dispose();
RemoveListener();
}
}
}