From 8249171066711141b4c5884012c7fa48fa59dbc0 Mon Sep 17 00:00:00 2001 From: Andrey Filipenkov Date: Tue, 2 Mar 2021 06:23:00 +0300 Subject: [PATCH] implement VCMIDirs --- lib/CIOSUtils.h | 15 ++++++++++++ lib/CIOSUtils.m | 24 +++++++++++++++++++ lib/CMakeLists.txt | 5 ++++ lib/VCMIDirs.cpp | 60 ++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 lib/CIOSUtils.h create mode 100644 lib/CIOSUtils.m diff --git a/lib/CIOSUtils.h b/lib/CIOSUtils.h new file mode 100644 index 000000000..0e5ac20cc --- /dev/null +++ b/lib/CIOSUtils.h @@ -0,0 +1,15 @@ +/* + * CIOSUtils.h, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ + +extern const char *ios_documentsPath(); +extern const char *ios_cachesPath(); + +extern const char *ios_bundlePath(); +extern const char *ios_frameworksPath(); diff --git a/lib/CIOSUtils.m b/lib/CIOSUtils.m new file mode 100644 index 000000000..f3f765352 --- /dev/null +++ b/lib/CIOSUtils.m @@ -0,0 +1,24 @@ +/* + * CIOSUtils.m, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ + +#import "CIOSUtils.h" + +@import Foundation; + +static const char *standardPath(NSSearchPathDirectory directory) +{ + return [NSFileManager.defaultManager URLForDirectory:directory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:NULL].path.UTF8String; +} + +const char *ios_documentsPath() { return standardPath(NSDocumentDirectory); } +const char *ios_cachesPath() { return standardPath(NSCachesDirectory); } + +const char *ios_bundlePath() { return NSBundle.mainBundle.bundlePath.UTF8String; } +const char *ios_frameworksPath() { return [NSBundle.mainBundle.bundlePath stringByAppendingPathComponent:@"Frameworks"].UTF8String; } // TODO: sharedFrameworksPath? diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index c816b6331..76f4cc4a5 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -439,6 +439,11 @@ set(lib_HEADERS VCMI_Lib.h ) +if(APPLE_IOS) + set(lib_SRCS ${lib_SRCS} CIOSUtils.m) + set(lib_HEADERS ${lib_HEADERS} CIOSUtils.h) +endif(APPLE_IOS) + assign_source_group(${lib_SRCS} ${lib_HEADERS}) add_library(vcmi SHARED ${lib_SRCS} ${lib_HEADERS}) diff --git a/lib/VCMIDirs.cpp b/lib/VCMIDirs.cpp index ee42f02b5..ecac91b86 100644 --- a/lib/VCMIDirs.cpp +++ b/lib/VCMIDirs.cpp @@ -349,7 +349,7 @@ class IVCMIDirsUNIX : public IVCMIDirs boost::filesystem::path clientPath() const override; boost::filesystem::path serverPath() const override; - bool developmentMode() const; + virtual bool developmentMode() const; }; bool IVCMIDirsUNIX::developmentMode() const @@ -362,12 +362,54 @@ bfs::path IVCMIDirsUNIX::clientPath() const { return binaryPath() / "vcmiclient" bfs::path IVCMIDirsUNIX::serverPath() const { return binaryPath() / "vcmiserver"; } #ifdef VCMI_APPLE -class VCMIDirsOSX final : public IVCMIDirsUNIX +class VCMIDirsApple : public IVCMIDirsUNIX +{ + public: + bfs::path userConfigPath() const override; + + std::string libraryName(const std::string& basename) const override; +}; + +bfs::path VCMIDirsApple::userConfigPath() const { return userDataPath() / "config"; } + +std::string VCMIDirsApple::libraryName(const std::string& basename) const { return "lib" + basename + ".dylib"; } + +#ifdef VCMI_IOS +extern "C" { +#import "CIOSUtils.h" +} + +class VCMIDirsIOS final : public VCMIDirsApple +{ + public: + bfs::path userDataPath() const override; + bfs::path userCachePath() const override; + bfs::path userLogsPath() const override; + + std::vector dataPaths() const override; + + bfs::path libraryPath() const override; + bfs::path binaryPath() const override; + + bool developmentMode() const override; +}; + +bfs::path VCMIDirsIOS::userDataPath() const { return {ios_documentsPath()}; } +bfs::path VCMIDirsIOS::userCachePath() const { return {ios_cachesPath()}; } +bfs::path VCMIDirsIOS::userLogsPath() const { return {ios_documentsPath()}; } + +std::vector VCMIDirsIOS::dataPaths() const { return {userDataPath()}; } + +bfs::path VCMIDirsIOS::libraryPath() const { return {ios_frameworksPath()}; } +bfs::path VCMIDirsIOS::binaryPath() const { return {ios_bundlePath()}; } + +bool VCMIDirsIOS::developmentMode() const { return false; } +#elif defined(VCMI_MAC) +class VCMIDirsOSX final : public VCMIDirsApple { public: boost::filesystem::path userDataPath() const override; boost::filesystem::path userCachePath() const override; - boost::filesystem::path userConfigPath() const override; boost::filesystem::path userLogsPath() const override; std::vector dataPaths() const override; @@ -375,8 +417,6 @@ class VCMIDirsOSX final : public IVCMIDirsUNIX boost::filesystem::path libraryPath() const override; boost::filesystem::path binaryPath() const override; - std::string libraryName(const std::string& basename) const override; - void init() override; }; @@ -436,7 +476,6 @@ bfs::path VCMIDirsOSX::userDataPath() const return bfs::path(homeDir) / "Library" / "Application Support" / "vcmi"; } bfs::path VCMIDirsOSX::userCachePath() const { return userDataPath(); } -bfs::path VCMIDirsOSX::userConfigPath() const { return userDataPath() / "config"; } bfs::path VCMIDirsOSX::userLogsPath() const { @@ -463,8 +502,7 @@ std::vector VCMIDirsOSX::dataPaths() const bfs::path VCMIDirsOSX::libraryPath() const { return "."; } bfs::path VCMIDirsOSX::binaryPath() const { return "."; } - -std::string VCMIDirsOSX::libraryName(const std::string& basename) const { return "lib" + basename + ".dylib"; } +#endif // VCMI_IOS, VCMI_MAC #elif defined(VCMI_XDG) class VCMIDirsXDG : public IVCMIDirsUNIX { @@ -635,9 +673,11 @@ namespace VCMIDirs static VCMIDirsAndroid singleton; #elif defined(VCMI_XDG) static VCMIDirsXDG singleton; - #elif defined(VCMI_APPLE) + #elif defined(VCMI_MAC) static VCMIDirsOSX singleton; - #endif + #elif defined(VCMI_IOS) + static VCMIDirsIOS singleton; + #endif static bool initialized = false; if (!initialized)