export-table-pulgin-csharp 1.1.96 → 1.1.98

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,242 @@
1
+
2
+ import { cmm, HandleSheetParams, Field, foreach, IPlugin, st, PluginBase, HandleBatchParams, iff, FieldType, makeFirstLetterLower, DataTable, OutFilePath } from "export-table-lib"
3
+ import { convMemberName, convTupleArrayType, convVarName, firstLetterUpper, genValue, getDescripts, getFieldType, getFkFieldType, getTitle, isSkipExportDefaults0 } from "./CSParseTool"
4
+ import * as fs from "fs-extra"
5
+
6
+ export function export_stuff(paras: HandleSheetParams): string | null {
7
+ let {
8
+ datas,
9
+ fields,
10
+ inject,
11
+ name,
12
+ objects,
13
+ packagename,
14
+ tables,
15
+ xxtea,
16
+ exportNamespace,
17
+ moreOptions,
18
+ } = paras;
19
+
20
+ let isSkipExportDefaults = !!moreOptions?.SkipDefaults ?? false
21
+ if (isSkipExportDefaults0) {
22
+ isSkipExportDefaults = true
23
+ }
24
+
25
+ let RowClass = firstLetterUpper(name)
26
+ let initFunc = name + "Init"
27
+ let mapfield = fields.find(a => a.type == "key")//如果是map,则生成对应的map
28
+ let mapName = name + "Map"
29
+
30
+ let temp = `
31
+ using System.Collections.Generic;
32
+ using System.Linq;
33
+
34
+ namespace ${exportNamespace}{
35
+ [System.Serializable]
36
+ public partial class ${RowClass} {
37
+
38
+ private static List<${RowClass}> _configs;
39
+
40
+ public static List<${RowClass}> Configs
41
+ {
42
+ get
43
+ {
44
+ if (_configs == null)
45
+ {
46
+ _configs = Collection.FindAll().ToList();
47
+ }
48
+
49
+ return _configs;
50
+ }
51
+ }
52
+
53
+ public ${RowClass}() { }
54
+ public ${RowClass}(${st(() => fields.map(f => `${getFieldType(f)} ${convVarName(f.name)}`).join(", "))})
55
+ {
56
+ ${foreach(fields, f =>
57
+ ` this.${convMemberName(f.name)} = ${convVarName(f.name)};`
58
+ )}
59
+ }
60
+
61
+ public virtual ${RowClass} MergeFrom(${RowClass} source)
62
+ {
63
+ ${foreach(fields, f =>
64
+ ` this.${convMemberName(f.name)} = source.${convMemberName(f.name)};`
65
+ )}
66
+ return this;
67
+ }
68
+
69
+ public virtual ${RowClass} Clone()
70
+ {
71
+ var config = new ${RowClass}();
72
+ config.MergeFrom(this);
73
+ return config;
74
+ }
75
+
76
+ ${cmm(/**生成字段 */)}
77
+ ${foreach(fields, f => `
78
+ /// <summary>
79
+ ${foreach(getDescripts(f), line =>
80
+ ` /// ${line}`
81
+ )}
82
+ /// </summary>
83
+ public ${getFieldType(f)} ${convMemberName(f.name)};
84
+
85
+ ${iff(f.rawType.startsWith("@"), () => `
86
+ /// <summary>
87
+ ${foreach(getDescripts(f), line =>
88
+ ` /// ${line}`
89
+ )}
90
+ /// </summary>
91
+ ${convTupleArrayType(f)}`)}`
92
+ )}
93
+
94
+ ${cmm(/**生成get字段 */)}
95
+ #region get字段
96
+ ${foreach(fields, f => {
97
+ if (f.nameOrigin != f.name) {
98
+ return ` public ${getFieldType(f)} ${getTitle(f).replace(" ", "_")} => ${convMemberName(f.name)};`
99
+ } else {
100
+ return ""
101
+ }
102
+ }
103
+ )}
104
+ #endregion
105
+
106
+ #region uid map
107
+ ${foreach(fields, f => {
108
+ if (f.isUnique) {
109
+ let memberName = convMemberName(f.name);
110
+ let paraName = convVarName(memberName);
111
+ let tempDictByMemberName = `tempDictBy${memberName}`;
112
+ let memberType = getFieldType(f);
113
+ return `
114
+ protected static Dictionary<${memberType}, ${RowClass}> ${tempDictByMemberName};
115
+ public static ${RowClass} GetConfigBy${memberName}(${memberType} ${paraName})
116
+ {
117
+ if (${tempDictByMemberName} == null)
118
+ {
119
+ ${tempDictByMemberName} = new Dictionary<int, ${memberType}>(Configs.Count);
120
+ }
121
+
122
+ if (${tempDictByMemberName}.TryGetValue(${paraName}, out var result))
123
+ {
124
+ return result;
125
+ }
126
+
127
+ result = Collection.FindOne(record => record.${memberName} == ${paraName});
128
+ ${tempDictByMemberName}.Add(${paraName}, result);
129
+ return result;
130
+ }
131
+ `
132
+ } else if (f.type == "number" || f.type == "float" || f.type == "int" || f.type == "long" || f.type == "string") {
133
+ let memberName = convMemberName(f.name);
134
+ let paraName = convVarName(memberName);
135
+ let tempRecordsDictByMemberName = `tempRecordsDictBy${memberName}`;
136
+ let memberType = getFieldType(f);
137
+ return `
138
+ protected static Dictionary<${memberType}, ${RowClass}[]> ${tempRecordsDictByMemberName};
139
+ public static ${RowClass}[] GetConfigsBy${memberName}(${memberType} ${paraName})
140
+ {
141
+ if (${tempRecordsDictByMemberName} != null && ${tempRecordsDictByMemberName}.TryGetValue(${paraName},out var retValue))
142
+ {
143
+ return retValue;
144
+ }
145
+ else
146
+ {
147
+ if (${tempRecordsDictByMemberName} == null)
148
+ {
149
+ ${tempRecordsDictByMemberName} = new Dictionary<${memberType}, ${RowClass}[]>(Configs.Count);
150
+ }
151
+ var records = Collection.Find(c => c.${memberName} == ${paraName}).ToArray();
152
+ ${tempRecordsDictByMemberName}.Add(${paraName}, records);
153
+ return records;
154
+ }
155
+ }
156
+ `
157
+ } else {
158
+ return ""
159
+ }
160
+ }
161
+ )}
162
+
163
+ #endregion uid map
164
+
165
+ #region 生成fk.get/set
166
+ ${foreach(fields, f => `
167
+ ${iff(f.type == "fk", () => `
168
+ ${iff(getFkFieldType(tables, f).toLowerCase() != "uid", () => `
169
+ protected ${convMemberName(f.fkTableName!)}[] _fk${convMemberName(f.name)}=null;
170
+ /**
171
+ * ${f.describe}
172
+ **/
173
+ public virtual ${convMemberName(f.fkTableName!)}[] ${convMemberName(f.name)}DataList{
174
+ get{
175
+ if(this._fk${convMemberName(f.name)}==null){
176
+ if(null==this.${convMemberName(f.name)}){
177
+ this._fk${convMemberName(f.name)} = new ${convMemberName(f.fkTableName!)}[0];
178
+ }else{
179
+ this._fk${convMemberName(f.name)}=${convMemberName(f.fkTableName!)}.Configs.FindAll(a=>a.${convMemberName(f.fkFieldName!)}!=null && this.${convMemberName(f.name)}==a.${convMemberName(f.fkFieldName!)}).ToArray();
180
+ }
181
+ }
182
+ return this._fk${convMemberName(f.name)};
183
+ }
184
+ }
185
+ `).else(() => `
186
+ protected ${convMemberName(f.fkTableName!)} _fk${convMemberName(f.name)}=null;
187
+ /**
188
+ * ${f.describe}
189
+ **/
190
+ public virtual ${convMemberName(f.fkTableName!)} ${convMemberName(f.name)}Data{
191
+ get{
192
+ if(this._fk${convMemberName(f.name)}==null){
193
+ this._fk${convMemberName(f.name)}=${convMemberName(f.fkTableName!)}.Configs.Find(a=>a.${convMemberName(f.fkFieldName!)}==this.${convMemberName(f.name)});
194
+ }
195
+ return this._fk${convMemberName(f.name)};
196
+ }
197
+ }
198
+ `)}
199
+ `)}
200
+ ${iff(f.type == "fk[]", () => `
201
+ protected ${convMemberName(f.fkTableName!)}[] _fk${convMemberName(f.name)}=null;
202
+ /**
203
+ * ${f.describe}
204
+ **/
205
+ public virtual ${convMemberName(f.fkTableName!)}[] ${convMemberName(f.name)}DataList{
206
+ get{
207
+ if(this._fk${convMemberName(f.name)}==null){
208
+ if(null==this.${convMemberName(f.name)}){
209
+ this._fk${convMemberName(f.name)} = new ${convMemberName(f.fkTableName!)}[0];
210
+ }else{
211
+ this._fk${convMemberName(f.name)}=${exportNamespace}.${convMemberName(f.fkTableName!)}.Configs.FindAll(a=>a.${convMemberName(f.fkFieldName!)}!=null && this.${convMemberName(f.name)}!.Contains(a.${convMemberName(f.fkFieldName!)})).ToArray();
212
+ }
213
+ }
214
+ return this._fk${convMemberName(f.name)};
215
+ }
216
+ }
217
+ `)}
218
+ `)}
219
+ #endregion 生成fk.get/set
220
+ }
221
+ }
222
+ `
223
+
224
+ return temp
225
+
226
+ }
227
+
228
+ export class ExportLiteDBCSPlugin extends PluginBase {
229
+ name = "csharp"
230
+ tags: string[] = ["litedbcs"]
231
+
232
+ handleSheet(paras: HandleSheetParams) {
233
+ let content = export_stuff(paras)
234
+ if (content != null) {
235
+ var fullName = `${paras.table.workbookName}-${paras.name}`
236
+ let savePath = new OutFilePath(paras.outPath, fullName, ".cs").fullPath
237
+ console.log("LFJKFELK:" + savePath)
238
+ fs.outputFileSync(savePath, content, "utf-8")
239
+ }
240
+ return content
241
+ }
242
+ }
@@ -0,0 +1,212 @@
1
+
2
+ import { cmm, HandleSheetParams, Field, foreach, IPlugin, st, PluginBase, HandleBatchParams, OutFilePath, makeFirstLetterUpper } from "export-table-lib"
3
+ import * as fs from "fs-extra"
4
+ import { TryConvValue, convMemberName } from "./CSParseTool";
5
+
6
+ var isSkipIndexLoader0 = process.argv.findIndex(v => v == "--SkipIndexLoader") >= 0
7
+
8
+ let firstLetterUpper = makeFirstLetterUpper;
9
+ export function exportUJson(paras: HandleSheetParams): string | null {
10
+ let {
11
+ datas,
12
+ fields,
13
+ name,
14
+ objects,
15
+ table,
16
+ } = paras;
17
+
18
+ var fullName = `${table.workbookName}-${name}`
19
+ let jsonString = JSON.stringify(objects.map(obj => {
20
+ var newObj = Object.create(null);
21
+ for (let f of fields) {
22
+ let key = f.name
23
+ var newKey = convMemberName(key);
24
+ newObj[newKey] = obj[key];
25
+
26
+ var line = f.rawType.replaceAll(/(?<=[^\w])(boolean)(?=[^\w]|$)/g, "bool");
27
+ let m = line.match(/\@\((\w+),(\w+)\)(\[\])?/)
28
+ if (m != null) {
29
+ // [{"Item1":99,"Item2":"klwjefl"}]
30
+ let content = obj[key] as string;
31
+ let index = 0;
32
+ let index2 = -1;
33
+ let objs: object[] = []
34
+ while (0 <= index && index < content.length) {
35
+ index2 = content.indexOf("|", index)
36
+ let numStr = content.substring(index, index2)
37
+ let t1 = m[1]
38
+ let v1 = TryConvValue(numStr, t1 as any, f);
39
+ index = content.indexOf(";;", index2)
40
+ let posEnd = index
41
+ if (index == -1) {
42
+ posEnd = content.length
43
+ } else {
44
+ index += 2
45
+ }
46
+ let ssStr = content.substring(index2 + 1, posEnd)
47
+ let t2 = m[2]
48
+ let v2 = TryConvValue(ssStr, t2 as any, f);
49
+ console.log(`parseinfo1: ${content}, ${index2}, ${index}, ${numStr}, ${t1}, ${v1}`)
50
+ console.log(`parseinfo2: ${content}, ${index2}, ${index}, ${ssStr}, ${t2}, ${v2}`)
51
+ objs.push({
52
+ Item1: v1,
53
+ Item2: v2,
54
+ })
55
+ }
56
+
57
+ let isArray = m[3] == "[]"
58
+ if (isArray) {
59
+ newObj[newKey + "Obj"] = objs
60
+ } else {
61
+ if (objs.length > 1) {
62
+ console.log(`配置错误,过多的条目数量: ${content}`)
63
+ }
64
+ newObj[newKey + "Obj"] = objs[0] ?? {}
65
+ }
66
+ }
67
+ }
68
+ // Object.keys(obj).forEach(key => {
69
+ // var newKey = convMemberName(key);
70
+ // newObj[newKey] = obj[key];
71
+ // })
72
+ return newObj
73
+ }));
74
+
75
+ return jsonString;
76
+
77
+ // // !!!必须开头没有空格
78
+ // let temp = `%YAML 1.1
79
+ // %TAG !u! tag:unity3d.com,2011:
80
+ // --- !u!114 &11400000
81
+ // MonoBehaviour:
82
+ // m_ObjectHideFlags: 0
83
+ // m_CorrespondingSourceObject: {fileID: 0}
84
+ // m_PrefabInstance: {fileID: 0}
85
+ // m_PrefabAsset: {fileID: 0}
86
+ // m_GameObject: {fileID: 0}
87
+ // m_Enabled: 1
88
+ // m_EditorHideFlags: 0
89
+ // m_Script: {fileID: 11500000, guid: 496f60086c072a8479a6e0b948efb5e8, type: 3}
90
+ // m_Name: ${fullName}
91
+ // m_EditorClassIdentifier:
92
+ // JsonText: ${JSON.stringify(jsonString)}
93
+ // `
94
+ // return temp
95
+
96
+ }
97
+
98
+ export function exportUJsonLoader(paras: HandleSheetParams): string | null {
99
+ let {
100
+ datas,
101
+ fields,
102
+ name,
103
+ objects,
104
+ table,
105
+ exportNamespace,
106
+ } = paras;
107
+
108
+ let jsonToolNamespaceIndex = process.argv.findIndex(v => v == "--JsonToolNamespace")
109
+ let jsonToolNamespace = "lang.json";
110
+ if (jsonToolNamespaceIndex >= 0 && process.argv.length > jsonToolNamespaceIndex + 1) {
111
+ jsonToolNamespace = process.argv[jsonToolNamespaceIndex + 1]
112
+ }
113
+
114
+ let RowClass = firstLetterUpper(name)
115
+ var fullName = `${table.workbookName}-${name}`
116
+ // !!!必须开头没有空格
117
+ let temp = `
118
+ using UnityEngine.AddressableAssets;
119
+ using System.Threading.Tasks;
120
+ using LiteDB;
121
+ using UnityEngine;
122
+ using ${jsonToolNamespace};
123
+
124
+ namespace ${exportNamespace}
125
+ {
126
+ public partial class ${RowClass}
127
+ {
128
+ protected static LiteDatabase Database;
129
+ protected static ILiteCollection<${RowClass}> Collection;
130
+ public static Task Load()
131
+ {
132
+ #if UNITY_EDITOR && ENABLE_CONFIG_LOG
133
+ Debug.Log("ReferConfig-${RowClass}");
134
+ #endif
135
+
136
+ var ldb = SharedLiteDB.Database;
137
+ Database = ldb;
138
+ const string key = "${fullName.replace("-", "_")}";
139
+ if (!ldb.CollectionExists(key))
140
+ {
141
+ Debug.LogError($"配表资源缺失: {key}");
142
+ }
143
+ Collection = ldb.GetCollection<${RowClass}>(key);
144
+
145
+ return Task.CompletedTask;
146
+ }
147
+ }
148
+ }
149
+ `
150
+ return temp
151
+
152
+ }
153
+
154
+ export class ExportLiteDBUJsonPlugin extends PluginBase {
155
+ name = "ujson"
156
+ tags: string[] = ["litedbujson"]
157
+
158
+ handleSheet(paras: HandleSheetParams) {
159
+ var fullName = `${paras.table.workbookName}-${paras.name}`
160
+ {
161
+ let content1 = exportUJsonLoader(paras)
162
+ if (content1 != null) {
163
+ let savePath = new OutFilePath(paras.outPath, fullName, "Loader.cs").fullPath
164
+ fs.outputFileSync(savePath, content1, "utf-8")
165
+ }
166
+ }
167
+ {
168
+ let content2 = exportUJson(paras)
169
+ if (content2 != null) {
170
+ let savePath = new OutFilePath(paras.outPath, fullName, ".json").fullPath
171
+ fs.outputFileSync(savePath, content2, "utf-8")
172
+ }
173
+ return content2
174
+ }
175
+ }
176
+
177
+ handleBatch(paras: HandleBatchParams): void {
178
+
179
+ let {
180
+ moreOptions,
181
+ tables,
182
+ exportNamespace,
183
+ } = paras
184
+ let isSkipIndexLoader = !!moreOptions?.SkipIndexLoader ?? false
185
+ if (isSkipIndexLoader0) {
186
+ isSkipIndexLoader = true
187
+ }
188
+ if (isSkipIndexLoader) {
189
+ return;
190
+ }
191
+
192
+ var temp = `
193
+ using System;
194
+ using System.Collections.Generic;
195
+ using System.Threading.Tasks;
196
+
197
+ namespace ${exportNamespace}
198
+ {
199
+ public static class DefaultConfigLoader{
200
+ public static IEnumerable<Func<Task>> Load(){
201
+ ${foreach(tables.sort((ta, tb) => ta.name.localeCompare(tb.name)), (table) => `
202
+ yield return ${firstLetterUpper(table.name)}.Load;
203
+ `)}
204
+ yield break;
205
+ }
206
+ }
207
+ }
208
+ `
209
+ let savePath = paras.outPath + "/DefaultConfigLoader.cs";
210
+ fs.outputFileSync(savePath, temp, "utf-8");
211
+ }
212
+ }
@@ -1,7 +1,7 @@
1
1
 
