1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-01 12:57:51 +02:00

extract iOS helpers into a shared lib

This commit is contained in:
Andrey Filipenkov 2022-08-15 16:29:11 +03:00
parent ec6c72807b
commit e1c9903a45
12 changed files with 120 additions and 99 deletions

View File

@ -404,6 +404,10 @@ set(SCRIPTING_LIB_DIR "${LIB_DIR}/scripting")
# Add subdirectories #
#######################################
if(APPLE_IOS)
add_subdirectory(ios)
endif()
include(VCMI_lib)
if(BUILD_SINGLE_APP)
add_subdirectory(lib_client)

View File

@ -198,6 +198,8 @@ if(WIN32)
target_compile_definitions(vcmiclient PRIVATE WINDOWS_IGNORE_PACKING_MISMATCH)
elseif(APPLE_IOS)
target_link_libraries(vcmiclient PRIVATE
iOS_utils
# FFmpeg
bz2
iconv

View File

@ -18,8 +18,10 @@
#ifdef VCMI_APPLE
#include <dispatch/dispatch.h>
#endif
extern double ios_screenScale(); // TODO ios: move to appropriate file
#ifdef VCMI_IOS
#include "iOS_utils.h"
#endif
const SDL_Color Colors::YELLOW = { 229, 215, 123, 0 };
@ -813,7 +815,7 @@ void CSDL_Ext::startTextInput(SDL_Rect * where)
SDL_RenderGetScale(renderer, &scaleX, &scaleY);
SDL_RenderGetViewport(renderer, &viewport);
auto nativeScale = ios_screenScale();
const auto nativeScale = iOS_utils::screenScale();
auto rectInScreenCoordinates = *where;
rectInScreenCoordinates.x = (viewport.x + rectInScreenCoordinates.x) * scaleX / nativeScale;
rectInScreenCoordinates.y = (viewport.y + rectInScreenCoordinates.y) * scaleY / nativeScale;

View File

@ -22,9 +22,6 @@
#import <UIKit/UIKit.h>
double ios_screenScale() { return UIScreen.mainScreen.nativeScale; }
@interface SDLViewObserver : NSObject <UIGestureRecognizerDelegate>
@property (nonatomic, strong) GameChatKeyboardHanlder * gameChatHandler;
@end

View File

@ -441,11 +441,6 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE)
${MAIN_LIB_DIR}/VCMI_Lib.h
)
if(APPLE_IOS)
set(lib_SRCS ${lib_SRCS} ${MAIN_LIB_DIR}/CIOSUtils.m)
set(lib_HEADERS ${lib_HEADERS} ${MAIN_LIB_DIR}/CIOSUtils.h)
endif(APPLE_IOS)
assign_source_group(${lib_SRCS} ${lib_HEADERS})
add_library(${TARGET_NAME} ${LIBRARY_TYPE} ${lib_SRCS} ${lib_HEADERS})
@ -454,6 +449,9 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE)
minizip::minizip ZLIB::ZLIB
${SYSTEM_LIBS} Boost::boost Boost::thread Boost::filesystem Boost::program_options Boost::locale Boost::date_time
)
if(APPLE_IOS)
target_link_libraries(${TARGET_NAME} PUBLIC iOS_utils)
endif()
target_include_directories(${TARGET_NAME}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}

11
ios/CMakeLists.txt Normal file
View File

@ -0,0 +1,11 @@
add_library(iOS_utils SHARED
iOS_utils.h
iOS_utils.mm
)
target_link_libraries(iOS_utils PRIVATE
"-framework Foundation"
"-framework UIKit"
)
target_include_directories(iOS_utils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
vcmi_set_output_dir(iOS_utils "")

31
ios/iOS_utils.h Normal file
View File

@ -0,0 +1,31 @@
/*
* iOS_utils.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
*
*/
#pragma once
#include <TargetConditionals.h>
#pragma GCC visibility push(default)
namespace iOS_utils
{
const char *documentsPath();
const char *cachesPath();
#if TARGET_OS_SIMULATOR
const char *hostApplicationSupportPath();
#endif
const char *bundlePath();
const char *frameworksPath();
const char *bundleIdentifier();
double screenScale();
}
#pragma GCC visibility pop

52
ios/iOS_utils.mm Normal file
View File

@ -0,0 +1,52 @@
/*
* iOS_utils.mm, 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
*
*/
#include "iOS_utils.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
namespace
{
NSString *standardPathNative(NSSearchPathDirectory directory)
{
return [NSFileManager.defaultManager URLForDirectory:directory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:NULL].path;
}
const char *standardPath(NSSearchPathDirectory directory) { return standardPathNative(directory).fileSystemRepresentation; }
}
namespace iOS_utils
{
const char *documentsPath() { return standardPath(NSDocumentDirectory); }
const char *cachesPath() { return standardPath(NSCachesDirectory); }
#if TARGET_OS_SIMULATOR
const char *hostApplicationSupportPath()
{
static NSString *applicationSupportPath;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
auto cachesPath = standardPathNative(NSCachesDirectory);
auto afterMacOsHomeDirPos = [cachesPath rangeOfString:@"Library/Developer"].location;
NSCAssert(afterMacOsHomeDirPos != NSNotFound, @"simulator directory location is not under user's home directory: %@", cachesPath);
applicationSupportPath = [[cachesPath substringToIndex:afterMacOsHomeDirPos] stringByAppendingPathComponent:@"Library/Application Support/vcmi"].stringByResolvingSymlinksInPath;
});
return applicationSupportPath.fileSystemRepresentation;
}
#endif
const char *bundlePath() { return NSBundle.mainBundle.bundlePath.fileSystemRepresentation; }
const char *frameworksPath() { return NSBundle.mainBundle.privateFrameworksPath.fileSystemRepresentation; }
const char *bundleIdentifier() { return NSBundle.mainBundle.bundleIdentifier.UTF8String; }
double screenScale() { return UIScreen.mainScreen.nativeScale; }
}

