Merge pull request #69334 from bruvzg/ios_user_dir

[iOS] Read document and cache path directly in the OS code, instead of passing in from main.
This commit is contained in:
Rémi Verschelde 2022-11-29 16:47:14 +01:00
commit 3f35f27838
No known key found for this signature in database
GPG Key ID: C3336907360768E1
4 changed files with 21 additions and 33 deletions

View File

@ -45,7 +45,7 @@
extern int gargc; extern int gargc;
extern char **gargv; extern char **gargv;
extern int ios_main(int, char **, String, String); extern int ios_main(int, char **);
extern void ios_finish(); extern void ios_finish();
@implementation AppDelegate @implementation AppDelegate
@ -66,12 +66,7 @@ static ViewController *mainViewController = nil;
// Create a full-screen window // Create a full-screen window
self.window = [[UIWindow alloc] initWithFrame:windowBounds]; self.window = [[UIWindow alloc] initWithFrame:windowBounds];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); int err = ios_main(gargc, gargv);
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]));
if (err != 0) { if (err != 0) {
// bail, things did not go very well for us, should probably output a message on screen with our error code... // bail, things did not go very well for us, should probably output a message on screen with our error code...

View File

@ -38,10 +38,6 @@
static OS_IOS *os = nullptr; 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) { int add_path(int p_argc, char **p_args) {
NSString *str = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"godot_path"]; NSString *str = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"godot_path"];
if (!str) { if (!str) {
@ -74,7 +70,7 @@ int add_cmdline(int p_argc, char **p_args) {
return p_argc; 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]); size_t len = strlen(argv[0]);
while (len--) { while (len--) {
@ -95,7 +91,7 @@ int ios_main(int argc, char **argv, String data_dir, String cache_dir) {
char cwd[512]; char cwd[512];
getcwd(cwd, sizeof(cwd)); getcwd(cwd, sizeof(cwd));
printf("cwd %s\n", 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 // We must override main when testing is enabled
TEST_MAIN_OVERRIDE TEST_MAIN_OVERRIDE

View File

@ -71,9 +71,6 @@ private:
virtual void finalize() override; virtual void finalize() override;
String user_data_dir;
String cache_dir;
bool is_focused = false; bool is_focused = false;
void deinitialize_modules(); void deinitialize_modules();
@ -81,7 +78,7 @@ private:
public: public:
static OS_IOS *get_singleton(); static OS_IOS *get_singleton();
OS_IOS(String p_data_dir, String p_cache_dir); OS_IOS();
~OS_IOS(); ~OS_IOS();
void initialize_modules(); void initialize_modules();

View File

@ -90,7 +90,7 @@ OS_IOS *OS_IOS::get_singleton() {
return (OS_IOS *)OS::get_singleton(); 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) { for (int i = 0; i < ios_init_callbacks_count; ++i) {
ios_init_callbacks[i](); ios_init_callbacks[i]();
} }
@ -101,11 +101,6 @@ OS_IOS::OS_IOS(String p_data_dir, String p_cache_dir) {
main_loop = nullptr; 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; Vector<Logger *> loggers;
loggers.push_back(memnew(SyslogLogger)); loggers.push_back(memnew(SyslogLogger));
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
@ -272,20 +267,25 @@ Error OS_IOS::shell_open(String p_uri) {
} }
String OS_IOS::get_user_data_dir() const { String OS_IOS::get_user_data_dir() const {
static bool user_data_dir_set = false; static String ret;
if (user_data_dir_set) { if (ret.is_empty()) {
String old_dir = user_data_dir; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
Ref<DirAccess> da = DirAccess::open(old_dir); if (paths && [paths count] >= 1) {
const_cast<OS_IOS *>(this)->user_data_dir = da->get_current_dir(); ret.parse_utf8([[paths firstObject] UTF8String]);
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());
} }
return user_data_dir; }
return ret;
} }
String OS_IOS::get_cache_path() const { 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 { String OS_IOS::get_locale() const {