file.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. ///if krom_windows
  2. let file_cmd_mkdir: string = "mkdir";
  3. let file_cmd_copy: string = "copy";
  4. ///else
  5. let file_cmd_mkdir: string = "mkdir -p";
  6. let file_cmd_copy: string = "cp";
  7. ///end
  8. let file_cloud: map_t<string, string[]> = null;
  9. let file_cloud_sizes: map_t<string, i32> = null;
  10. // ///if krom_android
  11. // let let file_internal: map_t<string, string[]> = null; // .apk contents
  12. // ///end
  13. function file_read_directory(path: string, folders_only: bool = false): string[] {
  14. if (starts_with(path, "cloud")) {
  15. let files: string[] = file_cloud != null ? map_get(file_cloud, string_replace_all(path, "\\", "/")) : null;
  16. return files != null ? files : [];
  17. }
  18. // ///if krom_android
  19. // path = string_replace_all(path, "//", "/");
  20. // if (file_internal == null) {
  21. // file_internal = [];
  22. // map_set(file_internal, "/data/plugins", BuildMacros.readDirectory("krom/data/plugins"));
  23. // map_set(file_internal, "/data/export_presets", BuildMacros.readDirectory("krom/data/export_presets"));
  24. // map_set(file_internal, "/data/keymap_presets", BuildMacros.readDirectory("krom/data/keymap_presets"));
  25. // map_set(file_internal, "/data/locale", BuildMacros.readDirectory("krom/data/locale"));
  26. // map_set(file_internal, "/data/meshes", BuildMacros.readDirectory("krom/data/meshes"));
  27. // map_set(file_internal, "/data/themes", BuildMacros.readDirectory("krom/data/themes"));
  28. // }
  29. // if (file_internal.exists(path)) return map_get(file_internal, path);
  30. // ///end
  31. return string_split(krom_read_directory(path, folders_only), "\n");
  32. }
  33. function file_create_directory(path: string) {
  34. krom_sys_command(file_cmd_mkdir + " \"" + path + "\"");
  35. }
  36. function file_copy(srcPath: string, dst_path: string) {
  37. krom_sys_command(file_cmd_copy + " \"" + srcPath + "\" \"" + dst_path + "\"");
  38. }
  39. function file_start(path: string) {
  40. ///if krom_windows
  41. krom_sys_command("start \"\" \"" + path + "\"");
  42. ///elseif krom_linux
  43. krom_sys_command("xdg-open \"" + path + "\"");
  44. ///else
  45. krom_sys_command("open \"" + path + "\"");
  46. ///end
  47. }
  48. function file_load_url(url: string) {
  49. krom_load_url(url);
  50. }
  51. function file_delete(path: string) {
  52. krom_delete_file(path);
  53. }
  54. function file_exists(path: string): bool {
  55. return krom_file_exists(path);
  56. }
  57. type file_download_data_t = {
  58. dst_path: string;
  59. done: (url: string)=>void;
  60. };
  61. let _file_download_map: map_t<string, file_download_data_t> = map_create();
  62. function file_download(url: string, dst_path: string, done: (url: string)=>void, size: i32 = 0) {
  63. ///if (krom_windows || krom_darwin || krom_ios || krom_android)
  64. let fdd: file_download_data_t = { dst_path: dst_path, done: done };
  65. map_set(_file_download_map, url, fdd);
  66. krom_http_request(url, size, function (url: string, ab: buffer_t) {
  67. let fdd: file_download_data_t = map_get(_file_download_map, url);
  68. if (ab != null) {
  69. krom_file_save_bytes(fdd.dst_path, ab);
  70. }
  71. fdd.done(url);
  72. });
  73. ///elseif krom_linux
  74. krom_sys_command("wget -O \"" + dst_path + "\" \"" + url + "\"");
  75. done(url);
  76. ///else
  77. krom_sys_command("curl -L " + url + " -o \"" + dst_path + "\"");
  78. done(url);
  79. ///end
  80. }
  81. type file_download_bytes_data_t = {
  82. url: string;
  83. save: string;
  84. done: (url: string, ab: buffer_t)=>void;
  85. };
  86. let _file_download_bytes_map: map_t<string, file_download_bytes_data_t> = map_create();
  87. function file_download_bytes(url: string, done: (url: string, ab: buffer_t)=>void) {
  88. let save: string = (path_is_protected() ? krom_save_path() : path_data() + path_sep) + "download.bin";
  89. let fdbd: file_download_bytes_data_t = { url: url, save: save, done: done };
  90. map_set(_file_download_bytes_map, url, fdbd);
  91. file_download(url, save, function (url: string) {
  92. let fdbd: file_download_bytes_data_t = map_get(_file_download_bytes_map, url);
  93. let buffer: buffer_t = krom_load_blob(fdbd.save);
  94. fdbd.done(fdbd.url, buffer);
  95. });
  96. }
  97. type file_cache_cloud_data_t = {
  98. dest: string;
  99. path: string;
  100. done: (dest: string)=>void;
  101. };
  102. let _file_cache_cloud_map: map_t<string, file_cache_cloud_data_t> = map_create();
  103. function file_cache_cloud(path: string, done: (s: string)=>void) {
  104. ///if krom_ios
  105. let path2: string = string_replace_all(path, "/", "_"); // Cache everything into root folder
  106. ///else
  107. let path2: string = path;
  108. ///end
  109. let dest: string = (path_is_protected() ? krom_save_path() : krom_get_files_location() + path_sep) + path2;
  110. if (file_exists(dest)) {
  111. ///if (krom_darwin || krom_ios)
  112. done(dest);
  113. ///else
  114. done((path_is_protected() ? krom_save_path() : path_working_dir() + path_sep) + path);
  115. ///end
  116. return;
  117. }
  118. let file_dir: string = substring(dest, 0, string_last_index_of(dest, path_sep));
  119. if (file_read_directory(file_dir)[0] == "") {
  120. file_create_directory(file_dir);
  121. }
  122. ///if krom_windows
  123. path = string_replace_all(path, "\\", "/");
  124. ///end
  125. let url: string = config_raw.server + "/" + path;
  126. let fccd: file_cache_cloud_data_t = { dest: dest, path: path, done: done };
  127. map_set(_file_cache_cloud_map, url, fccd);
  128. file_download(url, dest, function (url: string) {
  129. let fccd: file_cache_cloud_data_t = map_get(_file_cache_cloud_map, url);
  130. if (!file_exists(dest)) {
  131. console_error(strings_error5());
  132. fccd.done(null);
  133. return;
  134. }
  135. ///if (krom_darwin || krom_ios)
  136. fccd.done(fccd.dest);
  137. ///else
  138. fccd.done((path_is_protected() ? krom_save_path() : path_working_dir() + path_sep) + fccd.path);
  139. ///end
  140. }, map_get(file_cloud_sizes, path));
  141. }
  142. let _file_init_cloud_bytes_done: ()=>void;
  143. function file_init_cloud_bytes(done: ()=>void, append: string = "") {
  144. _file_init_cloud_bytes_done = done;
  145. file_download_bytes(config_raw.server + "/?list-type=2" + append, function (url: string, buffer: buffer_t) {
  146. if (buffer == null) {
  147. map_set(file_cloud, "cloud", []);
  148. console_error(strings_error5());
  149. return;
  150. }
  151. let files: string[] = [];
  152. let sizes: i32[] = [];
  153. let str: string = sys_buffer_to_string(buffer);
  154. let pos_start: i32 = 0;
  155. let pos_end: i32 = 0;
  156. while (true) {
  157. pos_start = string_index_of_pos(str, "<Key>", pos_start);
  158. if (pos_start == -1) {
  159. break;
  160. }
  161. pos_start += 5; // <Key>
  162. pos_end = string_index_of_pos(str, "</Key>", pos_start);
  163. array_push(files, substring(str, pos_start, pos_end));
  164. pos_start = string_index_of_pos(str, "<Size>", pos_end);
  165. pos_start += 6; //<Size>
  166. pos_end = string_index_of_pos(str, "</Size>", pos_start);
  167. array_push(sizes, Number(substring(str, pos_start, pos_end)));
  168. }
  169. for (let i: i32 = 0; i < files.length; ++i) {
  170. let file: string = files[i];
  171. if (path_is_folder(file)) {
  172. map_set(file_cloud, substring(file, 0, file.length - 1), []);
  173. }
  174. }
  175. for (let i: i32 = 0; i < files.length; ++i) {
  176. let file: string = files[i];
  177. let nested: bool = string_index_of(file, "/") != string_last_index_of(file, "/");
  178. if (nested) {
  179. let delim: i32 = path_is_folder(file) ? string_last_index_of(substring(file, 0, file.length - 1), "/") : string_last_index_of(file, "/");
  180. let parent: string = substring(file, 0, delim);
  181. let child: string = path_is_folder(file) ? substring(file, delim + 1, file.length - 1) : substring(file, delim + 1, file.length);
  182. array_push(map_get(file_cloud, parent), child);
  183. if (!path_is_folder(file)) {
  184. map_set(file_cloud_sizes, file, sizes[i]);
  185. }
  186. }
  187. }
  188. let is_truncated: bool = string_index_of(str, "<IsTruncated>true") > -1;
  189. if (is_truncated) {
  190. let pos_start: i32 = string_index_of(str, "<NextContinuationToken>");
  191. pos_start += 23;
  192. let pos_end: i32 = string_index_of_pos(str, "</NextContinuationToken>", pos_start);
  193. file_init_cloud_bytes(_file_init_cloud_bytes_done, "&start-after=" + substring(str, pos_start, pos_end));
  194. }
  195. else {
  196. _file_init_cloud_bytes_done();
  197. }
  198. });
  199. }
  200. function file_init_cloud(done: ()=>void) {
  201. file_cloud = map_create();
  202. file_cloud_sizes = map_create();
  203. file_init_cloud_bytes(done);
  204. }