path.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. ///if arm_windows
  2. let path_sep: string = "\\";
  3. ///else
  4. let path_sep: string = "/";
  5. ///end
  6. ///if arm_windows
  7. let path_pwd: string = "cd";
  8. ///else
  9. let path_pwd: string = "echo $PWD";
  10. ///end
  11. let path_mesh_formats: string[] = ["obj", "blend"];
  12. let path_texture_formats: string[] = ["jpg", "jpeg", "png", "tga", "bmp", "psd", "gif", "hdr", "k"];
  13. let path_mesh_importers: map_t<string, any> = map_create(); // JSValue -> (s: string)=>raw_mesh_t
  14. let path_texture_importers: map_t<string, any> = map_create(); // JSValue -> (s: string)=>gpu_texture_t
  15. let path_base_color_ext: string[] = ["albedo", "alb", "basecol", "basecolor", "diffuse", "diff", "base", "bc", "d", "color", "col"];
  16. let path_opacity_ext: string[] = ["opac", "opacity", "alpha"];
  17. let path_normal_map_ext: string[] = ["normal", "nor", "n", "nrm", "normalgl"];
  18. let path_occlusion_ext: string[] = ["ao", "occlusion", "ambientOcclusion", "o", "occ"];
  19. let path_roughness_ext: string[] = ["roughness", "rough", "r", "rgh"];
  20. let path_metallic_ext: string[] = ["metallic", "metal", "metalness", "m", "met"];
  21. let path_displacement_ext: string[] = ["displacement", "height", "h", "disp"];
  22. let path_working_dir_cache: string = null;
  23. function path_data(): string {
  24. return iron_get_files_location() + path_sep + data_path();
  25. }
  26. function path_to_relative(from: string, to: string): string {
  27. let a: string[] = string_split(from, path_sep);
  28. let b: string[] = string_split(to, path_sep);
  29. while (a[0] == b[0]) {
  30. array_shift(a);
  31. array_shift(b);
  32. if (a.length == 0 || b.length == 0) {
  33. break;
  34. }
  35. }
  36. let p: string = "";
  37. for (let i: i32 = 0; i < a.length; ++i) {
  38. p += ".." + path_sep;
  39. }
  40. p += string_array_join(b, path_sep);
  41. return p;
  42. }
  43. function path_normalize(path: string): string {
  44. if (ends_with(path, path_sep)) {
  45. path = substring(path, 0, path.length - 1);
  46. }
  47. let ar: string[] = string_split(path, path_sep);
  48. let i: i32 = 0;
  49. while (i < ar.length) {
  50. if (i > 0 && ar[i] == ".." && ar[i - 1] != "..") {
  51. array_splice(ar, i - 1, 2);
  52. i--;
  53. }
  54. else {
  55. i++;
  56. }
  57. }
  58. return string_array_join(ar, path_sep);
  59. }
  60. function path_base_dir(path: string): string {
  61. return substring(path, 0, string_last_index_of(path, path_sep) + 1);
  62. }
  63. function path_base_name(path: string): string {
  64. return substring(path, string_last_index_of(path, path_sep) + 1, string_last_index_of(path, "."));
  65. }
  66. function path_working_dir(): string {
  67. if (path_working_dir_cache == null) {
  68. let cmd: string = path_pwd;
  69. let save: string;
  70. if (path_is_protected()) {
  71. save = iron_internal_save_path();
  72. }
  73. else {
  74. save = path_data() + path_sep;
  75. }
  76. save += "working_dir.txt";
  77. iron_sys_command(cmd + " > \"" + save + "\"");
  78. path_working_dir_cache = sys_buffer_to_string(iron_load_blob(save));
  79. path_working_dir_cache = trim_end(path_working_dir_cache);
  80. }
  81. return path_working_dir_cache;
  82. }
  83. function path_is_mesh(path: string): bool {
  84. let p: string = to_lower_case(path);
  85. for (let i: i32 = 0; i < path_mesh_formats.length; ++i) {
  86. let s: string = path_mesh_formats[i];
  87. if (ends_with(p, "." + s)) {
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. function path_is_texture(path: string): bool {
  94. let p: string = to_lower_case(path);
  95. for (let i: i32 = 0; i < path_texture_formats.length; ++i) {
  96. let s: string = path_texture_formats[i];
  97. if (ends_with(p, "." + s)) {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. function path_is_font(path: string): bool {
  104. let p: string = to_lower_case(path);
  105. return ends_with(p, ".ttf") ||
  106. ends_with(p, ".ttc") ||
  107. ends_with(p, ".otf");
  108. }
  109. function path_is_project(path: string): bool {
  110. let p: string = to_lower_case(path);
  111. return ends_with(p, ".arm");
  112. }
  113. function path_is_plugin(path: string): bool {
  114. let p: string = to_lower_case(path);
  115. return ends_with(p, ".js");
  116. }
  117. function path_is_json(path: string): bool {
  118. let p: string = to_lower_case(path);
  119. return ends_with(p, ".json");
  120. }
  121. function path_is_text(path: string): bool {
  122. let p: string = to_lower_case(path);
  123. return ends_with(p, ".txt");
  124. }
  125. function path_is_gimp_color_palette(path: string): bool {
  126. let p: string = to_lower_case(path);
  127. return ends_with(p, ".gpl");
  128. }
  129. function path_is_known(path: string): bool {
  130. return path_is_mesh(path) || path_is_texture(path) || path_is_font(path) || path_is_project(path) || path_is_plugin(path) || path_is_text(path) || path_is_gimp_color_palette(path);
  131. }
  132. function path_check_ext(p: string, exts: string[]): bool {
  133. p = string_replace_all(p, "-", "_");
  134. for (let i: i32 = 0; i < exts.length; ++i) {
  135. let ext: string = exts[i];
  136. if (ends_with(p, "_" + ext) ||
  137. (string_index_of(p, "_" + ext + "_") >= 0 && !ends_with(p, "_preview") && !ends_with(p, "_icon"))) {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. function path_is_base_color_tex(p: string): bool {
  144. return path_check_ext(p, path_base_color_ext);
  145. }
  146. function path_is_opacity_tex(p: string): bool {
  147. return path_check_ext(p, path_opacity_ext);
  148. }
  149. function path_is_normal_map_tex(p: string): bool {
  150. return path_check_ext(p, path_normal_map_ext);
  151. }
  152. function path_is_occlusion_tex(p: string): bool {
  153. return path_check_ext(p, path_occlusion_ext);
  154. }
  155. function path_is_roughness_tex(p: string): bool {
  156. return path_check_ext(p, path_roughness_ext);
  157. }
  158. function path_is_metallic_tex(p: string): bool {
  159. return path_check_ext(p, path_metallic_ext);
  160. }
  161. function path_is_displacement_tex(p: string): bool {
  162. return path_check_ext(p, path_displacement_ext);
  163. }
  164. function path_is_folder(p: string): bool {
  165. let last: string = array_pop(string_split(string_replace_all(p, "\\", "/"), "/"));
  166. return string_index_of(last, ".") == -1;
  167. }
  168. function path_is_protected(): bool {
  169. ///if arm_windows
  170. return string_index_of(iron_get_files_location(), "Program Files") >= 0;
  171. ///elseif arm_android
  172. return true;
  173. ///elseif arm_ios
  174. return true;
  175. ///else
  176. return false;
  177. ///end
  178. }