Skip to content

Instantly share code, notes, and snippets.

@SamboyCoding
Created July 20, 2021 23:22
Show Gist options
  • Save SamboyCoding/0e827b07e1c938f022108b135d7caffb to your computer and use it in GitHub Desktop.
Save SamboyCoding/0e827b07e1c938f022108b135d7caffb to your computer and use it in GitHub Desktop.
import {readFileSync} from "fs";
const ObjectType = {
Map: 0,
String: 1,
Int32: 2
};
class Reader {
constructor(content) {
this.content = content;
this.index = 0;
}
hasMore() {
return this.index < this.content.length;
}
peekByte() {
if(!this.hasMore())
throw new Error("EOF");
return this.content[this.index];
}
readByte() {
if(!this.hasMore())
throw new Error("EOF");
const ret = this.content[this.index];
this.index++;
return ret;
}
readNullTerminatedString() {
let nullFound = false;
let ret = "";
while(!nullFound && this.hasMore()) {
let byte = this.readByte();
if(byte === 0) {
nullFound = true;
continue;
}
ret += String.fromCharCode(byte);
}
if(!nullFound)
throw new Error("EOF When reading null-terminating string");
return ret;
}
readInt16() {
return this.readByte() << 8 | this.readByte();
}
readInt32() {
const ret = this.content.readUIntLE(this.index, 4);
this.index += 4;
return ret;
}
readMapKV() {
const objectType = this.readByte();
const keyName = this.readNullTerminatedString();
let value;
switch(objectType) {
case ObjectType.Map:
value = this.readMap();
break;
case ObjectType.Int32:
value = this.readInt32();
break;
case ObjectType.String:
value = this.readNullTerminatedString();
break;
default:
console.log("Unknown object type " + objectType);
}
return {key: keyName, value};
}
readMap() {
const ret = {};
while(this.hasMore() && this.peekByte() !== 8) {
const kv = this.readMapKV();
ret[kv.key] = kv.value;
}
if(!this.hasMore())
throw new Error("EOF - expecting map terminator (0x08)");
this.readByte(); //Consume 0x08
return ret;
}
}
/**
*
* @param path {String}
* @return {any}
*/
function parse(path) {
const reader = new Reader(readFileSync(path));
return reader.readMap();
}
console.log(JSON.stringify(parse("C:\\Program Files (x86)\Steam\\userdata\\USERIDHERE\\config\\shortcuts.vdf"), null, 4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment