Skip to content

Commit

Permalink
Improve sb2 unpackaging
Browse files Browse the repository at this point in the history
Fixes:
- unpackaging raw sb2 (before we would return project.json but no assets and wrong type)
- unpackaging exe in https://gamejolt.com/games/Jazzcraft/474009
  • Loading branch information
GarboMuffin committed Apr 19, 2024
1 parent 4b8bc21 commit 570c1c8
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions unpackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,27 +245,39 @@ var unpackage = (function() {
const packagedZip = await unzipOrNull(blob);

if (packagedZip) {
// Zip files generated by the TurboWarp Packager can have a project.json alongside the assets
// Raw sb2, raw sb3, and zip files generated by TurboWarp Packager have a project.json alongside the assets.
const projectJSON = findFileInZip(packagedZip, 'project.json');
if (projectJSON) {
// The project is an sb3 project.
const innerFolderPath = getContainingFolder(projectJSON.name);
const innerZip = packagedZip.folder(innerFolderPath);

let sb3Assets = 0;
let sb2Assets = 0;

// Remove extra files that aren't part of the project but are in the same folder
// This matters for HTMLifier zips of Scratch 3 projects
// This removes extra files from HTMLifier zips of Scratch 3 projects
for (const path of Object.keys(innerZip.files)) {
const isPartOfProject = (
path === 'project.json' ||
/^[a-f0-9]{32}\.[a-z0-9]{3}$/i.test(path)
);
if (!isPartOfProject) {
if (path === 'project.json') {
// keep
} else if (/^[a-f0-9]{32}\.[a-z0-9]{3}$/i.test(path)) {
// sb3 asset; keep
sb3Assets++;
} else if (/^[0-9]+\.[a-z0-9]{3}$/i.test(path)) {
// sb2 asset; keep
sb2Assets++;
} else {
innerZip.remove(path);
}
}

// Guess project time based on assets because we can't reliably parse the JSON without
// importing @turbowarp/json. Only count as sb2 if we saw an sb2-style asset name and
// and nothing that looks like an sb3. sb3 is much more common so if we're unsure,
// we'll lean towards that if there's any ambiguity.
const type = sb2Assets > 0 && sb3Assets === 0 ? 'sb2' : 'sb3';

return {
type: 'sb3',
type,
data: await innerZip.generateAsync({
type: 'arraybuffer',
compression: 'DEFLATE'
Expand Down

0 comments on commit 570c1c8

Please sign in to comment.