This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCSystemConfig.cs
356 lines (296 loc) · 10.5 KB
/
CSystemConfig.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml.Linq;
namespace CSystemArc
{
internal class CSystemConfig
{
private static readonly Encoding SjisEncoding = Encoding.GetEncoding(932, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback);
private List<byte[]> _items;
private byte[] _data1;
private byte[] _data2;
public void Read(Stream stream)
{
byte[] stringData = BcdCompression.Decompress(stream);
_items = UnpackItems(stringData);
int data1Size = Bcd.Read(stream);
_data1 = new byte[data1Size];
stream.Read(_data1, 0, _data1.Length);
int data2Size = Bcd.Read(stream);
_data2 = new byte[data2Size];
stream.Read(_data2, 0, _data2.Length);
if (stream.Position != stream.Length)
throw new InvalidDataException();
}
public void Write(Stream stream)
{
ArraySegment<byte> stringData = PackItems(_items);
BcdCompression.Compress(stringData, stream);
Bcd.Write(stream, _data1.Length);
stream.Write(_data1, 0, _data1.Length);
Bcd.Write(stream, _data2.Length);
stream.Write(_data2, 0, _data2.Length);
}
public XDocument ToXml()
{
if (_items.Count > 8)
throw new InvalidDataException("Unexpected number of items");
XElement configElem =
new XElement(
"config",
TextItemToXml(0),
DictionaryItemToXml(1),
TextItemToXml(2),
TextItemToXml(3),
TextItemToXml(4),
TextItemToXml(5),
BinaryItemToXml(6),
BinaryItemToXml(7)
);
XElement data1Elem =
new XElement(
"data1",
BytesToHex(_data1)
);
XElement data2Elem =
new XElement(
"data2",
BytesToHex(_data2)
);
return new XDocument(
new XElement(
"csystem",
configElem,
data1Elem,
data2Elem
)
);
}
public void FromXml(XDocument doc)
{
XElement root = doc.Root;
if (root.Name != "csystem")
throw new InvalidDataException("Invalid root element name");
XElement configElem = root.Element("config");
if (configElem == null)
throw new InvalidDataException("<config> element missing");
_items = new List<byte[]>();
foreach (XElement itemElem in configElem.Elements("item"))
{
_items.Add(ItemFromXml(itemElem));
}
XElement data1Elem = root.Element("data1");
if (data1Elem == null)
throw new InvalidDataException("<data1> element is missing");
_data1 = HexToBytes(data1Elem.Value);
XElement data2Elem = root.Element("data2");
if (data2Elem == null)
throw new InvalidDataException("<data2> element is missing");
_data2 = HexToBytes(data2Elem.Value);
}
private static List<byte[]> UnpackItems(byte[] data)
{
MemoryStream stream = new MemoryStream(data);
BinaryReader reader = new BinaryReader(stream);
List<byte[]> items = new List<byte[]>();
while (stream.Position < stream.Length)
{
int length = reader.ReadInt32();
byte[] item = reader.ReadBytes(length);
if (item.Length > 0 && item[0] == (byte)'S')
{
byte[] xorlessItem = new byte[item.Length - 4];
xorlessItem[0] = item[0];
Array.Copy(item, 5, xorlessItem, 1, item.Length - 5);
item = xorlessItem;
}
if (item.Length >= 2 && item[item.Length - 2] == 0xFE && item[item.Length - 1] == 0xA)
{
byte[] trimmedItem = new byte[item.Length - 2];
Array.Copy(item, trimmedItem, trimmedItem.Length);
item = trimmedItem;
}
items.Add(item);
}
return items;
}
private static ArraySegment<byte> PackItems(List<byte[]> items)
{
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
foreach (byte[] item in items)
{
byte[] itemToWrite = item;
if (item[0] == (byte)'S')
{
byte[] xoredItem = new byte[item.Length + 4];
xoredItem[0] = item[0];
Array.Copy(item, 1, xoredItem, 5, item.Length - 1);
itemToWrite = xoredItem;
}
writer.Write(itemToWrite.Length);
writer.Write(itemToWrite);
}
stream.TryGetBuffer(out ArraySegment<byte> data);
return data;
}
private XElement BinaryItemToXml(int index)
{
if (index >= _items.Count)
return null;
byte[] item = _items[index];
return new XElement(
"item",
new XAttribute("type", "binary"),
BytesToHex(item)
);
}
private XElement TextItemToXml(int index)
{
if (index >= _items.Count)
return null;
try
{
string text = SjisEncoding.GetString(_items[index]);
return new XElement(
"item",
new XAttribute("type", "text"),
text
);
}
catch (DecoderFallbackException)
{
return BinaryItemToXml(index);
}
}
private XElement DictionaryItemToXml(int index)
{
if (index >= _items.Count)
return null;
byte[] item = _items[index];
XElement dictElem =
new XElement(
"item",
new XAttribute("type", "dict")
);
if ((char)item[0] != '#')
throw new InvalidDataException();
int offset = 1;
while (offset < item.Length)
{
string key = null;
while (true)
{
char c = (char)item[offset++];
if (c == ':')
break;
key += c;
}
int valueOffset = offset;
int value = 0;
while (true)
{
byte b = item[offset++];
if (b == 201)
break;
if (b <= 200)
value += b;
else if (b == 250)
value = 0;
}
if (offset == valueOffset + 1)
value = -1;
dictElem.Add(new XElement("entry", new XAttribute("key", key), value.ToString()));
}
return dictElem;
}
private static byte[] ItemFromXml(XElement elem)
{
switch (elem.Attribute("type")?.Value)
{
case "binary":
return BinaryItemFromXml(elem);
case "text":
return TextItemFromXml(elem);
case "dict":
return DictionaryItemFromXml(elem);
default:
throw new InvalidDataException("Unrecognized type for <item>");
}
}
private static byte[] BinaryItemFromXml(XElement elem)
{
return HexToBytes(elem.Value);
}
private static byte[] TextItemFromXml(XElement elem)
{
return SjisEncoding.GetBytes(elem.Value);
}
private static byte[] DictionaryItemFromXml(XElement elem)
{
MemoryStream stream = new MemoryStream();
stream.WriteByte((byte)'#');
foreach (XElement entry in elem.Elements("entry"))
{
string key = entry.Attribute("key").Value;
foreach (char c in key)
{
stream.WriteByte((byte)c);
}
stream.WriteByte((byte)':');
int value = int.Parse(entry.Value);
if (value < -1)
{
throw new InvalidDataException("Dictionary values can't be less than -1");
}
else if (value == -1)
{
}
else if (value == 0)
{
stream.WriteByte(250);
}
else
{
while (value > 200)
{
stream.WriteByte(200);
value -= 200;
}
stream.WriteByte((byte)value);
}
stream.WriteByte(201);
}
byte[] item = new byte[stream.Length];
stream.Position = 0;
stream.Read(item, 0, item.Length);
return item;
}
private static string BytesToHex(byte[] bytes)
{
StringBuilder hex = new StringBuilder();
foreach (byte b in bytes)
{
hex.AppendFormat("{0:X02} ", b);
}
if (hex.Length > 0)
hex.Length--;
return hex.ToString();
}
private static byte[] HexToBytes(string hex)
{
hex = hex.Replace(" ", "");
if (hex.Length % 2 != 0)
throw new InvalidDataException("Hex string must have an even number of digits");
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = byte.Parse(hex.Substring(2 * i, 2), NumberStyles.HexNumber);
}
return bytes;
}
}
}