mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
修改luban模板配置,支持异步加载配置实例
修改luban模板配置,支持异步加载配置实例
This commit is contained in:
@@ -3,6 +3,7 @@ using Bright.Serialization;
|
||||
using GameConfig;
|
||||
using TEngine;
|
||||
using TEngine.Core;
|
||||
using TEngine.Helper;
|
||||
|
||||
/// <summary>
|
||||
/// 配置加载器。
|
||||
@@ -13,19 +14,13 @@ public class ConfigLoader:Singleton<ConfigLoader>
|
||||
|
||||
private Tables _tables = null!;
|
||||
|
||||
public ConfigLoader()
|
||||
{
|
||||
this.Load();
|
||||
}
|
||||
|
||||
public Tables Tables
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!_init)
|
||||
{
|
||||
_init = true;
|
||||
Load();
|
||||
Log.Error("Config not loaded.");
|
||||
}
|
||||
return _tables;
|
||||
}
|
||||
@@ -34,15 +29,13 @@ public class ConfigLoader:Singleton<ConfigLoader>
|
||||
/// <summary>
|
||||
/// 加载配置。
|
||||
/// </summary>
|
||||
public void Load()
|
||||
public async Task LoadAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var tablesCtor = typeof(Tables).GetConstructors()[0];
|
||||
var loaderReturnType = tablesCtor.GetParameters()[0].ParameterType.GetGenericArguments()[1];
|
||||
|
||||
System.Delegate loader = new System.Func<string, ByteBuf>(LoadByteBuf);
|
||||
_tables = (Tables)tablesCtor.Invoke(new object[] { loader });
|
||||
_tables = new Tables();
|
||||
await _tables.LoadAsync(LoadByteBuf);
|
||||
_init = true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -56,9 +49,15 @@ public class ConfigLoader:Singleton<ConfigLoader>
|
||||
/// </summary>
|
||||
/// <param name="file">FileName</param>
|
||||
/// <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);
|
||||
}
|
||||
}
|
@@ -6,36 +6,37 @@
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
using Bright.Serialization;
|
||||
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
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 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(System.Func<string, ByteBuf> loader)
|
||||
public Tables() { }
|
||||
|
||||
public async Task LoadAsync(System.Func<string, Task<ByteBuf>> loader)
|
||||
{
|
||||
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);
|
||||
TbSkill = new Battle.TbSkill(loader("battle_tbskill"));
|
||||
TbSkill = new Battle.TbSkill(await loader("battle_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);
|
||||
TbBuffAttr = new Battle.TbBuffAttr(loader("battle_tbbuffattr"));
|
||||
TbBuffAttr = new Battle.TbBuffAttr(await loader("battle_tbbuffattr"));
|
||||
tables.Add("Battle.TbBuffAttr", TbBuffAttr);
|
||||
|
||||
PostInit();
|
||||
TbItem.Resolve(tables);
|
||||
TbSkill.Resolve(tables);
|
||||
TbBuff.Resolve(tables);
|
||||
TbBuffAttr.Resolve(tables);
|
||||
PostResolve();
|
||||
}
|
||||
|
||||
public void TranslateText(System.Func<string, string, string> translator)
|
||||
@@ -45,9 +46,6 @@ public partial class Tables
|
||||
TbBuff.TranslateText(translator);
|
||||
TbBuffAttr.TranslateText(translator);
|
||||
}
|
||||
|
||||
partial void PostInit();
|
||||
partial void PostResolve();
|
||||
}
|
||||
|
||||
}
|
@@ -9,6 +9,8 @@ public static class ConfigTableSystem
|
||||
{
|
||||
public static void Bind()
|
||||
{
|
||||
LoadConfigAsync();
|
||||
|
||||
// 框架需要一些的配置文件来启动服务器和创建网络服务所以需要ServerConfig.xlsx和MachineConfig.xlsx的配置
|
||||
// 由于配置表的代码是生成在框架外面的、框架没办法直接获取到配置文件
|
||||
// 考虑到这两个配置文件开发者可能会修改结构、所以提供了一个委托来让开发者开自己定义如何获取框架需要的东西
|
||||
@@ -139,5 +141,10 @@ public static class ConfigTableSystem
|
||||
return list;
|
||||
};
|
||||
}
|
||||
|
||||
public static async Task LoadConfigAsync()
|
||||
{
|
||||
await ConfigLoader.Instance.LoadAsync();
|
||||
}
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user