NetworkIdentity.cs 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using Mirror.RemoteCalls;
  5. using UnityEngine;
  6. using UnityEngine.Serialization;
  7. #if UNITY_EDITOR
  8. using UnityEditor;
  9. #if UNITY_2021_2_OR_NEWER
  10. using UnityEditor.SceneManagement;
  11. #else
  12. using UnityEditor.Experimental.SceneManagement;
  13. #endif
  14. #endif
  15. namespace Mirror
  16. {
  17. // Default = use interest management
  18. // ForceHidden = useful to hide monsters while they respawn etc.
  19. // ForceShown = useful to have score NetworkIdentities that always broadcast
  20. // to everyone etc.
  21. public enum Visibility { Default, ForceHidden, ForceShown }
  22. public struct NetworkIdentitySerialization
  23. {
  24. // IMPORTANT: int tick avoids floating point inaccuracy over days/weeks
  25. public int tick;
  26. public NetworkWriter ownerWriter;
  27. public NetworkWriter observersWriter;
  28. }
  29. /// <summary>NetworkIdentity identifies objects across the network.</summary>
  30. [DisallowMultipleComponent]
  31. // NetworkIdentity.Awake initializes all NetworkComponents.
  32. // let's make sure it's always called before their Awake's.
  33. [DefaultExecutionOrder(-1)]
  34. [AddComponentMenu("Network/Network Identity")]
  35. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-identity")]
  36. public sealed class NetworkIdentity : MonoBehaviour
  37. {
  38. /// <summary>Returns true if running as a client and this object was spawned by a server.</summary>
  39. //
  40. // IMPORTANT:
  41. // OnStartClient sets it to true. we NEVER set it to false after.
  42. // otherwise components like Skillbars couldn't use OnDestroy()
  43. // for saving, etc. since isClient may have been reset before
  44. // OnDestroy was called.
  45. //
  46. // we also DO NOT make it dependent on NetworkClient.active or similar.
  47. // we set it, then never change it. that's the user's expectation too.
  48. //
  49. // => fixes https://github.com/vis2k/Mirror/issues/1475
  50. public bool isClient { get; internal set; }
  51. /// <summary>Returns true if NetworkServer.active and server is not stopped.</summary>
  52. //
  53. // IMPORTANT:
  54. // OnStartServer sets it to true. we NEVER set it to false after.
  55. // otherwise components like Skillbars couldn't use OnDestroy()
  56. // for saving, etc. since isServer may have been reset before
  57. // OnDestroy was called.
  58. //
  59. // we also DO NOT make it dependent on NetworkServer.active or similar.
  60. // we set it, then never change it. that's the user's expectation too.
  61. //
  62. // => fixes https://github.com/vis2k/Mirror/issues/1484
  63. // => fixes https://github.com/vis2k/Mirror/issues/2533
  64. public bool isServer { get; internal set; }
  65. /// <summary>Return true if this object represents the player on the local machine.</summary>
  66. //
  67. // IMPORTANT:
  68. // OnStartLocalPlayer sets it to true. we NEVER set it to false after.
  69. // otherwise components like Skillbars couldn't use OnDestroy()
  70. // for saving, etc. since isLocalPlayer may have been reset before
  71. // OnDestroy was called.
  72. //
  73. // we also DO NOT make it dependent on NetworkClient.localPlayer or similar.
  74. // we set it, then never change it. that's the user's expectation too.
  75. //
  76. // => fixes https://github.com/vis2k/Mirror/issues/2615
  77. public bool isLocalPlayer { get; internal set; }
  78. /// <summary>True if this object only exists on the server</summary>
  79. public bool isServerOnly => isServer && !isClient;
  80. /// <summary>True if this object exists on a client that is not also acting as a server.</summary>
  81. public bool isClientOnly => isClient && !isServer;
  82. /// <summary>isOwned is true on the client if this NetworkIdentity is one of the .owned entities of our connection on the server.</summary>
  83. // for example: main player & pets are owned. monsters & npcs aren't.
  84. public bool isOwned { get; internal set; }
  85. // public so NetworkManager can reset it from StopClient.
  86. public bool clientStarted;
  87. /// <summary>The set of network connections (players) that can see this object.</summary>
  88. public readonly Dictionary<int, NetworkConnectionToClient> observers =
  89. new Dictionary<int, NetworkConnectionToClient>();
  90. /// <summary>The unique network Id of this object (unique at runtime).</summary>
  91. public uint netId { get; internal set; }
  92. /// <summary>Unique identifier for NetworkIdentity objects within a scene, used for spawning scene objects.</summary>
  93. // persistent scene id <sceneHash/32,sceneId/32> (see AssignSceneID comments)
  94. [FormerlySerializedAs("m_SceneId"), HideInInspector]
  95. public ulong sceneId;
  96. // assetId used to spawn prefabs across the network.
  97. // originally a Guid, but a 4 byte uint is sufficient
  98. // (as suggested by james)
  99. //
  100. // it's also easier to work with for serialization etc.
  101. // serialized and visible in inspector for easier debugging
  102. [SerializeField, HideInInspector] uint _assetId;
  103. // The AssetId trick:
  104. // Ideally we would have a serialized 'Guid m_AssetId' but Unity can't
  105. // serialize it because Guid's internal bytes are private
  106. //
  107. // Using just the Guid string would work, but it's 32 chars long and
  108. // would then be sent over the network as 64 instead of 16 bytes
  109. //
  110. // => The solution is to serialize the string internally here and then
  111. // use the real 'Guid' type for everything else via .assetId
  112. public uint assetId
  113. {
  114. get
  115. {
  116. #if UNITY_EDITOR
  117. // old UNET comment:
  118. // This is important because sometimes OnValidate does not run
  119. // (like when adding NetworkIdentity to prefab with no child links)
  120. if (_assetId == 0)
  121. SetupIDs();
  122. #endif
  123. return _assetId;
  124. }
  125. // assetId is set internally when creating or duplicating a prefab
  126. internal set
  127. {
  128. // should never be empty
  129. if (value == 0)
  130. {
  131. Debug.LogError($"Can not set AssetId to empty guid on NetworkIdentity '{name}', old assetId '{_assetId}'");
  132. return;
  133. }
  134. // always set it otherwise.
  135. // for new prefabs, it will set from 0 to N.
  136. // for duplicated prefabs, it will set from N to M.
  137. // either way, it's always set to a valid GUID.
  138. _assetId = value;
  139. // Debug.Log($"Setting AssetId on NetworkIdentity '{name}', new assetId '{value:X4}'");
  140. }
  141. }
  142. /// <summary>Make this object only exist when the game is running as a server (or host).</summary>
  143. [FormerlySerializedAs("m_ServerOnly")]
  144. [Tooltip("Prevents this object from being spawned / enabled on clients")]
  145. public bool serverOnly;
  146. // Set before Destroy is called so that OnDestroy doesn't try to destroy
  147. // the object again
  148. internal bool destroyCalled;
  149. /// <summary>Client's network connection to the server. This is only valid for player objects on the client.</summary>
  150. // TODO change to NetworkConnectionToServer, but might cause some breaking
  151. public NetworkConnection connectionToServer { get; internal set; }
  152. /// <summary>Server's network connection to the client. This is only valid for client-owned objects (including the Player object) on the server.</summary>
  153. public NetworkConnectionToClient connectionToClient
  154. {
  155. get => _connectionToClient;
  156. internal set
  157. {
  158. _connectionToClient?.RemoveOwnedObject(this);
  159. _connectionToClient = value;
  160. _connectionToClient?.AddOwnedObject(this);
  161. }
  162. }
  163. NetworkConnectionToClient _connectionToClient;
  164. // get all NetworkBehaviour components
  165. public NetworkBehaviour[] NetworkBehaviours { get; private set; }
  166. // to save bandwidth, we send one 64 bit dirty mask
  167. // instead of 1 byte index per dirty component.
  168. // which means we can't allow > 64 components (it's enough).
  169. const int MaxNetworkBehaviours = 64;
  170. // current visibility
  171. //
  172. // Default = use interest management
  173. // ForceHidden = useful to hide monsters while they respawn etc.
  174. // ForceShown = useful to have score NetworkIdentities that always broadcast
  175. // to everyone etc.
  176. //
  177. // TODO rename to 'visibility' after removing .visibility some day!
  178. [Tooltip("Visibility can overwrite interest management. ForceHidden can be useful to hide monsters while they respawn. ForceShown can be useful for score NetworkIdentities that should always broadcast to everyone in the world.")]
  179. public Visibility visible = Visibility.Default;
  180. // broadcasting serializes all entities around a player for each player.
  181. // we don't want to serialize one entity twice in the same tick.
  182. // so we cache the last serialization and remember the timestamp so we
  183. // know which Update it was serialized.
  184. // (timestamp is the same while inside Update)
  185. // => this way we don't need to pool thousands of writers either.
  186. // => way easier to store them per object
  187. NetworkIdentitySerialization lastSerialization = new NetworkIdentitySerialization
  188. {
  189. ownerWriter = new NetworkWriter(),
  190. observersWriter = new NetworkWriter()
  191. };
  192. // Keep track of all sceneIds to detect scene duplicates
  193. static readonly Dictionary<ulong, NetworkIdentity> sceneIds =
  194. new Dictionary<ulong, NetworkIdentity>();
  195. // Helper function to handle Command/Rpc
  196. internal void HandleRemoteCall(byte componentIndex, ushort functionHash, RemoteCallType remoteCallType, NetworkReader reader, NetworkConnectionToClient senderConnection = null)
  197. {
  198. // check if unity object has been destroyed
  199. if (this == null)
  200. {
  201. Debug.LogWarning($"{remoteCallType} [{functionHash}] received for deleted object [netId={netId}]");
  202. return;
  203. }
  204. // find the right component to invoke the function on
  205. if (componentIndex >= NetworkBehaviours.Length)
  206. {
  207. Debug.LogWarning($"Component [{componentIndex}] not found for [netId={netId}]");
  208. return;
  209. }
  210. NetworkBehaviour invokeComponent = NetworkBehaviours[componentIndex];
  211. if (!RemoteProcedureCalls.Invoke(functionHash, remoteCallType, reader, invokeComponent, senderConnection))
  212. {
  213. Debug.LogError($"Found no receiver for incoming {remoteCallType} [{functionHash}] on {gameObject.name}, the server and client should have the same NetworkBehaviour instances [netId={netId}].");
  214. }
  215. }
  216. // RuntimeInitializeOnLoadMethod -> fast playmode without domain reload
  217. // internal so it can be called from NetworkServer & NetworkClient
  218. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  219. internal static void ResetStatics()
  220. {
  221. // reset ALL statics
  222. ResetClientStatics();
  223. ResetServerStatics();
  224. }
  225. // reset only client sided statics.
  226. // don't touch server statics when calling StopClient in host mode.
  227. // https://github.com/vis2k/Mirror/issues/2954
  228. internal static void ResetClientStatics()
  229. {
  230. previousLocalPlayer = null;
  231. clientAuthorityCallback = null;
  232. }
  233. internal static void ResetServerStatics()
  234. {
  235. nextNetworkId = 1;
  236. }
  237. /// <summary>Gets the NetworkIdentity from the sceneIds dictionary with the corresponding id</summary>
  238. public static NetworkIdentity GetSceneIdentity(ulong id) => sceneIds[id];
  239. static uint nextNetworkId = 1;
  240. internal static uint GetNextNetworkId() => nextNetworkId++;
  241. /// <summary>Resets nextNetworkId = 1</summary>
  242. public static void ResetNextNetworkId() => nextNetworkId = 1;
  243. /// <summary>The delegate type for the clientAuthorityCallback.</summary>
  244. public delegate void ClientAuthorityCallback(NetworkConnectionToClient conn, NetworkIdentity identity, bool authorityState);
  245. /// <summary> A callback that can be populated to be notified when the client-authority state of objects changes.</summary>
  246. public static event ClientAuthorityCallback clientAuthorityCallback;
  247. // hasSpawned should always be false before runtime
  248. [SerializeField, HideInInspector] bool hasSpawned;
  249. public bool SpawnedFromInstantiate { get; private set; }
  250. // NetworkBehaviour components are initialized in Awake once.
  251. // Changing them at runtime would get client & server out of sync.
  252. // BUT internal so tests can add them after creating the NetworkIdentity
  253. internal void InitializeNetworkBehaviours()
  254. {
  255. // Get all NetworkBehaviour components, including children.
  256. // Some users need NetworkTransform on child bones, etc.
  257. // => Deterministic: https://forum.unity.com/threads/getcomponentsinchildren.4582/#post-33983
  258. // => Never null. GetComponents returns [] if none found.
  259. // => Include inactive. We need all child components.
  260. NetworkBehaviours = GetComponentsInChildren<NetworkBehaviour>(true);
  261. ValidateComponents();
  262. // initialize each one
  263. for (int i = 0; i < NetworkBehaviours.Length; ++i)
  264. {
  265. NetworkBehaviour component = NetworkBehaviours[i];
  266. component.netIdentity = this;
  267. component.ComponentIndex = (byte)i;
  268. }
  269. }
  270. void ValidateComponents()
  271. {
  272. if (NetworkBehaviours == null)
  273. {
  274. Debug.LogError($"NetworkBehaviours array is null on {gameObject.name}!\n" +
  275. $"Typically this can happen when a networked object is a child of a " +
  276. $"non-networked parent that's disabled, preventing Awake on the networked object " +
  277. $"from being invoked, where the NetworkBehaviours array is initialized.", gameObject);
  278. }
  279. else if (NetworkBehaviours.Length > MaxNetworkBehaviours)
  280. {
  281. Debug.LogError($"NetworkIdentity {name} has too many NetworkBehaviour components: only {MaxNetworkBehaviours} NetworkBehaviour components are allowed in order to save bandwidth.", this);
  282. }
  283. }
  284. // Awake is only called in Play mode.
  285. // internal so we can call it during unit tests too.
  286. internal void Awake()
  287. {
  288. // initialize NetworkBehaviour components.
  289. // Awake() is called immediately after initialization.
  290. // no one can overwrite it because NetworkIdentity is sealed.
  291. // => doing it here is the fastest and easiest solution.
  292. InitializeNetworkBehaviours();
  293. if (hasSpawned)
  294. {
  295. Debug.LogError($"{name} has already spawned. Don't call Instantiate for NetworkIdentities that were in the scene since the beginning (aka scene objects). Otherwise the client won't know which object to use for a SpawnSceneObject message.");
  296. SpawnedFromInstantiate = true;
  297. Destroy(gameObject);
  298. }
  299. hasSpawned = true;
  300. }
  301. void OnValidate()
  302. {
  303. // OnValidate is not called when using Instantiate, so we can use
  304. // it to make sure that hasSpawned is false
  305. hasSpawned = false;
  306. #if UNITY_EDITOR
  307. DisallowChildNetworkIdentities();
  308. SetupIDs();
  309. #endif
  310. }
  311. // expose our AssetId Guid to uint mapping code in case projects need to map Guids to uint as well.
  312. // this way their projects won't break if we change our mapping algorithm.
  313. // needs to be available at runtime / builds, don't wrap in #if UNITY_EDITOR
  314. public static uint AssetGuidToUint(Guid guid) => (uint)guid.GetHashCode(); // deterministic
  315. #if UNITY_EDITOR
  316. // child NetworkIdentities are not supported.
  317. // Disallow them and show an error for the user to fix.
  318. // This needs to work for Prefabs & Scene objects, so the previous check
  319. // in NetworkClient.RegisterPrefab is not enough.
  320. void DisallowChildNetworkIdentities()
  321. {
  322. #if UNITY_2020_3_OR_NEWER
  323. NetworkIdentity[] identities = GetComponentsInChildren<NetworkIdentity>(true);
  324. #else
  325. NetworkIdentity[] identities = GetComponentsInChildren<NetworkIdentity>();
  326. #endif
  327. if (identities.Length > 1)
  328. {
  329. // always log the next child component so it's easy to fix.
  330. // if there are multiple, then after removing it'll log the next.
  331. Debug.LogError($"'{name}' has another NetworkIdentity component on '{identities[1].name}'. There should only be one NetworkIdentity, and it must be on the root object. Please remove the other one.");
  332. }
  333. }
  334. void AssignAssetID(string path)
  335. {
  336. // only set if not empty. fixes https://github.com/vis2k/Mirror/issues/2765
  337. if (!string.IsNullOrWhiteSpace(path))
  338. {
  339. // if we generate the assetId then we MUST be sure to set dirty
  340. // in order to save the prefab object properly. otherwise it
  341. // would be regenerated every time we reopen the prefab.
  342. // -> Undo.RecordObject is the new EditorUtility.SetDirty!
  343. // -> we need to call it before changing.
  344. //
  345. // to verify this, duplicate a prefab and double click to open it.
  346. // add a log message if "_assetId != before_".
  347. // without RecordObject, it'll log every time because it's not saved.
  348. Undo.RecordObject(this, "Assigned AssetId");
  349. // uint before = _assetId;
  350. Guid guid = new Guid(AssetDatabase.AssetPathToGUID(path));
  351. assetId = AssetGuidToUint(guid);
  352. // if (_assetId != before) Debug.Log($"Assigned assetId={assetId} to {name}");
  353. }
  354. }
  355. void AssignAssetID(GameObject prefab) => AssignAssetID(AssetDatabase.GetAssetPath(prefab));
  356. // persistent sceneId assignment
  357. // (because scene objects have no persistent unique ID in Unity)
  358. //
  359. // original UNET used OnPostProcessScene to assign an index based on
  360. // FindObjectOfType<NetworkIdentity> order.
  361. // -> this didn't work because FindObjectOfType order isn't deterministic.
  362. // -> one workaround is to sort them by sibling paths, but it can still
  363. // get out of sync when we open scene2 in editor and we have
  364. // DontDestroyOnLoad objects that messed with the sibling index.
  365. //
  366. // we absolutely need a persistent id. challenges:
  367. // * it needs to be 0 for prefabs
  368. // => we set it to 0 in SetupIDs() if prefab!
  369. // * it needs to be only assigned in edit time, not at runtime because
  370. // only the objects that were in the scene since beginning should have
  371. // a scene id.
  372. // => Application.isPlaying check solves that
  373. // * it needs to detect duplicated sceneIds after duplicating scene
  374. // objects
  375. // => sceneIds dict takes care of that
  376. // * duplicating the whole scene file shouldn't result in duplicate
  377. // scene objects
  378. // => buildIndex is shifted into sceneId for that.
  379. // => if we have no scenes in build index then it doesn't matter
  380. // because by definition a build can't switch to other scenes
  381. // => if we do have scenes in build index then it will be != -1
  382. // note: the duplicated scene still needs to be opened once for it to
  383. // be set properly
  384. // * scene objects need the correct scene index byte even if the scene's
  385. // build index was changed or a duplicated scene wasn't opened yet.
  386. // => OnPostProcessScene is the only function that gets called for
  387. // each scene before runtime, so this is where we set the scene
  388. // byte.
  389. // * disabled scenes in build settings should result in same scene index
  390. // in editor and in build
  391. // => .gameObject.scene.buildIndex filters out disabled scenes by
  392. // default
  393. // * generated sceneIds absolutely need to set scene dirty and force the
  394. // user to resave.
  395. // => Undo.RecordObject does that perfectly.
  396. // * sceneIds should never be generated temporarily for unopened scenes
  397. // when building, otherwise editor and build get out of sync
  398. // => BuildPipeline.isBuildingPlayer check solves that
  399. void AssignSceneID()
  400. {
  401. // we only ever assign sceneIds at edit time, never at runtime.
  402. // by definition, only the original scene objects should get one.
  403. // -> if we assign at runtime then server and client would generate
  404. // different random numbers!
  405. if (Application.isPlaying)
  406. return;
  407. // no valid sceneId yet, or duplicate?
  408. bool duplicate = sceneIds.TryGetValue(sceneId, out NetworkIdentity existing) && existing != null && existing != this;
  409. if (sceneId == 0 || duplicate)
  410. {
  411. // clear in any case, because it might have been a duplicate
  412. sceneId = 0;
  413. // if a scene was never opened and we are building it, then a
  414. // sceneId would be assigned to build but not saved in editor,
  415. // resulting in them getting out of sync.
  416. // => don't ever assign temporary ids. they always need to be
  417. // permanent
  418. // => throw an exception to cancel the build and let the user
  419. // know how to fix it!
  420. if (BuildPipeline.isBuildingPlayer)
  421. throw new InvalidOperationException($"Scene {gameObject.scene.path} needs to be opened and resaved before building, because the scene object {name} has no valid sceneId yet.");
  422. // if we generate the sceneId then we MUST be sure to set dirty
  423. // in order to save the scene object properly. otherwise it
  424. // would be regenerated every time we reopen the scene, and
  425. // upgrading would be very difficult.
  426. // -> Undo.RecordObject is the new EditorUtility.SetDirty!
  427. // -> we need to call it before changing.
  428. Undo.RecordObject(this, "Generated SceneId");
  429. // generate random sceneId part (0x00000000FFFFFFFF)
  430. uint randomId = Utils.GetTrueRandomUInt();
  431. // only assign if not a duplicate of an existing scene id
  432. // (small chance, but possible)
  433. duplicate = sceneIds.TryGetValue(randomId, out existing) && existing != null && existing != this;
  434. if (!duplicate)
  435. {
  436. sceneId = randomId;
  437. //Debug.Log($"{name} in scene {gameObject.scene.name} sceneId assigned to:{sceneId:X}");
  438. }
  439. }
  440. // add to sceneIds dict no matter what
  441. // -> even if we didn't generate anything new, because we still need
  442. // existing sceneIds in there to check duplicates
  443. sceneIds[sceneId] = this;
  444. }
  445. // copy scene path hash into sceneId for scene objects.
  446. // this is the only way for scene file duplication to not contain
  447. // duplicate sceneIds as it seems.
  448. // -> sceneId before: 0x00000000AABBCCDD
  449. // -> then we clear the left 4 bytes, so that our 'OR' uses 0x00000000
  450. // -> then we OR the hash into the 0x00000000 part
  451. // -> buildIndex is not enough, because Editor and Build have different
  452. // build indices if there are disabled scenes in build settings, and
  453. // if no scene is in build settings then Editor and Build have
  454. // different indices too (Editor=0, Build=-1)
  455. // => ONLY USE THIS FROM POSTPROCESSSCENE!
  456. public void SetSceneIdSceneHashPartInternal()
  457. {
  458. // Use `ToLower` to that because BuildPipeline.BuildPlayer is case insensitive but hash is case sensitive
  459. // If the scene in the project is `forest.unity` but `Forest.unity` is given to BuildPipeline then the
  460. // BuildPipeline will use `Forest.unity` for the build and create a different hash than the editor will.
  461. // Using ToLower will mean the hash will be the same for these 2 paths
  462. // Assets/Scenes/Forest.unity
  463. // Assets/Scenes/forest.unity
  464. string scenePath = gameObject.scene.path.ToLower();
  465. // get deterministic scene hash
  466. uint pathHash = (uint)scenePath.GetStableHashCode();
  467. // shift hash from 0x000000FFFFFFFF to 0xFFFFFFFF00000000
  468. ulong shiftedHash = (ulong)pathHash << 32;
  469. // OR into scene id
  470. sceneId = (sceneId & 0xFFFFFFFF) | shiftedHash;
  471. // log it. this is incredibly useful to debug sceneId issues.
  472. //Debug.Log($"{name} in scene {gameObject.scene.name} scene index hash {pathHash:X} copied into sceneId {sceneId:X}");
  473. }
  474. void SetupIDs()
  475. {
  476. // is this a prefab?
  477. if (Utils.IsPrefab(gameObject))
  478. {
  479. // force 0 for prefabs
  480. sceneId = 0;
  481. AssignAssetID(gameObject);
  482. }
  483. // are we currently in prefab editing mode? aka prefab stage
  484. // => check prefabstage BEFORE SceneObjectWithPrefabParent
  485. // (fixes https://github.com/vis2k/Mirror/issues/976)
  486. // => if we don't check GetCurrentPrefabStage and only check
  487. // GetPrefabStage(gameObject), then the 'else' case where we
  488. // assign a sceneId and clear the assetId would still be
  489. // triggered for prefabs. in other words: if we are in prefab
  490. // stage, do not bother with anything else ever!
  491. else if (PrefabStageUtility.GetCurrentPrefabStage() != null)
  492. {
  493. // when modifying a prefab in prefab stage, Unity calls
  494. // OnValidate for that prefab and for all scene objects based on
  495. // that prefab.
  496. //
  497. // is this GameObject the prefab that we modify, and not just a
  498. // scene object based on the prefab?
  499. // * GetCurrentPrefabStage = 'are we editing ANY prefab?'
  500. // * GetPrefabStage(go) = 'are we editing THIS prefab?'
  501. if (PrefabStageUtility.GetPrefabStage(gameObject) != null)
  502. {
  503. // force 0 for prefabs
  504. sceneId = 0;
  505. //Debug.Log($"{name} scene:{gameObject.scene.name} sceneid reset to 0 because CurrentPrefabStage={PrefabStageUtility.GetCurrentPrefabStage()} PrefabStage={PrefabStageUtility.GetPrefabStage(gameObject)}");
  506. // get path from PrefabStage for this prefab
  507. #if UNITY_2020_1_OR_NEWER
  508. string path = PrefabStageUtility.GetPrefabStage(gameObject).assetPath;
  509. #else
  510. string path = PrefabStageUtility.GetPrefabStage(gameObject).prefabAssetPath;
  511. #endif
  512. AssignAssetID(path);
  513. }
  514. }
  515. // is this a scene object with prefab parent?
  516. else if (Utils.IsSceneObjectWithPrefabParent(gameObject, out GameObject prefab))
  517. {
  518. AssignSceneID();
  519. AssignAssetID(prefab);
  520. }
  521. else
  522. {
  523. AssignSceneID();
  524. // IMPORTANT: DO NOT clear assetId at runtime!
  525. // => fixes a bug where clicking any of the NetworkIdentity
  526. // properties (like ServerOnly/ForceHidden) at runtime would
  527. // call OnValidate
  528. // => OnValidate gets into this else case here because prefab
  529. // connection isn't known at runtime
  530. // => then we would clear the previously assigned assetId
  531. // => and NetworkIdentity couldn't be spawned on other clients
  532. // anymore because assetId was cleared
  533. if (!EditorApplication.isPlaying)
  534. {
  535. _assetId = 0;
  536. }
  537. // don't log. would show a lot when pressing play in uMMORPG/uSurvival/etc.
  538. //else Debug.Log($"Avoided clearing assetId at runtime for {name} after (probably) clicking any of the NetworkIdentity properties.");
  539. }
  540. }
  541. #endif
  542. // OnDestroy is called for all SPAWNED NetworkIdentities
  543. // => scene objects aren't destroyed. it's not called for them.
  544. //
  545. // Note: Unity will Destroy all networked objects on Scene Change, so we
  546. // have to handle that here silently. That means we cannot have any
  547. // warning or logging in this method.
  548. void OnDestroy()
  549. {
  550. // Objects spawned from Instantiate are not allowed so are destroyed right away
  551. // we don't want to call NetworkServer.Destroy if this is the case
  552. if (SpawnedFromInstantiate)
  553. return;
  554. // If false the object has already been unspawned
  555. // if it is still true, then we need to unspawn it
  556. // if destroy is already called don't call it again
  557. if (isServer && !destroyCalled)
  558. {
  559. // Do not add logging to this (see above)
  560. NetworkServer.Destroy(gameObject);
  561. }
  562. if (isLocalPlayer)
  563. {
  564. // previously there was a bug where isLocalPlayer was
  565. // false in OnDestroy because it was dynamically defined as:
  566. // isLocalPlayer => NetworkClient.localPlayer == this
  567. // we fixed it by setting isLocalPlayer manually and never
  568. // resetting it.
  569. //
  570. // BUT now we need to be aware of a possible data race like in
  571. // our rooms example:
  572. // => GamePlayer is in world
  573. // => player returns to room
  574. // => GamePlayer is destroyed
  575. // => NetworkClient.localPlayer is set to RoomPlayer
  576. // => GamePlayer.OnDestroy is called 1 frame later
  577. // => GamePlayer.OnDestroy 'isLocalPlayer' is true, so here we
  578. // are trying to clear NetworkClient.localPlayer
  579. // => which would overwrite the new RoomPlayer local player
  580. //
  581. // FIXED by simply only clearing if NetworkClient.localPlayer
  582. // still points to US!
  583. // => see also: https://github.com/vis2k/Mirror/issues/2635
  584. if (NetworkClient.localPlayer == this)
  585. NetworkClient.localPlayer = null;
  586. }
  587. if (isClient)
  588. {
  589. // ServerChangeScene doesn't send destroy messages.
  590. // some identities may persist in DDOL.
  591. // some are destroyed by scene change.
  592. // if an identity is still in .owned remove it.
  593. // fixes: https://github.com/MirrorNetworking/Mirror/issues/3308
  594. if (NetworkClient.connection != null)
  595. NetworkClient.connection.owned.Remove(this);
  596. // if an identity is still in .spawned, remove it too.
  597. // fixes: https://github.com/MirrorNetworking/Mirror/issues/3324
  598. NetworkClient.spawned.Remove(netId);
  599. }
  600. }
  601. internal void OnStartServer()
  602. {
  603. foreach (NetworkBehaviour comp in NetworkBehaviours)
  604. {
  605. // an exception in OnStartServer should be caught, so that one
  606. // component's exception doesn't stop all other components from
  607. // being initialized
  608. // => this is what Unity does for Start() etc. too.
  609. // one exception doesn't stop all the other Start() calls!
  610. try
  611. {
  612. comp.OnStartServer();
  613. }
  614. catch (Exception e)
  615. {
  616. Debug.LogException(e, comp);
  617. }
  618. }
  619. }
  620. internal void OnStopServer()
  621. {
  622. foreach (NetworkBehaviour comp in NetworkBehaviours)
  623. {
  624. // an exception in OnStartServer should be caught, so that one
  625. // component's exception doesn't stop all other components from
  626. // being initialized
  627. // => this is what Unity does for Start() etc. too.
  628. // one exception doesn't stop all the other Start() calls!
  629. try
  630. {
  631. comp.OnStopServer();
  632. }
  633. catch (Exception e)
  634. {
  635. Debug.LogException(e, comp);
  636. }
  637. }
  638. }
  639. internal void OnStartClient()
  640. {
  641. if (clientStarted) return;
  642. clientStarted = true;
  643. // Debug.Log($"OnStartClient {gameObject} netId:{netId}");
  644. foreach (NetworkBehaviour comp in NetworkBehaviours)
  645. {
  646. // an exception in OnStartClient should be caught, so that one
  647. // component's exception doesn't stop all other components from
  648. // being initialized
  649. // => this is what Unity does for Start() etc. too.
  650. // one exception doesn't stop all the other Start() calls!
  651. try
  652. {
  653. // user implemented startup
  654. comp.OnStartClient();
  655. }
  656. catch (Exception e)
  657. {
  658. Debug.LogException(e, comp);
  659. }
  660. }
  661. }
  662. internal void OnStopClient()
  663. {
  664. // In case this object was destroyed already don't call
  665. // OnStopClient if OnStartClient hasn't been called.
  666. if (!clientStarted) return;
  667. foreach (NetworkBehaviour comp in NetworkBehaviours)
  668. {
  669. // an exception in OnStopClient should be caught, so that
  670. // one component's exception doesn't stop all other components
  671. // from being initialized
  672. // => this is what Unity does for Start() etc. too.
  673. // one exception doesn't stop all the other Start() calls!
  674. try
  675. {
  676. comp.OnStopClient();
  677. }
  678. catch (Exception e)
  679. {
  680. Debug.LogException(e, comp);
  681. }
  682. }
  683. }
  684. internal static NetworkIdentity previousLocalPlayer = null;
  685. internal void OnStartLocalPlayer()
  686. {
  687. // ensure OnStartLocalPlayer is only called once.
  688. // Room demo would call it multiple times:
  689. // - once from ApplySpawnPayload
  690. // - once from OnObjectSpawnFinished
  691. //
  692. // to reproduce:
  693. // - open room demo, add the 3 scenes to build settings
  694. // - add OnStartLocalPlayer log to RoomPlayer prefab
  695. // - build, run server-only
  696. // - in editor, connect, press ready
  697. // - in server, start game
  698. // - notice multiple OnStartLocalPlayer logs in editor client
  699. //
  700. // explanation:
  701. // we send the spawn message multiple times. Whenever an object changes
  702. // authority, we send the spawn message again for the object. This is
  703. // necessary because we need to reinitialize all variables when
  704. // ownership change due to sync to owner feature.
  705. // Without this static, the second time we get the spawn message we
  706. // would call OnStartLocalPlayer again on the same object
  707. if (previousLocalPlayer == this)
  708. return;
  709. previousLocalPlayer = this;
  710. foreach (NetworkBehaviour comp in NetworkBehaviours)
  711. {
  712. // an exception in OnStartLocalPlayer should be caught, so that
  713. // one component's exception doesn't stop all other components
  714. // from being initialized
  715. // => this is what Unity does for Start() etc. too.
  716. // one exception doesn't stop all the other Start() calls!
  717. try
  718. {
  719. comp.OnStartLocalPlayer();
  720. }
  721. catch (Exception e)
  722. {
  723. Debug.LogException(e, comp);
  724. }
  725. }
  726. }
  727. internal void OnStopLocalPlayer()
  728. {
  729. foreach (NetworkBehaviour comp in NetworkBehaviours)
  730. {
  731. // an exception in OnStopLocalPlayer should be caught, so that
  732. // one component's exception doesn't stop all other components
  733. // from being initialized
  734. // => this is what Unity does for Start() etc. too.
  735. // one exception doesn't stop all the other Start() calls!
  736. try
  737. {
  738. comp.OnStopLocalPlayer();
  739. }
  740. catch (Exception e)
  741. {
  742. Debug.LogException(e, comp);
  743. }
  744. }
  745. }
  746. // build dirty mask for server owner & observers (= all dirty components).
  747. // faster to do it in one iteration instead of iterating separately.
  748. (ulong, ulong) ServerDirtyMasks(bool initialState)
  749. {
  750. ulong ownerMask = 0;
  751. ulong observerMask = 0;
  752. NetworkBehaviour[] components = NetworkBehaviours;
  753. for (int i = 0; i < components.Length; ++i)
  754. {
  755. NetworkBehaviour component = components[i];
  756. bool dirty = component.IsDirty();
  757. ulong nthBit = (1u << i);
  758. // owner needs to be considered for both SyncModes, because
  759. // Observers mode always includes the Owner.
  760. //
  761. // for initial, it should always sync owner.
  762. // for delta, only for ServerToClient and only if dirty.
  763. // ClientToServer comes from the owner client.
  764. if (initialState || (component.syncDirection == SyncDirection.ServerToClient && dirty))
  765. ownerMask |= nthBit;
  766. // observers need to be considered only in Observers mode
  767. //
  768. // for initial, it should always sync to observers.
  769. // for delta, only if dirty.
  770. // SyncDirection is irrelevant, as both are broadcast to
  771. // observers which aren't the owner.
  772. if (component.syncMode == SyncMode.Observers && (initialState || dirty))
  773. observerMask |= nthBit;
  774. }
  775. return (ownerMask, observerMask);
  776. }
  777. // build dirty mask for client.
  778. // server always knows initialState, so we don't need it here.
  779. ulong ClientDirtyMask()
  780. {
  781. ulong mask = 0;
  782. NetworkBehaviour[] components = NetworkBehaviours;
  783. for (int i = 0; i < components.Length; ++i)
  784. {
  785. // on the client, we need to consider different sync scenarios:
  786. //
  787. // ServerToClient SyncDirection:
  788. // do nothing.
  789. // ClientToServer SyncDirection:
  790. // serialize only if owned.
  791. // on client, only consider owned components with SyncDirection to server
  792. NetworkBehaviour component = components[i];
  793. if (isOwned && component.syncDirection == SyncDirection.ClientToServer)
  794. {
  795. // set the n-th bit if dirty
  796. // shifting from small to large numbers is varint-efficient.
  797. if (component.IsDirty()) mask |= (1u << i);
  798. }
  799. }
  800. return mask;
  801. }
  802. // check if n-th component is dirty.
  803. // in other words, if it has the n-th bit set in the dirty mask.
  804. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  805. internal static bool IsDirty(ulong mask, int index)
  806. {
  807. ulong nthBit = (ulong)(1 << index);
  808. return (mask & nthBit) != 0;
  809. }
  810. // serialize components into writer on the server.
  811. // check ownerWritten/observersWritten to know if anything was written
  812. // We pass dirtyComponentsMask into this function so that we can check
  813. // if any Components are dirty before creating writers
  814. internal void SerializeServer(bool initialState, NetworkWriter ownerWriter, NetworkWriter observersWriter)
  815. {
  816. // ensure NetworkBehaviours are valid before usage
  817. ValidateComponents();
  818. NetworkBehaviour[] components = NetworkBehaviours;
  819. // check which components are dirty for owner / observers.
  820. // this is quite complicated with SyncMode + SyncDirection.
  821. // see the function for explanation.
  822. //
  823. // instead of writing a 1 byte index per component,
  824. // we limit components to 64 bits and write one ulong instead.
  825. // the ulong is also varint compressed for minimum bandwidth.
  826. (ulong ownerMask, ulong observerMask) = ServerDirtyMasks(initialState);
  827. // if nothing dirty, then don't even write the mask.
  828. // otherwise, every unchanged object would send a 1 byte dirty mask!
  829. if (ownerMask != 0) Compression.CompressVarUInt(ownerWriter, ownerMask);
  830. if (observerMask != 0) Compression.CompressVarUInt(observersWriter, observerMask);
  831. // serialize all components
  832. // perf: only iterate if either dirty mask has dirty bits.
  833. if ((ownerMask | observerMask) != 0)
  834. {
  835. for (int i = 0; i < components.Length; ++i)
  836. {
  837. NetworkBehaviour comp = components[i];
  838. // is the component dirty for anyone (owner or observers)?
  839. // may be serialized to owner, observer, both, or neither.
  840. //
  841. // OnSerialize should only be called once.
  842. // this is faster, and it cleaner because it may set
  843. // internal state, counters, logs, etc.
  844. //
  845. // previously we always serialized to owner and then copied
  846. // the serialization to observers. however, since
  847. // SyncDirection it's not guaranteed to be in owner anymore.
  848. // so we need to serialize to temporary writer first.
  849. // and then copy as needed.
  850. bool ownerDirty = IsDirty(ownerMask, i);
  851. bool observersDirty = IsDirty(observerMask, i);
  852. if (ownerDirty || observersDirty)
  853. {
  854. // serialize into helper writer
  855. using (NetworkWriterPooled temp = NetworkWriterPool.Get())
  856. {
  857. comp.Serialize(temp, initialState);
  858. ArraySegment<byte> segment = temp.ToArraySegment();
  859. // copy to owner / observers as needed
  860. if (ownerDirty) ownerWriter.WriteBytes(segment.Array, segment.Offset, segment.Count);
  861. if (observersDirty) observersWriter.WriteBytes(segment.Array, segment.Offset, segment.Count);
  862. }
  863. // clear dirty bits for the components that we serialized.
  864. // do not clear for _all_ components, only the ones that
  865. // were dirty and had their syncInterval elapsed.
  866. //
  867. // we don't want to clear bits before the syncInterval
  868. // was elapsed, as then they wouldn't be synced.
  869. //
  870. // only clear for delta, not for full (spawn messages).
  871. // otherwise if a player joins, we serialize monster,
  872. // and shouldn't clear dirty bits not yet synced to
  873. // other players.
  874. if (!initialState) comp.ClearAllDirtyBits();
  875. }
  876. }
  877. }
  878. }
  879. // serialize components into writer on the client.
  880. internal void SerializeClient(NetworkWriter writer)
  881. {
  882. // ensure NetworkBehaviours are valid before usage
  883. ValidateComponents();
  884. NetworkBehaviour[] components = NetworkBehaviours;
  885. // check which components are dirty.
  886. // this is quite complicated with SyncMode + SyncDirection.
  887. // see the function for explanation.
  888. //
  889. // instead of writing a 1 byte index per component,
  890. // we limit components to 64 bits and write one ulong instead.
  891. // the ulong is also varint compressed for minimum bandwidth.
  892. ulong dirtyMask = ClientDirtyMask();
  893. // varint compresses the mask to 1 byte in most cases.
  894. // instead of writing an 8 byte ulong.
  895. // 7 components fit into 1 byte. (previously 7 bytes)
  896. // 11 components fit into 2 bytes. (previously 11 bytes)
  897. // 16 components fit into 3 bytes. (previously 16 bytes)
  898. // TODO imer: server knows amount of comps, write N bytes instead
  899. // if nothing dirty, then don't even write the mask.
  900. // otherwise, every unchanged object would send a 1 byte dirty mask!
  901. if (dirtyMask != 0) Compression.CompressVarUInt(writer, dirtyMask);
  902. // serialize all components
  903. // perf: only iterate if dirty mask has dirty bits.
  904. if (dirtyMask != 0)
  905. {
  906. // serialize all components
  907. for (int i = 0; i < components.Length; ++i)
  908. {
  909. NetworkBehaviour comp = components[i];
  910. // is this component dirty?
  911. // reuse the mask instead of calling comp.IsDirty() again here.
  912. if (IsDirty(dirtyMask, i))
  913. // if (isOwned && component.syncDirection == SyncDirection.ClientToServer)
  914. {
  915. // serialize into writer.
  916. // server always knows initialState, we never need to send it
  917. comp.Serialize(writer, false);
  918. // clear dirty bits for the components that we serialized.
  919. // do not clear for _all_ components, only the ones that
  920. // were dirty and had their syncInterval elapsed.
  921. //
  922. // we don't want to clear bits before the syncInterval
  923. // was elapsed, as then they wouldn't be synced.
  924. comp.ClearAllDirtyBits();
  925. }
  926. }
  927. }
  928. }
  929. // deserialize components from the client on the server.
  930. // there's no 'initialState'. server always knows the initial state.
  931. internal bool DeserializeServer(NetworkReader reader)
  932. {
  933. // ensure NetworkBehaviours are valid before usage
  934. ValidateComponents();
  935. NetworkBehaviour[] components = NetworkBehaviours;
  936. // first we deserialize the varinted dirty mask
  937. ulong mask = Compression.DecompressVarUInt(reader);
  938. // now deserialize every dirty component
  939. for (int i = 0; i < components.Length; ++i)
  940. {
  941. // was this one dirty?
  942. if (IsDirty(mask, i))
  943. {
  944. NetworkBehaviour comp = components[i];
  945. // safety check to ensure clients can only modify their own
  946. // ClientToServer components, nothing else.
  947. if (comp.syncDirection == SyncDirection.ClientToServer)
  948. {
  949. // deserialize this component
  950. // server always knows the initial state (initial=false)
  951. // disconnect if failed, to prevent exploits etc.
  952. if (!comp.Deserialize(reader, false)) return false;
  953. // server received state from the owner client.
  954. // set dirty so it's broadcast to other clients too.
  955. //
  956. // note that we set the _whole_ component as dirty.
  957. // everything will be broadcast to others.
  958. // SetSyncVarDirtyBits() would be nicer, but not all
  959. // components use [SyncVar]s.
  960. comp.SetDirty();
  961. }
  962. }
  963. }
  964. // successfully deserialized everything
  965. return true;
  966. }
  967. // deserialize components from server on the client.
  968. internal void DeserializeClient(NetworkReader reader, bool initialState)
  969. {
  970. // ensure NetworkBehaviours are valid before usage
  971. ValidateComponents();
  972. NetworkBehaviour[] components = NetworkBehaviours;
  973. // first we deserialize the varinted dirty mask
  974. ulong mask = Compression.DecompressVarUInt(reader);
  975. // now deserialize every dirty component
  976. for (int i = 0; i < components.Length; ++i)
  977. {
  978. // was this one dirty?
  979. if (IsDirty(mask, i))
  980. {
  981. // deserialize this component
  982. NetworkBehaviour comp = components[i];
  983. comp.Deserialize(reader, initialState);
  984. }
  985. }
  986. }
  987. // get cached serialization for this tick (or serialize if none yet).
  988. // IMPORTANT: int tick avoids floating point inaccuracy over days/weeks.
  989. // calls SerializeServer, so this function is to be called on server.
  990. internal NetworkIdentitySerialization GetServerSerializationAtTick(int tick)
  991. {
  992. // only rebuild serialization once per tick. reuse otherwise.
  993. // except for tests, where Time.frameCount never increases.
  994. // so during tests, we always rebuild.
  995. // (otherwise [SyncVar] changes would never be serialized in tests)
  996. //
  997. // NOTE: != instead of < because int.max+1 overflows at some point.
  998. if (lastSerialization.tick != tick
  999. #if UNITY_EDITOR
  1000. || !Application.isPlaying
  1001. #endif
  1002. )
  1003. {
  1004. // reset
  1005. lastSerialization.ownerWriter.Position = 0;
  1006. lastSerialization.observersWriter.Position = 0;
  1007. // serialize
  1008. SerializeServer(false,
  1009. lastSerialization.ownerWriter,
  1010. lastSerialization.observersWriter);
  1011. // set tick
  1012. lastSerialization.tick = tick;
  1013. //Debug.Log($"{name} (netId={netId}) serialized for tick={tickTimeStamp}");
  1014. }
  1015. // return it
  1016. return lastSerialization;
  1017. }
  1018. internal void AddObserver(NetworkConnectionToClient conn)
  1019. {
  1020. if (observers.ContainsKey(conn.connectionId))
  1021. {
  1022. // if we try to add a connectionId that was already added, then
  1023. // we may have generated one that was already in use.
  1024. return;
  1025. }
  1026. // Debug.Log($"Added observer: {conn.address} added for {gameObject}");
  1027. // if we previously had no observers, then clear all dirty bits once.
  1028. // a monster's health may have changed while it had no observers.
  1029. // but that change (= the dirty bits) don't matter as soon as the
  1030. // first observer comes.
  1031. // -> first observer gets full spawn packet
  1032. // -> afterwards it gets delta packet
  1033. // => if we don't clear previous dirty bits, observer would get
  1034. // the health change because the bit was still set.
  1035. // => ultimately this happens because spawn doesn't reset dirty
  1036. // bits
  1037. // => which happens because spawn happens separately, instead of
  1038. // in Broadcast() (which will be changed in the future)
  1039. //
  1040. // NOTE that NetworkServer.Broadcast previously cleared dirty bits
  1041. // for ALL SPAWNED that don't have observers. that was super
  1042. // expensive. doing it when adding the first observer has the
  1043. // same result, without the O(N) iteration in Broadcast().
  1044. //
  1045. // TODO remove this after moving spawning into Broadcast()!
  1046. if (observers.Count == 0)
  1047. {
  1048. ClearAllComponentsDirtyBits();
  1049. }
  1050. observers[conn.connectionId] = conn;
  1051. conn.AddToObserving(this);
  1052. }
  1053. // clear all component's dirty bits no matter what
  1054. internal void ClearAllComponentsDirtyBits()
  1055. {
  1056. foreach (NetworkBehaviour comp in NetworkBehaviours)
  1057. {
  1058. comp.ClearAllDirtyBits();
  1059. }
  1060. }
  1061. // this is used when a connection is destroyed, since the "observers" property is read-only
  1062. internal void RemoveObserver(NetworkConnection conn)
  1063. {
  1064. observers.Remove(conn.connectionId);
  1065. }
  1066. /// <summary>Assign control of an object to a client via the client's NetworkConnection.</summary>
  1067. // This causes hasAuthority to be set on the client that owns the object,
  1068. // and NetworkBehaviour.OnStartAuthority will be called on that client.
  1069. // This object then will be in the NetworkConnection.clientOwnedObjects
  1070. // list for the connection.
  1071. //
  1072. // Authority can be removed with RemoveClientAuthority. Only one client
  1073. // can own an object at any time. This does not need to be called for
  1074. // player objects, as their authority is setup automatically.
  1075. public bool AssignClientAuthority(NetworkConnectionToClient conn)
  1076. {
  1077. if (!isServer)
  1078. {
  1079. Debug.LogError("AssignClientAuthority can only be called on the server for spawned objects.");
  1080. return false;
  1081. }
  1082. if (conn == null)
  1083. {
  1084. Debug.LogError($"AssignClientAuthority for {gameObject} owner cannot be null. Use RemoveClientAuthority() instead.");
  1085. return false;
  1086. }
  1087. if (connectionToClient != null && conn != connectionToClient)
  1088. {
  1089. Debug.LogError($"AssignClientAuthority for {gameObject} already has an owner. Use RemoveClientAuthority() first.");
  1090. return false;
  1091. }
  1092. SetClientOwner(conn);
  1093. // The client will match to the existing object
  1094. NetworkServer.SendChangeOwnerMessage(this, conn);
  1095. clientAuthorityCallback?.Invoke(conn, this, true);
  1096. return true;
  1097. }
  1098. // used when adding players
  1099. internal void SetClientOwner(NetworkConnectionToClient conn)
  1100. {
  1101. // do nothing if it already has an owner
  1102. if (connectionToClient != null && conn != connectionToClient)
  1103. {
  1104. Debug.LogError($"Object {this} netId={netId} already has an owner. Use RemoveClientAuthority() first", this);
  1105. return;
  1106. }
  1107. // otherwise set the owner connection
  1108. connectionToClient = conn;
  1109. }
  1110. /// <summary>Removes ownership for an object.</summary>
  1111. // Applies to objects that had authority set by AssignClientAuthority,
  1112. // or NetworkServer.Spawn with a NetworkConnection parameter included.
  1113. // Authority cannot be removed for player objects.
  1114. public void RemoveClientAuthority()
  1115. {
  1116. if (!isServer)
  1117. {
  1118. Debug.LogError("RemoveClientAuthority can only be called on the server for spawned objects.");
  1119. return;
  1120. }
  1121. if (connectionToClient?.identity == this)
  1122. {
  1123. Debug.LogError("RemoveClientAuthority cannot remove authority for a player object");
  1124. return;
  1125. }
  1126. if (connectionToClient != null)
  1127. {
  1128. clientAuthorityCallback?.Invoke(connectionToClient, this, false);
  1129. NetworkConnectionToClient previousOwner = connectionToClient;
  1130. connectionToClient = null;
  1131. NetworkServer.SendChangeOwnerMessage(this, previousOwner);
  1132. }
  1133. }
  1134. // Reset is called when the user hits the Reset button in the
  1135. // Inspector's context menu or when adding the component the first time.
  1136. // This function is only called in editor mode.
  1137. //
  1138. // Reset() seems to be called only for Scene objects.
  1139. // we can't destroy them (they are always in the scene).
  1140. // instead we disable them and call Reset().
  1141. //
  1142. // Do not reset SyncObjects from Reset
  1143. // - Unspawned objects need to retain their list contents
  1144. // - They may be respawned, especially players, but others as well.
  1145. //
  1146. // OLD COMMENT:
  1147. // Marks the identity for future reset, this is because we cant reset
  1148. // the identity during destroy as people might want to be able to read
  1149. // the members inside OnDestroy(), and we have no way of invoking reset
  1150. // after OnDestroy is called.
  1151. internal void Reset()
  1152. {
  1153. hasSpawned = false;
  1154. clientStarted = false;
  1155. isClient = false;
  1156. isServer = false;
  1157. //isLocalPlayer = false; <- cleared AFTER ClearLocalPlayer below!
  1158. // remove authority flag. This object may be unspawned, not destroyed, on client.
  1159. isOwned = false;
  1160. NotifyAuthority();
  1161. netId = 0;
  1162. connectionToServer = null;
  1163. connectionToClient = null;
  1164. ClearObservers();
  1165. // clear local player if it was the local player,
  1166. // THEN reset isLocalPlayer AFTERWARDS
  1167. if (isLocalPlayer)
  1168. {
  1169. // only clear NetworkClient.localPlayer IF IT POINTS TO US!
  1170. // see OnDestroy() comments. it does the same.
  1171. // (https://github.com/vis2k/Mirror/issues/2635)
  1172. if (NetworkClient.localPlayer == this)
  1173. NetworkClient.localPlayer = null;
  1174. }
  1175. previousLocalPlayer = null;
  1176. isLocalPlayer = false;
  1177. }
  1178. bool hadAuthority;
  1179. internal void NotifyAuthority()
  1180. {
  1181. if (!hadAuthority && isOwned)
  1182. OnStartAuthority();
  1183. if (hadAuthority && !isOwned)
  1184. OnStopAuthority();
  1185. hadAuthority = isOwned;
  1186. }
  1187. internal void OnStartAuthority()
  1188. {
  1189. foreach (NetworkBehaviour comp in NetworkBehaviours)
  1190. {
  1191. // an exception in OnStartAuthority should be caught, so that one
  1192. // component's exception doesn't stop all other components from
  1193. // being initialized
  1194. // => this is what Unity does for Start() etc. too.
  1195. // one exception doesn't stop all the other Start() calls!
  1196. try
  1197. {
  1198. comp.OnStartAuthority();
  1199. }
  1200. catch (Exception e)
  1201. {
  1202. Debug.LogException(e, comp);
  1203. }
  1204. }
  1205. }
  1206. internal void OnStopAuthority()
  1207. {
  1208. foreach (NetworkBehaviour comp in NetworkBehaviours)
  1209. {
  1210. // an exception in OnStopAuthority should be caught, so that one
  1211. // component's exception doesn't stop all other components from
  1212. // being initialized
  1213. // => this is what Unity does for Start() etc. too.
  1214. // one exception doesn't stop all the other Start() calls!
  1215. try
  1216. {
  1217. comp.OnStopAuthority();
  1218. }
  1219. catch (Exception e)
  1220. {
  1221. Debug.LogException(e, comp);
  1222. }
  1223. }
  1224. }
  1225. // Called when NetworkIdentity is destroyed
  1226. internal void ClearObservers()
  1227. {
  1228. foreach (NetworkConnectionToClient conn in observers.Values)
  1229. {
  1230. conn.RemoveFromObserving(this, true);
  1231. }
  1232. observers.Clear();
  1233. }
  1234. }
  1235. }