【特性】支持数据模板。允许自定义生成数据(自定义生成的数据不能被标准的生成代码识别,因此只能用于自定义加载)

main
walon 2021-08-27 16:35:22 +08:00
parent 45279810ec
commit 7ea907cc5b
8 changed files with 130 additions and 37 deletions

View File

@ -591,6 +591,22 @@ luban同时支持两种级别的分组
![tag](docs/images/examples/tag_01.png)
## excel 列 默认值
该特性只对excel格式文件有效。当单元格为空时该字段使用默认值。
```xml
<bean name="DemoDefault">
<var name="id" type="int"/>
<var name="x" type="int" default="10">
</bean>
<table name="TbDemoDefault" value="DemoDefault" input="default.xlsx"/>
```
![default](docs/images/adv/def_50.png)
### 常量别名
游戏里经常会出现一些常用的类似枚举的值,比如说 升级丹的 id,在很多地方都要填,如果直接它的道具 id,既不直观,也容易出错。 Luban 支持常量替换。对于需要常量替换的字段,添加 convert=”枚举类”。 如果填写的值是 枚举名或者别名,则替换为 相应的整数。否则 按照整数解析。

View File

@ -89,6 +89,46 @@ text的key和text字段都是string类型因此在连续单元格或者sep产
![数据](images/examples/ex_i10n_2.png)
## datetime 类型
- 时间是常用的数据类型。Luban 特地提供了支持。
有两种形式,一种以纯字符串的方式填写。
- 以纯字符串方式填写
填写格式为 以下 4 种。
- yyyy-mm-dd hh:mm:ss 如 1999-08-08 01:30:29
- yyyy-mm-dd hh:mm 如 2000-08-07 07:40
- yyyy-mm-dd hh 如 2001-09-05 07
- yyyy-mm-dd 如 2003-04-05
- 以 excel内置的时间格式填写
- 为 Item 新增一个 失效时间字段 expire_time 。
- [定义](images/adv/def_38.png)
``` xml
<bean name="Item">
...
<var name="expire_time" type="datetime"/>
</bean>
```
- 配置
图中红框中第一个时间以 excel的时间格式填写
剩下第234个时间以 纯字符串形式填写
![如图](images/adv/def_39.png)
## 可空变量
- 有时候会有一种变量,我们希望它 功能生效时填一个有效值,功能不生效里,用一个值来表示。 例如 int 类型,常常拿 0 或者-1 作无效值常量。 但有时候0 或-1 也是有效值时,这种做法就不生效了。或者说 项目组内 有时候拿 0有时候拿-1 作无效值标记,很不统一。我们借鉴 sql 及 c#,引入 可空值概念,用 null 表达空值。
- 我们为 Item 添加 min_use_level 字段,类型为 int? 当填有效值时,使用时要检查等级,否则不检查。
- [定义](images/adv/def_36.png)
``` xml
<bean name="Item">
...
<var name="min_use_level" type="int?"/>
</bean>
```
- 配置
![如图](images/adv/def_37.png)
## 列表类型 list,int
- 我们新增一个字段, 宝箱的随机抽取道具列表 random_item_ids。
@ -375,45 +415,22 @@ text的key和text字段都是string类型因此在连续单元格或者sep产
- 定义不变,但 excel 的填法有区别,数据如下:
- ![如图](images/adv/def_35.png)
## 可空变量
- 有时候会有一种变量,我们希望它 功能生效时填一个有效值,功能不生效里,用一个值来表示。 例如 int 类型,常常拿 0 或者-1 作无效值常量。 但有时候0 或-1 也是有效值时,这种做法就不生效了。或者说 项目组内 有时候拿 0有时候拿-1 作无效值标记,很不统一。我们借鉴 sql 及 c#,引入 可空值概念,用 null 表达空值。
- 我们为 Item 添加 min_use_level 字段,类型为 int? 当填有效值时,使用时要检查等级,否则不检查。
- [定义](images/adv/def_36.png)
``` xml
<bean name="Item">
...
<var name="min_use_level" type="int?"/>
</bean>
```
- 配置
![如图](images/adv/def_37.png)
## 默认值
## datetime 类型
该特性只对excel格式文件有效。当单元格为空时该字段使用默认值。
- 时间是常用的数据类型。Luban 特地提供了支持。
有两种形式,一种以纯字符串的方式填写。
- 以纯字符串方式填写
填写格式为 以下 4 种。
- yyyy-mm-dd hh:mm:ss 如 1999-08-08 01:30:29
- yyyy-mm-dd hh:mm 如 2000-08-07 07:40
- yyyy-mm-dd hh 如 2001-09-05 07
- yyyy-mm-dd 如 2003-04-05
- 以 excel内置的时间格式填写
```xml
<bean name="DemoDefault">
<var name="id" type="int"/>
<var name="x" type="int" default="10">
</bean>
- 为 Item 新增一个 失效时间字段 expire_time 。
- [定义](images/adv/def_38.png)
``` xml
<bean name="Item">
...
<var name="expire_time" type="datetime"/>
</bean>
```
- 配置
图中红框中第一个时间以 excel的时间格式填写
剩下第234个时间以 纯字符串形式填写
<table name="TbDemoDefault" value="DemoDefault" input="default.xlsx"/>
![如图](images/adv/def_39.png)
```
![default](images/adv/def_50.png)
## convert 常量替换

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

