|
| 1 | +import JSZip from "jszip"; |
1 | 2 | import type { Datapack } from "./datapack"; |
2 | 3 |
|
3 | 4 | interface DatapackChange { |
@@ -70,8 +71,46 @@ export class DatapackModifier { |
70 | 71 | for (const change of this.changeQueue) { |
71 | 72 | await this.applyChange(change); |
72 | 73 | } |
| 74 | + |
73 | 75 | // Cache with changes created -> write to zip |
| 76 | + let packs: {[key: string]: JSZip} = {}; |
| 77 | + |
| 78 | + for (const file_path in this.changeCache) { |
| 79 | + if (Object.prototype.hasOwnProperty.call(this.changeCache, file_path)) { |
| 80 | + const pack_id = file_path.split(":")[0]; |
| 81 | + |
| 82 | + if (!(pack_id in packs)) { |
| 83 | + packs[pack_id] = new JSZip(); |
| 84 | + } |
| 85 | + |
| 86 | + packs[pack_id].file( |
| 87 | + file_path.split(":")[1], |
| 88 | + this.changeCache[file_path] |
| 89 | + ); |
| 90 | + } |
| 91 | + else throw new Error("what"); |
| 92 | + } |
| 93 | + |
74 | 94 | console.timeEnd("Applying changes to packs..."); |
| 95 | + for (const pack in packs) { |
| 96 | + if (Object.prototype.hasOwnProperty.call(packs, pack)) { |
| 97 | + const zip = packs[pack]; |
| 98 | + await this.saveFile(zip); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public async saveFile(zip: JSZip) { |
| 104 | + await zip.generateAsync({type:"blob"}).then((content) => { |
| 105 | + var link = document.createElement("a"), url = URL.createObjectURL(content); |
| 106 | + link.href = url; link.download = `modified datapack test.zip`; |
| 107 | + document.body.appendChild(link); |
| 108 | + link.click(); |
| 109 | + setTimeout(function() { |
| 110 | + document.body.removeChild(link); |
| 111 | + window.URL.revokeObjectURL(url); |
| 112 | + }, 0); |
| 113 | + }); |
75 | 114 | } |
76 | 115 |
|
77 | 116 | //#region ///// FILE CACHE MANIPULATION ///// |
|
0 commit comments