zipTool.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * This JS file contains the ZIP tools
  3. */
  4. class ZipTool {
  5. constructor(parent) {
  6. this.parent = parent;
  7. this.zipCode;
  8. }
  9. set ZipCode(value) {
  10. this.zipCode = value;
  11. }
  12. addContentToZip (zip, name, url, replace, buffer, then) {
  13. if (url.substring(0, 5) == "data:" || url.substring(0, 5) == "http:" || url.substring(0, 5) == "blob:" || url.substring(0, 6) == "https:") {
  14. then();
  15. return;
  16. }
  17. var xhr = new XMLHttpRequest();
  18. xhr.open('GET', url, true);
  19. if (buffer) {
  20. xhr.responseType = "arraybuffer";
  21. }
  22. xhr.onreadystatechange = function () {
  23. if (xhr.readyState === 4) {
  24. if (xhr.status === 200) {
  25. var text;
  26. if (!buffer) {
  27. if (replace) {
  28. var splits = replace.split("\r\n");
  29. for (var index = 0; index < splits.length; index++) {
  30. splits[index] = " " + splits[index];
  31. }
  32. replace = splits.join("\r\n");
  33. text = xhr.responseText.replace("####INJECT####", replace);
  34. } else {
  35. text = xhr.responseText;
  36. }
  37. }
  38. zip.file(name, buffer ? xhr.response : text);
  39. then();
  40. }
  41. }
  42. };
  43. xhr.send(null);
  44. };
  45. addTexturesToZip (zip, index, textures, folder, then) {
  46. if (index === textures.length || !textures[index].name) {
  47. then();
  48. return;
  49. }
  50. if (textures[index].isRenderTarget || textures[index] instanceof BABYLON.DynamicTexture || textures[index].name.indexOf("data:") !== -1) {
  51. this.addTexturesToZip(zip, index + 1, textures, folder, then);
  52. return;
  53. }
  54. if (textures[index].isCube) {
  55. if (textures[index].name.indexOf("dds") === -1 && textures[index].name.indexOf(".env") === -1) {
  56. if (textures[index]._extensions) {
  57. for (var i = 0; i < 6; i++) {
  58. textures.push({ name: textures[index].name + textures[index]._extensions[i] });
  59. }
  60. } else if (textures[index]._files) {
  61. for (var i = 0; i < 6; i++) {
  62. textures.push({ name: textures[index]._files[i] });
  63. }
  64. }
  65. }
  66. else {
  67. textures.push({ name: textures[index].name });
  68. }
  69. this.addTexturesToZip(zip, index + 1, textures, folder, then);
  70. return;
  71. }
  72. if (folder == null) {
  73. folder = zip.folder("textures");
  74. }
  75. var url;
  76. if (textures[index].video) {
  77. url = textures[index].video.currentSrc;
  78. } else {
  79. // url = textures[index].name;
  80. url = textures[index].url ? textures[index].url : textures[index].name;
  81. }
  82. var name = textures[index].name.replace("textures/", "");
  83. // var name = url.substr(url.lastIndexOf("/") + 1);
  84. if (url != null) {
  85. this.addContentToZip(folder,
  86. name,
  87. url,
  88. null,
  89. true,
  90. function () {
  91. this.addTexturesToZip(zip, index + 1, textures, folder, then);
  92. }.bind(this));
  93. }
  94. else {
  95. this.addTexturesToZip(zip, index + 1, textures, folder, then);
  96. }
  97. };
  98. addImportedFilesToZip (zip, index, importedFiles, folder, then) {
  99. if (index === importedFiles.length) {
  100. then();
  101. return;
  102. }
  103. if (!folder) {
  104. folder = zip.folder("scenes");
  105. }
  106. var url = importedFiles[index];
  107. var name = url.substr(url.lastIndexOf("/") + 1);
  108. this.addContentToZip(folder,
  109. name,
  110. url,
  111. null,
  112. true,
  113. function () {
  114. this.addImportedFilesToZip(zip, index + 1, importedFiles, folder, then);
  115. }.bind(this));
  116. };
  117. getZip (engine) {
  118. if (engine.scenes.length === 0) {
  119. return;
  120. }
  121. var zip = new JSZip();
  122. var scene = engine.scenes[0];
  123. var textures = scene.textures;
  124. var importedFiles = scene.importedMeshesFiles;
  125. document.getElementById("statusBar").innerHTML = "Creating archive... Please wait.";
  126. var regex = /CreateGroundFromHeightMap\(".+", "(.+)"/g;
  127. do {
  128. let match = regex.exec(this.zipCode);
  129. if (!match) {
  130. break;
  131. }
  132. textures.push({ name: match[1] });
  133. } while(true);
  134. this.addContentToZip(zip,
  135. "index.html",
  136. "/zipContent/index.html",
  137. this.zipCode,
  138. false,
  139. function () {
  140. this.addTexturesToZip(zip,
  141. 0,
  142. textures,
  143. null,
  144. function () {
  145. this.addImportedFilesToZip(zip,
  146. 0,
  147. importedFiles,
  148. null,
  149. function () {
  150. var blob = zip.generate({ type: "blob" });
  151. saveAs(blob, "sample.zip");
  152. document.getElementById("statusBar").innerHTML = "";
  153. }.bind(this));
  154. }.bind(this));
  155. }.bind(this));
  156. };
  157. }