forked from davyxu/tabtoy
/
text.go
115 lines (107 loc) · 3.63 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package cssrc
// 报错行号+3
const templateText = `// Generated by github.com/davyxu/tabtoy
// DO NOT EDIT!!
// Version: {{.Version}}
using System;
using System.Collections.Generic;
namespace {{.PackageName}}
{ {{range $sn, $objName := $.Types.EnumNames}}
public enum {{$objName}}
{ {{range $fi,$field := $.Types.AllFieldByName $objName}}
{{$field.FieldName}} = {{$field.Value}}, // {{if not $field.Note}}{{$field.Name}}{{else}}{{ $field.Note}}{{end}} {{end}}
}
{{end}} {{range $sn, $objName := $.Types.StructNames}}
public partial class {{$objName}} : tabtoy.ITableSerializable
{
{{range $fi,$field := $.Types.AllFieldByName $objName}}{{if IsWarpFieldName $ $field}}public {{CSType $field}} {{$field.FieldName}} = {{CSDefaultValue $ $field}};
{{end}}{{end}}
{{if $.GenBinary}}#region Deserialize Code
public void Deserialize( tabtoy.TableReader reader )
{
UInt32 mamaSaidTagNameShouldBeLong = 0;
while ( reader.ReadTag(ref mamaSaidTagNameShouldBeLong) )
{
switch (mamaSaidTagNameShouldBeLong)
{
{{range $fi,$field := $.Types.AllFieldByName $objName}}{{if IsWarpFieldName $ $field}}case {{CSTag $ $fi $field}}:
{
reader.Read{{CSReader $ $field}}( ref {{$field.FieldName}} );
}
break;
{{end}}{{end}}
default:
{
reader.SkipFiled(mamaSaidTagNameShouldBeLong);
}
break;
}
}
}
#endregion {{end}}
}
{{end}}
// Combine struct
public partial class {{.CombineStructName}}
{ {{range $ti, $tab := $.Datas.AllTables}}
// table: {{$tab.HeaderType}}
public List<{{$tab.HeaderType}}> {{$tab.HeaderType}} = new List<{{$tab.HeaderType}}>(); {{end}}
// Indices
{{range $ii, $idx := GetIndices $}}{{if IsWarpFieldName $ $idx.FieldInfo}}public Dictionary<{{CSType $idx.FieldInfo}},{{$idx.Table.HeaderType}}> {{$idx.Table.HeaderType}}By{{$idx.FieldInfo.FieldName}} = new Dictionary<{{CSType $idx.FieldInfo}},{{$idx.Table.HeaderType}}>();
{{end}}{{end}}
{{if HasKeyValueTypes $}}
{{range $ti, $name := GetKeyValueTypeNames $}}
// table: {{$name}}
public {{$name}} GetKeyValue_{{$name}}()
{
return {{$name}}[0];
}{{end}}{{end}}
public void ResetData( )
{ {{range $ti, $tab := $.Datas.AllTables}}
{{$tab.HeaderType}}.Clear(); {{end}}
{{range $ii, $idx := GetIndices $}}{{if IsWarpFieldName $ $idx.FieldInfo}}{{$idx.Table.HeaderType}}By{{$idx.FieldInfo.FieldName}}.Clear();
{{end}}{{end}}
}
{{if $.GenBinary}}
public void Deserialize( tabtoy.TableReader reader )
{
reader.ReadHeader();
UInt32 mamaSaidTagNameShouldBeLong = 0;
while ( reader.ReadTag(ref mamaSaidTagNameShouldBeLong) )
{
if (mamaSaidTagNameShouldBeLong == 0x6f0000)
{
var tabName = string.Empty;
reader.ReadString(ref tabName);
switch (tabName)
{ {{range $ti, $tab := $.Datas.AllTables}}
case "{{$tab.HeaderType}}":
{
reader.ReadStruct(ref {{$tab.HeaderType}});
}
break;{{end}}
default:
{
reader.SkipFiled(mamaSaidTagNameShouldBeLong);
}
break;
}
}
}
}
public void IndexData( string tabName = "")
{ {{range $ii, $idx := GetIndices $}}
if (tabName == "" || tabName == "{{$idx.Table.HeaderType}}")
{
{{if IsWarpFieldName $ $idx.FieldInfo}}foreach( var kv in {{$idx.Table.HeaderType}} )
{
{{$idx.Table.HeaderType}}By{{$idx.FieldInfo.FieldName}}[kv.{{$idx.FieldInfo.FieldName}}] = kv;
}
{{end}}
}
{{end}}
}
{{end}}
}
}
`