Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser refactor #24

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Node
node_modules/

# VSCode
.vscode/launch.json

# Project
test-data/
/lib/tsconfig.tsbuildinfo
/lib/tsconfig.tsbuildinfo

# Mac OS
.DS_Store
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tabWidth": 2,
"semi": true,
"singleQuote": false
}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"typescript.tsdk": "./node_modules/typescript/lib",
"editor.tabSize": 2
"editor.tabSize": 2,
"prettier.configPath": "./.prettierrc",
}
2 changes: 1 addition & 1 deletion dts/binary-serializer/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export interface BinaryParsable {
export interface BinaryWritable {
write(writer: DataWriter): void;
}
export declare type BinarySerializable = BinaryParsable & BinaryWritable;
export type BinarySerializable = BinaryParsable & BinaryWritable;
1 change: 1 addition & 0 deletions dts/cli/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
7 changes: 7 additions & 0 deletions dts/cli/lib.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import minimist from "minimist";
export type cliArgs = {
saveFile: string;
showProgress: boolean;
showTags: boolean;
};
export declare function parseTestArgs(rawArgs: minimist.ParsedArgs): cliArgs;
2 changes: 2 additions & 0 deletions dts/cli/test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import yargs from "yargs";
export declare function mount(y: yargs.Argv): yargs.Argv<{}>;
2 changes: 1 addition & 1 deletion dts/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export * from "./binary-serializer/types";
export { ParseError } from "./parser";
export { progressReporter } from "./progress";
export { tagReporter } from "./tagger";
export { E_VERSION_MAJOR, E_VERSION_MINOR, } from "./save-structure/version-validator";
export { E_VERSION_MAJOR, E_VERSION_MINOR, } from "./save-structure/version";
export interface ParseOptions extends SaveGameParserOptions {
interceptor?: ParseInterceptor;
}
Expand Down
3 changes: 3 additions & 0 deletions dts/parser/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @module parser
*/
export * from "./parse";
export * from "./unparse";
export * from "./errors";
5 changes: 3 additions & 2 deletions dts/parser/parse/parser.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataReader } from "../../binary-serializer";
export declare type ParseIterator<T> = Generator<any, T, any>;
export declare type ParseInterceptor = (value: any) => any;
import { ParserInstruction } from "../types";
export type ParseIterator<T> = Generator<ParserInstruction, T, any>;
export type ParseInterceptor = (instruction: ParserInstruction) => ParserInstruction;
export declare function parse<T>(reader: DataReader, readParser: ParseIterator<T>, interceptor?: ParseInterceptor): T;
110 changes: 70 additions & 40 deletions dts/parser/parse/read-instructions.d.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,106 @@
import { LongNum } from "../../binary-serializer/types";
import { ParserInstruction } from "../types";
import { ParseIterator } from "./parser";
export interface BasicReadInstruction {
export type PrimaryNameReturn = {
byte: number;
"signed-byte": number;
"byte-array": ArrayBuffer;
"uint-16": number;
"int-16": number;
"uint-32": number;
"int-32": number;
"uint-64": LongNum;
"int-64": LongNum;
single: number;
double: number;
chars: string;
"klei-string": string | null;
"skip-bytes": void;
"reader-position": number;
};
export type PrimaryTName = keyof PrimaryNameReturn & string;
export type PrimaryInstruction = {
byte: ReadByteInstruction;
"signed-byte": ReadSByteInstruction;
"byte-array": ReadBytesInstruction;
"uint-16": ReadUInt16Instruction;
"int-16": ReadInt16Instruction;
"uint-32": ReadUInt32Instruction;
"int-32": ReadInt32Instruction;
"uint-64": ReadUInt64Instruction;
"int-64": ReadInt64Instruction;
single: ReadSingleInstruction;
double: ReadDoubleInstruction;
chars: ReadCharsInstruction;
"klei-string": ReadKleiStringInstruction;
"skip-bytes": SkipBytesInstruction;
"reader-position": GetReaderPosition;
};
export type TNameOf<TReturn> = {
[TName in PrimaryTName]: PrimaryNameReturn[TName] extends TReturn ? TName : never;
}[PrimaryTName];
export type InstOf<TReturn> = TReturn extends PrimaryReturn ? PrimaryInstruction[TNameOf<TReturn>] : ReadCompressedInstruction<TReturn>;
export type PrimaryReturn = PrimaryNameReturn[PrimaryTName];
export declare abstract class BasicReadInstruction<TName extends string> extends ParserInstruction {
type: "read";
dataType: string;
isMeta: false;
abstract dataType: TName;
}
export interface ReadByteInstruction extends BasicReadInstruction {
export declare class ReadByteInstruction extends BasicReadInstruction<"byte"> {
dataType: "byte";
}
export declare function readByte(): ReadByteInstruction;
export interface ReadSByteInstruction extends BasicReadInstruction {
export declare class ReadSByteInstruction extends BasicReadInstruction<"signed-byte"> {
dataType: "signed-byte";
}
export declare function readSByte(): ReadSByteInstruction;
export interface ReadBytesInstruction extends BasicReadInstruction {
export declare class ReadBytesInstruction extends BasicReadInstruction<"byte-array"> {
length?: number | undefined;
dataType: "byte-array";
length?: number;
constructor(length?: number | undefined);
}
export declare function readBytes(length?: number): ReadBytesInstruction;
export interface ReadUInt16Instruction extends BasicReadInstruction {
export declare class ReadUInt16Instruction extends BasicReadInstruction<"uint-16"> {
dataType: "uint-16";
}
export declare function readUInt16(): ReadUInt16Instruction;
export interface ReadInt16Instruction extends BasicReadInstruction {
export declare class ReadInt16Instruction extends BasicReadInstruction<"int-16"> {
dataType: "int-16";
}
export declare function readInt16(): ReadInt16Instruction;
export interface ReadUInt32Instruction extends BasicReadInstruction {
export declare class ReadUInt32Instruction extends BasicReadInstruction<"uint-32"> {
dataType: "uint-32";
}
export declare function readUInt32(): ReadUInt32Instruction;
export interface ReadInt32Instruction extends BasicReadInstruction {
export declare class ReadInt32Instruction extends BasicReadInstruction<"int-32"> {
dataType: "int-32";
}
export declare function readInt32(): ReadInt32Instruction;
export interface ReadUInt64Instruction extends BasicReadInstruction {
export declare class ReadUInt64Instruction extends BasicReadInstruction<"uint-64"> {
dataType: "uint-64";
}
export declare function readUInt64(): ReadUInt64Instruction;
export interface ReadInt64Instruction extends BasicReadInstruction {
export declare class ReadInt64Instruction extends BasicReadInstruction<"int-64"> {
dataType: "int-64";
}
export declare function readInt64(): ReadInt64Instruction;
export interface ReadSingleInstruction extends BasicReadInstruction {
export declare class ReadSingleInstruction extends BasicReadInstruction<"single"> {
dataType: "single";
}
export declare function readSingle(): ReadSingleInstruction;
export interface ReadDoubleInstruction extends BasicReadInstruction {
export declare class ReadDoubleInstruction extends BasicReadInstruction<"double"> {
dataType: "double";
}
export declare function readDouble(): ReadDoubleInstruction;
export interface ReadCharsInstruction extends BasicReadInstruction {
dataType: "chars";
export declare class ReadCharsInstruction extends BasicReadInstruction<"chars"> {
length: number;
dataType: "chars";
constructor(length: number);
}
export declare function readChars(length: number): ReadCharsInstruction;
export interface ReadKleiStringInstruction extends BasicReadInstruction {
export declare class ReadKleiStringInstruction extends BasicReadInstruction<"klei-string"> {
dataType: "klei-string";
}
export declare function readKleiString(): ReadKleiStringInstruction;
export interface SkipBytesInstruction extends BasicReadInstruction {
dataType: "skip-bytes";
export declare class SkipBytesInstruction extends BasicReadInstruction<"skip-bytes"> {
length: number;
dataType: "skip-bytes";
constructor(length: number);
}
export declare function skipBytes(length: number): SkipBytesInstruction;
export interface ReadCompressedInstruction extends BasicReadInstruction {
export declare class ReadCompressedInstruction<TInner> extends BasicReadInstruction<"compressed"> {
parser: ParseIterator<TInner>;
dataType: "compressed";
parser: ParseIterator<any>;
constructor(parser: ParseIterator<TInner>);
}
export declare function readCompressed(parser: ParseIterator<any>): ReadCompressedInstruction;
export interface GetReaderPosition extends BasicReadInstruction {
export declare class GetReaderPosition extends BasicReadInstruction<"reader-position"> {
dataType: "reader-position";
}
export declare function getReaderPosition(): GetReaderPosition;
export declare type ReadInstruction = ReadByteInstruction | ReadSByteInstruction | ReadBytesInstruction | ReadUInt16Instruction | ReadInt16Instruction | ReadUInt32Instruction | ReadInt32Instruction | ReadUInt64Instruction | ReadInt64Instruction | ReadSingleInstruction | ReadDoubleInstruction | ReadCharsInstruction | ReadKleiStringInstruction | SkipBytesInstruction | ReadCompressedInstruction | GetReaderPosition;
export declare type ReadDataTypes = ReadInstruction["dataType"];
export type ReadInstruction = ReadByteInstruction | ReadSByteInstruction | ReadBytesInstruction | ReadUInt16Instruction | ReadInt16Instruction | ReadUInt32Instruction | ReadInt32Instruction | ReadUInt64Instruction | ReadInt64Instruction | ReadSingleInstruction | ReadDoubleInstruction | ReadCharsInstruction | ReadKleiStringInstruction | SkipBytesInstruction | ReadCompressedInstruction<any> | GetReaderPosition;
export declare function isReadInstruction(value: any): value is ReadInstruction;
6 changes: 3 additions & 3 deletions dts/parser/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface ParserInstruction {
type: string;
isMeta?: boolean;
export declare abstract class ParserInstruction {
abstract type: string;
abstract isMeta: boolean;
}
export declare function isMetaInstruction(inst: ParserInstruction): boolean;
4 changes: 2 additions & 2 deletions dts/parser/unparse/unparser.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataWriter } from "../../binary-serializer";
export declare type UnparseIterator = Generator<any, any, any>;
export declare type UnparseInterceptor = (value: any) => any;
export type UnparseIterator = Generator<any, any, any>;
export type UnparseInterceptor = (value: any) => any;
export declare function unparse<T>(writer: DataWriter, unparser: UnparseIterator, interceptor?: UnparseInterceptor): T;
4 changes: 2 additions & 2 deletions dts/parser/unparse/write-instructions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ export interface WriteCompressedInstruction extends BasicWriteInstruction {
unparser: UnparseIterator;
}
export declare function writeCompressed(unparser: UnparseIterator): WriteCompressedInstruction;
export declare type WriteInstruction = WriteByteInstruction | WriteSByteInstruction | WriteBytesInstruction | WriteUInt16Instruction | WriteInt16Instruction | WriteUInt32Instruction | WriteInt32Instruction | WriteUInt64Instruction | WriteInt64Instruction | WriteSingleInstruction | WriteDoubleInstruction | WriteCharsInstruction | WriteKleiStringInstruction | GetWriterPositionInstruction | WriteDataLengthBeginInstruction | WriteDataLengthEndInstruction | WriteCompressedInstruction;
export declare type WriteDataTypes = WriteInstruction["dataType"];
export type WriteInstruction = WriteByteInstruction | WriteSByteInstruction | WriteBytesInstruction | WriteUInt16Instruction | WriteInt16Instruction | WriteUInt32Instruction | WriteInt32Instruction | WriteUInt64Instruction | WriteInt64Instruction | WriteSingleInstruction | WriteDoubleInstruction | WriteCharsInstruction | WriteKleiStringInstruction | GetWriterPositionInstruction | WriteDataLengthBeginInstruction | WriteDataLengthEndInstruction | WriteCompressedInstruction;
export type WriteDataTypes = WriteInstruction["dataType"];
export declare function isWriteInstruction(value: any): value is WriteInstruction;
5 changes: 3 additions & 2 deletions dts/progress/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ParserInstruction } from "../parser/types";
export interface ProgressInstruction extends ParserInstruction {
export declare class ProgressInstruction implements ParserInstruction {
message: string;
type: "progress";
isMeta: true;
message: string;
constructor(message: string);
}
2 changes: 1 addition & 1 deletion dts/save-structure/const-data/accessories.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Accessory {
};
}
export declare const AccessoryTypes: ("body" | "hat" | "hat_hair" | "hair_always" | "hair" | "headshape" | "eyes" | "mouth" | "neck" | "arm")[];
export declare type AccessoryType = typeof AccessoryTypes extends (infer R)[] ? R : never;
export type AccessoryType = typeof AccessoryTypes extends (infer R)[] ? R : never;
export declare const EyeAccessoryNames: ("eyes_001" | "eyes_002" | "eyes_003" | "eyes_004" | "eyes_005")[];
export declare const HeadshapeAccessoryNames: ("headshape_001" | "headshape_002" | "headshape_003" | "headshape_004")[];
export declare const MouthAccessoryNames: ("mouth_001" | "mouth_002" | "mouth_003" | "mouth_004")[];
Expand Down
2 changes: 1 addition & 1 deletion dts/save-structure/const-data/geysers/geyser-type.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HashedString } from "../../data-types";
export declare const GeyserTypeNames: readonly ["steam", "hot_steam", "hot_water", "slush_water", "filthy_water", "salt_water", "small_volcano", "big_volcano", "liquid_co2", "hot_co2", "hot_hydrogen", "hot_po2", "slimy_po2", "chlorine_gas", "methane", "molten_copper", "molten_iron", "molten_gold", "oil_drip"];
export declare type GeyserType = HashedString;
export type GeyserType = HashedString;
export declare const GeyserType: import("../../data-types").HashedStringEnum<"steam" | "hot_steam" | "hot_water" | "slush_water" | "filthy_water" | "salt_water" | "small_volcano" | "big_volcano" | "liquid_co2" | "hot_co2" | "hot_hydrogen" | "hot_po2" | "slimy_po2" | "chlorine_gas" | "methane" | "molten_copper" | "molten_iron" | "molten_gold" | "oil_drip">;
2 changes: 1 addition & 1 deletion dts/save-structure/const-data/skills/skill-group.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HashedString } from "../../data-types";
export declare const MinionSkillGroupNames: ("Farming" | "Ranching" | "Mining" | "Cooking" | "Art" | "Building" | "Management" | "Research" | "Suits" | "Hauling" | "Technicals" | "MedicalAid" | "Basekeeping")[];
export declare type MinionSkillGroup = HashedString;
export type MinionSkillGroup = HashedString;
export declare const MinionSkillGroup: import("../../data-types").HashedStringEnum<"Farming" | "Ranching" | "Mining" | "Cooking" | "Art" | "Building" | "Management" | "Research" | "Suits" | "Hauling" | "Technicals" | "MedicalAid" | "Basekeeping">;
Loading