Skip to content

Commit 7f121dc

Browse files
committed
Upgrade deps, cleanup codebase, make web builds less terrible (they're still terrible), add vectors, add null terminated string.
1 parent 43c0ae2 commit 7f121dc

19 files changed

Lines changed: 937 additions & 209 deletions

.idea/.gitignore

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

.idea/vcs.xml

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

base/BufferShim.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ class BrowserBuffer {
88
public buffer:Uint8Array;
99
private dataView:DataView;
1010

11-
public constructor(dataOrSize: Array<number> | ArrayBufferLike | number) {
11+
public constructor(dataOrSize: Array<number> | ArrayBufferLike | number | Uint8Array) {
1212
if (typeof(dataOrSize) === "number") {
1313
this.buffer = new Uint8Array(dataOrSize);
1414
} else if (typeof(dataOrSize) === "object") {
15+
// @ts-ignore it'll be fine, trust.
1516
this.buffer = new Uint8Array(dataOrSize); // Convert whatever comes in to a Uint8Array.
1617
} else {
1718
this.buffer = new Uint8Array(0); // Fallback
@@ -377,7 +378,7 @@ class BrowserBuffer {
377378
}
378379
}
379380

380-
export function getBufferClass() : BufferConstructor {
381+
export default function getBufferClass() : BufferConstructor {
381382
if (typeof(Buffer) === "undefined") {
382383
// @ts-ignore
383384
return BrowserBuffer;

base/ReaderBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
22
// Check LICENSE in repository root for more information.
33

4-
export class ReaderBase {
4+
export default class ReaderBase {
55
public buffer:Buffer;
66
public offset:number;
77

base/Vec2.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
export default class Vec2 {
2+
public x: number;
3+
public y: number;
4+
5+
public constructor(x: number, y: number) {
6+
this.x = x;
7+
this.y = y;
8+
}
9+
10+
public static From(other: Vec2) {
11+
return new Vec2(other.x, other.y);
12+
}
13+
14+
public set(other: Vec2) {
15+
this.x = other.x;
16+
this.y = other.y;
17+
}
18+
19+
public set_d(x: number, y: number) {
20+
this.x = x;
21+
this.y = y;
22+
}
23+
24+
public add(other: Vec2) {
25+
this.x += other.x;
26+
this.y += other.y;
27+
}
28+
29+
public add_d(x: number, y: number) {
30+
this.x += x;
31+
this.y += y;
32+
}
33+
34+
public sub(other: Vec2) {
35+
this.x -= other.x;
36+
this.y -= other.y;
37+
}
38+
39+
public sub_d(x: number, y: number) {
40+
this.x -= x;
41+
this.y -= y;
42+
}
43+
44+
public mult(other: Vec2) {
45+
this.x *= other.x;
46+
this.y *= other.y;
47+
}
48+
49+
public mult_d(x: number, y: number) {
50+
this.x *= x;
51+
this.y *= y;
52+
}
53+
54+
public div(other: Vec2) {
55+
this.x /= other.x;
56+
this.y /= other.y;
57+
}
58+
59+
public div_d(x: number, y: number) {
60+
this.x /= x;
61+
this.y /= y;
62+
}
63+
64+
public dot(other: Vec2) {
65+
return this.x * other.x + this.y * other.y;
66+
}
67+
68+
public dot_d(x: number, y: number) {
69+
return this.x * x + this.y * y;
70+
}
71+
72+
public get length() {
73+
return Math.sqrt(this.dot(this));
74+
}
75+
}

base/Vec3.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
export default class Vec3 {
2+
public x: number;
3+
public y: number;
4+
public z: number;
5+
6+
public constructor(x: number, y: number, z: number) {
7+
this.x = x;
8+
this.y = y;
9+
this.z = z;
10+
}
11+
12+
public static From(other: Vec3) {
13+
return new Vec3(other.x, other.y, other.z);
14+
}
15+
16+
public set(other: Vec3) {
17+
this.x = other.x;
18+
this.y = other.y;
19+
this.z = other.z;
20+
}
21+
22+
public set_d(x: number, y: number, z: number) {
23+
this.x = x;
24+
this.y = y;
25+
this.z = z;
26+
}
27+
28+
public add(other: Vec3) {
29+
this.x += other.x;
30+
this.y += other.y;
31+
this.z += other.z;
32+
}
33+
34+
public add_d(x: number, y: number, z: number) {
35+
this.x += x;
36+
this.y += y;
37+
this.z += z;
38+
}
39+
40+
public sub(other: Vec3) {
41+
this.x -= other.x;
42+
this.y -= other.y;
43+
this.z -= other.z;
44+
}
45+
46+
public sub_d(x: number, y: number, z: number) {
47+
this.x -= x;
48+
this.y -= y;
49+
this.z -= z;
50+
}
51+
52+
public mult(other: Vec3) {
53+
this.x *= other.x;
54+
this.y *= other.y;
55+
this.z *= other.z;
56+
}
57+
58+
public mult_d(x: number, y: number, z: number) {
59+
this.x *= x;
60+
this.y *= y;
61+
this.z *= z;
62+
}
63+
64+
public div(other: Vec3) {
65+
this.x /= other.x;
66+
this.y /= other.y;
67+
this.z /= other.z;
68+
}
69+
70+
public div_d(x: number, y: number, z: number) {
71+
this.x /= x;
72+
this.y /= y;
73+
this.z /= z;
74+
}
75+
76+
public dot(other: Vec3) {
77+
return this.x * other.x + this.y * other.y + this.z * other.z;
78+
}
79+
80+
public dot_d(x: number, y: number, z: number) {
81+
return this.x * x + this.y * y + this.z * z;
82+
}
83+
84+
public cross(other: Vec3) {
85+
return new Vec3(this.y * other.z - this.z * other.y, this.z * other.x - this.x * other.z, this.x * other.y - this.y * other.x);
86+
}
87+
88+
public cross_d(x: number, y: number, z: number) {
89+
return new Vec3(this.y * z - this.z * y, this.z * x - this.x * z, this.x * y - this.y * x);
90+
}
91+
92+
public get length() {
93+
return Math.sqrt(this.dot(this));
94+
}
95+
}

base/WriterBase.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
22
// Check LICENSE in repository root for more information.
33

4-
import { getBufferClass } from "./BufferShim";
4+
import getBufferClass from "./BufferShim";
55

6-
export class WriterBase {
6+
export default class WriterBase {
77
public buffer:Buffer;
88
public offset:number;
99
public readonly resizable:boolean;
@@ -125,4 +125,25 @@ export class WriterBase {
125125

126126
return this;
127127
}
128+
129+
public writeCString(value: string) {
130+
let buffer: Buffer;
131+
if (this.resizable) {
132+
buffer = getBufferClass().alloc(value.length);
133+
} else {
134+
buffer = this.buffer;
135+
}
136+
137+
for (let i = 0; i < value.length; i++) {
138+
buffer.writeUInt8(value.charCodeAt(i), i);
139+
}
140+
141+
if (this.resizable) {
142+
this.writeBuffer(buffer);
143+
}
144+
145+
this.writeUByte(0) // null
146+
147+
return this;
148+
}
128149
}

index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
22
// Check LICENSE in repository root for more information.
33

4-
import { IReader } from "./readers/IReader";
5-
import { IWriter } from "./writers/IWriter";
6-
import { ReaderBE } from "./readers/ReaderBE";
7-
import { ReaderLE } from "./readers/ReaderLE";
8-
import { WriterBE } from "./writers/WriterBE";
9-
import { WriterLE } from "./writers/WriterLE";
4+
import IReader from "./readers/IReader";
5+
import IWriter from "./writers/IWriter";
6+
import ReaderBE from "./readers/ReaderBE";
7+
import ReaderLE from "./readers/ReaderLE";
8+
import WriterBE from "./writers/WriterBE";
9+
import WriterLE from "./writers/WriterLE";
1010

1111
export enum Endian {
1212
LE,

0 commit comments

Comments
 (0)