-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CRT-24] Use composition instead of patching VM
- Loading branch information
Showing
6 changed files
with
97 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ScratchCrtConfig } from "scratch-vm"; | ||
|
||
export const defaultCrtConfig: ScratchCrtConfig = { | ||
allowedBlocks: {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import VM, { ScratchCrtConfig } from "scratch-vm"; | ||
import { defaultCrtConfig } from "./default-crt-config"; | ||
import JSZip from "jszip"; | ||
|
||
/** | ||
* Load a Scratch project from a .sb, .sb2, .sb3 or json string. | ||
* @param input A json string, object, or ArrayBuffer representing the project to load. | ||
* @return Promise that resolves after targets are installed. | ||
*/ | ||
export const loadCrtProject = async ( | ||
vm: VM, | ||
input: ArrayBufferView | ArrayBuffer | string | object, | ||
): Promise<void> => { | ||
// overwrite any existing config | ||
vm.crtConfig = { | ||
...defaultCrtConfig, | ||
}; | ||
|
||
if (input instanceof ArrayBuffer) { | ||
const zip = new JSZip(); | ||
await zip.loadAsync(input); | ||
|
||
const configFile = zip.file("crt.json"); | ||
|
||
if (configFile) { | ||
// if the project contains a crt.json file, we parse it | ||
const config: ScratchCrtConfig = await configFile | ||
.async("text") | ||
.then((text) => JSON.parse(text)); | ||
|
||
vm.crtConfig = config; | ||
} | ||
} | ||
|
||
return vm.loadProject(input); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import VM from "scratch-vm"; | ||
import JSZip from "jszip"; | ||
|
||
/** | ||
* @returns Project in a Scratch 3.0 JSON representation. | ||
*/ | ||
export const saveCrtProject = async (vm: VM): Promise<Blob> => { | ||
const blob = await vm.saveProjectSb3(); | ||
|
||
const zip = new JSZip(); | ||
await zip.loadAsync(blob); | ||
|
||
zip.file("crt.json", JSON.stringify(vm.crtConfig)); | ||
|
||
return zip.generateAsync({ | ||
// options consistent with https://github.com/scratchfoundation/scratch-vm/blob/766c767c7a2f3da432480ade515de0a9f98804ba/src/virtual-machine.js#L400C19-L407C12 | ||
type: "blob", | ||
mimeType: "application/x.scratch.sb3", | ||
compression: "DEFLATE", | ||
compressionOptions: { | ||
level: 6, | ||
}, | ||
}); | ||
}; |