77import Engine from '../../src/engine/core/Engine.js' ;
88import { InputService } from '../../src/engine/input/index.js' ;
99import { Theme , ThemeTokens } from '../../src/engine/theme/index.js' ;
10+ import {
11+ parseBooleanFlag ,
12+ normalizeDebugMode ,
13+ readStoredBoolean ,
14+ writeStoredBoolean ,
15+ isLocalDebugEnvironment ,
16+ resolveDebugConfig
17+ } from '../../src/shared/utils/debugConfigUtils.js' ;
1018import { createSampleGameDevConsoleIntegration } from '../../tools/dev/devConsoleIntegration.js' ;
1119import { createAsteroidsShowcaseDebugPlugin } from './debug/asteroidsShowcaseDebug.js' ;
1220import AsteroidsGameScene from './game/AsteroidsGameScene.js' ;
@@ -20,105 +28,6 @@ function sanitizeText(value) {
2028 return typeof value === 'string' ? value . trim ( ) : '' ;
2129}
2230
23- function parseBooleanFlag ( value , fallback ) {
24- const normalized = sanitizeText ( value ) . toLowerCase ( ) ;
25- if ( ! normalized ) {
26- return fallback ;
27- }
28- if ( normalized === '1' || normalized === 'true' || normalized === 'on' || normalized === 'yes' ) {
29- return true ;
30- }
31- if ( normalized === '0' || normalized === 'false' || normalized === 'off' || normalized === 'no' ) {
32- return false ;
33- }
34- return fallback ;
35- }
36-
37- function normalizeDebugMode ( value , fallback = 'prod' ) {
38- const normalized = sanitizeText ( value ) . toLowerCase ( ) ;
39- if ( normalized === 'dev' || normalized === 'qa' || normalized === 'prod' ) {
40- return normalized ;
41- }
42- return fallback ;
43- }
44-
45- function readStoredBoolean ( key ) {
46- if ( ! key || typeof globalThis . localStorage === 'undefined' ) {
47- return null ;
48- }
49-
50- try {
51- const value = globalThis . localStorage . getItem ( key ) ;
52- if ( value === '1' ) {
53- return true ;
54- }
55- if ( value === '0' ) {
56- return false ;
57- }
58- } catch {
59- return null ;
60- }
61-
62- return null ;
63- }
64-
65- function writeStoredBoolean ( key , value ) {
66- if ( ! key || typeof globalThis . localStorage === 'undefined' ) {
67- return ;
68- }
69-
70- try {
71- globalThis . localStorage . setItem ( key , value ? '1' : '0' ) ;
72- } catch {
73- // Ignore storage failures to keep startup resilient.
74- }
75- }
76-
77- function isLocalDebugEnvironment ( documentRef ) {
78- const protocol = sanitizeText ( documentRef ?. location ?. protocol ) || sanitizeText ( globalThis ?. location ?. protocol ) ;
79- const hostname = sanitizeText ( documentRef ?. location ?. hostname ) || sanitizeText ( globalThis ?. location ?. hostname ) ;
80-
81- if ( protocol === 'file:' ) {
82- return true ;
83- }
84-
85- return hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1' ;
86- }
87-
88- function resolveDebugConfig ( documentRef ) {
89- const search = sanitizeText ( documentRef ?. location ?. search ) || sanitizeText ( globalThis ?. location ?. search ) ;
90- const searchParams = new URLSearchParams ( search ) ;
91- const queryMode = searchParams . get ( 'debugMode' ) ;
92- const queryEnabled = searchParams . get ( 'debug' ) ;
93- const queryRemember = searchParams . get ( 'rememberDebug' ) ;
94- const queryDemo = searchParams . get ( 'debugDemo' ) ;
95- const localDebugEnvironment = isLocalDebugEnvironment ( documentRef ) ;
96- const rememberDebugState = parseBooleanFlag ( queryRemember , false ) ;
97- const demoMode = parseBooleanFlag ( queryDemo , false ) ;
98- const defaultMode = localDebugEnvironment
99- ? 'dev'
100- : normalizeDebugMode ( BUILD_DEBUG_MODE , 'prod' ) ;
101- const debugMode = normalizeDebugMode ( queryMode , demoMode ? 'qa' : defaultMode ) ;
102- const fallbackEnabled = ( BUILD_DEBUG_ENABLED === true || localDebugEnvironment ) && debugMode !== 'prod' ;
103- const storedDebugEnabled = rememberDebugState && queryEnabled === null
104- ? readStoredBoolean ( DEBUG_STATE_STORAGE_KEY )
105- : null ;
106- const debugEnabled = demoMode
107- ? true
108- : parseBooleanFlag ( queryEnabled , storedDebugEnabled ?? fallbackEnabled ) ;
109-
110- if ( rememberDebugState ) {
111- writeStoredBoolean ( DEBUG_STATE_STORAGE_KEY , debugEnabled ) ;
112- }
113-
114- return {
115- debugMode,
116- debugEnabled,
117- rememberDebugState,
118- demoMode
119- } ;
120- }
121-
12231function applyDefaultDebugPreset ( devConsoleIntegration , presetCommands ) {
12332 if ( ! devConsoleIntegration || typeof devConsoleIntegration . executeCommand !== 'function' ) {
12433 return '' ;
0 commit comments