-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
387 lines (362 loc) · 13.9 KB
/
Copy pathvite.config.js
File metadata and controls
387 lines (362 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import crypto from 'node:crypto'
import fs from 'node:fs'
import { fileURLToPath, URL } from 'node:url'
const LAYERS_FILE = fileURLToPath(new URL('./src/data/layers.js', import.meta.url))
const MAPS_DIR = fileURLToPath(new URL('./public/maps', import.meta.url))
const MAP_TILES_MANIFEST = fileURLToPath(new URL('./public/map-tiles/manifest.json', import.meta.url))
function computeMapAssetVersion() {
try {
if (fs.existsSync(MAP_TILES_MANIFEST)) {
const manifest = JSON.parse(fs.readFileSync(MAP_TILES_MANIFEST, 'utf8'))
if (manifest?.version) return String(manifest.version)
}
const files = fs.readdirSync(MAPS_DIR, { withFileTypes: true })
const signature = files
.filter((entry) => entry.isFile())
.map((entry) => {
const stat = fs.statSync(new URL(`./public/maps/${entry.name}`, import.meta.url))
return `${entry.name}:${stat.size}:${Math.trunc(stat.mtimeMs)}`
})
.sort()
.join('|')
return signature
? crypto.createHash('sha1').update(signature).digest('hex').slice(0, 12)
: String(Date.now())
} catch {
return String(Date.now())
}
}
const MAP_ASSET_VERSION = computeMapAssetVersion()
function readBody(request) {
return new Promise((resolve, reject) => {
let body = ''
request.on('data', (chunk) => { body += chunk })
request.on('end', () => resolve(body))
request.on('error', reject)
})
}
function sendJson(response, payload, statusCode = 200) {
response.statusCode = statusCode
response.setHeader('Content-Type', 'application/json; charset=utf-8')
response.end(JSON.stringify(payload))
}
function formatGeofence(geofence) {
if (!geofence?.enabled) {
return ` geofence: {
enabled: false,
}`
}
const points = Array.isArray(geofence.points)
? geofence.points.map((point) => ({ x: Number(point?.x), y: Number(point?.y) }))
: []
if (points.length < 3
|| !points.every((point) => Number.isFinite(point.x) && Number.isFinite(point.y))
|| ![geofence.zMin, geofence.zMax].every((value) => Number.isFinite(Number(value)))) {
throw new Error('Invalid geofence values')
}
const pointLines = points
.map((point) => ` { x: ${point.x}, y: ${point.y} },`)
.join('\n')
return ` geofence: {
enabled: true,
points: [
${pointLines}
],
zMin: ${Number(geofence.zMin)},
zMax: ${Number(geofence.zMax)},
}`
}
function formatCoordinateMapping(mapping) {
const points = Array.isArray(mapping?.points)
? mapping.points.map((point) => ({
raw: [Number(point?.raw?.[0]), Number(point?.raw?.[1])],
map: [Number(point?.map?.[0]), Number(point?.map?.[1])],
}))
: []
if (points.length !== 3
|| !points.every((point) => [...point.raw, ...point.map].every(Number.isFinite))) {
throw new Error('Invalid coordinate mapping')
}
const pointLines = points
.map((point) => ` { raw: [${point.raw[0]}, ${point.raw[1]}], map: [${point.map[0]}, ${point.map[1]}] },`)
.join('\n')
const calibratedAt = mapping.calibratedAt
? `\n calibratedAt: '${String(mapping.calibratedAt).replaceAll("'", "\\'")}',`
: ''
return ` coordinateMapping: {
version: 1,
calibrated: ${mapping.calibrated === true},${calibratedAt}
points: [
${pointLines}
],
}`
}
function normalizeCoordinateTransform(transform) {
const allowedPlanes = new Set(['xoy', 'yoz', 'xoz'])
const allowedMirrorPlanes = new Set(['xoy', 'yoz', 'xoz'])
const plane = String(transform?.plane || 'xoy').toLowerCase()
if (!allowedPlanes.has(plane)) throw new Error('Invalid coordinate plane')
const readRotation = (axis) => Number(transform?.rotationDegrees?.[axis]) || 0
const readBoolean = (value) => value === true
const mirrorPlanes = Array.isArray(transform?.mirrorPlanes)
? transform.mirrorPlanes
.map((value) => String(value).toLowerCase())
.filter((value, index, values) => allowedMirrorPlanes.has(value) && values.indexOf(value) === index)
: []
return {
rotationDegrees: {
x: readRotation('x'),
y: readRotation('y'),
z: readRotation('z'),
},
plane,
mirrorPlanes,
flipAxes: {
x: readBoolean(transform?.flipAxes?.x),
y: readBoolean(transform?.flipAxes?.y),
z: readBoolean(transform?.flipAxes?.z),
},
offset: {
x: Number(transform?.offset?.x) || 0,
y: Number(transform?.offset?.y) || 0,
z: Number(transform?.offset?.z) || 0,
},
}
}
function formatCoordinateTransform(transform) {
const normalized = normalizeCoordinateTransform(transform)
const optionalMirror = normalized.mirrorPlanes.length
? `\n mirrorPlanes: [${normalized.mirrorPlanes.map((plane) => `'${plane}'`).join(', ')}],`
: ''
const flippedAxes = Object.entries(normalized.flipAxes)
.filter(([, enabled]) => enabled)
.map(([axis]) => `${axis}: true`)
const optionalFlipAxes = flippedAxes.length
? `\n flipAxes: { ${flippedAxes.join(', ')} },`
: ''
const offsets = Object.entries(normalized.offset)
.filter(([, value]) => value !== 0)
.map(([axis, value]) => `${axis}: ${value}`)
const optionalOffset = offsets.length
? `\n offset: { ${offsets.join(', ')} },`
: ''
return ` coordinateTransform: {
rotationDegrees: { x: ${normalized.rotationDegrees.x}, y: ${normalized.rotationDegrees.y}, z: ${normalized.rotationDegrees.z} },
plane: '${normalized.plane}',${optionalMirror}${optionalFlipAxes}${optionalOffset}
}`
}
function formatComposite(composite) {
const escapeText = (value) => String(value || '').replaceAll("'", "\\'")
const items = Array.isArray(composite?.items)
? composite.items.map((item) => ({
layerId: escapeText(item?.layerId),
x: Number(item?.x) || 0,
y: Number(item?.y) || 0,
}))
: []
if (!items.length || items.some((item) => !item.layerId)) throw new Error('Invalid composite items')
const markers = Array.isArray(composite?.markers)
? composite.markers.map((marker) => ({
id: escapeText(marker?.id),
label: escapeText(marker?.label),
type: ['portal', 'extraction', 'special-room'].includes(marker?.type) ? marker.type : 'portal',
layerId: escapeText(marker?.layerId),
x: Number(marker?.x) || 0,
y: Number(marker?.y) || 0,
}))
: []
if (markers.some((marker) => !marker.id || !marker.layerId)) throw new Error('Invalid composite markers')
const markerIds = new Set(markers.map((marker) => marker.id))
const edges = Array.isArray(composite?.edges)
? composite.edges.map((edge) => ({
from: escapeText(edge?.from),
to: escapeText(edge?.to),
bidirectional: edge?.bidirectional === true,
points: Array.isArray(edge?.points)
? edge.points.map((point) => ({ x: Number(point?.x), y: Number(point?.y) }))
: [],
}))
: []
if (edges.some((edge) =>
!markerIds.has(edge.from)
|| !markerIds.has(edge.to)
|| edge.from === edge.to
|| edge.points.some((point) => !Number.isFinite(point.x) || !Number.isFinite(point.y)))) {
throw new Error('Invalid composite edges')
}
const itemLines = items
.map((item) => ` { layerId: '${item.layerId}', x: ${item.x}, y: ${item.y} },`)
.join('\n')
const markerBlock = markers.length
? `\n markers: [
${markers
.map((marker) => ` { id: '${marker.id}', label: '${marker.label}', type: '${marker.type}', layerId: '${marker.layerId}', x: ${marker.x}, y: ${marker.y} },`)
.join('\n')}
],`
: ''
const edgeBlock = edges.length
? `\n edges: [
${edges
.map((edge) => {
const points = edge.points.length
? `, points: [${edge.points.map((point) => `{ x: ${point.x}, y: ${point.y} }`).join(', ')}]`
: ''
const bidirectional = edge.bidirectional ? ', bidirectional: true' : ''
return ` { from: '${edge.from}', to: '${edge.to}'${bidirectional}${points} },`
})
.join('\n')}
],`
: ''
return ` composite: {
items: [
${itemLines}
],${markerBlock}${edgeBlock}
}`
}
function getLayerSourceRange(source, layerId) {
const layerStart = source.indexOf(`id: '${layerId}'`)
if (layerStart < 0) throw new Error(`Unknown layer: ${layerId}`)
const nextLayer = source.indexOf("\n {\n id: '", layerStart + 1)
const layerEnd = nextLayer < 0 ? source.indexOf('\n },\n]', layerStart) : nextLayer
if (layerEnd < 0) throw new Error(`Cannot locate layer: ${layerId}`)
return { layerStart, layerEnd }
}
function updateLayerComposite(layerId, composite) {
const source = fs.readFileSync(LAYERS_FILE, 'utf8')
const { layerStart, layerEnd } = getLayerSourceRange(source, layerId)
const layerSource = source.slice(layerStart, layerEnd)
const compositePattern = / composite:\s*\{[\s\S]*?\n \},(?=\n coordinateTransform:|\n navigation:)/
if (!compositePattern.test(layerSource)) throw new Error(`Layer ${layerId} has no composite block`)
const updatedLayer = layerSource.replace(compositePattern, `${formatComposite(composite)},`)
fs.writeFileSync(
LAYERS_FILE,
`${source.slice(0, layerStart)}${updatedLayer}${source.slice(layerEnd)}`,
'utf8',
)
}
function updateLayerCoordinateTransform(layerId, transform) {
const source = fs.readFileSync(LAYERS_FILE, 'utf8')
const { layerStart, layerEnd } = getLayerSourceRange(source, layerId)
const layerSource = source.slice(layerStart, layerEnd)
const transformPattern = / coordinateTransform:\s*(?:\{[\s\S]*?\n \}|[A-Z0-9_]+),/
const formatted = `${formatCoordinateTransform(transform)},`
let updatedLayer
if (transformPattern.test(layerSource)) {
updatedLayer = layerSource.replace(transformPattern, formatted)
} else {
const navigationPattern = / navigation:/
if (!navigationPattern.test(layerSource)) throw new Error(`Layer ${layerId} has no navigation block`)
updatedLayer = layerSource.replace(navigationPattern, `${formatted}\n navigation:`)
}
fs.writeFileSync(
LAYERS_FILE,
`${source.slice(0, layerStart)}${updatedLayer}${source.slice(layerEnd)}`,
'utf8',
)
}
function updateLayerGeofence(layerId, geofence) {
const source = fs.readFileSync(LAYERS_FILE, 'utf8')
const { layerStart, layerEnd } = getLayerSourceRange(source, layerId)
const layerSource = source.slice(layerStart, layerEnd)
const geofencePattern = / geofence:\s*\{[\s\S]*?\n \},(?=\n coordinateMapping:)/
if (!geofencePattern.test(layerSource)) throw new Error(`Layer ${layerId} has no geofence block`)
const updatedLayer = layerSource.replace(geofencePattern, `${formatGeofence(geofence)},`)
fs.writeFileSync(
LAYERS_FILE,
`${source.slice(0, layerStart)}${updatedLayer}${source.slice(layerEnd)}`,
'utf8',
)
}
function updateLayerCoordinateMapping(layerId, mapping) {
const source = fs.readFileSync(LAYERS_FILE, 'utf8')
const { layerStart, layerEnd } = getLayerSourceRange(source, layerId)
const layerSource = source.slice(layerStart, layerEnd)
// Do not require a following layer terminator: the final MAP_LAYERS item
// ends immediately before the array closing bracket.
const mappingPattern = / coordinateMapping:\s*\{[\s\S]*?\n \},/
if (!mappingPattern.test(layerSource)) {
throw new Error(`Layer ${layerId} has no coordinateMapping block`)
}
const updatedLayer = layerSource.replace(
mappingPattern,
`${formatCoordinateMapping(mapping)},`,
)
fs.writeFileSync(
LAYERS_FILE,
`${source.slice(0, layerStart)}${updatedLayer}${source.slice(layerEnd)}`,
'utf8',
)
}
function localLayerEditorPlugin() {
return {
name: 'local-layer-editor',
configureServer(server) {
server.middlewares.use('/api/layer-geofence', async (request, response) => {
if (request.method !== 'POST') {
sendJson(response, { error: 'Method not allowed' }, 405)
return
}
try {
const { layerId, geofence } = JSON.parse(await readBody(request))
updateLayerGeofence(String(layerId || ''), geofence)
sendJson(response, { ok: true })
} catch (error) {
sendJson(response, { error: error.message }, 400)
}
})
server.middlewares.use('/api/layer-calibration', async (request, response) => {
if (request.method !== 'POST') {
sendJson(response, { error: 'Method not allowed' }, 405)
return
}
try {
const { layerId, coordinateMapping } = JSON.parse(await readBody(request))
updateLayerCoordinateMapping(String(layerId || ''), coordinateMapping)
sendJson(response, { ok: true })
} catch (error) {
sendJson(response, { error: error.message }, 400)
}
})
server.middlewares.use('/api/layer-transform', async (request, response) => {
if (request.method !== 'POST') {
sendJson(response, { error: 'Method not allowed' }, 405)
return
}
try {
const { layerId, coordinateTransform } = JSON.parse(await readBody(request))
updateLayerCoordinateTransform(String(layerId || ''), coordinateTransform)
sendJson(response, { ok: true })
} catch (error) {
sendJson(response, { error: error.message }, 400)
}
})
server.middlewares.use('/api/layer-composite', async (request, response) => {
if (request.method !== 'POST') {
sendJson(response, { error: 'Method not allowed' }, 405)
return
}
try {
const { layerId, composite } = JSON.parse(await readBody(request))
updateLayerComposite(String(layerId || ''), composite)
sendJson(response, { ok: true })
} catch (error) {
sendJson(response, { error: error.message }, 400)
}
})
},
}
}
export default defineConfig({
base: './',
define: {
__MAP_ASSET_VERSION__: JSON.stringify(MAP_ASSET_VERSION),
},
plugins: [vue(), localLayerEditorPlugin()],
server: {
watch: {
ignored: [LAYERS_FILE],
},
},
})