mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-21 21:05:56 +00:00
Offer Restart now under the button prompts while they change after restart
The row opens a confirmation; Restart writes totk.ini, closes the game as Quit game does and starts totk.exe again with the same command line and environment (plat_restart). The new process waits until the old one is gone before it moves the log and opens the saves (plat_restart_wait), and the confirmation waits while the game writes a save. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
19fb3a75c9
commit
703e721bf8
10 files changed
+138
-11
No files matched your search
@@ -129,3 +129,11 @@ PlatChild *plat_child_start(const char *exe, const char *arg, uint32_t *err);
|
||||
void plat_child_stop(PlatChild *c); // kills the child and frees c
|
||||
int plat_child_write(PlatChild *c, const void *data, size_t n); // 1 when all n bytes were written
|
||||
long plat_child_read(PlatChild *c, void *buf, size_t cap); // bytes read (at least 1); 0 or negative: pipe broke
|
||||
|
||||
// ---- restart: this program again, in a new process that starts once this one is gone
|
||||
// Starts this executable again with the same command line, environment and working folder, and no handle of this
|
||||
// process. 1 when it started; the caller then exits.
|
||||
int plat_restart(void);
|
||||
// In a process plat_restart started: waits, at most timeout_ms, until the process that started it has exited. 1 after
|
||||
// the wait, -1 on timeout, 0 when there is nothing to wait for (not started by plat_restart, or that process is gone).
|
||||
int plat_restart_wait(uint32_t timeout_ms);
|
||||
@@ -706,3 +706,45 @@ long plat_child_read(PlatChild *c, void *buf, size_t cap) {
|
||||
if (!ReadFile(c->out, buf, (DWORD)cap, &got, NULL)) return -1;
|
||||
return (long)got;
|
||||
}
|
||||
|
||||
// ---- restart
|
||||
// TOTK_RESTART_WAIT names the process that called plat_restart, as its id and creation time: a process that has only
|
||||
// reused the id is not waited for. Waiting keeps the new process off the old one's files (the log it moves at start,
|
||||
// the saves) until the old one is gone; no handle is inherited, so none of them stays open in the new one either.
|
||||
#define RESTART_WAIT "TOTK_RESTART_WAIT"
|
||||
|
||||
static unsigned long long process_created(HANDLE h) {
|
||||
FILETIME c, e, k, u;
|
||||
return GetProcessTimes(h, &c, &e, &k, &u) ? (unsigned long long)c.dwHighDateTime << 32 | c.dwLowDateTime : 0;
|
||||
}
|
||||
|
||||
int plat_restart(void) {
|
||||
char wait[48];
|
||||
snprintf(wait, sizeof wait, "%lu.%llu", GetCurrentProcessId(), process_created(GetCurrentProcess()));
|
||||
SetEnvironmentVariableA(RESTART_WAIT, wait);
|
||||
STARTUPINFOW si = {.cb = sizeof si};
|
||||
PROCESS_INFORMATION pi;
|
||||
wchar_t *cmd = _wcsdup(GetCommandLineW()); // CreateProcessW may write its command line
|
||||
BOOL ok = cmd && CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
|
||||
free(cmd);
|
||||
SetEnvironmentVariableA(RESTART_WAIT, NULL);
|
||||
if (!ok) return 0;
|
||||
CloseHandle(pi.hThread);
|
||||
CloseHandle(pi.hProcess);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int plat_restart_wait(uint32_t timeout_ms) {
|
||||
char v[48];
|
||||
DWORD n = GetEnvironmentVariableA(RESTART_WAIT, v, sizeof v);
|
||||
if (!n) return 0;
|
||||
SetEnvironmentVariableA(RESTART_WAIT, NULL); // the processes this one starts do not wait again
|
||||
unsigned long pid = 0;
|
||||
unsigned long long created = 0;
|
||||
if (n >= sizeof v || sscanf(v, "%lu.%llu", &pid, &created) != 2) return 0;
|
||||
HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
|
||||
if (!h) return 0; // already gone
|
||||
int r = process_created(h) != created ? 0 : WaitForSingleObject(h, timeout_ms) == WAIT_OBJECT_0 ? 1 : -1;
|
||||
CloseHandle(h);
|
||||
return r;
|
||||
}
|
||||
Reference in new issue
Block a user