-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamWorldMemory.cs
More file actions
251 lines (223 loc) · 8.31 KB
/
SteamWorldMemory.cs
File metadata and controls
251 lines (223 loc) · 8.31 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using LiveSplit.Memory;
namespace LiveSplit.SteamWorldDig {
public partial class SteamWorldMemory {
public Process Program { get; set; }
public bool IsHooked { get; set; } = false;
private DateTime lastHooked;
private static Dictionary<string, byte[]> upgradeSegments = new Dictionary<string, byte[]>();
private static Dictionary<string, IntPtr> upgradePointers = new Dictionary<string, IntPtr>();
private static Dictionary<string, byte[]> areaSegments = new Dictionary<string, byte[]>();
private static Dictionary<string, IntPtr> areaPointers = new Dictionary<string, IntPtr>();
private static Dictionary<string, byte[]> exitSegments = new Dictionary<string, byte[]>();
private static Dictionary<string, IntPtr> exitPointers = new Dictionary<string, IntPtr>();
public SteamWorldMemory() {
lastHooked = DateTime.MinValue;
}
public List<IntPtr> FindAllUpgrades()
{
// First locate all occurences of "upgrade_podium"
List<IntPtr> upgrades = Program.FindAllSignatures("upgrade_podium");
// Remove the ones that are not related to a level upgrade
for (int i = upgrades.Count - 1; i >= 0; i--)
{
int type = Program.Read<int>(upgrades[i] + 0x30);
int size = Program.Read<int>(upgrades[i] + 0x2c);
if ((size == 0) || (type != 0x0F && type != 0x1F))
{
upgrades.RemoveAt(i);
}
}
return upgrades;
}
public void SaveAllUpgrades()
{
List<IntPtr> upgrade_ptrs = FindAllUpgrades();
foreach (IntPtr ptr in upgrade_ptrs)
{
byte[] segment = Program.Read(ptr + 0x1C, 0x18);
string key;
if (Program.Read<int>(ptr + 0x2C) < 0x10)
{
key = Program.ReadAscii(ptr + 0x1C);
}
else
{
key = Program.ReadAscii((IntPtr)Program.Read<uint>(ptr + 0x1C));
}
upgradeSegments[key] = segment;
upgradePointers[key] = ptr + 0x1C;
}
}
public string PrintAllUpgrades()
{
StringBuilder sb = new StringBuilder();
foreach (var ptr in upgradePointers)
{
sb.Append(ptr.Key).Append(',');
}
if (sb.Length > 0)
{
sb.Length--;
}
return sb.ToString();
}
public List<string> GetUpgradeNames()
{
List<string> upgrades = new List<string>(upgradePointers.Keys);
upgrades.Sort();
return upgrades;
}
public void RandomizeUpgrade(string oldUpgrade, string newUpgrade)
{
Program.Write(upgradePointers[oldUpgrade], upgradeSegments[newUpgrade]);
}
public List<IntPtr> FindAllAreaEntries()
{
// First locate all occurences of "[start]"
List<IntPtr> areas = Program.FindAllSignatures("[start]");
// Remove the ones that are not a real area
for (int i = areas.Count - 1; i >= 0; i--)
{
int type = Program.Read<int>(areas[i] - 0x08);
uint size = Program.Read<uint>(areas[i] - 0x0c);
if ((size == 0 || size > 0x80) || (type != 0x0F && type != 0x1F))
{
areas.RemoveAt(i);
}
}
return areas;
}
public void SaveAllAreaEntries()
{
List<IntPtr> areas_ptrs = FindAllAreaEntries();
foreach (IntPtr ptr in areas_ptrs)
{
byte[] segment = Program.Read(ptr - 0x1C, 0x34);
string key;
if (Program.Read<int>(ptr - 0x0c) < 0x10)
{
key = Program.ReadAscii(ptr - 0x1C);
}
else
{
key = Program.ReadAscii((IntPtr)Program.Read<uint>(ptr - 0x1C));
}
// Don't randomize the boss room
if (!key.Contains("boss"))
{
areaSegments[key] = segment;
areaPointers[key] = ptr - 0x1C;
}
}
}
public List<string> GetAreaNames()
{
List<string> areas = new List<string>(areaPointers.Keys);
areas.Sort();
return areas;
}
public string PrintAllAreaEntries()
{
return String.Join(", ", GetAreaNames());
}
public List<IntPtr> FindAllAreaExits()
{
List<IntPtr> areas = Program.FindAllSignatures("PlayerExit1");
areas.AddRange(Program.FindAllSignatures("PlayerExit2"));
// Remove the ones that are not a real exits
for (int i = areas.Count - 1; i >= 0; i--)
{
int type = Program.Read<int>(areas[i] + 0x54);
uint size = Program.Read<uint>(areas[i] + 0x50);
if ((size == 0 || size > 0x80) || (type != 0x0F && type != 0x1F && type != 0x2F))
{
areas.RemoveAt(i);
}
}
return areas;
}
public void SaveAllAreaExits()
{
List<IntPtr> areas_ptrs = FindAllAreaExits();
foreach (IntPtr ptr in areas_ptrs)
{
byte[] segment = Program.Read(ptr + 0x24, 0x34);
string key;
if (Program.Read<int>(ptr + 0x50) < 0x10)
{
key = Program.ReadAscii(ptr + 0x40);
}
else
{
key = Program.ReadAscii((IntPtr)Program.Read<uint>(ptr + 0x40));
}
// Special case: Vectron entry is marked as an exit, so inserting it in the entries
if (key == "from_oldworld")
{
string to = Program.ReadAscii(ptr + 0x24);
if (to == "vectron")
{
areaSegments["vectron"] = segment;
areaPointers["vectron"] = ptr + 0x24;
continue;
}
}
// Insert the vectron exit manually because we may not have inserted the vectron entry yet.
if (key == "from_vectron")
{
exitSegments["vectron"] = segment;
exitPointers["vectron"] = ptr + 0x24;
continue;
}
// Filter keys that are not "from_xxx" where xxx is an area entry
foreach (var entryptr in areaPointers)
{
if (key == ("from_" + entryptr.Key))
{
exitSegments[entryptr.Key] = segment;
exitPointers[entryptr.Key] = ptr + 0x24;
break;
}
}
}
}
public List<string> GetExitNames()
{
List<string> areas = new List<string>(exitPointers.Keys);
areas.Sort();
return areas;
}
public string PrintAllAreaExits()
{
return String.Join(", ", GetExitNames());
}
public void RandomizeArea(string oldArea, string newArea)
{
Program.Write(areaPointers[oldArea], areaSegments[newArea]);
Program.Write(exitPointers[newArea], exitSegments[oldArea]);
//Program.Write(exitPointers[oldArea], exitSegments[newArea]);
}
public bool HookProcess() {
if ((Program == null || Program.HasExited) && DateTime.Now > lastHooked.AddSeconds(1)) {
lastHooked = DateTime.Now;
Process[] processes = Process.GetProcessesByName("SteamWorldDig");
Program = processes.Length == 0 ? null : processes[0];
IsHooked = true;
}
if (Program == null || Program.HasExited) {
IsHooked = false;
}
return IsHooked;
}
public void Dispose() {
if (Program != null) {
Program.Dispose();
}
}
}
}