Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit 3d6d142

Browse files
Move ExitCleanup to GameManager.cs
1 parent bbf8015 commit 3d6d142

2 files changed

Lines changed: 52 additions & 50 deletions

File tree

Scripts/Scenes/Main/GameManager.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Godot;
22
using System;
3+
using System.Threading.Tasks;
34

45
namespace GodotModules
56
{
@@ -26,6 +27,55 @@ public override void _Input(InputEvent @event)
2627
UtilOptions.ToggleFullscreen();
2728
}
2829

30+
public override async void _Notification(int what)
31+
{
32+
if (what == MainLoop.NotificationWmQuitRequest)
33+
{
34+
Instance.GetTree().SetAutoAcceptQuit(false);
35+
36+
await ExitCleanup();
37+
}
38+
}
39+
40+
/// <summary>
41+
/// All cleanup should be done in here
42+
/// </summary>
43+
private static async Task ExitCleanup()
44+
{
45+
try
46+
{
47+
if (NetworkManager.GameServer != null)
48+
if (NetworkManager.GameServer.IsRunning)
49+
{
50+
NetworkManager.GameServer.ENetCmds.Enqueue(new ENetCmd(ENetOpcode.ClientWantsToExitApp));
51+
NetworkManager.GameServer.Dispose();
52+
NetworkManager.GameServer.Stop();
53+
}
54+
55+
if (NetworkManager.GameClient != null)
56+
if (NetworkManager.GameClient.IsRunning)
57+
{
58+
NetworkManager.GameClient.Dispose();
59+
NetworkManager.GameClient.Stop();
60+
}
61+
62+
UtilOptions.SaveOptions();
63+
NetworkManager.WebClient.Dispose();
64+
65+
//if (SceneGameServers.PingServersCTS != null)
66+
//SceneGameServers.PingServersCTS.Dispose();
67+
if (NetworkManager.ClientConnectingTokenSource != null)
68+
NetworkManager.ClientConnectingTokenSource.Dispose();
69+
}
70+
catch (Exception e)
71+
{
72+
Logger.LogErr(e, "Game exit cleanup");
73+
await Task.Delay(3000);
74+
}
75+
76+
Instance.GetTree().Quit();
77+
}
78+
2979
public static void SpawnPopupMessage(string message)
3080
{
3181
var popupMessage = Prefabs.PopupMessage.Instance<UIPopupMessage>();

Scripts/Scenes/Main/NetworkManager.cs

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public class NetworkManager : Node
1414
public static ConcurrentQueue<GodotCmd> GodotCmds { get; set; }
1515
public static GameServer GameServer { get; set; }
1616
public static GameClient GameClient { get; set; }
17-
private static WebClient WebClient { get; set; }
17+
public static WebClient WebClient { get; set; }
1818
public static NetworkManager Instance { get; set; }
19-
private static CancellationTokenSource ClientConnectingTokenSource { get; set; }
19+
public static CancellationTokenSource ClientConnectingTokenSource { get; set; }
2020

2121
public static DisconnectOpcode DisconnectOpcode { get; set; }
2222
public static uint PeerId { get; set; } // this clients peer id (grabbed from server at some point)
@@ -107,54 +107,6 @@ public override async void _Process(float delta)
107107
}
108108
}
109109

110-
public override void _Notification(int what)
111-
{
112-
if (what == MainLoop.NotificationWmQuitRequest)
113-
{
114-
Instance.GetTree().SetAutoAcceptQuit(false);
115-
116-
ExitCleanup();
117-
}
118-
}
119-
120-
/// <summary>
121-
/// All cleanup should be done in here
122-
/// </summary>
123-
private static void ExitCleanup()
124-
{
125-
try
126-
{
127-
if (NetworkManager.GameServer != null)
128-
if (NetworkManager.GameServer.IsRunning)
129-
{
130-
GameServer.ENetCmds.Enqueue(new ENetCmd(ENetOpcode.ClientWantsToExitApp));
131-
GameServer.Dispose();
132-
GameServer.Stop();
133-
}
134-
135-
if (NetworkManager.GameClient != null)
136-
if (NetworkManager.GameClient.IsRunning)
137-
{
138-
GameClient.Dispose();
139-
GameClient.Stop();
140-
}
141-
142-
UtilOptions.SaveOptions();
143-
WebClient.Dispose();
144-
145-
//if (SceneGameServers.PingServersCTS != null)
146-
//SceneGameServers.PingServersCTS.Dispose();
147-
if (ClientConnectingTokenSource != null)
148-
ClientConnectingTokenSource.Dispose();
149-
}
150-
catch (Exception e)
151-
{
152-
GD.Print("Exception on game exit cleanup: " + e);
153-
}
154-
155-
Instance.GetTree().Quit();
156-
}
157-
158110
public static void StartClient(string ip, ushort port)
159111
{
160112
if (GameClient != null)

0 commit comments

Comments
 (0)