Skip to content

Commit

Permalink
- thcrap_loader: implement the "Update when the game exits" checkbox [V]
Browse files Browse the repository at this point in the history
- thcrap: add 2 parameters to thcrap_inject_into_new, to return the
handles to the caller if he wants them. Also fixes a handle leak.
  • Loading branch information
brliron committed Mar 27, 2018
1 parent eb76467 commit 3351d71
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
15 changes: 14 additions & 1 deletion thcrap/src/inject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ int thcrap_inject_into_running(HANDLE hProcess, const char *run_cfg_fn)
return ret;
}

BOOL thcrap_inject_into_new(const char *exe_fn, char *args)
BOOL thcrap_inject_into_new(const char *exe_fn, char *args, HANDLE *hProcess, HANDLE *hThread)
{
int ret = 0;
STRLEN_DEC(exe_fn);
Expand Down Expand Up @@ -786,6 +786,19 @@ BOOL thcrap_inject_into_new(const char *exe_fn, char *args)
}
VLA_FREE(exe_fn_local);
VLA_FREE(exe_dir_local);

if (hProcess) {
*hProcess = pi.hProcess;
}
else {
CloseHandle(pi.hProcess);
}
if (hThread) {
*hThread = pi.hThread;
}
else {
CloseHandle(pi.hThread);
}
return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion thcrap/src/inject.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ int thcrap_inject_into_running(HANDLE hProcess, const char *run_cfg_fn);

// Starts [exe_fn] as a new process with the given command-line arguments, and
// injects thcrap with the current run configuration into it.
BOOL thcrap_inject_into_new(const char *exe_fn, char *args);
BOOL thcrap_inject_into_new(const char *exe_fn, char *args, HANDLE *hProcess, HANDLE *hThread);
2 changes: 1 addition & 1 deletion thcrap/src/thcrap_update_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ ASSERT_FUNCTION_PROTO(stack_update);
BOOL loader_update_with_UI_wrapper(const char *exe_fn, char *args)
{
CALL_WRAPPED_FUNCTION(loader_update_with_UI, exe_fn, args)
return thcrap_inject_into_new(exe_fn, args);
return thcrap_inject_into_new(exe_fn, args, NULL, NULL);
}
ASSERT_FUNCTION_PROTO(loader_update_with_UI);

Expand Down
19 changes: 16 additions & 3 deletions thcrap_update/src/loader_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static LRESULT CALLBACK loader_update_proc(HWND hWnd, UINT uMsg, WPARAM wParam,
EnterCriticalSection(&state->cs);
state->game_started = true;
LeaveCriticalSection(&state->cs);
thcrap_inject_into_new(state->exe_fn, state->args);
thcrap_inject_into_new(state->exe_fn, state->args, NULL, NULL);
}
break;

Expand Down Expand Up @@ -303,7 +303,8 @@ DWORD WINAPI loader_update_window_create_and_run(LPVOID param)
331, 105, 153, 23, state->hwnd[HWND_MAIN], (HMENU)HWND_BUTTON_RUN, hMod, NULL);

// Settings and logs
state->hwnd[HWND_CHECKBOX_UPDATE_AT_EXIT] = CreateWindowW(L"Button", L"Install updates after running the game", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
state->hwnd[HWND_CHECKBOX_UPDATE_AT_EXIT] = CreateWindowW(L"Button", L"Install updates after running the game (requires a restart)",
WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
5, 145, 480, 18, state->hwnd[HWND_MAIN], (HMENU)HWND_CHECKBOX_UPDATE_AT_EXIT, hMod, NULL);
if (state->update_at_exit) {
CheckDlgButton(state->hwnd[HWND_MAIN], HWND_CHECKBOX_UPDATE_AT_EXIT, BST_CHECKED);
Expand Down Expand Up @@ -491,6 +492,18 @@ BOOL loader_update_with_UI(const char *exe_fn, char *args)
hLogEdit = state.hwnd[HWND_EDIT_LOGS];
log_set_hook(log_callback, log_ncallback);

if (state.update_at_exit) {
EnterCriticalSection(&state.cs);
// We didn't enable the "start game" button yet, the game can't be running.
state.game_started = true;
LeaveCriticalSection(&state.cs);

SetWindowTextW(state.hwnd[HWND_LABEL_STATUS], L"Waiting until the game exits...");
HANDLE hProcess;
ret = thcrap_inject_into_new(exe_fn, args, &hProcess, NULL);
WaitForSingleObject(hProcess, INFINITE);
}

stack_update(update_filter_global, NULL, loader_update_progress_callback, &state);

// Update the thcrap engine
Expand Down Expand Up @@ -522,7 +535,7 @@ BOOL loader_update_with_UI(const char *exe_fn, char *args)
state.game_started = true;
LeaveCriticalSection(&state.cs);
if (game_started == false) {
ret = thcrap_inject_into_new(exe_fn, args);
ret = thcrap_inject_into_new(exe_fn, args, NULL, NULL);
}
if (state.background_updates) {
int time_between_updates;
Expand Down

0 comments on commit 3351d71

Please sign in to comment.