Resolve browser field projection through plugin adapters
This commit is contained in:
parent
fbca4f2044
commit
fe71b8d2c5
11 changed files with 442 additions and 15 deletions
136
packages/eve-browser-lowering/src/fields-browser-adapter.ts
Normal file
136
packages/eve-browser-lowering/src/fields-browser-adapter.ts
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
const FIELD_SCHEMAS = new Set([
|
||||
"gamecult.fields.splats.v1",
|
||||
"gamecult.fields.gravity.v1",
|
||||
"gamecult.fields.objects.v1",
|
||||
]);
|
||||
|
||||
export interface EveBrowserPluginAdapter {
|
||||
pluginId: string;
|
||||
capabilities: readonly string[];
|
||||
schemas: readonly string[];
|
||||
normalizeDocument(schemaId: string | undefined, value: unknown): unknown;
|
||||
}
|
||||
|
||||
export const fieldsBrowserAdapter: EveBrowserPluginAdapter = {
|
||||
pluginId: "fields.surface",
|
||||
capabilities: ["field.surface2d", "gravity.surface", "field.scalar", "field.vector", "field.objects"],
|
||||
schemas: [...FIELD_SCHEMAS],
|
||||
normalizeDocument: normalizeFieldsDocument,
|
||||
};
|
||||
|
||||
export function normalizeFieldsDocument(schemaId: string | undefined, value: unknown): unknown {
|
||||
const tuple = Array.isArray(value) ? value : undefined;
|
||||
const documentSchema = tuple ? tuple[0] : (value as { schema?: unknown } | undefined)?.schema;
|
||||
const schema = String(schemaId || documentSchema || "");
|
||||
if (!tuple || !FIELD_SCHEMAS.has(schema)) return value;
|
||||
if (schema === "gamecult.fields.splats.v1") return normalizeSplats(tuple);
|
||||
if (schema === "gamecult.fields.gravity.v1") return normalizeGravity(tuple);
|
||||
return normalizeObjects(tuple);
|
||||
}
|
||||
|
||||
function normalizeSplats(value: unknown[]): Record<string, unknown> {
|
||||
const splats = array(value[9]);
|
||||
return {
|
||||
schema: value[0], frameId: value[1], publishedAtUtc: value[2], simulationTimeSeconds: value[3],
|
||||
runId: value[4], zoneIndex: value[5], zoneName: value[6], viewport: normalizeViewport(value[7]),
|
||||
layers: array(value[8]).map(normalizeSplatLayer),
|
||||
splats: {
|
||||
count: splats[0] ?? 0, centerX: splats[1] || [], centerY: splats[2] || [],
|
||||
halfExtentX: splats[3] || [], halfExtentY: splats[4] || [], rotationCos: splats[5] || [],
|
||||
rotationSin: splats[6] || [], channel: splats[7] || [], falloff: splats[8] || [],
|
||||
valueR: splats[9] || [], valueG: splats[10] || [], valueB: splats[11] || [], valueA: splats[12] || [],
|
||||
sourceKey: splats[13] || [], layerIndex: splats[14] || [], sourceKind: splats[15] || [],
|
||||
frequencyX: splats[16] || [], frequencyY: splats[17] || [], phaseX: splats[18] || [],
|
||||
phaseY: splats[19] || [], animationSpeed: splats[20] || [], sourceFlags: splats[21] || [],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeGravity(value: unknown[]): Record<string, unknown> {
|
||||
return {
|
||||
schema: value[0], frameId: value[1], publishedAtUtc: value[2], simulationTimeSeconds: value[3],
|
||||
runId: value[4], zoneIndex: value[5], zoneName: value[6], viewport: normalizeViewport(value[7]),
|
||||
gravityInfluences: array(value[8]).map(normalizeGravityInfluence),
|
||||
bodies: array(value[9]).map(normalizeBody), terrainRadius: value[10], terrainDepth: value[11],
|
||||
terrainDepthExponent: value[12], terrainWaveFrequency: value[13],
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeObjects(value: unknown[]): Record<string, unknown> {
|
||||
return {
|
||||
schema: value[0], frameId: value[1], publishedAtUtc: value[2], simulationTimeSeconds: value[3],
|
||||
runId: value[4], zoneIndex: value[5], zoneName: value[6], currentEntityKey: value[7],
|
||||
viewport: normalizeViewport(value[8]), controlledEntityIndices: array(value[9]),
|
||||
objects: array(value[10]).map(normalizeObject),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeViewport(value: unknown): unknown {
|
||||
const row = array(value);
|
||||
return row.length ? { minX: row[0], minY: row[1], maxX: row[2], maxY: row[3] } : value;
|
||||
}
|
||||
|
||||
function normalizeSplatLayer(value: unknown): unknown {
|
||||
const row = array(value);
|
||||
if (!row.length) return value;
|
||||
return {
|
||||
layerKey: row[0], displayName: row[1], channel: row[2], blendMode: row[3], graphicsFormat: row[4],
|
||||
clearBeforeDraw: row[5], clearR: row[6], clearG: row[7], clearB: row[8], clearA: row[9],
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeGravityInfluence(value: unknown): unknown {
|
||||
const row = array(value);
|
||||
if (!row.length) return value;
|
||||
return {
|
||||
bodyKey: row[0], orbitKey: row[1], kind: row[2], x: row[3], y: row[4], radius: row[5],
|
||||
gravityDepth: row[6], gravityDepthExponent: row[7], waveRadius: row[8], waveDepth: row[9], waveSpeed: row[10],
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeBody(value: unknown): unknown {
|
||||
const row = array(value);
|
||||
if (!row.length) return value;
|
||||
return {
|
||||
bodyKey: row[0], orbitKey: row[1], name: row[2], kind: row[3], x: row[4], y: row[5], radius: row[6],
|
||||
isAsteroidBelt: row[7], body: row[8], iconAsset: normalizeAsset(row[9]),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeObject(value: unknown): unknown {
|
||||
const row = array(value);
|
||||
if (!row.length) return value;
|
||||
return {
|
||||
entityIndex: row[0], entityKey: row[1], displayName: row[2], kind: row[3], factionKey: row[4],
|
||||
x: row[5], y: row[6], z: row[7], directionX: row[8], directionY: row[9], velocityX: row[10],
|
||||
velocityY: row[11], controlled: row[12], targetEntityIndex: row[13], isActive: row[14], visibility: row[15],
|
||||
status: normalizeStatus(row[16]), inventory: array(row[17]).map(normalizeInventoryItem), iconAsset: normalizeAsset(row[18]),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeStatus(value: unknown): unknown {
|
||||
const row = array(value);
|
||||
return row.length ? { hull: row[0], shield: row[1], heat: row[2] } : value || {};
|
||||
}
|
||||
|
||||
function normalizeInventoryItem(value: unknown): unknown {
|
||||
const row = array(value);
|
||||
if (!row.length) return value;
|
||||
return {
|
||||
source: row[0], itemKey: row[1], quantity: row[2], quality: row[3], durability: row[4], enabled: row[5],
|
||||
sourceIndex: row[6], x: row[7], y: row[8], iconAsset: normalizeAsset(row[9]),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeAsset(value: unknown): unknown {
|
||||
const row = array(value);
|
||||
if (!row.length) return value || {};
|
||||
return {
|
||||
assetKey: row[0], kind: row[1], uri: row[2], transport: row[3], contentHash: row[4],
|
||||
mimeType: row[5], metadata: row[6] || {},
|
||||
};
|
||||
}
|
||||
|
||||
function array(value: unknown): unknown[] {
|
||||
return Array.isArray(value) ? value : [];
|
||||
}
|
||||
|
|
@ -1,3 +1,7 @@
|
|||
import { fieldsBrowserAdapter, type EveBrowserPluginAdapter } from "./fields-browser-adapter.js";
|
||||
|
||||
export { fieldsBrowserAdapter, normalizeFieldsDocument, type EveBrowserPluginAdapter } from "./fields-browser-adapter.js";
|
||||
|
||||
export interface EveSurfaceComponent {
|
||||
id?: string;
|
||||
kind?: string;
|
||||
|
|
@ -55,6 +59,15 @@ export interface EveProviderSurfaceAdvertisement {
|
|||
status?: string;
|
||||
transport?: string;
|
||||
worldInteraction?: EveSurfaceWorldInteraction;
|
||||
requiresPlugins?: EvePluginRequirement[];
|
||||
}
|
||||
|
||||
export interface EvePluginRequirement {
|
||||
pluginId: string;
|
||||
versionRange?: string;
|
||||
availability?: "required" | "optional" | "optional-nested";
|
||||
requiredCapabilities?: string[];
|
||||
optionalCapabilities?: string[];
|
||||
}
|
||||
|
||||
export interface EveProviderAdvertisement {
|
||||
|
|
@ -74,6 +87,7 @@ export interface EveBrowserLoweringOptions {
|
|||
commandSink?: (intent: EveCommandIntent, component: EveSurfaceComponent) => void | Promise<void>;
|
||||
documentResolver?: (request: EveEmbeddedDocumentRequest, component: EveSurfaceComponent) => Promise<EveResolvedDocument | EveSurfaceDocument | EveSurfaceDocument["surface"] | undefined>;
|
||||
provider?: EveProviderAdvertisement;
|
||||
pluginAdapters?: readonly EveBrowserPluginAdapter[];
|
||||
source?: string;
|
||||
statusElement?: HTMLElement;
|
||||
}
|
||||
|
|
@ -93,6 +107,31 @@ export interface EveBrowserProviderHostOptions {
|
|||
requestedSurfaceId?: string;
|
||||
source?: string;
|
||||
statusElement?: HTMLElement;
|
||||
pluginAdapters?: readonly EveBrowserPluginAdapter[];
|
||||
}
|
||||
|
||||
export const defaultBrowserPluginAdapters: readonly EveBrowserPluginAdapter[] = [fieldsBrowserAdapter];
|
||||
|
||||
export function resolveRequiredPluginAdapters(
|
||||
surface: EveProviderSurfaceAdvertisement,
|
||||
available: readonly EveBrowserPluginAdapter[] = defaultBrowserPluginAdapters,
|
||||
): readonly EveBrowserPluginAdapter[] {
|
||||
const resolved: EveBrowserPluginAdapter[] = [];
|
||||
for (const requirement of surface.requiresPlugins || []) {
|
||||
const adapter = available.find(candidate => candidate.pluginId === requirement.pluginId);
|
||||
if (!adapter) {
|
||||
if ((requirement.availability || "required") === "required") {
|
||||
throw new Error(`Missing required Eve plugin adapter ${requirement.pluginId}.`);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const missing = (requirement.requiredCapabilities || []).filter(capability => !adapter.capabilities.includes(capability));
|
||||
if (missing.length > 0) {
|
||||
throw new Error(`Eve plugin adapter ${requirement.pluginId} lacks required capabilities: ${missing.join(", ")}.`);
|
||||
}
|
||||
resolved.push(adapter);
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
|
||||
export function selectAdvertisedSurface(
|
||||
|
|
@ -115,6 +154,7 @@ export class EveBrowserProviderHost {
|
|||
private pollHandle: number | undefined;
|
||||
private provider: EveProviderAdvertisement | undefined;
|
||||
private selected: EveProviderSurfaceAdvertisement | undefined;
|
||||
private pluginAdapters: readonly EveBrowserPluginAdapter[] = [];
|
||||
|
||||
constructor(
|
||||
private readonly host: HTMLElement,
|
||||
|
|
@ -125,6 +165,10 @@ export class EveBrowserProviderHost {
|
|||
async start(): Promise<void> {
|
||||
this.provider = await this.transport.providerAdvertisement();
|
||||
this.selected = selectAdvertisedSurface(this.provider, this.options.requestedSurfaceId);
|
||||
this.pluginAdapters = resolveRequiredPluginAdapters(
|
||||
this.selected,
|
||||
this.options.pluginAdapters || defaultBrowserPluginAdapters,
|
||||
);
|
||||
this.active = true;
|
||||
await this.refresh();
|
||||
const pollMs = Math.max(0, this.options.pollMs ?? 250);
|
||||
|
|
@ -154,6 +198,7 @@ export class EveBrowserProviderHost {
|
|||
commandSink: intent => this.submit(intent),
|
||||
documentResolver: this.transport.resolveDocument,
|
||||
provider: this.provider,
|
||||
pluginAdapters: this.pluginAdapters,
|
||||
source: this.options.source,
|
||||
statusElement: this.options.statusElement,
|
||||
});
|
||||
|
|
@ -989,10 +1034,10 @@ async function resolveGravitySurfaceDocuments(
|
|||
try {
|
||||
let changed = false;
|
||||
for (const request of requests) {
|
||||
const resolved = await options.documentResolver(request, node);
|
||||
let document = objectProps((resolved as EveResolvedDocument | undefined)?.document);
|
||||
const resolved = await options.documentResolver(request, node) as EveResolvedDocument | undefined;
|
||||
let document = objectProps(normalizePluginDocument(resolved?.schemaId || request.schemaId, resolved?.document, options));
|
||||
if (Object.keys(document).length === 0) {
|
||||
document = objectProps(await fetchGravitySurfaceDocument(request, options));
|
||||
document = objectProps(normalizePluginDocument(request.schemaId, await fetchGravitySurfaceDocument(request, options), options));
|
||||
}
|
||||
if (!document || Object.keys(document).length === 0) continue;
|
||||
const key = request.slotId === "renderSplats"
|
||||
|
|
@ -1012,6 +1057,16 @@ async function resolveGravitySurfaceDocuments(
|
|||
}
|
||||
}
|
||||
|
||||
function normalizePluginDocument(
|
||||
schemaId: string | undefined,
|
||||
value: unknown,
|
||||
options: EveBrowserLoweringOptions,
|
||||
): unknown {
|
||||
const adapters = options.pluginAdapters || defaultBrowserPluginAdapters;
|
||||
const adapter = adapters.find(candidate => !schemaId || candidate.schemas.includes(schemaId));
|
||||
return adapter ? adapter.normalizeDocument(schemaId, value) : value;
|
||||
}
|
||||
|
||||
async function fetchGravitySurfaceDocument(
|
||||
request: EveEmbeddedDocumentRequest,
|
||||
options: EveBrowserLoweringOptions,
|
||||
|
|
|
|||
Loading…
Reference in a new issue