diff --git a/project/Build.xml b/project/Build.xml index 63c662b1c..5ed66e691 100644 --- a/project/Build.xml +++ b/project/Build.xml @@ -229,6 +229,7 @@ + diff --git a/project/include/system/System.h b/project/include/system/System.h index 971e4109a..0027a5fac 100644 --- a/project/include/system/System.h +++ b/project/include/system/System.h @@ -3,6 +3,7 @@ #include #include +#include namespace lime { @@ -26,7 +27,10 @@ namespace lime { public: static bool GetAllowScreenTimeout (); - static const char* GetDirectory (SystemDirectory type, const char* company, const char* title); + static std::wstring* GetDirectory (SystemDirectory type, const char* company, const char* title); + #ifdef IPHONE + static std::wstring* GetIOSDirectory (SystemDirectory type); + #endif static value GetDisplay (int id); static int GetNumDisplays (); static double GetTimer (); diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index 06c25c72b..8d577ea09 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -1289,25 +1289,13 @@ namespace lime { value lime_system_get_directory (int type, HxString company, HxString title) { - const char* path = System::GetDirectory ((SystemDirectory)type, company.__s, title.__s); + std::wstring* path = System::GetDirectory ((SystemDirectory)type, company.__s, title.__s); if (path) { - value _path = alloc_string (path); - - if (type != 4) { - - // TODO: Make this more consistent - - //This free() causes crashes on mac at least. Commenting it out makes it work - //again but may cause a small memory leak. Some more consideration is - //necessary to figure out what to do here - - //free ((char*) path); - - } - - return _path; + value result = alloc_wstring (path->c_str ()); + delete path; + return result; } else { diff --git a/project/src/backend/sdl/SDLSystem.cpp b/project/src/backend/sdl/SDLSystem.cpp index 39c7cba3a..9ea0b2da5 100644 --- a/project/src/backend/sdl/SDLSystem.cpp +++ b/project/src/backend/sdl/SDLSystem.cpp @@ -88,39 +88,54 @@ namespace lime { } - const char* System::GetDirectory (SystemDirectory type, const char* company, const char* title) { + std::wstring* System::GetDirectory (SystemDirectory type, const char* company, const char* title) { switch (type) { - case APPLICATION: + case APPLICATION: { - return SDL_GetBasePath (); + std::string path = std::string (SDL_GetBasePath ()); + std::wstring* result = new std::wstring (path.begin (), path.end ()); + return result; break; + + } - case APPLICATION_STORAGE: + case APPLICATION_STORAGE: { - return SDL_GetPrefPath (company, title); + std::string path = std::string (SDL_GetPrefPath (company, title)); + std::wstring* result = new std::wstring (path.begin (), path.end ()); + return result; break; + + } case DESKTOP: { #if defined (HX_WINRT) Windows::Storage::StorageFolder folder = Windows::Storage::KnownFolders::HomeGroup; - std::wstring resultW (folder->Begin ()); - std::string result (resultW.begin (), resultW.end ()); - return result.c_str (); + std::wstring* result = new std::wstring (folder->Begin ()); + return result; #elif defined (HX_WINDOWS) - char result[MAX_PATH] = ""; - SHGetFolderPath (NULL, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, result); - return WIN_StringToUTF8 (result); + char folderPath[MAX_PATH] = ""; + SHGetFolderPath (NULL, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, folderPath); + WIN_StringToUTF8 (folderPath); + std::string path = std::string (folderPath); + std::wstring* result = new std::wstring (path.begin (), path.end ()); + return result; + + #elif defined (IPHONE) + + return System::GetIOSDirectory (type); #elif !defined (ANDROID) - std::string result = std::string (getenv ("HOME")) + std::string ("/Desktop"); - return result.c_str (); + std::string path = std::string (getenv ("HOME")) + std::string ("/Desktop"); + std::wstring* result = new std::wstring (path.begin (), path.end ()); + return result; #endif break; @@ -132,15 +147,21 @@ namespace lime { #if defined (HX_WINRT) Windows::Storage::StorageFolder folder = Windows::Storage::KnownFolders::DocumentsLibrary; - std::wstring resultW (folder->Begin ()); - std::string result (resultW.begin (), resultW.end ()); - return result.c_str (); + std::wstring* result = std::wstring (folder->Begin ()); + return result; #elif defined (HX_WINDOWS) - char result[MAX_PATH] = ""; - SHGetFolderPath (NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, result); - return WIN_StringToUTF8 (result); + char folderPath[MAX_PATH] = ""; + SHGetFolderPath (NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, folderPath); + WIN_StringToUTF8 (folderPath); + std::string path = std::string (folderPath); + std::wstring* result = new std::wstring (path.begin (), path.end ()); + return result; + + #elif defined (IPHONE) + + return System::GetIOSDirectory (type); #elif defined (ANDROID) @@ -148,8 +169,9 @@ namespace lime { #else - std::string result = std::string (getenv ("HOME")) + std::string ("/Documents"); - return result.c_str (); + std::string path = std::string (getenv ("HOME")) + std::string ("/Documents"); + std::wstring* result = new std::wstring (path.begin (), path.end ()); + return result; #endif break; @@ -164,9 +186,12 @@ namespace lime { #elif defined (HX_WINDOWS) - char result[MAX_PATH] = ""; - SHGetFolderPath (NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, result); - return WIN_StringToUTF8 (result); + char folderPath[MAX_PATH] = ""; + SHGetFolderPath (NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, folderPath); + WIN_StringToUTF8 (folderPath); + std::string path = std::string (folderPath); + std::wstring* result = new std::wstring (path.begin (), path.end ()); + return result; #elif defined (HX_MACOS) @@ -198,15 +223,21 @@ namespace lime { #if defined (HX_WINRT) Windows::Storage::StorageFolder folder = Windows::Storage::ApplicationData::Current->RoamingFolder; - std::wstring resultW (folder->Begin ()); - std::string result (resultW.begin (), resultW.end ()); - return result.c_str (); + std::wstring* result = new std::wstring (folder->Begin ()); + return result; #elif defined (HX_WINDOWS) - char result[MAX_PATH] = ""; - SHGetFolderPath (NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, result); - return WIN_StringToUTF8 (result); + char folderPath[MAX_PATH] = ""; + SHGetFolderPath (NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, folderPath); + WIN_StringToUTF8 (folderPath); + std::string path = std::string (folderPath); + std::wstring* result = new std::wstring (path.begin (), path.end ()); + return result; + + #elif defined (IPHONE) + + return System::GetIOSDirectory (type); #elif defined (ANDROID) @@ -215,7 +246,8 @@ namespace lime { #else std::string result = getenv ("HOME"); - return result.c_str (); + std::wstring* result = new std::wstring (path.begin (), path.end ()); + return result; #endif break; diff --git a/project/src/system/System.mm b/project/src/system/System.mm new file mode 100644 index 000000000..35e2ce257 --- /dev/null +++ b/project/src/system/System.mm @@ -0,0 +1,54 @@ +#ifdef IPHONE +#import +#endif + +#include + + +namespace lime { + + + std::wstring* System::GetIOSDirectory (SystemDirectory type) { + + #ifndef OBJC_ARC + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + #endif + + NSUInteger searchType = NSDocumentDirectory; + + switch (type) { + + case DESKTOP: + + searchType = NSDesktopDirectory; + break; + + case USER: + + searchType = NSUserDirectory; + break; + + } + + std::wstring* result = 0; + + NSArray* paths = NSSearchPathForDirectoriesInDomains (searchType, NSUserDomainMask, YES); + + if (paths && [paths count] > 0) { + + NSString* basePath = paths.firstObject; + std::string path = std::string ([basePath UTF8String]); + result = new std::wstring (path.begin (), path.end ()); + + } + + #ifndef OBJC_ARC + [pool drain]; + #endif + + return result; + + } + + +} \ No newline at end of file