Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions src/processors/gridsetProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2524,9 +2524,8 @@ class GridsetProcessor extends BaseProcessor {
return;
}

const AdmZip = (await import('adm-zip')).default;
const originalZip = new AdmZip(originalPath);
const outputZip = new AdmZip();
const originalZip = await this.options.zipAdapter(originalPath);
const outputZip = await this.options.zipAdapter();

// Check if any page has pending mutations
const hasPendingMutations = Object.values(tree.pages).some(
Expand Down Expand Up @@ -2556,15 +2555,13 @@ class GridsetProcessor extends BaseProcessor {
(page) => this.createBasicGridXml(page)
);

// Copy remaining files
for (const entry of originalZip.getEntries()) {
if (entry.isDirectory) continue;
if (!outputZip.getEntry(entry.entryName)) {
outputZip.addFile(entry.entryName, entry.getData());
}
// Copy files
const outputFiles = [];
for (const name of originalZip.listFiles()) {
const data = await originalZip.readFile(name);
outputFiles.push({ name, data });
}

const outputBuffer = outputZip.toBuffer();
const outputBuffer = await outputZip.writeFiles(outputFiles);
await writeBinaryToPath(outputPath, outputBuffer);
return;
}
Expand Down Expand Up @@ -2601,17 +2598,17 @@ class GridsetProcessor extends BaseProcessor {
modifiedGridFiles.add(gridPath);

// Try to get the original grid.xml file
const originalEntry = originalZip.getEntry(gridPath);
const originalEntries = originalZip.listFiles();

if (!originalEntry) {
if (!originalEntries.includes(gridPath)) {
// If original doesn't exist, create a new basic grid
const basicGrid = this.createBasicGridXml(page);
newGridFiles.set(gridPath, basicGrid);
continue;
}

// Parse the original grid XML
const originalContent = originalEntry.getData().toString('utf-8');
const originalContent = (await originalZip.readFile(gridPath)).toString();
const originalGrid = parser.parse(originalContent);

if (!originalGrid.Grid) {
Expand Down Expand Up @@ -2816,24 +2813,24 @@ class GridsetProcessor extends BaseProcessor {
}

// Copy all files from original zip, replacing modified grid files
for (const entry of originalZip.getEntries()) {
if (entry.isDirectory) continue;

const outputFiles = [];
for (const entry of originalZip.listFiles()) {
// Skip grid.xml files that we're modifying
if (modifiedGridFiles.has(entry.entryName)) {
const newContent = newGridFiles.get(entry.entryName);
if (modifiedGridFiles.has(entry)) {
const newContent = newGridFiles.get(entry);
if (newContent) {
outputZip.addFile(entry.entryName, Buffer.from(newContent, 'utf8'));
outputFiles.push({ name: entry, data: Buffer.from(newContent, 'utf8') });
}
continue;
}

// Copy all other files as-is
outputZip.addFile(entry.entryName, entry.getData());
const data = await originalZip.readFile(entry);
outputFiles.push({ name: entry, data });
}

// Write the output ZIP
const outputBuffer = outputZip.toBuffer();
const outputBuffer = await outputZip.writeFiles(outputFiles);
await writeBinaryToPath(outputPath, outputBuffer);
}

Expand Down
46 changes: 14 additions & 32 deletions src/processors/obfProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -935,30 +935,21 @@ class ObfProcessor extends BaseProcessor {
return;
}

const AdmZip = (await import('adm-zip')).default;
const originalZip = new AdmZip(originalPath);
const outputZip = new AdmZip();

// Track which .obf files we're modifying
const modifiedObfFiles = new Set<string>();

// Generate new .obf files for pages in the tree
const newObfFiles = new Map<string, string>();
const originalZip = await this.options.zipAdapter(originalPath);
const outputZip = await this.options.zipAdapter();
const outputFiles = [];

for (const page of Object.values(tree.pages)) {
const obfFilename = this.getPageFilename(page.id, tree.metadata);
modifiedObfFiles.add(obfFilename);
const name = this.getPageFilename(page.id, tree.metadata);

// createObfBoardFromPage will automatically apply mutations if present
const obfBoard = this.createObfBoardFromPage(page, 'Board', tree.metadata);
const obfContent = JSON.stringify(obfBoard, null, 2);
newObfFiles.set(obfFilename, obfContent);
const data = JSON.stringify(obfBoard, null, 2);
outputFiles.push({ name, data });
}

// Generate updated manifest if we have pages
if (Object.keys(tree.pages).length > 0) {
modifiedObfFiles.add('manifest.json');

const manifest: ObfManifest = {
format: OBF_FORMAT_VERSION,
root: tree.metadata.defaultHomePageId,
Expand All @@ -973,29 +964,20 @@ class ObfProcessor extends BaseProcessor {
sounds: {},
},
};

newObfFiles.set('manifest.json', JSON.stringify(manifest));
const data = Buffer.from(JSON.stringify(manifest), 'utf8');
outputFiles.push({ name: 'manifest.json', data });
}

// Copy all files from original zip, replacing modified .obf files
for (const entry of originalZip.getEntries()) {
if (entry.isDirectory) continue;

// Skip .obf files that we're modifying
if (modifiedObfFiles.has(entry.entryName)) {
const newContent = newObfFiles.get(entry.entryName);
if (newContent) {
outputZip.addFile(entry.entryName, Buffer.from(newContent, 'utf8'));
}
continue;
// Add remaining .obf from original zip
for (const entry of originalZip.listFiles()) {
if (!outputFiles.find((file) => file.name === entry)) {
const data = await originalZip.readFile(entry);
outputFiles.push({ name: entry, data });
}

// Copy all other files as-is (preserves images, sounds, etc.)
outputZip.addFile(entry.entryName, entry.getData());
}

// Write the output ZIP
const outputBuffer = outputZip.toBuffer();
const outputBuffer = await outputZip.writeFiles(outputFiles);
await writeBinaryToPath(outputPath, outputBuffer);
}

Expand Down
Loading