1- import type { ScoringZonePreferences } from "@/systems/preferences/PreferenceTypes"
2- import { mirabuf } from "../proto/mirabuf"
1+ import type MirabufSceneObject from "@/mirabuf/MirabufSceneObject.ts"
2+ import { mirabuf } from "@/proto/mirabuf"
3+ import {
4+ defaultFieldPreferences ,
5+ type FieldPreferences ,
6+ type ScoringZonePreferences ,
7+ } from "@/systems/preferences/PreferenceTypes"
38
4- interface DevtoolMiraData {
9+ export interface DevtoolMiraData {
510 "devtool:scoring_zones" : ScoringZonePreferences [ ]
611 "devtool:camera_locations" : unknown
7- "devtool:spawn_points " : unknown
12+ "devtool:spawn_locations " : FieldPreferences [ "spawnLocations" ]
813 "devtool:a" : unknown
914 "devtool:b" : unknown
1015 "devtool:test" : unknown
@@ -15,6 +20,87 @@ interface DevtoolMiraData {
1520 // additional devtool keys to be added in future
1621}
1722
23+ export const devtoolHandlers = {
24+ "devtool:scoring_zones" : {
25+ get ( field ) {
26+ return field . fieldPreferences ?. scoringZones ?? defaultFieldPreferences ( ) . scoringZones
27+ } ,
28+ set ( field , val ) {
29+ val ??= defaultFieldPreferences ( ) . scoringZones
30+ if ( ! field . fieldPreferences || ! this . validate ( val ) ) {
31+ console . warn ( "validation failed" , val , field . fieldPreferences )
32+ return
33+ }
34+ field . fieldPreferences . scoringZones = val
35+ field . updateScoringZones ( )
36+ } ,
37+ validate ( val ) : val is ScoringZonePreferences [ ] {
38+ if ( ! Array . isArray ( val ) ) return false
39+ return val . every (
40+ z =>
41+ typeof z === "object" &&
42+ z !== null &&
43+ typeof z . name === "string" &&
44+ ( z . alliance === "red" || z . alliance === "blue" ) &&
45+ ( typeof z . parentNode === "string" || z . parentNode === undefined ) &&
46+ typeof z . points === "number" &&
47+ typeof z . destroyGamepiece === "boolean" &&
48+ typeof z . persistentPoints === "boolean" &&
49+ Array . isArray ( z . deltaTransformation )
50+ )
51+ } ,
52+ } ,
53+ "devtool:spawn_locations" : {
54+ get ( field ) {
55+ return field . fieldPreferences ?. spawnLocations ?? defaultFieldPreferences ( ) . spawnLocations
56+ } ,
57+ set ( field , val ) {
58+ val ??= defaultFieldPreferences ( ) . spawnLocations
59+ if ( ! field . fieldPreferences || ! this . validate ( val ) ) {
60+ console . warn ( "validation failed" , val , field . fieldPreferences )
61+ return
62+ }
63+ field . fieldPreferences . spawnLocations = val
64+ } ,
65+ validate ( val : unknown ) : val is FieldPreferences [ "spawnLocations" ] {
66+ const isStructureCorrect =
67+ typeof val === "object" &&
68+ val != null &&
69+ "red" in val &&
70+ "blue" in val &&
71+ "default" in val &&
72+ "hasConfiguredLocations" in val
73+
74+ if ( ! isStructureCorrect ) return false
75+ return ( [ "red" , "blue" ] as const ) . every ( v => {
76+ const obj = val [ v ]
77+ if ( ! ( typeof obj === "object" && obj != null && 1 in obj && 2 in obj && 3 in obj ) ) return false
78+ return ( [ 1 , 2 , 3 ] as const ) . every ( v => {
79+ const spawnposition = obj [ v ]
80+ return (
81+ typeof spawnposition == "object" &&
82+ spawnposition != null &&
83+ "pos" in spawnposition &&
84+ "yaw" in spawnposition &&
85+ Array . isArray ( spawnposition [ "pos" ] ) &&
86+ spawnposition [ "pos" ] . length == 3 &&
87+ typeof spawnposition [ "yaw" ] == "number"
88+ )
89+ } )
90+ } )
91+ } ,
92+ } ,
93+ } as const satisfies Partial < {
94+ [ K in keyof DevtoolMiraData ] : {
95+ get ( field : MirabufSceneObject ) : DevtoolMiraData [ K ]
96+ set ( field : MirabufSceneObject , val : unknown | null ) : void
97+ validate ( val : unknown ) : val is DevtoolMiraData [ K ]
98+ }
99+ } >
100+
101+ export type DevtoolKey = keyof typeof devtoolHandlers
102+ export const devtoolKeys = Object . keys ( devtoolHandlers ) as DevtoolKey [ ]
103+
18104/**
19105 * Utility for reading and writing developer tool data in the mira file's UserData field.
20106 * Docs: https://www.mirabuf.dev/#mirabuf.UserData
0 commit comments