拓展自定义鲁班加载配置,支持UniTask异步 默认使用同步加载

拓展自定义鲁班加载配置,支持UniTask异步 默认使用同步加载
This commit is contained in:
ALEXTANG
2023-08-17 22:00:55 +08:00
parent d7a60002d4
commit bf73ce333b
15 changed files with 592 additions and 107 deletions

View File

@@ -1,14 +1,11 @@
using System.Collections.Generic;
using System.Threading;
using Bright.Serialization;
using Cysharp.Threading.Tasks;
using GameBase;
using GameConfig;
using TEngine;
using UnityEngine;
/// <summary>
/// 配置加载器
/// 配置加载器
/// </summary>
public class ConfigLoader:Singleton<ConfigLoader>
{
@@ -22,84 +19,46 @@ public class ConfigLoader:Singleton<ConfigLoader>
{
if (!_init)
{
Log.Error("Config not loaded. You need Take LoadAsync at first.");
Load();
}
return _tables;
}
}
private readonly Dictionary<string, TextAsset> _configs = new Dictionary<string, TextAsset>();
/// <summary>
/// 异步加载配置。
/// 加载配置。
/// </summary>
public async UniTask LoadAsync()
public void Load()
{
_tables = new Tables();
await _tables.LoadAsync(LoadByteBufAsync);
_tables = new Tables(LoadByteBuf);
_init = true;
}
/// <summary>
/// 异步加载二进制配置。
/// 加载二进制配置。
/// </summary>
/// <param name="file">FileName</param>
/// <returns>ByteBuf</returns>
private async UniTask<ByteBuf> LoadByteBufAsync(string file)
private ByteBuf LoadByteBuf(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<TextAsset>(location,CancellationToken.None);
ret = textAssets.bytes;
RegisterTextAssets(file, textAssets);
}
#if false
Log.Warning($"LoadByteBuf {file} used time {gameTickWatcher.ElapseTime()}");
#endif
var textAssets = GameModule.Resource.LoadAsset<TextAsset>($"{SettingsUtils.FrameworkGlobalSettings.ConfigFolderName}{file}.bytes");
byte[] ret = textAssets.bytes;
return new ByteBuf(ret);
}
/// <summary>
/// 注册配置资源。
/// </summary>
/// <param name="key">资源Key。</param>
/// <param name="value">资源实例。</param>
/// <returns>注册成功。</returns>
private bool RegisterTextAssets(string key, TextAsset value)
{
if (string.IsNullOrEmpty(key))
{
return false;
}
if (value == null)
{
return false;
}
_configs[key] = value;
return true;
}
}
public class ConfigSystem : BaseLogicSys<ConfigSystem>
{
public override bool OnInit()
{
InitConfig().Forget();
Log.Warning("ConfigSystem OnInit");
InitConfig();
return base.OnInit();
}
private async UniTaskVoid InitConfig()
private void InitConfig()
{
await ConfigLoader.Instance.LoadAsync();
ConfigLoader.Instance.Load();
}
}
}