feat: custom components

This commit is contained in:
TrèsAbhi committed 2025-05-27 12:39:04 -05:00
1 parent bc6bb4f10e
commit 69b13fd33a
9 files changed
+164 -58

No files matched your search

@@ -3,9 +3,12 @@ import { Any } from '@protos/google/protobuf/any';
import { MessageFns } from '../protos';
export class CatalogItemAccessor {
public id: string;
private components: Record<string, Any> = {};
constructor(item: CatalogItem) {
this.id = item.catalog_id;
for (const component of item.components) {
if (
component.value === undefined ||
@@ -18,6 +21,11 @@ export class CatalogItemAccessor {
}
}
addComponent(key: string, value: Any) {
this.components[key] = value;
return this;
}
get<Type>(Message: MessageFns<Type>, component: string) {
if (component in this.components) {
return Message.decode(this.components[component].value);
@@ -25,4 +33,19 @@ export class CatalogItemAccessor {
throw new Error(`Unknown component: ${component}`);
}
pack() {
const item: CatalogItem = { catalog_id: this.id, components: [] };
for (const componentId in this.components) {
const component = this.components[componentId];
item.components.push({
key: componentId,
value: { type_url: component.type_url, component },
});
}
return item;
}
}
@@ -1,5 +1,8 @@
import { CatalogItem } from '@protos/blitz_items';
import { Metadata } from '@protos/blitz_metadata';
import { CatalogItemsComponent } from '@protos/blitz_static_catalog_items_group_component';
import { BlitzKitAllTankEntities } from '@protos/blitzkit_static_all_tank_entities';
import { BlitzKitTankCatalogComponent } from '@protos/blitzkit_static_tank_component';
import { CatalogItemAccessor } from './catalogItemAccessor';
export class UnpackedMetadataAccessor {
@@ -20,6 +23,70 @@ export class UnpackedMetadataAccessor {
throw new Error(`Unknown catalog ID: ${item}`);
}
addCatalogItem(item: CatalogItemAccessor) {
this.items[item.id] = item;
return this;
}
addCatalogItemWithComponent(
catalogId: string,
componentId: string,
url: string,
value: Uint8Array,
) {
this.addCatalogItem(
new CatalogItemAccessor({
catalog_id: catalogId,
components: [],
}).addComponent(componentId, { type_url: url, value }),
);
}
getAllTanks() {
return this.get('CatalogItemsGroupEntity.all_tanks').get(
CatalogItemsComponent,
'catalogItemsGroupComponent',
);
}
injectBlitzkit() {
this.injectBlitzKitTankComponents();
this.injectBlitzKitAllTankEntities();
return this;
}
injectBlitzKitTankComponents() {
for (const item of this.getAllTanks().catalog_items) {
const tank = this.get(item);
const value = BlitzKitTankCatalogComponent.encode({
id: 'TEST',
set: 'TEST',
}).finish();
tank.addComponent('blitzkitTankCatalogComponent', {
type_url:
'./blitzkit_static_tank_component.BlitzKitTankCatalogComponent',
value,
});
}
}
injectBlitzKitAllTankEntities() {
const allTanksEntities: BlitzKitAllTankEntities = { tanks: [] };
for (const item of this.getAllTanks().catalog_items) {
allTanksEntities.tanks.push(this.get(item).pack());
}
this.addCatalogItemWithComponent(
'BlitzKitAllTankEntities.all_tanks',
'blitzkitAllTankEntitiesComponent',
'./blitzkit_static_all_tank_entities.BlitzKitAllTankEntities',
BlitzKitAllTankEntities.encode(allTanksEntities).finish(),
);
}
}
export class DiscreteMetadataAccessor {