breaking: NetworkMatch component to hold matchId. Prepares for AOI2 match checker. (#2831)

* NetworkMatch component

* NetworkMatchChecker currentMatch points to NetworkMatch.matchId

* NetworkMatchChecker.Update just like scene checker

* remove NetworkMatchChecker.matchId

* adjust tests

* fixed comments, simplified null check

Co-authored-by: MrGadget1024 <github@clevertech.net>
This commit is contained in:
vis2k 2021-07-14 23:46:09 +08:00 committed by GitHub
parent fe36a83353
commit 79024450f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 701 additions and 614 deletions

View File

@ -0,0 +1,11 @@
// simple component that holds match information
using System;
namespace Mirror
{
public class NetworkMatch : NetworkBehaviour
{
///<summary>Set this to the same value on all networked objects that belong to a given match</summary>
public Guid matchId;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5d17e718851449a6879986e45c458fb7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -14,6 +14,7 @@ namespace Mirror
[DisallowMultipleComponent]
[AddComponentMenu("Network/NetworkMatchChecker")]
[RequireComponent(typeof(NetworkIdentity))]
[RequireComponent(typeof(NetworkMatch))]
[HelpURL("https://mirror-networking.gitbook.io/docs/components/network-match-checker")]
public class NetworkMatchChecker : NetworkVisibility
{
@ -22,59 +23,14 @@ public class NetworkMatchChecker : NetworkVisibility
new Dictionary<Guid, HashSet<NetworkIdentity>>();
// internal for tests
internal Guid currentMatch = Guid.Empty;
[Header("Diagnostics")]
[SyncVar]
public string currentMatchDebug;
/// <summary>
/// Set this to the same value on all networked objects that belong to a given match
/// </summary>
public Guid matchId
internal Guid currentMatch
{
get { return currentMatch; }
set
{
if (currentMatch == value) return;
// cache previous match so observers in that match can be rebuilt
Guid previousMatch = currentMatch;
// Set this to the new match this object just entered ...
currentMatch = value;
// ... and copy the string for the inspector because Unity can't show Guid directly
currentMatchDebug = currentMatch.ToString();
if (previousMatch != Guid.Empty)
{
// Remove this object from the hashset of the match it just left
matchPlayers[previousMatch].Remove(netIdentity);
// RebuildObservers of all NetworkIdentity's in the match this object just left
RebuildMatchObservers(previousMatch);
}
if (currentMatch != Guid.Empty)
{
// Make sure this new match is in the dictionary
if (!matchPlayers.ContainsKey(currentMatch))
matchPlayers.Add(currentMatch, new HashSet<NetworkIdentity>());
// Add this object to the hashset of the new match
matchPlayers[currentMatch].Add(netIdentity);
// RebuildObservers of all NetworkIdentity's in the match this object just entered
RebuildMatchObservers(currentMatch);
}
else
{
// Not in any match now...RebuildObservers will clear and add self
netIdentity.RebuildObservers(false);
}
}
get => GetComponent<NetworkMatch>().matchId;
set => GetComponent<NetworkMatch>().matchId = value;
}
internal Guid lastMatch;
public override void OnStartServer()
{
if (currentMatch == Guid.Empty) return;
@ -99,8 +55,7 @@ public override void OnStopServer()
void RebuildMatchObservers(Guid specificMatch)
{
foreach (NetworkIdentity networkIdentity in matchPlayers[specificMatch])
if (networkIdentity != null)
networkIdentity.RebuildObservers(false);
networkIdentity?.RebuildObservers(false);
}
#region Observers
@ -114,7 +69,7 @@ void RebuildMatchObservers(Guid specificMatch)
public override bool OnCheckObserver(NetworkConnection conn)
{
// Not Visible if not in a match
if (matchId == Guid.Empty)
if (currentMatch == Guid.Empty)
return false;
NetworkMatchChecker networkMatchChecker = conn.identity.GetComponent<NetworkMatchChecker>();
@ -122,7 +77,7 @@ public override bool OnCheckObserver(NetworkConnection conn)
if (networkMatchChecker == null)
return false;
return networkMatchChecker.matchId == matchId;
return networkMatchChecker.currentMatch == currentMatch;
}
/// <summary>
@ -141,5 +96,47 @@ public override void OnRebuildObservers(HashSet<NetworkConnection> observers, bo
}
#endregion
[ServerCallback]
void Update()
{
// only if changed
if (currentMatch == lastMatch)
return;
// This object is in a new match so observers in the prior match
// and the new match need to rebuild their respective observers lists.
// Remove this object from the hashset of the match it just left
if (lastMatch != Guid.Empty)
{
matchPlayers[lastMatch].Remove(netIdentity);
// RebuildObservers of all NetworkIdentity's in the match this
// object just left
RebuildMatchObservers(lastMatch);
}
if (currentMatch != Guid.Empty)
{
// Make sure this new match is in the dictionary
if (!matchPlayers.ContainsKey(currentMatch))
matchPlayers.Add(currentMatch, new HashSet<NetworkIdentity>());
// Add this object to the hashset of the new match
matchPlayers[currentMatch].Add(netIdentity);
// RebuildObservers of all NetworkIdentity's in the match this object just entered
RebuildMatchObservers(currentMatch);
}
else
{
// Not in any match now...RebuildObservers will clear and add self
netIdentity.RebuildObservers(false);
}
// save last rebuild's match
lastMatch = currentMatch;
}
}
}

View File

@ -10,6 +10,7 @@ GameObject:
m_Component:
- component: {fileID: 1204658229548937417}
- component: {fileID: 7660209086417859164}
- component: {fileID: -3992624706284245286}
- component: {fileID: -3043620344775994659}
m_Layer: 0
m_Name: MatchPlayer
@ -28,6 +29,7 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
@ -46,8 +48,21 @@ MonoBehaviour:
m_EditorClassIdentifier:
sceneId: 0
serverOnly: 0
m_AssetId:
visible: 0
m_AssetId: 11d2414f1e120a14c98cba6866e5348f
hasSpawned: 0
--- !u!114 &-3992624706284245286
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3559083313143303799}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5d17e718851449a6879986e45c458fb7, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &-3043620344775994659
MonoBehaviour:
m_ObjectHideFlags: 0

View File

@ -486,7 +486,7 @@ void OnServerStartMatch(NetworkConnection conn)
{
GameObject matchControllerObject = Instantiate(matchControllerPrefab);
#pragma warning disable 618
matchControllerObject.GetComponent<NetworkMatchChecker>().matchId = matchId;
matchControllerObject.GetComponent<NetworkMatch>().matchId = matchId;
#pragma warning restore 618
NetworkServer.Spawn(matchControllerObject);
@ -498,7 +498,7 @@ void OnServerStartMatch(NetworkConnection conn)
GameObject player = Instantiate(NetworkManager.singleton.playerPrefab);
#pragma warning disable 618
player.GetComponent<NetworkMatchChecker>().matchId = matchId;
player.GetComponent<NetworkMatch>().matchId = matchId;
#pragma warning restore 618
NetworkServer.AddPlayerForConnection(playerConn, player);

View File

@ -11,6 +11,8 @@ public class NetworkMatchCheckerTest : MirrorEditModeTest
GameObject player2;
GameObject player3;
#pragma warning disable 618
NetworkMatch player1Match;
NetworkMatch player2Match;
NetworkMatchChecker player1MatchChecker;
NetworkMatchChecker player2MatchChecker;
#pragma warning restore 618
@ -26,10 +28,10 @@ public override void SetUp()
base.SetUp();
#pragma warning disable 618
CreateNetworked(out player1, out NetworkIdentity _, out player1MatchChecker);
CreateNetworked(out player1, out NetworkIdentity _, out player1Match, out player1MatchChecker);
player1.name = "TestPlayer1";
CreateNetworked(out player2, out NetworkIdentity _, out player2MatchChecker);
CreateNetworked(out player2, out NetworkIdentity _, out player2Match, out player2MatchChecker);
player2.name = "TestPlayer2";
player3 = new GameObject("TestPlayer3", typeof(NetworkIdentity));
@ -126,14 +128,15 @@ public void OnCheckObserverShouldBeFalseForEmptyGuid()
Assert.IsFalse(player2Visable);
}
/*
[Test]
public void SettingMatchIdShouldRebuildObservers()
{
string guidMatch1 = Guid.NewGuid().ToString();
// make players join same match
player1MatchChecker.matchId = new Guid(guidMatch1);
player2MatchChecker.matchId = new Guid(guidMatch1);
player1Match.matchId = new Guid(guidMatch1);
player2Match.matchId = new Guid(guidMatch1);
// check player1's observers contains player 2
Assert.IsTrue(player1MatchChecker.netIdentity.observers.ContainsValue(player2MatchChecker.connectionToClient));
@ -148,11 +151,11 @@ public void ChangingMatchIdShouldRebuildObservers()
string guidMatch2 = Guid.NewGuid().ToString();
// make players join same match
player1MatchChecker.matchId = new Guid(guidMatch1);
player2MatchChecker.matchId = new Guid(guidMatch1);
player1Match.matchId = new Guid(guidMatch1);
player2Match.matchId = new Guid(guidMatch1);
// make player2 join different match
player2MatchChecker.matchId = new Guid(guidMatch2);
player2Match.matchId = new Guid(guidMatch2);
// check player1's observers does NOT contain player 2
Assert.IsFalse(player1MatchChecker.netIdentity.observers.ContainsValue(player2MatchChecker.connectionToClient));
@ -166,16 +169,17 @@ public void ClearingMatchIdShouldRebuildObservers()
string guidMatch1 = Guid.NewGuid().ToString();
// make players join same match
player1MatchChecker.matchId = new Guid(guidMatch1);
player2MatchChecker.matchId = new Guid(guidMatch1);
player1Match.matchId = new Guid(guidMatch1);
player2Match.matchId = new Guid(guidMatch1);
// make player 2 leave match
player2MatchChecker.matchId = Guid.Empty;
player2Match.matchId = Guid.Empty;
// check player1's observers does NOT contain player 2
Assert.IsFalse(player1MatchChecker.netIdentity.observers.ContainsValue(player2MatchChecker.connectionToClient));
// check player2's observers does NOT contain player 1
Assert.IsFalse(player2MatchChecker.netIdentity.observers.ContainsValue(player1MatchChecker.connectionToClient));
}
*/
}
}