Skip to content

Commit

Permalink
File API
Browse files Browse the repository at this point in the history
Super happy about this! While working on the cross-platform (in regards to the different Minecraft versions/platforms) Minecraft world APIs, I decided to go back and rework the code for this projectt before moving this existing code over to there. I looked into the status of the File API constructor in NodeJS, as I remembered it being one of the most recent things that would be a great upgrade here, and looks like it just landed about a month ago! Super awesome. That takes one more thing out, and now it's a little bit less of a headache to make work everywhere. That's awesome.

nodejs/node#39015
nodejs/node#47153
nodejs/node@7bc0e6a
  • Loading branch information
Offroaders123 committed May 2, 2023
1 parent 80bced2 commit 08eab50
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 56 deletions.
26 changes: 9 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"name": "gamedata-parser",
"version": "0.3.0",
"private": true,
"description": "An experimental script to parse Minecraft Legacy Console PS3 saves!",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsc",
"dev": "tsc -w"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Offroaders123/Gamedata-Parser.git"
Expand All @@ -25,7 +30,7 @@
"url": "https://github.com/Offroaders123/Gamedata-Parser/issues"
},
"homepage": "https://github.com/Offroaders123/Gamedata-Parser#readme",
"optionalDependencies": {
"typescript": "^4.8.4"
"devDependencies": {
"typescript": "^5.0.4"
}
}
64 changes: 29 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,44 @@ export interface Definition {
length: number;
}

export class Gamedata {
static read(data: Uint8Array){
const definitions = this.readDefinitions(data);
export function read(data: Uint8Array){
const definitions = readDefinitions(data);

const result: Gamedata[] = [];
const result: File[] = [];

for (const { name, offset, length } of definitions){
const content = new Uint8Array(data.slice(offset,offset + length));
const file = new Gamedata(name,content);
result.push(file);
}

return result;
for (const { name, offset, length } of definitions){
const content = data.subarray(offset,offset + length);
const file = new File([content],name);
result.push(file);
}

static getDefinitions(data: Uint8Array){
const view = new DataView(data.buffer);
const offset = view.getUint32(0);

return new Uint8Array(data.slice(offset));
}
return result;
}

static readDefinitions(data: Uint8Array){
const definitions = this.getDefinitions(data);
const result: Definition[] = [];
export function getDefinitions(data: Uint8Array){
const view = new DataView(data.buffer,data.byteOffset,data.byteLength);
const offset = view.getUint32(0);
const definitions = data.subarray(offset);
return definitions;
}

for (let i = 0; i < definitions.byteLength; i += 144){
const definition = new Uint8Array(definitions.slice(i,i + 144));
const name = new TextDecoder("utf-16be").decode(definition).split("\0")[0].replace("-1r.","-1/r.");
// Replace call fixes a naming inconsistency for Nether region files.
export function readDefinitions(data: Uint8Array){
const definitions = getDefinitions(data);
const result: Definition[] = [];

const header = new Uint8Array(definition.slice(128));
const view = new DataView(header.buffer);
for (let i = 0; i < definitions.byteLength; i += 144){
const definition = definitions.subarray(i,i + 144);
const name = new TextDecoder("utf-16be").decode(definition).split("\0")[0].replace("-1r.","-1/r.");
// Replace call fixes a naming inconsistency for Nether region files.

const length = view.getUint32(0);
const offset = view.getUint32(4);
const header = definition.subarray(128);
const view = new DataView(header.buffer,header.byteOffset,header.byteLength);

result.push({ name, offset, length });
}
const length = view.getUint32(0);
const offset = view.getUint32(4);

return result;
result.push({ name, offset, length });
}

constructor(public name: string, public data: Uint8Array) {}
}

export default Gamedata;
return result;
}
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { readFile, writeFile, mkdir } from "node:fs/promises";
import { join, dirname } from "node:path";
import Gamedata from "../dist/index.js";
import * as Gamedata from "../dist/index.js";

const data = await readFile(new URL("./world/GAMEDATA",import.meta.url));

Expand All @@ -13,5 +13,5 @@ for (const file of files){
const path = decodeURIComponent(new URL(join("./world_data",file.name),import.meta.url).pathname);

await mkdir(dirname(path),{ recursive: true });
writeFile(path,file.data);
writeFile(path,new Uint8Array(await file.arrayBuffer()));
}

0 comments on commit 08eab50

Please sign in to comment.