diff --git a/src/openrct2/platform/Platform.Android.cpp b/src/openrct2/platform/Platform.Android.cpp new file mode 100644 index 0000000000..18f257f50f --- /dev/null +++ b/src/openrct2/platform/Platform.Android.cpp @@ -0,0 +1,39 @@ +#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers +/***************************************************************************** +* OpenRCT2, an open source clone of Roller Coaster Tycoon 2. +* +* OpenRCT2 is the work of many authors, a full list can be found in contributors.md +* For more information, visit https://github.com/OpenRCT2/OpenRCT2 +* +* OpenRCT2 is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* A full copy of the GNU General Public License can be found in licence.txt +*****************************************************************************/ +#pragma endregion + +#ifdef __ANDROID__ + +#include "Platform2.h" + +namespace Platform +{ + std::string GetFolderPath(SPECIAL_FOLDER folder) + { + // Android builds currently only read from /sdcard/openrct2* + switch (folder) + { + case SPECIAL_FOLDER::USER_CACHE: + case SPECIAL_FOLDER::USER_CONFIG: + case SPECIAL_FOLDER::USER_DATA: + case SPECIAL_FOLDER::USER_HOME: + return "/sdcard"; + default: + return std::string(); + } + } +} + +#endif diff --git a/src/openrct2/platform/Platform.Linux.cpp b/src/openrct2/platform/Platform.Linux.cpp new file mode 100644 index 0000000000..c6e4f03878 --- /dev/null +++ b/src/openrct2/platform/Platform.Linux.cpp @@ -0,0 +1,67 @@ +#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers +/***************************************************************************** +* OpenRCT2, an open source clone of Roller Coaster Tycoon 2. +* +* OpenRCT2 is the work of many authors, a full list can be found in contributors.md +* For more information, visit https://github.com/OpenRCT2/OpenRCT2 +* +* OpenRCT2 is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* A full copy of the GNU General Public License can be found in licence.txt +*****************************************************************************/ +#pragma endregion + +#ifdef __linux__ + +#include +#include "../core/Path.hpp" +#include "Platform2.h" + +namespace Platform +{ + std::string GetFolderPath(SPECIAL_FOLDER folder) + { + switch (folder) + { + case SPECIAL_FOLDER::USER_CACHE: + { + auto path = GetEnvironmentPath("XDG_CACHE_HOME"); + if (path.empty()) + { + auto home = GetFolderPath(SPECIAL_FOLDER::USER_HOME); + path = Path::Combine(home, ".cache"); + } + return path; + } + case SPECIAL_FOLDER::USER_CONFIG: + { + auto path = GetEnvironmentPath("XDG_CONFIG_HOME"); + if (path.empty()) + { + auto home = GetFolderPath(SPECIAL_FOLDER::USER_HOME); + path = Path::Combine(home, ".config"); + } + return path; + } + case SPECIAL_FOLDER::USER_DATA: + { + auto path = GetEnvironmentPath("XDG_DATA_HOME"); + if (path.empty()) + { + auto home = GetFolderPath(SPECIAL_FOLDER::USER_HOME); + path = Path::Combine(home, ".local/share"); + } + return path; + } + case SPECIAL_FOLDER::USER_HOME: + return GetHomePath(); + default: + return std::string(); + } + } +} + +#endif diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp new file mode 100644 index 0000000000..18b258cbae --- /dev/null +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -0,0 +1,87 @@ +#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers +/***************************************************************************** +* OpenRCT2, an open source clone of Roller Coaster Tycoon 2. +* +* OpenRCT2 is the work of many authors, a full list can be found in contributors.md +* For more information, visit https://github.com/OpenRCT2/OpenRCT2 +* +* OpenRCT2 is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* A full copy of the GNU General Public License can be found in licence.txt +*****************************************************************************/ +#pragma endregion + +#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) + +#include +#include +#include +#include "../core/String.hpp" +#include "Platform2.h" +#include "platform.h" + +namespace Platform +{ + uint32 GetTicks() + { + return platform_get_ticks(); + } + + std::string GetEnvironmentVariable(const std::string &name) + { + return String::ToStd(getenv(name.c_str())); + } + + std::string GetEnvironmentPath(const char * name) + { + auto value = getenv(name); + if (value == nullptr) + { + return std::string(); + } + else + { + auto colon = std::strchr(value, ':'); + if (colon == nullptr) + { + return std::string(value); + } + else + { + return std::string(value, colon); + } + } + } + + std::string GetHomePath() + { + std::string path; + auto pw = getpwuid(getuid()); + if (pw != nullptr) + { + path = pw->pw_dir; + } + else + { + path = GetEnvironmentVariable("HOME"); + } + if (path.empty()) + { + path = "/"; + } + return path; + } + + std::string GetInstallPath() + { + utf8 path[MAX_PATH]; + platform_resolve_openrct_data_path(); + platform_get_openrct_data_path(path, sizeof(path)); + return path; + } +} + +#endif diff --git a/src/openrct2/platform/Platform2.cpp b/src/openrct2/platform/Platform.Win32.cpp similarity index 55% rename from src/openrct2/platform/Platform2.cpp rename to src/openrct2/platform/Platform.Win32.cpp index 988d45ee00..3b94c7f818 100644 --- a/src/openrct2/platform/Platform2.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -14,21 +14,17 @@ *****************************************************************************/ #pragma endregion -#include #ifdef _WIN32 - #define WIN32_LEAN_AND_MEAN - #include - #include - #undef GetEnvironmentVariable - #if !defined(__MINGW32__) && ((NTDDI_VERSION >= NTDDI_VISTA) && !defined(_USING_V110_SDK71_) && !defined(_ATL_XP_TARGETING)) - #define __USE_SHGETKNOWNFOLDERPATH__ - #endif -#else - #include +#define WIN32_LEAN_AND_MEAN +#include +#include +#undef GetEnvironmentVariable + +#if !defined(__MINGW32__) && ((NTDDI_VERSION >= NTDDI_VISTA) && !defined(_USING_V110_SDK71_) && !defined(_ATL_XP_TARGETING)) + #define __USE_SHGETKNOWNFOLDERPATH__ #endif - #include "../core/Path.hpp" #include "../core/String.hpp" #include "../core/Util.hpp" @@ -44,7 +40,6 @@ namespace Platform std::string GetEnvironmentVariable(const std::string &name) { -#ifdef _WIN32 std::wstring result; auto wname = String::ToUtf16(name); wchar_t wvalue[256]; @@ -58,40 +53,13 @@ namespace Platform auto wlvalue = new wchar_t[valueSize]; GetEnvironmentVariableW(wname.c_str(), wlvalue, valueSize); result = wlvalue; - delete wlvalue; + delete[] wlvalue; } return String::ToUtf8(result); -#else - return String::ToStd(getenv(name.c_str())); -#endif } -#ifndef _WIN32 - static std::string GetEnvironmentPath(const char * name) - { - auto value = getenv(name); - if (value == nullptr) - { - return std::string(); - } - else - { - auto colon = std::strchr(value, ':'); - if (colon == nullptr) - { - return std::string(value); - } - else - { - return std::string(value, colon); - } - } - } -#endif - static std::string GetHomePathViaEnvironment() { -#ifdef _WIN32 std::string result; auto homedrive = GetEnvironmentVariable("HOMEDRIVE"); auto homepath = GetEnvironmentVariable("HOMEPATH"); @@ -100,12 +68,8 @@ namespace Platform result = Path::Combine(homedrive, homepath); } return result; -#else - return GetEnvironmentVariable("HOME"); -#endif } -#ifdef _WIN32 #ifdef __USE_SHGETKNOWNFOLDERPATH__ static std::string WIN32_GetKnownFolderPath(REFKNOWNFOLDERID rfid) { @@ -129,14 +93,12 @@ namespace Platform } return path; } -#endif #endif std::string GetFolderPath(SPECIAL_FOLDER folder) { switch (folder) { -#if defined(_WIN32) // We currently store everything under Documents/OpenRCT2 case SPECIAL_FOLDER::USER_CACHE: case SPECIAL_FOLDER::USER_CONFIG: @@ -170,72 +132,6 @@ namespace Platform } return path; } -#elif defined (__ANDROID__) - // Android builds currently only read from /sdcard/openrct2* - case SPECIAL_FOLDER::USER_CACHE: - case SPECIAL_FOLDER::USER_CONFIG: - case SPECIAL_FOLDER::USER_DATA: - case SPECIAL_FOLDER::USER_HOME: - return "/sdcard"; -#elif defined (__MACOS__) - // macOS stores everything in ~/Library/Application Support/OpenRCT2 - case SPECIAL_FOLDER::USER_CACHE: - case SPECIAL_FOLDER::USER_CONFIG: - case SPECIAL_FOLDER::USER_DATA: - case SPECIAL_FOLDER::USER_HOME: - { - auto home = GetFolderPath(SPECIAL_FOLDER::USER_HOME); - return Path::Combine(home, "Library/Application Support"); - } -#else - case SPECIAL_FOLDER::USER_CACHE: - { - auto path = GetEnvironmentPath("XDG_CACHE_HOME"); - if (path.empty()) - { - auto home = GetFolderPath(SPECIAL_FOLDER::USER_HOME); - path = Path::Combine(home, ".cache"); - } - return path; - } - case SPECIAL_FOLDER::USER_CONFIG: - { - auto path = GetEnvironmentPath("XDG_CONFIG_HOME"); - if (path.empty()) - { - auto home = GetFolderPath(SPECIAL_FOLDER::USER_HOME); - path = Path::Combine(home, ".config"); - } - return path; - } - case SPECIAL_FOLDER::USER_DATA: - { - auto path = GetEnvironmentPath("XDG_DATA_HOME"); - if (path.empty()) - { - auto home = GetFolderPath(SPECIAL_FOLDER::USER_HOME); - path = Path::Combine(home, ".local/share"); - } - return path; - } - case SPECIAL_FOLDER::USER_HOME: - { - std::string path; - auto pw = getpwuid(getuid()); - if (pw != nullptr) - { - path = pw->pw_dir; - } - else - { - path = GetHomePathViaEnvironment(); - } - if (path.empty()) - { - return "/"; - } - } -#endif default: return std::string(); } @@ -243,9 +139,11 @@ namespace Platform std::string GetInstallPath() { - utf8 path[260]; + utf8 path[MAX_PATH]; platform_resolve_openrct_data_path(); platform_get_openrct_data_path(path, sizeof(path)); return path; } } + +#endif diff --git a/src/openrct2/platform/Platform.macOS.cpp b/src/openrct2/platform/Platform.macOS.cpp new file mode 100644 index 0000000000..25e5a9bd4c --- /dev/null +++ b/src/openrct2/platform/Platform.macOS.cpp @@ -0,0 +1,44 @@ +#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers +/***************************************************************************** +* OpenRCT2, an open source clone of Roller Coaster Tycoon 2. +* +* OpenRCT2 is the work of many authors, a full list can be found in contributors.md +* For more information, visit https://github.com/OpenRCT2/OpenRCT2 +* +* OpenRCT2 is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* A full copy of the GNU General Public License can be found in licence.txt +*****************************************************************************/ +#pragma endregion + +#ifdef __MACOS__ + +#include "../core/Path.hpp" +#include "Platform2.h" + +namespace Platform +{ + std::string GetFolderPath(SPECIAL_FOLDER folder) + { + // macOS stores everything in ~/Library/Application Support/OpenRCT2 + switch (folder) + { + case SPECIAL_FOLDER::USER_CACHE: + case SPECIAL_FOLDER::USER_CONFIG: + case SPECIAL_FOLDER::USER_DATA: + { + auto home = GetFolderPath(SPECIAL_FOLDER::USER_HOME); + return Path::Combine(home, "Library/Application Support"); + } + case SPECIAL_FOLDER::USER_HOME: + return GetHomePath(); + default: + return std::string(); + } + } +} + +#endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 5e0dc72319..16f5eb9575 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -35,6 +35,11 @@ namespace Platform std::string GetEnvironmentVariable(const std::string &name); std::string GetFolderPath(SPECIAL_FOLDER folder); std::string GetInstallPath(); + +#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) + std::string GetEnvironmentPath(const char * name); + std::string GetHomePath(); +#endif } #endif