-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMSBT.cs
1007 lines (843 loc) · 23.7 KB
/
MSBT.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
namespace MsbtEditor
{
public enum EncodingByte : byte
{
UTF8 = 0x00,
Unicode = 0x01
}
public class Header
{
public string Identifier; // MsgStdBn
public byte[] ByteOrderMark;
public UInt16 Unknown1; // Always 0x0000
public EncodingByte EncodingByte;
public byte Unknown2; // Always 0x03
public UInt16 NumberOfSections;
public UInt16 Unknown3; // Always 0x0000
public UInt32 FileSize;
public byte[] Unknown4; // Always 0x0000 0000 0000 0000 0000
public UInt32 FileSizeOffset;
}
public class Section
{
public string Identifier;
public UInt32 SectionSize; // Begins after Unknown1
public byte[] Padding1; // Always 0x0000 0000
}
public interface IEntry
{
string ToString();
string ToString(Encoding encoding);
byte[] Value { get; set; }
UInt32 Index { get; set; }
}
public class LBL1 : Section
{
public UInt32 NumberOfGroups;
public List<Group> Groups;
public List<IEntry> Labels;
}
public class Group
{
public UInt32 NumberOfLabels;
public UInt32 Offset;
}
public class Label : IEntry
{
private UInt32 _index;
public UInt32 Length;
public string Name;
public UInt32 Checksum;
public IEntry String;
public byte[] Value
{
get
{
return String.Value;
}
set
{
String.Value = value;
}
}
public UInt32 Index
{
get
{
return _index;
}
set
{
_index = value;
}
}
public override string ToString()
{
return (Length > 0 ? Name : (_index + 1).ToString());
}
public string ToString(Encoding encoding)
{
return (Length > 0 ? Name : (_index + 1).ToString());
}
}
public class NLI1 : Section
{
public byte[] Unknown2; // Tons of unknown data
}
public class ATO1 : Section
{
public byte[] Unknown2; // Large collection of 0xFF
}
public class ATR1 : Section
{
public UInt32 NumberOfAttributes;
public byte[] Unknown2; // Tons of unknown data
}
public class TSY1 : Section
{
public byte[] Unknown2; // Tons of unknown data
}
public class TXT2 : Section
{
public UInt32 NumberOfStrings;
public List<IEntry> Strings;
public List<IEntry> OriginalStrings;
}
public class String : IEntry
{
private byte[] _text;
private UInt32 _index;
public byte[] Value
{
get
{
return _text;
}
set
{
_text = value;
}
}
public UInt32 Index
{
get
{
return _index;
}
set
{
_index = value;
}
}
public override string ToString()
{
return (_index + 1).ToString();
}
public string ToString(Encoding encoding)
{
return encoding.GetString(_text);
}
}
public class InvalidMSBTException : Exception
{
public InvalidMSBTException(string message) : base(message) { }
}
public class MSBT
{
public FileInfo File { get; set; }
public Header Header = new Header();
public LBL1 LBL1 = new LBL1();
public NLI1 NLI1 = new NLI1();
public ATO1 ATO1 = new ATO1();
public ATR1 ATR1 = new ATR1();
public TSY1 TSY1 = new TSY1();
public TXT2 TXT2 = new TXT2();
public Encoding FileEncoding = Encoding.Unicode;
public List<string> SectionOrder = new List<string>();
public bool HasLabels = false;
private byte paddingChar = 0xAB;
public static UInt32 LabelMaxLength = 64;
public static string LabelFilter = @"^[a-zA-Z0-9_]+$";
public MSBT(string filename)
{
File = new FileInfo(filename);
if (File.Exists && filename.Length > 0)
{
FileStream fs = System.IO.File.Open(File.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReaderX br = new BinaryReaderX(fs);
// Initialize Members
LBL1.Groups = new List<Group>();
LBL1.Labels = new List<IEntry>();
TXT2.Strings = new List<IEntry>();
TXT2.OriginalStrings = new List<IEntry>();
// Header
Header.Identifier = br.ReadString(8);
if (Header.Identifier != "MsgStdBn")
throw new InvalidMSBTException("The file provided is not a valid MSBT file.");
// Byte Order
Header.ByteOrderMark = br.ReadBytes(2);
br.ByteOrder = Header.ByteOrderMark[0] > Header.ByteOrderMark[1] ? ByteOrder.LittleEndian : ByteOrder.BigEndian;
Header.Unknown1 = br.ReadUInt16();
// Encoding
Header.EncodingByte = (EncodingByte)br.ReadByte();
FileEncoding = (Header.EncodingByte == EncodingByte.UTF8 ? Encoding.UTF8 : Encoding.Unicode);
Header.Unknown2 = br.ReadByte();
Header.NumberOfSections = br.ReadUInt16();
Header.Unknown3 = br.ReadUInt16();
Header.FileSizeOffset = (UInt32)br.BaseStream.Position; // Record offset for future use
Header.FileSize = br.ReadUInt32();
Header.Unknown4 = br.ReadBytes(10);
if (Header.FileSize != br.BaseStream.Length)
throw new InvalidMSBTException("The file provided is not a valid MSBT file.");
SectionOrder.Clear();
for (int i = 0; i < Header.NumberOfSections; i++)
{
switch (br.PeekString())
{
case "LBL1":
ReadLBL1(br);
SectionOrder.Add("LBL1");
break;
case "NLI1":
ReadNLI1(br);
SectionOrder.Add("NLI1");
break;
case "ATO1":
ReadATO1(br);
SectionOrder.Add("ATO1");
break;
case "ATR1":
ReadATR1(br);
SectionOrder.Add("ATR1");
break;
case "TSY1":
ReadTSY1(br);
SectionOrder.Add("TSY1");
break;
case "TXT2":
ReadTXT2(br);
SectionOrder.Add("TXT2");
break;
}
}
br.Close();
}
}
// Tools
public uint LabelChecksum(string label)
{
uint group = 0;
for (int i = 0; i < label.Length; i++)
{
group *= 0x492;
group += label[i];
group &= 0xFFFFFFFF;
}
return group % LBL1.NumberOfGroups;
}
// Reading
private void ReadLBL1(BinaryReaderX br)
{
LBL1.Identifier = br.ReadString(4);
LBL1.SectionSize = br.ReadUInt32();
LBL1.Padding1 = br.ReadBytes(8);
long startOfLabels = br.BaseStream.Position;
LBL1.NumberOfGroups = br.ReadUInt32();
for (int i = 0; i < LBL1.NumberOfGroups; i++)
{
Group grp = new Group();
grp.NumberOfLabels = br.ReadUInt32();
grp.Offset = br.ReadUInt32();
LBL1.Groups.Add(grp);
}
foreach (Group grp in LBL1.Groups)
{
br.BaseStream.Seek(startOfLabels + grp.Offset, SeekOrigin.Begin);
for (int i = 0; i < grp.NumberOfLabels; i++)
{
Label lbl = new Label();
lbl.Length = Convert.ToUInt32(br.ReadByte());
lbl.Name = br.ReadString((int)lbl.Length);
lbl.Index = br.ReadUInt32();
lbl.Checksum = (uint)LBL1.Groups.IndexOf(grp);
LBL1.Labels.Add(lbl);
}
}
// Old rename correction
foreach (Label lbl in LBL1.Labels)
{
uint previousChecksum = lbl.Checksum;
lbl.Checksum = LabelChecksum(lbl.Name);
if (previousChecksum != lbl.Checksum)
{
LBL1.Groups[(int)previousChecksum].NumberOfLabels -= 1;
LBL1.Groups[(int)lbl.Checksum].NumberOfLabels += 1;
}
}
if (LBL1.Labels.Count > 0)
HasLabels = true;
PaddingSeek(br);
}
private void ReadNLI1(BinaryReaderX br)
{
NLI1.Identifier = br.ReadString(4);
NLI1.SectionSize = br.ReadUInt32();
NLI1.Padding1 = br.ReadBytes(8);
NLI1.Unknown2 = br.ReadBytes((int)NLI1.SectionSize); // Read in the entire section at once since we don't know what it's for
PaddingSeek(br);
}
private void ReadATO1(BinaryReaderX br)
{
ATO1.Identifier = br.ReadString(4);
ATO1.SectionSize = br.ReadUInt32();
ATO1.Padding1 = br.ReadBytes(8);
ATO1.Unknown2 = br.ReadBytes((int)ATO1.SectionSize); // Read in the entire section at once since we don't know what it's for
}
private void ReadATR1(BinaryReaderX br)
{
ATR1.Identifier = br.ReadString(4);
ATR1.SectionSize = br.ReadUInt32();
ATR1.Padding1 = br.ReadBytes(8);
ATR1.NumberOfAttributes = br.ReadUInt32();
ATR1.Unknown2 = br.ReadBytes((int)ATR1.SectionSize - sizeof(UInt32)); // Read in the entire section at once since we don't know what it's for
PaddingSeek(br);
}
private void ReadTSY1(BinaryReaderX br)
{
TSY1.Identifier = br.ReadString(4);
TSY1.SectionSize = br.ReadUInt32();
TSY1.Padding1 = br.ReadBytes(8);
TSY1.Unknown2 = br.ReadBytes((int)TSY1.SectionSize); // Read in the entire section at once since we don't know what it's for
PaddingSeek(br);
}
private void ReadTXT2(BinaryReaderX br)
{
TXT2.Identifier = br.ReadString(4);
TXT2.SectionSize = br.ReadUInt32();
TXT2.Padding1 = br.ReadBytes(8);
long startOfStrings = br.BaseStream.Position;
TXT2.NumberOfStrings = br.ReadUInt32();
List<UInt32> offsets = new List<UInt32>();
for (int i = 0; i < TXT2.NumberOfStrings; i++)
offsets.Add(br.ReadUInt32());
for (int i = 0; i < TXT2.NumberOfStrings; i++)
{
String str = new String();
UInt32 nextOffset = (i + 1 < offsets.Count) ? ((UInt32)startOfStrings + offsets[i + 1]) : ((UInt32)startOfStrings + TXT2.SectionSize);
br.BaseStream.Seek(startOfStrings + offsets[i], SeekOrigin.Begin);
List<byte> result = new List<byte>();
while (br.BaseStream.Position < nextOffset && br.BaseStream.Position < Header.FileSize)
{
if (Header.EncodingByte == EncodingByte.UTF8)
result.Add(br.ReadByte());
else
{
byte[] unichar = br.ReadBytes(2);
if (br.ByteOrder == ByteOrder.BigEndian)
Array.Reverse(unichar);
result.AddRange(unichar);
}
}
str.Value = result.ToArray();
str.Index = (uint)i;
TXT2.OriginalStrings.Add(str);
// Duplicate entries for editing
String estr = new String();
estr.Value = str.Value;
estr.Index = str.Index;
TXT2.Strings.Add(estr);
}
// Tie in LBL1 labels
foreach (Label lbl in LBL1.Labels)
lbl.String = TXT2.Strings[(int)lbl.Index];
PaddingSeek(br);
}
private void PaddingSeek(BinaryReaderX br)
{
long remainder = br.BaseStream.Position % 16;
if (remainder > 0)
{
paddingChar = br.ReadByte();
br.BaseStream.Seek(-1, SeekOrigin.Current);
br.BaseStream.Seek(16 - remainder, SeekOrigin.Current);
}
}
// Manipulation
public Label AddLabel(string name)
{
String nstr = new String();
nstr.Value = new byte[] { };
TXT2.Strings.Add(nstr);
String dstr = new String();
dstr.Value = nstr.Value;
TXT2.OriginalStrings.Add(dstr);
Label nlbl = new Label();
nlbl.Length = (uint)name.Trim().Length;
nlbl.Name = name.Trim();
nlbl.Index = (uint)TXT2.Strings.IndexOf(nstr);
nlbl.Checksum = LabelChecksum(name.Trim());
nlbl.String = nstr;
LBL1.Labels.Add(nlbl);
LBL1.Groups[(int)nlbl.Checksum].NumberOfLabels += 1;
ATR1.NumberOfAttributes += 1;
TXT2.NumberOfStrings += 1;
return nlbl;
}
public void RenameLabel(Label lbl, string newName)
{
lbl.Length = (uint)Encoding.ASCII.GetBytes(newName.Trim()).Length;
lbl.Name = newName.Trim();
LBL1.Groups[(int)lbl.Checksum].NumberOfLabels -= 1;
lbl.Checksum = LabelChecksum(newName.Trim());
LBL1.Groups[(int)lbl.Checksum].NumberOfLabels += 1;
}
public void RemoveLabel(Label lbl)
{
int textIndex = TXT2.Strings.IndexOf(lbl.String);
for (int i = 0; i < TXT2.NumberOfStrings; i++)
if (LBL1.Labels[i].Index > textIndex)
LBL1.Labels[i].Index -= 1;
LBL1.Groups[(int)lbl.Checksum].NumberOfLabels -= 1;
LBL1.Labels.Remove(lbl);
ATR1.NumberOfAttributes -= 1;
TXT2.Strings.RemoveAt((int)lbl.Index);
TXT2.OriginalStrings.RemoveAt((int)lbl.Index);
TXT2.NumberOfStrings -= 1;
}
// Saving
public bool Save()
{
bool result = false;
try
{
FileStream fs = System.IO.File.Create(File.FullName);
BinaryWriterX bw = new BinaryWriterX(fs);
// Byte Order
bw.ByteOrder = Header.ByteOrderMark[0] > Header.ByteOrderMark[1] ? ByteOrder.LittleEndian : ByteOrder.BigEndian;
// Header
bw.WriteASCII(Header.Identifier);
bw.Write(Header.ByteOrderMark);
bw.Write(Header.Unknown1);
bw.Write((byte)Header.EncodingByte);
bw.Write(Header.Unknown2);
bw.Write(Header.NumberOfSections);
bw.Write(Header.Unknown3);
bw.Write(Header.FileSize);
bw.Write(Header.Unknown4);
foreach (string section in SectionOrder)
{
if (section == "LBL1")
WriteLBL1(bw);
else if (section == "NLI1")
WriteNLI1(bw);
else if (section == "ATO1")
WriteATO1(bw);
else if (section == "ATR1")
WriteATR1(bw);
else if (section == "TSY1")
WriteTSY1(bw);
else if (section == "TXT2")
WriteTXT2(bw);
}
// Update FileSize
long fileSize = bw.BaseStream.Position;
bw.BaseStream.Seek(Header.FileSizeOffset, SeekOrigin.Begin);
bw.Write((UInt32)fileSize);
bw.Close();
}
catch (Exception)
{ }
return result;
}
private bool WriteLBL1(BinaryWriterX bw)
{
bool result = false;
try
{
// Calculate Section Size
UInt32 newSize = sizeof(UInt32);
foreach (Group grp in LBL1.Groups)
newSize += (UInt32)(sizeof(UInt32) + sizeof(UInt32));
foreach (Label lbl in LBL1.Labels)
newSize += (UInt32)(sizeof(byte) + lbl.Name.Length + sizeof(UInt32));
// Calculate Group Offsets
UInt32 offsetsLength = LBL1.NumberOfGroups * sizeof(UInt32) * 2 + sizeof(UInt32);
UInt32 runningTotal = 0;
for (int i = 0; i < LBL1.Groups.Count; i++)
{
LBL1.Groups[i].Offset = offsetsLength + runningTotal;
foreach (Label lbl in LBL1.Labels)
if (lbl.Checksum == i)
runningTotal += (UInt32)(sizeof(byte) + lbl.Name.Length + sizeof(UInt32));
}
// Write
bw.WriteASCII(LBL1.Identifier);
bw.Write(newSize);
bw.Write(LBL1.Padding1);
bw.Write(LBL1.NumberOfGroups);
foreach (Group grp in LBL1.Groups)
{
bw.Write(grp.NumberOfLabels);
bw.Write(grp.Offset);
}
foreach (Group grp in LBL1.Groups)
{
foreach (Label lbl in LBL1.Labels)
{
if (lbl.Checksum == LBL1.Groups.IndexOf(grp))
{
bw.Write(Convert.ToByte(lbl.Length));
bw.WriteASCII(lbl.Name);
bw.Write(lbl.Index);
}
}
}
PaddingWrite(bw);
result = true;
}
catch (Exception)
{
result = false;
}
return result;
}
private bool WriteNLI1(BinaryWriterX bw)
{
bool result = false;
try
{
bw.WriteASCII(NLI1.Identifier);
bw.Write(NLI1.SectionSize);
bw.Write(NLI1.Padding1);
bw.Write(NLI1.Unknown2);
PaddingWrite(bw);
result = true;
}
catch (Exception)
{
result = false;
}
return result;
}
private bool WriteATO1(BinaryWriterX bw)
{
bool result = false;
try
{
bw.WriteASCII(ATO1.Identifier);
bw.Write(ATO1.SectionSize);
bw.Write(ATO1.Padding1);
bw.Write(ATO1.Unknown2);
result = true;
}
catch (Exception)
{
result = false;
}
return result;
}
private bool WriteATR1(BinaryWriterX bw)
{
bool result = false;
try
{
bw.WriteASCII(ATR1.Identifier);
bw.Write(ATR1.SectionSize);
bw.Write(ATR1.Padding1);
bw.Write(ATR1.NumberOfAttributes);
bw.Write(ATR1.Unknown2);
PaddingWrite(bw);
result = true;
}
catch (Exception)
{
result = false;
}
return result;
}
private bool WriteTSY1(BinaryWriterX bw)
{
bool result = false;
try
{
bw.WriteASCII(TSY1.Identifier);
bw.Write(TSY1.SectionSize);
bw.Write(TSY1.Padding1);
bw.Write(TSY1.Unknown2);
PaddingWrite(bw);
result = true;
}
catch (Exception)
{
result = false;
}
return result;
}
private bool WriteTXT2(BinaryWriterX bw)
{
bool result = false;
try
{
// Calculate Section Size
UInt32 newSize = (UInt32)(TXT2.NumberOfStrings * sizeof(UInt32) + sizeof(UInt32));
for (int i = 0; i < TXT2.NumberOfStrings; i++)
newSize += (UInt32)((String)TXT2.Strings[i]).Value.Length;
bw.WriteASCII(TXT2.Identifier);
bw.Write(newSize);
bw.Write(TXT2.Padding1);
long startOfStrings = bw.BaseStream.Position;
bw.Write(TXT2.NumberOfStrings);
List<UInt32> offsets = new List<UInt32>();
UInt32 offsetsLength = TXT2.NumberOfStrings * sizeof(UInt32) + sizeof(UInt32);
UInt32 runningTotal = 0;
for (int i = 0; i < TXT2.NumberOfStrings; i++)
{
offsets.Add(offsetsLength + runningTotal);
runningTotal += (UInt32)((String)TXT2.Strings[i]).Value.Length;
}
for (int i = 0; i < TXT2.NumberOfStrings; i++)
bw.Write(offsets[i]);
for (int i = 0; i < TXT2.NumberOfStrings; i++)
{
for (int j = 0; j < TXT2.Strings.Count; j++)
TXT2.OriginalStrings[i] = TXT2.Strings[i];
if (Header.EncodingByte == EncodingByte.UTF8)
bw.Write(((String)TXT2.Strings[i]).Value);
else
{
if (Header.ByteOrderMark[0] == 0xFF)
bw.Write(((String)TXT2.Strings[i]).Value);
else
for (int j = 0; j < ((String)TXT2.Strings[i]).Value.Length; j += 2)
{
bw.Write(((String)TXT2.Strings[i]).Value[j + 1]);
bw.Write(((String)TXT2.Strings[i]).Value[j]);
}
}
}
PaddingWrite(bw);
result = true;
}
catch (Exception)
{
result = false;
}
return result;
}
private void PaddingWrite(BinaryWriterX bw)
{
long remainder = bw.BaseStream.Position % 16;
if (remainder > 0)
for (int i = 0; i < 16 - remainder; i++)
bw.Write(paddingChar);
}
// Tools
public string ExportToCSV(string filename)
{
string result = "Successfully exported to CSV.";
try
{
StringBuilder sb = new StringBuilder();
List<string> row = new List<string>();
IEntry ent = null;
if (HasLabels)
{
sb.AppendLine("Label,String");
for (int i = 0; i < TXT2.NumberOfStrings; i++)
{
Label lbl = (Label)LBL1.Labels[i];
ent = LBL1.Labels[i];
row.Add(ent.ToString());
row.Add("\"" + lbl.String.ToString(FileEncoding).Replace("\"", "\"\"") + "\"");
sb.AppendLine(string.Join(",", row.ToArray()));
row.Clear();
}
}
else
{
sb.AppendLine("Index,String");
for (int i = 0; i < TXT2.NumberOfStrings; i++)
{
String str = (String)TXT2.Strings[i];
ent = LBL1.Labels[i];
row.Add((ent.Index + 1).ToString());
row.Add("\"" + str.ToString(FileEncoding).Replace("\"", "\"\"") + "\"");
sb.AppendLine(string.Join(",", row.ToArray()));
row.Clear();
}
}
FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(new byte[] { 0xEF, 0xBB, 0xBF });
bw.Write(sb.ToString().ToCharArray());
bw.Close();
}
catch (IOException ioex)
{
result = "File Access Error - " + ioex.Message;
}
catch (Exception ex)
{
result = "Unknown Error - " + ex.Message;
}
return result;
}
public string ExportXMSBT(string filename, bool overwrite = true)
{
string result = "Successfully exported to XMSBT.";
try
{
if (!System.IO.File.Exists(filename) || (System.IO.File.Exists(filename) && overwrite))
{
if (HasLabels)
{
XmlDocument xmlDocument = new XmlDocument();
XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Encoding = FileEncoding;
xmlSettings.Indent = true;
xmlSettings.IndentChars = "\t";
xmlSettings.CheckCharacters = false;
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", FileEncoding.WebName, null);
xmlDocument.AppendChild(xmlDeclaration);
XmlElement xmlRoot = xmlDocument.CreateElement("xmsbt");
xmlDocument.AppendChild(xmlRoot);
foreach (Label lbl in LBL1.Labels)
{
XmlElement xmlEntry = xmlDocument.CreateElement("entry");
xmlRoot.AppendChild(xmlEntry);
XmlAttribute xmlLabelAttribute = xmlDocument.CreateAttribute("label");
xmlLabelAttribute.Value = lbl.Name;
xmlEntry.Attributes.Append(xmlLabelAttribute);
XmlElement xmlString = xmlDocument.CreateElement("text");
xmlString.InnerText = FileEncoding.GetString(lbl.String.Value).Replace("\n", "\r\n").TrimEnd('\0').Replace("\0", @"\0");
xmlEntry.AppendChild(xmlString);
}
System.IO.StreamWriter stream = new StreamWriter(filename, false, FileEncoding);
xmlDocument.Save(XmlWriter.Create(stream, xmlSettings));
stream.Close();
}
else
{
result = "XMSBT does not currently support files without an LBL1 section.";
}
}
else
{
result = filename + " already exists and overwrite was set to false.";
}
}
catch (Exception ex)
{
result = "Unknown Error - " + ex.Message;
}
return result;
}
public string ImportXMSBT(string filename, bool addNew = false)
{
string result = "Successfully imported from XMSBT.";
try
{
if (HasLabels)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(filename);
XmlNode xmlRoot = xmlDocument.SelectSingleNode("/xmsbt");
Dictionary<string, Label> labels = new Dictionary<string, Label>();
foreach (Label lbl in LBL1.Labels)
labels.Add(lbl.Name, lbl);
foreach (XmlNode entry in xmlRoot.SelectNodes("entry"))
{
string label = entry.Attributes["label"].Value;
byte[] str = FileEncoding.GetBytes(entry.SelectSingleNode("text").InnerText.Replace("\r\n", "\n").Replace(@"\0", "\0") + "\0");
if (labels.ContainsKey(label))
labels[label].String.Value = str;
else if (addNew)
{
if (label.Trim().Length <= MSBT.LabelMaxLength && Regex.IsMatch(label.Trim(), MSBT.LabelFilter))
{
Label lbl = AddLabel(label);
lbl.String.Value = str;
}
}
}
}
else
{
result = "XMSBT does not currently support files without an LBL1 section.";
}
}
catch (Exception ex)
{
result = "Unknown Error - " + ex.Message;
}
return result;
}
public string ExportXMSBTMod(string outFilename, string sourceFilename, bool overwrite = true)
{
string result = "Successfully exported to XMSBT.";
try
{
if (!System.IO.File.Exists(outFilename) || (System.IO.File.Exists(outFilename) && overwrite))
{
if (HasLabels)
{
XmlDocument xmlDocument = new XmlDocument();
XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Encoding = FileEncoding;
xmlSettings.Indent = true;
xmlSettings.IndentChars = "\t";
xmlSettings.CheckCharacters = false;
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", FileEncoding.WebName, null);
xmlDocument.AppendChild(xmlDeclaration);
XmlElement xmlRoot = xmlDocument.CreateElement("xmsbt");
xmlDocument.AppendChild(xmlRoot);
MSBT source = new MSBT(sourceFilename);
foreach (Label lbl in LBL1.Labels)
{
bool export = true;
foreach (Label lblSource in source.LBL1.Labels)
if (lbl.Name == lblSource.Name && lbl.String.Value.SequenceEqual(lblSource.String.Value))
{
export = false;
break;
}
if (export)
{
XmlElement xmlEntry = xmlDocument.CreateElement("entry");
xmlRoot.AppendChild(xmlEntry);
XmlAttribute xmlLabelAttribute = xmlDocument.CreateAttribute("label");
xmlLabelAttribute.Value = lbl.Name;
xmlEntry.Attributes.Append(xmlLabelAttribute);
XmlElement xmlString = xmlDocument.CreateElement("text");
xmlString.InnerText = FileEncoding.GetString(lbl.String.Value).Replace("\n", "\r\n").TrimEnd('\0').Replace("\0", @"\0");
xmlEntry.AppendChild(xmlString);
}
}
System.IO.StreamWriter stream = new StreamWriter(outFilename, false, FileEncoding);
xmlDocument.Save(XmlWriter.Create(stream, xmlSettings));
stream.Close();
}
else
{
result = "XMSBT does not currently support files without an LBL1 section.";
}
}
else
{
result = outFilename + " already exists and overwrite was set to false.";
}
}
catch (Exception ex)
{