forked from Nifyr/Fire-Emblem-Three-Houses-Randomizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextBinFormatter.cs
80 lines (72 loc) · 2.68 KB
/
TextBinFormatter.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Fire_Emblem_Three_Houses_Randomizer_V2
{
public class TextBinFormatter : TextFormatter
{
uint languageCount;
List<uint> languagePointers;
List<uint> languageSizes;
List<TextBinLanguageSection> languages;
public TextBinFormatter() { }
public TextBinFormatter(byte[] buffer)
{
int offset = 0;
languageCount = BitConverter.ToUInt32(buffer, offset);
offset += 4;
languagePointers = new List<uint>();
languageSizes = new List<uint>();
languages = new List<TextBinLanguageSection>();
for (int i = 0; i < languageCount; i++)
{
languagePointers.Add(BitConverter.ToUInt32(buffer, offset + 0));
languageSizes.Add(BitConverter.ToUInt32(buffer, offset + 4));
languages.Add(new TextBinLanguageSection(buffer, languagePointers[i]));
offset += 8;
}
}
public override byte[] getBytes()
{
fixPointers();
List<byte> bytes = new List<byte>();
bytes.AddRange(BitConverter.GetBytes(languageCount));
for (int i = 0; i < languages.Count; i++)
{
bytes.AddRange(BitConverter.GetBytes(languagePointers[i]));
bytes.AddRange(BitConverter.GetBytes(languageSizes[i]));
}
for (int i = 0; i < languages.Count; i++)
bytes.AddRange(languages[i].getBytes());
byte[] byteArray = new byte[bytes.Count];
for (int i = 0; i < byteArray.Length; i++)
byteArray[i] = bytes[i];
return byteArray;
}
private void fixPointers()
{
uint offset = 4;
for (int i = 0; i < languages.Count; i++)
offset += 8;
for (int i = 0; i < languages.Count; i++)
{
languagePointers[i] = offset;
languageSizes[i] = (uint)languages[i].getBytes().Count;
offset += languageSizes[i];
}
}
public override List<string> getStrings(bool includeInvalids)
{
return languages[1].getStrings(includeInvalids);
}
public override void setStrings(List<string> strings, bool includeInvalids)
{
List<string> strings2 = new List<string>();
strings2.AddRange(strings);
languages[1].setStrings(strings, includeInvalids);
languages[2].setStrings(strings2, includeInvalids);
}
}
}