@etothepii/satisfactory-file-parser 0.0.10 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/build/index.d.ts +15 -0
  2. package/build/index.js +49 -0
  3. package/build/parser/blueprint-reader.d.ts +18 -0
  4. package/build/parser/blueprint-reader.js +136 -0
  5. package/build/parser/blueprint-writer.d.ts +21 -0
  6. package/build/parser/blueprint-writer.js +69 -0
  7. package/build/parser/byte/alignment.enum.d.ts +4 -0
  8. package/build/parser/byte/alignment.enum.js +8 -0
  9. package/build/parser/byte/byte-reader.class.d.ts +38 -0
  10. package/build/parser/byte/byte-reader.class.js +132 -0
  11. package/build/parser/byte/byte-writer.class.d.ts +38 -0
  12. package/build/parser/byte/byte-writer.class.js +149 -0
  13. package/build/parser/error/parser.error.d.ts +12 -0
  14. package/build/parser/error/parser.error.js +28 -0
  15. package/build/parser/parser.d.ts +44 -0
  16. package/build/parser/parser.js +84 -0
  17. package/build/parser/satisfactory/blueprint/blueprint.d.ts +20 -0
  18. package/build/parser/satisfactory/blueprint/blueprint.js +2 -0
  19. package/build/parser/satisfactory/level.class.d.ts +21 -0
  20. package/build/parser/satisfactory/level.class.js +148 -0
  21. package/build/parser/satisfactory/objects/DataFields.d.ts +13 -0
  22. package/build/parser/satisfactory/objects/DataFields.js +249 -0
  23. package/build/parser/satisfactory/objects/ObjectReference.d.ts +9 -0
  24. package/build/parser/satisfactory/objects/ObjectReference.js +17 -0
  25. package/build/parser/satisfactory/objects/Property.d.ts +270 -0
  26. package/build/parser/satisfactory/objects/Property.js +1150 -0
  27. package/build/parser/satisfactory/objects/SaveComponent.d.ts +16 -0
  28. package/build/parser/satisfactory/objects/SaveComponent.js +31 -0
  29. package/build/parser/satisfactory/objects/SaveEntity.d.ts +25 -0
  30. package/build/parser/satisfactory/objects/SaveEntity.js +70 -0
  31. package/build/parser/satisfactory/objects/SaveObject.d.ts +14 -0
  32. package/build/parser/satisfactory/objects/SaveObject.js +29 -0
  33. package/build/parser/satisfactory/satisfactory-save.d.ts +57 -0
  34. package/build/parser/satisfactory/satisfactory-save.js +11 -0
  35. package/build/parser/satisfactory/static-data.d.ts +7 -0
  36. package/build/parser/satisfactory/static-data.js +2 -0
  37. package/build/parser/satisfactory/structs/util.types.d.ts +40 -0
  38. package/build/parser/satisfactory/structs/util.types.js +95 -0
  39. package/build/parser/save-reader.d.ts +43 -0
  40. package/build/parser/save-reader.js +251 -0
  41. package/build/parser/save-writer.d.ts +22 -0
  42. package/build/parser/save-writer.js +121 -0
  43. package/package.json +3 -3
  44. package/index.d.ts +0 -2356
  45. package/index.js +0 -2364
  46. package/rollup.config.mjs +0 -11
