Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(NetworkManagerGUI): prevent potential NRE spam if reference is lost and tidy up #1130

Merged
merged 11 commits into from
Feb 3, 2023

Conversation

SoftwareGuy
Copy link
Contributor

@SoftwareGuy SoftwareGuy commented Feb 2, 2023

While chasing a ghost error due to Unity adding two NetworkManagerGUIs to one object and I forgot to check that, I discovered that the component and spam NREs in its OnGUI loop.

So, what this PR does is quite a fair bit actually...

  1. Short-circuits if there's no NetworkManager component specified and Auto Configure is turned off. This prevents crap loads of NRE spam due to OnGUI trying to access stuff on a null object.
  2. It introduces a new "Auto Configure" option which means people can slap on the component, tick a box and it'll automatically reference NetworkManager should it exist on the same GameObject. If it doesn't, it will throw an ArgumentNullException and tell them to fix it. However, I have implemented it so if you do specify a NetworkManager reference and leave it ticked to automatically configure itself that it will not override your manual reference.
  3. Adds tooltips and explanations as well as adds headers to the configuration options.
  4. Blows off a ton of dust and gives the code a restructure with regions, etc.

As an added bonus, it also addresses Visual Studio complaining about IDE0048: "Add parentheses for clarity" by... you name it, adding parentheses around some Rect math.

Mirage. Makin' NetworkManageGUI great again.

@SoftwareGuy SoftwareGuy changed the title refactor(NetworkManagerGUI): Prevent potential NRE spam if reference is lost and tidy up refactor(NetworkManagerGUI): prevent potential NRE spam if reference is lost and tidy up Feb 2, 2023
@SoftwareGuy
Copy link
Contributor Author

Example of what it looks like now:

image

Remove regions as per James request
// Is this STILL null? Then we throw in the towel and go home.
if (NetworkManager == null)
{
throw new ArgumentNullException("NetworkManager", $"You requested automatic configuration for the NetworkManagerGUI component on '{gameObject.name}'" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be InvalidOperationException instead of Argument

// Coburn, 2023-01-29:
// If automatic configuration of NetworkManager is enabled, then attempt to grab the
// NetworkManager that this script is attached to.
if (AutoConfigureNetworkManager)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it hard to follow this chain of if/else,, would be better to do early returns instead

{
if (logger.logEnabled)
{
logger.LogWarning($"You have enabled Auto Configure for this NetworkManagerGUI component on '{gameObject.name}' but the NetworkManager field is already set. " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we need this warning.

@James-Frowen James-Frowen merged commit 86b5c3d into MirageNet:master Feb 3, 2023
github-actions bot pushed a commit that referenced this pull request Feb 3, 2023
## [129.6.1](v129.6.0...v129.6.1) (2023-02-03)

### Bug Fixes

* fixing typo in DisallowMultipleComponent ([715aa33](715aa33))
* **NetworkManagerGUI:** prevent potential NRE spam if reference is lost and tidy up ([#1130](#1130)) ([86b5c3d](86b5c3d))
@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2023

🎉 This PR is included in version 129.6.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants