【特性】新增gen_types类型 data_template 以及新增配套参数--template_name,用于模板生成自定义格式的配置数据

main
walon 2021-08-27 19:23:28 +08:00
parent 2fde2a481e
commit 434d52ed8d
5 changed files with 54 additions and 7 deletions

View File

@ -21,9 +21,12 @@ namespace Luban.Job.Cfg
[Option("output_data_json_monolithic_file", Required = false, HelpText = "output monolithic json file")]
public string OutputDataJsonMonolithicFile { get; set; }
[Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_json,code_cs_unity_json,code_lua_bin,code_java_bin,code_java_json,code_go_bin,code_go_json,code_cpp_bin,code_python3_json,code_typescript_bin,code_typescript_json,data_bin,data_lua,data_json,data_json2,data_json_monolithic,data_resources . can be multi")]
[Option("gen_types", Required = true, HelpText = "code_cs_bin,code_cs_json,code_cs_unity_json,code_lua_bin,code_java_bin,code_java_json,code_go_bin,code_go_json,code_cpp_bin,code_python3_json,code_typescript_bin,code_typescript_json,data_bin,data_lua,data_json,data_json2,data_json_monolithic,data_resources,data_template . can be multi")]
public string GenType { get; set; }
[Option("template_name", Required = false, HelpText = "template name. use with gen_types=data_template")]
public string TemplateName { get; set; }
[Option('s', "service", Required = true, HelpText = "service")]
public string Service { get; set; }

View File

@ -0,0 +1,38 @@
using Luban.Common.Protos;
using Luban.Job.Cfg.Cache;
using Luban.Job.Cfg.Utils;
using Luban.Job.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Luban.Job.Cfg.Generate
{
[Render("data_template")]
class TemplateDataScatterRender : DataRenderBase
{
public override void Render(GenContext ctx)
{
string genType = ctx.GenArgs.TemplateName;
foreach (var table in ctx.ExportTables)
{
ctx.Tasks.Add(Task.Run(() =>
{
var file = RenderFileUtil.GetOutputFileName(genType, table.OutputDataFile);
var records = ctx.Assembly.GetTableExportDataList(table);
if (!FileRecordCacheManager.Ins.TryGetRecordOutputData(table, records, genType, out string md5))
{
var content = DataExporterUtil.ToTemplateOutputData(table, records, genType);
md5 = CacheFileUtil.GenStringOrBytesMd5AndAddCache(file, content);
FileRecordCacheManager.Ins.AddCachedRecordOutputData(table, records, genType, md5);
}
ctx.GenDataFilesInOutputDataDir.Add(new FileInfo() { FilePath = file, MD5 = md5 });
}));
}
}
}
}

View File

@ -87,6 +87,11 @@ namespace Luban.Job.Cfg
errMsg = "--input_l10n_text_files must be provided with --output_l10n_not_translated_text_file";
return false;
}
if (genTypes.Contains("data_template") ^ !string.IsNullOrWhiteSpace(options.TemplateName))
{
errMsg = "gen_types data_template should use with --template_name";
return false;
}
}
if (string.IsNullOrWhiteSpace(options.BranchName) ^ string.IsNullOrWhiteSpace(options.BranchInputDataDir))

View File

@ -19,13 +19,14 @@ namespace Luban.Job.Cfg.Utils
{
public static class DataExporterUtil
{
public static object ToOutputData(DefTable table, List<Record> records, string dataType)
{
if (StringTemplateUtil.TryGetTemplate($"config/data/{dataType[5..]}", out Template template))
public static string ToTemplateOutputData(DefTable table, List<Record> records, string templateName)
{
Template template = StringTemplateUtil.GetTemplate($"config/data/{templateName}");
return template.RenderData(table, records.Select(r => r.Data).ToList());
}
public static object ToOutputData(DefTable table, List<Record> records, string dataType)
{
switch (dataType)
{
case "data_bin":

View File

@ -137,7 +137,7 @@ namespace Luban.Job.Common.Utils
public static string GetOutputFileName(string genType, string fileName)
{
return $"{(genType.EndsWith("lua") ? fileName.Replace('.', '_') : fileName)}.{GetOutputFileSuffix(genType)}";
return $"{(genType.Contains("lua") ? fileName.Replace('.', '_') : fileName)}.{GetOutputFileSuffix(genType)}";
}
}
}