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 ( `
2525import micropip
@@ -31,17 +31,32 @@ print(package_list)
3131
3232from pathlib import Path
3333import 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}
0 commit comments