path.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ///if krom_windows
  2. let path_sep: string = "\\";
  3. ///else
  4. let path_sep: string = "/";
  5. ///end
  6. ///if krom_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, (s: string)=>any> = map_create();
  14. let path_texture_importers: map_t<string, (s: string)=>image_t> = map_create();
  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 krom_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. a.shift();
  31. b.shift();
  32. if (a.length == 0 || b.length == 0) {
  33. break;
  34. }
  35. }
  36. let base: string = "";
  37. for (let i: i32 = 0; i < a.length - 1; ++i) {
  38. base += ".." + path_sep;
  39. }
  40. base += b.join(path_sep);
  41. return base;
  42. }
  43. function path_normalize(path: string): string {
  44. let ar: string[] = string_split(path, path_sep);
  45. let i: i32 = 0;
  46. while (i < ar.length) {
  47. if (i > 0 && ar[i] == ".." && ar[i - 1] != "..") {
  48. array_splice(ar, i - 1, 2);
  49. i--;
  50. }
  51. else {
  52. i++;
  53. }
  54. }
  55. return ar.join(path_sep);
  56. }
  57. function path_base_dir(path: string): string {
  58. return substring(path, 0, string_last_index_of(path, path_sep) + 1);
  59. }
  60. function path_working_dir(): string {
  61. if (path_working_dir_cache == null) {
  62. let cmd: string = path_pwd;
  63. let save: string = (path_is_protected() ? krom_save_path() : path_data() + path_sep) + "working_dir.txt";
  64. krom_sys_command(cmd + " > \"" + save + "\"");
  65. path_working_dir_cache = trim_end(sys_buffer_to_string(krom_load_blob(save)));
  66. }
  67. return path_working_dir_cache;
  68. }
  69. function path_is_mesh(path: string): bool {
  70. let p: string = to_lower_case(path);
  71. for (let i: i32 = 0; i < path_mesh_formats.length; ++i) {
  72. let s: string = path_mesh_formats[i];
  73. if (ends_with(p, "." + s)) {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. function path_is_texture(path: string): bool {
  80. let p: string = to_lower_case(path);
  81. for (let i: i32 = 0; i < path_texture_formats.length; ++i) {
  82. let s: string = path_texture_formats[i];
  83. if (ends_with(p, "." + s)) {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. function path_is_font(path: string): bool {
  90. let p: string = to_lower_case(path);
  91. return ends_with(p, ".ttf") ||
  92. ends_with(p, ".ttc") ||
  93. ends_with(p, ".otf");
  94. }
  95. function path_is_project(path: string): bool {
  96. let p: string = to_lower_case(path);
  97. return ends_with(p, ".arm");
  98. }
  99. function path_is_plugin(path: string): bool {
  100. let p: string = to_lower_case(path);
  101. return ends_with(p, ".js");
  102. }
  103. function path_is_json(path: string): bool {
  104. let p: string = to_lower_case(path);
  105. return ends_with(p, ".json");
  106. }
  107. function path_is_text(path: string): bool {
  108. let p: string = to_lower_case(path);
  109. return ends_with(p, ".txt");
  110. }
  111. function path_is_gimp_color_palette(path: string): bool {
  112. let p: string = to_lower_case(path);
  113. return ends_with(p, ".gpl");
  114. }
  115. function path_is_known(path: string): bool {
  116. 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);
  117. }
  118. function path_check_ext(p: string, exts: string[]): bool {
  119. p = string_replace_all(p, "-", "_");
  120. for (let i: i32 = 0; i < exts.length; ++i) {
  121. let ext: string = exts[i];
  122. if (ends_with(p, "_" + ext) ||
  123. (string_index_of(p, "_" + ext + "_") >= 0 && !ends_with(p, "_preview") && !ends_with(p, "_icon"))) {
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. function path_is_base_color_tex(p: string): bool {
  130. return path_check_ext(p, path_base_color_ext);
  131. }
  132. function path_is_opacity_tex(p: string): bool {
  133. return path_check_ext(p, path_opacity_ext);
  134. }
  135. function path_is_normal_map_tex(p: string): bool {
  136. return path_check_ext(p, path_normal_map_ext);
  137. }
  138. function path_is_occlusion_tex(p: string): bool {
  139. return path_check_ext(p, path_occlusion_ext);
  140. }
  141. function path_is_roughness_tex(p: string): bool {
  142. return path_check_ext(p, path_roughness_ext);
  143. }
  144. function path_is_metallic_tex(p: string): bool {
  145. return path_check_ext(p, path_metallic_ext);
  146. }
  147. function path_is_displacement_tex(p: string): bool {
  148. return path_check_ext(p, path_displacement_ext);
  149. }
  150. function path_is_folder(p: string): bool {
  151. return string_index_of(string_split(string_replace_all(p, "\\", "/"), "/").pop(), ".") == -1;
  152. }
  153. function path_is_protected(): bool {
  154. ///if krom_windows
  155. return string_index_of(krom_get_files_location(), "Program Files") >= 0;
  156. ///elseif krom_android
  157. return true;
  158. ///elseif krom_ios
  159. return true;
  160. ///else
  161. return false;
  162. ///end
  163. }