-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJson2TableTools.cs
287 lines (265 loc) · 6.91 KB
/
Json2TableTools.cs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
public class Json2TableTools
{
/// <summary>
/// json表转换成lua表
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static string GetLuaFromJson(string json)
{
using (MemoryStream stream = new MemoryStream())
using (StreamWriter writer = new StreamWriter(stream))
{
JObject jObject = JObject.Parse(json);
writer.Write("local config = ");
Json2Table(jObject.ToString(), writer);
writer.Write("return config");
writer.Flush();
writer.Close();
var bs = stream.ToArray();
return Encoding.UTF8.GetString(bs);
}
}
/// <summary>
/// lua表装换成json表
/// </summary>
/// <param name="lua"></param>
/// <returns></returns>
public static string GetJsonFromLua(string lua)
{
using (MemoryStream stream = new MemoryStream())
using (StreamWriter writer = new StreamWriter(stream))
{
Table2Json(lua, writer);
writer.Flush();
writer.Close();
var bs = stream.ToArray();
return Encoding.UTF8.GetString(bs);
}
}
/// <summary>
/// 初始化配置列表
/// </summary>
public static void OnInitConfigList(string path)
{
var files = Directory.GetFiles(path).Where((p) => p.EndsWith(".lua")).Select((p) => Path.GetFileName(p).Replace(".lua", string.Empty)).ToList();
using (FileStream file = new FileStream(path + @"\ConfigSetting.lua", FileMode.Create))
using (StreamWriter writer = new StreamWriter(file))
{
writer.WriteLine(@"local config = {");
for (int i = 0; i < files.Count; i++)
{
if (i == files.Count - 1)
{
writer.WriteLine(string.Format(@" [""{0}""] = 1", files[i]));
}
else
{
writer.WriteLine(string.Format(@" [""{0}""] = 1,", files[i]));
}
}
writer.WriteLine(@"}");
writer.WriteLine(@"return config");
}
}
static void Json2Table(string file, StreamWriter writer)
{
byte[] array = Encoding.UTF8.GetBytes(file);
using (MemoryStream stream = new MemoryStream(array))
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
while (!reader.EndOfStream)
{
var context = reader.ReadLine();
if (context.EndsWith("[],"))
{
context = context.Substring(0, context.Length - 3) + "{},";
}
else if (context.EndsWith("[]"))
{
context = context.Substring(0, context.Length - 2) + "{}";
//context = context.Replace("]", "}");
}
else if (context.EndsWith("["))
{
context = context.Substring(0, context.Length - 1) + "{";
//context = context.Replace("],", "},");
}
else if (context.EndsWith("]"))
{
context = context.Substring(0, context.Length - 1) + "}";
//context = context.Replace("],", "},");
}
else if (context.EndsWith("],"))
{
context = context.Substring(0, context.Length - 2) + "},";
//context = context.Replace("],", "},");
}
if (context.Contains("null"))
{
Console.WriteLine("null Value=> " + context);
context = context.Replace("null", "nil");
}
if (context.Contains(":"))
{
var test = context.Replace(" ", string.Empty);
var index = test.IndexOf(':');
if (test.ToArray()[index - 1] == '"')
{
///满足条件
index = context.IndexOf(':');
var key = context.Substring(0, index);
var count = GetSpaceCout(key);
key = key.Trim().Replace(@"""", string.Empty);
var value = context.Substring(index + 1, context.Length - index - 1).Trim();
long number = 0;
if (long.TryParse(key, out number))
{
key = string.Format(@"[""{0}""]", key);
}
writer.WriteLine(GetSpace(count) + key + " = " + value);
}
else
{
writer.WriteLine(context);
}
}
else
{
writer.WriteLine(context);
}
}
}
}
static void Table2Json(string file, StreamWriter writer)
{
List<int> cache = new List<int>();
byte[] array = Encoding.UTF8.GetBytes(file);
using (MemoryStream stream = new MemoryStream(array))
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
List<string> source = new List<string>();
while (!reader.EndOfStream)
{
source.Add(reader.ReadLine());
}
for (int i = 0; i < source.Count; i++)
{
var context = source[i];
if (context.Contains("local config = "))
{
context = context.Replace("local config = ", string.Empty);
}
if (context.Contains("return config"))
{
context = context.Replace("return config", string.Empty);
}
if (context.Contains("nil"))
{
Console.WriteLine("nil Value=> " + context);
context = context.Replace("nil", "null");
}
if (context.Contains("="))
{
var index = context.IndexOf('=');
if (context.ToArray()[index - 1] == ' ' && context.ToArray()[index + 1] == ' ')
{
///满足条件
index = context.IndexOf('=');
var key = context.Substring(0, index);
var count = GetSpaceCout(key);
key = key.Trim();
var value = context.Substring(index + 1, context.Length - index - 1).Trim();
if (key.Contains("[") || key.Contains("]"))
{
key = key.Replace("[", string.Empty).Replace("]", string.Empty);
}
if (!key.Contains(@""""))
{
key = string.Format(@"""{0}""", key);
}
if (value == "{}")
{
value = "[]";
}
if (value == "{},")
{
value = "[],";
}
if (value == "{")
{
string nextLine = source[i + 1].Trim();
if (nextLine.StartsWith("{"))
{
value = "[";
cache.Add(count);
}
else if (!nextLine.Contains(" = "))
{
value = "[";
cache.Add(count);
}
}
writer.WriteLine(GetSpace(count) + key + " : " + value);
}
else
{
if (!string.IsNullOrEmpty(context))
{
writer.WriteLine(context);
}
}
}
else
{
var test = context.Trim();
if (test.StartsWith("}"))
{
var count = GetSpaceCout(context);
if (cache.Count > 0 && cache[cache.Count - 1] == count)
{
context = context.Replace("}", "]");
cache.RemoveAt(cache.Count - 1);
}
}
if (!string.IsNullOrEmpty(context))
{
writer.WriteLine(context);
}
}
}
}
}
static string GetSpace(int count)
{
string str = string.Empty;
for (int i = 0; i < count; i++)
{
str += " ";
}
return str;
}
static int GetSpaceCout(string str)
{
int count = 0;
var array = str.ToArray();
for (int i = 0; i < array.Length; i++)
{
if (array[i] == ' ')
{
count++;
}
else
{
break;
}
}
return count;
}
}