forked from marcussacana/ThreeHousesSlave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextSFile.cs
More file actions
164 lines (117 loc) · 4.83 KB
/
Copy pathTextSFile.cs
File metadata and controls
164 lines (117 loc) · 4.83 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
using AdvancedBinary;
namespace ThreeHousesSlave
{
class TextSFile
{
struct TextSHeader
{
public uint unk1;
public uint unk2;
public uint unk3;
public uint textPointerLength;
public uint count;
public uint unk4;
public uint unk5;
public uint unk6;
[RArray(nameof(count))]
public uint[] textpointers;
public uint textTableSize;
}
byte[] Script;
public TextSFile(byte[] Script)
{
this.Script = Script;
}
public string[] Import()
{
using MemoryStream Stream = new MemoryStream(Script);
StructReader Reader = new StructReader(Stream);
if (!IsValid(Stream))
throw new FileLoadException();
Stream.Position = 0;
var header = new TextSHeader();
Reader.ReadStruct(ref header);
var PaddingSize = header.textPointerLength - (header.count + 1) * 4;
var StringOffset = Stream.Position + PaddingSize;
var Strings = new List<string>();
for (int i = 0; i < header.textpointers.Length; i++)
{
Stream.Position = header.textpointers[i] + StringOffset;
Strings.Add(Reader.ReadString(StringStyle.CString));
}
return Strings.ToArray();
}
public byte[] Export(string[] Strings)
{
using MemoryStream Stream = new MemoryStream(Script);
StructReader Reader = new StructReader(Stream);
if (!IsValid(Stream))
throw new FileLoadException();
Stream.Position = 0;
var header = new TextSHeader();
Reader.ReadStruct(ref header);
var PaddingSize = header.textPointerLength - (header.count + 1) * 4;
var StringOffset = Stream.Position + PaddingSize;
using MemoryStream OutStream = new MemoryStream();
StructWriter Writer = new StructWriter(OutStream);
for (int i = 0; i < header.textpointers.Length; ++i)
{
header.textpointers[i] = (uint)Writer.Position;
Writer.WriteString(Strings[i], StringStyle.CString);
}
var PaddingData = new byte[PaddingSize];
for (int i = 0; i < PaddingData.Length; i++)
PaddingData[i] = 0xDD;
Writer.Flush();
var StringBuffer = OutStream.ToArray();
header.textTableSize = (uint)StringBuffer.LongLength;
OutStream.Position = 0;
Writer.WriteStruct(ref header);
Writer.Write(PaddingData);
Writer.Write(StringBuffer);
Writer.Flush();
return OutStream.ToArray();
}
public static bool IsValid(Stream stream)
{
try
{
var header = new TextSHeader();
StructReader Reader = new StructReader(stream);
stream.Position = 12;
uint PtrLen = Reader.ReadUInt32();
uint Count = Reader.ReadUInt32();
var PaddingSize = PtrLen - (Count + 1) * 4;
if (PtrLen != (Count + 1 + (PaddingSize/4)) * 4)
return false;
if (PtrLen == Count || PtrLen == 0 || PtrLen > stream.Length - 16 || Count * 4 > stream.Length)
return false;
stream.Position = 0;
Reader.ReadStruct(ref header);
var StringOffset = stream.Position + PaddingSize;
if (header.textpointers.First() != 0)
return false;
long CurrentSize = 0;
var Offset = Reader.Position;
Reader.Position = StringOffset;
for (var i = 0; i < header.textpointers.Length; i++)
{
bool IsLast = i + 1 >= header.textpointers.Length;
var Pointer = header.textpointers[i];
if (Pointer != CurrentSize || Pointer > stream.Length)
return false;
var CurrentOffset = Reader.Position;
_ = Reader.ReadString(StringStyle.CString);
CurrentSize += Reader.Position - CurrentOffset;
if (IsLast && Reader.Read(new byte[1], 0, 1) != 0)
return false;
}
Reader.Position = Offset;
return true;
}
catch {
return false;
}
}
}
}