MatchInterestManagement.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Mirror
  4. {
  5. public class MatchInterestManagement : InterestManagement
  6. {
  7. readonly Dictionary<Guid, HashSet<NetworkIdentity>> matchObjects =
  8. new Dictionary<Guid, HashSet<NetworkIdentity>>();
  9. readonly Dictionary<NetworkIdentity, Guid> lastObjectMatch =
  10. new Dictionary<NetworkIdentity, Guid>();
  11. HashSet<Guid> dirtyMatches = new HashSet<Guid>();
  12. public override void OnSpawned(NetworkIdentity identity)
  13. {
  14. Guid currentMatch = identity.GetComponent<NetworkMatch>().matchId;
  15. lastObjectMatch[identity] = currentMatch;
  16. // Guid.Empty is never a valid matchId...do not add to matchObjects collection
  17. if (currentMatch == Guid.Empty) return;
  18. // Debug.Log($"MatchInterestManagement.OnSpawned({identity.name}) currentMatch: {currentMatch}");
  19. if (!matchObjects.TryGetValue(currentMatch, out HashSet<NetworkIdentity> objects))
  20. {
  21. objects = new HashSet<NetworkIdentity>();
  22. matchObjects.Add(currentMatch, objects);
  23. }
  24. objects.Add(identity);
  25. }
  26. public override void OnDestroyed(NetworkIdentity identity)
  27. {
  28. Guid currentMatch = lastObjectMatch[identity];
  29. lastObjectMatch.Remove(identity);
  30. if (currentMatch != Guid.Empty && matchObjects.TryGetValue(currentMatch, out HashSet<NetworkIdentity> objects) && objects.Remove(identity))
  31. RebuildMatchObservers(currentMatch);
  32. }
  33. void Update()
  34. {
  35. // only on server
  36. if (!NetworkServer.active) return;
  37. // for each spawned:
  38. // if match changed:
  39. // add previous to dirty
  40. // add new to dirty
  41. foreach (NetworkIdentity netIdentity in NetworkIdentity.spawned.Values)
  42. {
  43. Guid currentMatch = lastObjectMatch[netIdentity];
  44. Guid newMatch = netIdentity.GetComponent<NetworkMatch>().matchId;
  45. if (newMatch == currentMatch) continue;
  46. // Mark new/old scenes as dirty so they get rebuilt
  47. // Guid.Empty is never a valid matchId
  48. if (currentMatch != Guid.Empty)
  49. dirtyMatches.Add(currentMatch);
  50. dirtyMatches.Add(newMatch);
  51. // This object is in a new match so observers in the prior match
  52. // and the new scene need to rebuild their respective observers lists.
  53. // Remove this object from the hashset of the match it just left
  54. // Guid.Empty is never a valid matchId
  55. if (currentMatch != Guid.Empty)
  56. matchObjects[currentMatch].Remove(netIdentity);
  57. // Set this to the new match this object just entered
  58. lastObjectMatch[netIdentity] = newMatch;
  59. // Guid.Empty is never a valid matchId...do not add to matchObjects collection
  60. if (newMatch == Guid.Empty) continue;
  61. // Make sure this new match is in the dictionary
  62. if (!matchObjects.ContainsKey(newMatch))
  63. matchObjects.Add(newMatch, new HashSet<NetworkIdentity>());
  64. // Add this object to the hashset of the new match
  65. matchObjects[newMatch].Add(netIdentity);
  66. }
  67. // rebuild all dirty matchs
  68. foreach (Guid dirtyMatch in dirtyMatches)
  69. {
  70. RebuildMatchObservers(dirtyMatch);
  71. }
  72. dirtyMatches.Clear();
  73. }
  74. void RebuildMatchObservers(Guid matchId)
  75. {
  76. foreach (NetworkIdentity netIdentity in matchObjects[matchId])
  77. if (netIdentity != null)
  78. NetworkServer.RebuildObservers(netIdentity, false);
  79. }
  80. public override bool OnCheckObserver(NetworkIdentity identity, NetworkConnection newObserver)
  81. {
  82. return identity.GetComponent<NetworkMatch>().matchId ==
  83. newObserver.identity.GetComponent<NetworkMatch>().matchId;
  84. }
  85. public override void OnRebuildObservers(NetworkIdentity identity, HashSet<NetworkConnection> newObservers,
  86. bool initialize)
  87. {
  88. Guid matchId = identity.GetComponent<NetworkMatch>().matchId;
  89. // Guid.Empty is never a valid matchId
  90. if (matchId == Guid.Empty) return;
  91. if (!matchObjects.TryGetValue(matchId, out HashSet<NetworkIdentity> objects))
  92. return;
  93. // Add everything in the hashset for this object's current match
  94. foreach (NetworkIdentity networkIdentity in objects)
  95. if (networkIdentity != null && networkIdentity.connectionToClient != null)
  96. newObservers.Add(networkIdentity.connectionToClient);
  97. }
  98. }
  99. }