-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
258 lines (224 loc) · 8.5 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace cgcd
{
class Program
{
static Dictionary<string, string> data = new Dictionary<string, string>();
enum Tok
{
WORD, // example
ASSIGN, // =
NEWLINE, // \n
COMMENT,
NUMBER, // 1234
EOF
}
class Token
{
public Tok type;
public object literal;
}
class Tokenizer
{
int i = 0;
int len;
int line = 1;
string buffer;
List<Token> tokens;
public List<Token> Tokenize(string file)
{
tokens = new List<Token>();
buffer = File.ReadAllText(file)+ "\r\n";
len = buffer.Length - 1;
while (i != len)
{
Scan();
}
tokens.Add(new Token
{
type = Tok.EOF
});
return tokens;
}
public void Scan()
{
void _n(Tok type)
=> tokens.Add(new Token { type = type });
var t = Read();
switch (t)
{
case '/':
{
var p = Peek();
if (p == '/')
{
Read();
Comment();
}
}
break;
case '=':
{
switch (Peek())
{
default: _n(Tok.ASSIGN); break;
}
}
break;
case '\r': break;
case '\t': break;
case '\n': line++; _n(Tok.NEWLINE); break;
case ' ': break;
default:
{
if (char.IsDigit(t) || (t == '-' && char.IsDigit(Peek())))
{
var number = ReadUntil(null, new char[] { ' ', '(', ')', ',', '+', '*', '\r', '\n' });
tokens.Add(
new Token { type = Tok.NUMBER, literal = int.Parse(number) }
);
}
else if (char.IsLetter(t))
{
var word = ReadUntil(null, new char[] { ' ', '(', ')', ',', '\r', '\n' });
tokens.Add(
new Token { type = Tok.WORD, literal = word }
);
}
else
{
throw new Exception($"unable to read '{t}' at pos {i}, line {line}");
}
break;
}
}
}
public void Comment()
{
i++;
tokens.Add(new Token { type = Tok.COMMENT, literal = ReadUntil("comment", new char[] { '\r', '\n' }) });
}
public string ReadUntil(string t, char c)
=> ReadUntil(t, new char[] { c });
public string ReadUntil(string t, char[] c)
{
int spos = i;
var sb = new StringBuilder();
sb.Append(buffer[i - 1]);
while (true)
{
if (c.Any(x => x == Peek()))
{
return sb.ToString();
}
sb.Append(Read());
if (EOF())
throw new Exception($"unterminated {t}, starting from {spos}");
}
}
public bool Digit()
=> char.IsDigit(buffer[i]);
public bool EOF()
=> i == len;
public char Read()
=> buffer[i++];
public char Peek()
=> buffer[i];
}
static void Main(string[] args)
{
// Force english language, for exceptions
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var ch = Path.Combine(dir, "character.txt");
if (args.Length > 0)
ch = args[0];
if (!File.Exists(ch))
{
Console.WriteLine($"Unable to find {ch}");
return;
}
var t = new Tokenizer();
var tokens = t.Tokenize(ch);
var i = 0;
while(i < tokens.Count)
{
var token = tokens[i];
if (token.type == Tok.WORD && tokens[i + 1].type == Tok.ASSIGN)
{
string v = "";
if (tokens[i + 2].literal is int)
v = ((int)tokens[i + 2].literal).ToString();
else
v = (string)tokens[i + 2].literal;
data[(string)token.literal] = v;
}
i++;
}
Console.WriteLine($"Parsed {ch}");
byte[] _int(string s, int min, int max)
{
var num = int.Parse(data[s]);
byte[] bytes = BitConverter.GetBytes(num);
Array.Reverse(bytes, 0, bytes.Length);
return bytes;
}
int _int_is(string s)
=> int.Parse(data[s]);
var outFile = Path.Combine(dir, "character.gcd");
if (args.Length > 1)
outFile = args[1];
if (File.Exists(outFile))
File.Delete(outFile);
Console.WriteLine($"Writing {outFile}...");
int numBytes = 0;
using (var br = new BinaryWriter(File.Open(outFile, FileMode.CreateNew)))
{
var max = new byte[] { 0xff, 0xff, 0xff, 0xff };
var _null = new byte[] { 0x00, 0x00, 0x00, 0x00 };
br.Write(_null);
br.Write(_int("st", 0, 10));
br.Write(_int("pe", 0, 10));
br.Write(_int("en", 0, 10));
br.Write(_int("ch", 0, 10));
br.Write(_int("in", 0, 10));
br.Write(_int("ag", 0, 10));
br.Write(_int("lk", 0, 10));
for (var x = 0; x < 26; x++) // 0x20 - 0x84
br.Write(_null);
br.Write(_int("age", 1, 99));
br.Write(_int("gender", 0,1));
for (var x = 0; x < 35; x++) // 0x90 - 0x118
br.Write(_null);
for (var x = 0; x < 18; x++) // all skills
br.Write(_int("sk_" + x, 0, 300));
// 0x164-0x16F
br.Write(_null);
br.Write(_null);
br.Write(_null);
br.Write(_null);
var name = Encoding.ASCII.GetBytes(data["name"]).ToList();
while (name.Count < 32)
name.Add((byte)0);
br.Write(name.ToArray());
br.Write(_int_is("tag_1") == -1 ? max : _int("tag_1", 0, 17));
br.Write(_int_is("tag_2") == -1 ? max : _int("tag_2", 0, 17));
br.Write(_int_is("tag_3") == -1 ? max : _int("tag_3", 0, 17));
br.Write(_int_is("tag_4") == -1 ? max : _int("tag_4", 0, 17));
br.Write(_int_is("trait_1") == -1 ? max : _int("trait_1", 0, 15));
br.Write(_int_is("trait_2") == -1 ? max : _int("trait_2", 0, 15));
br.Write(_int("char_points", 0, 99));
br.Write(_null);
br.Write(_int("style", 0 , 10));
numBytes = (int)br.BaseStream.Position;
}
Console.WriteLine($"Done! Wrote {numBytes} bytes to {outFile}");
}
}
}