forked from x1nixmzeng/AlienBML
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBML.cs
More file actions
1089 lines (879 loc) · 32.6 KB
/
BML.cs
File metadata and controls
1089 lines (879 loc) · 32.6 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
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
// Alien Isolation (Binary XML converter)
// Written by WRS (xentax.com)
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using CathodeLib;
using System.Linq;
namespace CATHODE
{
/// <summary>
/// Handles BML config files as XML douments
/// </summary>
public class BML : CathodeFile
{
public XmlDocument Content { get { return GetContent(); } set { SetContent(value); } }
public static new Implementation Implementation = Implementation.CREATE | Implementation.LOAD | Implementation.SAVE;
public BML(string path) : base(path) { }
public BML(MemoryStream stream, string path = "") : base(stream, path) { }
public BML(byte[] data, string path = "") : base(data, path) { }
private Header _header;
private Node _root = new Node();
#region FILE_IO
override protected bool LoadInternal(MemoryStream stream)
{
bool valid = true;
using (BinaryReader reader = new BinaryReader(stream))
{
valid &= _header.Read(reader);
if (!valid)
{
reader.Close();
return valid;
}
BMLString.StringPool1.Clear();
BMLString.StringPool2.Clear();
valid &= ReadAllNodes(reader);
}
return valid;
}
override protected bool SaveInternal()
{
using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(_filepath)))
{
FixupAllNodes(_root, true);
Node[] nodearray = GetNodeArray();
UInt32 node_size = 0;
foreach (Node n in nodearray)
node_size += n.Size();
MemoryStream p1 = BMLString.StringPool1.Export();
MemoryStream p2 = BMLString.StringPool2.Export();
UInt32 block1 = Header.Size() + node_size + 1;
UInt32 block2 = block1 + 1 + (UInt32)p1.Length;
UInt32 block3 = block2 + (UInt32)p2.Length;
UInt32 file_size = block3 + 1;
_header.Fixup(block1, block2, block3);
writer.BaseStream.SetLength(file_size);
UInt32 of1 = block1 + 1;
UInt32 of2 = block2;
_header.Write(writer);
UInt32 cur_depth = 0;
UInt32 cur_depth_offset = 0;
foreach (Node n in nodearray)
{
if (n.Flags.Children > 0)
{
if (cur_depth != n.Depth + 1)
{
cur_depth_offset = Header.Size();
foreach (Node nn in nodearray)
{
if (nn.Depth == n.Depth + 1) break;
cur_depth_offset += nn.Size();
}
cur_depth = n.Depth + 1;
}
n.Offset = cur_depth_offset;
foreach (Node child in n.Nodes)
cur_depth_offset += child.Size();
}
n.Write(writer, of1, of2);
}
writer.BaseStream.Seek(block1 + 1, SeekOrigin.Begin);
p1.WriteTo(writer.BaseStream);
p2.WriteTo(writer.BaseStream);
}
return true;
}
#endregion
#region ACCESSORS
/// <summary>
/// Get the content of the BML file (as XML)
/// </summary>
private XmlDocument GetContent()
{
FixupAllNodes(_root, true);
XmlDocument xml = new XmlDocument();
xml.LoadXml(DumpNode(_root));
return xml;
}
/// <summary>
/// Set the content of the BML file (as XML)
/// </summary>
private bool SetContent(XmlDocument doc)
{
bool valid = true;
BMLString.StringPool1.Clear();
BMLString.StringPool2.Clear();
_root = new Node();
_root.SetDeclaration();
foreach (XmlNode xnode in doc.ChildNodes)
{
switch (xnode.NodeType)
{
case XmlNodeType.XmlDeclaration:
XmlDeclaration decl = (xnode as XmlDeclaration);
if (decl.Version != null && decl.Version.Length > 0)
{
Attribute ver = new Attribute();
ver.ReadXML("version", decl.Version);
_root.Attributes.Add(ver);
}
if (decl.Encoding != null && decl.Encoding.Length > 0)
{
Attribute enc = new Attribute();
enc.ReadXML("encoding", decl.Encoding);
_root.Attributes.Add(enc);
}
if (decl.Standalone != null && decl.Standalone.Length > 0)
{
Attribute sta = new Attribute();
sta.ReadXML("standalone", decl.Standalone);
_root.Attributes.Add(sta);
}
_root.Flags.Attributes = Convert.ToByte(_root.Attributes.Count & 0xFF);
break;
case XmlNodeType.Element:
Node actual_root = new Node();
valid &= actual_root.ReadXML(xnode as XmlElement, _root.Depth + 1);
_root.Nodes.Add(actual_root);
break;
}
}
return valid;
}
#endregion
#region HELPERS
private bool ReadWrapper(BinaryReader br, ref Node owner, UInt32 depth)
{
bool success = true;
Node n = new Node();
success &= n.Read(br, depth);
// try to parse other blocks
if( success )
{
if( n.Offset != 0 )
{
long pos = br.BaseStream.Position;
// seek to child pos
br.BaseStream.Position = n.Offset;
for (UInt32 i = 0; i < n.Flags.Children; i++)
{
success &= ReadWrapper(br, ref n, depth + 1);
}
// seek back
br.BaseStream.Position = pos;
}
owner.Nodes.Add(n);
}
return success;
}
private bool ReadAllNodes(BinaryReader br)
{
bool success = _root.Read(br, 0);
// this is always the initial node
success &= (_root.Text.value == "?xml");
// there should be at least 1 child node
success &= (_root.Flags.Children > 0);
if(!success) return false;
success &= ReadWrapper(br, ref _root, _root.Depth +1);
return success;
}
private string DumpNode(Node n, int depth = 0)
{
string d = "";
bool ignored = (depth == 0 && n.Attributes.Count == 0 );
if( !ignored )
{
d += String.Format("<{0}", n.Text.value);
foreach (Attribute a in n.Attributes)
{
// Now encodes XML entities (value)
d += String.Format(" {0}=\"{1}\"", a.Name.value, BMLString.EncodeXml(a.Value.value));
}
}
if (n.Nodes.Count > 0)
{
if (!ignored)
{
if( depth == 0 )
{
// first xml tag must end in matching <? tags ?>
d += "?";
}
d += ">";
if( n.End != null ) d += n.End.value;
}
if (n.Inner != null && n.Inner.value.Length != 0)
{
// Now encodes XML entities
d += BMLString.EncodeXml(n.Inner.value);
}
foreach (Node node in n.Nodes)
{
d += DumpNode(node, depth + 1);
}
// uh, the first xml tag doesn't need to close
if (depth != 0)
{
d += String.Format("</{0}>", n.Text.value);
if( n.End2 != null ) d += n.End2.value;
}
}
else if( !ignored )
{
if (n.Inner != null && n.Inner.value.Length != 0)
{
d += ">";
d += n.Inner.value;
d += String.Format("</{0}>", n.Text.value);
d += n.End2.value;
}
else
{
// <tag /> and <tag a="b" />
if( n.Attributes.Count > 0 )
{
d += " ";
}
d += "/>";
if (n.End != null) d += n.End.value;
if (n.End2 != null) d += n.End2.value;
}
}
return d;
}
private void FixupAllNodes(Node n, bool last_node)
{
n.Fixup(last_node);
int last = n.Nodes.Count - 1;
int count = 0;
foreach (Node c in n.Nodes)
{
FixupAllNodes(c, count == last);
++count;
}
}
private Node[] GetNodesAtDepth(List<Node> nodes, UInt32 depth)
{
List<Node> local_nodes = new List<Node>();
foreach( Node n in nodes )
{
if( n.Depth == depth )
{
local_nodes.Add(n);
}
else if( n.Depth < depth )
{
Node[] ns = GetNodesAtDepth(n.Nodes, depth);
if( ns.Length > 0 )
{
local_nodes.AddRange(ns);
}
}
}
return local_nodes.ToArray();
}
private Node[] GetNodeArray()
{
List<Node> nodes = new List<Node>();
// level 0
nodes.Add(_root);
// level 1+
UInt32 depth = 1;
while ( true )
{
Node[] ns = GetNodesAtDepth(_root.Nodes, depth);
if( ns.Length == 0 )
{
break;
}
else
{
nodes.AddRange(ns);
++depth;
}
}
return nodes.ToArray();
}
#endregion
#region STRUCTURES
struct Header
{
const string XML_FLAG = "xml\0";
public UInt32 blockData { get; private set; }
public UInt32 blockStrings { get; private set; }
public UInt32 blockLineEndings { get; private set; }
public bool Read(BinaryReader br)
{
bool valid = true;
string magic = Encoding.Default.GetString(br.ReadBytes(XML_FLAG.Length));
valid &= (magic == XML_FLAG);
blockData = br.ReadUInt32();
blockStrings = br.ReadUInt32();
blockLineEndings = br.ReadUInt32();
valid &= (blockLineEndings < br.BaseStream.Length);
valid &= (blockStrings < blockLineEndings);
valid &= (blockData < blockStrings);
return valid;
}
public bool Write(BinaryWriter bw)
{
bool valid = true;
bw.Write(Encoding.Default.GetBytes(XML_FLAG), 0, XML_FLAG.Length);
valid &= blockLineEndings != 0;
valid &= blockStrings != 0;
valid &= blockData != 0;
bw.Write(blockData);
bw.Write(blockStrings);
bw.Write(blockLineEndings);
return valid;
}
public void Fixup(UInt32 of1, UInt32 of2, UInt32 of3)
{
blockData = of1;
blockStrings = of2;
blockLineEndings = of3;
}
static public UInt32 Size()
{
return 16;
}
}
// complete.
struct Attribute
{
public BMLString.Ref Name { get; private set; }
public BMLString.Ref Value { get; private set; }
public bool ReadXML(string str_name, string str_value)
{
Name = new BMLString.Ref(str_name, true);
Value = new BMLString.Ref(BMLString.DecodeXml(str_value), true);
return true;
}
public bool Read(BinaryReader br)
{
Name = new BMLString.Ref(br, true);
Value = new BMLString.Ref(br, true);
return true;
}
public bool Write(BinaryWriter bw)
{
bw.Write(Name.offset);
bw.Write(Value.offset);
return true;
}
static public UInt32 Size()
{
return 8;
}
}
class NodeFlags
{
public Byte Attributes { get; set; }
public Byte RawInfo { get; set; }
public bool unknown_1
{
// 1 << 0
get { return GetFlag(0x1); }
set { SetFlag(0x1, value); }
}
public bool unknown_2
{
// 1 << 1
get { return GetFlag(0x2); }
set { SetFlag(0x2, value); }
}
public bool ContinueSequence
{
// 1 << 2
get { return GetFlag(0x4); }
set { SetFlag(0x4, value); }
}
bool GetFlag(Byte mask)
{
return (RawInfo & mask) != 0;
}
void SetFlag(Byte mask, bool value)
{
RawInfo &= Convert.ToByte((~mask) & 0xFF);
if (value)
{
RawInfo |= mask;
}
}
public UInt16 Children { get; set; }
public NodeFlags()
{
Attributes = 0;
RawInfo = 0;
Children = 0;
}
public bool Read(BinaryReader br)
{
UInt32 bytes = br.ReadUInt32();
// bit format:
// aaaa aaaa iiic cccc cccc cccc cccc cccc
// 8-bits : number of attributes
Attributes = Convert.ToByte(bytes & 0xFF);
// 3-bits : info flags
RawInfo = Convert.ToByte((bytes >> 8) & 0x7);
// 21-bits : number of child nodes
UInt32 raw_children = (bytes >> 11) & 0x1FFFFF;
// note: we store raw_children as u16 for alignment purposes
// aaaa aaaa iiic cccc cccc cccc ccc- ---- (- = ignored)
Children = Convert.ToUInt16(raw_children & 0xFFFF);
return true;
}
public bool Write(BinaryWriter bw)
{
UInt32 bytes = 0;
UInt32 tmp = Children;
bytes |= tmp << 11;
tmp = RawInfo;
bytes |= (tmp & 0x7) << 8;
tmp = Attributes;
bytes |= (tmp & 0xFF);
bw.Write(bytes);
return true;
}
static public UInt32 Size()
{
return 4;
}
}
class Node
{
public List<Node> Nodes { get; private set; }
public List<Attribute> Attributes { get; private set; }
public BMLString.Ref End2 { get; private set; }
public BMLString.Ref Text { get; private set; }
public BMLString.Ref End { get; private set; }
public BMLString.Ref Inner { get; private set; }
public UInt32 Offset { get; set; } // public modifier
public UInt32 Depth { get; private set; }
public NodeFlags Flags { get; private set; }
public Node()
{
Nodes = new List<Node>();
Attributes = new List<Attribute>();
Flags = new NodeFlags();
Depth = 0;
}
public void SetDeclaration()
{
Text = new BMLString.Ref("?xml", true);
Flags.Children = 0;
}
bool HasElementSibling(XmlElement ele)
{
XmlNode sibling = ele.NextSibling;
while (sibling != null)
{
if (sibling.NodeType == XmlNodeType.Element)
{
return true;
}
sibling = sibling.NextSibling;
}
return false;
}
public bool ReadXML(XmlElement ele, UInt32 depth)
{
bool valid = true;
Depth = depth;
Text = new BMLString.Ref(ele.Name, true);
if (ele.HasAttributes)
{
if (ele.Attributes.Count > 0xFF)
{
valid = false;
return valid;
}
foreach (XmlAttribute attr in ele.Attributes)
{
Attribute a = new Attribute();
a.ReadXML(attr.Name, attr.Value);
Attributes.Add(a);
}
}
if (ele.HasChildNodes)
{
// inner text is treated as a special text node, so it has children.. (YIKES)
foreach (XmlNode xnode in ele.ChildNodes)
{
// special parser requirements
switch (xnode.NodeType)
{
case XmlNodeType.Element:
XmlElement child = (xnode as XmlElement);
Node nchild = new Node();
valid &= nchild.ReadXML(child, depth + 1);
if (valid)
{
Nodes.Add(nchild);
}
break;
case XmlNodeType.Text:
Inner = new BMLString.Ref(BMLString.DecodeXml(xnode.Value), false);
End2 = new BMLString.Ref("\r\n", false);
break;
case XmlNodeType.Comment:
// Could be added as Inner/End2, but not required
break;
default:
break;
}
}
}
bool last_child = !HasElementSibling(ele);
Fixup(last_child);
return valid;
}
public bool Read(BinaryReader br, UInt32 depth)
{
bool valid = true;
Depth = depth;
Text = new BMLString.Ref(br, true);
valid &= Flags.Read(br);
// get attributes
if (Flags.Attributes > 0)
{
for (UInt32 attribs = 0; attribs < (UInt32)Flags.Attributes; attribs++)
{
Attribute a = new Attribute();
valid &= a.Read(br);
if (!valid)
{
return false;
}
Attributes.Add(a);
}
}
switch (Flags.RawInfo)
{
case 0: // 000
Offset = br.ReadUInt32();
break;
case 1: // 001
End = new BMLString.Ref(br, false);
Offset = br.ReadUInt32();
break;
case 2: // 010 -> last in sequence
case 6: // 110 -> continued sequence
End = new BMLString.Ref(br, false);
if (Flags.Children > 0)
{
Offset = br.ReadUInt32();
}
break;
case 3: // 011 -> last in sequence
case 7: // 111 -> continued sequence
// note: inner text is stored in the second pool
Inner = new BMLString.Ref(br, false); // inner text or line diff
End2 = new BMLString.Ref(br, false); // line ending
if (Flags.Children > 0)
{
Offset = br.ReadUInt32();
}
break;
default:
// flags may need sorting out
break;
}
return valid;
}
public UInt32 Size()
{
UInt32 my_size = 0;
// text offset
my_size += 4;
// flags
my_size += NodeFlags.Size();
// attribute entries (read from flags)
my_size += Attribute.Size() * Flags.Attributes;
// check against info
switch (Flags.RawInfo)
{
case 0:
my_size += 4;
break;
case 1:
my_size += 4 + 4;
break;
case 2:
case 6:
my_size += 4;
if (Flags.Children > 0) my_size += 4;
break;
case 3:
case 7:
my_size += 4 + 4;
if (Flags.Children > 0) my_size += 4;
break;
default:
break;
}
return my_size;
}
public void Fixup(bool last_child)
{
Flags.RawInfo = 0;
Flags.Attributes = Convert.ToByte(Attributes.Count & 0xFF);
Flags.Children = Convert.ToUInt16(Nodes.Count & 0xFFFF);
if (Text.value == "?xml")
{
if (Flags.Attributes == 0)
{
// just offsets to child
Flags.unknown_1 = false;
Flags.unknown_2 = false;
Flags.ContinueSequence = false;
// raw flags are now 000
}
else
{
if (End == null)
{
End = new BMLString.Ref("\r\n", false);
}
// declaration kept - child mandatory
Flags.unknown_1 = true;
Flags.unknown_2 = false;
Flags.ContinueSequence = false;
// raw flags are now 001
}
}
else
{
if (Inner != null)
{
// has inner kept; child optional
Flags.unknown_1 = true;
Flags.unknown_2 = true;
Flags.ContinueSequence = !last_child;
// raw flags are now either 011 or 111
if (End2 == null)
{
End2 = new BMLString.Ref("\r\n", false);
}
}
else
{
// end spacing, child optional
Flags.unknown_1 = false;
Flags.unknown_2 = true;
Flags.ContinueSequence = !last_child;
// raw flags are now either 010 or 110
if (End == null)
{
End = new BMLString.Ref("\r\n", false);
}
}
}
}
public bool Write(BinaryWriter bw, UInt32 of1, UInt32 of2)
{
// text offset
Text.Fixup(of1, of2);
bw.Write(Text.offset);
// flags
Flags.Write(bw);
foreach (Attribute a in Attributes)
{
a.Name.Fixup(of1, of2);
a.Value.Fixup(of1, of2);
a.Write(bw);
}
switch (Flags.RawInfo)
{
case 0:
bw.Write(Offset);
break;
case 1:
End.Fixup(of1, of2);
bw.Write(End.offset);
bw.Write(Offset);
break;
case 2:
case 6:
End.Fixup(of1, of2);
bw.Write(End.offset);
if (Flags.Children > 0) bw.Write(Offset);
break;
case 3:
case 7:
Inner.Fixup(of1, of2);
End2.Fixup(of1, of2);
bw.Write(Inner.offset);
bw.Write(End2.offset);
if (Flags.Children > 0) bw.Write(Offset);
break;
default:
break;
}
return true;
}
}
class BMLString
{
#region Common methods for reading strings from a BinaryReader
static public string MakeCleanString(Byte[] bytes)
{
return Encoding.Default.GetString(bytes).TrimEnd('\0');
}
static public string ReadInlineNullTerminatedString(BinaryReader br)
{
List<byte> buf = new List<byte>();
for (byte b = br.ReadByte(); b != 0x0; b = br.ReadByte())
{
buf.Add(b);
}
return MakeCleanString(buf.ToArray());
}
static public string ReadNullTerminatedStringAt(BinaryReader br, UInt32 targetpos)
{
br.BaseStream.Position = targetpos;
return ReadInlineNullTerminatedString(br);
}
static public string ReadNullTerminatedString(BinaryReader br, UInt32 targetpos)
{
#if DEBUG
if (targetpos >= br.BaseStream.Length)
{
return "";
}
#endif
long pos = br.BaseStream.Position;
string str = ReadNullTerminatedStringAt(br, targetpos);
br.BaseStream.Position = pos;
return str;
}
#endregion
static private void FixupXmlEntityInternal(ref string str, string src, string dest)
{
if (str.IndexOf(src) >= 0)
{
str = str.Replace(src, dest);
}
}
static public string EncodeXml(string str)
{
string xml_str = str;
FixupXmlEntityInternal(ref xml_str, "\"", """);
FixupXmlEntityInternal(ref xml_str, "&", "&");
FixupXmlEntityInternal(ref xml_str, "'", "'");
FixupXmlEntityInternal(ref xml_str, "<", "<");
FixupXmlEntityInternal(ref xml_str, ">", ">");
return xml_str;
}
static public string DecodeXml(string xml_str)
{
string str = xml_str;
FixupXmlEntityInternal(ref str, """, "\"");
FixupXmlEntityInternal(ref str, "&", "&");
FixupXmlEntityInternal(ref str, "'", "'");
FixupXmlEntityInternal(ref str, "<", "<");
FixupXmlEntityInternal(ref str, ">", ">");
return str;
}
struct Inst
{
public string Value;
public UInt32 Offset;
public Inst(string str_value)
{
Value = str_value;
Offset = 0;
}
}
public class Cache
{
private List<Inst> Strings;
public Cache()
{
Strings = new List<Inst>();
}
public void Clear()
{
Strings.Clear();
}
public void AddString(string str)
{
if (!Strings.Exists(i => i.Value == str))
{
Strings.Add(new Inst(str));
}
}