From 21074e13c3e9591b9d9f0c7b556d0e4204e51d74 Mon Sep 17 00:00:00 2001 From: Arseniy Shestakov Date: Wed, 9 Aug 2017 01:09:29 +0300 Subject: [PATCH] macOS: always change working directory for launcher and server This make relative paths work properly when launched from DMG or from different directory. --- client/CMT.cpp | 2 +- launcher/main.cpp | 2 +- launcher/mainwindow_moc.cpp | 4 ++++ server/CVCMIServer.cpp | 8 +++++++- test/main.cpp | 2 +- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/client/CMT.cpp b/client/CMT.cpp index f60e597a6..7714983de 100644 --- a/client/CMT.cpp +++ b/client/CMT.cpp @@ -229,7 +229,7 @@ int wmain(int argc, wchar_t* argv[]) #elif defined(VCMI_APPLE) || defined(VCMI_ANDROID) int SDL_main(int argc, char *argv[]) #else -int main(int argc, char** argv) +int main(int argc, char * argv[]) #endif { #ifdef VCMI_ANDROID diff --git a/launcher/main.cpp b/launcher/main.cpp index 9856178d9..49f0d80ff 100644 --- a/launcher/main.cpp +++ b/launcher/main.cpp @@ -12,7 +12,7 @@ #include "mainwindow_moc.h" #include "sdldisplayquery.h" -int main(int argc, char *argv[]) +int main(int argc, char * argv[]) { QApplication vcmilauncher(argc, argv); auto displayList = getDisplays(); diff --git a/launcher/mainwindow_moc.cpp b/launcher/mainwindow_moc.cpp index 0d954f722..43412592e 100644 --- a/launcher/mainwindow_moc.cpp +++ b/launcher/mainwindow_moc.cpp @@ -21,6 +21,10 @@ void MainWindow::load() { + // Set current working dir to executable folder. + // This is important on Mac for relative paths to work inside DMG. + QDir::setCurrent(QApplication::applicationDirPath()); + console = new CConsoleHandler(); CBasicLogConfigurator logConfig(VCMIDirs::get().userCachePath() / "VCMI_Launcher_log.txt", console); logConfig.configureDefault(); diff --git a/server/CVCMIServer.cpp b/server/CVCMIServer.cpp index 08e12bb02..8722f9b92 100644 --- a/server/CVCMIServer.cpp +++ b/server/CVCMIServer.cpp @@ -607,8 +607,14 @@ void handleLinuxSignal(int sig) } #endif -int main(int argc, char** argv) +int main(int argc, char * argv[]) { +#ifdef VCMI_APPLE + // Correct working dir executable folder (not bundle folder) so we can use executable relative paths + std::string executablePath = argv[0]; + std::string workDir = executablePath.substr(0, executablePath.rfind('/')); + chdir(workDir.c_str()); +#endif // Installs a sig sev segmentation violation handler // to log stacktrace #if defined(__GNUC__) && !defined (__MINGW32__) && !defined(VCMI_ANDROID) diff --git a/test/main.cpp b/test/main.cpp index 52b7c53ef..08da1f468 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -12,7 +12,7 @@ #include "CVcmiTestConfig.h" #include "../lib/filesystem/CMemoryBuffer.h" -int main(int argc, char ** argv) +int main(int argc, char * argv[]) { ::testing::InitGoogleTest(&argc, argv); CVcmiTestConfig test;