path.ts 5.1 KB

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