Skip to content

Commit e718a02

Browse files
committed
add code to detect/clean legacy install
on windows, will prompt user on linux, will advise user on how to manually clean also removed install mode - can't seem to make work with steam API rn
1 parent 4166d21 commit e718a02

1 file changed

Lines changed: 97 additions & 9 deletions

File tree

package/launchdf.cpp

Lines changed: 97 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,80 @@ bool waitForDF(bool nowait) {
236236

237237
#endif
238238

239+
constexpr const char* old_filelist[] {
240+
"hack",
241+
"stonesense",
242+
#ifdef WIN32
243+
"binpatch.exe",
244+
"dfhack-run.exe",
245+
"allegro-5.2.dll",
246+
"allegro_color-5.2.dll",
247+
"allegro_font-5.2.dll",
248+
"allegro_image-5.2.dll",
249+
"allegro_primitives-5.2.dll",
250+
"allegro_ttf-5.2.dll",
251+
"allegro-5.2.dll",
252+
"dfhack-client.dll",
253+
"dfhooks_dfhack.dll",
254+
"lua53.dll",
255+
"protobuf-lite.dll"
256+
#else
257+
"binpatch",
258+
"dfhack-run",
259+
"liballegro-5.2.so",
260+
"liballegro_color-5.2.so",
261+
"liballegro_font-5.2.so",
262+
"liballegro_image-5.2.so",
263+
"liballegro_primitives-5.2.so",
264+
"liballegro_ttf-5.2.so",
265+
"liballegro-5.2.so",
266+
"libdfhack-client.so",
267+
"libdfhooks_dfhack.so",
268+
"liblua53.so",
269+
"libprotobuf-lite.so"
270+
#endif
271+
};
272+
273+
bool check_for_old_install(std::filesystem::path df_path)
274+
{
275+
for (auto file : old_filelist)
276+
{
277+
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)
282+
return true;
283+
}
284+
return false;
285+
}
286+
287+
void remove_old_install(std::filesystem::path df_path)
288+
{
289+
std::string message{
290+
"Removing legacy files:"
291+
};
292+
293+
for (auto file : old_filelist)
294+
{
295+
std::error_code ec;
296+
297+
std::filesystem::path p = df_path / file;
298+
299+
if (std::filesystem::is_directory(p))
300+
std::filesystem::remove_all(p, ec);
301+
else if (std::filesystem::is_regular_file(p))
302+
std::filesystem::remove(p, ec);
303+
else
304+
continue;
305+
306+
message += "\n" + p.string() + ": " + (ec ? "failed to remove - " + ec.message() : "removed successfully");
307+
}
308+
#ifdef WIN32
309+
MessageBoxW(NULL, std::wstring(message.begin(), message.end()).c_str(), L"Legacy Install Cleanup", 0);
310+
#endif
311+
}
312+
239313
#ifdef WIN32
240314
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nShowCmd) {
241315
#else
@@ -247,19 +321,14 @@ int main(int argc, char* argv[]) {
247321
}
248322

249323
bool nowait = false;
250-
bool installmode = false;
251324
#ifdef WIN32
252325
std::wstring cmdline(lpCmdLine);
253326
if (cmdline.find(L"--nowait") != std::wstring::npos)
254327
nowait = true;
255-
if (cmdline.find(L"--install") != std::wstring::npos)
256-
installmode = true;
257328
#else
258329
for (int idx = 0; idx < argc; ++idx) {
259330
if (strcmp(argv[idx], "--nowait") == 0)
260331
nowait = true;
261-
if (strcmp(argv[idx], "--install") == 0)
262-
installmode = true;
263332
}
264333
#endif
265334

@@ -365,10 +434,30 @@ int main(int argc, char* argv[]) {
365434
#endif
366435
exit(1);
367436
}
368-
}
437+
bool dirty = check_for_old_install(df_install_folder);
438+
if (dirty)
439+
{
440+
#ifdef WIN32
441+
int ok = MessageBoxW(NULL, L"A legacy install of DFHack has been detected in the Dwarf Fortress folder. This likely means that you have installed DFHack with the old Steam client (or manually). This legacy installation will almost certainly interfere with using DFHack. Do you want to remove the old files now? (recommended)", L"Legacy DFHack Install Detected", MB_OKCANCEL);
369442

370-
if (installmode)
371-
exit(0);
443+
if (ok == IDOK)
444+
remove_old_install(df_install_folder);
445+
#else
446+
int response = 0;
447+
std::string filelist;
448+
for (auto file : old_filelist)
449+
if (std::filesystem::exists(df_install_folder / file))
450+
filelist += (filelist.empty() ? "" : std::string(",")) + file;
451+
452+
std::string message{
453+
"A legacy install of DFHack has been detected in the Dwarf Fortress directory.This likely means that you have installed DFHack with the old Steam client (or manually).This installation will almost certainly interfere with using DFHack. \n\n"
454+
"To remove these files, run the following command: rm -r " + df_install_folder.string() + "/{ " + filelist + "}\n\n"
455+
};
456+
457+
notify(message.c_str());
458+
#endif
459+
}
460+
}
372461

373462
if (!wrap_launch(launch_via_steam))
374463
exit(1);
@@ -389,6 +478,5 @@ int main(int argc, char* argv[]) {
389478
usleep(1000000);
390479
#endif
391480
}
392-
393481
exit(0);
394482
}

0 commit comments

Comments
 (0)