This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy patheditor_utils.pas
356 lines (275 loc) · 7.79 KB
/
editor_utils.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
{ This file is a part of Map editor for VCMI project
Copyright (C) 2013-2017 Alexander Shishkin alexvins@users.sourceforge.net
This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit editor_utils;
{$mode objfpc}{$H+}
interface
uses
sysutils, Classes, types, typinfo, editor_types, editor_consts, LazUTF8, fpjson, gmap, FPimage;
type
{ TStringCompare }
TStringCompare = class
public
class function c(a,b: AnsiString): boolean;
end;
//TCustomIDCompare = specialize gutil.TLess<TCustomID>;
TNameToIdMap = specialize TMap<AnsiString,TCustomID,TStringCompare>;
//TIdToNameMap = specialize TMap<TCustomID,AnsiString,TCustomIDCompare>;
function ExtractModID(AIdentifier:AnsiString): AnsiString;
function ExtractModID2(var AIdentifier:AnsiString): AnsiString;
procedure LeToNInPlase(var Val:Int32); inline;
function CrStrList: TStringList;
procedure FillStringArray(ASrc: TStrings; var ADest: TStringDynArray);
function NormalizeModId(AModId: TModId): TModId;
function NormalizeResourceName(const AName: string): string;
function NormalizeKeyWord(const AValue: string): string;
function NormalizeIndentifier(const AValue: AnsiString): AnsiString;
function StripScope(const AIdentifier: string): string;
procedure GenerateDefaultVisitableFrom(ADest: TStrings; AGroup: UInt8; Atyp: Tobj);
procedure GenerateDefaultVisitableFrom(ADest: TJSONArray; AGroup: UInt8; Atyp: Tobj);
function RGBAColorToFpColor(const c: TRBGAColor): TFPColor;
function DecodeFullIdentifier(const ASource: AnsiString; out AMetaclass: TMetaclass; out AScope: AnsiString; out AIdentifier: AnsiString): Boolean;
function EncodeFullIdentifier(AMetaclass: AnsiString; AScope: AnsiString; AIdentifier: AnsiString): AnsiString;
function EncodeIdentifier(AScope: AnsiString; AIdentifier: AnsiString): AnsiString;
function CompareStringProxy(const s1,s2: string): integer;
implementation
uses
LazFileUtils;
function CompareStringProxy(const s1,s2: string): integer;
begin
Result := UTF8CompareStr(s1, s2);
end;
function ExtractModID(AIdentifier:AnsiString): AnsiString;
var
colon_pos: SizeInt;
begin
if(AIdentifier = '') then
exit('');
colon_pos := pos(':',AIdentifier);
if colon_pos <= 0 then
exit('');//object is from core
Result := copy(AIdentifier, 1, colon_pos-1);
end;
function ExtractModID2(var AIdentifier: AnsiString): AnsiString;
var
colon_pos: SizeInt;
begin
if(AIdentifier = '') then
exit('');
colon_pos := pos(':',AIdentifier);
if colon_pos <= 0 then
exit('');//object is from core
Result := copy(AIdentifier, 1, colon_pos-1);
AIdentifier:=copy(AIdentifier, colon_pos+1, MaxInt);
end;
procedure LeToNInPlase(var Val:Int32); inline;
begin
val := LEtoN(val);
end;
function CrStrList: TStringList;
begin
Result := TStringList.Create;
Result.Sorted := True;
Result.Duplicates := dupIgnore;
end;
procedure FillStringArray(ASrc: TStrings; var ADest: TStringDynArray);
var
i: Integer;
begin
SetLength(ADest, ASrc.Count);
for i := 0 to ASrc.Count - 1 do
begin
ADest[i] := ASrc[i];
end;
end;
function NormalizeModId(AModId: TModId): TModId;
begin
Result := Trim(LowerCase(AModId));
end;
function NormalizeResourceName(const AName: string): string;
begin
Result := SetDirSeparators(AName);
Result := UpperCase(Result);
Result := ExtractFileNameWithoutExt(Result);
end;
function NormalizeKeyWord(const AValue: string): string;
begin
result := UTF8LowerCase(UTF8Trim(AValue));
end;
function NormalizeIndentifier(const AValue: AnsiString): AnsiString;
var
id_only: AnsiString;
mod_id, mod_id2: String;
begin
id_only := AValue;
mod_id := ExtractModID2(id_only);
mod_id2 := mod_id;
while mod_id2 <> '' do
begin
mod_id2 := ExtractModID2(id_only);
end;
Result := EncodeIdentifier(mod_id, id_only);
end;
function StripScope(const AIdentifier: string): string;
var
colon_position: SizeInt;
begin
colon_position := Pos(':', AIdentifier);
if colon_position <=0 then
begin
Result := AIdentifier;
end
else
begin
Result := copy(AIdentifier, colon_position+1, MaxInt);
end;
end;
procedure GenerateDefaultVisitableFrom(ADest: TStrings; AGroup: UInt8;
Atyp: Tobj);
var
from_top: Boolean;
str: String;
begin
if (AGroup=2) or (AGroup=3) or (AGroup=4) or (AGroup=5) then
from_top := true
else
case TObj(Atyp) of
TObj.FLOTSAM,
TObj.SEA_CHEST,
Tobj.SHIPWRECK_SURVIVOR,
Tobj.BUOY,
Tobj.OCEAN_BOTTLE,
TObj.BOAT,
TObj.WHIRLPOOL,
Tobj.GARRISON,
Tobj.GARRISON2,
Tobj.SCHOLAR,
Tobj.CAMPFIRE,
Tobj.BORDERGUARD,
Tobj.BORDER_GATE,
Tobj.QUEST_GUARD,
Tobj.CORPSE: from_top:= true;
else
from_top := false;
end;
//top line
if from_top then
str := '+++'
else
str := '---';
UniqueString(str);
ADest.Add(str);
//middle line
str := '+-+';
UniqueString(str);
ADest.Add(str);
//bollom line
str := '+++';
UniqueString(str);
ADest.Add(str)
end;
procedure GenerateDefaultVisitableFrom(ADest: TJSONArray; AGroup: UInt8;
Atyp: Tobj);
var
tmp: TStringList;
s: String;
begin
tmp := TStringList.Create;
try
GenerateDefaultVisitableFrom(tmp, AGroup, Atyp);
ADest.Clear;
for s in tmp do
begin
ADest.Add(s);
end;
finally
tmp.Free;
end;
end;
function RGBAColorToFpColor(const c: TRBGAColor): TFPColor;
begin
Result.Red:=c.r + c.r shl 8;
Result.Green:=c.g + c.g shl 8;
Result.Blue:= c.b + c.b shl 8;
Result.Alpha:=c.a + c.a shl 8;
end;
function DecodeFullIdentifier(const ASource: AnsiString; out AMetaclass: TMetaclass; out AScope: AnsiString; out
AIdentifier: AnsiString): Boolean;
var
Metaclass, scope, identifier: AnsiString;
dot_pos: SizeInt;
raw_metaclass: Integer;
begin
Result := false;
Metaclass := '';
identifier:=ASource;
scope:= ExtractModID2(identifier);
dot_pos := pos('.', identifier);
if dot_pos <= 0 then
begin
Metaclass := identifier;
identifier:='';
end
else
begin
Metaclass := copy(identifier, 1, dot_pos-1);
identifier := copy(identifier, dot_pos+1, MaxInt);
end;
raw_metaclass := GetEnumValue(TypeInfo(TMetaclass),Metaclass);
Result := raw_metaclass >= 0;
AMetaclass := TMetaclass(raw_metaclass);
AScope := scope;
AIdentifier:=identifier;
Result := true;
end;
function EncodeFullIdentifier(AMetaclass: AnsiString; AScope: AnsiString; AIdentifier: AnsiString): AnsiString;
var
scope_in: AnsiString;
begin
scope_in := ExtractModID2(AIdentifier);
if AScope = '' then
begin
AScope := scope_in;
end;
if AScope = '' then
begin
if AIdentifier = '' then
begin
Result := AMetaclass;
end
else
begin
Result := AMetaclass + '.' + AIdentifier;
end;
end
else
begin
Result := AScope + ':' + AMetaclass + '.' + AIdentifier;
end;
end;
function EncodeIdentifier(AScope: AnsiString; AIdentifier: AnsiString): AnsiString;
begin
if AScope = '' then
begin
Result := AIdentifier;
end
else
begin
Result := AScope + ':' + AIdentifier;
end;
end;
{ TStringCompare }
class function TStringCompare.c(a, b: AnsiString): boolean;
begin
Result := CompareText(a,b) > 0;
end;
end.