Skip to content

Commit

Permalink
Improve Prefab.entities type and IDE hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
mdingena committed Apr 17, 2024
1 parent 92f76b8 commit 4b1b275
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Entity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('Entity.fromBinary()', () => {
const entityName = 'Fire';
const prefab = new Prefab(prefabName, {
entities: {
Fire: new Entity<typeof prefabName>(`${entityName}_${entityHash}`, {
Fire_30100: new Entity<typeof prefabName>(`${entityName}_${entityHash}`, {
hash: entityHash,
isAlive: false,
components: {
Expand Down
1 change: 1 addition & 0 deletions src/Prefab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@ describe('Prefab.removeAllEntities()', () => {
]
}
}),
/* @ts-expect-error Add in an unknown entity. */
Unknown_1337: new Entity('Unknown', { hash: 1337 })
}
});
Expand Down
6 changes: 3 additions & 3 deletions src/Prefab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ export class Prefab<TPrefabName extends ATTPrefabName = ATTPrefabName> {
}

/* Get component versions from prefab entities. */
for (const entity of Object.values(this.entities)) {
for (const entity of Object.values(this.entities) as Entity<TPrefabName>[]) {
for (const component of Object.values(entity.components)) {
if (Array.isArray(component)) {
for (const unknownComponent of component) {
Expand Down Expand Up @@ -1266,7 +1266,7 @@ export class Prefab<TPrefabName extends ATTPrefabName = ATTPrefabName> {
throw new Error('You must pass an entity name to remove from this prefab.');
}

delete this.entities[entityKey as string];
delete this.entities[entityKey];

return this;
}
Expand Down Expand Up @@ -1501,7 +1501,7 @@ export class Prefab<TPrefabName extends ATTPrefabName = ATTPrefabName> {
if (typeof validFireEntity !== 'undefined') {
const currentFireEntity = Object.values(this.entities).find(entity => entity.name === validFireEntity.name);
const version = currentFireEntity?.components.HeatSourceBase?.version ?? FALLBACK_HEAT_SOURCE_BASE_VERSION;
const key = `${validFireEntity.name}_${validFireEntity.hash}`;
const key = `${validFireEntity.name}_${validFireEntity.hash}` as EntityKey<TPrefabName>;

this.entities[key] = new Entity<TPrefabName>(key as keyof (typeof ATTPrefabs)[TPrefabName]['embedded'], {
hash: validFireEntity.hash,
Expand Down
3 changes: 2 additions & 1 deletion src/types/PrefabEntities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ATTPrefabName } from './ATTPrefabName.js';
import type { EntityKey } from './EntityKey.js';
import type { Entity } from '../Entity.js';

export type PrefabEntities<TPrefabName extends ATTPrefabName> = {
[EntityName: string]: Entity<TPrefabName>;
[Key in EntityKey<TPrefabName>]?: Entity<TPrefabName>;
};
5 changes: 5 additions & 0 deletions src/utils/readEntities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('readEntities()', () => {
hash: firstEntityHash,
isAlive: true
}),
/* @ts-expect-error Add in an unknown entity. */
Unknown_23002: new Entity(secondEntityName, {
hash: secondEntityHash,
isAlive: false
Expand Down Expand Up @@ -63,9 +64,13 @@ describe('readEntities()', () => {
expect(entities['Slot_Multi_6136']?.name).toStrictEqual(firstEntityName);
expect(entities['Slot_Multi_6136']?.isAlive).toBe(true);

/* @ts-expect-error Access unknown entity. */
expect(entities['Unknown_23002']).toBeInstanceOf(Entity);
/* @ts-expect-error Access unknown entity. */
expect(entities['Unknown_23002']?.hash).toStrictEqual(secondEntityHash);
/* @ts-expect-error Access unknown entity. */
expect(entities['Unknown_23002']?.name).toStrictEqual(secondEntityName);
/* @ts-expect-error Access unknown entity. */
expect(entities['Unknown_23002']?.isAlive).toBe(false);
});
});
Expand Down
13 changes: 7 additions & 6 deletions src/utils/readEntities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BinaryReader } from '../BinaryReader.js';
import type { ATTPrefabName } from '../types/ATTPrefabName.js';
import type { EntityKey } from '../types/EntityKey.js';
import type { PrefabEntities } from '../types/PrefabEntities.js';

import { Entity } from '../Entity.js';
Expand All @@ -24,12 +25,12 @@ import { ATTPrefabs } from '../types/ATTPrefabs.js';
*
* const component = readEntities(reader, prefabName, componentVersions);
*/
export function readEntities(
export function readEntities<TPrefabName extends ATTPrefabName>(
reader: BinaryReader,
prefabName: ATTPrefabName,
prefabName: TPrefabName,
componentVersions?: Map<number, number>
): PrefabEntities<typeof prefabName> {
const entities: Record<string, Entity<typeof prefabName>> = {};
): PrefabEntities<TPrefabName> {
const entities: PrefabEntities<TPrefabName> = {};

const isLooping = true;

Expand All @@ -48,11 +49,11 @@ export function readEntities(
}>(ATTPrefabs[prefabName].embedded).find(attPrefabEntity => attPrefabEntity.hash === hash);

const name = entity?.name ?? 'Unknown';
const key = `${name}_${hash}`;
const key = `${name}_${hash}` as EntityKey<TPrefabName>;

entities[key] = Entity.fromBinary<typeof prefabName>(reader, {
hash,
key: name === 'Unknown' ? name : key,
key: name === 'Unknown' ? name : (key as string),
componentVersions
});
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/writeEntities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('writeEntities()', () => {
hash: 30100,
isAlive: true
}),
/* @ts-expect-error Add in an unknown entity. */
Unknown_23002: new Entity(secondEntityKey, {
hash: secondEntityHash,
isAlive: false
Expand Down

0 comments on commit 4b1b275

Please sign in to comment.