Skip to content

Commit

Permalink
Fix structure file extraction for completed packs
Browse files Browse the repository at this point in the history
- Extraction of multiple structure files from packs with multiple structure files is now supported.
- Original file names for structure files are put in their comments, a really niche feature for only zip files apparently.
  • Loading branch information
SuperLlama88888 committed Nov 26, 2024
1 parent 18a4a9e commit 47687e6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
34 changes: 20 additions & 14 deletions HoloPrint.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,10 +688,14 @@ export async function makePack(structureFiles, config = {}, resourcePackStack, p

let pack = new JSZip();
if(structureFiles.length == 1) {
pack.file(".mcstructure", structureFiles[0]);
pack.file(".mcstructure", structureFiles[0], {
comment: structureFiles[0].name
});
} else {
structureFiles.forEach((structureFile, i) => {
pack.file(`${i}.mcstructure`, structureFile);
pack.file(`${i}.mcstructure`, structureFile, {
comment: structureFile.name
});
});
}
pack.file("manifest.json", JSON.stringify(manifest));
Expand Down Expand Up @@ -737,18 +741,20 @@ export async function makePack(structureFiles, config = {}, resourcePackStack, p
return new File([zippedPack], `${packName}.holoprint.mcpack`);
}
/**
* Retrieves the structure file from a completed HoloPrint resource pack
* Retrieves the structure files from a completed HoloPrint resource pack.
* @param {File} resourcePack HoloPrint resource pack (`*.mcpack)
* @returns {Promise<File>}
* @returns {Promise<Array<File>>}
*/
export async function extractStructureFileFromPack(resourcePack) {
export async function extractStructureFilesFromPack(resourcePack) {
let packFolder = await JSZip.loadAsync(resourcePack);
let structureBlob = await packFolder.file(".mcstructure")?.async("blob");
if(!structureBlob) {
return undefined;
}
let structureZipObjects = Object.values(packFolder.files).filter(file => file.name.endsWith(".mcstructure"));
let structureBlobs = await Promise.all(structureZipObjects.map(async zipObject => await zipObject.async("blob")));
let packName = resourcePack.name.slice(0, resourcePack.name.indexOf("."));
return new File([structureBlob], `${packName}.mcstructure`);
if(structureBlobs.length == 1) {
return [new File([structureBlobs[0]], structureZipObjects[0].comment ?? `${packName}.mcstructure`)];
} else {
return await Promise.all(structureBlobs.map(async (structureBlob, i) => new File([structureBlob], structureZipObjects[i].comment ?? `${packName}_${i}.mcstructure`)));
}
}
/**
* Updates a HoloPrint resource pack by remaking it.
Expand All @@ -759,11 +765,11 @@ export async function extractStructureFileFromPack(resourcePack) {
* @returns {Promise<File>}
*/
export async function updatePack(resourcePack, config, resourcePackStack, previewCont) {
let structureFile = extractStructureFileFromPack(resourcePack);
if(!structureFile) {
throw new Error(`No structure file found inside resource pack ${resourcePack.name}; cannot update pack!`);
let structureFiles = extractStructureFilesFromPack(resourcePack);
if(!structureFiles) {
throw new Error(`No structure files found inside resource pack ${resourcePack.name}; cannot update pack!`);
}
return await makePack(structureFile, config, resourcePackStack, previewCont);
return await makePack(structureFiles, config, resourcePackStack, previewCont);
}
/**
* Returns the default pack name that would be used if no pack name is specified.
Expand Down
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,8 @@ async function handleInputFiles(files) {
let resourcePacks = files.filter(file => file.name.endsWith(".mcpack"));

for(let resourcePack of resourcePacks) {
let structureFile = await HoloPrint.extractStructureFileFromPack(resourcePack);
if(structureFile) {
structureFiles.push(structureFile);
}
let extractedStructureFiles = await HoloPrint.extractStructureFilesFromPack(resourcePack);
structureFiles.push(...extractedStructureFiles);
}
if(structureFiles.length) {
let dataTransfer = new DataTransfer();
Expand Down

0 comments on commit 47687e6

Please sign in to comment.