Skip to content

Commit

Permalink
Revert "fix: #2802 NetworkManager uses OnDestroy for shutdown instead…
Browse files Browse the repository at this point in the history
… of OnApplicationQuit (#3935)"

This reverts commit af133e6.
  • Loading branch information
miwarnec committed Dec 3, 2024
1 parent a10dc60 commit c61510b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 42 deletions.
57 changes: 27 additions & 30 deletions Assets/Mirror/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,32 @@ public void StopClient()
NetworkClient.Disconnect();
}

// called when quitting the application by closing the window / pressing
// stop in the editor. virtual so that inheriting classes'
// OnApplicationQuit() can call base.OnApplicationQuit() too
// (this can't be in OnDestroy: https://github.com/MirrorNetworking/Mirror/issues/3952)
public virtual void OnApplicationQuit()
{
// stop client first
// (we want to send the quit packet to the server instead of waiting
// for a timeout)
if (NetworkClient.isConnected)
{
StopClient();
//Debug.Log("OnApplicationQuit: stopped client");
}

// stop server after stopping client (for proper host mode stopping)
if (NetworkServer.active)
{
StopServer();
//Debug.Log("OnApplicationQuit: stopped server");
}

// Call ResetStatics to reset statics and singleton
ResetStatics();
}

/// <summary>Set the frame rate for a headless builds. Override to disable or modify.</summary>
// useful for dedicated servers.
// useful for headless benchmark clients.
Expand Down Expand Up @@ -747,41 +773,12 @@ public static void ResetStatics()
singleton = null;
}

// called when quitting the application by closing the window / pressing
// stop in the editor.
// use OnDestroy instead of OnApplicationQuit:
// fixes: https://github.com/MirrorNetworking/Mirror/issues/2802
// virtual so that inheriting classes' OnDestroy() can call base.OnDestroy() too
public virtual void OnDestroy()
{
//Debug.Log("NetworkManager destroyed");

// stop client first
// (we want to send the quit packet to the server instead of waiting
// for a timeout)
if (NetworkClient.isConnected)
{
StopClient();
//Debug.Log("OnApplicationQuit: stopped client");
}

// stop server after stopping client (for proper host mode stopping)
if (NetworkServer.active)
{
StopServer();
//Debug.Log("OnApplicationQuit: stopped server");
}

// Call ResetStatics to reset statics and singleton
ResetStatics();
}

// [Obsolete] in case someone is inheriting it.
// don't use this anymore.
// fixes: https://github.com/MirrorNetworking/Mirror/issues/2802
// DEPRECATED 2024-10-29
[Obsolete("Override OnDestroy instead of OnApplicationQuit.")]
public virtual void OnApplicationQuit() {}

/// <summary>The name of the current network scene.</summary>
// set by NetworkManager when changing the scene.
// new clients will automatically load this scene.
Expand Down
12 changes: 3 additions & 9 deletions Assets/Mirror/Core/Transport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,9 @@ public virtual void ServerLateUpdate() {}
/// <summary>Shut down the transport, both as client and server</summary>
public abstract void Shutdown();

// [Obsolete] in case someone is inheriting it.
// don't use this anymore.
// fixes: https://github.com/MirrorNetworking/Mirror/issues/2802
// DEPRECATED 2024-10-29
[Obsolete("Override OnDestroy instead of OnApplicationQuit.")]
public virtual void OnApplicationQuit() {}

// fixes: https://github.com/MirrorNetworking/Mirror/issues/2802
public virtual void OnDestroy()
/// <summary>Called by Unity when quitting. Inheriting Transports should call base for proper Shutdown.</summary>
// (this can't be in OnDestroy: https://github.com/MirrorNetworking/Mirror/issues/3952)
public virtual void OnApplicationQuit()
{
// stop transport (e.g. to shut down threads)
// (when pressing Stop in the Editor, Unity keeps threads alive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ public override void LateUpdate()
base.LateUpdate();
}

/// <summary>
/// Runs on both Server and Client
/// </summary>
public override void OnDestroy()
{
base.OnDestroy();
//UnityEngine.Debug.Log("OnDestroy");
}

#endregion

#region Start & Stop
Expand All @@ -78,10 +87,10 @@ public override void ConfigureHeadlessFrameRate()
/// <summary>
/// called when quitting the application by closing the window / pressing stop in the editor
/// </summary>
public override void OnDestroy()
public override void OnApplicationQuit()
{
base.OnDestroy();
//UnityEngine.Debug.Log("OnDestroy");
base.OnApplicationQuit();
//UnityEngine.Debug.Log("OnApplicationQuit");
}

#endregion
Expand Down
8 changes: 8 additions & 0 deletions Assets/Mirror/Examples/PlayerTest/PlayerTestNetMan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public override void ConfigureHeadlessFrameRate()
base.ConfigureHeadlessFrameRate();
}

/// <summary>
/// called when quitting the application by closing the window / pressing stop in the editor
/// </summary>
public override void OnApplicationQuit()
{
base.OnApplicationQuit();
}

#endregion

#region Scene Management
Expand Down

0 comments on commit c61510b

Please sign in to comment.