Move platform-dependent code to system_*.cpp

pyro-refactor
DavivaD 2019-07-27 16:59:51 +02:00
parent cd140f1384
commit 7d30a82747
11 changed files with 110 additions and 25 deletions

View File

@ -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);
}

View File

@ -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;

View File

@ -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);

View File

@ -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:

View File

@ -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);

View File

@ -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:

View File

@ -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

View File

@ -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;
};

View File

@ -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;

View File

@ -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:

View File

@ -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;