using System.Collections.Generic; using System.Threading; using Bright.Serialization; using Cysharp.Threading.Tasks; using GameBase; using GameConfig; using TEngine; using UnityEngine; /// /// 配置加载器 /// public class ConfigSystem : Singleton { private bool _init = false; private Tables _tables; public Tables Tables { get { if (!_init) { #if !UNITY_WEBGL _init = true; #endif Log.Error("Config not loaded. You need Take LoadAsync at first."); } return _tables; } } private readonly Dictionary _configs = new Dictionary(); /// /// 异步加载配置。 /// public async UniTask LoadAsync() { _tables = new Tables(); await _tables.LoadAsync(LoadByteBufAsync); _init = true; } /// /// 异步加载二进制配置。 /// /// FileName /// ByteBuf private async UniTask LoadByteBufAsync(string file) { #if false GameTickWatcher gameTickWatcher = new GameTickWatcher(); #endif byte[] ret; var location = file; if (_configs.TryGetValue(location, out var config)) { ret = config.bytes; } else { var textAssets = await GameModule.Resource.LoadAssetAsync(location, CancellationToken.None); ret = textAssets.bytes; RegisterTextAssets(file, textAssets); } #if false Log.Warning($"LoadByteBuf {file} used time {gameTickWatcher.ElapseTime()}"); #endif return new ByteBuf(ret); } /// /// 注册配置资源。 /// /// 资源Key。 /// 资源实例。 /// 注册成功。 private bool RegisterTextAssets(string key, TextAsset value) { if (string.IsNullOrEmpty(key)) { return false; } if (value == null) { return false; } _configs[key] = value; return true; } }