修改luban模板配置,支持异步加载配置实例

修改luban模板配置,支持异步加载配置实例
This commit is contained in:
ALEXTANG
2023-08-15 12:07:21 +08:00
parent 5239b89326
commit aa0d40c758
8 changed files with 132 additions and 101 deletions

View File

@@ -38,6 +38,7 @@ public partial class GameApp
//带生命周期的单例系统。 //带生命周期的单例系统。
AddLogicSys(BehaviourSingleSystem.Instance); AddLogicSys(BehaviourSingleSystem.Instance);
AddLogicSys(DataCenterSys.Instance); AddLogicSys(DataCenterSys.Instance);
AddLogicSys(ConfigSystem.Instance);
GMBehaviourSystem.Instance.Active(); GMBehaviourSystem.Instance.Active();
} }

View File

@@ -1,8 +1,11 @@
using System.Collections.Generic;
using System.Threading;
using Bright.Serialization; using Bright.Serialization;
using System.IO; using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using GameBase; using GameBase;
using GameConfig; using GameConfig;
using SimpleJSON; using TEngine;
using UnityEngine; using UnityEngine;
/// <summary> /// <summary>
@@ -20,57 +23,84 @@ public class ConfigLoader:Singleton<ConfigLoader>
{ {
if (!_init) if (!_init)
{ {
_init = true; Log.Error("Config not loaded. You need Take LoadAsync at first.");
Load();
} }
return _tables; return _tables;
} }
} }
/// <summary> private readonly Dictionary<string, TextAsset> _configs = new Dictionary<string, TextAsset>();
/// 加载配置
/// </summary>
public void Load()
{
var tablesCtor = typeof(Tables).GetConstructors()[0];
var loaderReturnType = tablesCtor.GetParameters()[0].ParameterType.GetGenericArguments()[1];
System.Delegate loader = loaderReturnType == typeof(ByteBuf) /// <summary>
? new System.Func<string, ByteBuf>(LoadByteBuf) /// 异步加载配置。
: (System.Delegate)new System.Func<string, JSONNode>(LoadJson); /// </summary>
_tables = (Tables)tablesCtor.Invoke(new object[] { loader }); public async Task LoadAsync()
{
_tables = new Tables();
await _tables.LoadAsync(LoadByteBufAsync);
_init = true;
} }
/// <summary> /// <summary>
/// 加载Json配置。 /// 异步加载二进制配置。
/// </summary>
/// <param name="file">FileName</param>
/// <returns>JSONNode</returns>
private JSONNode LoadJson(string file)
{
#if UNITY_EDITOR
var ret = File.ReadAllText($"{Application.dataPath}/../GenerateDatas/json/{file}.json", System.Text.Encoding.UTF8);
#else
var textAssets = GameModule.Resource.LoadAsset<TextAsset>(file);
var ret = textAssets.text;
#endif
return JSON.Parse(ret);
}
/// <summary>
/// 加载二进制配置。
/// </summary> /// </summary>
/// <param name="file">FileName</param> /// <param name="file">FileName</param>
/// <returns>ByteBuf</returns> /// <returns>ByteBuf</returns>
private ByteBuf LoadByteBuf(string file) private async Task<ByteBuf> LoadByteBufAsync(string file)
{ {
byte[] ret = null; #if false
#if UNITY_EDITOR GameTickWatcher gameTickWatcher = new GameTickWatcher();
ret = File.ReadAllBytes($"{Application.dataPath}/../GenerateDatas/bytes/{file}.bytes"); #endif
#else byte[] ret;
var textAssets = GameModule.Resource.LoadAsset<TextAsset>(file); 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; ret = textAssets.bytes;
RegisterTextAssets(file, textAssets);
}
#if false
Log.Warning($"LoadByteBuf {file} used time {gameTickWatcher.ElapseTime()}");
#endif #endif
return new ByteBuf(ret); 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();
return base.OnInit();
}
private async UniTaskVoid InitConfig()
{
await ConfigLoader.Instance.LoadAsync();
}
} }

