BuildTools.hx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package hrt.impl;
  2. class BuildTools {
  3. static function log( str : String ) {
  4. #if hl
  5. Sys.println(str);
  6. #else
  7. trace(str);
  8. #end
  9. hxd.System.timeoutTick();
  10. }
  11. /**
  12. Build all files in `baseDir` directory (default: `res/`).
  13. */
  14. public static function buildAllFiles( ?baseDir : String, ?onProgress : (percent:Float, currentFile:String) -> Void, ?onError : String -> Void, ?onDone : (count:Int, errCount:Int) -> Void ) {
  15. log("[INFO] Start building all files " + Date.now());
  16. var baseDir = baseDir ?? "res/";
  17. function getPath(path : String) {
  18. return baseDir + path;
  19. }
  20. var onProgress = onProgress ?? function(percent, currentFile) {
  21. log('[PROGRESS] ($percent%) $currentFile');
  22. };
  23. var onError = onError ?? function(msg) {
  24. log('[INFO] $msg');
  25. };
  26. var onDone = onDone ?? function(count, errCount) {
  27. };
  28. var startTime = haxe.Timer.stamp();
  29. var lastTime = startTime;
  30. var all = [""];
  31. var errors = [];
  32. var done = 0;
  33. function loop() {
  34. while( true ) {
  35. if( all.length == 0 ) {
  36. onProgress(100, "");
  37. log("[INFO] Finished building " + done + " directory/files " + Date.now());
  38. if( errors.length > 0 ) {
  39. onError("Errors during Build Files:\n" + errors.join("\n"));
  40. }
  41. onDone(done, errors.length);
  42. return;
  43. }
  44. if( haxe.Timer.stamp() - lastTime > 0.1 ) {
  45. lastTime = haxe.Timer.stamp();
  46. onProgress(Std.int(done*1000/(done+all.length))/10, all[0]);
  47. haxe.Timer.delay(loop, 0);
  48. return;
  49. }
  50. var path = all.shift();
  51. var e = try hxd.res.Loader.currentInstance.load(path).entry catch( e ) {
  52. if( path != "" ) { // skip root error
  53. errors.push(e.message);
  54. }
  55. null;
  56. }
  57. if( e == null && path == "" ) e = hxd.res.Loader.currentInstance.fs.getRoot();
  58. if( e != null ) done++;
  59. if( e != null && e.isDirectory ) {
  60. var base = path;
  61. if( base != "" ) base += "/";
  62. for( f in sys.FileSystem.readDirectory(getPath(path)) ) {
  63. var path = base + f;
  64. if( path == ".tmp" ) continue;
  65. if( sys.FileSystem.isDirectory(getPath(path)) )
  66. all.unshift(path);
  67. else
  68. all.push(path);
  69. }
  70. }
  71. }
  72. }
  73. loop();
  74. }
  75. }