Skip to content

Commit

Permalink
Fix unpackaging some older HTMLifier projects
Browse files Browse the repository at this point in the history
closes #8
  • Loading branch information
GarboMuffin committed Apr 19, 2024
1 parent 2f6add3 commit 2beaeec
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions unpackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ var unpackage = (function() {
return unpackageBinaryBlob(decodeDataURI(dataURI));
}

// HTML files generated by HTMLifier have an inline JSON options object with inline base64
const htmlifierOptions = text.match(/<script>\nconst GENERATED = \d+\nconst initOptions = ({[\s\S]+})\ninit\(initOptions\)\n<\/script>/m);
// HTML files generated by some versions of HTMLifier store the project as fields in initOptions
let htmlifierOptions = text.match(/<script>\nconst GENERATED = \d+\nconst initOptions = ({[\s\S]+})\ninit\(initOptions\)\n<\/script>/m);
if (htmlifierOptions) {
const htmlifierAssets = JSON.parse(htmlifierOptions[1]).assets;

Expand Down Expand Up @@ -368,6 +368,28 @@ var unpackage = (function() {
};
}

// HTML files generated by older versions of HTMLifier with TYPE === "json" have PROJECT_JSON and ASSETS constants
// https://github.com/SheepTester/htmlifier/blob/b80b860f64bf7c4a908f3c9e74bac6285b7a1687/hacky-file-getter.js#L166
htmlifierOptions = text.match(/var TYPE = 'json',\nPROJECT_JSON = "([^"]*)",\nASSETS = ({[^}]*}),/m);
if (htmlifierOptions) {
const projectJSON = decodeDataURI(htmlifierOptions[1]);
const assetsJSON = JSON.parse(htmlifierOptions[2]);

const newZip = new JSZip();
newZip.file('project.json', projectJSON);
for (const name of Object.keys(assetsJSON)) {
newZip.file(name, decodeDataURI(assetsJSON[name]));
}

return {
type: 'sb3',
data: await newZip.generateAsync({
type: 'arraybuffer',
compression: 'DEFLATE'
})
};
}

throw new Error('Input was not a zip and we could not find project.');
};

Expand Down

0 comments on commit 2beaeec

Please sign in to comment.