From 5fe4f6fc05c84b6f06a8482b06678ea2ca1838c4 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Fri, 2 Sep 2016 21:43:31 -0700 Subject: [PATCH] Make system directories more consistent --- lime/system/System.hx | 190 ++++++++++++++++++++---------------------- 1 file changed, 91 insertions(+), 99 deletions(-) diff --git a/lime/system/System.hx b/lime/system/System.hx index e910edfff..cad09598f 100644 --- a/lime/system/System.hx +++ b/lime/system/System.hx @@ -44,6 +44,8 @@ class System { public static var numDisplays (get, null):Int; public static var userDirectory (get, null):String; + @:noCompletion private static var __directories = new Map (); + #if (js && html5) @:keep @:expose("lime.embed") @@ -240,6 +242,89 @@ class System { } + private static function __getDirectory (type:SystemDirectory):String { + + #if (lime_cffi && !macro) + + if (__directories.exists (type)) { + + return __directories.get (type); + + } else { + + var path:String; + + if (type == APPLICATION_STORAGE) { + + var company = "MyCompany"; + var file = "MyApplication"; + + if (Application.current != null && Application.current.config != null) { + + if (Application.current.config.company != null) { + + company = Application.current.config.company; + + } + + if (Application.current.config.file != null) { + + file = Application.current.config.file; + + } + + } + + path = lime_system_get_directory (type, company, file); + + } else { + + path = lime_system_get_directory (type, null, null); + + } + + #if windows + var seperator = "\\"; + #else + var seperator = "/"; + #end + + if (path != null && path.length > 0 && !StringTools.endsWith (path, seperator)) { + + path += seperator; + + } + + __directories.set (type, path); + return path; + + } + + #elseif flash + + if (type != FONTS && Capabilities.playerType == "Desktop") { + + var propertyName = switch (type) { + + case APPLICATION: "applicationDirectory"; + case APPLICATION_STORAGE: "applicationStorageDirectory"; + case DESKTOP: "desktopDirectory"; + case DOCUMENTS: "documentsDirectory"; + default: "userDirectory"; + + } + + return Reflect.getProperty (Type.resolveClass ("flash.filesystem.File"), propertyName).nativePath; + + } + + #end + + return null; + + } + + // Get & Set Methods @@ -271,114 +356,35 @@ class System { private static function get_applicationDirectory ():String { - #if (lime_cffi && !macro) - return lime_system_get_directory (SystemDirectory.APPLICATION, null, null); - #elseif flash - if (Capabilities.playerType == "Desktop") { - - return Reflect.getProperty (Type.resolveClass ("flash.filesystem.File"), "applicationDirectory").nativePath; - - } else { - - return null; - - } - #else - return null; - #end + return __getDirectory (APPLICATION); } private static function get_applicationStorageDirectory ():String { - var company = "MyCompany"; - var file = "MyApplication"; - - if (Application.current != null && Application.current.config != null) { - - if (Application.current.config.company != null) { - - company = Application.current.config.company; - - } - - if (Application.current.config.file != null) { - - file = Application.current.config.file; - - } - - } - - #if (lime_cffi && !macro) - return lime_system_get_directory (SystemDirectory.APPLICATION_STORAGE, company, file); - #elseif flash - if (Capabilities.playerType == "Desktop") { - - return Reflect.getProperty (Type.resolveClass ("flash.filesystem.File"), "applicationStorageDirectory").nativePath; - - } else { - - return null; - - } - #else - return null; - #end + return __getDirectory (APPLICATION_STORAGE); } private static function get_desktopDirectory ():String { - #if (lime_cffi && !macro) - return lime_system_get_directory (SystemDirectory.DESKTOP, null, null); - #elseif flash - if (Capabilities.playerType == "Desktop") { - - return Reflect.getProperty (Type.resolveClass ("flash.filesystem.File"), "desktopDirectory").nativePath; - - } else { - - return null; - - } - #else - return null; - #end + return __getDirectory (DESKTOP); } private static function get_documentsDirectory ():String { - #if (lime_cffi && !macro) - return lime_system_get_directory (SystemDirectory.DOCUMENTS, null, null); - #elseif flash - if (Capabilities.playerType == "Desktop") { - - return Reflect.getProperty (Type.resolveClass ("flash.filesystem.File"), "documentsDirectory").nativePath; - - } else { - - return null; - - } - #else - return null; - #end + return __getDirectory (DOCUMENTS); } private static function get_fontsDirectory ():String { - #if (lime_cffi && !macro) - return lime_system_get_directory (SystemDirectory.FONTS, null, null); - #else - return null; - #end + return __getDirectory (FONTS); } @@ -396,21 +402,7 @@ class System { private static function get_userDirectory ():String { - #if (lime_cffi && !macro) - return lime_system_get_directory (SystemDirectory.USER, null, null); - #elseif flash - if (Capabilities.playerType == "Desktop") { - - return Reflect.getProperty (Type.resolveClass ("flash.filesystem.File"), "userDirectory").nativePath; - - } else { - - return null; - - } - #else - return null; - #end + return __getDirectory (USER); }