Skip to content

Commit

Permalink
XML Version support in PCK reading apparently
Browse files Browse the repository at this point in the history
  • Loading branch information
DexrnZacAttack committed Oct 5, 2024
1 parent a93956e commit 4f9dd1d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,4 @@ dist
/testing/reading/msscmp
/testing/results
/staging
/testing/reading
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ tsconfig.build.json
.vscode
/testing_internal
/.idea
/staging
/staging
.github
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "liblce",
"version": "1.2.11",
"version": "1.2.12",
"description": "A TS library for Minecraft Legacy Console Edition files + more",
"type": "module",
"main": "./dist/index.js",
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export * from "./loc/readLoc.js";
// arc
export * from "./arc/readArc.js";
export * from "./arc/writeArc.js";
// consoles
export * from "./console/consoles.js";
// msscmp
export * from "./msscmp/readMsscmp.js";
// pck
export * from "./pck/readPck.js";
// consoles
export * from "./console/consoles.js";

export interface MsscmpFile {
fileName: string,
Expand Down Expand Up @@ -81,6 +81,7 @@ export interface PckFileData {

export interface PckFile {
version: number;
xmlVersion?: number;
lookupTable: LookupTable[]
fileTable: PckFileData[];
};
Expand Down
24 changes: 21 additions & 3 deletions src/pck/readPck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*
* File Contributors (based off of GitHub commits):
* - DexrnZacAttack
* - Offroaders123
*
* Other credits:
* - NessieHax
Expand All @@ -17,9 +16,14 @@
import { bReader } from "binaryio.js";
import { LookupTable, PckFileData, PckFile, PckFileEntry, PckKV, PckFileTypes } from "../index.js";

export async function readPCK(file: File, isLittle = false): Promise<PckFile> {
export async function readPCK(file: File, isLittle?: boolean): Promise<PckFile> {
const reader = new bReader(await file.arrayBuffer(), isLittle);
let readXMLVersion = false;

if (typeof isLittle === 'undefined' && reader.readUInt() > 4)
reader.setEndianness(true);

reader.setPos(0);
const version = reader.readUInt();

const lookupTable: LookupTable[] = [];
Expand All @@ -29,9 +33,22 @@ export async function readPCK(file: File, isLittle = false): Promise<PckFile> {
const lookupTableCount = reader.readUInt();

for (var i = 0; i < lookupTableCount; i++) {
lookupTable.push({ offset: reader.readUInt(), name: reader.readString16(reader.readUInt() * 2) });
const offset = reader.readUInt();
const name = reader.readString16(reader.readUInt() * 2);

if (name == "XMLVERSION")
readXMLVersion = true;

lookupTable.push({ offset, name});
reader.readUInt();
}

let xmlVersion;

if (readXMLVersion)
xmlVersion = reader.readUInt();

readXMLVersion = false;

const fileTableCount = reader.readUInt();

Expand All @@ -56,6 +73,7 @@ export async function readPCK(file: File, isLittle = false): Promise<PckFile> {

return {
version,
xmlVersion,
lookupTable: lookupTable,
fileTable: PckFiles
}
Expand Down
26 changes: 24 additions & 2 deletions testing/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readdir, readFile, stat, writeFile } from "fs/promises";

import path, { join, resolve, relative } from "path";

import { CompressionTypes, compressSave, writeSave, readARC, readSave, writeArc, readMSSCMP } from "../src/index.js";
import { CompressionTypes, compressSave, writeSave, readARC, readSave, writeArc, readMSSCMP, readPCK } from "../src/index.js";
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, statSync } from "fs";

async function runArcTest(): Promise<ArrayBuffer> {
Expand Down Expand Up @@ -55,6 +55,22 @@ async function runMSSCMPTest() {
}
}

async function runPCKTest() {
try {
const files = readdirSync("reading\\PCK");

for (const file of files) {
const filePath = path.join("reading\\PCK", file);
if (statSync(filePath).isFile()) {
console.log(`Reading PCK ${filePath}`)
await readPCK(new File([readFileSync(filePath)], "pcktest"));
}
}
} catch (error) {
console.error('Error reading PCK:', error);
}
}


async function runTests() {
if (!existsSync("results")) {
Expand Down Expand Up @@ -90,7 +106,13 @@ async function runTests() {
// To run this test, place .MSSCMP files "./reading/MSSCMP".
if (existsSync("reading/MSSCMP") && lstatSync("reading/MSSCMP").isDirectory()) {
console.log("Reading MSSCMPs");
runMSSCMPTest();
await runMSSCMPTest();
}

// To run this test, place .PCK files "./reading/PCK".
if (existsSync("reading/PCK") && lstatSync("reading/PCK").isDirectory()) {
console.log("Reading PCKs");
await runPCKTest();
}
}

Expand Down

0 comments on commit 4f9dd1d

Please sign in to comment.