Skip to content

Commit

Permalink
fix(NetworkManagerGUI): prevent potential NRE spam if reference is lo…
Browse files Browse the repository at this point in the history
…st and tidy up (#1130)

* Add autoconfiguration bool option with tooltip

* System define and Awake function implementation

* Short-circuit to prevent NREs

Somehow NMGUI can lose references for some reason, so short-circuit to prevent log spam.

* update comment

* apply Visual Studio IDE0048 recommendation

"Add parentheses for clarity"

* Regions for tidying up

* Throw warning if trying to Auto Configure but NM field is set

If auto configure is set but NetworkManager field is not null, then don't do anything.

* possible future todo

* Update NetworkManagerGUI.cs

Remove regions as per James request

* fix ups as suggested by James' review
  • Loading branch information
SoftwareGuy committed Feb 3, 2023
1 parent 8a5cb91 commit 86b5c3d
Showing 1 changed file with 58 additions and 11 deletions.
69 changes: 58 additions & 11 deletions Assets/Mirage/Components/NetworkManagerGUI.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,63 @@
using System;
using UnityEngine;

namespace Mirage
{
public class NetworkManagerGUI : MonoBehaviour
{
public NetworkManager NetworkManager;
[Header("Defaults")]
[Tooltip("This is the default string displayed in the client address text box.")]
public string NetworkAddress = "localhost";

[Range(0.01f, 10f)]
[Header("References")]
[Tooltip("If enabled, we'll automatically reference NetworkManager in script's initialization.\nIf you've already set the NetworkManager field, auto configuring will not be performed.")]
public bool AutoConfigureNetworkManager = true;

[Tooltip("The value in this property field (if set) will not be overwritten if Auto Configuration is enabled.")]
public NetworkManager NetworkManager;

[Header("Cosmetics")]
[Range(0.01f, 10f), Tooltip("Adjusts scale of the GUI elements. 1x is normal size. 2x might be sane for big screens. <= 0.5x or > 2x is silly.")]
public float Scale = 1f;

[Tooltip("Select where you want the NetworkManagerGUI elements to be located on the screen.")]
public TextAnchor GUIAnchor = TextAnchor.UpperLeft;

private void Awake()
{
// Coburn, 2023-02-02:
// If automatic configuration of NetworkManager is enabled, then attempt to grab the
// NetworkManager that this script is attached to.
if (AutoConfigureNetworkManager)
{
// Ensure this is null before doing anything. If not, return early.
if (NetworkManager != null)
{
return;
}

// Attempt to get the NetworkManager component.
NetworkManager = GetComponent<NetworkManager>();

// Is this STILL null? Then we throw in the towel and go home.
if (NetworkManager == null)
{
throw new InvalidOperationException($"You requested automatic configuration for the NetworkManagerGUI component on '{gameObject.name}'" +
$" but one could not be found. Either disable automatic configuration or ensure this script is on a GameObject with a Mirage NetworkManager.");
}
}
}

private void OnGUI()
{
// Coburn, 2023-02-02: Apparently NMGUI can/may lose reference to NetworkManager for reasons unknown
// (maybe due to being in and out of DDOL and scene changes?). To prevent a NRE being spewed every OnGUI
// update, short-circuit here to prevent log spam.
if (NetworkManager == null)
{
return;
}

GUIUtility.ScaleAroundPivot(Vector2.one * Scale, GetPivotFromAnchor(GUIAnchor));

if (!NetworkManager.Server.Active && !NetworkManager.Client.Active)
Expand All @@ -29,6 +74,7 @@ private void StartButtons(Rect position)
{
GUILayout.BeginArea(position);

// Coburn, 2023-02-02, possible TODO: Remove the server buttons for WebGL since it can't be used as server
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
GUILayout.Box("WebGL cannot host");
Expand Down Expand Up @@ -90,6 +136,7 @@ private void StatusLabels(Rect position)
}
}
}

if (NetworkManager.Client.IsConnected)
{
GUILayout.Label($"Client: address={NetworkAddress}");
Expand All @@ -108,30 +155,26 @@ private void StatusLabels(Rect position)
GUILayout.EndArea();
}

private const int WIDTH = 200;
private const int PADDING_X = 10;
private const int PADDING_Y = 10;

private static Rect GetRectFromAnchor(TextAnchor anchor, int height)
{
switch (anchor)
{
case TextAnchor.UpperLeft:
return new Rect(PADDING_X, PADDING_Y, WIDTH, height);
case TextAnchor.UpperCenter:
return new Rect(Screen.width / 2 - (WIDTH / 2), PADDING_Y, WIDTH, height);
return new Rect((Screen.width / 2) - (WIDTH / 2), PADDING_Y, WIDTH, height);
case TextAnchor.UpperRight:
return new Rect(Screen.width - (WIDTH + PADDING_X), PADDING_Y, WIDTH, height);
case TextAnchor.MiddleLeft:
return new Rect(PADDING_X, Screen.height / 2 - (height / 2), WIDTH, height);
return new Rect(PADDING_X, (Screen.height / 2) - (height / 2), WIDTH, height);
case TextAnchor.MiddleCenter:
return new Rect(Screen.width / 2 - (WIDTH / 2), Screen.height / 2 - (height / 2), WIDTH, height);
return new Rect((Screen.width / 2) - (WIDTH / 2), (Screen.height / 2) - (height / 2), WIDTH, height);
case TextAnchor.MiddleRight:
return new Rect(Screen.width - (WIDTH + PADDING_X), Screen.height / 2 - (height / 2), WIDTH, height);
return new Rect(Screen.width - (WIDTH + PADDING_X), (Screen.height / 2) - (height / 2), WIDTH, height);
case TextAnchor.LowerLeft:
return new Rect(PADDING_X, Screen.height - (height + PADDING_Y), WIDTH, height);
case TextAnchor.LowerCenter:
return new Rect(Screen.width / 2 - (WIDTH / 2), Screen.height - (height + PADDING_Y), WIDTH, height);
return new Rect((Screen.width / 2) - (WIDTH / 2), Screen.height - (height + PADDING_Y), WIDTH, height);
default: // Lower right
return new Rect(Screen.width - (WIDTH + PADDING_X), Screen.height - (height + PADDING_Y), WIDTH, height);
}
Expand Down Expand Up @@ -176,5 +219,9 @@ private void ClickClient()
{
NetworkManager.Client.Connect(NetworkAddress);
}

private const int WIDTH = 200;
private const int PADDING_X = 10;
private const int PADDING_Y = 10;
}
}

0 comments on commit 86b5c3d

Please sign in to comment.