This repository was archived by the owner on Feb 24, 2025. It is now read-only.
forked from crskycode/CIRCUS_MES_FD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript.cs
366 lines (292 loc) · 9.77 KB
/
Script.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.IO;
#pragma warning disable IDE0063
namespace CIRCUS_MES_FA
{
public class Script
{
public void Load(string filePath)
{
using (var reader = new BinaryReader(File.OpenRead(filePath)))
{
Read(reader);
}
}
public void Save(string filePath)
{
using (var writer = new BinaryWriter(File.Create(filePath)))
{
writer.Write(_jumpList.Count);
foreach (var e in _jumpList)
{
writer.Write(e);
}
foreach (var e in _idList)
{
writer.Write(e);
}
writer.Write(_codeBlock);
writer.Flush();
}
}
List<uint> _jumpList;
List<ushort> _idList;
byte[] _codeBlock;
Assembly _assembly;
void Read(BinaryReader reader)
{
// Read jump list
var count = reader.ReadInt32();
_jumpList = new List<uint>(count);
for (var i = 0; i < count; i++)
{
_jumpList.Add(reader.ReadUInt32());
}
_idList = new List<ushort>();
for (var i = 0; i < count; i++)
{
_idList.Add(reader.ReadUInt16());
}
// Read code block
var size = Convert.ToInt32(reader.BaseStream.Length - reader.BaseStream.Position);
_codeBlock = reader.ReadBytes(size);
// Parse code
Parse();
}
void Parse()
{
var stream = new MemoryStream(_codeBlock);
var reader = new BinaryReader(stream);
var version = reader.ReadByte();
var type = reader.ReadUInt16();
Debug.WriteLine($"Version: {version}");
Debug.WriteLine($"Type: {type}");
_assembly = new Assembly();
while (stream.Position < stream.Length)
{
var inst = Operation.Unknow;
var addr = Convert.ToInt32(stream.Position);
var code = reader.ReadByte();
if (code >= 0x28)
{
if (code >= 0x30)
{
if (code >= 0x4B)
{
if (code >= 0x4F)
{
reader.ReadUInt16();
reader.ReadUInt16();
reader.ReadUInt16();
reader.ReadUInt16();
}
else
{
// Encrypted
inst = Operation.LoadEncryptString;
reader.ReadCString();
}
}
else
{
reader.ReadCString();
}
}
else
{
reader.ReadByte();
reader.ReadCString();
}
}
else
{
reader.ReadByte();
reader.ReadByte();
}
var length = Convert.ToInt32(stream.Position) - addr;
_assembly.Add(addr, length, inst);
}
if (_assembly.BytesLength != stream.Length - 3)
{
throw new Exception("Parsing failed");
}
}
Instruct[] GetStringInstructs()
{
return _assembly.Instructs
.Where(a => a.Op == Operation.LoadEncryptString)
.ToArray();
}
public void ExportText(string filePath, Encoding encoding)
{
var inst = GetStringInstructs();
if (inst.Length == 0)
{
// No string in this script
return;
}
//var encoding = Encoding.GetEncoding("gbk");
// Create output text file
using var writer = File.CreateText(filePath);
foreach (var e in inst)
{
if (e.Length <= 2)
{
// Skip empty string
continue;
}
// Copy the string bytes to buffer
var length = e.Length - 2;
var bytes = new byte[length];
Array.Copy(_codeBlock, e.Addr + 1, bytes, 0, length);
// Only the encrypted string here
DecryptString(bytes);
var s = encoding.GetString(bytes);
s = s.Replace("\r", "\\r");
s = s.Replace("\n", "\\n");
// Write out text
writer.WriteLine($"◇{e.Addr:X8}◇{s}");
writer.WriteLine($"◆{e.Addr:X8}◆{s}");
writer.WriteLine();
}
writer.Flush();
}
public void ImportText(string filePath, Encoding encoding)
{
var inst = GetStringInstructs();
if (inst.Length == 0)
{
// No string in this script
return;
}
var translated = new Dictionary<int, string>();
// Read translation file
using (var reader = new StreamReader(filePath))
{
var _lineNo = 0;
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var lineNo = _lineNo++;
if (line.Length == 0 || line[0] != '◆')
{
// Skip empty line and original line
continue;
}
// Get input
var m = Regex.Match(line, "◆(.+?)◆(.+$)");
// Check format
if (!m.Success || m.Groups.Count != 3)
{
throw new Exception($"Bad format at line: {lineNo}");
}
// Get address and string
var addr = int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);
var s = m.Groups[2].Value;
s = s.Replace("\\r", "\r");
s = s.Replace("\\n", "\n");
translated.Add(addr, s);
}
}
if (translated.Count == 0)
{
// No translated string loaded
return;
}
// Rebuild the code block
var stream = new MemoryStream();
var writer = new BinaryWriter(stream);
// Version & Type
writer.Write(_codeBlock, 0, 2);
// Write instructs
foreach (var e in _assembly.Instructs)
{
e.NewAddr = Convert.ToInt32(stream.Position);
// Find translated string
if (e.Op == Operation.LoadEncryptString &&
translated.TryGetValue(e.Addr, out string s))
{
// Write translated string
var bytes = encoding.GetBytes(s);
EncryptString(bytes);
writer.Write(_codeBlock[e.Addr]); // Opcode
writer.Write(bytes);
writer.Write((byte)0);
}
else
{
writer.Write(_codeBlock, e.Addr, e.Length);
}
// Update jump list
var jumpIndex = _jumpList.FindIndex(a => (a & 0x7fffffff) == e.Addr);
if (jumpIndex != -1)
{
var addrVal = _jumpList[jumpIndex] & 0x80000000;
addrVal |= (uint)e.NewAddr & 0x7fffffff;
_jumpList[jumpIndex] = addrVal;
}
}
writer.Flush();
_codeBlock = stream.ToArray();
}
static void DecryptString(byte[] bytes)
{
for (var i = 0; i < bytes.Length; i++)
{
bytes[i] += 0x20;
}
}
static void EncryptString(byte[] bytes)
{
for (var i = 0; i < bytes.Length; i++)
{
if (bytes[i] == 0x20)
{
bytes[i] = 0x24;
}
bytes[i] -= 0x20;
}
}
enum Operation
{
Unknow,
LoadEncryptString
}
class Instruct
{
public int Addr { get; }
public int NewAddr { get; set; }
public int Length { get; }
public Operation Op { get; }
public Instruct(int address, int length, Operation op)
{
Addr = address;
Length = length;
Op = op;
}
}
class Assembly
{
public List<Instruct> Instructs { get; } = new();
public void Add(int address, int length, Operation op)
{
Instructs.Add(new Instruct(address, length, op));
}
public void Clear()
{
Instructs.Clear();
}
public int BytesLength
{
get => Instructs.Sum(a => a.Length);
}
}
}
}