Path.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package arm.sys;
  2. import iron.data.Data;
  3. class Path {
  4. #if krom_windows // No inline for plugin access
  5. public static var sep = "\\";
  6. #else
  7. public static var sep = "/";
  8. #end
  9. public static var meshFormats = ["obj", "fbx", "blend"];
  10. public static var textureFormats = ["jpg", "jpeg", "png", "tga", "bmp", "psd", "gif", "hdr", "k"];
  11. public static var meshImporters = new Map<String, String->(Dynamic->Void)->Void>();
  12. public static var textureImporters = new Map<String, String->(kha.Image->Void)->Void>();
  13. public static var baseColorExt = ["albedo", "alb", "basecol", "basecolor", "diffuse", "diff", "base", "bc", "d", "color", "col"];
  14. public static var opacityExt = ["opac", "opacity", "alpha"];
  15. public static var normalMapExt = ["normal", "nor", "n", "nrm", "normalgl"];
  16. public static var occlusionExt = ["ao", "occlusion", "ambientOcclusion", "o", "occ"];
  17. public static var roughnessExt = ["roughness", "rough", "r", "rgh"];
  18. public static var metallicExt = ["metallic", "metal", "metalness", "m", "met"];
  19. public static var displacementExt = ["displacement", "height", "h", "disp"];
  20. static var workingDirCache: String = null;
  21. public static function data(): String {
  22. return Krom.getFilesLocation() + Path.sep + Data.dataPath;
  23. }
  24. public static function toRelative(from: String, to: String): String {
  25. var a = from.split(Path.sep);
  26. var b = to.split(Path.sep);
  27. while (a[0] == b[0]) {
  28. a.shift();
  29. b.shift();
  30. if (a.length == 0 || b.length == 0) break;
  31. }
  32. var base = "";
  33. for (i in 0...a.length - 1) base += ".." + Path.sep;
  34. base += b.join(Path.sep);
  35. return base;
  36. }
  37. public static function normalize(path: String): String {
  38. var ar = path.split(Path.sep);
  39. var i = 0;
  40. while (i < ar.length) {
  41. if (i > 0 && ar[i] == ".." && ar[i - 1] != "..") {
  42. ar.splice(i - 1, 2);
  43. i--;
  44. }
  45. else i++;
  46. }
  47. return ar.join(Path.sep);
  48. }
  49. public static function baseDir(path: String): String {
  50. return path.substr(0, path.lastIndexOf(Path.sep) + 1);
  51. }
  52. public static function workingDir(): String {
  53. if (workingDirCache == null) {
  54. #if krom_windows
  55. var cmd = "cd";
  56. #else
  57. var cmd = "echo $PWD";
  58. #end
  59. var save = (Path.isProtected() ? Krom.savePath() : Path.data() + Path.sep) + "working_dir.txt";
  60. Krom.sysCommand(cmd + ' > "' + save + '"');
  61. workingDirCache = haxe.io.Bytes.ofData(Krom.loadBlob(save)).toString().rtrim();
  62. }
  63. return workingDirCache;
  64. }
  65. public static function isMesh(path: String): Bool {
  66. var p = path.toLowerCase();
  67. for (s in meshFormats) if (p.endsWith("." + s)) return true;
  68. return false;
  69. }
  70. public static function isTexture(path: String): Bool {
  71. var p = path.toLowerCase();
  72. for (s in textureFormats) if (p.endsWith("." + s)) return true;
  73. return false;
  74. }
  75. public static function isFont(path: String): Bool {
  76. var p = path.toLowerCase();
  77. return p.endsWith(".ttf") ||
  78. p.endsWith(".ttc") ||
  79. p.endsWith(".otf");
  80. }
  81. public static function isProject(path: String): Bool {
  82. var p = path.toLowerCase();
  83. return p.endsWith(".arm");
  84. }
  85. public static function isPlugin(path: String): Bool {
  86. var p = path.toLowerCase();
  87. return p.endsWith(".js");
  88. // p.endsWith(".wasm") ||
  89. // p.endsWith(".zip");
  90. }
  91. public static function isJson(path: String): Bool {
  92. var p = path.toLowerCase();
  93. return p.endsWith(".json");
  94. }
  95. public static function isText(path: String): Bool {
  96. var p = path.toLowerCase();
  97. return p.endsWith(".txt");
  98. }
  99. public static function isGimpColorPalette(path: String): Bool {
  100. var p = path.toLowerCase();
  101. return p.endsWith(".gpl");
  102. }
  103. public static function isKnown(path: String): Bool {
  104. return isMesh(path) || isTexture(path) || isFont(path) || isProject(path) || isPlugin(path) || isText(path) || isGimpColorPalette(path);
  105. }
  106. static function checkExt(p: String, exts: Array<String>): Bool {
  107. p = p.replace("-", "_");
  108. for (ext in exts) {
  109. if (p.endsWith("_" + ext) ||
  110. (p.indexOf("_" + ext + "_") >= 0 && !p.endsWith("_preview") && !p.endsWith("_icon"))) {
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. public static inline function isBaseColorTex(p: String): Bool {
  117. return checkExt(p, baseColorExt);
  118. }
  119. public static inline function isOpacityTex(p: String): Bool {
  120. return checkExt(p, opacityExt);
  121. }
  122. public static inline function isNormalMapTex(p: String): Bool {
  123. return checkExt(p, normalMapExt);
  124. }
  125. public static inline function isOcclusionTex(p: String): Bool {
  126. return checkExt(p, occlusionExt);
  127. }
  128. public static inline function isRoughnessTex(p: String): Bool {
  129. return checkExt(p, roughnessExt);
  130. }
  131. public static inline function isMetallicTex(p: String): Bool {
  132. return checkExt(p, metallicExt);
  133. }
  134. public static inline function isDisplacementTex(p: String): Bool {
  135. return checkExt(p, displacementExt);
  136. }
  137. public static function isFolder(p: String): Bool {
  138. return p.replace("\\", "/").split("/").pop().indexOf(".") == -1;
  139. }
  140. public static function isProtected(): Bool {
  141. #if krom_windows
  142. return Krom.getFilesLocation().indexOf("Program Files") >= 0;
  143. #elseif krom_android
  144. return true;
  145. #elseif krom_ios
  146. return true;
  147. #else
  148. return false;
  149. #end
  150. }
  151. }