View File

@ -1,36 +0,0 @@
/*
* 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
*
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <TargetConditionals.h>
#ifdef __OBJC__
@class NSURL;
#endif
extern const char *ios_documentsPath();
extern const char *ios_cachesPath();
#if TARGET_OS_SIMULATOR
extern const char *ios_hostApplicationSupportPath();
#endif
extern const char *ios_bundlePath();
extern const char *ios_frameworksPath();
extern const char *ios_bundleIdentifier();
#ifdef __cplusplus
}
#endif

View File

@ -1,42 +0,0 @@
/*
* 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/Foundation.h>
static NSString *standardPathNative(NSSearchPathDirectory directory)
{
return [NSFileManager.defaultManager URLForDirectory:directory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:NULL].path;
}
static const char *standardPath(NSSearchPathDirectory directory) { return standardPathNative(directory).fileSystemRepresentation; }
const char *ios_documentsPath() { return standardPath(NSDocumentDirectory); }
const char *ios_cachesPath() { return standardPath(NSCachesDirectory); }
#if TARGET_OS_SIMULATOR
const char *ios_hostApplicationSupportPath()
{
static NSString *applicationSupportPath;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__auto_type cachesPath = standardPathNative(NSCachesDirectory);
__auto_type afterMacOsHomeDirPos = [cachesPath rangeOfString:@"Library/Developer"].location;
NSCAssert(afterMacOsHomeDirPos != NSNotFound, @"simulator directory location is not under user's home directory: %@", cachesPath);
applicationSupportPath = [[cachesPath substringToIndex:afterMacOsHomeDirPos] stringByAppendingPathComponent:@"Library/Application Support/vcmi"].stringByResolvingSymlinksInPath;
});
return applicationSupportPath.fileSystemRepresentation;
}
#endif
const char *ios_bundlePath() { return NSBundle.mainBundle.bundlePath.fileSystemRepresentation; }
const char *ios_frameworksPath() { return NSBundle.mainBundle.privateFrameworksPath.fileSystemRepresentation; }
const char *ios_bundleIdentifier() { return NSBundle.mainBundle.bundleIdentifier.UTF8String; }

View File

@ -11,6 +11,10 @@
#include "StdInc.h"
#include "VCMIDirs.h"
#ifdef VCMI_IOS
#include "iOS_utils.h"
#endif
VCMI_LIB_NAMESPACE_BEGIN
namespace bfs = boost::filesystem;
@ -377,8 +381,6 @@ bfs::path VCMIDirsApple::userConfigPath() const { return userDataPath() / "confi
std::string VCMIDirsApple::libraryName(const std::string& basename) const { return "lib" + basename + ".dylib"; }
#ifdef VCMI_IOS
#import "CIOSUtils.h"
class VCMIDirsIOS final : public VCMIDirsApple
{
public:
@ -392,25 +394,25 @@ class VCMIDirsIOS final : public VCMIDirsApple
bfs::path binaryPath() 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()}; }
bfs::path VCMIDirsIOS::userDataPath() const { return {iOS_utils::documentsPath()}; }
bfs::path VCMIDirsIOS::userCachePath() const { return {iOS_utils::cachesPath()}; }
bfs::path VCMIDirsIOS::userLogsPath() const { return {iOS_utils::documentsPath()}; }
std::vector<bfs::path> VCMIDirsIOS::dataPaths() const
{
std::vector<bfs::path> paths;
paths.reserve(4);
#ifdef VCMI_IOS_SIM
paths.emplace_back(ios_hostApplicationSupportPath());
paths.emplace_back(iOS_utils::hostApplicationSupportPath());
#endif
paths.emplace_back(userDataPath());
paths.emplace_back(ios_documentsPath());
paths.emplace_back(iOS_utils::documentsPath());
paths.emplace_back(binaryPath());
return paths;
}
bfs::path VCMIDirsIOS::libraryPath() const { return {ios_frameworksPath()}; }
bfs::path VCMIDirsIOS::binaryPath() const { return {ios_bundlePath()}; }
bfs::path VCMIDirsIOS::libraryPath() const { return {iOS_utils::frameworksPath()}; }
bfs::path VCMIDirsIOS::binaryPath() const { return {iOS_utils::bundlePath()}; }
#elif defined(VCMI_MAC)
class VCMIDirsOSX final : public VCMIDirsApple
{

View File

@ -31,7 +31,7 @@ namespace ELogLevel
}
}
#elif defined(VCMI_IOS)
#import "../CIOSUtils.h"
#import "iOS_utils.h"
extern "C" {
#include <os/log.h>
}
@ -391,7 +391,7 @@ void CLogConsoleTarget::write(const LogRecord & record)
currentLog = logIt->second;
else
{
currentLog = os_log_create(ios_bundleIdentifier(), domainName.c_str());
currentLog = os_log_create(iOS_utils::bundleIdentifier(), domainName.c_str());
logs.insert({domainName, currentLog});
}
os_log_with_type(currentLog, type, "%{public}s", message.c_str());