-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaHelper.cs
More file actions
157 lines (135 loc) · 5.19 KB
/
LuaHelper.cs
File metadata and controls
157 lines (135 loc) · 5.19 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Text;
namespace LanguageXlsxToLua
{
public static class LuaHelper
{
static string Indentation = "\t";
static string TableSeparator = "-";
static string KeyValueString = "{0} = {1},\n";
static string IndexValueString = "[{0}] = {1},\n";
static string TableCloser = "},\n";
public static string GetLuaStringFromFields(Field[] fields)
{
LuaFieldNode tree = BuildLuaTableTree(fields);
StringBuilder sb = new StringBuilder();
sb.Append("local Language = {\n");
if (tree.List != null)
{
foreach (KeyValuePair<int, LuaFieldNode> pair in tree.List)
{
sb.Append(ParseArrayBranch(pair.Key, pair.Value, Indentation));
}
}
if (tree.Map != null)
{
foreach (KeyValuePair<string, LuaFieldNode> pair in tree.Map)
{
sb.Append(ParseMapBranch(pair.Key, pair.Value, Indentation));
}
}
sb.Append("\n}\nreturn Language;");
return sb.ToString();
}
/// <summary>
/// Parse field data into a tree reflecting
/// </summary>
/// <param name="fields"></param>
/// <returns></returns>
static LuaFieldNode BuildLuaTableTree(Field[] fields)
{
LuaFieldNode root = new LuaFieldNode();
foreach (Field field in fields)
{
UnfoldLuaField(root, field.Name, field.Value);
}
return root;
}
static void UnfoldLuaField(LuaFieldNode root, string name, string value)
{
string[] parts = name.Split(TableSeparator);
string fieldName = parts[0];
int treeDepth = parts.Length;
LuaFieldNode node;
if (!root.TryGetNode(fieldName, out node))
{
node = new LuaFieldNode();
root.AddTableNode(fieldName, node);
}
if (parts.Length == 1)
{
node.AddValue(value);
}
else
{
for (int i = 1; i < treeDepth; i++)
{
string key = parts[i];
LuaFieldNode child = null;
if (!node.TryGetNode(key, out child))
{
child = new LuaFieldNode();
node.AddTableNode(key, child);
}
node = child;
}
node.AddValue(value);
}
}
static string ParseArrayBranch(int index, LuaFieldNode node, string indentation)
{
StringBuilder sb = new StringBuilder();
switch (node.FieldType)
{
case LuaFieldType.Single:
sb.Append(indentation + string.Format(IndexValueString, index, node.Value));
break;
case LuaFieldType.Array:
sb.Append(indentation + "[" + index + "] = {\n");
foreach (KeyValuePair<int, LuaFieldNode> pair in node.List)
{
sb.Append(ParseArrayBranch(pair.Key, pair.Value, indentation + Indentation));
}
sb.Append(indentation + TableCloser);
break;
case LuaFieldType.Hash:
sb.Append(indentation + "[" + index + "] = {\n");
foreach (KeyValuePair<string, LuaFieldNode> pair in node.Map)
{
sb.Append(ParseMapBranch(pair.Key, pair.Value, indentation + Indentation));
}
sb.Append(indentation + TableCloser);
break;
};
return sb.ToString();
}
static string ParseMapBranch(string key, LuaFieldNode node, string indentation)
{
StringBuilder sb = new StringBuilder();
switch (node.FieldType)
{
case LuaFieldType.Single:
sb.Append(indentation + string.Format(KeyValueString, key, node.Value));
break;
case LuaFieldType.Array:
sb.Append(indentation + key + " = {\n");
foreach (KeyValuePair<int, LuaFieldNode> pair in node.List)
{
sb.Append(ParseArrayBranch(pair.Key, pair.Value, indentation + Indentation));
}
sb.Append(indentation + TableCloser);
break;
case LuaFieldType.Hash:
sb.Append(indentation + key + " = {\n");
foreach (KeyValuePair<string, LuaFieldNode> pair in node.Map)
{
sb.Append(ParseMapBranch(pair.Key, pair.Value, indentation + Indentation));
}
sb.Append(indentation + TableCloser);
break;
};
return sb.ToString();
}
}
}