11using DG . Tweening ;
2+ using OpenAI . Realtime ;
23using SEE . GO ;
34using SEE . Net ;
45using SEE . Tools . OpenTelemetry ;
56using SEE . Utils . Config ;
67using SEE . Utils . Paths ;
78using Sirenix . OdinInspector ;
89using Sirenix . Serialization ;
10+ using System ;
911using System . Collections . Generic ;
1012using System . IO ;
1113using System . Threading ;
@@ -159,7 +161,44 @@ private void OnApplicationQuit()
159161 {
160162 TracingHelperService . Shutdown ( true ) ;
161163 User . VoiceChat . EndVoiceChat ( VoiceChat ) ;
162- Instance . Save ( ) ;
164+ }
165+
166+ /// <summary>
167+ /// Registers the quit callback when the object becomes enabled.
168+ /// This ensures that application shutdown can be handled gracefully.
169+ /// </summary>
170+ private void OnEnable ( )
171+ {
172+ Application . wantsToQuit += SaveOnQuit ;
173+ }
174+
175+ /// <summary>
176+ /// Unregisters the quit callback when the object is disabled.
177+ /// This prevents callbacks from being invoked on inactive objects.
178+ /// </summary>
179+ private void OnDisable ( )
180+ {
181+ Application . wantsToQuit -= SaveOnQuit ;
182+ }
183+
184+ /// <summary>
185+ /// Called when the application is about to quit.
186+ /// Attempts to save the current instance state before shutdown.
187+ /// </summary>
188+ /// <returns>
189+ /// Returns <c>true</c> to allow the application to quit.
190+ /// </returns>
191+ private bool SaveOnQuit ( )
192+ {
193+ try
194+ {
195+ Instance . Save ( ) ;
196+ }
197+ catch ( Exception e )
198+ {
199+ Debug . LogError ( $ "Error during quit: { e } \n ") ;
200+ }
201+ return true ;
163202 }
164203
165204 /// <summary>
@@ -311,12 +350,20 @@ private void Load(string filename)
311350 protected virtual void Save ( ConfigWriter writer )
312351 {
313352 Player . Save ( writer , playerLabel ) ;
314- Network . Save ( writer , networkLabel ) ;
315353 writer . Save ( VoiceChat . ToString ( ) , voiceChatLabel ) ;
316354 Telemetry . Save ( writer , telemetryLabel ) ;
317355 writer . Save ( InputType . ToString ( ) , inputTypeLabel ) ;
318356 Video . Save ( writer , videoLabel ) ;
319357 Audio . Save ( writer , audioLabel ) ;
358+ try
359+ {
360+ Network . Save ( writer , networkLabel ) ;
361+ }
362+ catch ( System . Exception )
363+ {
364+ Debug . LogError ( "Network settings could not be saved.\n " ) ;
365+ throw ;
366+ }
320367 }
321368
322369 /// <summary>
@@ -326,12 +373,12 @@ protected virtual void Save(ConfigWriter writer)
326373 protected virtual void Restore ( Dictionary < string , object > attributes )
327374 {
328375 Player . Restore ( attributes , playerLabel ) ;
329- Network . Restore ( attributes , networkLabel ) ;
330376 ConfigIO . RestoreEnum ( attributes , voiceChatLabel , ref VoiceChat ) ;
331377 Telemetry . Restore ( attributes , telemetryLabel ) ;
332378 ConfigIO . RestoreEnum ( attributes , inputTypeLabel , ref InputType ) ;
333379 Video . Restore ( attributes , videoLabel ) ;
334380 Audio . Restore ( attributes , audioLabel ) ;
381+ Network . Restore ( attributes , networkLabel ) ;
335382 }
336383
337384 #endregion Configuration I/O
0 commit comments