allow launching DF to work under wine

develop
Myk Taylor 2023-04-07 12:41:46 -07:00
parent a91bc23dfc
commit 91b35475fa
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
1 changed files with 52 additions and 19 deletions

@ -1,7 +1,26 @@
#include <process.h>
#include <windows.h> #include <windows.h>
int WINAPI wWinMain(HINSTANCE hi, HINSTANCE hpi, PWSTR cmd, int ns) static BOOL is_running_on_wine() {
{ static const char *(CDECL *pwine_get_version)(void);
HMODULE hntdll = GetModuleHandle("ntdll.dll");
if(!hntdll)
return FALSE;
pwine_get_version = (void *)GetProcAddress(hntdll, "wine_get_version");
return !!pwine_get_version;
}
static LPCWSTR launch_via_steam_posix() {
const char* argv[] = { "/bin/sh", "-c", "\"steam -applaunch 975370\"", NULL };
// does not return on success
_execv(argv[0], argv);
return L"Could not launch Dwarf Fortress";
}
static LPCWSTR launch_via_steam_windows() {
STARTUPINFOW si; STARTUPINFOW si;
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
@ -12,28 +31,42 @@ int WINAPI wWinMain(HINSTANCE hi, HINSTANCE hpi, PWSTR cmd, int ns)
WCHAR steamPath[1024]; WCHAR steamPath[1024];
DWORD datasize = 1024; DWORD datasize = 1024;
LONG retCode = RegGetValueW(HKEY_CURRENT_USER, L"SOFTWARE\\Valve\\Steam", L"SteamExe", RRF_RT_REG_SZ, NULL, &steamPath, &datasize); LONG retCode = RegGetValueW(HKEY_CURRENT_USER, L"SOFTWARE\\Valve\\Steam",
L"SteamExe", RRF_RT_REG_SZ, NULL, &steamPath, &datasize);
MessageBoxW(NULL, steamPath, NULL, 0);
if (retCode != ERROR_SUCCESS) if (retCode != ERROR_SUCCESS)
{ return L"Could not find Steam client executable";
MessageBoxW(NULL, L"Could not find Steam client executable", NULL, 0);
exit(1);
}
WCHAR commandLine[1024] = L"steam.exe -applaunch 975370"; WCHAR commandLine[1024] = L"steam.exe -applaunch 975370";
if (CreateProcessW(steamPath, if (CreateProcessW(steamPath, commandLine,
commandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi) == 0)
NULL, return L"Could not launch Dwarf Fortress";
NULL,
FALSE, return NULL;
0, }
NULL,
NULL, // this method doesn't properly attribute Steam playtime metrics to DF,
&si, // but that's better than not having DF start at all.
&pi) == 0) static BOOL launch_direct() {
{ STARTUPINFOA si;
MessageBoxW(NULL, L"could not launch Dwarf Fortress", NULL, 0); PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
return CreateProcessA("Dwarf Fortress.exe",
NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
}
int WINAPI wWinMain(HINSTANCE hi, HINSTANCE hpi, PWSTR cmd, int ns) {
LPCWSTR err = is_running_on_wine() ? launch_via_steam_posix() : launch_via_steam_windows();
if (err && !launch_direct()) {
MessageBoxW(NULL, err, NULL, 0);
exit(1); exit(1);
} }