View File

@@ -6,36 +6,37 @@
// </auto-generated> // </auto-generated>
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
using Bright.Serialization; using Bright.Serialization;
using System.Threading.Tasks;
namespace GameConfig namespace GameConfig
{ {
public partial class Tables
{
public item.TbItem TbItem {get; }
public Battle.TbSkill TbSkill {get; }
public Battle.TbBuff TbBuff {get; }
public Battle.TbBuffAttr TbBuffAttr {get; }
public Tables(System.Func<string, ByteBuf> loader) public sealed class Tables
{
public item.TbItem TbItem {get; private set; }
public Battle.TbSkill TbSkill {get; private set; }
public Battle.TbBuff TbBuff {get; private set; }
public Battle.TbBuffAttr TbBuffAttr {get; private set; }
public Tables() { }
public async Task LoadAsync(System.Func<string, Task<ByteBuf>> loader)
{ {
var tables = new System.Collections.Generic.Dictionary<string, object>(); var tables = new System.Collections.Generic.Dictionary<string, object>();
TbItem = new item.TbItem(loader("item_tbitem")); TbItem = new item.TbItem(await loader("item_tbitem"));
tables.Add("item.TbItem", TbItem); tables.Add("item.TbItem", TbItem);
TbSkill = new Battle.TbSkill(loader("battle_tbskill")); TbSkill = new Battle.TbSkill(await loader("battle_tbskill"));
tables.Add("Battle.TbSkill", TbSkill); tables.Add("Battle.TbSkill", TbSkill);
TbBuff = new Battle.TbBuff(loader("battle_tbbuff")); TbBuff = new Battle.TbBuff(await loader("battle_tbbuff"));
tables.Add("Battle.TbBuff", TbBuff); tables.Add("Battle.TbBuff", TbBuff);
TbBuffAttr = new Battle.TbBuffAttr(loader("battle_tbbuffattr")); TbBuffAttr = new Battle.TbBuffAttr(await loader("battle_tbbuffattr"));
tables.Add("Battle.TbBuffAttr", TbBuffAttr); tables.Add("Battle.TbBuffAttr", TbBuffAttr);
PostInit();
TbItem.Resolve(tables); TbItem.Resolve(tables);
TbSkill.Resolve(tables); TbSkill.Resolve(tables);
TbBuff.Resolve(tables); TbBuff.Resolve(tables);
TbBuffAttr.Resolve(tables); TbBuffAttr.Resolve(tables);
PostResolve();
} }
public void TranslateText(System.Func<string, string, string> translator) public void TranslateText(System.Func<string, string, string> translator)
@@ -45,9 +46,6 @@ public partial class Tables
TbBuff.TranslateText(translator); TbBuff.TranslateText(translator);
TbBuffAttr.TranslateText(translator); TbBuffAttr.TranslateText(translator);
} }
partial void PostInit();
partial void PostResolve();
} }
} }

View File

