Path.hx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package arm.sys;
  2. import iron.data.Data;
  3. using StringTools;
  4. class Path {
  5. #if krom_windows // no inline for plugin access
  6. public static var sep = "\\";
  7. #else
  8. public static var sep = "/";
  9. #end
  10. public static var meshFormats = ["obj", "fbx", "blend"];
  11. public static var textureFormats = ["jpg", "jpeg", "png", "tga", "bmp", "psd", "gif", "hdr"];
  12. public static var meshImporters = new Map<String, String->(Dynamic->Void)->Void>();
  13. public static var textureImporters = new Map<String, String->(kha.Image->Void)->Void>();
  14. public static function data(): String {
  15. #if krom_windows
  16. var path = Data.dataPath.replace("/", "\\");
  17. #else
  18. var path = Data.dataPath;
  19. #end
  20. return Krom.getFilesLocation() + Path.sep + path;
  21. }
  22. public static function toRelative(from: String, to: String): String {
  23. from = haxe.io.Path.normalize(from);
  24. to = haxe.io.Path.normalize(to);
  25. var a = from.split("/");
  26. var b = to.split("/");
  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 += "../";
  34. base += b.join("/");
  35. return haxe.io.Path.normalize(base);
  36. }
  37. public static function baseDir(path: String): String {
  38. path = haxe.io.Path.normalize(path);
  39. var base = path.substr(0, path.lastIndexOf("/") + 1);
  40. #if krom_windows
  41. base = base.substr(0, 2) + "\\" + base.substr(3);
  42. #end
  43. return base;
  44. }
  45. public static function workingDir(): String {
  46. #if krom_windows
  47. var cmd = "cd";
  48. #else
  49. var cmd = "echo $PWD";
  50. #end
  51. var save = data() + sep + "tmp.txt";
  52. Krom.sysCommand(cmd + ' > "' + save + '"');
  53. return haxe.io.Bytes.ofData(Krom.loadBlob(save)).toString().rtrim();
  54. }
  55. public static function isMesh(path: String): Bool {
  56. var p = path.toLowerCase();
  57. for (s in meshFormats) if (p.endsWith("." + s)) return true;
  58. return false;
  59. }
  60. public static function isTexture(path: String): Bool {
  61. var p = path.toLowerCase();
  62. for (s in textureFormats) if (p.endsWith("." + s)) return true;
  63. return false;
  64. }
  65. public static function isFont(path: String): Bool {
  66. var p = path.toLowerCase();
  67. return p.endsWith(".ttf");
  68. }
  69. public static function isProject(path: String): Bool {
  70. var p = path.toLowerCase();
  71. return p.endsWith(".arm");
  72. }
  73. public static function isPlugin(path: String): Bool {
  74. var p = path.toLowerCase();
  75. return p.endsWith(".js");
  76. // p.endsWith(".wasm") ||
  77. // p.endsWith(".zip");
  78. }
  79. public static function isBaseTex(p: String): Bool {
  80. return p.endsWith("_albedo") ||
  81. p.endsWith("_alb") ||
  82. p.endsWith("_basecol") ||
  83. p.endsWith("_basecolor") ||
  84. p.endsWith("_diffuse") ||
  85. p.endsWith("_diff") ||
  86. p.endsWith("_base") ||
  87. p.endsWith("_bc") ||
  88. p.endsWith("_d") ||
  89. p.endsWith("_color") ||
  90. p.endsWith("_col");
  91. }
  92. public static function isOpacTex(p: String): Bool {
  93. return p.endsWith("_opac") ||
  94. p.endsWith("_alpha") ||
  95. p.endsWith("_opacity");
  96. }
  97. public static function isNorTex(p: String): Bool {
  98. return p.endsWith("_normal") ||
  99. p.endsWith("_nor") ||
  100. p.endsWith("_n") ||
  101. p.endsWith("_nrm");
  102. }
  103. public static function isOccTex(p: String): Bool {
  104. return p.endsWith("_ao") ||
  105. p.endsWith("_occlusion") ||
  106. p.endsWith("_ambientOcclusion") ||
  107. p.endsWith("_o") ||
  108. p.endsWith("_occ");
  109. }
  110. public static function isRoughTex(p: String): Bool {
  111. return p.endsWith("_roughness") ||
  112. p.endsWith("_roug") ||
  113. p.endsWith("_r") ||
  114. p.endsWith("_rough") ||
  115. p.endsWith("_rgh");
  116. }
  117. public static function isMetTex(p: String): Bool {
  118. return p.endsWith("_metallic") ||
  119. p.endsWith("_metal") ||
  120. p.endsWith("_metalness") ||
  121. p.endsWith("_m") ||
  122. p.endsWith("_met");
  123. }
  124. public static function isDispTex(p: String): Bool {
  125. return p.endsWith("_displacement") ||
  126. p.endsWith("_height") ||
  127. p.endsWith("_h") ||
  128. p.endsWith("_disp");
  129. }
  130. public static function isFolder(p: String): Bool {
  131. return p.indexOf(".") == -1;
  132. }
  133. #if krom_windows
  134. public static function isAscii(s: String): Bool {
  135. for (i in 0...s.length) if (s.charCodeAt(i) > 127) return false;
  136. return true;
  137. }
  138. public static function shortPath(s: String): String {
  139. var cmd = 'for %I in ("' + s + '") do echo %~sI';
  140. var save = data() + sep + "tmp.txt";
  141. Krom.sysCommand(cmd + ' > "' + save + '"');
  142. return haxe.io.Bytes.ofData(Krom.loadBlob(save)).toString().rtrim();
  143. }
  144. #end
  145. }