@@ -0,0 +1,15 @@
1
+ export { Level } from './parser/satisfactory/level.class';
2
+ export { DataFields } from './parser/satisfactory/objects/DataFields';
3
+ export { ObjectReference } from './parser/satisfactory/objects/ObjectReference';
4
+ export * from './parser/satisfactory/objects/Property';
5
+ export { SaveComponent } from './parser/satisfactory/objects/SaveComponent';
6
+ export { SaveEntity } from './parser/satisfactory/objects/SaveEntity';
7
+ export type * from './parser/satisfactory/satisfactory-save';
8
+ export { StaticData } from './parser/satisfactory/static-data';
9
+ export { BlueprintConfigReader, BlueprintReader } from './parser/blueprint-reader';
10
+ export { BlueprintConfigWriter, BlueprintWriter } from './parser/blueprint-writer';
11
+ export type * from './parser/satisfactory/structs/util.types';
12
+ export { SaveReader } from './parser/save-reader';
13
+ export { SaveWriter } from './parser/save-writer';
14
+ export { CompressionLibraryError, CorruptSaveError, ParserError, UnsupportedVersionError } from './parser/error/parser.error';
15
+ export { SaveParser } from './parser/parser';
package/build/index.js ADDED
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.SaveParser = exports.UnsupportedVersionError = exports.ParserError = exports.CorruptSaveError = exports.CompressionLibraryError = exports.SaveWriter = exports.SaveReader = exports.BlueprintWriter = exports.BlueprintConfigWriter = exports.BlueprintReader = exports.BlueprintConfigReader = exports.SaveEntity = exports.SaveComponent = exports.ObjectReference = exports.DataFields = exports.Level = void 0;
18
+ // types & classes for convenience
19
+ var level_class_1 = require("./parser/satisfactory/level.class");
20
+ Object.defineProperty(exports, "Level", { enumerable: true, get: function () { return level_class_1.Level; } });
21
+ var DataFields_1 = require("./parser/satisfactory/objects/DataFields");
22
+ Object.defineProperty(exports, "DataFields", { enumerable: true, get: function () { return DataFields_1.DataFields; } });
23
+ var ObjectReference_1 = require("./parser/satisfactory/objects/ObjectReference");
24
+ Object.defineProperty(exports, "ObjectReference", { enumerable: true, get: function () { return ObjectReference_1.ObjectReference; } });
25
+ __exportStar(require("./parser/satisfactory/objects/Property"), exports);
26
+ var SaveComponent_1 = require("./parser/satisfactory/objects/SaveComponent");
27
+ Object.defineProperty(exports, "SaveComponent", { enumerable: true, get: function () { return SaveComponent_1.SaveComponent; } });
28
+ var SaveEntity_1 = require("./parser/satisfactory/objects/SaveEntity");
29
+ Object.defineProperty(exports, "SaveEntity", { enumerable: true, get: function () { return SaveEntity_1.SaveEntity; } });
30
+ // should better be removed in a future update to prevent shenanigans.
31
+ var blueprint_reader_1 = require("./parser/blueprint-reader");
32
+ Object.defineProperty(exports, "BlueprintConfigReader", { enumerable: true, get: function () { return blueprint_reader_1.BlueprintConfigReader; } });
33
+ Object.defineProperty(exports, "BlueprintReader", { enumerable: true, get: function () { return blueprint_reader_1.BlueprintReader; } });
34
+ var blueprint_writer_1 = require("./parser/blueprint-writer");
35
+ Object.defineProperty(exports, "BlueprintConfigWriter", { enumerable: true, get: function () { return blueprint_writer_1.BlueprintConfigWriter; } });
36
+ Object.defineProperty(exports, "BlueprintWriter", { enumerable: true, get: function () { return blueprint_writer_1.BlueprintWriter; } });
37
+ var save_reader_1 = require("./parser/save-reader");
38
+ Object.defineProperty(exports, "SaveReader", { enumerable: true, get: function () { return save_reader_1.SaveReader; } });
39
+ var save_writer_1 = require("./parser/save-writer");
40
+ Object.defineProperty(exports, "SaveWriter", { enumerable: true, get: function () { return save_writer_1.SaveWriter; } });
41
+ // errors
42
+ var parser_error_1 = require("./parser/error/parser.error");
43
+ Object.defineProperty(exports, "CompressionLibraryError", { enumerable: true, get: function () { return parser_error_1.CompressionLibraryError; } });
44
+ Object.defineProperty(exports, "CorruptSaveError", { enumerable: true, get: function () { return parser_error_1.CorruptSaveError; } });
45
+ Object.defineProperty(exports, "ParserError", { enumerable: true, get: function () { return parser_error_1.ParserError; } });
46
+ Object.defineProperty(exports, "UnsupportedVersionError", { enumerable: true, get: function () { return parser_error_1.UnsupportedVersionError; } });
47
+ // facade
48
+ var parser_1 = require("./parser/parser");
49
+ Object.defineProperty(exports, "SaveParser", { enumerable: true, get: function () { return parser_1.SaveParser; } });
@@ -0,0 +1,18 @@
1
+ import { ByteReader } from "./byte/byte-reader.class";
2
+ import { BlueprintConfig, BlueprintHeader } from "./satisfactory/blueprint/blueprint";
3
+ import { SaveComponent } from "./satisfactory/objects/SaveComponent";
4
+ import { SaveEntity } from "./satisfactory/objects/SaveEntity";
5
+ import { ChunkCompressionInfo } from "./satisfactory/satisfactory-save";
6
+ export declare class BlueprintReader extends ByteReader {
7
+ compressionInfo: ChunkCompressionInfo;
8
+ constructor(bluePrintBuffer: ArrayBuffer);
9
+ static ReadHeader(reader: ByteReader): BlueprintHeader;
10
+ inflateChunks(): any;
11
+ static ParseObjects(reader: ByteReader): (SaveEntity | SaveComponent)[];
12
+ }
13
+ export declare class BlueprintConfigReader extends ByteReader {
14
+ bluePrintConfigBuffer: ArrayBuffer;
15
+ constructor(bluePrintConfigBuffer: ArrayBuffer);
16
+ parse: () => BlueprintConfig;
17
+ static ParseConfig(reader: ByteReader): BlueprintConfig;
18
+ }
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BlueprintConfigReader = exports.BlueprintReader = void 0;
7
+ const pako_1 = __importDefault(require("pako"));
8
+ const alignment_enum_1 = require("./byte/alignment.enum");
9
+ const byte_reader_class_1 = require("./byte/byte-reader.class");
10
+ const parser_error_1 = require("./error/parser.error");
11
+ const level_class_1 = require("./satisfactory/level.class");
12
+ const util_types_1 = require("./satisfactory/structs/util.types");
13
+ class BlueprintReader extends byte_reader_class_1.ByteReader {
14
+ constructor(bluePrintBuffer) {
15
+ super(bluePrintBuffer, alignment_enum_1.Alignment.LITTLE_ENDIAN);
16
+ this.compressionInfo = {
17
+ packageFileTag: 0,
18
+ maxChunkContentSize: 0,
19
+ chunkHeaderSize: 48
20
+ };
21
+ }
22
+ static ReadHeader(reader) {
23
+ const unk = reader.readBytes(3 * 4); // 02 00 00 00, 24 00 00 00, 7F 3B 03 00 - always
24
+ const positionThingOrWhat = (0, util_types_1.ParseVec3)(reader); // 04 00 00 00, 04 00 00 00, 04 00 00 00 - always
25
+ // list of item costs.
26
+ let itemTypeCount = reader.readInt32();
27
+ const itemCosts = new Array(itemTypeCount).fill(['', 0]);
28
+ for (let i = 0; i < itemTypeCount; i++) {
29
+ let indexOrWhat = reader.readInt32();
30
+ let itemPathName = reader.readString();
31
+ let itemCount = reader.readInt32();
32
+ itemCosts[i] = [itemPathName, itemCount];
33
+ }
34
+ // list of recipes
35
+ let recipeCount = reader.readInt32();
36
+ const recipeRefs = new Array(recipeCount).fill('');
37
+ for (let i = 0; i < recipeCount; i++) {
38
+ let indexOrWhat = reader.readInt32();
39
+ const recipeName = reader.readString();
40
+ recipeRefs[i] = recipeName;
41
+ }
42
+ return {
43
+ recipeReferences: recipeRefs,
44
+ itemCosts
45
+ };
46
+ }
47
+ inflateChunks() {
48
+ // free memory
49
+ this.fileBuffer = this.fileBuffer.slice(this.currentByte);
50
+ this.handledByte = 0;
51
+ this.currentByte = 0;
52
+ this.maxByte = this.fileBuffer.byteLength;
53
+ let currentChunks = [];
54
+ let totalUncompressedBodySize = 0;
55
+ // read while we can handle
56
+ while (this.handledByte < this.maxByte) {
57
+ // Read chunk info size...
58
+ let chunkHeader = new DataView(this.fileBuffer.slice(0, this.compressionInfo.chunkHeaderSize));
59
+ this.currentByte = this.compressionInfo.chunkHeaderSize;
60
+ this.handledByte += this.compressionInfo.chunkHeaderSize;
61
+ if (this.compressionInfo.packageFileTag <= 0) {
62
+ this.compressionInfo.packageFileTag = chunkHeader.getUint32(0, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
63
+ }
64
+ if (this.compressionInfo.maxChunkContentSize <= 0) {
65
+ this.compressionInfo.maxChunkContentSize = chunkHeader.getInt32(8, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN); //00 00 02 00 = 131072
66
+ }
67
+ const chunkCompressedLength = chunkHeader.getInt32(32, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
68
+ const chunkUncompressedLength = chunkHeader.getInt32(40, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
69
+ totalUncompressedBodySize += chunkUncompressedLength;
70
+ const currentChunkSize = chunkCompressedLength;
71
+ let currentChunk = this.fileBuffer.slice(this.currentByte, this.currentByte + currentChunkSize);
72
+ this.handledByte += currentChunkSize;
73
+ this.currentByte += currentChunkSize;
74
+ // Free memory from previous chunk...
75
+ this.fileBuffer = this.fileBuffer.slice(this.currentByte);
76
+ this.currentByte = 0;
77
+ try {
78
+ // Inflate current chunk
79
+ let currentInflatedChunk = null;
80
+ currentInflatedChunk = pako_1.default.inflate(currentChunk);
81
+ currentChunks.push(currentInflatedChunk);
82
+ }
83
+ catch (err) {
84
+ throw new parser_error_1.ParserError('ParserError', 'An error occurred while calling pako inflate.' + err);
85
+ }
86
+ }
87
+ //TODO we can get rid of file buffer here.
88
+ // Create one big chunk out of the little chunks
89
+ let newChunkLength = currentChunks.map(cc => cc.length).reduce((prev, cur) => prev + cur);
90
+ const bigWholeChunk = new Uint8Array(newChunkLength);
91
+ let currentLength = 0;
92
+ for (let i = 0; i < currentChunks.length; i++) {
93
+ bigWholeChunk.set(currentChunks[i], currentLength);
94
+ currentLength += currentChunks[i].length;
95
+ }
96
+ // reset reader on this body content.
97
+ this.currentByte = 0;
98
+ this.maxByte = bigWholeChunk.buffer.byteLength;
99
+ this.bufferView = new DataView(bigWholeChunk.buffer);
100
+ return {
101
+ newChunkLength,
102
+ numChunks: currentChunks.length,
103
+ inflatedData: bigWholeChunk
104
+ };
105
+ }
106
+ static ParseObjects(reader) {
107
+ const totalBodyRestSize = reader.readInt32();
108
+ // object headers
109
+ const objectHeadersBinarySize = reader.readInt32();
110
+ let objects = [];
111
+ level_class_1.Level.ReadObjectHeaders(reader, objects, () => { });
112
+ // objects contents
113
+ level_class_1.Level.ReadObjectContents(reader, objects, 0, () => { });
114
+ return objects;
115
+ }
116
+ }
117
+ exports.BlueprintReader = BlueprintReader;
118
+ class BlueprintConfigReader extends byte_reader_class_1.ByteReader {
119
+ constructor(bluePrintConfigBuffer) {
120
+ super(bluePrintConfigBuffer, alignment_enum_1.Alignment.LITTLE_ENDIAN);
121
+ this.bluePrintConfigBuffer = bluePrintConfigBuffer;
122
+ this.parse = () => BlueprintConfigReader.ParseConfig(this);
123
+ }
124
+ static ParseConfig(reader) {
125
+ const unk = reader.readInt32(); // unknown, seems to always be 02.
126
+ const description = reader.readString();
127
+ const unk3 = reader.readInt32(); // iconID. default is 0x0E03 = 782
128
+ const colorMaybe = (0, util_types_1.ParseCol4RGBA)(reader); // LinearColor, which is 4x float32 as RGBA
129
+ return {
130
+ description,
131
+ color: colorMaybe,
132
+ iconID: unk3
133
+ };
134
+ }
135
+ }
136
+ exports.BlueprintConfigReader = BlueprintConfigReader;
@@ -0,0 +1,21 @@
1
+ import { SaveComponent } from "../parser/satisfactory/objects/SaveComponent";
2
+ import { SaveEntity } from "../parser/satisfactory/objects/SaveEntity";
3
+ import { ByteWriter } from "./byte/byte-writer.class";
4
+ import { BlueprintConfig, BlueprintHeader } from "./satisfactory/blueprint/blueprint";
5
+ import { ChunkCompressionInfo } from "./satisfactory/satisfactory-save";
6
+ export declare class BlueprintWriter extends ByteWriter {
7
+ constructor();
8
+ static SerializeHeader(writer: ByteWriter, header: BlueprintHeader): void;
9
+ generateChunks(compressionInfo: ChunkCompressionInfo, posAfterHeader: number, onBinaryBeforeCompressing: (binary: ArrayBuffer) => void, onHeader: (header: Uint8Array) => void, onChunk: (chunk: Uint8Array) => void, onFinish: (summary: {
10
+ errors: string[];
11
+ chunkSummary: {
12
+ uncompressedSize: number;
13
+ compressedSize: number;
14
+ }[];
15
+ }) => void): void;
16
+ static SerializeObjects(writer: ByteWriter, objects: (SaveEntity | SaveComponent)[]): void;
17
+ }
18
+ export declare class BlueprintConfigWriter extends ByteWriter {
19
+ constructor();
20
+ static SerializeConfig(writer: ByteWriter, config: BlueprintConfig): void;
21
+ }
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BlueprintConfigWriter = exports.BlueprintWriter = void 0;
4
+ const level_class_1 = require("../parser/satisfactory/level.class");
5
+ const util_types_1 = require("../parser/satisfactory/structs/util.types");
6
+ const alignment_enum_1 = require("./byte/alignment.enum");
7
+ const byte_writer_class_1 = require("./byte/byte-writer.class");
8
+ const parser_error_1 = require("./error/parser.error");
9
+ const save_writer_1 = require("./save-writer");
10
+ class BlueprintWriter extends byte_writer_class_1.ByteWriter {
11
+ constructor() {
12
+ super(alignment_enum_1.Alignment.LITTLE_ENDIAN);
13
+ }
14
+ static SerializeHeader(writer, header) {
15
+ writer.writeInt32(2); // unknown, seems to always be 02.
16
+ writer.writeInt32(36); // TODO - unknown what it is, but its constant so far.
17
+ writer.writeInt32(211839); // TODO - unknown what it is, but its constant so far.
18
+ // TODO - whatever those are, but they are constant so far.
19
+ writer.writeInt32(4);
20
+ writer.writeInt32(4);
21
+ writer.writeInt32(4);
22
+ // list of item costs.
23
+ writer.writeInt32(header.itemCosts.length);
24
+ for (const itemCost of header.itemCosts) {
25
+ writer.writeInt32(0); //TODO: unk or what
26
+ writer.writeString(itemCost[0]);
27
+ writer.writeInt32(itemCost[1]);
28
+ }
29
+ // list of recipes.
30
+ writer.writeInt32(header.recipeReferences.length);
31
+ for (const recipeReference of header.recipeReferences) {
32
+ writer.writeInt32(0); //TODO: unk or what
33
+ writer.writeString(recipeReference);
34
+ }
35
+ }
36
+ generateChunks(compressionInfo, posAfterHeader, onBinaryBeforeCompressing, onHeader, onChunk, onFinish) {
37
+ if (posAfterHeader <= 0) {
38
+ throw new parser_error_1.ParserError('ParserError', 'seems like this buffer has no header. Please write the header first before you can generate chunks.');
39
+ }
40
+ // send plain header first.
41
+ const header = new Uint8Array(this.bufferArray.slice(0, posAfterHeader));
42
+ onHeader(header);
43
+ // create save body
44
+ this.bufferArray = this.bufferArray.slice(posAfterHeader);
45
+ save_writer_1.SaveWriter.GenerateCompressedChunksFromData(this.bufferArray, compressionInfo, onBinaryBeforeCompressing, onChunk, onFinish, this.alignment);
46
+ }
47
+ static SerializeObjects(writer, objects) {
48
+ // object headers
49
+ const headersLenIndicator = writer.getBufferPosition();
50
+ writer.writeInt32(0);
51
+ level_class_1.Level.SerializeObjectHeaders(writer, objects);
52
+ writer.writeBinarySizeFromPosition(headersLenIndicator, headersLenIndicator + 4);
53
+ // objects contents
54
+ level_class_1.Level.SerializeObjectContents(writer, objects, 0);
55
+ }
56
+ }
57
+ exports.BlueprintWriter = BlueprintWriter;
58
+ class BlueprintConfigWriter extends byte_writer_class_1.ByteWriter {
59
+ constructor() {
60
+ super(alignment_enum_1.Alignment.LITTLE_ENDIAN);
61
+ }
62
+ static SerializeConfig(writer, config) {
63
+ writer.writeInt32(2); // unknown, seems to always be 02.
64
+ writer.writeString(config.description);
65
+ writer.writeInt32(config.iconID);
66
+ (0, util_types_1.SerializeCol4RGBA)(writer, config.color);
67
+ }
68
+ }
69
+ exports.BlueprintConfigWriter = BlueprintConfigWriter;
@@ -0,0 +1,4 @@
1
+ export declare enum Alignment {
2
+ BIG_ENDIAN = 0,
3
+ LITTLE_ENDIAN = 1
4
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Alignment = void 0;
4
+ var Alignment;
5
+ (function (Alignment) {
6
+ Alignment[Alignment["BIG_ENDIAN"] = 0] = "BIG_ENDIAN";
7
+ Alignment[Alignment["LITTLE_ENDIAN"] = 1] = "LITTLE_ENDIAN";
8
+ })(Alignment = exports.Alignment || (exports.Alignment = {}));
@@ -0,0 +1,38 @@
1
+ import { Alignment } from "./alignment.enum";
2
+ export declare abstract class ByteReader {
3
+ protected bufferView: DataView;
4
+ protected fileBuffer: ArrayBuffer;
5
+ protected alignment: Alignment;
6
+ protected currentByte: number;
7
+ protected handledByte: number;
8
+ protected maxByte: number;
9
+ protected lastStrRead: number;
10
+ constructor(fileBuffer: ArrayBuffer, alignment: Alignment);
11
+ /**
12
+ * Resets the reader on the given arraybuffer to start from the beginning.
13
+ * @param newFileBuffer the new array buffer to be read.
14
+ */
15
+ reset(newFileBuffer: ArrayBuffer): void;
16
+ skipBytes(byteLength?: number): void;
17
+ readByte(): number;
18
+ readBytes(count: number): Uint8Array;
19
+ readHex(hexLength: number): string;
20
+ readInt8(): number;
21
+ readUint8(): number;
22
+ readUint16(): number;
23
+ readInt32(): number;
24
+ readUint32(): number;
25
+ readLong(): bigint;
26
+ readFloat(): number;
27
+ readDouble(): number;
28
+ protected getStringInfo(): {
29
+ payload: string;
30
+ counter: number;
31
+ };
32
+ getBufferPosition: () => number;
33
+ getBufferSlice: (begin: number, end: number | undefined) => ArrayBuffer;
34
+ getBufferProgress: () => number;
35
+ getBufferLength: () => number;
36
+ getBuffer: () => ArrayBuffer;
37
+ readString(): string;
38
+ }
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ByteReader = void 0;
4
+ const alignment_enum_1 = require("./alignment.enum");
5
+ class ByteReader {
6
+ constructor(fileBuffer, alignment) {
7
+ this.currentByte = 0;
8
+ this.handledByte = 0;
9
+ this.maxByte = 0;
10
+ this.lastStrRead = 0;
11
+ this.getBufferPosition = () => this.currentByte;
12
+ this.getBufferSlice = (begin, end) => this.bufferView.buffer.slice(begin, end);
13
+ this.getBufferProgress = () => this.currentByte / this.bufferView.buffer.byteLength;
14
+ this.getBufferLength = () => this.bufferView.buffer.byteLength;
15
+ this.getBuffer = () => this.bufferView.buffer;
16
+ this.alignment = alignment;
17
+ this.reset(fileBuffer);
18
+ }
19
+ /**
20
+ * Resets the reader on the given arraybuffer to start from the beginning.
21
+ * @param newFileBuffer the new array buffer to be read.
22
+ */
23
+ reset(newFileBuffer) {
24
+ this.fileBuffer = newFileBuffer;
25
+ this.bufferView = new DataView(newFileBuffer, 0, Math.min(102400, this.fileBuffer.byteLength));
26
+ this.maxByte = newFileBuffer.byteLength;
27
+ this.currentByte = 0;
28
+ this.handledByte = 0;
29
+ }
30
+ /*
31
+ * Byte Manipulation
32
+ */
33
+ skipBytes(byteLength = 1) {
34
+ this.currentByte += byteLength;
35
+ }
36
+ readByte() {
37
+ // TODO can i not just readByte() ??
38
+ return parseInt(this.bufferView.getUint8(this.currentByte++).toString());
39
+ }
40
+ readBytes(count) {
41
+ return new Uint8Array(new Array(count).fill(0).map(pl => this.bufferView.getUint8(this.currentByte++)));
42
+ }
43
+ readHex(hexLength) {
44
+ let hexPart = [];
45
+ for (let i = 0; i < hexLength; i++) {
46
+ let currentHex = String.fromCharCode(this.bufferView.getUint8(this.currentByte++));
47
+ hexPart.push(currentHex);
48
+ }
49
+ return hexPart.join('');
50
+ }
51
+ readInt8() {
52
+ let data = this.bufferView.getInt8(this.currentByte++);
53
+ return data;
54
+ }
55
+ readUint8() {
56
+ let data = this.bufferView.getUint8(this.currentByte++);
57
+ return data;
58
+ }
59
+ readUint16() {
60
+ let data = this.bufferView.getUint16(this.currentByte, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
61
+ this.currentByte += 2;
62
+ return data;
63
+ }
64
+ readInt32() {
65
+ let data = this.bufferView.getInt32(this.currentByte, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
66
+ this.currentByte += 4;
67
+ return data;
68
+ }
69
+ readUint32() {
70
+ let data = this.bufferView.getUint32(this.currentByte, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
71
+ this.currentByte += 4;
72
+ return data;
73
+ }
74
+ readLong() {
75
+ let data = this.bufferView.getBigInt64(this.currentByte, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
76
+ this.currentByte += 8;
77
+ return data;
78
+ }
79
+ readFloat() {
80
+ let data = this.bufferView.getFloat32(this.currentByte, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
81
+ this.currentByte += 4;
82
+ return data;
83
+ }
84
+ readDouble() {
85
+ let data = this.bufferView.getFloat64(this.currentByte, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
86
+ this.currentByte += 8;
87
+ return data;
88
+ }
89
+ getStringInfo() {
90
+ let payload = '';
91
+ let counter = this.currentByte;
92
+ try {
93
+ let strLength = this.bufferView.getInt32(this.currentByte, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
94
+ counter += 4;
95
+ payload = new Array(strLength - 1).fill('').map(c => String.fromCharCode(this.bufferView.getUint8(counter++))).join('');
96
+ }
97
+ catch (error) { }
98
+ this.currentByte -= counter;
99
+ return { payload, counter };
100
+ }
101
+ readString() {
102
+ let strLength = this.readInt32();
103
+ this.lastStrRead = strLength;
104
+ let startBytes = this.currentByte;
105
+ if (strLength === 0) {
106
+ return '';
107
+ }
108
+ // Range error!
109
+ if (strLength > (this.bufferView.buffer.byteLength - this.currentByte)) {
110
+ let errorMessage = `Cannot read string of length ${strLength} at position ${this.currentByte} as it exceeds the end at ${this.bufferView.buffer.byteLength}`;
111
+ throw new Error(errorMessage);
112
+ }
113
+ // it uses UTF16 if text is non-ascii, even if it would fit into UTF8.
114
+ if (strLength < 0) {
115
+ const string = new Array(-strLength - 1).fill('').map(c => String.fromCharCode(this.readUint16()));
116
+ this.currentByte += 2;
117
+ return string.join('');
118
+ }
119
+ //default UTF-8
120
+ try {
121
+ const string = new Array(strLength - 1).fill('').map(c => String.fromCharCode(this.readUint8()));
122
+ this.currentByte += 1;
123
+ return string.join('');
124
+ }
125
+ catch (error) {
126
+ let errorMessage = `Cannot read UTF8 string of length ${strLength} at position ${this.currentByte}.`;
127
+ console.log(errorMessage, error);
128
+ throw error;
129
+ }
130
+ }
131
+ }
132
+ exports.ByteReader = ByteReader;
@@ -0,0 +1,38 @@
1
+ import { Alignment } from "./alignment.enum";
2
+ export declare abstract class ByteWriter {
3
+ protected alignment: Alignment;
4
+ protected bufferArray: ArrayBuffer;
5
+ protected bufferView: DataView;
6
+ protected currentByte: number;
7
+ constructor(alignment: Alignment, bufferSize?: number);
8
+ skipBytes(count?: number): void;
9
+ jumpTo(pos: number): void;
10
+ writeByte(value: number): void;
11
+ writeBytesArray(bytes: number[]): void;
12
+ writeBytes(bytes: Uint8Array): void;
13
+ writeInt8(value: number): void;
14
+ writeUint8(value: number): void;
15
+ writeInt16(value: number): void;
16
+ writeUint16(value: number): void;
17
+ writeInt32(value: number): void;
18
+ writeUint32(value: number): void;
19
+ writeInt64(value: bigint): void;
20
+ writeUint64(value: bigint): void;
21
+ writeFloat(value: number): void;
22
+ writeDouble(value: number): void;
23
+ writeString(value: string): void;
24
+ static IsASCIICompatible: (value: string) => boolean;
25
+ getBufferPosition: () => number;
26
+ getBufferSlice: (start: number, end?: number) => ArrayBuffer;
27
+ writeBinarySizeFromPosition(lenIndicatorPos: number, start: number): void;
28
+ /**
29
+ * automatically extends the current buffer if the given space exceeds the available rest capacity of the current buffer.
30
+ * @param countNeededBytes the needed space
31
+ * @param factor how big the appended buffer should be in comparison to the current one. Values >= 1 make sense.
32
+ */
33
+ protected extendBufferIfNeeded(countNeededBytes: number, factor?: number): void;
34
+ protected truncateBuffer(): void;
35
+ endWriting(): ArrayBuffer;
36
+ static ToInt32(num: number): Uint8Array;
37
+ static AppendBuffer(buffer1: ArrayBuffer, buffer2: ArrayBuffer): ArrayBuffer;
38
+ }