Skip to content

Commit 37f3b09

Browse files
committed
feat: download zip for sb2gs
1 parent b5c9a3c commit 37f3b09

6 files changed

Lines changed: 91 additions & 27 deletions

File tree

addons/goboscript/addon.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"versionAdded": "1.44.0",
2020
"userscripts": [
2121
{
22-
"url": "sb2gs.js",
22+
"url": "userscript.js",
2323
"matches": [
2424
"projects"
2525
]

addons/goboscript/importet.js

Lines changed: 0 additions & 3 deletions
This file was deleted.
4 Bytes
Binary file not shown.

addons/goboscript/sb2gs.js

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
/* global chrome */
2-
import * as pyodidePkg from "./pyodide/pyodide.mjs";
2+
import downloadBlob from "../../libraries/common/cs/download-blob.js";
3+
4+
5+
export const sb2gsWhlName = "sb2gs-2.0.0-py3-none-any.whl";
36

47
/**
58
* @param addon {UserscriptAddon}
69
* @param console
10+
* @param pyodide
11+
* @param decompileButton {HTMLButtonElement}
712
* @returns {Promise<void>}
813
*/
9-
export default async function({ addon, console }) {
10-
const decompileButton = document.createElement("button");
11-
decompileButton.className = "button sa-decompile-button";
12-
decompileButton.title = "decompile to goboscript code";
13-
decompileButton.appendChild(document.createElement("span")).innerText = "Decompile";
14-
15-
const pyodide = await pyodidePkg.loadPyodide();
16-
await pyodide.loadPackage("micropip");
14+
export async function decompile(addon, console, pyodide, decompileButton) {
15+
const vm = addon.tab.traps.vm;
16+
/**@type {Blob}*/
17+
const project = await vm.saveProjectSb3();
18+
const inputPath = "/input.sb3";
19+
const outputDirPath = "/workdir";
20+
const outputPath = "/output.zip";
1721

18-
// TESTING
19-
pyodide.FS.writeFile("/input.sb3", await ((await fetch(`${addon.self.dir}/test.sb3`)).bytes()));
20-
21-
const sb2gsWhlName = "sb2gs-2.0.0-py3-none-any.whl";
22-
pyodide.FS.writeFile(`/${sb2gsWhlName}`, await ((await fetch(`${addon.self.dir}/${sb2gsWhlName}`)).bytes()));
22+
pyodide.FS.writeFile(inputPath, new Uint8Array(await project.arrayBuffer()));
2323

2424
await pyodide.runPythonAsync(`
2525
import micropip
@@ -31,17 +31,32 @@ print(package_list)
3131
3232
from pathlib import Path
3333
import sb2gs
34+
import shutil
35+
36+
input_path = Path("${inputPath}")
37+
output_dir_path = Path("${outputDirPath}")
3438
35-
sb2gs.decompile(Path("/input.sb3"), Path("/output"))
39+
sb2gs.decompile(input_path, output_dir_path)
40+
41+
shutil.make_archive("${outputPath.slice(0, -4)}", "zip", "${outputDirPath}")
3642
`);
43+
// This is the zip file provided by shutil.make_archive
44+
const data = pyodide.FS.readFile(outputPath);
45+
// for now, let's just download it instead of making an entire file viewing interface
46+
const blob = new Blob([data]);
47+
48+
// We need to determine the title of the file.
49+
// The logic for this is taken from download-button/userscript.js
50+
const username = await addon.auth.fetchUsername();
51+
const projectAuthor = addon.tab.redux.state.preview.projectInfo.author?.username;
52+
53+
const isOwn = username === projectAuthor;
54+
55+
const title = isOwn ? document.querySelector(".project-title input") : document.querySelector(".project-title");
56+
const titleStr = isOwn ? title.value : title.innerText;
3757

38-
console.log(pyodide.FS.readdir("/output"));
39-
// END TESTING
58+
const projectId = window.location.pathname.split("/")[2]
4059

41-
while (true) {
42-
const seeInside = await addon.tab.waitForElement(".see-inside-button", {
43-
markAsSeen: true,
44-
});
45-
seeInside.parentElement.appendChild(decompileButton);
46-
}
60+
const beginFilenameWithId = true;
61+
downloadBlob((beginFilenameWithId ? `${projectId} ` : "") + titleStr + '.zip', blob);
4762
}

addons/goboscript/test.sb3

-22.1 KB
Binary file not shown.

addons/goboscript/userscript.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* global chrome */
2+
import * as sb2gs from './sb2gs.js';
3+
import * as pyodidePkg from "./pyodide/pyodide.mjs";
4+
5+
/**
6+
* @param addon {UserscriptAddon}
7+
* @param console
8+
* @returns {Promise<void>}
9+
*/
10+
export default async function({ addon, console }) {
11+
const decompileButton = document.createElement("button");
12+
decompileButton.className = "button sa-decompile-button waiting";
13+
decompileButton.title = "decompile to goboscript code";
14+
decompileButton.appendChild(document.createElement("span")).innerText = "Decompile";
15+
16+
const pyodidePromise = pyodidePkg.loadPyodide();
17+
18+
const pyodidePackagingPromise = pyodidePromise.then(async (pyodide) => {
19+
console.log("PYODIDE LOADED");
20+
21+
const loadMicropipPromise = pyodide.loadPackage("micropip");
22+
23+
const sb2gsWhlFile = await ((await
24+
fetch(`${addon.self.dir}/${sb2gs.sb2gsWhlName}`)).bytes());
25+
26+
pyodide.FS.writeFile(`/${sb2gs.sb2gsWhlName}`, sb2gsWhlFile);
27+
28+
await loadMicropipPromise;
29+
30+
console.log("EXITING PYODIDE PACKAGING");
31+
return pyodide;
32+
});
33+
34+
decompileButton.onclick = async () => {
35+
console.log("DECOMPILE BUTTON CLICKED");
36+
const pyodide = await pyodidePackagingPromise;
37+
console.log("AWAITED PYODIDE PACKAGING");
38+
39+
decompileButton.classList.remove("waiting");
40+
decompileButton.classList.add("loading");
41+
await sb2gs.decompile(addon, console, pyodide, decompileButton);
42+
decompileButton.classList.remove("loading");
43+
decompileButton.classList.add("waiting");
44+
}
45+
46+
while (true) {
47+
const seeInside = await addon.tab.waitForElement(".see-inside-button", {
48+
markAsSeen: true,
49+
});
50+
seeInside.parentElement.appendChild(decompileButton);
51+
}
52+
}

0 commit comments

Comments
 (0)