2023-04-07 13:41:46 -06:00
|
|
|
#include <process.h>
|
2023-03-07 23:13:08 -07:00
|
|
|
#include <windows.h>
|
2023-04-18 07:21:54 -06:00
|
|
|
#include <TlHelp32.h>
|
2023-04-18 04:00:36 -06:00
|
|
|
#include "steam_api.h"
|
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
#include <string>
|
|
|
|
|
2023-04-18 04:00:36 -06:00
|
|
|
const uint32 DFHACK_STEAM_APPID = 2346660;
|
2023-04-18 07:21:54 -06:00
|
|
|
const uint32 DF_STEAM_APPID = 975370;
|
2023-03-07 23:13:08 -07:00
|
|
|
|
2023-04-07 13:41:46 -06:00
|
|
|
static BOOL is_running_on_wine() {
|
2023-04-18 04:00:36 -06:00
|
|
|
typedef const char* (CDECL wine_get_version)(void);
|
|
|
|
static wine_get_version* pwine_get_version;
|
2023-04-07 13:41:46 -06:00
|
|
|
HMODULE hntdll = GetModuleHandle("ntdll.dll");
|
|
|
|
if(!hntdll)
|
|
|
|
return FALSE;
|
|
|
|
|
2023-04-18 04:00:36 -06:00
|
|
|
pwine_get_version = (wine_get_version*) GetProcAddress(hntdll, "wine_get_version");
|
2023-04-07 13:41:46 -06:00
|
|
|
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() {
|
2023-03-31 20:05:19 -06:00
|
|
|
STARTUPINFOW si;
|
2023-03-07 23:13:08 -07:00
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
|
|
|
ZeroMemory(&si, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
ZeroMemory(&pi, sizeof(pi));
|
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
WCHAR steamPath[1024] = L"";
|
2023-03-31 20:05:19 -06:00
|
|
|
DWORD datasize = 1024;
|
|
|
|
|
2023-04-07 13:41:46 -06:00
|
|
|
LONG retCode = RegGetValueW(HKEY_CURRENT_USER, L"SOFTWARE\\Valve\\Steam",
|
|
|
|
L"SteamExe", RRF_RT_REG_SZ, NULL, &steamPath, &datasize);
|
|
|
|
|
2023-03-31 20:05:19 -06:00
|
|
|
if (retCode != ERROR_SUCCESS)
|
2023-04-07 13:41:46 -06:00
|
|
|
return L"Could not find Steam client executable";
|
2023-03-31 20:05:19 -06:00
|
|
|
|
|
|
|
WCHAR commandLine[1024] = L"steam.exe -applaunch 975370";
|
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
BOOL res = CreateProcessW(steamPath, commandLine,
|
|
|
|
NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
|
|
|
|
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
2023-04-07 13:41:46 -06:00
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return L"Could not launch Dwarf Fortress";
|
|
|
|
}
|
2023-04-07 13:41:46 -06:00
|
|
|
}
|
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
static LPCWSTR launch_direct() {
|
2023-04-07 15:40:38 -06:00
|
|
|
STARTUPINFOW si;
|
2023-04-07 13:41:46 -06:00
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
|
|
|
ZeroMemory(&si, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
ZeroMemory(&pi, sizeof(pi));
|
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
BOOL res = CreateProcessW(L"Dwarf Fortress.exe",
|
2023-04-07 13:41:46 -06:00
|
|
|
NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
|
2023-04-18 07:21:54 -06:00
|
|
|
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
|
|
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return L"Could not launch via non-steam fallback method";
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD findDwarfFortressProcess()
|
|
|
|
{
|
|
|
|
PROCESSENTRY32W entry;
|
|
|
|
entry.dwSize = sizeof(PROCESSENTRY32W);
|
|
|
|
|
|
|
|
const auto snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
|
|
|
|
|
|
|
|
if (!Process32FirstW(snapshot, &entry))
|
|
|
|
{
|
|
|
|
CloseHandle(snapshot);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
std::wstring executableName(entry.szExeFile);
|
|
|
|
if (executableName == L"Dwarf Fortress.exe")
|
|
|
|
{
|
|
|
|
CloseHandle(snapshot);
|
|
|
|
return entry.th32ProcessID;
|
|
|
|
}
|
|
|
|
} while (Process32NextW(snapshot, &entry));
|
|
|
|
|
|
|
|
CloseHandle(snapshot);
|
|
|
|
return -1;
|
2023-04-07 13:41:46 -06:00
|
|
|
}
|
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nShowCmd) {
|
2023-04-18 04:00:36 -06:00
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
// initialize steam context
|
|
|
|
if (SteamAPI_RestartAppIfNecessary(DFHACK_STEAM_APPID))
|
2023-04-18 04:00:36 -06:00
|
|
|
{
|
2023-04-18 07:21:54 -06:00
|
|
|
exit(0);
|
2023-04-18 04:00:36 -06:00
|
|
|
}
|
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
if (!SteamAPI_Init())
|
|
|
|
{
|
|
|
|
// could not initialize steam context, attempt fallback launch
|
|
|
|
LPCWSTR err = launch_direct();
|
|
|
|
if (err != NULL)
|
2023-04-18 04:00:36 -06:00
|
|
|
{
|
2023-04-18 07:21:54 -06:00
|
|
|
MessageBoxW(NULL, err, NULL, 0);
|
|
|
|
exit(1);
|
2023-04-18 04:00:36 -06:00
|
|
|
}
|
2023-04-18 07:21:54 -06:00
|
|
|
exit(0);
|
|
|
|
}
|
2023-04-18 04:00:36 -06:00
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
bool wine = is_running_on_wine();
|
2023-04-18 04:00:36 -06:00
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
if (wine)
|
|
|
|
{
|
|
|
|
// attempt launch via steam client
|
|
|
|
LPCWSTR err = launch_via_steam_posix();
|
|
|
|
|
|
|
|
if (err != NULL)
|
|
|
|
// steam client launch failed, attempt fallback launch
|
|
|
|
err = launch_direct();
|
2023-04-07 13:41:46 -06:00
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
if (err != NULL)
|
|
|
|
{
|
|
|
|
MessageBoxW(NULL, err, NULL, 0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// steam detected and not running in wine
|
|
|
|
|
|
|
|
bool df_installed = SteamApps()->BIsAppInstalled(DF_STEAM_APPID);
|
|
|
|
|
|
|
|
if (!df_installed)
|
|
|
|
{
|
|
|
|
// Steam DF is not installed. Assume DF is installed in same directory as DFHack and do a fallback launch
|
|
|
|
LPCWSTR err = launch_direct();
|
|
|
|
|
|
|
|
if (err != NULL)
|
|
|
|
{
|
|
|
|
MessageBoxW(NULL, err, NULL, 0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// obtain DF app path
|
|
|
|
|
|
|
|
char buf[2048] = "";
|
|
|
|
|
2023-04-18 10:12:29 -06:00
|
|
|
int b1 = SteamApps()->GetAppInstallDir(DFHACK_STEAM_APPID, (char*)&buf, 2048);
|
|
|
|
std::string dfhack_install_folder = (b1 != -1) ? std::string(buf) : "";
|
2023-04-18 07:21:54 -06:00
|
|
|
|
|
|
|
int b2 = SteamApps()->GetAppInstallDir(DF_STEAM_APPID, (char*)&buf, 2048);
|
2023-04-18 10:12:29 -06:00
|
|
|
std::string df_install_folder = (b2 != -1) ? std::string(buf) : "";
|
2023-04-18 07:21:54 -06:00
|
|
|
|
|
|
|
|
|
|
|
if (df_install_folder != dfhack_install_folder)
|
|
|
|
{
|
|
|
|
// DF and DFHack are not installed in the same library
|
|
|
|
MessageBoxW(NULL, L"DFHack and Dwarf Fortress must be installed in the same Steam library.\nAborting.", NULL, 0);
|
2023-03-07 23:13:08 -07:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-04-18 07:21:54 -06:00
|
|
|
DWORD df_pid = findDwarfFortressProcess();
|
|
|
|
|
|
|
|
if (df_pid == -1)
|
|
|
|
{
|
|
|
|
LPCWSTR err = launch_via_steam_windows();
|
|
|
|
if (err != NULL)
|
|
|
|
{
|
|
|
|
MessageBoxW(NULL, err, NULL, 0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
int counter = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (counter++ > 60)
|
|
|
|
{
|
|
|
|
MessageBoxW(NULL, L"Dwarf Fortress took too long to launch, aborting", NULL, 0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Sleep(1000);
|
|
|
|
df_pid = findDwarfFortressProcess();
|
|
|
|
} while (df_pid == -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE hDF = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, df_pid);
|
|
|
|
|
|
|
|
// in the future open an IPC connection so that we can proxy SteamAPI calls for the DFSteam module
|
|
|
|
|
|
|
|
// this will eventuallyh need to become a loop with a WaitForMultipleObjects call
|
|
|
|
WaitForSingleObject(hDF, INFINITE);
|
|
|
|
|
|
|
|
CloseHandle(hDF);
|
|
|
|
|
2023-03-07 23:13:08 -07:00
|
|
|
exit(0);
|
|
|
|
}
|