-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cs
383 lines (353 loc) · 11.4 KB
/
Util.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
using System;
using System.Collections.Generic;
namespace ToGLocInject {
public static class Util {
public static byte GetColorComponent(this System.Drawing.Color color, int component) {
switch (component) {
case 0: return color.R;
case 1: return color.G;
case 2: return color.B;
case 3: return color.A;
}
throw new Exception("Wrong component index.");
}
public static string ConvertToFullwidth(string str) {
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (char c in str) {
char? cc = ConvertToFullwidth(c);
if (cc == null)
throw new Exception("conversion failed");
sb.Append(cc.Value);
}
return sb.ToString();
}
public static char? ConvertToFullwidth(char c) {
switch (c) {
case ' ': return ' ';
case '0': return '0';
case '1': return '1';
case '2': return '2';
case '3': return '3';
case '4': return '4';
case '5': return '5';
case '6': return '6';
case '7': return '7';
case '8': return '8';
case '9': return '9';
case 'A': return 'A';
case 'B': return 'B';
case 'C': return 'C';
case 'D': return 'D';
case 'E': return 'E';
case 'F': return 'F';
case 'G': return 'G';
case 'H': return 'H';
case 'I': return 'I';
case 'J': return 'J';
case 'K': return 'K';
case 'L': return 'L';
case 'M': return 'M';
case 'N': return 'N';
case 'O': return 'O';
case 'P': return 'P';
case 'Q': return 'Q';
case 'R': return 'R';
case 'S': return 'S';
case 'T': return 'T';
case 'U': return 'U';
case 'V': return 'V';
case 'W': return 'W';
case 'X': return 'X';
case 'Y': return 'Y';
case 'Z': return 'Z';
case 'a': return 'a';
case 'b': return 'b';
case 'c': return 'c';
case 'd': return 'd';
case 'e': return 'e';
case 'f': return 'f';
case 'g': return 'g';
case 'h': return 'h';
case 'i': return 'i';
case 'j': return 'j';
case 'k': return 'k';
case 'l': return 'l';
case 'm': return 'm';
case 'n': return 'n';
case 'o': return 'o';
case 'p': return 'p';
case 'q': return 'q';
case 'r': return 'r';
case 's': return 's';
case 't': return 't';
case 'u': return 'u';
case 'v': return 'v';
case 'w': return 'w';
case 'x': return 'x';
case 'y': return 'y';
case 'z': return 'z';
case '!': return '!';
case '"': return '"';
case '#': return '#';
case '$': return '$';
case '%': return '%';
case '&': return '&';
case '\'': return ''';
case '(': return '(';
case ')': return ')';
case '*': return '*';
case '+': return '+';
case ',': return ',';
case '-': return '-';
case '.': return '.';
case '/': return '/';
case ':': return ':';
case ';': return ';';
case '<': return '<';
case '=': return '=';
case '>': return '>';
case '?': return '?';
case '@': return '@';
case '[': return '[';
case '\\': return '\';
case ']': return ']';
case '^': return '^';
case '_': return '_';
case '`': return '`';
case '{': return '{';
case '|': return '|';
case '}': return '}';
case '~': return '~';
case '¢': return '¢';
case '£': return '£';
case '¬': return '¬';
case '¦': return '¦';
case '¥': return '¥';
default: return null;
}
}
}
internal enum Version {
J, U, E, W, Wv0
}
internal class E {
public int where;
public List<int> entries;
public E(int beforeWhichEntry, int entry) {
this.where = beforeWhichEntry;
this.entries = new List<int> { entry };
}
public E(int beforeWhichEntry, List<int> entries) {
this.where = beforeWhichEntry;
this.entries = entries;
}
}
internal class M {
public List<E> Adds = new List<E>();
public List<(int entry, char character, bool keepChar, int maxSplits)> FeedSplits = new List<(int entry, char character, bool keepChar, int maxSplits)>();
public List<(int entry, List<int> where)> PosSplits = new List<(int entry, List<int> where)>();
public List<(int target, List<int> sources, string joiner, List<int> newlinesToRemove, List<int> newlinesToAdd)> Merges = new List<(int target, List<int> sources, string joiner, List<int> newlinesToRemove, List<int> newlinesToAdd)>();
public List<int> Removes = new List<int>();
public bool ReplaceEmptyStringsInsteadOfSkippingNegativeAdds = false;
public M() { }
public M(M m) {
Adds = new List<E>(m.Adds);
}
public M(List<E> adds) {
Adds = adds;
}
public M FeedSplit(int e, char ch = '\f', bool keepChar = false, int maxSplits = int.MaxValue) {
FeedSplits.Add((e, ch, keepChar, maxSplits));
return this;
}
public M PosSplit(int e, int pos) {
PosSplits.Add((e, new List<int>() { pos }));
return this;
}
public M PosSplit(int e, int p1, int p2) {
PosSplits.Add((e, new List<int>() { p1, p2 }));
return this;
}
public M A(E add) {
Adds.Add(add);
return this;
}
public M A(int beforeWhichEntry, int entry) {
Adds.Add(new E(beforeWhichEntry, entry));
return this;
}
public M A(int beforeWhichEntry, int entry1, int entry2) {
Adds.Add(new E(beforeWhichEntry, new List<int>() { entry1, entry2 }));
return this;
}
public M A(int beforeWhichEntry, int entry1, int entry2, int entry3) {
Adds.Add(new E(beforeWhichEntry, new List<int>() { entry1, entry2, entry3 }));
return this;
}
public M A(int beforeWhichEntry, int entry1, int entry2, int entry3, int entry4) {
Adds.Add(new E(beforeWhichEntry, new List<int>() { entry1, entry2, entry3, entry4 }));
return this;
}
public M A(int beforeWhichEntry, int entry1, int entry2, int entry3, int entry4, int entry5) {
Adds.Add(new E(beforeWhichEntry, new List<int>() { entry1, entry2, entry3, entry4, entry5 }));
return this;
}
public M A(int beforeWhichEntry, List<int> entries) {
Adds.Add(new E(beforeWhichEntry, entries));
return this;
}
public M Merge(int target, int toJoinInto, string joiner, List<int> newlinesToRemove = null, List<int> newlinesToAdd = null) {
Merges.Add((target, new List<int>() { target, toJoinInto }, joiner, newlinesToRemove, newlinesToAdd));
Removes.Add(toJoinInto);
return this;
}
public M Merge(int target, int toJoinInto1, int toJoinInto2, string joiner, List<int> newlinesToRemove = null, List<int> newlinesToAdd = null) {
Merges.Add((target, new List<int>() { target, toJoinInto1, toJoinInto2 }, joiner, newlinesToRemove, newlinesToAdd));
Removes.Add(toJoinInto1);
Removes.Add(toJoinInto2);
return this;
}
public M Dup() {
return new M(this);
}
public M EnableEmptyStrings() {
ReplaceEmptyStringsInsteadOfSkippingNegativeAdds = true;
return this;
}
}
internal class W {
public const int KEEP_ORIGINAL_MARK_MAPPED = -2;
public const int KEEP_ORIGINAL_MARK_UNMAPPED = -1;
public List<(int w, int u)> PrecalculatedReplacements = new List<(int w, int u)>(); // specific wii IDs and their matching US IDs
public List<(int w, string u)> PrecalculatedReplacementsDirect = new List<(int w, string u)>();
public delegate string PostProcessString(string original, string replacement);
public List<(int w, PostProcessString func)> PostProcessing = new List<(int w, PostProcessString pps)>();
public W R(int widx, int replaceWithUIdx) { // replace w entry with specific string from US PS3 file
if (replaceWithUIdx < 0) {
throw new Exception("invalid replace index");
}
ConfirmUnset(widx);
PrecalculatedReplacements.Add((widx, replaceWithUIdx));
return this;
}
public W Sys(int widx) { // mark as system string, if it automaps to a thing but really should be left alone, or just to reduce noise in the csvs
ConfirmUnset(widx);
PrecalculatedReplacements.Add((widx, KEEP_ORIGINAL_MARK_MAPPED));
return this;
}
public W Un(int widx) { // mark as explicitly untranslated string, if it automaps to a thing it shouldn't
ConfirmUnset(widx);
PrecalculatedReplacements.Add((widx, KEEP_ORIGINAL_MARK_UNMAPPED));
return this;
}
public W P(int widx, PostProcessString func) { // register function for postprocessing a string after everything else has been done
PostProcessing.Add((widx, func));
return this;
}
public W RP(int widx, int uidx, PostProcessString func) { // combines replacement with postprocessing in one convenient call!
return R(widx, uidx).P(widx, func);
}
public W R(int widx, string replacement) { // replace w entry with custom string
ConfirmUnset(widx);
PrecalculatedReplacementsDirect.Add((widx, replacement));
return this;
}
private void ConfirmUnset(int widx) {
foreach (var a in PrecalculatedReplacements) {
if (widx == a.w) {
throw new Exception("multiple definitions for explicit string replacements");
}
}
foreach (var a in PrecalculatedReplacementsDirect) {
if (widx == a.w) {
throw new Exception("multiple definitions for explicit string replacements");
}
}
}
public W Password(int widx) {
return P(widx, ConvertToFullwidth);
}
private static string ConvertToFullwidth(string original, string replacement) {
return Util.ConvertToFullwidth(replacement);
}
}
internal class MappingData {
public bool Confirmed;
public M J;
public M U;
public W W;
public bool MultiplyOutSkit;
public bool SkipTextMapping;
public bool ReplaceInWiiV0;
public ContainedVoiceInfo VoiceInject;
public MappingData(bool c = false, M j = null, M u = null, W w = null, bool multiplyOutSkit = false, bool skipTextMapping = false, bool replaceInWiiV0 = false, ContainedVoiceInfo voiceInject = null) {
Confirmed = c;
J = j ?? new M();
U = u ?? new M();
W = w ?? new W();
MultiplyOutSkit = multiplyOutSkit;
SkipTextMapping = skipTextMapping;
ReplaceInWiiV0 = replaceInWiiV0;
VoiceInject = voiceInject;
}
}
internal enum MappingType { SelfMatch, CompMatch, JpOnly, EnOnly, NotMatched, DirectMatched, VoiceLineMatched }
internal class MappedEntry {
public int jpos;
public string jp;
public int upos;
public string en;
public MappingType type;
public override string ToString() {
return "[" + type + "] => " + jp + " / " + en;
}
}
internal class MemChunk {
public uint Address;
public uint FreeBytes;
public System.IO.MemoryStream File;
public HyoutaPluginBase.IRomMapper Mapper;
public bool IsInternal;
public override string ToString() {
return "0x" + FreeBytes.ToString("X") + " free bytes at 0x" + Address.ToString("X");
}
}
internal class SJisString {
public byte[] Data;
public SJisString(byte[] data) {
Data = data;
}
public override bool Equals(object obj) {
SJisString other = obj as SJisString;
if (other == null) {
return false;
}
if (Data.Length != other.Data.Length) {
return false;
}
for (int i = 0; i < Data.Length; ++i) {
if (Data[i] != other.Data[i]) {
return false;
}
}
return true;
}
public override int GetHashCode() {
int v = Data.Length;
for (int i = 0; i < Data.Length; ++i) {
v += Data[i];
}
return v;
}
}
internal class Ps3ElfMapper : HyoutaPluginBase.IRomMapper {
public bool TryMapRamToRom(ulong ramAddress, out ulong value) {
value = ramAddress - 0x10000;
return value < 0x881600;
}
public bool TryMapRomToRam(ulong romAddress, out ulong value) {
value = romAddress + 0x10000;
return true;
}
}
}