Skip to content

Commit 5d63042

Browse files
authored
Merge pull request #3 from mdingena/refactor-component-types
Refactor component types
2 parents 2a6037c + 4e4cc27 commit 5d63042

14 files changed

+78
-100
lines changed

src/components/index.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
import * as transcoders from './transcoders';
22

3-
export { ComponentVersion } from './ComponentVersion';
4-
53
export type ComponentName = keyof typeof transcoders;
64

5+
export type BasicDecay = transcoders.BasicDecay.BasicDecay;
6+
export type DurabilityModule = transcoders.DurabilityModule.DurabilityModule;
7+
export type HeatSourceBase = transcoders.HeatSourceBase.HeatSourceBase;
8+
export type LiquidContainer = transcoders.LiquidContainer.LiquidContainer;
9+
export type NetworkRigidbody = transcoders.NetworkRigidbody.NetworkRigidbody;
10+
export type PhysicalMaterialPart = transcoders.PhysicalMaterialPart.PhysicalMaterialPart;
11+
export type Pickup = transcoders.Pickup.Pickup;
12+
export type PickupDock = transcoders.PickupDock.PickupDock;
13+
export type PopulationSpawnArea = transcoders.PopulationSpawnArea.PopulationSpawnArea;
14+
export type SpawnArea = transcoders.SpawnArea.SpawnArea;
15+
export type StatManager = transcoders.StatManager.StatManager;
16+
export type WoodcutTree = transcoders.WoodcutTree.WoodcutTree;
17+
718
export type KnownComponent =
8-
| transcoders.BasicDecay.Component
9-
| transcoders.DurabilityModule.Component
10-
| transcoders.HeatSourceBase.Component
11-
| transcoders.LiquidContainer.Component
12-
| transcoders.NetworkRigidbody.Component
13-
| transcoders.PhysicalMaterialPart.Component
14-
| transcoders.Pickup.Component
15-
| transcoders.PickupDock.Component
16-
| transcoders.PopulationSpawnArea.Component
17-
| transcoders.SpawnArea.Component
18-
| transcoders.StatManager.Component
19-
| transcoders.WoodcutTree.Component;
19+
| BasicDecay
20+
| DurabilityModule
21+
| HeatSourceBase
22+
| LiquidContainer
23+
| NetworkRigidbody
24+
| PhysicalMaterialPart
25+
| Pickup
26+
| PickupDock
27+
| PopulationSpawnArea
28+
| SpawnArea
29+
| StatManager
30+
| WoodcutTree;
2031

2132
export type UnknownComponent = { hash: number; data: string };
2233

2334
export type Component = KnownComponent | UnknownComponent[];
2435

36+
export { ComponentVersion } from './ComponentVersion';
37+
2538
export { transcoders };

src/components/transcoders/basicDecay.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import { ComponentHash } from '../../ComponentHash';
22
import { BinaryReader, createBinaryWriter } from '../../utils';
33

4-
export const HASH = ComponentHash.BasicDecay;
5-
export const VERSION = 3;
6-
74
const HUNDRED_YEARS_TICKS = 31557600000000000;
85

