1
0
mirror of https://github.com/djhackersdev/bemanitools.git synced 2025-01-31 12:13:42 +01:00

launcher: Add before-hook support

Required to hook certain things that happen on module load / avs boot:
 - network adapter hooking for avs
 - envvar setting

Note: the logging facilities are NOT setup yet at this point
so these hooks cannot use avs for logging
This commit is contained in:
Will Xyen 2020-04-04 23:37:10 -07:00
parent e8943edff6
commit 0d3fa54316
3 changed files with 44 additions and 23 deletions

View File

@ -93,10 +93,34 @@ static AVS_LOG_WRITER(log_callback, chars, nchars, ctx)
free(utf16);
}
static void load_hook_dlls(struct array* hook_dlls) {
const char *hook_dll;
for (size_t i = 0; i < hook_dlls->nitems; i++) {
hook_dll = *array_item(char *, hook_dlls, i);
if (LoadLibraryA(hook_dll) == NULL) {
LPSTR buffer;
FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR) &buffer,
0,
NULL);
log_fatal("%s: Failed to load hook DLL: %s", hook_dll, buffer);
LocalFree(buffer);
}
}
}
int main(int argc, const char **argv)
{
int i;
const char *hook_dll;
bool ok;
HANDLE logfile;
@ -170,6 +194,8 @@ int main(int argc, const char **argv)
log_fatal("%s: /config missing", options.avs_config_path);
}
load_hook_dlls(&options.before_hook_dlls);
avs_context_init(
avs_config_root,
options.avs_heap_size,
@ -188,27 +214,7 @@ int main(int argc, const char **argv)
/* Load hook DLLs */
for (i = 0; i < options.hook_dlls.nitems; i++) {
hook_dll = *array_item(char *, &options.hook_dlls, i);
if (LoadLibraryA(hook_dll) == NULL) {
LPSTR buffer;
FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR) &buffer,
0,
NULL);
log_fatal("%s: Failed to load hook DLL: %s", hook_dll, buffer);
LocalFree(buffer);
}
}
load_hook_dlls(&options.hook_dlls);
/* Inject GetModuleHandle hooks */

View File

@ -23,6 +23,7 @@ void options_init(struct options *options)
options->logfile = NULL;
options->remote_debugger = false;
array_init(&options->hook_dlls);
array_init(&options->before_hook_dlls);
}
bool options_read_cmdline(struct options *options, int argc, const char **argv)
@ -115,6 +116,16 @@ bool options_read_cmdline(struct options *options, int argc, const char **argv)
break;
case 'B':
if (i + 1 >= argc) {
return false;
}
*array_append(const char *, &options->before_hook_dlls) =
argv[++i];
break;
case 'Y':
if (i + 1 >= argc) {
return false;
@ -170,6 +181,8 @@ void options_print_usage(void)
" -R [pcbid] Specify Soft ID (default: use ea3 config)\n"
" -K [filename] Load hook DLL (can be specified multiple "
"times)\n"
" -B [filename] Load pre-hook DLL loaded before avs boot "
"(can be specified multiple times)\n"
" -Y [filename] Log to a file in addition to the console\n"
" -D Halt the launcher before bootstrapping AVS "
"until a"
@ -179,4 +192,5 @@ void options_print_usage(void)
void options_fini(struct options *options)
{
array_fini(&options->hook_dlls);
array_fini(&options->before_hook_dlls);
}

View File

@ -17,6 +17,7 @@ struct options {
const char *module;
const char *logfile;
struct array hook_dlls;
struct array before_hook_dlls;
bool remote_debugger;
};