diff --git a/src/common/system/system.cpp b/src/common/system/system.cpp index 44064556..3c306ac0 100644 --- a/src/common/system/system.cpp +++ b/src/common/system/system.cpp @@ -192,3 +192,13 @@ std::string CSystemUtils::GetSaveDir() { return "./saves"; } + +void CSystemUtils::OpenPath(std::string path) +{ + assert(false); +} + +void CSystemUtils::OpenWebsite(std::string website) +{ + assert(false); +} diff --git a/src/common/system/system.h b/src/common/system/system.h index aaf54954..2ef0b5cb 100644 --- a/src/common/system/system.h +++ b/src/common/system/system.h @@ -134,6 +134,12 @@ public: //! Returns the save dir location virtual std::string GetSaveDir(); + //! Opens a path with default file browser + virtual void OpenPath(std::string path); + + //! Opens a website with default web browser + virtual void OpenWebsite(std::string website); + //! Sleep for given amount of microseconds virtual void Usleep(int usecs) = 0; diff --git a/src/common/system/system_linux.cpp b/src/common/system/system_linux.cpp index 6578830d..2dda1c00 100644 --- a/src/common/system/system_linux.cpp +++ b/src/common/system/system_linux.cpp @@ -126,6 +126,28 @@ std::string CSystemUtilsLinux::GetSaveDir() #endif } +void CSystemUtilsLinux::OpenPath(std::string path) +{ + int result; + + result = system(("xdg-open \""+path+"\"").c_str()); + if (result == -1) + { + GetLogger()->Error("Failed to open path: %s\n", path.c_str()); + } +} + +void CSystemUtilsLinux::OpenWebsite(std::string website) +{ + int result; + + result = system(("xdg-open \""+website+"\"").c_str()); + if (result == -1) + { + GetLogger()->Error("Failed to open website: %s\n", website.c_str()); + } +} + void CSystemUtilsLinux::Usleep(int usec) { usleep(usec); diff --git a/src/common/system/system_linux.h b/src/common/system/system_linux.h index f1576f31..c41c30d4 100644 --- a/src/common/system/system_linux.h +++ b/src/common/system/system_linux.h @@ -45,6 +45,9 @@ public: std::string GetSaveDir() override; + void OpenPath(std::string path) override; + void OpenWebsite(std::string website) override; + void Usleep(int usec) override; private: diff --git a/src/common/system/system_macosx.cpp b/src/common/system/system_macosx.cpp index 9ef7ce1d..129037aa 100644 --- a/src/common/system/system_macosx.cpp +++ b/src/common/system/system_macosx.cpp @@ -113,6 +113,28 @@ std::string CSystemUtilsMacOSX::GetSaveDir() #endif } +void CSystemUtilsLinux::OpenPath(std::string path) +{ + int result; + + result = system(("open \""+path+"\"").c_str()); // TODO: Test on macOS + if (result == -1) + { + GetLogger()->Error("Failed to open path: %s\n", path.c_str()); + } +} + +void CSystemUtilsLinux::OpenWebsite(std::string website) +{ + int result; + + result = system(("open \""+website+"\"").c_str()); // TODO: Test on macOS + if (result == -1) + { + GetLogger()->Error("Failed to open website: %s\n", website.c_str()); + } +} + void CSystemUtilsMacOSX::Usleep(int usec) { usleep(usec); diff --git a/src/common/system/system_macosx.h b/src/common/system/system_macosx.h index 5b572ec4..dc4d2733 100644 --- a/src/common/system/system_macosx.h +++ b/src/common/system/system_macosx.h @@ -36,6 +36,9 @@ public: std::string GetLangPath() override; std::string GetSaveDir() override; + void OpenPath(std::string path) override; + void OpenWebsite(std::string website) override; + void Usleep(int usec) override; private: diff --git a/src/common/system/system_other.cpp b/src/common/system/system_other.cpp index a3311a74..7d439868 100644 --- a/src/common/system/system_other.cpp +++ b/src/common/system/system_other.cpp @@ -39,6 +39,16 @@ long long int CSystemUtilsOther::TimeStampExactDiff(SystemTimeStamp* before, Sys return (after->sdlTicks - before->sdlTicks) * 1000000ll; } +void CSystemUtilsOther::OpenPath(std::string path) +{ + // TODO +} + +void CSystemUtilsOther::OpenWebsite(std::string website) +{ + // TODO +} + void CSystemUtilsOther::Usleep(int usec) { SDL_Delay(usec / 1000); // close enough diff --git a/src/common/system/system_other.h b/src/common/system/system_other.h index ac80701b..d457b1e6 100644 --- a/src/common/system/system_other.h +++ b/src/common/system/system_other.h @@ -49,6 +49,9 @@ public: void GetCurrentTimeStamp(SystemTimeStamp *stamp) override; long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) override; + void OpenPath(std::string path) override; + void OpenWebsite(std::string website) override; + void Usleep(int usec) override; }; diff --git a/src/common/system/system_windows.cpp b/src/common/system/system_windows.cpp index a20bfcf1..3400817d 100644 --- a/src/common/system/system_windows.cpp +++ b/src/common/system/system_windows.cpp @@ -131,6 +131,28 @@ std::string CSystemUtilsWindows::GetSaveDir() #endif } +void CSystemUtilsWindows::OpenPath(std::string path) +{ + int result; + + result = system(("explorer \""+path+"\"").c_str()); // TODO: Test on macOS + if (result == -1) + { + GetLogger()->Error("Failed to open path: %s\n", path.c_str()); + } +} + +void CSystemUtilsWindows::OpenWebsite(std::string website) +{ + int result; + + result = system(("rundll32 url.dll,FileProtocolHandler \""+website+"\"").c_str()); // TODO: Test on macOS + if (result == -1) + { + GetLogger()->Error("Failed to open website: %s\n", website.c_str()); + } +} + void CSystemUtilsWindows::Usleep(int usec) { LARGE_INTEGER ft; diff --git a/src/common/system/system_windows.h b/src/common/system/system_windows.h index 74f02455..54c8611e 100644 --- a/src/common/system/system_windows.h +++ b/src/common/system/system_windows.h @@ -43,6 +43,9 @@ public: std::string GetSaveDir() override; + void OpenPath(std::string path) override; + void OpenWebsite(std::string website) override; + void Usleep(int usec) override; public: diff --git a/src/ui/screen/screen_setup_mods.cpp b/src/ui/screen/screen_setup_mods.cpp index 696e6a02..d3266e2d 100644 --- a/src/ui/screen/screen_setup_mods.cpp +++ b/src/ui/screen/screen_setup_mods.cpp @@ -22,6 +22,8 @@ #include "app/app.h" #include "app/pathman.h" +#include "common/system/system.h" + #include "common/restext.h" #include "common/config.h" #include "common/logger.h" @@ -126,8 +128,8 @@ bool CScreenSetupMods::EventProcess(const Event &event) CWindow* pw; CButton* pb; CList* pl; - int result; - std::string modName, modPath; + std::string modName, modPath, website = "https://www.moddb.com/games/colobot-gold-edition"; + auto systemUtils = CSystemUtils::Create(); // platform-specific utils if (!CScreenSetup::EventProcess(event)) return false; @@ -157,18 +159,7 @@ bool CScreenSetupMods::EventProcess(const Event &event) if (event.type == EVENT_INTERFACE_MODS_DIR) { modPath = CResourceManager::GetSaveLocation() + "/" + "mods"; - #if defined(PLATFORM_WINDOWS) - std::replace(modPath.begin(), modPath.end(), '/', '\\'); - result = system(("explorer \""+modPath+"\"").c_str()); - #elif defined(PLATFORM_LINUX) - result = system(("xdg-open \""+modPath+"\"").c_str()); - #elif defined(PLATFORM_MACOSX) - result = system(("open \""+modPath+"\"").c_str()); //TODO: Test on macOS - #endif - if (result == -1) - { - GetLogger()->Error("Failed to open Mods directory! Does directory exists?\n"); - } + systemUtils->OpenPath(modPath); } switch (event.type) { @@ -201,17 +192,7 @@ bool CScreenSetupMods::EventProcess(const Event &event) break; case EVENT_INTERFACE_WORKSHOP: - #if defined(PLATFORM_WINDOWS) - result = system("rundll32 url.dll,FileProtocolHandler \"https://www.moddb.com/games/colobot-gold-edition\""); - #elif defined(PLATFORM_LINUX) - result = system("xdg-open \"https://www.moddb.com/games/colobot-gold-edition\""); - #elif defined(PLATFORM_MACOSX) - result = system("open \"https://www.moddb.com/games/colobot-gold-edition\""); //TODO: Test on macOS - #endif - if (result == -1) - { - GetLogger()->Error("Failed to open Workshop page! Is any Web Broswer installed?\n"); - } + systemUtils->OpenWebsite(website); break; default: return true;