View File

@ -14,6 +14,9 @@ namespace Luban.Job.Cfg.Generate
[Render("data_json")]
[Render("data_json2")]
[Render("data_lua")]
[Render("data_xml")]
[Render("data_yaml")]
[Render("data_erlang")]
class DataScatterRender : DataRenderBase
{
public override void Render(GenContext ctx)

View File

@ -1,3 +1,4 @@
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Defs;
using Scriban;
using System.Collections.Generic;
@ -23,5 +24,24 @@ namespace Luban.Job.Cfg
ctx.PushGlobal(env);
return template.Render(ctx);
}
public static string RenderData(this Template template, DefTable table, List<DBean> exportDatas, Dictionary<string, object> extraModels = null)
{
var ctx = new TemplateContext();
var env = new TTypeTemplateExtends
{
["table"] = table,
["datas"] = exportDatas,
};
if (extraModels != null)
{
foreach ((var k, var v) in extraModels)
{
env[k] = v;
}
}
ctx.PushGlobal(env);
return template.Render(ctx);
}
}
}

View File

@ -5,6 +5,8 @@ using Luban.Job.Cfg.DataVisitors;
using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.l10n;
using Luban.Job.Cfg.RawDefs;
using Luban.Job.Common.Utils;
using Scriban;
using System;
using System.Collections.Generic;
using System.IO;
@ -19,6 +21,11 @@ namespace Luban.Job.Cfg.Utils
{
public static object ToOutputData(DefTable table, List<Record> records, string dataType)
{
if (StringTemplateUtil.TryGetTemplate($"config/data/{dataType[5..]}", out Template template))
{
return template.RenderData(table, records.Select(r => r.Data).ToList());
}
switch (dataType)
{
case "data_bin":

View File

@ -18,16 +18,27 @@ namespace Luban.Job.Common.Utils
TemplateSearchPaths.Add(path);
}
public static string GetTemplateString(string templateName)
public static bool TryGetTemplateString(string templateName, out string result)
{
foreach (var searchPath in TemplateSearchPaths)
{
var fullPath = $"{searchPath}/{templateName}.tpl";
if (File.Exists(fullPath))
{
return File.ReadAllText(fullPath, Encoding.UTF8);
result = File.ReadAllText(fullPath, Encoding.UTF8);
return true;
}
}
result = null;
return false;
}
public static string GetTemplateString(string templateName)
{
if (TryGetTemplateString(templateName, out var strTpl))
{
return strTpl;
}
throw new FileNotFoundException($"can't find {templateName}.tpl in paths:{string.Join(';', TemplateSearchPaths)}");
}
@ -38,6 +49,21 @@ namespace Luban.Job.Common.Utils
return s_templates.GetOrAdd(templateName, tn => Template.Parse(GetTemplateString(tn)));
}
public static bool TryGetTemplate(string templateName, out Template template)
{
if (s_templates.TryGetValue(templateName, out template))
{
return true;
}
if (TryGetTemplateString(templateName, out var strTpm))
{
template = s_templates.GetOrAdd(templateName, tn => Template.Parse(strTpm));
return true;
}
template = null;
return false;
}
public static Template GetOrAddTemplate(string templateName, Func<string, Template> creator)
{
return s_templates.GetOrAdd(templateName, creator);

View File

@ -2,6 +2,10 @@
"profiles": {
"Luban.Server": {
"commandName": "Project"
},
"TestTemplate": {
"commandName": "Project",
"commandLineArgs": "-t D:\\workspace\\luban_examples\\Projects\\CustomTemplates"
}
}
}