1212#include < string>
1313#include < filesystem>
1414#include < fstream>
15+ #include < optional>
1516
1617#define xstr (s ) str(s)
1718#define str (s ) #s
@@ -275,10 +276,7 @@ bool check_for_old_install(std::filesystem::path df_path)
275276 for (auto file : old_filelist)
276277 {
277278 std::filesystem::path p = df_path / file;
278- bool exists = std::filesystem::exists (p);
279- // std::wstring message = L"Checking for legacy files:\n" + p.wstring() + L": " + (exists ? L"found" : L"not found");
280- // MessageBoxW(NULL, message.c_str(), L"Checking for legacy files", 0);
281- if (exists)
279+ if (std::filesystem::exists (p))
282280 return true ;
283281 }
284282 return false ;
@@ -341,42 +339,38 @@ int main(int argc, char* argv[]) {
341339 }
342340
343341#ifdef WIN32
344- if (is_running_on_wine ()) {
345- // attempt launch via steam client
346- LPCWSTR err = launch_via_steam_posix ();
347-
348- if (err != NULL )
349- // steam client launch failed, attempt fallback launch
350- err = launch_direct ();
351-
352- if (err != NULL )
353- {
354- MessageBoxW (NULL , err, NULL , 0 );
355- exit (1 );
356- }
357- exit (0 );
358- }
342+ bool wine_detected = is_running_on_wine ();
343+ #else
344+ bool wine_detected = false ;
359345#endif
360346
361- // steam detected and not running in wine
347+ bool df_detected = SteamApps ()-> BIsAppInstalled (DF_STEAM_APPID);
362348
363- if (!SteamApps ()-> BIsAppInstalled (DF_STEAM_APPID) ) {
349+ if (!df_detected ) {
364350 // Steam DF is not installed. Assume DF is installed in same directory as DFHack and do a fallback launch
365351 exit (wrap_launch (launch_direct) ? 0 : 1 );
366352 }
367353
368- // obtain DF app path
369-
370- char buf[2048 ] = " " ;
354+ // obtain DF and DFHack app paths
371355
372- int b1 = SteamApps ()->GetAppInstallDir (DFHACK_STEAM_APPID, (char *)&buf, 2048 );
373- std::filesystem::path dfhack_install_folder = (b1 != -1 ) ? std::string (buf) : " " ;
356+ auto get_app_path_from_steam = [] (AppId_t appid) -> std::optional<std::filesystem::path> {
357+ char buf[2048 ] = " " ;
358+ int bytes = SteamApps ()->GetAppInstallDir (appid, (char *)&buf, 2048 );
359+ if (bytes == -1 )
360+ return std::nullopt ;
361+ // steam API counts the null terminator in the byte count returned
362+ if (buf[bytes] == ' \0 ' ) bytes--;
363+ return std::string (buf, bytes);
364+ };
374365
375- int b2 = SteamApps ()-> GetAppInstallDir (DF_STEAM_APPID, ( char *)&buf, 2048 );
376- std::filesystem::path df_install_folder = (b2 != - 1 ) ? std::string (buf) : " " ;
366+ auto opt_dfhack_install_folder = get_app_path_from_steam (DFHACK_STEAM_APPID );
367+ auto opt_df_install_folder = get_app_path_from_steam (DF_STEAM_APPID) ;
377368
378- if (df_install_folder != dfhack_install_folder )
369+ if (opt_dfhack_install_folder && opt_df_install_folder && (*opt_df_install_folder != *opt_dfhack_install_folder) )
379370 {
371+ auto & dfhack_install_folder = *opt_dfhack_install_folder;
372+ auto & df_install_folder = *opt_df_install_folder;
373+
380374#ifdef WIN32
381375 constexpr auto dfhooks_dll_name = " dfhooks.dll" ;
382376 constexpr auto dfhook_dfhack_dll_name = " dfhooks_dfhack.dll" ;
@@ -414,8 +408,8 @@ int main(int argc, char* argv[]) {
414408#ifdef WIN32
415409 std::wstring message{
416410 L" Failed to inject DFHack into Dwarf Fortress\n\n "
417- L" Details:\n " + std::filesystem::relative ( dfhooks_dll_src) .wstring () +
418- L" -> " + std::filesystem::relative ( dfhooks_dll_dst) .wstring () +
411+ L" Details:\n " + dfhooks_dll_src.wstring () +
412+ L" -> " + dfhooks_dll_dst.wstring () +
419413 L" \n\n Error code: " + std::to_wstring (ec.value ()) +
420414 L" \n Error message: " + std::filesystem::relative (ec.message ()).wstring ()
421415 };
@@ -424,8 +418,8 @@ int main(int argc, char* argv[]) {
424418#else
425419 std::string message{
426420 " Failed to inject DFHack into Dwarf Fortress\n\n "
427- " Details:\n " + std::filesystem::relative ( dfhooks_dll_src) .string () +
428- " -> " + std::filesystem::relative ( dfhooks_dll_dst) .string () +
421+ " Details:\n " + dfhooks_dll_src.string () +
422+ " -> " + dfhooks_dll_dst.string () +
429423 " \n\n Error code: " + std::to_string (ec.value ()) +
430424 " \n Error message: " + std::filesystem::relative (ec.message ()).string ()
431425 };
@@ -459,6 +453,25 @@ int main(int argc, char* argv[]) {
459453 }
460454 }
461455
456+ #ifdef WIN32
457+ if (wine_detected)
458+ {
459+ // attempt launch via steam client
460+ LPCWSTR err = launch_via_steam_posix ();
461+
462+ if (err != NULL )
463+ // steam client launch failed, attempt fallback launch
464+ err = launch_direct ();
465+
466+ if (err != NULL )
467+ {
468+ MessageBoxW (NULL , err, NULL , 0 );
469+ exit (1 );
470+ }
471+ exit (0 );
472+ }
473+ #endif
474+
462475 if (!wrap_launch (launch_via_steam))
463476 exit (1 );
464477
0 commit comments