zipTool.js 5.6 KB

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