-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacure.agc
1919 lines (1569 loc) · 49.2 KB
/
bacure.agc
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// version 0.9.3 23 Mar 2022
/*
Bacure - Little help for Basickness patients
App Game Kit 2 Tier 1 language helper functions
by Leo Rela Leo.Rela@gmail.com
Note: Function names beginning with "_" e.g. like _DoSomething are internal ones, don't use them directly
Features:
+ Boolean constants
+ Low performance string key/value maps + wrappers for float and integers
+ Low performance string value sets + wrappers for float and integers
+ Type guess/check for string content: int, float or str
+ (De)serialize maps to/from CSV files
+ Logging
+ TGF Trivial Graph Format parser
+ Simple XML parser (Partial support)
+ Tiled (map editor) TMX file parser (Partial support, read TMX support specification* for more info)
+ Stack for string values
+ Generate UUIDs
+ Yarn document parser
+ DialogTree D3 support http://sol.gfxile.net/d3/ (**)
*) Use Tiled 1.1.0 with following export settings:
Format: .tmx
Orientation: Orthogonal
Tile layer format: CSV
As Tiled is only partially supported so there are NO support for:
- External TSX tile set files
- Other tile layer data formats than CSV
- Embedded Tile data, tile content must be taken from Image file
- Wang sets
- Chunks
**) D3 Studio 110808 ALPHA supported
Format: XML
*/
// set TRUE only for development purposes.
// Set FALSE before you publish app except if you want it to write logs
global GLOBAL_LOGGER_ENABLED as string = TRUE
/*
Boolean
*/
#constant TRUE "T"
#constant FALSE "F"
// type names for map/set type check
#constant TYPE_STRING "string"
#constant TYPE_FLOAT "float"
#constant TYPE_INTEGER "int"
// null return value for Maps and Stacks
#constant NULL_VAL "__null__"
// whitespaces
#constant WHITESPACES chr(32)+chr(10)+chr(11)+chr(12)+chr(13)
// chars used in UUID
#constant HEX_CHARS "abcdef1234567890"
// others
#constant CRLF chr(13)+chr(10)
#constant qq chr(34)
// internal logging
#constant BACLOG = "bacure.log"
// read file to string
function ReadFile(filename$, linesep$)
file = OpenToRead(filename$)
data$ = ""
while FileEOF(file) <> 1
data$ = data$ + ReadLine(file) + linesep$
endwhile
CloseFile(file)
endfunction data$
type Map
keys as string[]
values as string[]
endtype
function MapPutFloat(map ref as Map, key$, value#)
MapPut(map, key$, Str(value#))
endfunction
function MapPutInt(map ref as Map, key$, value)
MapPut(map, key$, Str(value))
endfunction
function MapPut(map ref as Map, key$, value$)
MapPutStr(map, key$, value$)
endfunction
function MapPutStr(map ref as Map, key$, value$)
idx = _MapGetKeyIdx(map, key$)
// if key exists then overwrite value
if(idx <> -1)
map.values[idx] = value$
else
// key does not exists
map.keys.insert(key$)
map.values.insert(value$)
endif
endfunction
function MapHasKey(map ref as Map, key$)
if(_MapGetKeyIdx(map, key$) = -1) then exitfunction FALSE
endfunction TRUE
function _MapGetKeyIdx(map ref as Map, key$)
for i = 0 to map.keys.length
if map.keys[i] = key$ then exitfunction i
next
endfunction -1
function MapGetInt(map ref as Map, key$)
v = Val(MapGet(map, key$))
endfunction v
function MapGetFloat(map ref as Map, key$)
v as float
v = ValFloat(MapGet(map, key$))
endfunction v
function MapGet(map ref as Map, key$)
v$ = MapGetStr(map, key$)
endfunction v$
function MapGet2(map ref as Map, key$, default$)
v$ = MapGetStr(map, key$)
if v$ = NULL_VAL then exitfunction default$
endfunction v$
function MapGetStr(map ref as Map, key$)
keyidx = _MapGetKeyIdx(map, key$)
if(keyidx >= 0) then exitfunction map.values[keyidx]
endfunction NULL_VAL
function MapGetKeys(map ref as Map)
endfunction map.keys
function MapGetValues(map ref as Map)
endfunction map.values
function MapRemove(map ref as Map, key$)
keyidx = _MapGetKeyIdx(map, key$)
if(keyidx = -1) then exitfunction FALSE
map.keys.remove(keyidx)
map.values.remove(keyidx)
endfunction TRUE
function MapRemoveAll(map ref as Map)
map.keys.length = -1
map.values.length = -1
endfunction
function MapSortByIntValue(m ref as Map, ascbool$)
for i = m.keys.length to 1 step -1
for j = 0 to i-1
//if val(m.values[j]) < val(m.values[j+1])
if _IsAGreaterThanB(Val(m.values[j]), Val(m.values[j+1]), ascbool$) = TRUE
tmpval$ = m.values[j]
m.values[j] = m.values[j+1]
m.values[j+1] = tmpval$
tmpkey$ = m.keys[j]
m.keys[j] = m.keys[j+1]
m.keys[j+1] = tmpkey$
endif
next
next
endfunction
function _IsAGreaterThanB(a, b, reverse$)
if reverse$ = TRUE and a > b then exitfunction TRUE
if reverse$ = FALSE and a < b then exitfunction TRUE
endfunction FALSE
function SetToPrintable(set ref as Set)
str$=""
for i = 0 to set.values.length
value$ = set.values[i]
str$ = str$ + "'" + value$ + "',"
str$ = str$ + CRLF
next
endfunction str$
// returns proper TYPE_xxx constant or NULL_VAL if no key/value not found
function MapGetType(map ref as Map, key$)
if MapHasKey(map, key$) = FALSE then exitfunction NULL_VAL
v$ = MapGet(map, key$)
typ$ = GetValueType(v$)
endfunction typ$
function MapGetSize(map ref as Map)
endfunction map.keys.length+1
function SetGetSize(set ref as Set)
endfunction set.values.length+1
/*
Type guess functionality for string content
*/
// TRUE if character for given ascii is 0-9
function IsStrNumber(str$)
for i = 1 to len(str$)
if IsNumber(asc(mid(str$, i, 1))) = FALSE then exitfunction FALSE
next
endfunction TRUE
function IsStrAlpha(str$)
for i = 1 to len(str$)
if IsAlpha(asc(mid(str$, i, 1))) = FALSE then exitfunction FALSE
next
endfunction TRUE
function IsStrAlphaNumeric(str$)
for i = 1 to len(str$)
if IsAlphaNumeric(asc(mid(str$, i, 1))) = FALSE then exitfunction FALSE
next
endfunction TRUE
function IsNumber(ascii)
if ascii >= 48 and ascii <= 57 then exitfunction TRUE
endfunction FALSE
// TRUE if character for given ascii is A-Z or a-z
function IsAlpha(ascii)
if (ascii >= 97 and ascii <= 122) or (ascii >= 65 and ascii <= 90) then exitfunction TRUE
endfunction FALSE
// true if character for given ascii is alpha or numeric
function IsAlphaNumeric(ascii)
if IsNumber(ascii) = TRUE or IsAlpha(ascii) = TRUE then exitfunction TRUE
endfunction FALSE
// returns TYPE_INTEGER, TYPE_FLOAT or TYPE_STRING for given string
// e.g. "77" returns TYPE_INTEGER, "77.1" returns TYPE_FLOAT and "77abdef." returns TYPE_STRING
// given string is trimmed for WHITESPACE chars
function GetValueType(s$)
s$ = TrimString(s$, WHITESPACES)
if len(s$) = 0 then exitfunction TYPE_STRING
hasNumber$ = FALSE
pointCount = 0 // number of point "." chars, more than one means it string rather than float
firstch$ = Left(s$, 1)
if firstch$ = "-" or firstch$ = "+" and len(s$) > 1 then startidx = 2 else startidx = 1
for i = startidx to len(s$)
c$ = mid(s$, i, 1)
a = asc(c$)
if IsNumber(a) = TRUE
hasNumber$ = TRUE
elseif a = 46 // point '.' found
// TODO handle float like .1 = 0.1
inc pointCount, 1
if pointCount > 1 then exitfunction TYPE_STRING
else // there is at least one char other than number or point "." then this is a string
exitfunction TYPE_STRING
endif
next
if hasNumber$ = TRUE
if pointCount = 0
exitfunction TYPE_INTEGER
elseif pointCount = 1
exitfunction TYPE_FLOAT
endif
endif
endfunction TYPE_STRING
function IsEmptyOrNull(t$)
if t$ = "" or t$ = NULL_VAL then exitfunction TRUE
endfunction FALSE
function MapToStr(map ref as Map)
str$ = ""
for i = 0 to map.keys.length
key$ = map.keys[i]
value$ = map.values[i]
str$ = str$ + key$ + "=" + value$
if i < map.keys.length then str$ = str$ + ", "
next
endfunction str$
/*
Map (de)serialization
*/
function MapToCsvLine(map ref as Map, mapname$)
str$ = _HexEncodeStr(mapname$)
//format: mapname, key1, value1, key2, value2 ..."
for i = 0 to map.keys.length
key$ = map.keys[i]
value$ = map.values[i]
str$ = str$ + "," + _HexEncodeStr(key$) + "," + _HexEncodeStr(value$)
next
endfunction str$
function CsvLineToMap(str$)
map as Map
count = CountStringTokens2(str$, ",")
for i = 2 to count step 2
key$ = GetStringToken2(str$, ",", i)
value$ = GetStringToken2(str$, ",", i+1)
map.keys.insert(_HexDecodeStr(key$))
map.values.insert(_HexDecodeStr(value$))
next
endfunction map
function _HexEncodeStr(str$)
ret$ = ""
for i = 1 to Len(str$)
char$ = Mid(str$, i, 1)
code = Asc(char$)
hex$ = Hex(code)
ret$ = ret$ + hex$
next
endfunction ret$
function _HexDecodeStr(str$)
ret$ = ""
for i = 1 to Len(str$) step 2
hex$ = Mid(str$, i, 2)
code = Val(hex$, 16)
char$ = Chr(code)
ret$ = ret$ + char$
next
endfunction ret$
/*
Sets. Mostly copied code from Maps
*/
type Set
values as string[]
endtype
function SetPutFloat(set ref as Set, value#)
SetPut(set, Str(value#))
endfunction
function SetPutInt(set ref as Set, value)
SetPut(set, Str(value))
endfunction
function SetPut(set ref as Set, value$)
SetPutStr(set, value$)
endfunction
function SetPutStr(set ref as Set, value$)
idx = _SetGetValueIdx(set, value$)
// if key exists then overwrite value
if(idx >= 0)
set.values[idx] = value$
else
set.values.insert(value$)
endif
endfunction
function SetHasValue(set ref as Set, value$)
keyidx = _SetGetValueIdx(set, value$)
if(keyidx = -1) then exitfunction FALSE
endfunction TRUE
function _SetGetValueIdx(set ref as Set, value$)
for i = 0 to set.values.length
if(set.values[i] = value$)
exitfunction i
endif
next
endfunction -1
function SetGetInt(set ref as Set, value$)
v = Val(SetGet(set, value$))
endfunction v
function SetGetFloat(set ref as Set, value$)
v as float
v = ValFloat(SetGet(set, value$))
endfunction v
function SetGet(set ref as Set, value$)
v$ = SetGetStr(set, value$)
endfunction v$
function SetGetStr(set ref as Set, value$)
keyidx = _SetGetValueIdx(set, value$)
if(keyidx >= 0)
if(set.values[keyidx] = value$)
exitfunction set.values[keyidx]
endif
endif
endfunction NULL_VAL
function SetRemove(set ref as Set, value$)
keyidx = _SetGetValueIdx(set, value$)
if(keyidx = -1)
exitfunction FALSE
endif
set.values.remove(keyidx)
endfunction TRUE
function SetRemoveAll(set ref as Set)
set.values.length = -1
endfunction
// returns proper TYPE_xxx constant or NULL_VAL if no key/value not found
function GetSetType(set ref as Set, value$)
if SetHasValue(set, value$) = FALSE then exitfunction NULL_VAL
v$ = SetGet(set, value$)
typ$ = GetValueType(v$)
endfunction typ$
/*
* Logging
*/
#constant LOG_TIME_FORMAT_DATETIME "DT"
#constant LOG_TIME_FORMAT_TIME "T"
#constant LOG_TIME_FORMAT_NONE "N"
#constant LOG_ERROR 0
#constant LOG_WARN 1
#constant LOG_INFO 2
#constant LOG_DEBUG 3
#constant LOG_TRACE 4
#constant LOG_NONE 5
global LOGGER_LEVEL_NAMES as string[0]
global LOGGER_FILEIDS_BY_FILE as Map
global LOGGER_LEVELS_BY_FILE as Map
global LOGGER_TIME_FORMATS_BY_FILE as Map
function InitLog(file$, loglevel, appendbool$, timeformat$)
if loglevel >= LOG_NONE or loglevel < LOG_ERROR then exitfunction
// check if logger level names array needs to be initialized
if LOGGER_LEVEL_NAMES.length = 0 //
LOGGER_LEVEL_NAMES.length = 6
LOGGER_LEVEL_NAMES[LOG_ERROR] = "ERROR"
LOGGER_LEVEL_NAMES[LOG_WARN] = "WARN"
LOGGER_LEVEL_NAMES[LOG_INFO] = "INFO"
LOGGER_LEVEL_NAMES[LOG_DEBUG] = "DEBUG"
LOGGER_LEVEL_NAMES[LOG_TRACE] = "TRACE"
LOGGER_LEVEL_NAMES[LOG_NONE] = "NONE"
endif
GLOBAL_LOGGER_ENABLED = TRUE
MapPutInt(LOGGER_LEVELS_BY_FILE, file$, loglevel)
MapPutStr(LOGGER_TIME_FORMATS_BY_FILE, file$, timeformat$)
if appendbool$ = TRUE then append = 1 else append = 0
fileid = OpenToWrite(file$, append)
MapPutInt(LOGGER_FILEIDS_BY_FILE, file$, fileid)
CloseFile(fileid)
//info("*** Bacure logging initialized with level " + LOGGER_LEVEL_NAMES[LOGGER_LEVEL] + " ***")
endfunction
function _WriteLog(file$, level, msg$)
if GLOBAL_LOGGER_ENABLED = FALSE then exitfunction
if MapHasKey(LOGGER_LEVELS_BY_FILE, file$) = FALSE then exitfunction
loggerlevel = MapGetInt(LOGGER_LEVELS_BY_FILE, file$)
if level > loggerlevel or level < LOG_ERROR then exitfunction
levelname$ = LOGGER_LEVEL_NAMES[level]
timeformat$ = MapGetStr(LOGGER_TIME_FORMATS_BY_FILE, file$)
if timeformat$ = LOG_TIME_FORMAT_DATETIME
msg$ = "[" + GetCurrentDate() + " " + GetCurrentTime() + "] " + levelname$ + ": " + msg$
elseif timeformat$ = LOG_TIME_FORMAT_TIME
msg$ = "[" + GetCurrentTime() + "] " + levelname$ + ": " + msg$
else
msg$ = levelname$ + ": " + msg$
endif
fileid = MapGetInt(LOGGER_FILEIDS_BY_FILE, file$)
OpenToWrite(fileid, file$, 1)
WriteLine(fileid, msg$)
CloseFile(fileid)
endfunction
function CloseAllLogs()
keys as string[]
keys = MapGetKeys(LOGGER_FILEIDS_BY_FILE)
for i = 0 to keys.length
CloseLog(keys[i])
next
endfunction
function CloseLog(file$)
if MapHasKey(LOGGER_FILEIDS_BY_FILE, file$) = FALSE then exitfunction
fileid = MapGetInt(LOGGER_FILEIDS_BY_FILE, file$)
CloseFile(fileid)
endfunction
function error(file$, msg$)
_WriteLog(file$, LOG_ERROR, msg$)
endfunction
function warn(file$, msg$)
_WriteLog(file$, LOG_WARN, msg$)
endfunction
function info(file$, msg$)
_WriteLog(file$, LOG_INFO, msg$)
endfunction
function debug(file$, msg$)
_WriteLog(file$, LOG_DEBUG, msg$)
endfunction
function trace(file$, msg$)
_WriteLog(file$, LOG_TRACE, msg$)
endfunction
/*
TGF Trivial Graph Format
*/
type TGraph
id as string
nodes as Map
edges as TGraphEdge[]
endtype
type TGraphEdge
fromId as string
toId as string
text as string
tags as Map
endtype
function TgfParseFile(file$)
graphs as TGraph[]
tgfFile = OpenToRead(file$)
phase = 0 // 0=id, 1=nodes(npc lines), 2=edges(node to node, player lines and tags)
graph as TGraph
sep$ = Chr(34)
while FileEOF(tgfFile) <> 1
line$ = TrimString(ReadLine(tgfFile), " ")
if Left(line$, 1) = "#"
Inc phase
if(phase = 3)
phase = 0
graphs.insert(graph)
endif
else
select phase
case 0: // id
graph.id = line$
endcase
case 1: // nodes
idx = FindString(line$, " ")
nodeId$ = Left(line$, idx-1)
textLen = Len(line$)-1-Len(nodeId$)
nodeText$ = ReplaceString(Right(line$, textLen), "''", Chr(34), -1)
MapPut(graph.nodes, nodeId$, nodeText$)
endcase
case 2: // edges
edge as TGraphEdge
// node ids from->to
idPart$ = TrimString(GetStringToken(line$, sep$, 1), " ")
edge.fromId = GetStringToken(idPart$, " ", 1)
edge.toId = GetStringToken(idPart$, " ", 2)
// text
edge.text = ReplaceString(GetStringToken(line$, sep$, 2), "''", Chr(34), -1)
// tags map
if(CountStringTokens(line$, sep$) >= 3)
tagMap as Map
tagPart$ = GetStringToken(line$, sep$, 3)
for i = 1 to CountStringTokens(tagPart$, " ")
tagItem$ = GetStringToken(tagPart$, " ", i)
key$ = GetStringToken2(tagItem$, "=", 1)
value$ = GetStringToken2(tagItem$, "=", 2)
MapPut(tagMap, key$, value$)
next
edge.tags = tagMap
endif
graph.edges.insert(edge)
endcase
endselect
endif
endwhile
CloseFile(tgfFile)
endfunction graphs
/*
Stack
*/
// Tests if this stack is empty.
function StaEmpty(a ref as string[])
if(a.length = -1) then exitfunction TRUE
endfunction FALSE
// Looks at the object at the top of this stack without removing it from the stack.
function StaPeek(a ref as string[])
if(a.length = -1) then exitfunction NULL_VAL
ret$ = a[a.length]
endfunction ret$
// Look idx object from at the top of stack. idx=1 is top
function StaPeekByIdx(a ref as string[], idx)
if(idx > a.length+1) then exitfunction NULL_VAL
ret$ = a[a.length+1 - idx]
endfunction ret$
// Removes the object at the top of this stack and returns that object as the value of this function.
function StaPop(a ref as string[])
if(a.length = -1) then exitfunction NULL_VAL
ret$ = StaPeek(a)
a.remove()
endfunction ret$
// size of current stack
function StaLen(a ref as string[])
endfunction a.length+1
// Pushes an item onto the top of this stack.
function StaPush(a ref as string[], val as string)
a.insert(val)
endfunction
// Returns the 1-based position where an object is on this stack. 1 = top
function StaSearch(a ref as string[], val as string)
if(a.length >= 0)
dis = 1
for i = a.length to 0 step -1
if(a[i] = val) then exitfunction dis
Inc dis, 1
next
endif
endfunction -1
/*
XML parser
*/
type XmlElement
name as string
content as string
isSelfClosed as string
isStartTag as string
attributes as Map
isValid as string
endtype
// decode XML unsafe characters
function _XmlStr(s$)
s$ = ReplaceString(s$, "'", "'", -1)
s$ = ReplaceString(s$, "<", "<", -1)
s$ = ReplaceString(s$, ">", ">", -1)
s$ = ReplaceString(s$, """, chr(34), -1)
s$ = ReplaceString(s$, "&", "&", -1)
endfunction s$
function XmlParseFile(filename$)
document as XmlElement[]
document = XmlParseDocument(ReadFile(filename$, ""))
endfunction document
function XmlParseDocument(data as string)
trace(BACLOG, "XmlParseDocument parsing content '" + data + "'")
elements as XmlElement[]
e as XmlElement
tokenNum = CountStringTokens(data, "<")
debug(BACLOG, "XmlParseDocument " + str(tokenNum) + " tokens found")
for i = 1 to tokenNum
token$ = GetStringToken(data, "<", i)
trace(BACLOG, "Parsed token:"+token$)
e = _XmlParseElement(token$)
name$ = e.name
debug(BACLOG, "XmlParseDocument found element:" + name$)
elements.insert(e)
next
endfunction elements
function _XmlParseElement(x as string)
trace(BACLOG, "_XmlParseElement raw input '"+x+"'")
e as XmlElement
x = TrimString(x, WHITESPACES)
//trace(BACLOG, "_XmlParseElement parse element '<" + x + "'")
e.isValid = TRUE
if(Left(x, 1) <> "/")
e.isStartTag = TRUE
else
e.isStartTag = FALSE
endif
trace(BACLOG, "_XmlParseElement is start tag: " + e.isStartTag)
// if no content then Len = 0
content$ = _XmlGetContent(x)
// remove content from element data
contentLen = Len(content$)
if(contentLen > 0)
e.content = _XmlStr(content$)
x = Left(x, Len(x)-contentLen)
trace(BACLOG, "_XmlParseElement content '" + content$ + "'")
endif
if(Right(x, 2) = "/>")
e.isSelfClosed = TRUE
elseif(Right(x, 1) = ">")
e.isSelfClosed = FALSE
else
e.isValid = FALSE
warn(BACLOG, "_XmlParseElement ignoring invalid element '" + x + "'")
exitfunction e
endif
trace(BACLOG, "_XmlParseElement is self closed elem: " + e.isSelfClosed)
// parse tag name
tmp$ = ""
wsIdx = _XmlFirstIndexOfChar(x, WHITESPACES) // returns -1 if no chars found
if(wsIdx = 0)
// the element does not have attributes so parse only a tag name
//tmp$ = ""
il = Len(x)
for i = 1 to il
ch$ = Mid(x, i, 1)
if(ch$ <> ">" and ch$ <> "/")
tmp$ = tmp$ + ch$
endif
next
e.name = tmp$
//debug(BACLOG, "_XmlParseElement found new element '" + e.name + "'")
trace(BACLOG, "_XmlParseElement element has no attributes")
lll$ = "len=" + str(e.attributes.keys.length)
trace(BACLOG, lll$)
exitfunction e
else
// attributes must be parsed
name$ = Left(x, wsIdx-1)
e.name = name$
// parser states, initial state is attrState
state as string
state = "a" // initial state is attr
attribute as string
for i = wsIdx to Len(x)
ch$ = Mid(x, i, 1)
select state
case "a" // attribute
if(ch$ = "=")
attribute = tmp$
trace(BACLOG, "_XmlParseElement found attribute name '" + attribute + "'")
tmp$ = ""
state = "="
elseif(_XmlIsWhitespace(ch$) = FALSE)
tmp$ = tmp$ + ch$
endif
endcase
case "=" // equals
if(_XmlIsWhitespace(ch$) = TRUE)
// ignore
elseif(ch$ = chr(34)) // " found
state = "v" // next state: parse attr value
else
// onko error?
warn(BACLOG, "_XmlParseElement attribute is invalid due missing " + chr(34))
e.isValid = FALSE
endif
endcase
case "v" // value
if(ch$ = chr(34))
state = "a"
debug(BACLOG, "_XmlParseElement attribute found: name='" + attribute + "' value='" + tmp$ + "'")
tmp$ = _XmlStr(tmp$)
MapPut(e.attributes, attribute, tmp$)
tmp$ = ""
else
tmp$ = tmp$ + ch$
endif
endcase
endselect
next
endif
endfunction e
function _XmlIsWhitespace(char$)
if(char$ = " ") then exitfunction TRUE
code = Asc(char$)
if(code >= 10 and code <= 13) then exitfunction FALSE
endfunction FALSE
// returns 0 if no char found, otherwise char index
function _XmlFirstIndexOfChar(s as string, chars as string)
for i = 1 to Len(s)
ch$ = Mid(s, i, 1)
//integer FindString( str, findStr )
if(FindString(chars, ch$) > 0)
exitfunction i
endif
next
endfunction 0
function _XmlGetContent(elem$)
elemLen = Len(elem$)
for i = elemLen to 1 step -1
c$ = Mid(elem$, i, 1)
if(c$ = ">") then exit
next
content$ = Mid(elem$, i+1, elemLen)
endfunction content$
/*
* UUID
*/
function GenUUID()
uuid$ = ""
for i = 0 to 35
charidx = random(1, 16)
if i = 8 or i = 13 or i = 18 or i = 23
uuid$ = uuid$ + "-"
else
uuid$ = uuid$ + Mid(HEX_CHARS, charidx, 1)
endif
next
endfunction uuid$
/*
* Yarn dialogue editor support. This does not support new "->" kind lines yet
*/
/*
// returns -1 if no match
function _GetFirstIndexOfChar(str$, singleChar$)
le = Len(str$)
for i = 1 to le
if Mid(str$, i, 1) = singleChar$ then exitfunction i
next
endfunction -1
type YarnDialogEntry
text as string
endtype
type YarnNode
title as string
tags as Set
lines as YarnDialogEntry[]
replies as YarnOptionEntry[]
endtype
type YarnOptionEntry
text as string
target as string
endtype
type YarnDocument
nodes as YarnNode[]
endtype
#constant YARN_HEADER = "h"
#constant YARN_BODY = "b"
function ParseYarnDocument(content$)
state as String
state = YARN_HEADER
lineCount = CountStringTokens(content$, chr(10))
doc as YarnDocument
currentNode as YarnNode
currentOptionEntry as YarnOptionEntry
currentDialogEntry as YarnDialogEntry
for lineNum = 1 to lineCount
line$ = GetStringToken(content$, chr(10), lineNum)
line$ = TrimString(line$, WHITESPACES)
itemCount = CountStringTokens(line$, ":")
// if it's header line with content like key:value then it is really header line
if state = YARN_HEADER and _GetFirstIndexOfChar(line$, ":") >= 2
key$ = GetStringToken(line$, ":", 1)
if key$ = "title"
currentNode.title = GetStringToken(line$, ":", 2)
elseif key$ = "tags"
// TODO: add items to set, selvitä välimerkki
endif
// else, it's a body line: either raw text or with a line with options
elseif state = YARN_BODY
// minimum option line [[a|b]] len=7
sepIdx = _GetFirstIndexOfChar(line$, "|")
if len(line$) >= 7 and left(line$, 2) = "[[" and right(line$, 2) = "]]" and sepIdx >= 4
// it is an option line
else
currentDialogEntry.text = line$
endif
elseif line$ = "---"
//body starts
state = YARN_BODY
elseif line$ = "==="
// next entry
state = YARN_HEADER
endif
next
endfunction doc
*/
/*
* D3 studio XML format
*/
type D3Text
lang as string
data as string
endtype
type D3Answer
targetid as string
need as Set
not_ as Set
answer as D3Text
userdata as string
endtype
type D3Card
id as string
settags as Set
cleartags as Set
userdata as string
question as D3Text
answers as D3Answer[]
endtype
type D3Deck
id as string
userdata as String
cards as D3Card[]
endtype
// keep all decks in memory?
global _decks as D3Deck[]