Skip to content

Commit

Permalink
Merge pull request godotengine#69334 from bruvzg/ios_user_dir
Browse files Browse the repository at this point in the history
[iOS] Read document and cache path directly in the OS code, instead of passing in from main.
  • Loading branch information
akien-mga committed Nov 29, 2022
2 parents 6fdbf79 + 0cea664 commit 3f35f27
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 33 deletions.
9 changes: 2 additions & 7 deletions platform/ios/app_delegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
extern int gargc;
extern char **gargv;

extern int ios_main(int, char **, String, String);
extern int ios_main(int, char **);
extern void ios_finish();

@implementation AppDelegate
Expand All @@ -66,12 +66,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// Create a full-screen window
self.window = [[UIWindow alloc] initWithFrame:windowBounds];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];

int err = ios_main(gargc, gargv, String::utf8([documentsDirectory UTF8String]), String::utf8([cacheDirectory UTF8String]));
int err = ios_main(gargc, gargv);

if (err != 0) {
// bail, things did not go very well for us, should probably output a message on screen with our error code...
Expand Down
8 changes: 2 additions & 6 deletions platform/ios/godot_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@

static OS_IOS *os = nullptr;

int add_path(int, char **);
int add_cmdline(int, char **);
int ios_main(int, char **, String);

int add_path(int p_argc, char **p_args) {
NSString *str = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"godot_path"];
if (!str) {
Expand Down Expand Up @@ -74,7 +70,7 @@ int add_cmdline(int p_argc, char **p_args) {
return p_argc;
}

int ios_main(int argc, char **argv, String data_dir, String cache_dir) {
int ios_main(int argc, char **argv) {
size_t len = strlen(argv[0]);

while (len--) {
Expand All @@ -95,7 +91,7 @@ int ios_main(int argc, char **argv, String data_dir, String cache_dir) {
char cwd[512];
getcwd(cwd, sizeof(cwd));
printf("cwd %s\n", cwd);
os = new OS_IOS(data_dir, cache_dir);
os = new OS_IOS();

// We must override main when testing is enabled
TEST_MAIN_OVERRIDE
Expand Down
5 changes: 1 addition & 4 deletions platform/ios/os_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,14 @@ class OS_IOS : public OS_Unix {

virtual void finalize() override;

String user_data_dir;
String cache_dir;

bool is_focused = false;

void deinitialize_modules();

public:
static OS_IOS *get_singleton();

OS_IOS(String p_data_dir, String p_cache_dir);
OS_IOS();
~OS_IOS();

void initialize_modules();
Expand Down
32 changes: 16 additions & 16 deletions platform/ios/os_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void register_dynamic_symbol(char *name, void *address) {
return (OS_IOS *)OS::get_singleton();
}

OS_IOS::OS_IOS(String p_data_dir, String p_cache_dir) {
OS_IOS::OS_IOS() {
for (int i = 0; i < ios_init_callbacks_count; ++i) {
ios_init_callbacks[i]();
}
Expand All @@ -101,11 +101,6 @@ void register_dynamic_symbol(char *name, void *address) {

main_loop = nullptr;

// can't call set_data_dir from here, since it requires DirAccess
// which is initialized in initialize_core
user_data_dir = p_data_dir;
cache_dir = p_cache_dir;

Vector<Logger *> loggers;
loggers.push_back(memnew(SyslogLogger));
#ifdef DEBUG_ENABLED
Expand Down Expand Up @@ -272,20 +267,25 @@ void register_dynamic_symbol(char *name, void *address) {
}

String OS_IOS::get_user_data_dir() const {
static bool user_data_dir_set = false;
if (user_data_dir_set) {
String old_dir = user_data_dir;
Ref<DirAccess> da = DirAccess::open(old_dir);
const_cast<OS_IOS *>(this)->user_data_dir = da->get_current_dir();
user_data_dir_set = true;

printf("setting data dir to %s from %s\n", user_data_dir.utf8().get_data(), old_dir.utf8().get_data());
static String ret;
if (ret.is_empty()) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if (paths && [paths count] >= 1) {
ret.parse_utf8([[paths firstObject] UTF8String]);
}
}
return user_data_dir;
return ret;
}

String OS_IOS::get_cache_path() const {
return cache_dir;
static String ret;
if (ret.is_empty()) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
if (paths && [paths count] >= 1) {
ret.parse_utf8([[paths firstObject] UTF8String]);
}
}
return ret;
}

String OS_IOS::get_locale() const {
Expand Down

0 comments on commit 3f35f27

Please sign in to comment.