-
Notifications
You must be signed in to change notification settings - Fork 1
/
sceHeader.cs
212 lines (199 loc) · 9.43 KB
/
sceHeader.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
using StreamFAdd;
using System;
using System.Collections.Generic;
namespace sceWork
{
internal class sceHeader
{
public uint offsetScript;
public uint offsetStrings;
public List<sceStrings> fileStrings;
public List<int> lineNumberList;
public List<string> plainStringList;
public List<int> sizes;
public sceHeader(StreamFunctionAdd sfa)
{
if (sfa.ReadAnsiStringSize(8) != "TOD1RSCE")
{
throw new Exception("Error #1: Bad Magic");
}
offsetScript = sfa.ReadUInt32();
offsetStrings = sfa.ReadUInt32();
fileStrings = new List<sceStrings>();
sizes = new List<int>();
sfa.PositionStream = offsetScript;
while (sfa.PositionStream < offsetStrings)
{
//The file is composed by some bytecode above and strings below
//then the game gets the offset to a string whith the opcode 0x47
//so we loop until then, it can get some false positives though
if (sfa.ReadByte() == 0x47)
{
byte num = sfa.ReadByte();
if (num >> 4 == 1)
{
if (sfa.ReadByte() == 0x0)
{
fileStrings.Add(new sceStrings((uint)sfa.PositionStream - 1, offsetStrings)
{
offset = ((uint)num & 0xF) + offsetStrings,
typeOffset = sceStrings.OffsetType.ShortOffset
});
}
else
{
sfa.PositionStream--;
}
}
if (num >> 4 == 5)
{
sceStrings sceStrings = new sceStrings((uint)sfa.PositionStream, offsetStrings);
sceStrings.offset = (uint)((num & 0xF) << 8) + sfa.ReadByte() + offsetStrings;
sceStrings.typeOffset = sceStrings.OffsetType.MediumOffset;
if (sfa.ReadByte() == 0)
fileStrings.Add(sceStrings);
}
if (num == 0x90)
{
sceStrings sceStrings = new sceStrings((uint)sfa.PositionStream, offsetStrings);
sceStrings.offset = sfa.ReadUInt16() + offsetStrings;
sceStrings.typeOffset = sceStrings.OffsetType.LargeOffset;
if (sfa.ReadByte() == 0)
fileStrings.Add(sceStrings);
}
}
}
//order the offsets by string position
fileStrings.Sort((s1, s2) => s1.offset.CompareTo(s2.offset));
//Get sizes
for (int index = 0; index < fileStrings.Count; ++index)
{
if (index < fileStrings.Count - 1)
sizes.Add((int)fileStrings[index + 1].offset - (int)fileStrings[index].offset);
else
sizes.Add((int)(uint)sfa.LengthStream - (int)fileStrings[index].offset);
}
for (int index = 0; index < fileStrings.Count; ++index)
fileStrings[index].ReadData(sfa, sizes[index]);
}
public void WriteStrings(StreamFunctionAdd sfa, bool dedup = false)
{
sfa.PositionStream = offsetStrings;
sfa.LengthStream = offsetStrings;
List<int> failedMediumStrings = new List<int>();
List<int> failedShortStrings = new List<int>();
uint lastShortLength = 0;
uint lastMediumLength = 0;
long realPos = 0;
bool matched = false;
for (int index = 0; index < fileStrings.Count; ++index)
{
if (dedup)
{
for (int index1 = index - 1; index1 >= 0; --index1)
{
if (index == 0) break;
if (fileStrings[index].data.Count == fileStrings[index1].data.Count)
{
if (System.Linq.Enumerable.SequenceEqual(fileStrings[index].data, fileStrings[index1].data))
{
realPos = sfa.PositionStream;
fileStrings[index].offset = fileStrings[index1].offset;
matched = true;
break;
}
}
}
if (!matched)
fileStrings[index].WriteData(sfa);
}
else
{
fileStrings[index].WriteData(sfa);
}
if (index > 0)
{
uint num1 = fileStrings[index].offset - offsetStrings;
sfa.PositionStream = fileStrings[index].myOffset - 1U;
if (num1 < 0x10 && fileStrings[index].typeOffset == sceStrings.OffsetType.ShortOffset)
{
byte num2 = (byte)(0x10 + num1);
sfa.WriteByte(num2);
sfa.WriteByte(0);
}
else if (num1 < 0x1000 && fileStrings[index].typeOffset == sceStrings.OffsetType.MediumOffset)
{
byte num2 = (byte)(0x50 + (num1 >> 8));
sfa.WriteByte(num2);
byte num3 = (byte)(num1 & byte.MaxValue);
sfa.WriteUInt16(num3);
}
else if (num1 < 0x10000 && fileStrings[index].typeOffset == sceStrings.OffsetType.LargeOffset)
{
sfa.WriteByte(0x90);
sfa.WriteUInt16((ushort)num1);
}
else
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
if (Program.verbose)
{
MiscUtils.Warn("Can't fit desired pointer in the available space...");
MiscUtils.Warn("- Failed at block: " + index + ", around line " + lineNumberList[index]);
MiscUtils.Warn("- String: " + plainStringList[index]);
//MiscUtils.Warn("- Intended offset: " + num1);
MiscUtils.Warn(string.Format("- Position: 0x{0:X6}", fileStrings[index].offset));
MiscUtils.Warn("- Pointer type: " + fileStrings[index].typeOffset);
MiscUtils.Warn("Continuing insertion, but leaving pointer unchanged, expect text errors!");
Console.WriteLine();
//Console.ReadKey();
//throw new InvalidOperationException();
}
if (fileStrings[index].typeOffset == sceStrings.OffsetType.MediumOffset)
{
failedMediumStrings.Add(index);
lastMediumLength = num1;
}
else
{
failedShortStrings.Add(index);
lastShortLength = num1;
}
}
if (matched)
{
sfa.PositionStream = realPos;
matched = false;
continue;
}
sfa.PositionStream = sfa.LengthStream;
}
}
if (failedShortStrings.Count != 0 || failedMediumStrings.Count != 0)
{
MiscUtils.Warn("Couldn't insert all lines due to pointer size issues");
MiscUtils.Warn("Inserted the other lines, but left the faulty ones untouched, expect text errors!");
Console.WriteLine();
if (failedShortStrings.Count != 0)
{
MiscUtils.Info("Found " + failedShortStrings.Count + " strings over the 16 line");
MiscUtils.Info("Remove " + (lastShortLength - 16) + " bytes before line "
+ lineNumberList[failedShortStrings[0]] + " to get them back to a valid position");
MiscUtils.Info("Line " + lineNumberList[failedShortStrings[0]] + " for reference:");
MiscUtils.Info("\"" + plainStringList[failedShortStrings[0]] + "\"");
Console.WriteLine();
}
if (failedMediumStrings.Count != 0)
{
MiscUtils.Info("Found " + failedMediumStrings.Count + " strings over the 4096 line");
MiscUtils.Info("Remove " + (lastMediumLength - 4096) + " bytes before line "
+ lineNumberList[failedMediumStrings[0]] + " to get them back to a valid position");
MiscUtils.Info("Line " + lineNumberList[failedMediumStrings[0]] + " for reference:");
MiscUtils.Info("\"" + plainStringList[failedMediumStrings[0]] + "\"");
Console.WriteLine();
}
}
sfa.SeekValueWrite(2U);
}
}
}