-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsfae.h
349 lines (306 loc) · 12.2 KB
/
sfae.h
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
#pragma once
#ifndef _SFAE
#define _SFAE
#include "pch.h"
#include "log.h"
#include "cfg.h"
#include "asi.h"
#include "memory.h"
#include "hook.h"
#define SFAE_VERSION "1.4.6"
using namespace memory;
namespace sfae
{
class Settings
{
#pragma region PropertyMacro
#define Property(type, name, defaultValue, comment)\
private:\
type _##name = _cfg.get<type>(#name, defaultValue, comment);\
public:\
type get##name()\
{\
return _##name;\
}\
\
void set##name(type value)\
{\
_##name = value;\
_cfg.set<type>(#name, value);\
_cfg.save();\
}
#pragma endregion
private:
cfg::cfg _cfg;
Property(bool, ShowConsole, false, "Show a console window with the log output")
Property(bool, ShowInGameMessage, false, "Show a message when you open the in-game console for the first time\nto tell you if SFAE is working or not")
Property(bool, RunInBackground, true, "Stop the game from pausing when in background")
Property(bool, EnableASILoader, false, "Enable the ASI loader")
Property(std::string, ASILoaderRelativePath, "SFAE ASIL", "The relative path to the directory to load .asi/.dll files from")
Property(uint32_t, ASILoaderReloadKey, VK_F11, "The reload key used in the ASI loader\nDefault is F11\nFind key codes at https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes")
Property(uint32_t, ASILoaderReloadKeyModifier, VK_LSHIFT, "The reload key modifier used in the ASI loader\nDefault is left shift\nSet to 0 for no modifier key")
public:
Settings(const char* filePath)
: _cfg(filePath)
{
_cfg.save();
}
} settings("sfae.cfg");
namespace pointers
{
memory::handle everModded;
memory::handle jsonReadBool;
}
/// <summary>
/// We hook a function that the game uses to read information about Creation mods and set every mod's "achievement_friendly" value to true
/// </summary>
inline Hook* hkAchievementFriendly{};
inline char __fastcall achievementFriendlyHk(const char** a1, const char* valueName, __int64 resultPtr, char a4, char a5)
{
auto result = hkAchievementFriendly->original<decltype(&achievementFriendlyHk)>()(a1, valueName, resultPtr, a4, a5);
if (valueName && strcmp(valueName, "achievement_friendly") == 0 && resultPtr && !*(bool*)resultPtr)
{
*((bool*)resultPtr) = true;
}
return result;
}
void loadConsole()
{
// we've moded over to a .cfg file instead of the console file,
// but for now we'll update the cfg setting if the console file exists
std::filesystem::path currentDirectory = std::filesystem::current_path();
auto
file1 = currentDirectory / "sfae.console",
file2 = currentDirectory / "sfae.console.txt";
if (fs::exists(file1) ||
fs::exists(file2))
{
if (!settings.getShowConsole())
settings.setShowConsole(true);
util::remove_file(file1);
util::remove_file(file2);
}
if (
settings.getShowConsole() &&
AllocConsole() &&
freopen("CONOUT$", "w", stdout))
{
char buf[128];
sprintf_s(buf, 128, "Starfield Achievement Enabler %s", SFAE_VERSION);
SetConsoleTitleA(buf);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
}
void start()
{
// this string is present within Starfield.exe
// if it is not present in the running process module then we abort
if (!memory::pattern::find_string("$CannotTravelToAnUnknownBody"))
{
return;
}
// load console if needed
loadConsole();
info("Version %s Loaded in %s", SFAE_VERSION, memory::getCurrentModuleFileName().c_str());
info("Initializing..");
// create code patches
// this patch is for disabling the games mod detection
auto modCheck = Patch(
"Mod Check",
{
0x31, // xor eax, eax
0xC0, //
0xC3, // ret
},
{
{"E8 ? ? ? ? 84 ? ? ? ? 74 ? 41 ? ? 80 ? ? ? ? ? FB"},
},
[](memory::handle& ptr)
{
ptr = ptr.resolve_relative_call();
return true;
}
);
// this patch is for disabling a check within the function that references string "Achievement %d awarded"
auto achievementAwarded = Patch(
"Achievement Awarded",
{
0x90, // nop
0x90 // nop
},
{
{"48 ? ? ? ? ? ? 83 ? ? 01 75 ? E8 ? ? ? ? 4C", 11},
{"83 ? ? 75 ? ? ? ? ? ? ? ? ? ? ? ? ? E8 ? ? ? ? ? ? ? ? ? ? ? ? 75 ? E8 ? ? ? ? ? ? ? ? ? ? ? ? 41", 30}
});
// this patch is for the message that pops up when you try to load a save while mods are loaded
auto modsMessage = Patch(
"Mods Message",
{
0x90, // nop
0x90, // nop
0x90, // nop
0x90, // nop
0x90, // nop
0x90 // nop
},
{ {"89 ? ? ? ? ? ? ? 48 ? ? ? ? ? ? E8 ? ? ? ? 48 ? ? ? E8 ? ? ? ? 4D ? ? ? 04 01 00 00 48 ? ? ? ? ? ? FF"} });
// this patch is for the message it shows in the console warning popup
auto consoleMessageText = StringPatch(
"Console Message Text",
"_",
"$UsingConsoleMayDisableAchievements");
consoleMessageText.set_text("SFAE %s: Working", SFAE_VERSION);
// this patch is for the message it shows in the mods loaded warning popup
auto modsMessageText = StringPatch(
"Mods Message Text",
"_",
"$LoadVanillaSaveWithMods");
modsMessageText.set_text("SFAE %s: Working", SFAE_VERSION);
// this patch is for disabling the console warning popup
auto consoleMessage = Patch(
"Console Message",
{
0xC3, // ret
0x90, // nop
0x90, // nop
0x90, // nop
0x90 // nop
},
{ {"48 ? ? ? ? ? 48 ? ? ? 48 ? ? 80 ? ? 00 0F ? ? ? ? ? 48 ? ? ? ? ? ? 48 ? ? ? ? 00 00 00 00"} });
// find Achievement Friendly
if (!pattern::find("C6 ? ? ? ? ? 01 4C ? ? ? ? ? ? C6 ? ? ? 00 ? ? ? 48 ? ? ? ? ? ? 48 ? ? ? ? E8 ? ? ? ? ? ? ? ? ? ? ? ? 4C ? ? ? ? ? ? C6 ? ? ? 00 45 ? ?", &pointers::jsonReadBool))
{
err("Couldn't Find \"Achievement Friendly\"");
}
else
{
pointers::jsonReadBool = pointers::jsonReadBool.add(35).rip();
info("Found \"Achievement Friendly\" -> %s+%08X", memory::getCurrentModuleFileName().c_str(), pointers::jsonReadBool.as<uint8_t*>() - (uint8_t*)GetModuleHandle(0));
hkAchievementFriendly = new DetourHook("Achievement Friendly", pointers::jsonReadBool.raw(), achievementFriendlyHk);
if (!hkAchievementFriendly->enable())
{
info("Couldn't hook \"Achievement Friendly\"");
}
else
{
info("Hooked \"Achievement Friendly\"");
}
}
// find everModded
if (!pattern::find("40 ? 48 ? ? ? 48 ? ? ? ? ? ? 4C ? ? ? ? ? ? ? ? C6 ? ? ? ? ? 01 E8 ? ? ? ? 65 ? ? ? ? ? ? ? ? 48 ? ? B8 ? ? ? ? ? ? ? 00 75", &pointers::everModded))
{
err("Couldn't Find \"Ever Modded\"");
}
else
{
pointers::everModded = pointers::everModded.add(24).rip().add(1);
info("Found \"Ever Modded\" -> %s+%08X", memory::getCurrentModuleFileName().c_str(), pointers::everModded.as<uint8_t*>() - (uint8_t*)GetModuleHandle(0));
}
// these patches are for allowing the game to run properly while tabbed out
auto backgroundCheck1 = Patch(
"Background Check 1",
{
0xEB, // jne -> jmp
},
{ {"75 ? 40 ? ? 75 ? 33 ? ? ? 01 E8 ? ? ? ? 48 ? ? ? ? ? ? 48 ? ? 74 ? 48 ? ? ? ? ? ? E8"} });
modsMessageText.enable();
consoleMessageText.enable();
if (!modCheck.is_valid() ||
!modCheck.enable() ||
!achievementAwarded.is_valid() ||
!achievementAwarded.enable() ||
!pointers::everModded.raw())
{
consoleMessageText.set_text("SFAE %s: Not working", SFAE_VERSION);
modsMessageText.set_text("SFAE %s: Not working", SFAE_VERSION);
}
else
{
modsMessage.enable();
if (!settings.getShowInGameMessage())
consoleMessage.enable();
}
// Check if all patches and everModded are valid
if (!modCheck.is_valid() ||
!achievementAwarded.is_valid() ||
!hkAchievementFriendly->enabled() ||
!modsMessage.is_valid() ||
!modsMessageText.is_valid() ||
!consoleMessage.is_valid() ||
!consoleMessageText.is_valid() ||
//!backgroundCheck1.is_valid() ||
!pointers::everModded.raw())
{
const char* fmt =
"SFAE Version:\t%s\n"
"Main Module:\t%s\n\n"
"At least one signature has not been found\n"
"Mod Check:\t%s\n"
"Achi Awarded:\t%s\n"
"Achi Friendly:\t%s\n"
"Mods Message:\t%s\n"
"Mods Msg Text:\t%s\n"
"Console Message:\t%s\n"
"Console Msg Text:\t%s\n"
//"Bkgrnd Check 1:\t%s\n"
"Ever Modded:\t%s\n\n"
"Will you get achievements?\n"
"With mods?\t%s\n"
"Using console?\t%s";
util::fmt_msgbox(
NULL,
"SFAE",
MB_ICONWARNING,
fmt,
SFAE_VERSION,
memory::getCurrentModuleFileName().c_str(),
modCheck.is_valid() ? "Found" : "Not Found",
achievementAwarded.is_valid() ? "Found" : "Not Found",
hkAchievementFriendly->enabled() ? "Found" : "Not Found",
modsMessage.is_valid() ? "Found" : "Not Found",
modsMessageText.is_valid() ? "Found" : "Not Found",
consoleMessage.is_valid() ? "Found" : "Not Found",
consoleMessageText.is_valid() ? "Found" : "Not Found",
//backgroundCheck1.is_valid() ? "Found" : "Not Found",
pointers::everModded.raw() ? "Found" : "Not Found",
(modCheck.is_valid() && modCheck.is_enabled()) ? "Yes" : "No",
pointers::everModded.raw() ? "Yes" : "No"
);
}
if (settings.getRunInBackground() && backgroundCheck1.is_valid())
{
backgroundCheck1.enable();
}
// do not go into the loop if we did not find "Ever Modded"
if (pointers::everModded.raw())
{
// enter an infinite loop to monitor everModded for the duration of game session
// should run about 200 times per second
info("Monitoring \"Ever Modded\"");
CreateThread(nullptr, 0, [](PVOID) -> DWORD
{
auto ptr = pointers::everModded.as<uint8_t*>();
while (true)
{
// if everModded is not 0, change it back to 0
if (*ptr != 0)
{
*ptr = 0;
info("Blocked \"Ever Modded\" Change");
}
std::this_thread::sleep_for(5ms);
}
}, 0, 0, 0);
}
if (settings.getEnableASILoader())
{
asi::start(
settings.getASILoaderRelativePath(),
settings.getASILoaderReloadKey(),
settings.getASILoaderReloadKeyModifier()
);
}
info("Done Initializing");
}
}
#endif // !_SFAE