-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
281 lines (227 loc) · 10.2 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System.CommandLine;
using System.CommandLine.NamingConventionBinder;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.Json;
using CsvHelper;
using CsvHelper.Configuration;
using Newtonsoft.Json;
using JsonException = System.Text.Json.JsonException;
namespace HogwartsLocalisationConverter
{
internal class Program
{
//private const string MAGIC = "A\0V\0A\0F\0D\0I\0C\0T\0 \02\0.\00\0\0\0";
private const string MAGIC = "AVAFDICT 2.0 \0";
private const long HEADER_SIZE = 72;
public static void Main(string[] args)
{
var bin2JsonCommand = new Command("bin2json", "Convert .bin to .json")
{
new Argument<FileInfo>("file", "File to convert")
};
var json2BinCommand = new Command("json2bin", "Convert .json to .bin")
{
new Argument<FileInfo>("file", "File to convert")
};
var csv2BinCommand = new Command("csv2bin", "Convert .csv to .bin")
{
new Argument<FileInfo>("file", "File to convert")
};
var rootCommand = new RootCommand("Converts to and from Hogwarts Legacy language .bin files.")
{
new Argument<FileInfo>("file", "File to convert (will detect extension and attempt to use the necessary converter).")
};
rootCommand.Handler = CommandHandler.Create<FileInfo>(GuessTheFile);
bin2JsonCommand.Handler = CommandHandler.Create<FileInfo>(BinToJson);
json2BinCommand.Handler = CommandHandler.Create<FileInfo>(JsonToBin);
csv2BinCommand.Handler = CommandHandler.Create<FileInfo>(CsvToBin);
rootCommand.AddCommand(bin2JsonCommand);
rootCommand.AddCommand(json2BinCommand);
rootCommand.AddCommand(csv2BinCommand);
rootCommand.Invoke(args);
}
private static void GuessTheFile(FileInfo file)
{
switch (file.Extension)
{
case ".bin":
Console.WriteLine($"Detected a BIN file. Converting to JSON...");
BinToJson(file);
break;
case ".json":
Console.WriteLine($"Detected a JSON file. Converting to BIN...");
JsonToBin(file);
break;
case ".csv":
Console.WriteLine($"Detected a CSV file. Converting to BIN...");
CsvToBin(file);
break;
default:
Console.WriteLine($"Unknown file extension");
break;
}
}
public class Foo
{
public string key { get; set; }
public string value { get; set; }
}
private static void CsvToBin(FileInfo file)
{
var entries = new Dictionary<string, string>();
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false,
};
using (var reader = new StreamReader(file.FullName))
{
using (var csv = new CsvReader(reader, config))
{
try
{
var records = csv.GetRecords<Foo>();
entries = records.ToDictionary(r => r.key, r =>
{
string s = r.value.Replace(@"\n", "\n");
s = s.Replace(@"\""", @"""");
return s;
});
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine("Press Enter key to close...");
Console.ReadLine();
return;
}
}
}
WriteBinFromDictionary(file.FullName, entries);
}
private static void JsonToBin(FileInfo file)
{
var entries = new Dictionary<string, string>();
// load json into dictionary
try
{
entries = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(file.FullName));
}
catch (JsonReaderException jre)
{
Console.WriteLine($"Couldn't parse json. error={jre.Message}");
return;
}
//File.WriteAllText(Path.ChangeExtension(file.FullName, ".json"), jsonString);
WriteBinFromDictionary(file.FullName, entries);
}
static string AddSuffix(string filename, string suffix)
{
string fDir = Path.GetDirectoryName(filename);
string fName = Path.GetFileNameWithoutExtension(filename);
string fExt = Path.GetExtension(filename);
return Path.Combine(fDir, String.Concat(fName, suffix, fExt));
}
private static void WriteBinFromDictionary(string file, Dictionary<string, string> entries)
{
if (entries.Count == 0)
{
Console.WriteLine($"Empty dictionary");
return;
}
string modifiedPath = Path.ChangeExtension(AddSuffix(file,"-modified"), ".bin");
using (var fs = File.Open(modifiedPath, FileMode.Create))
{
using (var bw = new BinaryWriter(fs, Encoding.UTF8, false))
{
//string magic = MAGIC; // always "AVAFDICT 2.0 \0"
long entryCount = entries.Count;
long headerSize = HEADER_SIZE; // always 72
long entriesSize = entryCount * 24; // each entry header is 24 bytes
long dataStart = entriesSize + headerSize; // start of data section is after the header and the entries
long dataSize = 0; // size of all the strings put together
bw.Write(Encoding.Unicode.GetBytes(MAGIC));
bw.Write(entryCount);
bw.Write(headerSize);
bw.Write(entriesSize);
bw.Write(dataStart);
bw.Write(dataSize);
long offset = 0;
// write headers section
for(int i = 0; i< entries.Count; i++)
{
var entry = entries.ElementAt(i);
int keySize = Encoding.UTF8.GetBytes(entry.Key).Length;
int valueSize = Encoding.UTF8.GetBytes(entry.Value).Length;
bw.Write(offset); // 8 - key offset
offset += keySize;
bw.Write(keySize); // 4 - key size,
bw.Write(offset); // 8 - value offset, every 24 bytes, we write this? running total?
offset += valueSize;
bw.Write(valueSize); // 4 - value size
}
long position = bw.BaseStream.Position;
// write data section
for(int i = 0; i< entries.Count; i++)
{
var entry = entries.ElementAt(i);
bw.Write(Encoding.UTF8.GetBytes(entry.Key));
bw.Write(Encoding.UTF8.GetBytes(entry.Value));
}
dataSize = bw.BaseStream.Position - position;
// go back to beginning and write datasize
bw.Seek(64, SeekOrigin.Begin);
bw.Write(dataSize);
}
}
}
private static void BinToJson(FileInfo file)
{
using (var br = new BinaryReader(file.OpenRead()))
{
string magic;
long entryCount;
long headerSize;
long entriesSize;
long dataStart;
long dataSize;
magic = Encoding.Unicode.GetString(br.ReadBytes(32));
//magic = br.ReadBytes(32);
if (magic != MAGIC)
{
Console.WriteLine("this is not a ava dict bin file");
return;
}
entryCount = br.ReadInt64();
headerSize = br.ReadInt64();
entriesSize = br.ReadInt64();
dataStart = br.ReadInt64();
dataSize = br.ReadInt64();
Dictionary<string, string> entries = new Dictionary<string, string>();
for(int i = 0; i< entryCount; i++)
{
long keyOffset = br.ReadInt64();
int keySize = br.ReadInt32();
string key = ReadStringAtOffset(br, keyOffset + dataStart, keySize);
//IDs.Add(RemoveNewLine(ID));
long valueOffset = br.ReadInt64();
int valueSize = br.ReadInt32();
string value = ReadStringAtOffset(br, valueOffset + dataStart, valueSize);
entries.Add(key, value);
}
var jsonString = JsonConvert.SerializeObject(entries, Formatting.Indented);
string modifiedPath = Path.ChangeExtension(AddSuffix(file.FullName,"-modified"), ".json");
File.WriteAllText(modifiedPath, jsonString);
}
}
static string ReadStringAtOffset(BinaryReader br, long offset, int length)
{
long position = br.BaseStream.Position;
br.BaseStream.Seek(offset,SeekOrigin.Begin);
string s = Encoding.UTF8.GetString(br.ReadBytes(length));
br.BaseStream.Seek(position, SeekOrigin.Begin);
return s;
}
}
}