Skip to content

Commit 08eab50

Browse files
committed
File API
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
1 parent 80bced2 commit 08eab50

File tree

4 files changed

+47
-56
lines changed

4 files changed

+47
-56
lines changed

package-lock.json

Lines changed: 9 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
{
22
"name": "gamedata-parser",
33
"version": "0.3.0",
4+
"private": true,
45
"description": "An experimental script to parse Minecraft Legacy Console PS3 saves!",
56
"type": "module",
67
"main": "./dist/index.js",
78
"types": "./dist/index.d.ts",
9+
"scripts": {
10+
"build": "tsc",
11+
"dev": "tsc -w"
12+
},
813
"repository": {
914
"type": "git",
1015
"url": "git+https://github.com/Offroaders123/Gamedata-Parser.git"
@@ -25,7 +30,7 @@
2530
"url": "https://github.com/Offroaders123/Gamedata-Parser/issues"
2631
},
2732
"homepage": "https://github.com/Offroaders123/Gamedata-Parser#readme",
28-
"optionalDependencies": {
29-
"typescript": "^4.8.4"
33+
"devDependencies": {
34+
"typescript": "^5.0.4"
3035
}
3136
}

src/index.ts

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,44 @@ export interface Definition {
44
length: number;
55
}
66

7-
export class Gamedata {
8-
static read(data: Uint8Array){
9-
const definitions = this.readDefinitions(data);
7+
export function read(data: Uint8Array){
8+
const definitions = readDefinitions(data);
109

11-
const result: Gamedata[] = [];
10+
const result: File[] = [];
1211

13-
for (const { name, offset, length } of definitions){
14-
const content = new Uint8Array(data.slice(offset,offset + length));
15-
const file = new Gamedata(name,content);
16-
result.push(file);
17-
}
18-
19-
return result;
12+
for (const { name, offset, length } of definitions){
13+
const content = data.subarray(offset,offset + length);
14+
const file = new File([content],name);
15+
result.push(file);
2016
}
2117

22-
static getDefinitions(data: Uint8Array){
23-
const view = new DataView(data.buffer);
24-
const offset = view.getUint32(0);
25-
26-
return new Uint8Array(data.slice(offset));
27-
}
18+
return result;
19+
}
2820

29-
static readDefinitions(data: Uint8Array){
30-
const definitions = this.getDefinitions(data);
31-
const result: Definition[] = [];
21+
export function getDefinitions(data: Uint8Array){
22+
const view = new DataView(data.buffer,data.byteOffset,data.byteLength);
23+
const offset = view.getUint32(0);
24+
const definitions = data.subarray(offset);
25+
return definitions;
26+
}
3227

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

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

41-
const length = view.getUint32(0);
42-
const offset = view.getUint32(4);
37+
const header = definition.subarray(128);
38+
const view = new DataView(header.buffer,header.byteOffset,header.byteLength);
4339

44-
result.push({ name, offset, length });
45-
}
40+
const length = view.getUint32(0);
41+
const offset = view.getUint32(4);
4642

47-
return result;
43+
result.push({ name, offset, length });
4844
}
4945

50-
constructor(public name: string, public data: Uint8Array) {}
51-
}
52-
53-
export default Gamedata;
46+
return result;
47+
}

test/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

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

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

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

0 commit comments

Comments
 (0)