-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanitxtjson.pas
318 lines (286 loc) · 6.79 KB
/
anitxtjson.pas
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
unit AniTxtJson;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, JsonTools, CastleDownload;
type
TAniLine = record
Success: Boolean;
FromFrame: Integer;
ToFrame: Integer;
Action: String;
end;
function AniTxtToJson(const InFile: String): TJsonNode;
function ParseText(const S: String): TAniLine;
function ParseText_FormatA(const S: String): TAniLine;
function ParseText_FormatB(const S: String): TAniLine;
implementation
const
CHECK_TAB = $09;
CHECK_DIGIT_0 = $30;
CHECK_DIGIT_9 = $39;
CHECK_COLON = $3A;
CHECK_UCASE_A = $41;
CHECK_UCASE_Z = $5A;
CHECK_LCASE_A = $61;
CHECK_LCASE_Z = $7A;
{$inline on}
function isDigit(c: BYTE): Boolean; inline;
begin
Result := (((c >= CHECK_DIGIT_0) and (c <= CHECK_DIGIT_9)));
end;
function isAlpha(c: BYTE): Boolean; inline;
begin
Result := (((c >= CHECK_UCASE_A) and (c <= CHECK_UCASE_Z)) or
((c >= CHECK_LCASE_A) and (c <= CHECK_LCASE_Z)));
end;
{$inline off}
// <Action><Tab(s)><Start><End>
function ParseText_FormatC(const S: String): TAniLine;
var
p: PChar;
l: Integer;
i: Integer;
stage: Integer;
TmpFrom: String;
TmpTo: String;
begin
stage := 0;
TmpFrom := EmptyStr;
TmpTo := EmptyStr;
Result := Default(TAniLine);
// Get a pointer to the start of the string
p := PChar(S);
// Length of string
l := Length(S);
// Loop over the string
for i := 0 to l - 1 do
begin
if (stage = 0) and (ord(p^) = CHECK_TAB) then
Inc(stage)
else if (stage > 0) and isDigit(ord(p^)) then
begin
case stage of
1:
begin
TmpFrom += p^;
end;
2:
begin
TmpTo += p^;
end;
end;
end
else
begin
if (stage = 0) then
Result.Action += p^;
if (stage = 1) and (Length(TmpFrom) > 0) and not(isDigit(ord(p^))) then
Inc(stage);
if (stage = 2) and (Length(TmpTo) > 0) and not(isDigit(ord(p^))) then
Break;
end;
Inc(p);
end;
if (stage = 2) then
begin
Result.Success := True;
Result.FromFrame := StrToIntDef(TmpFrom.Trim, 0);
Result.ToFrame := StrToIntDef(TmpTo.Trim, 0);
Result.Action := Result.Action.Trim;
end;
end;
// <Action>:<Start><End>
function ParseText_FormatB(const S: String): TAniLine;
var
p: PChar;
l: Integer;
i: Integer;
stage: Integer;
TmpFrom: String;
TmpTo: String;
begin
stage := 0;
TmpFrom := EmptyStr;
TmpTo := EmptyStr;
Result := Default(TAniLine);
// Get a pointer to the start of the string
p := PChar(S);
// Length of string
l := Length(S);
// Loop over the string
for i := 0 to l - 1 do
begin
if (stage = 0) and (ord(p^) = CHECK_COLON) then
Inc(stage)
else if (stage > 0) and isDigit(ord(p^)) then
begin
case stage of
1:
begin
TmpFrom += p^;
end;
2:
begin
TmpTo += p^;
end;
end;
end
else
begin
if (stage = 0) then
Result.Action += p^;
if (stage = 1) and (Length(TmpFrom) > 0) and not(isDigit(ord(p^))) then
Inc(stage);
if (stage = 2) and (Length(TmpTo) > 0) and not(isDigit(ord(p^))) then
Break;
end;
Inc(p);
end;
if (stage = 2) then
begin
Result.Success := True;
Result.FromFrame := StrToIntDef(TmpFrom.Trim, 0);
Result.ToFrame := StrToIntDef(TmpTo.Trim, 0);
Result.Action := Result.Action.Trim;
end;
end;
// <Start><End><Action>
function ParseText_FormatA(const S: String): TAniLine;
var
p: PChar;
l: Integer;
i: Integer;
stage: Integer;
TmpFrom: String;
TmpTo: String;
begin
stage := 0;
TmpFrom := EmptyStr;
TmpTo := EmptyStr;
Result := Default(TAniLine);
// Get a pointer to the start of the string
p := PChar(S);
// Length of string
l := Length(S);
// Loop over the string
for i := 0 to l - 1 do
begin
if (stage < 2) and isDigit(ord(p^)) then
begin
case stage of
0:
begin
TmpFrom += p^;
end;
1:
begin
TmpTo += p^;
end;
end;
end
else
begin
if (stage = 0) and (Length(TmpFrom) > 0) and not(isDigit(ord(p^))) then
Inc(stage);
if (stage = 1) and (Length(TmpTo) > 0) and not(isDigit(ord(p^))) then
Inc(stage);
if (stage = 2) then
Result.Action += p^;
end;
Inc(p);
end;
if (stage = 2) then
begin
Result.Success := True;
Result.FromFrame := StrToIntDef(TmpFrom.Trim, 0);
Result.ToFrame := StrToIntDef(TmpTo.Trim, 0);
Result.Action := Result.Action.Trim;
end;
end;
function ParseText(const S: String): TAniLine;
var
p: PChar;
l: Integer;
i: Integer;
begin
Result := Default(TAniLine);
// Get a pointer to the start of the string
p := PChar(S);
// Length of string
l := Length(S);
// Loop over the string
for i := 0 to l - 1 do
begin
if isDigit(ord(p^)) then
begin
Result := ParseText_FormatA(S);
Break;
end;
if isAlpha(ord(p^)) then
begin
Result := ParseText_FormatB(S);
if not(Result.Success) then
Result := ParseText_FormatC(S);
Break;
end;
Inc(p);
end;
end;
function AniTxtToJson(const InFile: String): TJsonNode;
var
F: TTextReader;
I: Integer;
S: string;
L: TAniLine;
Json: TJsonNode;
JArray: TJsonNode;
JObject: TJsonNode;
begin
if not(FileExists(InFile)) then
begin
if IsConsole then
WriteLn('File not found');
Exit(nil);
end;
Json := TJsonNode.Create;
JArray := Json.Add('Animations', nkObject);
JArray.Kind := nkArray;
I := 0;
F := nil;
try
F := TTextReader.Create(InFile);
try
while not F.Eof do
begin
Inc(I);
S := F.ReadLn;
S := S.Trim;
if Length(S) = 0 then
continue;
L := ParseText(S);
if not(L.Success) then
begin
JSon.Free;
JSon := nil;
Break;
end;
JObject := JArray.Add('', nkObject);
JObject.Add('FromFrame', L.FromFrame);
JObject.Add('ToFrame', L.ToFrame);
JObject.Add('Action', L.Action);
end;
except
on E : Exception do
begin
if IsConsole then
WriteLn('Error (' + E.ClassName + ') : ' + E.Message);
end;
end;
finally
if F <> nil then
F.Free;
end;
Result := Json;
end;
end.