@@ -3,6 +3,7 @@ using Bright.Serialization;
using GameConfig; using GameConfig;
using TEngine; using TEngine;
using TEngine.Core; using TEngine.Core;
using TEngine.Helper;
/// <summary> /// <summary>
/// 配置加载器。 /// 配置加载器。
@@ -13,19 +14,13 @@ public class ConfigLoader:Singleton<ConfigLoader>
private Tables _tables = null!; private Tables _tables = null!;
public ConfigLoader()
{
this.Load();
}
public Tables Tables public Tables Tables
{ {
get get
{ {
if (!_init) if (!_init)
{ {
_init = true; Log.Error("Config not loaded.");
Load();
} }
return _tables; return _tables;
} }
@@ -34,15 +29,13 @@ public class ConfigLoader:Singleton<ConfigLoader>
/// <summary> /// <summary>
/// 加载配置。 /// 加载配置。
/// </summary> /// </summary>
public void Load() public async Task LoadAsync()
{ {
try try
{ {
var tablesCtor = typeof(Tables).GetConstructors()[0]; _tables = new Tables();
var loaderReturnType = tablesCtor.GetParameters()[0].ParameterType.GetGenericArguments()[1]; await _tables.LoadAsync(LoadByteBuf);
_init = true;
System.Delegate loader = new System.Func<string, ByteBuf>(LoadByteBuf);
_tables = (Tables)tablesCtor.Invoke(new object[] { loader });
} }
catch (Exception e) catch (Exception e)
{ {
@@ -56,9 +49,15 @@ public class ConfigLoader:Singleton<ConfigLoader>
/// </summary> /// </summary>
/// <param name="file">FileName</param> /// <param name="file">FileName</param>
/// <returns>ByteBuf</returns> /// <returns>ByteBuf</returns>
private ByteBuf LoadByteBuf(string file) private async Task<ByteBuf> LoadByteBuf(string file)
{ {
byte[]ret = File.ReadAllBytes($"../../../Config/GameConfig/{file}.bytes"); #if false
GameTickWatcher gameTickWatcher = new GameTickWatcher();
#endif
var ret = await File.ReadAllBytesAsync($"../../../Config/GameConfig/{file}.bytes");
#if false
Log.Warning($"LoadByteBuf {file} used time {gameTickWatcher.ElapseTime()}");
#endif
return new ByteBuf(ret); return new ByteBuf(ret);
} }
} }

View File

@@ -6,36 +6,37 @@
// </auto-generated> // </auto-generated>
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
using Bright.Serialization; using Bright.Serialization;
using System.Threading.Tasks;
namespace GameConfig namespace GameConfig
{ {
public partial class Tables
{
public item.TbItem TbItem {get; }
public Battle.TbSkill TbSkill {get; }
public Battle.TbBuff TbBuff {get; }
public Battle.TbBuffAttr TbBuffAttr {get; }
public Tables(System.Func<string, ByteBuf> loader) public sealed class Tables
{
public item.TbItem TbItem {get; private set; }
public Battle.TbSkill TbSkill {get; private set; }
public Battle.TbBuff TbBuff {get; private set; }
public Battle.TbBuffAttr TbBuffAttr {get; private set; }
public Tables() { }
public async Task LoadAsync(System.Func<string, Task<ByteBuf>> loader)
{ {
var tables = new System.Collections.Generic.Dictionary<string, object>(); var tables = new System.Collections.Generic.Dictionary<string, object>();
TbItem = new item.TbItem(loader("item_tbitem")); TbItem = new item.TbItem(await loader("item_tbitem"));
tables.Add("item.TbItem", TbItem); tables.Add("item.TbItem", TbItem);
TbSkill = new Battle.TbSkill(loader("battle_tbskill")); TbSkill = new Battle.TbSkill(await loader("battle_tbskill"));
tables.Add("Battle.TbSkill", TbSkill); tables.Add("Battle.TbSkill", TbSkill);
TbBuff = new Battle.TbBuff(loader("battle_tbbuff")); TbBuff = new Battle.TbBuff(await loader("battle_tbbuff"));
tables.Add("Battle.TbBuff", TbBuff); tables.Add("Battle.TbBuff", TbBuff);
TbBuffAttr = new Battle.TbBuffAttr(loader("battle_tbbuffattr")); TbBuffAttr = new Battle.TbBuffAttr(await loader("battle_tbbuffattr"));
tables.Add("Battle.TbBuffAttr", TbBuffAttr); tables.Add("Battle.TbBuffAttr", TbBuffAttr);
PostInit();
TbItem.Resolve(tables); TbItem.Resolve(tables);
TbSkill.Resolve(tables); TbSkill.Resolve(tables);
TbBuff.Resolve(tables); TbBuff.Resolve(tables);
TbBuffAttr.Resolve(tables); TbBuffAttr.Resolve(tables);
PostResolve();
} }
public void TranslateText(System.Func<string, string, string> translator) public void TranslateText(System.Func<string, string, string> translator)
@@ -45,9 +46,6 @@ public partial class Tables
TbBuff.TranslateText(translator); TbBuff.TranslateText(translator);
TbBuffAttr.TranslateText(translator); TbBuffAttr.TranslateText(translator);
} }
partial void PostInit();
partial void PostResolve();
} }
} }

View File

@@ -9,6 +9,8 @@ public static class ConfigTableSystem
{ {
public static void Bind() public static void Bind()
{ {
LoadConfigAsync();
// 框架需要一些的配置文件来启动服务器和创建网络服务所以需要ServerConfig.xlsx和MachineConfig.xlsx的配置 // 框架需要一些的配置文件来启动服务器和创建网络服务所以需要ServerConfig.xlsx和MachineConfig.xlsx的配置
// 由于配置表的代码是生成在框架外面的、框架没办法直接获取到配置文件 // 由于配置表的代码是生成在框架外面的、框架没办法直接获取到配置文件
// 考虑到这两个配置文件开发者可能会修改结构、所以提供了一个委托来让开发者开自己定义如何获取框架需要的东西 // 考虑到这两个配置文件开发者可能会修改结构、所以提供了一个委托来让开发者开自己定义如何获取框架需要的东西
@@ -139,5 +141,10 @@ public static class ConfigTableSystem
return list; return list;
}; };
} }
public static async Task LoadConfigAsync()
{
await ConfigLoader.Instance.LoadAsync();
}
} }
#endif #endif

