-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
279 lines (240 loc) · 7.87 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
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Common.Shared.Min.Extensions;
using IO.Extensions;
using SoE.Models.Enums;
using SRAM.SoE.Helpers;
using SRAM.SoE.Models.Structs;
using WRAM.Snes9x.Helpers;
namespace SRAM.SoE
{
public static class Program
{
private const string Delimiter = ", ";
public static void Main(string[] args)
{
Console.BufferHeight = 1000;
Console.WindowHeight = 50;
try
{
if (args.Length == 0)
throw new ArgumentException("Pass a file as argument.");
var filePath = args[0];
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Reading file:");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(filePath);
Console.WriteLine();
var savestateFile = SavestateReader.Load(filePath);
var sramFile = SavestateWramHelper.CreateSramFileFromSavestate(savestateFile, GameRegion.EnglishNtsc);
var fileName = Path.GetFileNameWithoutExtension(filePath);
var srmFilePath = Path.Join(Path.GetDirectoryName(filePath), fileName + ".srm");
if (File.Exists(srmFilePath))
File.Copy(srmFilePath, srmFilePath + ".backup", true);
sramFile.Save(srmFilePath);
DumpWram();
DumpWSram();
ShowCommands(filePath);
Console.ForegroundColor = ConsoleColor.Gray;
while (true)
{
var key = Console.ReadLine();
try
{
switch (key)
{
case "":
continue;
case "q":
return;
case "?":
ShowCommands(filePath);
break;
case "cls":
Console.Clear();
break;
case "vars":
ShowVariables();
break;
case "slot":
Console.WriteLine(sramFile.GetSegment(0).ToString());
break;
case { } when key.StartsWith("var "):
ShowVariable(sramFile.GetSegment(0).Data, key);
break;
case { } when key.StartsWith("byte"):
ShowOffsetNumber(sramFile.GetSegmentBytes(0), key);
break;
case { } when key.StartsWith("char"):
ShowOffsetCharacters(sramFile.GetSegmentBytes(0), key);
break;
case { } when key.StartsWith("string"):
ShowOffsetString(sramFile.GetSegmentBytes(0), key);
break;
default:
Console.WriteLine(@$"Unknown command name: ""{key}""");
break;
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR:" + ex.Message);
Console.WriteLine();
Console.ReadKey();
}
}
void DumpWSram()
{
Console.ForegroundColor = ConsoleColor.Green;
var file = $"2_W-SramDump_{sramFile.Size}_bytes.txt";
File.WriteAllBytes(file, sramFile.Buffer);
Console.WriteLine($"Dumped W-S-RAM ({sramFile.Size} bytes): {file}");
var bytes = sramFile.GetSegmentBytes(0);
file = $"3_SaveSlot0Dump_{bytes.Length}_bytes.txt";
File.WriteAllBytes(file, bytes);
Console.WriteLine($"Dumped SaveSlot0 ({bytes.Length} bytes): {file}");
Console.WriteLine();
Console.ResetColor();
}
void DumpWram()
{
Console.ForegroundColor = ConsoleColor.Green;
var data = savestateFile.RAM.Data;
var file = $"1_WramDump_{data.Length}_bytes.txt";
File.WriteAllBytes(file, data);
Console.WriteLine($"Dumped WRAM ({data.Length} bytes): {file}");
Console.ResetColor();
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR:" + ex.Message);
Console.WriteLine();
Console.ReadKey();
}
}
private static void ShowOffsetNumber(byte[] saveSlotBytes, string key)
{
Console.ForegroundColor = ConsoleColor.Yellow;
var valueIndex = key.IndexOf(" ") + 1;
var sizeIndex = key.IndexOf(":");
string value;
int offset;
if (sizeIndex == -1)
{
offset = ParseInt(key[valueIndex..]);
value = saveSlotBytes[offset].ToString();
}
else
{
offset = ParseInt(key.Substring(valueIndex, sizeIndex - valueIndex));
var size = ParseInt(key[(sizeIndex + 1)..]);
value = saveSlotBytes[offset..(offset + size + 1)].Format();
}
Console.WriteLine($"{offset}: {value}");
Console.WriteLine();
Console.ResetColor();
}
private static int ParseInt(string input)
{
if (input.StartsWith("x"))
return int.Parse(input.Remove("x")!, NumberStyles.AllowHexSpecifier);
return int.Parse(input);
}
private static void ShowOffsetString(byte[] saveSlotBytes, string key)
{
Console.ForegroundColor = ConsoleColor.Yellow;
string value;
int offset;
var valueIndex = key.IndexOf(" ") + 1;
var sizeIndex = key.IndexOf(":");
if (sizeIndex == -1)
{
offset = ParseInt(key[valueIndex..]);
value = ((char)saveSlotBytes[offset]).ToString();
}
else
{
offset = ParseInt(key.Substring(valueIndex, sizeIndex - valueIndex));
var size = ParseInt(key[(sizeIndex + 1)..]);
value = Encoding.ASCII.GetString(saveSlotBytes[offset..(offset + size + 1)]);
}
Console.WriteLine($"{offset}: [{value}]");
Console.WriteLine();
Console.ResetColor();
}
private static void ShowOffsetCharacters(byte[] saveSlotBytes, string key)
{
Console.ForegroundColor = ConsoleColor.Yellow;
string value;
int offset;
var valueIndex = key.IndexOf(" ") + 1;
var sizeIndex = key.IndexOf(":");
if (sizeIndex == -1)
{
offset = ParseInt(key[valueIndex..]);
value = ((char)saveSlotBytes[offset]).ToString();
}
else
{
offset = ParseInt(key[valueIndex..sizeIndex]);
var size = ParseInt(key[(sizeIndex + 1)..]);
value = Encoding.ASCII.GetChars(saveSlotBytes[offset..(offset + size + 1)]).Join(Delimiter);
}
Console.WriteLine($"{offset}: [{value}]");
Console.WriteLine();
Console.ResetColor();
}
private static void ShowCommands(string savestateFile)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Commands:");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("?: This command list");
Console.WriteLine("sramoffset: Setzt das W-S-RAM-Offset");
Console.WriteLine("vars: List saveslot variable names");
Console.WriteLine("sslot: Shows the S-RAM slot 0 content");
Console.WriteLine("wslot: Shows the W-RAM slot 0 content");
Console.WriteLine("var {name}: Shows the text value of that variable name");
Console.WriteLine("byte {offset}: Shows the byte at offset X as number");
Console.WriteLine("bytes {offset:size}: Shows the bytes at offset X with");
Console.WriteLine("char {offset}: Shows the char at offset X as number");
Console.WriteLine("chars {offset:size}: Shows the chars at offset X with");
Console.WriteLine("string {offset:size}: Shows the ANSII string at the given offset");
Console.WriteLine("cls: Clears the window");
Console.WriteLine("q: Quit");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Cmd-Args:");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Arg {{0}} ~ Savestate file: [{savestateFile}");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Hint: Hex-numbers must be prefixed with x (e.g. x100)");
Console.WriteLine();
Console.ResetColor();
}
private static void ShowVariables()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(typeof(SaveSlotDataSoE).GetPublicInstanceFields().Select(e => e.Name).Join(Environment.NewLine));
Console.WriteLine();
Console.ResetColor();
}
private static void ShowVariable(SaveSlotDataSoE saveSlot, string key)
{
var name = key[4..].Trim();
var fieldInfo = saveSlot.GetType().GetPublicInstanceFields().SingleOrDefault(e => e.Name.EqualsInsensitive(name));
if (fieldInfo is null)
throw new ArgumentException(@$"Field ""{key}"" does not exist");
var value = fieldInfo!.GetValue(saveSlot)?.ToString();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"{fieldInfo.Name}: {value}");
Console.WriteLine();
Console.ResetColor();
}
}
}