File.hx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package arm.sys;
  2. import haxe.io.Bytes;
  3. class File {
  4. #if krom_windows
  5. static inline var cmd_mkdir = "mkdir";
  6. static inline var cmd_copy = "copy";
  7. #else
  8. static inline var cmd_mkdir = "mkdir -p";
  9. static inline var cmd_copy = "cp";
  10. #end
  11. static var cloud: Map<String, Array<String>> = null;
  12. static var cloudSizes: Map<String, Int> = null;
  13. #if krom_android
  14. static var internal: Map<String, Array<String>> = null; // .apk contents
  15. #end
  16. public static function readDirectory(path: String, foldersOnly = false): Array<String> {
  17. if (path.startsWith("cloud")) {
  18. var files = cloud != null ? cloud.get(path.replace("\\", "/")) : null;
  19. return files != null ? files : [];
  20. }
  21. #if krom_android
  22. path = path.replace("//", "/");
  23. if (internal == null) {
  24. internal = [];
  25. internal.set("/data/plugins", BuildMacros.readDirectory("krom/data/plugins"));
  26. internal.set("/data/export_presets", BuildMacros.readDirectory("krom/data/export_presets"));
  27. internal.set("/data/keymap_presets", BuildMacros.readDirectory("krom/data/keymap_presets"));
  28. internal.set("/data/locale", BuildMacros.readDirectory("krom/data/locale"));
  29. internal.set("/data/meshes", BuildMacros.readDirectory("krom/data/meshes"));
  30. internal.set("/data/themes", BuildMacros.readDirectory("krom/data/themes"));
  31. }
  32. if (internal.exists(path)) return internal.get(path);
  33. #end
  34. return Krom.readDirectory(path, foldersOnly).split("\n");
  35. }
  36. public static function createDirectory(path: String) {
  37. Krom.sysCommand(cmd_mkdir + ' "' + path + '"');
  38. }
  39. public static function copy(srcPath: String, dstPath: String) {
  40. Krom.sysCommand(cmd_copy + ' "' + srcPath + '" "' + dstPath + '"');
  41. }
  42. public static function start(path: String) {
  43. #if krom_windows
  44. Krom.sysCommand('start "" "' + path + '"');
  45. #elseif krom_linux
  46. Krom.sysCommand('xdg-open "' + path + '"');
  47. #else
  48. Krom.sysCommand('open "' + path + '"');
  49. #end
  50. }
  51. public static function loadUrl(url: String) {
  52. Krom.loadUrl(url);
  53. }
  54. public static function delete(path: String) {
  55. Krom.deleteFile(path);
  56. }
  57. public static function exists(path: String): Bool {
  58. return Krom.fileExists(path);
  59. }
  60. public static function download(url: String, dstPath: String, done: Void->Void, size = 0) {
  61. #if (krom_windows || krom_darwin || krom_ios || krom_android)
  62. Krom.httpRequest(url, size, function(ab: js.lib.ArrayBuffer) {
  63. if (ab != null) Krom.fileSaveBytes(dstPath, ab);
  64. done();
  65. });
  66. #elseif krom_linux
  67. Krom.sysCommand('wget -O "' + dstPath + '" "' + url + '"');
  68. done();
  69. #else
  70. Krom.sysCommand('curl -L ' + url + ' -o "' + dstPath + '"');
  71. done();
  72. #end
  73. }
  74. public static function downloadBytes(url: String, done: Bytes->Void) {
  75. var save = (Path.isProtected() ? Krom.savePath() : Path.data() + Path.sep) + "download.bin";
  76. File.download(url, save, function() {
  77. var bytes: Bytes = null;
  78. try {
  79. bytes = Bytes.ofData(Krom.loadBlob(save));
  80. }
  81. catch (e: Dynamic) {}
  82. done(bytes);
  83. });
  84. }
  85. public static function cacheCloud(path: String, done: String->Void) {
  86. #if krom_ios
  87. var path2 = path.replace("/", "_"); // Cache everything into root folder
  88. #else
  89. var path2 = path;
  90. #end
  91. var dest = (Path.isProtected() ? Krom.savePath() : Krom.getFilesLocation() + Path.sep) + path2;
  92. if (File.exists(dest)) {
  93. #if (krom_darwin || krom_ios)
  94. done(dest);
  95. #else
  96. done((Path.isProtected() ? Krom.savePath() : Path.workingDir() + Path.sep) + path);
  97. #end
  98. return;
  99. }
  100. var fileDir = dest.substr(0, dest.lastIndexOf(Path.sep));
  101. if (File.readDirectory(fileDir)[0] == "") {
  102. File.createDirectory(fileDir);
  103. }
  104. #if krom_windows
  105. path = path.replace("\\", "/");
  106. #end
  107. var url = Config.raw.server + "/" + path;
  108. File.download(url, dest, function() {
  109. if (!File.exists(dest)) {
  110. Console.error(Strings.error5());
  111. done(null);
  112. return;
  113. }
  114. #if (krom_darwin || krom_ios)
  115. done(dest);
  116. #else
  117. done((Path.isProtected() ? Krom.savePath() : Path.workingDir() + Path.sep) + path);
  118. #end
  119. }, cloudSizes.get(path));
  120. }
  121. static function initCloudBytes(done: Void->Void, append = "") {
  122. File.downloadBytes(Config.raw.server + "/?list-type=2" + append, function(bytes: Bytes) {
  123. if (bytes == null) {
  124. cloud.set("cloud", []);
  125. Console.error(Strings.error5());
  126. return;
  127. }
  128. var files: Array<String> = [];
  129. var sizes: Array<Int> = [];
  130. var xml = Xml.parse(bytes.toString());
  131. for (e in xml.firstElement().elementsNamed("Contents")) {
  132. for (k in e.elementsNamed("Key")) {
  133. files.push(k.firstChild().nodeValue);
  134. }
  135. for (k in e.elementsNamed("Size")) {
  136. sizes.push(Std.parseInt(k.firstChild().nodeValue));
  137. }
  138. }
  139. for (file in files) {
  140. if (Path.isFolder(file)) {
  141. cloud.set(file.substr(0, file.length - 1), []);
  142. }
  143. }
  144. for (i in 0...files.length) {
  145. var file = files[i];
  146. var nested = file.indexOf("/") != file.lastIndexOf("/");
  147. if (nested) {
  148. var delim = Path.isFolder(file) ? file.substr(0, file.length - 1).lastIndexOf("/") : file.lastIndexOf("/");
  149. var parent = file.substr(0, delim);
  150. var child = Path.isFolder(file) ? file.substring(delim + 1, file.length - 1) : file.substr(delim + 1);
  151. cloud.get(parent).push(child);
  152. if (!Path.isFolder(file)) {
  153. cloudSizes.set(file, sizes[i]);
  154. }
  155. }
  156. }
  157. var isTruncated = xml.firstElement().elementsNamed("IsTruncated").next().firstChild().nodeValue == "true";
  158. if (isTruncated) {
  159. initCloudBytes(done, "&start-after=" + xml.firstElement().elementsNamed("NextContinuationToken").next().firstChild().nodeValue);
  160. }
  161. else done();
  162. });
  163. }
  164. static function initCloud(done: Void->Void) {
  165. cloud = [];
  166. cloudSizes = [];
  167. initCloudBytes(done);
  168. }
  169. }