-
Notifications
You must be signed in to change notification settings - Fork 7
/
asi.h
169 lines (137 loc) · 4.57 KB
/
asi.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
#pragma once
#ifndef _ASI
#define _ASI
#include "pch.h"
#include "log.h"
// Window title and class used for FindWindow
// to get hWnd for WndProc hook for reload shortcut
#define WindowTitle "Starfield"
#define WindowClass "Starfield"
namespace asi
{
uint32_t reloadKey = VK_F11;
uint32_t reloadKeyModifier = VK_LSHIFT;
std::string relativePath = "SFAE ASIL";
std::atomic_bool want_reload = false;
fs::path plugin_directory;
std::vector<std::pair<std::string, HMODULE>> plugins;
bool validate_directory()
{
if (!fs::exists(plugin_directory))
return false;
if (!fs::is_directory(plugin_directory))
return false;
return true;
}
bool is_module_loaded(HMODULE hModule)
{
MODULEINFO info;
return GetModuleInformation(GetCurrentProcess(), hModule, &info, sizeof(info));
}
bool is_module_loaded(const char* name)
{
return GetModuleHandleA(name) != 0;
}
void unload_plugins()
{
for (auto it = plugins.begin(); it != plugins.end();)
{
info_no_newline("Unloading \"%s\".. ", it->first.c_str());
if (is_module_loaded(it->second) ? FreeLibrary(it->second) : true)
{
log("Done\n");
it = plugins.erase(it);
continue;
}
log("Failed [Err=%d]\n", GetLastError());
it++;
}
}
void load_plugins()
{
if (!validate_directory())
return;
for (const auto& entry : fs::directory_iterator(plugin_directory))
{
auto ext = entry.path().extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
if (ext == ".asi" || ext == ".dll")
{
auto filename = entry.path().filename().generic_string();
auto filepath = entry.path().generic_string();
if (is_module_loaded(filename.c_str()))
{
info("Already Loaded: \"%s\"", filename.c_str());
}
info_no_newline("Loading \"%s\".. ", filename.c_str());
auto hModule = LoadLibraryA(filepath.c_str());
if (!hModule)
{
log("Failed [Err=%d]\n", GetLastError());
continue;
}
log("Done\n");
plugins.push_back({ filename, hModule });
}
}
}
WNDPROC orgWndProc;
// WndProc Hook
// We read input here for the plugin reload hotkey
LRESULT __stdcall WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_KEYDOWN:
// If the key down is our RELOAD_KEY
if (reloadKey == wParam)
{
// And if we have a modifier key it must be pressed also
if (reloadKeyModifier && !(GetAsyncKeyState(reloadKeyModifier) & 0x8000))
break;
want_reload = true;
}
break;
default:
break;
}
// Call the original WndProc or the game won't get any input
return CallWindowProcW((WNDPROC)orgWndProc, hWnd, uMsg, wParam, lParam);
}
void start(std::string relativePath, uint32_t reloadKey, uint32_t reloadKeyModifier)
{
asi::reloadKey = reloadKey;
asi::reloadKeyModifier = reloadKeyModifier;
asi::relativePath = relativePath;
CreateThread(
0, 0,
[](PVOID)->DWORD
{
plugin_directory = fs::current_path() / asi::relativePath;
load_plugins();
short reload{}, reload_mod = true;
while (true)
{
// If WndProc hook isn't set up, try to set it up
if (!orgWndProc)
{
auto hWnd = FindWindowA(WindowClass, WindowTitle);
if (hWnd)
{
orgWndProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)WndProc);
}
}
if (want_reload)
{
info("Reloading Plugins");
unload_plugins();
load_plugins();
want_reload = false;
}
std::this_thread::sleep_for(100ms);
}
},
0, 0, 0);
}
}
#endif