Skip to content

Commit 7cffa59

Browse files
authored
Merge pull request #900 from uni-bremen-agst/898-usersettings-are-not-properly-saved
898 usersettings are not properly saved Closes #898
2 parents cd9d1c7 + 8bd9db9 commit 7cffa59

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

Assets/SEE/Net/Network.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -911,12 +911,24 @@ private IList<AddressInfo> AddressesInfo
911911
public virtual void Save(ConfigWriter writer, string label)
912912
{
913913
writer.BeginGroup(label);
914-
writer.Save(GameScene, gameSceneLabel);
915-
writer.Save(ServerPort, serverPortLabel);
916-
writer.Save(ServerIP4Address, serverIP4AddressLabel);
917-
writer.Save(RoomPassword, roomPasswordLabel);
918-
writer.Save(BackendServerAPI, backendServerAPILabel);
919-
writer.EndGroup();
914+
try
915+
{
916+
writer.Save(GameScene, gameSceneLabel);
917+
writer.Save(RoomPassword, roomPasswordLabel);
918+
writer.Save(BackendServerAPI, backendServerAPILabel);
919+
writer.Save(ServerPort, serverPortLabel);
920+
writer.Save(ServerIP4Address, serverIP4AddressLabel);
921+
}
922+
catch (System.Exception e)
923+
{
924+
Debug.LogError("An error occurred while saving the user settings.\n");
925+
Debug.LogException(e);
926+
throw;
927+
}
928+
finally
929+
{
930+
writer.EndGroup();
931+
}
920932
}
921933

922934
/// <summary>
@@ -931,7 +943,6 @@ public virtual void Restore(Dictionary<string, object> attributes, string label)
931943
Dictionary<string, object> values = dictionary as Dictionary<string, object>;
932944

933945
ConfigIO.Restore(values, gameSceneLabel, ref GameScene);
934-
ConfigIO.Restore(values, roomPasswordLabel, ref RoomPassword);
935946
{
936947
int value = ServerPort;
937948
ConfigIO.Restore(values, serverPortLabel, ref value);
@@ -942,6 +953,7 @@ public virtual void Restore(Dictionary<string, object> attributes, string label)
942953
ConfigIO.Restore(values, serverIP4AddressLabel, ref value);
943954
ServerIP4Address = value;
944955
}
956+
ConfigIO.Restore(values, roomPasswordLabel, ref RoomPassword);
945957
ConfigIO.Restore(values, backendServerAPILabel, ref BackendServerAPI);
946958
}
947959
}

Assets/SEE/UserSettings/UserSettings.cs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using DG.Tweening;
2+
using OpenAI.Realtime;
23
using SEE.GO;
34
using SEE.Net;
45
using SEE.Tools.OpenTelemetry;
56
using SEE.Utils.Config;
67
using SEE.Utils.Paths;
78
using Sirenix.OdinInspector;
89
using Sirenix.Serialization;
10+
using System;
911
using System.Collections.Generic;
1012
using System.IO;
1113
using 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

Comments
 (0)