From edeae704f90118bc2aafa91e2835d58e854228bb Mon Sep 17 00:00:00 2001 From: MrSimbax Date: Sun, 24 Feb 2019 15:49:05 +0100 Subject: [PATCH 1/4] Use base dir instead of working dir for data files --- CMakeLists.txt | 8 ++++++++ src/common/config.h.cmake | 4 ++-- src/common/system/system.cpp | 5 +++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 887cf06d..797b29aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -370,24 +370,32 @@ if(PORTABLE OR (PLATFORM_WINDOWS AND MXE)) # We need to use STRING because PATH doesn't accept relative paths set(COLOBOT_INSTALL_BIN_DIR ./ CACHE STRING "Colobot binary directory") set(COLOBOT_INSTALL_LIB_DIR ./ CACHE STRING "Colobot libraries directory") + set(COLOBOT_RELATIVE_DATA_DIR data CACHE STRING "Colobot shared data directory (relative)") + set(COLOBOT_RELATIVE_I18N_DIR lang CACHE STRING "Colobot translations directory (relative)") set(COLOBOT_INSTALL_DATA_DIR ./data CACHE STRING "Colobot shared data directory") set(COLOBOT_INSTALL_I18N_DIR ./lang CACHE STRING "Colobot translations directory") set(COLOBOT_INSTALL_DOC_DIR ./doc CACHE STRING "Colobot documentation directory") elseif(PLATFORM_WINDOWS) set(COLOBOT_INSTALL_BIN_DIR ${CMAKE_INSTALL_PREFIX}/ CACHE PATH "Colobot binary directory") set(COLOBOT_INSTALL_LIB_DIR ${CMAKE_INSTALL_PREFIX}/ CACHE PATH "Colobot libraries directory") + set(COLOBOT_RELATIVE_DATA_DIR data CACHE STRING "Colobot shared data directory (relative)") + set(COLOBOT_RELATIVE_I18N_DIR lang CACHE STRING "Colobot translations directory (relative)") set(COLOBOT_INSTALL_DATA_DIR ${CMAKE_INSTALL_PREFIX}/data CACHE PATH "Colobot shared data directory") set(COLOBOT_INSTALL_I18N_DIR ${CMAKE_INSTALL_PREFIX}/lang CACHE PATH "Colobot translations directory") set(COLOBOT_INSTALL_DOC_DIR ${CMAKE_INSTALL_PREFIX}/doc CACHE PATH "Colobot documentation directory") elseif(PLATFORM_MACOSX) set(COLOBOT_INSTALL_BIN_DIR ../MacOS CACHE STRING "Colobot binary directory") set(COLOBOT_INSTALL_LIB_DIR ../MacOS CACHE STRING "Colobot libraries directory") + set(COLOBOT_RELATIVE_DATA_DIR . CACHE STRING "Colobot shared data directory (relative)") + set(COLOBOT_RELATIVE_I18N_DIR i18n CACHE STRING "Colobot translations directory (relative)") set(COLOBOT_INSTALL_DATA_DIR . CACHE STRING "Colobot shared data directory") set(COLOBOT_INSTALL_I18N_DIR i18n CACHE SRING "Colobot translations directory") set(COLOBOT_INSTALL_DOC_DIR doc CACHE STRING "Colobot documentation directory") else() set(COLOBOT_INSTALL_BIN_DIR ${CMAKE_INSTALL_PREFIX}/games CACHE PATH "Colobot binary directory") set(COLOBOT_INSTALL_LIB_DIR ${CMAKE_INSTALL_PREFIX}/lib/colobot CACHE PATH "Colobot libraries directory") + set(COLOBOT_RELATIVE_DATA_DIR ../share/games/colobot CACHE STRING "Colobot shared data directory (relative)") + set(COLOBOT_RELATIVE_I18N_DIR ../share/locale CACHE STRING "Colobot translations directory (relative)") set(COLOBOT_INSTALL_DATA_DIR ${CMAKE_INSTALL_PREFIX}/share/games/colobot CACHE PATH "Colobot shared data directory") set(COLOBOT_INSTALL_I18N_DIR ${CMAKE_INSTALL_PREFIX}/share/locale CACHE PATH "Colobot translations directory") set(COLOBOT_INSTALL_DOC_DIR ${CMAKE_INSTALL_PREFIX}/share/doc/colobot CACHE PATH "Colobot documentation directory") diff --git a/src/common/config.h.cmake b/src/common/config.h.cmake index f6d0bcae..6100d7dc 100644 --- a/src/common/config.h.cmake +++ b/src/common/config.h.cmake @@ -18,5 +18,5 @@ #cmakedefine PORTABLE_SAVES @PORTABLE_SAVES@ -#define COLOBOT_DEFAULT_DATADIR "@COLOBOT_INSTALL_DATA_DIR@" -#define COLOBOT_I18N_DIR "@COLOBOT_INSTALL_I18N_DIR@" +#define COLOBOT_DEFAULT_DATADIR "@COLOBOT_RELATIVE_DATA_DIR@" +#define COLOBOT_I18N_DIR "@COLOBOT_RELATIVE_I18N_DIR@" diff --git a/src/common/system/system.cpp b/src/common/system/system.cpp index 12dc1d36..efec405b 100644 --- a/src/common/system/system.cpp +++ b/src/common/system/system.cpp @@ -36,6 +36,7 @@ #include #include +#include std::unique_ptr CSystemUtils::Create() { @@ -178,12 +179,12 @@ float CSystemUtils::TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *afte std::string CSystemUtils::GetDataPath() { - return COLOBOT_DEFAULT_DATADIR; + return std::string{PHYSFS_getBaseDir()} + COLOBOT_DEFAULT_DATADIR; } std::string CSystemUtils::GetLangPath() { - return COLOBOT_I18N_DIR; + return std::string{PHYSFS_getBaseDir()} + COLOBOT_I18N_DIR; } std::string CSystemUtils::GetSaveDir() From 7268bcca1109e760589e16b32aa50415f3e47d8c Mon Sep 17 00:00:00 2001 From: MrSimbax Date: Sun, 24 Feb 2019 17:16:54 +0100 Subject: [PATCH 2/4] Use workdir paths on dev builds --- src/common/system/system.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/common/system/system.cpp b/src/common/system/system.cpp index efec405b..2ef642ab 100644 --- a/src/common/system/system.cpp +++ b/src/common/system/system.cpp @@ -179,12 +179,20 @@ float CSystemUtils::TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *afte std::string CSystemUtils::GetDataPath() { +#if DEV_BUILD + return std::string{"./"} + COLOBOT_DEFAULT_DATADIR; +#else return std::string{PHYSFS_getBaseDir()} + COLOBOT_DEFAULT_DATADIR; +#endif } std::string CSystemUtils::GetLangPath() { +#if DEV_BUILD + return std::string{"./"} + COLOBOT_I18N_DIR; +#else return std::string{PHYSFS_getBaseDir()} + COLOBOT_I18N_DIR; +#endif } std::string CSystemUtils::GetSaveDir() From 266944c9d27b4bf2458feb4de4c5403fb9f38858 Mon Sep 17 00:00:00 2001 From: MrSimbax Date: Mon, 25 Feb 2019 23:00:05 +0100 Subject: [PATCH 3/4] Use SDL_GetBasePath() instead of physfs because it's buggy in old version --- src/common/system/system.cpp | 13 ++++++++++--- src/common/system/system.h | 4 ++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/common/system/system.cpp b/src/common/system/system.cpp index 2ef642ab..c1886852 100644 --- a/src/common/system/system.cpp +++ b/src/common/system/system.cpp @@ -36,7 +36,7 @@ #include #include -#include +#include std::unique_ptr CSystemUtils::Create() { @@ -177,12 +177,19 @@ float CSystemUtils::TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *afte return result; } +std::string CSystemUtils::GetBasePath() +{ + if (m_basePath.empty()) + m_basePath = SDL_GetBasePath(); + return m_basePath; +} + std::string CSystemUtils::GetDataPath() { #if DEV_BUILD return std::string{"./"} + COLOBOT_DEFAULT_DATADIR; #else - return std::string{PHYSFS_getBaseDir()} + COLOBOT_DEFAULT_DATADIR; + return GetBasePath() + COLOBOT_DEFAULT_DATADIR; #endif } @@ -191,7 +198,7 @@ std::string CSystemUtils::GetLangPath() #if DEV_BUILD return std::string{"./"} + COLOBOT_I18N_DIR; #else - return std::string{PHYSFS_getBaseDir()} + COLOBOT_I18N_DIR; + return GetBasePath() + COLOBOT_I18N_DIR; #endif } diff --git a/src/common/system/system.h b/src/common/system/system.h index 36e736c9..7912f27a 100644 --- a/src/common/system/system.h +++ b/src/common/system/system.h @@ -127,6 +127,9 @@ public: /** The difference is \a after - \a before. */ virtual long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) = 0; + //! Returns the path where the executable binary is located + virtual std::string GetBasePath(); + //! Returns the data path (containing textures, levels, helpfiles, etc) virtual std::string GetDataPath(); @@ -140,5 +143,6 @@ public: virtual void Usleep(int usecs) = 0; private: + std::string m_basePath; std::vector> m_timeStamps; }; From 472aadf9ab3339ef069757600e6d277d953d936f Mon Sep 17 00:00:00 2001 From: MrSimbax Date: Mon, 25 Feb 2019 23:42:34 +0100 Subject: [PATCH 4/4] Fix minor memory leak --- src/common/system/system.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/common/system/system.cpp b/src/common/system/system.cpp index c1886852..0b8001a1 100644 --- a/src/common/system/system.cpp +++ b/src/common/system/system.cpp @@ -180,7 +180,11 @@ float CSystemUtils::TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *afte std::string CSystemUtils::GetBasePath() { if (m_basePath.empty()) - m_basePath = SDL_GetBasePath(); + { + auto* path = SDL_GetBasePath(); + m_basePath = path; + SDL_free(path); + } return m_basePath; }