-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocHelper.cs
955 lines (892 loc) · 35.1 KB
/
LocHelper.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
* Loc files are not terribly complex.
* They are ucfb fiels with file type, name and body.
* Each of the strings are layed out as:
* string_id_hash (4 bytes)
* 2 byte number ( total size of the info needed fof the string [string, pointer, size])
* string_content (2 byte characters)
* termination sequence of 32-bit '0' (ie 00000000)
*/
namespace LVLTool
{
/// <summary>
/// class for localization operations.
/// </summary>
public class LocHelper
{
private Dictionary<uint, string> mStringSet = null;
private Dictionary<uint, string> mStringSet2 = null;
private byte[] BodyBytes = { 0x42, 0x4f, 0x44, 0x59 };
private uint BodyStart
{
get
{
uint retVal = (uint)BinUtils.GetLocationOfGivenBytes(24, BodyBytes, mData, 20);
retVal += 8;
return retVal;
}
}
public byte[] GetUcfbData()
{
MakeUcfb(mData, BodyEnd);
byte[] retVal = mData.ToArray();
return retVal;
}
/// <summary>
/// The entire data of the loc resource file
/// </summary>
private List<byte> mData;
public byte[] GetRawData()
{
return mData.ToArray();
}
private int mBodySizeLoc = -1;
private int BodySizeLoc
{
get
{
//if (mBodySizeLoc == -1)
{
byte[] bodyStr = new byte[] { 0x42, 0x4f, 0x44, 0x59 };//"BODY"
long loc = BinUtils.GetLocationOfGivenBytes(0x10, bodyStr, mData, 0x30);
mBodySizeLoc = (int)(loc + 4);
}
return mBodySizeLoc;
}
}
private uint BodyEnd
{
get
{
uint bodyLen = BinUtils.GetNumberAtLocation(BodySizeLoc, mData);
uint retVal = (uint)(BodySizeLoc + bodyLen);
return retVal;
}
}
private void AddBodySize(int size)
{
uint bodySize = BinUtils.GetNumberAtLocation(BodySizeLoc, mData);
bodySize = (uint)(bodySize + size);
BinUtils.WriteNumberAtLocation(BodySizeLoc, bodySize, mData);
}
public LocHelper(byte[] data)
{
mData = new List<byte>( data.Length + 1024);
mData.AddRange(data);
}
internal static void MakeUcfb(List<byte> data, uint bodyEnd)
{
// trim
int end = (int) bodyEnd;
for (int i = data.Count - 1; i > end; i--)
data.RemoveAt(i);
// add UCFB header
if (data[0] != (byte)'u')
{
data.Insert(0, 0);
data.Insert(0, 0);
data.Insert(0, 0);
data.Insert(0, 0);
data.Insert(0, 0);
data.Insert(0, 0);
data.Insert(0, 0);
data.Insert(0, 0);
data[0] = (byte)'u';
data[1] = (byte)'c';
data[2] = (byte)'f';
data[3] = (byte)'b';
}
// add bytes at the end (if necessary)
int rem = (data.Count - 1) % 4;
switch (rem)
{
case 0: // add 3
data.Add(0);
data.Add(0);
data.Add(0);
break;
case 1: // add 2
data.Add(0);
data.Add(0);
break;
case 2: // add 1
data.Add(0);
break;
}
int sz = data.Count - 8;
BinUtils.WriteNumberAtLocation(4, (uint)sz, data);// ucfb size
BinUtils.WriteNumberAtLocation(0xc, (uint)(sz - 8), data); // Locl size
}
/// <summary>
/// Add string to the end of the loc file
/// </summary>
/// <param name="stringId"></param>
/// <param name="content"></param>
public void AddString(string stringId, string content)
{
uint hashId = 0;
if (stringId.Length > 2 && stringId[0] == '0' && stringId[1] == 'x')
{
try
{
hashId = UInt32.Parse(stringId.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
catch (Exception e)
{
Console.WriteLine("Error! invalid string key:'{0}'", stringId);
throw e;
}
}
else
hashId = HashHelper.HashString(stringId);
UInt16 sz = (UInt16)((content.Length * 2) + 2 + 4 + 4); // 2:size info 4:hashId 4 min zero bytes
bool sixZeros = false;
if (sz % 4 != 0)
{
sz += (UInt16)2;
sixZeros = true;
}
if (BodyEnd + sz > mData.Count)
{
AddBytes( (int)(BodyEnd + sz ) - mData.Count);
}
int stringLoc = (int)(BodyEnd - 4);
//write hashId
BinUtils.WriteNumberAtLocation(stringLoc, hashId, mData);
// write the size
BinUtils.Write2ByteNumberAtLocation(stringLoc + 4, sz, mData);
// lay down string
stringLoc += 6; /** MODIFYING 'stringLoc' NOW!! **/
for (int i = 0; i < content.Length; i++)
{
BinUtils.Write2ByteNumberAtLocation(stringLoc, content[i], mData);
stringLoc += 2;
}
BinUtils.WriteNumberAtLocation(stringLoc, 0, mData);
if (sixZeros)
BinUtils.Write2ByteNumberAtLocation(stringLoc + 4, 0, mData);
AddBodySize(sz);
}
/// <summary>
/// String size seem to be always be a multiple of 4. With 4-6 'zero bytes' at the end
/// </summary>
/// <param name="stringId">the id of the string to modify</param>
/// <param name="newValue">the new value</param>
/// <returns>The string location, -1 if not found</returns>
public int SetString(string stringId, string newValue)
{
uint hashId = GetHashId(stringId);
StringBuilder sb = new StringBuilder(20);
int stringLoc = GetString(hashId, sb);
string prevValue = sb.ToString();
if (stringLoc > 0 && prevValue != newValue)
{
UInt32 oldSz = BinUtils.Get2ByteNumberAtLocation(stringLoc + 4, mData);
UInt16 sz = (UInt16)((newValue.Length * 2) + 2 + 4 + 4); // 2:size info 4:hashId 4 min zero bytes
if (sz % 4 != 0)
sz += (UInt16)2;
bool sixZeros = false;
int diff = (int)(sz - oldSz); //2 * (newValue.Length - prevValue.Length);
if (diff % 4 != 0)
{
diff = diff + 2;
sixZeros = true;
}
int dataEnd = mData.Count + diff;
if (diff > 0)
{
AddBytes(diff);
ShiftDataDown(stringLoc, diff, dataEnd);
}
else if (diff < 0)
{
ShiftDataUp(stringLoc, -1* diff, dataEnd);
}
//write hashId
BinUtils.WriteNumberAtLocation(stringLoc, hashId, mData);
// write the size
BinUtils.Write2ByteNumberAtLocation(stringLoc + 4,sz, mData);
// lay down string
stringLoc += 6; /** MODIFYING 'stringLoc' NOW!! **/
for (int i = 0; i < newValue.Length; i++)
{
BinUtils.Write2ByteNumberAtLocation(stringLoc, newValue[i], mData);
stringLoc += 2;
}
BinUtils.WriteNumberAtLocation(stringLoc, 0, mData);
if( sixZeros )
BinUtils.Write2ByteNumberAtLocation(stringLoc+4, 0, mData);
AddBodySize(diff);
}
return stringLoc;
}
private void AddBytes(int numBytes)
{
for (int i = 0; i < numBytes; i++)
mData.Add(0);
}
public void SetByte(int loc, byte b)
{
mData[loc] = b;
}
private void ShiftDataDown(int startIndex, int amount, int dataEnd)
{
for (int i = dataEnd - amount; i > startIndex; i--)
{
SetByte(i, mData[i - amount]); // for debugging
//mData[i] = mData[i - amount]; // for speed
}
}
private void ShiftDataUp(int startIndex, int amount, int dataEnd)
{
for (int i = startIndex; i < dataEnd; i++)
{
SetByte(i, mData[i + amount]); // for debugging
//mData[i] = mData[i + amount]; // for speed
}
}
public string GetAllStrings()
{
/*bool debug = true;
UInt16 prevSz = 0;
int prevLoc = 0; debug stuff*/
mStringSet = new Dictionary<uint, string>(2000);
mStringSet2 = new Dictionary<uint, string>(200);
StringBuilder cur = new StringBuilder(80);
StringBuilder sb = new StringBuilder(50*3200 );
int stringLoc = (int)BodyStart;
UInt32 currentHash = 0;
UInt16 sz = 0;
int byteStart =0;
int byteEnd = 0;
char current = '\0';
string stringId = "";
while ( ( currentHash = BinUtils.GetNumberAtLocation(stringLoc, mData)) > 0 )
{
stringId = HashHelper.GetStringFromHash(currentHash);
if (stringId == null)
stringId = String.Format("0x{0:x6}", currentHash);
sb.Append(stringId);
sb.Append("=\"");
sz = BinUtils.Get2ByteNumberAtLocation(stringLoc + 4, mData);
//Console.WriteLine("GetAllStrings: Size={0}", sz);
byteStart = stringLoc + 6;
byteEnd = stringLoc + sz -1;
current = '\0';
for (int i = byteStart; i < byteEnd; i += 2)
{
current = (char)BinUtils.Get2ByteNumberAtLocation(i, mData);
if (current == '"')
{
sb.Append("\\\""); // escape the quote
cur.Append("\\\"");
}
else if (current == '\\')
{
sb.Append("\\\\"); // escape the escape!
cur.Append("\\\\");
}
else if (current != '\0')
{
sb.Append(current);
cur.Append(current);
}
}
if (currentHash != 0xffffffff)
{
if (mStringSet.ContainsKey(currentHash))
{
if (!mStringSet2.ContainsKey(currentHash))
mStringSet2.Add(currentHash, cur.ToString());
else
Console.WriteLine("Sorry, not showing 3+ instances of string 0x{0:x}:'{1}'", currentHash, cur.ToString() );
//Console.WriteLine("Error! key 0x{0:x} ({1}) already exists as: {2}", currentHash, stringId, mStringSet[currentHash]);
//Console.WriteLine("Cannot add {0}:{1}",stringId, cur.ToString() );
}
else
mStringSet.Add(currentHash, cur.ToString());
}
/*if (debug && cur.Length == 0)
Console.WriteLine("MT, sz:0x{0:x2} pos:0x{1:x6} prevSz:0x{2:x2} prevPos:0x{3:x6}", sz, stringLoc, prevSz, prevLoc);
prevLoc = stringLoc; // TODO remove these 2 after debugging
prevSz = sz;
*/
cur.Length = 0; // clear
sb.Append("\"\n");
stringLoc = NextStringLoc(stringLoc);
if (stringLoc + 10 > mData.Count) // for a string you need minimum 4 bytes for the hash, 2 for size and 4 for nulls
break;
}
//Console.WriteLine("mStringSet2.Count:{0}",mStringSet2.Count);
return sb.ToString();
}
/// <summary>
/// returns empty string when cant find string.
/// </summary>
/// <param name="stringId"></param>
/// <returns></returns>
public string GetString(string stringId)
{
uint hash = GetHashId(stringId);
StringBuilder builder = new StringBuilder(50);
GetString(hash, builder);
return builder.ToString();
}
internal static uint GetHashId(string stringId)
{
uint hash = 0;
if (stringId.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
{
try
{
hash = UInt32.Parse(stringId.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
catch (Exception) { }
}
else
hash = HashHelper.HashString(stringId);
return hash;
}
/// <summary>
/// Places the string with the given hashid into the stringbuilder passed in.
/// String ids can be present multiple times. This will return the last occurance
/// of the string.
/// </summary>
/// <param name="hashId"></param>
/// <param name="sb">string will be added to the builder</param>
/// <returns>The location of the string id, -1 if not found </returns>
public int GetString(UInt32 hashId, StringBuilder sb)
{
int stringLoc = (int)BodyStart;
int occurance = 0;
int target_occurance = 1;
if (mStringSet2 != null && mStringSet2.ContainsKey(hashId))
target_occurance++;
UInt32 currentHash = 0;
while (true)
{
currentHash = BinUtils.GetNumberAtLocation(stringLoc, mData);
if (currentHash == hashId)
{
occurance++;
if( occurance == target_occurance)
break;
}
stringLoc = NextStringLoc(stringLoc);
if (stringLoc + 10 > mData.Count)
return -1;
}
UInt16 sz = BinUtils.Get2ByteNumberAtLocation(stringLoc + 4, mData);
int byteStart = stringLoc + 6;
int byteEnd = stringLoc + sz -1;
char current = '\0';
for (int i = byteStart; i < byteEnd; i += 2)
{
current = (char)BinUtils.Get2ByteNumberAtLocation(i, mData);
if (current != '\0')
sb.Append(current);
}
return stringLoc;
}
public void DisableString(string stringId, int target_occurance)
{
UInt32 hashId = GetHashId(stringId);
int location = GetStringLoc(hashId, target_occurance);
if (location > -1)
{
BinUtils.WriteNumberAtLocation(location, 0xFFFFFFFF, mData);
}
else
{
Console.WriteLine("Could not find stringId: {0}", stringId);
}
}
/// <summary>
/// Returns the position of the given string id.
/// </summary>
/// <param name="hashId"></param>
/// <param name="target_occurance">1 for the first occurance</param>
/// <returns></returns>
public int GetStringLoc(UInt32 hashId, int target_occurance)
{
int stringLoc = (int)BodyStart;
int occurance = 0;
UInt32 currentHash = 0;
while (true)
{
currentHash = BinUtils.GetNumberAtLocation(stringLoc, mData);
if (currentHash == hashId)
{
occurance++;
if (occurance == target_occurance)
break;
}
stringLoc = NextStringLoc(stringLoc);
if (stringLoc + 10 > mData.Count)
return -1;
}
return stringLoc;
}
private int NextStringLoc(int stringLoc)
{
UInt16 sz = BinUtils.Get2ByteNumberAtLocation(stringLoc + 4, mData);
int retVal = sz + stringLoc;
return retVal;
}
public void SaveToUcfbFile(string filename)
{
// for debugging
System.IO.File.WriteAllBytes(filename, GetUcfbData());
}
private static string GetKey(int pos, string text)
{
string retVal = null;
StringBuilder sb = new StringBuilder(30);
string skipThese = "\r\n\" ";
for (int i = pos; i < text.Length; i++)
{
if (text[i] == '=')
break;
if(!skipThese.Contains(text[i]))
sb.Append(text[i]);
}
retVal = sb.ToString().Trim();
return retVal;
}
private static string ParseStringValue(int pos, string text)
{
string retVal = null;
StringBuilder sb = new StringBuilder(30);
int index = text.IndexOf('\"', pos);
char prev = '\0';
if (index > -1)
{
for (int i = index + 1; i < text.Length; i++)
{
if (text[i] == '"' && prev != '\\')
break;
else if (text[i] == '\\' && prev == '\\')
prev = ' ';// the escape has been escaped (this is kindof a hack for the string "\")
else
{
sb.Append(text[i]);
prev = text[i];
}
}
retVal = sb.ToString();
}
return retVal;
}
/// <summary>
/// Go through the text and only add the strings that are not present already.
/// </summary>
/// <param name="text"></param>
internal void AddNewStrings(string text)
{
ApplyText(text, true, true);
}
public void ApplyText(string text)
{
ApplyText(text, false, false);
}
/// <summary>
/// Adds/replaces the strings in the current loc file.
/// </summary>
/// <param name="text">The text to apply</param>
/// <param name="addOnly">When true, do not modify existing strings; just add the new ones.</param>
public void ApplyText(string text, bool addOnly, bool skipEmptyStrings)
{
GetAllStrings();
int pos = 0;
string key = "";
string value = "";
Dictionary<string, string> stringsToAdd = new Dictionary<string, string>();
Dictionary<string, string> stringsToSet = new Dictionary<string, string>();
uint stringId = 0;
while (pos < text.Length)
{
key = GetKey(pos, text);
if (String.IsNullOrEmpty(key))
{
pos++;
continue;
}
if (key.Length > 2 && key[0] == '0' && key[1] == 'x')
{
try
{
stringId = UInt32.Parse(key.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
catch(Exception e)
{
Console.WriteLine("Error! invalid string key:'{0}'", key);
throw e;
}
}
else
stringId = HashHelper.HashString(key);
pos += (key.Length);
value = ParseStringValue(pos, text);
if (value == null)
{
pos++;
Console.WriteLine("Warning! No value found for key:'{0}'", key);
continue;
}
if (mStringSet.ContainsKey(stringId))
{
if (mStringSet[stringId] != value)
{
if (stringsToSet.ContainsKey(key))
stringsToSet[key] = value;
else
stringsToSet.Add(key, value);
}
}
else
{
if( stringsToAdd.ContainsKey(key))
stringsToAdd[key] = value;
else
stringsToAdd.Add(key, value);
}
pos += value.Length;
// advance to next line
pos = text.IndexOf('\n', pos);
if (pos == -1)
break;
pos++;// advance past new line
}
if (!addOnly)
{
foreach (string k in stringsToSet.Keys)
SetString(k, stringsToSet[k]);
}
List<string> hashThese = new List<string>();
foreach (string k in stringsToAdd.Keys)
{
if (skipEmptyStrings && stringsToAdd[k] == String.Empty)
{
// do not include empty strings
}
else
{
hashThese.Add(k);
AddString(k, stringsToAdd[k]);
if (Program.Verbose)
Console.WriteLine("adding string:'[{0}]: '{1}'", k, stringsToAdd[k]);
}
}
if (hashThese.Count > 0)
HashHelper.AddStringsToDictionary(hashThese);
}
/// <summary>
/// Expects text like in the 'AddStringsExample.txt' file
/// </summary>
/// <param name="text"></param>
/// <param name="errors"></param>
/// <returns>Returns the binary string data, is not in UCFB format.</returns>
public static List<byte> GetBinaryLocData(string text, List<string> errors)
{
List<byte> retVal = new List<byte>();
List<LocalizedString> myStrings = ParseStrings(text, errors);
foreach (LocalizedString element in myStrings)
{
retVal.AddRange(element.GetLocBytes());
}
return retVal;
}
public static List<LocalizedString> ParseStrings(string text, List<string> errors)
{
List<LocalizedString> retVal = new List<LocalizedString>(2000);
int pos = 0;
string key = "";
string value = "";
while (pos < text.Length)
{
key = GetKey(pos, text);
if (String.IsNullOrEmpty(key))
{
pos++;
continue;
}
if (key.Contains('"') || key.Contains('\n'))
{
Console.WriteLine("ParseStrings error! Key = " + key );
}
pos += (key.Length);
value = ParseStringValue(pos, text);
if (value == null)
{
pos++;
errors.Add(string.Format("Warning! No value found for key:'{0}'", key));
continue;
}
retVal.Add(new LocalizedString(key, value)); // add the string
pos += value.Length;
// advance to next line
pos = text.IndexOf('\n', pos);
if (pos == -1)
break;
pos++;// advance past new line
}
return retVal;
}
public string GetStringsAsCfg(List<string> errors)
{
string input = GetAllStrings();
List<LocalizedString> stringList = ParseStrings(input, errors);
StringBuilder sb = new StringBuilder(input.Length);
string currentScope = "";
string lastScope = "";
sb.Append("DataBase()\n{\n");
for (int i = 0; i < stringList.Count; i++)
{
if (stringList[i].StringId.IndexOf("0x") == -1)
{
currentScope = GetStringScope(stringList[i].StringId);
if (!currentScope.Equals(lastScope, StringComparison.CurrentCultureIgnoreCase))
{
AppendScope(currentScope, lastScope, sb);
}
AppendString(stringList[i], sb);
lastScope = currentScope;
}
else {
string errorMsg = String.Format("Skipping {0}=\"{1}\"\n", stringList[i].StringId, stringList[i].Content);
errors.Add(errorMsg);
Console.Write(errorMsg);
}
}
string[] parts = lastScope.Split(".".ToCharArray());
for (int j = parts.Length-1; j > -1; j--)
{
sb.Append(' ', 2+ j*2);
sb.Append("}\n");
}
sb.Append("}");
return sb.ToString();
}
private void AppendString(LocalizedString localizedString, StringBuilder builder)
{
int size = 4 + 2 * localizedString.Content.Length;
int index = localizedString.StringId.LastIndexOf('.');
string id = localizedString.StringId.Substring(index + 1);
string[] parts = localizedString.StringId.Split(".".ToCharArray());
string indent = new string (' ', parts.Length * 2);
string indentInner = indent + " ";
builder.Append(indent);
builder.Append("VarBinary(\"");
builder.Append(id);
builder.Append("\")\n");
builder.Append(indent);
builder.Append("{\n");
builder.Append(indentInner);
builder.Append("Size(");
builder.Append(size);
builder.Append(");\n");
builder.Append(indentInner);
builder.Append("Value(\"");
string result = GetCrazyPrefix(localizedString.Content) + ConvertToStupidFormat(localizedString.Content);
for (int i = 0; i < result.Length; i++)
{
if (i != 0 && i % 64 == 0)
{
builder.Append("\");\n"); // finish off prev
builder.Append(indentInner);
builder.Append("Value(\""); // start new one
}
builder.Append(result[i]);
}
builder.Append("\");\n");
builder.Append(indent);
builder.Append("}\n");
}
private string ConvertToStupidFormat(string input)
{
StringBuilder sb = new StringBuilder();
string current = "";
string flipped = "";
foreach (char c in input)
{
current = String.Format("{0:X4}", (uint)c);
flipped = "" + current[3] + current[2] + current[1] + current[0];
sb.Append(flipped);
}
return sb.ToString();
}
private string GetCrazyPrefix(string input)
{
string len = string.Format("{0}00000000", 2 * input.Length);
len = len.Substring(0, 8);
return len;
}
private void AppendScope(string currentScope, string lastScope, StringBuilder sb)
{
string[] currentParts = currentScope.Split(".".ToCharArray());
string[] lastParts = lastScope.Split(".".ToCharArray());
int i = 0;
i = 0;
while (currentParts.Length > i && lastParts.Length > i && lastParts[i] == currentParts[i])
i++;
string currentWord = "";
string lastWord = "";
if (lastScope.Length > 0)
{
for (int j = lastParts.Length - 1; j > -1; j--)
{
lastWord = lastParts[j];
lastWord = String.Join(".", lastParts, 0, j+1);
if (currentParts.Length > j)
{
currentWord = currentParts[j];
currentWord = String.Join(".", currentParts, 0, j+1);
}
else
currentWord = "";
if (!currentWord.Equals(lastWord, StringComparison.CurrentCultureIgnoreCase))
{
sb.Append(' ', 2 + i * 2);
sb.Append("}\n");
}
}
}
string indent = "";
for (; i < currentParts.Length; i++)
{
indent = new string(' ',(i+1)*2);
sb.Append(String.Format("{0}VarScope(\"{1}\")\n", indent, currentParts[i]));
sb.Append(String.Format("{0}{{\n",indent));
}
}
private string GetStringScope(string p)
{
int index = p.LastIndexOf('.');
if (index > -1)
{
string retVal = p.Substring(0, index );
return retVal;
}
return null;
}
public static string GetVarBinaryObjectPaths(string input)
{
var builder = new StringBuilder();
var trimmedLine = "";
var lastLine = "";
var lines = input.Split("\n".ToCharArray());
List<string> dudes = new List<string>(6);
int braceCount = 0;
for(int i = 0; i < lines.Length; i++)
{
trimmedLine = lines[i].Trim();
if (trimmedLine == "{")
braceCount++;
else if (trimmedLine == "}")
{
braceCount--;
if(dudes.Count > 0)
dudes.RemoveAt(dudes.Count - 1);
}
else if (trimmedLine.StartsWith("VarScope(\""))
{
var scopeName = trimmedLine
.Substring("VarScope(\"".Length)
.TrimEnd(new char[] { '\"', ')' });
dudes.Add(scopeName);
}
else if (trimmedLine.StartsWith("VarBinary(\""))
{
try
{
var varName = trimmedLine
.Substring("VarBinary(\"".Length)
.Split(new char[] { '\"', ')' }, StringSplitOptions.RemoveEmptyEntries)
.First();
dudes.Add(varName);
builder.Append(String.Join(".", dudes.ToArray()));
builder.Append("\n");
}
catch { }
}
lastLine = trimmedLine;
}
return builder.ToString();
}
private static void AdvanceToLine(string[] lines, ref int index, string line)
{
for (; index < lines.Length; index++)
{
if (lines[index].Trim() == line)
break;
}
}
/*
* string_id_hash (4 bytes)
* 2 byte number ( total size of the info needed for the string [string, pointer, size])
* string_content (2 byte characters)
* termination sequence of 32-bit '0' (ie 00000000)
*/
}
public class LocalizedString
{
public string StringId { get; set; }
public string Content { get; set; }
public LocalizedString(string id, string content)
{
this.StringId = id;
this.Content = content;
}
public List<byte> GetLocBytes()
{
/* special cases
if (current == '"')
{
sb.Append("\\\""); // escape the quote
cur.Append("\\\"");
}
else if (current == '\\')
{
sb.Append("\\\\"); // escape the escape!
cur.Append("\\\\");
}
*/
string string_data = Content.Replace("\\\\", "\\").Replace("\\\"", "\"").Replace("\r\n", "\n").Replace("\n", "\r\n");
if (string_data == "")
string_data = " ";
uint hashId = LocHelper.GetHashId(StringId);
UInt16 sz = (UInt16)((string_data.Length * 2) + 2 + 4 + 4); // 2:size info 4:hashId 4 min zero bytes
if (sz % 4 != 0)
sz += (UInt16)2;
//if (string_data == "") sz = 0x10; // empty string hack
bool sixZeros = false; // TODO: figure out how to tell if we use this.
List<byte> mData = new List<byte>( new byte[sz]);
//write hashId
int stringLoc = 0;
BinUtils.WriteNumberAtLocation(stringLoc, hashId, mData);
// write the size
BinUtils.Write2ByteNumberAtLocation(stringLoc + 4, sz, mData);
// lay down string
stringLoc += 6; /** MODIFYING 'stringLoc' NOW!! **/
for (int i = 0; i < string_data.Length; i++)
{
BinUtils.Write2ByteNumberAtLocation(stringLoc, string_data[i], mData);
stringLoc += 2;
}
BinUtils.WriteNumberAtLocation(stringLoc, 0, mData);
if (sixZeros)
BinUtils.Write2ByteNumberAtLocation(stringLoc + 4, 0, mData);
return mData;
}
}
}
/*
* what should we do?
* 1. re-create .cfg files? (may not have all string ids for this to be possible)
* 2. Add strings if they don't exist - yes
* 3. modify strings if they are different - yes
*/