From 38d46b2dda0243cfa60bc6945859ebd587e0a851 Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Mon, 27 Jun 2022 17:45:44 -0400 Subject: Update MacOS to compile w/ Boost MacOS seems to rely on an older/different C++ stdlib than Windows or Linux. So replacing those with Boost equivalents. Mostly contained to the Resources thankfully! --- src/resources/Resources.cpp | 81 ++++++++++++++++++++---------------- src/resources/Resources.h | 41 +++++++++--------- src/resources/macos/MacResources.cpp | 20 ++++----- src/resources/macos/MacResources.h | 14 +++---- 4 files changed, 80 insertions(+), 76 deletions(-) (limited to 'src/resources') diff --git a/src/resources/Resources.cpp b/src/resources/Resources.cpp index ee43002..60ea836 100644 --- a/src/resources/Resources.cpp +++ b/src/resources/Resources.cpp @@ -30,50 +30,50 @@ #include Resources::Resources() { - this->font = std::make_shared(); - this->defaultsLua = std::make_shared(); + this->font = std::make_shared(); + this->defaultsLua = std::make_shared(); } void Resources::loadFontFiles() { // We will search 1 level deep for (auto &base : this->fontSearchDirs()) { - if (fs::exists(base) && fs::is_directory(base)) { - for (auto &item : fs::directory_iterator(base)) { + if (filesystem::exists(base) && filesystem::is_directory(base)) { + for (auto &item : filesystem::directory_iterator(base)) { // only 1 deep - if (item.exists() && item.is_directory()) { - for (auto &file : fs::directory_iterator(item)) { - if (file.exists() && file.is_regular_file()) { - auto ext = file.path().extension(); + if (filesystem::exists(item) && filesystem::is_directory(item)) { + for (auto &file : filesystem::directory_iterator(item)) { + if (filesystem::exists(file) && filesystem::is_regular_file(file)) { + auto ext = filesystem::path(file).extension(); bool matched = std::any_of(std::begin(FONT_TYPES), std::end(FONT_TYPES), [ext](auto &supported) { return supported == ext.generic_string(); }); if (matched) { - fonts.push_back(file.path()); + fonts.emplace_back(file); // set default if not set - if (!exists(*this->font) || - !is_regular_file(*this->font) && - file.path().filename() == DEFAULT_FONT) { - *this->font = file.path(); + if ((!filesystem::exists(*this->font) || + !filesystem::is_regular_file(*this->font)) && + filesystem::path(file).filename() == DEFAULT_FONT) { + *this->font = filesystem::path(file); } } } } - } else if (item.is_regular_file()) { - auto ext = item.path().extension(); + } else if (filesystem::is_regular_file(item)) { + auto ext = filesystem::path(item).extension(); bool matched = std::any_of(std::begin(FONT_TYPES), std::end(FONT_TYPES), [ext](auto &supported) { return supported == ext.generic_string(); }); if (matched) { - fonts.push_back(item.path()); + fonts.emplace_back(item); // set default if not set - if (!exists(*this->font) || - !is_regular_file(*this->font) && - item.path().filename() == DEFAULT_FONT) { - *this->font = item.path(); + if ((!filesystem::exists(*this->font) || + !filesystem::is_regular_file(*this->font)) && + filesystem::path(item).filename() == DEFAULT_FONT) { + *this->font = filesystem::path(item); } } } @@ -84,11 +84,11 @@ void Resources::loadFontFiles() { void Resources::loadLevels() { for (auto &base : this->levelSearchDirs()) { - if (fs::exists(base) && fs::is_directory(base)) { - for (auto &dir : fs::directory_iterator(base)) { - if (dir.exists() && dir.is_directory()) { - if (fs::exists(dir / DNG_MAP)) { - this->levels.push_back(dir.path()); + if (filesystem::exists(base) && filesystem::is_directory(base)) { + for (auto &dir : filesystem::directory_iterator(base)) { + if (filesystem::exists(dir) && filesystem::is_directory(dir)) { + if (filesystem::exists(dir / DNG_MAP)) { + this->levels.emplace_back(dir); } } } @@ -99,36 +99,43 @@ void Resources::loadLevels() { void Resources::loadDefaultLuaFile() { for (auto &base : this->defaultsSearchDirs()) { auto f = base / DEFAULT_LUA; - if (fs::exists(f)) { + if (filesystem::exists(f)) { *this->defaultsLua = f; break; } } } -std::vector Resources::getFontFiles() { return this->fonts; } -std::vector Resources::getLevels() { return this->levels; } -shared_ptr Resources::getFontFile() { return this->font; } -shared_ptr Resources::updateFont(int idx) { +std::vector Resources::getFontFiles() { return this->fonts; } +std::vector Resources::getLevels() { return this->levels; } +std::shared_ptr Resources::getFontFile() { + return this->font; +} +std::shared_ptr Resources::updateFont(int idx) { auto f = this->fonts[idx]; *this->font = f; return getFontFile(); } -shared_ptr Resources::getDefaultsLuaFile() { +std::shared_ptr Resources::getDefaultsLuaFile() { return this->defaultsLua; } -shared_ptr Resources::getLevelMap(int idx) { +std::shared_ptr Resources::getLevelMap(int idx) { auto lvlBase = this->levels[idx]; - fs::path dngMap = lvlBase / DNG_MAP; + filesystem::path dngMap = lvlBase / DNG_MAP; // existence of the level dng.map is asserted in the initializer - assert(fs::exists(dngMap)); - return std::make_shared(dngMap); + assert(filesystem::exists(dngMap)); + return std::make_shared(dngMap); } -std::optional> Resources::getLevelProcLua(int idx) { +optional> +Resources::getLevelProcLua(int idx) { auto lvlBase = this->levels[idx]; auto procLua = lvlBase / PROC_LUA; if (exists(procLua)) { - return std::make_shared(procLua); + return std::make_shared(procLua); } else { +#ifdef __APPLE__ + return boost::optional>(); +#else return nullopt; +#endif } } \ No newline at end of file diff --git a/src/resources/Resources.h b/src/resources/Resources.h index 2e0e7c0..c307c1b 100644 --- a/src/resources/Resources.h +++ b/src/resources/Resources.h @@ -29,13 +29,15 @@ #define DNG_RESOURCES_H #ifdef __APPLE__ -#include -namespace fs = std::experimental::filesystem; +#include "boost/filesystem.hpp" +#include "boost/optional.hpp" +using namespace boost; #else #include -namespace fs = std::filesystem; -#endif #include +using namespace std; +#endif +#include #include // struct LevelInfo { @@ -43,9 +45,8 @@ namespace fs = std::filesystem; // filesystem::path dngMap; // optional procLua; // }; -using namespace std; -static const string FONT_TYPES[] = {".otf", ".ttf"}; +static const std::string FONT_TYPES[] = {".otf", ".ttf"}; static const filesystem::path DEFAULT_LUA{"defaults.lua"}; static const filesystem::path DNG_MAP{"dng.map"}; static const filesystem::path PROC_LUA{"proc.lua"}; @@ -54,26 +55,26 @@ static const char *DEFAULT_FONT = "PressStart2P-vaV7.ttf"; class Resources { protected: - shared_ptr font; // chosen font - shared_ptr defaultsLua; // defaults lua file - vector fonts; // all found maps - vector levels; // all found maps - virtual vector levelSearchDirs() = 0; - virtual vector defaultsSearchDirs() = 0; - virtual vector fontSearchDirs() = 0; + std::shared_ptr font; // chosen font + std::shared_ptr defaultsLua; // defaults lua file + std::vector fonts; // all found maps + std::vector levels; // all found maps + virtual std::vector levelSearchDirs() = 0; + virtual std::vector defaultsSearchDirs() = 0; + virtual std::vector fontSearchDirs() = 0; public: Resources(); void loadLevels(); void loadFontFiles(); void loadDefaultLuaFile(); - vector getFontFiles(); - vector getLevels(); - shared_ptr updateFont(int idx); - shared_ptr getFontFile(); - shared_ptr getDefaultsLuaFile(); - shared_ptr getLevelMap(int idx); - optional> getLevelProcLua(int idx); + std::vector getFontFiles(); + std::vector getLevels(); + std::shared_ptr updateFont(int idx); + std::shared_ptr getFontFile(); + std::shared_ptr getDefaultsLuaFile(); + std::shared_ptr getLevelMap(int idx); + optional> getLevelProcLua(int idx); /* Helper method to convert any path to a const char * * Windows uses wchar_t so this can help provide a common way to * interact with files diff --git a/src/resources/macos/MacResources.cpp b/src/resources/macos/MacResources.cpp index 2b41daa..9ae3499 100644 --- a/src/resources/macos/MacResources.cpp +++ b/src/resources/macos/MacResources.cpp @@ -26,28 +26,26 @@ //======================================================================== #include "MacResources.h" -namespace fs = std::experimental::filesystem; MacResources::MacResources() : Resources() { - this->workingDir = std::filesystem::current_path(); - this->exeDir = std::filesystem::canonical("/proc/self/exe").remove_filename(); + this->workingDir = filesystem::current_path(); + this->exeDir = filesystem::canonical("/proc/self/exe").remove_filename(); // set an initial value - will get hardset in load if found - std::filesystem::path f = - workingDir / "res" / DEFAULT_FONT; // default as fallback - this->font = std::make_shared(f); - this->defaultsLua = std::make_shared(); + filesystem::path f = workingDir / "res" / DEFAULT_FONT; // default as fallback + this->font = std::make_shared(f); + this->defaultsLua = std::make_shared(); } -std::vector MacResources::levelSearchDirs() { +std::vector MacResources::levelSearchDirs() { return {workingDir / "maps", exeDir / "maps"}; } -std::vector MacResources::defaultsSearchDirs() { +std::vector MacResources::defaultsSearchDirs() { return {workingDir / "dnglib", exeDir / "dnglib"}; } -std::vector MacResources::fontSearchDirs() { +std::vector MacResources::fontSearchDirs() { return {workingDir / "res", exeDir / "res"}; } -const char *MacResources::convert_to_str(std::filesystem::path &path) { +const char *MacResources::convert_to_str(filesystem::path &path) { return path.c_str(); } diff --git a/src/resources/macos/MacResources.h b/src/resources/macos/MacResources.h index 1d19575..4cb66cd 100644 --- a/src/resources/macos/MacResources.h +++ b/src/resources/macos/MacResources.h @@ -29,21 +29,19 @@ #define DNG_MACRESOURCES_H #include "../Resources.h" -namespace fs = std::experimental::filesystem; - class MacResources : public Resources { public: MacResources(); protected: public: - const char *convert_to_str(fs::path &path) override; + const char *convert_to_str(filesystem::path &path) override; protected: - fs::path exeDir; - fs::path workingDir; - std::vector levelSearchDirs() override; - std::vector defaultsSearchDirs() override; - std::vector fontSearchDirs() override; + filesystem::path exeDir; + filesystem::path workingDir; + std::vector levelSearchDirs() override; + std::vector defaultsSearchDirs() override; + std::vector fontSearchDirs() override; }; #endif // DNG_MACRESOURCES_H -- cgit v1.2.3-54-g00ecf