From 91b35475fa941bd4d08107161fc023510412c92e Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Fri, 7 Apr 2023 12:41:46 -0700 Subject: [PATCH 1/3] allow launching DF to work under wine --- package/windows/launchdf.c | 71 ++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/package/windows/launchdf.c b/package/windows/launchdf.c index 588624511..e903d8a84 100644 --- a/package/windows/launchdf.c +++ b/package/windows/launchdf.c @@ -1,7 +1,26 @@ +#include #include -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; PROCESS_INFORMATION pi; @@ -12,28 +31,42 @@ int WINAPI wWinMain(HINSTANCE hi, HINSTANCE hpi, PWSTR cmd, int ns) WCHAR steamPath[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) - { - MessageBoxW(NULL, L"Could not find Steam client executable", NULL, 0); - exit(1); - } + return L"Could not find Steam client executable"; WCHAR commandLine[1024] = L"steam.exe -applaunch 975370"; - if (CreateProcessW(steamPath, - commandLine, - NULL, - NULL, - FALSE, - 0, - NULL, - NULL, - &si, - &pi) == 0) - { - MessageBoxW(NULL, L"could not launch Dwarf Fortress", NULL, 0); + if (CreateProcessW(steamPath, commandLine, + NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi) == 0) + return L"Could not launch Dwarf Fortress"; + + return NULL; +} + +// this method doesn't properly attribute Steam playtime metrics to DF, +// but that's better than not having DF start at all. +static BOOL launch_direct() { + STARTUPINFOA si; + 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); } From 4d758589cba10fbca8453cfc27636112c0184f85 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Fri, 7 Apr 2023 13:17:17 -0700 Subject: [PATCH 2/3] remove debug statement --- package/windows/launchdf.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/package/windows/launchdf.c b/package/windows/launchdf.c index e903d8a84..6aea4e7d2 100644 --- a/package/windows/launchdf.c +++ b/package/windows/launchdf.c @@ -34,8 +34,6 @@ static LPCWSTR launch_via_steam_windows() { 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) return L"Could not find Steam client executable"; From 14bc22ff316b7efdac33f640ec2fa5283c958d27 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Fri, 7 Apr 2023 14:40:38 -0700 Subject: [PATCH 3/3] A -> W to align with existing codepaths --- package/windows/launchdf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package/windows/launchdf.c b/package/windows/launchdf.c index 6aea4e7d2..992bf6636 100644 --- a/package/windows/launchdf.c +++ b/package/windows/launchdf.c @@ -49,14 +49,14 @@ static LPCWSTR launch_via_steam_windows() { // this method doesn't properly attribute Steam playtime metrics to DF, // but that's better than not having DF start at all. static BOOL launch_direct() { - STARTUPINFOA si; + STARTUPINFOW si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); - return CreateProcessA("Dwarf Fortress.exe", + return CreateProcessW(L"Dwarf Fortress.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); }