View File

@@ -1,14 +1,15 @@
using Bright.Serialization; using Bright.Serialization;
using System.Threading.Tasks;
{{ {{
name = x.name name = x.name
namespace = x.namespace namespace = x.namespace
tables = x.tables tables = x.tables
}} }}
namespace {{namespace}}
{
{{cs_start_name_space_grace x.namespace}} public sealed class {{name}}
public partial class {{name}}
{ {
{{~for table in tables ~}} {{~for table in tables ~}}
{{~if table.comment != '' ~}} {{~if table.comment != '' ~}}
@@ -16,22 +17,22 @@ public partial class {{name}}
/// {{table.escape_comment}} /// {{table.escape_comment}}
/// </summary> /// </summary>
{{~end~}} {{~end~}}
public {{table.full_name}} {{table.name}} {get; } public {{table.full_name}} {{table.name}} {get; private set; }
{{~end~}} {{~end~}}
public {{name}}(System.Func<string, ByteBuf> loader) public {{name}}() { }
public async Task LoadAsync(System.Func<string, Task<ByteBuf>> loader)
{ {
var tables = new System.Collections.Generic.Dictionary<string, object>(); var tables = new System.Collections.Generic.Dictionary<string, object>();
{{~for table in tables ~}} {{~for table in tables ~}}
{{table.name}} = new {{table.full_name}}(loader("{{table.output_data_file}}")); {{table.name}} = new {{table.full_name}}(await loader("{{table.output_data_file}}"));
tables.Add("{{table.full_name}}", {{table.name}}); tables.Add("{{table.full_name}}", {{table.name}});
{{~end~}} {{~end~}}
PostInit();
{{~for table in tables ~}} {{~for table in tables ~}}
{{table.name}}.Resolve(tables); {{table.name}}.Resolve(tables);
{{~end~}} {{~end~}}
PostResolve();
} }
public void TranslateText(System.Func<string, string, string> translator) public void TranslateText(System.Func<string, string, string> translator)
@@ -40,9 +41,6 @@ public partial class {{name}}
{{table.name}}.TranslateText(translator); {{table.name}}.TranslateText(translator);
{{~end~}} {{~end~}}
} }
partial void PostInit();
partial void PostResolve();
} }
{{cs_end_name_space_grace x.namespace}} }