2
2
  import { cmm, HandleSheetParams, Field, foreach, IPlugin, st, PluginBase, HandleBatchParams, OutFilePath, makeFirstLetterUpper } from "export-table-lib"
3
3
  import * as fs from "fs-extra"
4
- import { TryConvValue } from "./ExportCSPlugin";
4
+ import { TryConvValue, convMemberName } from "./CSParseTool";
5
5
 
6
6
  var isSkipIndexLoader0 = process.argv.findIndex(v => v == "--SkipIndexLoader") >= 0
7
7
 
@@ -15,13 +15,6 @@ export function exportUJson(paras: HandleSheetParams): string | null {
15
15
  table,
16
16
  } = paras;
17
17
 
18
- let firstLetterLower = function (str: string) {
19
- return str.charAt(0).toLowerCase() + str.slice(1);
20
- };
21
- let convMemberName = function (str: string) {
22
- return str.split("_").map(s => firstLetterUpper(s)).join("")
23
- }
24
-
25
18
  var fullName = `${table.workbookName}-${name}`
26
19
  let jsonString = JSON.stringify(objects.map(obj => {
27
20
  var newObj = Object.create(null);
@@ -148,6 +141,7 @@ namespace ${exportNamespace}
148
141
  #endif
149
142
  if (configJson != null)
150
143
  {
144
+ Debug.Log($"解析配表: {loadUrl}");
151
145
  ${RowClass}[] jsonObjs;
152
146
  try
153
147
  {
package/src/index.ts CHANGED
@@ -1,8 +1,12 @@
1
1
 
2
2
  import { ExportPlugin as ExportCSPlugin } from "./ExportCSPlugin"
3
+ import { ExportLiteDBCSPlugin } from "./ExportLiteDBCSPlugin"
4
+ import { ExportLiteDBUJsonPlugin } from "./ExportLiteDBUnityCSJsonPlugin"
3
5
  import { ExportUJsonPlugin } from "./ExportUnityCSJsonPlugin"
4
6
 
5
7
  export const ExportPlugins = [
6
8
  new ExportCSPlugin(),
7
9
  new ExportUJsonPlugin(),
10
+ new ExportLiteDBCSPlugin(),
11
+ new ExportLiteDBUJsonPlugin(),
8
12
  ]
@@ -0,0 +1,2 @@
1
+ node E:\DATA\Projects\netease\quicktable\export-table\dist\launch.js export src dist --tags csharp:litedbcs --libs E:\DATA\Projects\netease\quicktable %*
2
+ node E:\DATA\Projects\netease\quicktable\export-table\dist\launch.js export src dist --tags csharp:litedbujson --libs E:\DATA\Projects\netease\quicktable %*