9-
export type Component = {
6+
export type BasicDecay = {
107
isDisabled?: boolean;
118
timelineEntry?: number;
129
};
1310

14-
export const decode = (reader: BinaryReader): Component => ({
11+
export const decode = (reader: BinaryReader): BasicDecay => ({
1512
isDisabled: reader.boolean(),
1613
timelineEntry: reader.uLong()
1714
});
1815

19-
export const encode = ({ isDisabled = true, timelineEntry = HUNDRED_YEARS_TICKS }: Component): string => {
16+
export const encode = ({ isDisabled = true, timelineEntry = HUNDRED_YEARS_TICKS }: BasicDecay): string => {
2017
const writer = createBinaryWriter();
2118

2219
/* Component hash. */

src/components/transcoders/durabilityModule.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import { ComponentHash } from '../../ComponentHash';
22
import { BinaryReader, createBinaryWriter } from '../../utils';
33

4-
export const HASH = ComponentHash.DurabilityModule;
5-
export const VERSION = 1;
6-
7-
export type Component = {
4+
export type DurabilityModule = {
85
integrity?: number;
96
};
107

11-
export const decode = (reader: BinaryReader): Component => ({
8+
export const decode = (reader: BinaryReader): DurabilityModule => ({
129
integrity: reader.float()
1310
});
1411

15-
export const encode = ({ integrity = 1 }: Component): string => {
12+
export const encode = ({ integrity = 1 }: DurabilityModule): string => {
1613
const writer = createBinaryWriter();
1714

1815
/* Component hash. */

src/components/transcoders/heatSourceBase.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import { ComponentHash } from '../../ComponentHash';
22
import { BinaryReader, createBinaryWriter } from '../../utils';
33

4-
export const HASH = ComponentHash.HeatSourceBase;
5-
export const VERSION = 2;
6-
74
const HUNDRED_YEARS_TICKS = 31557600000000000;
85

9-
export type Component = {
6+
export type HeatSourceBase = {
107
isLit?: boolean;
118
progress?: number;
129
time?: number;
1310
};
1411

15-
export const decode = (reader: BinaryReader): Component => ({
12+
export const decode = (reader: BinaryReader): HeatSourceBase => ({
1613
isLit: reader.boolean(),
1714
progress: reader.float(),
1815
time: reader.uLong()
1916
});
2017

21-
export const encode = ({ isLit = true, progress = 0, time = HUNDRED_YEARS_TICKS }: Component): string => {
18+
export const encode = ({ isLit = true, progress = 0, time = HUNDRED_YEARS_TICKS }: HeatSourceBase): string => {
2219
const writer = createBinaryWriter();
2320

2421
/* Component hash. */

src/components/transcoders/liquidContainer.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { ComponentHash } from '../../ComponentHash';
22
import { BinaryReader, createBinaryWriter } from '../../utils';
33

4-
export const HASH = ComponentHash.LiquidContainer;
5-
export const VERSION = 1;
6-
74
type Color = {
85
r: number;
96
g: number;
@@ -26,7 +23,7 @@ type CustomData = {
2623
foodChunks: FoodChunk[];
2724
};
2825

29-
export type Component = {
26+
export type LiquidContainer = {
3027
canAddTo?: boolean;
3128
canRemoveFrom?: boolean;
3229
contentLevel?: number;
@@ -36,8 +33,8 @@ export type Component = {
3633
customData?: null | CustomData;
3734
};
3835

39-
export const decode = (reader: BinaryReader): Component => {
40-
const result: Component = {
36+
export const decode = (reader: BinaryReader): LiquidContainer => {
37+
const result: LiquidContainer = {
4138
canAddTo: reader.boolean(),
4239
canRemoveFrom: reader.boolean(),
4340
contentLevel: reader.int(),
@@ -105,7 +102,7 @@ export const encode = ({
105102
isCustom = false,
106103
presetHash = 0,
107104
customData = null
108-
}: Component): string => {
105+
}: LiquidContainer): string => {
109106
const writer = createBinaryWriter();
110107

111108
/* Component hash. */

src/components/transcoders/networkRigidbody.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { ComponentHash } from '../../ComponentHash';
22
import { BinaryReader, createBinaryWriter } from '../../utils';
33

4-
export const HASH = ComponentHash.NetworkRigidbody;
5-
export const VERSION = 1;
6-
7-
export type Component = {
4+
export type NetworkRigidbody = {
85
position?: {
96
x: number;
107
y: number;
@@ -30,7 +27,7 @@ export type Component = {
3027
};
3128
};
3229

33-
export const decode = (reader: BinaryReader): Component => ({
30+
export const decode = (reader: BinaryReader): NetworkRigidbody => ({
3431
position: {
3532
x: reader.float(),
3633
y: reader.float(),
@@ -63,7 +60,7 @@ export const encode = ({
6360
isServerSleeping = false,
6461
velocity = { x: 0, y: 0, z: 0 },
6562
angularVelocity = { x: 0, y: 0, z: 0 }
66-
}: Component): string => {
63+
}: NetworkRigidbody): string => {
6764
const writer = createBinaryWriter();
6865

6966
/* Component hash. */

src/components/transcoders/physicalMaterialPart.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ import { ComponentHash } from '../../ComponentHash';
22
import { PhysicalMaterialPartHash } from '../../PhysicalMaterialPartHash';
33
import { BinaryReader, createBinaryWriter } from '../../utils';
44

5-
export const HASH = ComponentHash.PhysicalMaterialPart;
6-
export const VERSION = 1;
7-
8-
export type Component = {
5+
export type PhysicalMaterialPart = {
96
materialHash?: number;
107
};
118

12-
export const decode = (reader: BinaryReader): Component => ({
9+
export const decode = (reader: BinaryReader): PhysicalMaterialPart => ({
1310
materialHash: reader.uInt()
1411
});
1512

16-
export const encode = ({ materialHash = PhysicalMaterialPartHash.Iron }: Component): string => {
13+
export const encode = ({ materialHash = PhysicalMaterialPartHash.Iron }: PhysicalMaterialPart): string => {
1714
const writer = createBinaryWriter();
1815

1916
/* Component hash. */

src/components/transcoders/pickup.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import { ComponentHash } from '../../ComponentHash';
22
import { BinaryReader, createBinaryWriter } from '../../utils';
33

4-
export const HASH = ComponentHash.Pickup;
5-
export const VERSION = 1;
6-
7-
export type Component = {
4+
export type Pickup = {
85
lastInteractorPlayerId?: number;
96
};
107

11-
export const decode = (reader: BinaryReader): Component => ({
8+
export const decode = (reader: BinaryReader): Pickup => ({
129
lastInteractorPlayerId: reader.int()
1310
});
1411

15-
export const encode = ({ lastInteractorPlayerId = 0 }: Component): string => {
12+
export const encode = ({ lastInteractorPlayerId = 0 }: Pickup): string => {
1613
const writer = createBinaryWriter();
1714

1815
/* Component hash. */

src/components/transcoders/pickupDock.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import { ComponentHash } from '../../ComponentHash';
22
import { BinaryReader, createBinaryWriter } from '../../utils';
33

4-
export const HASH = ComponentHash.PickupDock;
5-
export const VERSION = 2;
6-
7-
export type Component = {
4+
export type PickupDock = {
85
dockedTypeHash?: number;
96
quantity?: number;
107
childIndex?: number;
118
};
129

13-
export const decode = (reader: BinaryReader): Component => ({
10+
export const decode = (reader: BinaryReader): PickupDock => ({
1411
dockedTypeHash: reader.uInt(),
1512
quantity: reader.int(),
1613
childIndex: reader.int()
1714
});
1815

19-
export const encode = ({ dockedTypeHash = 0, quantity = 1, childIndex = 0 }: Component): string => {
16+
export const encode = ({ dockedTypeHash = 0, quantity = 1, childIndex = 0 }: PickupDock): string => {
2017
const writer = createBinaryWriter();
2118

2219
/* Component hash. */

src/components/transcoders/populationSpawnArea.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ import { ComponentHash } from '../../ComponentHash';
22
import { PopulationDefinitionHash } from '../../PopulationDefinitionHash';
33
import { BinaryReader, createBinaryWriter } from '../../utils';
44

5-
export const HASH = ComponentHash.PopulationSpawnArea;
6-
export const VERSION = 2;
7-
85
type PopulationSaveDataChild = {
96
index: number;
107
pointIndex: number;
118
};
129

13-
export type Component = {
10+
export type PopulationSpawnArea = {
1411
definition?: number;
1512
isPopulationStarted?: boolean;
1613
children?: PopulationSaveDataChild[];
@@ -22,7 +19,7 @@ export type Component = {
2219
isOneOff?: boolean;
2320
};
2421

25-
export const decode = (reader: BinaryReader): Component => {
22+
export const decode = (reader: BinaryReader): PopulationSpawnArea => {
2623
const definition = reader.uInt();
2724
const isPopulationStarted = reader.boolean();
2825

@@ -66,7 +63,7 @@ export const encode = ({
6663
numberOfSpawnPoints = 40,
6764
startingPopulation = 5,
6865
isOneOff = false
69-
}: Component): string => {
66+
}: PopulationSpawnArea): string => {
7067
const writer = createBinaryWriter();
7168

7269
/* Component hash. */

src/components/transcoders/spawnArea.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import { ComponentHash } from '../../ComponentHash';
22
import { BinaryReader, createBinaryWriter } from '../../utils';
33

4-
export const HASH = ComponentHash.SpawnArea;
5-
export const VERSION = 1;
6-
7-
export type Component = {
4+
export type SpawnArea = {
85
size?: number;
96
groundLayers?: number;
107
avoidLayers?: number;
118
isAligningNormal?: boolean;
129
maxAcceptableAngleDot?: number;
1310
};
1411

15-
export const decode = (reader: BinaryReader): Component => ({
12+
export const decode = (reader: BinaryReader): SpawnArea => ({
1613
size: reader.float(),
1714
groundLayers: reader.int(),
1815
avoidLayers: reader.int(),
@@ -26,7 +23,7 @@ export const encode = ({
2623
avoidLayers = 8721,
2724
isAligningNormal = false,
2825
maxAcceptableAngleDot = -1
29-
}: Component): string => {
26+
}: SpawnArea): string => {
3027
const writer = createBinaryWriter();
3128

3229
/* Component hash. */

src/components/transcoders/statManager.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { ComponentHash } from '../../ComponentHash';
22
import { BinaryReader, createBinaryWriter } from '../../utils';
33

4-
export const HASH = ComponentHash.StatManager;
5-
export const VERSION = 2;
6-
74
type Stat = null | {
85
hash: number;
96
baseFlat: number;
@@ -29,13 +26,13 @@ type IndirectStatModifier = null | {
2926
modifiers: IndirectModifierSaveData[];
3027
};
3128

32-
export type Component = {
29+
export type StatManager = {
3330
stats?: Stat[];
3431
modifiers?: TimedModifier[];
3532
indirectStatModifiers?: IndirectStatModifier[];
3633
};
3734

38-
export const decode = (reader: BinaryReader): Component => {
35+
export const decode = (reader: BinaryReader): StatManager => {
3936
/* Get stats array. */
4037
const statsLength = reader.uInt();
4138
const stats: Stat[] = [];
@@ -108,7 +105,7 @@ export const decode = (reader: BinaryReader): Component => {
108105
return { stats, modifiers, indirectStatModifiers };
109106
};
110107

111-
export const encode = ({ stats = [], modifiers = [], indirectStatModifiers = [] }: Component): string => {
108+
export const encode = ({ stats = [], modifiers = [], indirectStatModifiers = [] }: StatManager): string => {
112109
const writer = createBinaryWriter();
113110

114111
/* Component hash. */

src/components/transcoders/woodcutTree.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@ import { PresetHash } from '../../PresetHash';
33
import { SpeciesHash } from '../../SpeciesHash';
44
import { BinaryReader, createBinaryWriter } from '../../utils';
55

6-
export const HASH = ComponentHash.WoodcutTree;
7-
export const VERSION = 3;
8-
9-
export type Component = {
6+
export type WoodcutTree = {
107
presetHash?: 0 | PresetHash;
118
speciesHash?: SpeciesHash;
129
};
1310

14-
export const decode = (reader: BinaryReader): Component => ({
11+
export const decode = (reader: BinaryReader): WoodcutTree => ({
1512
presetHash: reader.uInt(),
1613
speciesHash: reader.uInt()
1714
});
1815

19-
export const encode = ({ presetHash = 0, speciesHash = SpeciesHash.Oak }: Component): string => {
16+
export const encode = ({ presetHash = 0, speciesHash = SpeciesHash.Oak }: WoodcutTree): string => {
2017
const writer = createBinaryWriter();
2118

2219
/* Component hash. */

0 commit comments

Comments
 (0)