NetworkIdentity.cs 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. using System;
  2. using System.Collections.Generic;
  3. using Mirror.RemoteCalls;
  4. using UnityEngine;
  5. using UnityEngine.Serialization;
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. #if UNITY_2021_2_OR_NEWER
  9. using UnityEditor.SceneManagement;
  10. #elif UNITY_2018_3_OR_NEWER
  11. using UnityEditor.Experimental.SceneManagement;
  12. #endif
  13. #endif
  14. namespace Mirror
  15. {
  16. // Default = use interest management
  17. // ForceHidden = useful to hide monsters while they respawn etc.
  18. // ForceShown = useful to have score NetworkIdentities that always broadcast
  19. // to everyone etc.
  20. public enum Visibility { Default, ForceHidden, ForceShown }
  21. public struct NetworkIdentitySerialization
  22. {
  23. // IMPORTANT: int tick avoids floating point inaccuracy over days/weeks
  24. public int tick;
  25. public NetworkWriter ownerWriter;
  26. public NetworkWriter observersWriter;
  27. }
  28. /// <summary>NetworkIdentity identifies objects across the network.</summary>
  29. [DisallowMultipleComponent]
  30. // NetworkIdentity.Awake initializes all NetworkComponents.
  31. // let's make sure it's always called before their Awake's.
  32. [DefaultExecutionOrder(-1)]
  33. [AddComponentMenu("Network/NetworkIdentity")]
  34. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-identity")]
  35. public sealed class NetworkIdentity : MonoBehaviour
  36. {
  37. /// <summary>Returns true if running as a client and this object was spawned by a server.</summary>
  38. //
  39. // IMPORTANT:
  40. // OnStartClient sets it to true. we NEVER set it to false after.
  41. // otherwise components like Skillbars couldn't use OnDestroy()
  42. // for saving, etc. since isClient may have been reset before
  43. // OnDestroy was called.
  44. //
  45. // we also DO NOT make it dependent on NetworkClient.active or similar.
  46. // we set it, then never change it. that's the user's expectation too.
  47. //
  48. // => fixes https://github.com/vis2k/Mirror/issues/1475
  49. public bool isClient { get; internal set; }
  50. /// <summary>Returns true if NetworkServer.active and server is not stopped.</summary>
  51. //
  52. // IMPORTANT:
  53. // OnStartServer sets it to true. we NEVER set it to false after.
  54. // otherwise components like Skillbars couldn't use OnDestroy()
  55. // for saving, etc. since isServer may have been reset before
  56. // OnDestroy was called.
  57. //
  58. // we also DO NOT make it dependent on NetworkServer.active or similar.
  59. // we set it, then never change it. that's the user's expectation too.
  60. //
  61. // => fixes https://github.com/vis2k/Mirror/issues/1484
  62. // => fixes https://github.com/vis2k/Mirror/issues/2533
  63. public bool isServer { get; internal set; }
  64. /// <summary>Return true if this object represents the player on the local machine.</summary>
  65. //
  66. // IMPORTANT:
  67. // OnStartLocalPlayer sets it to true. we NEVER set it to false after.
  68. // otherwise components like Skillbars couldn't use OnDestroy()
  69. // for saving, etc. since isLocalPlayer may have been reset before
  70. // OnDestroy was called.
  71. //
  72. // we also DO NOT make it dependent on NetworkClient.localPlayer or similar.
  73. // we set it, then never change it. that's the user's expectation too.
  74. //
  75. // => fixes https://github.com/vis2k/Mirror/issues/2615
  76. public bool isLocalPlayer { get; internal set; }
  77. /// <summary>True if this object only exists on the server</summary>
  78. public bool isServerOnly => isServer && !isClient;
  79. /// <summary>True if this object exists on a client that is not also acting as a server.</summary>
  80. public bool isClientOnly => isClient && !isServer;
  81. /// <summary>True if this object is the authoritative player object on the client.</summary>
  82. // Determined at runtime. For most objects, authority is held by the server.
  83. // For objects that had their authority set by AssignClientAuthority on
  84. // the server, this will be true on the client that owns the object. NOT
  85. // on other clients.
  86. public bool hasAuthority { get; internal set; }
  87. /// <summary>The set of network connections (players) that can see this object.</summary>
  88. // note: null until OnStartServer was called. this is necessary for
  89. // SendTo* to work properly in server-only mode.
  90. public Dictionary<int, NetworkConnection> observers;
  91. /// <summary>The unique network Id of this object (unique at runtime).</summary>
  92. public uint netId { get; internal set; }
  93. /// <summary>Unique identifier for NetworkIdentity objects within a scene, used for spawning scene objects.</summary>
  94. // persistent scene id <sceneHash/32,sceneId/32> (see AssignSceneID comments)
  95. [FormerlySerializedAs("m_SceneId"), HideInInspector]
  96. public ulong sceneId;
  97. /// <summary>Make this object only exist when the game is running as a server (or host).</summary>
  98. [FormerlySerializedAs("m_ServerOnly")]
  99. [Tooltip("Prevents this object from being spawned / enabled on clients")]
  100. public bool serverOnly;
  101. // Set before Destroy is called so that OnDestroy doesn't try to destroy
  102. // the object again
  103. internal bool destroyCalled;
  104. /// <summary>Client's network connection to the server. This is only valid for player objects on the client.</summary>
  105. public NetworkConnection connectionToServer { get; internal set; }
  106. /// <summary>Server's network connection to the client. This is only valid for client-owned objects (including the Player object) on the server.</summary>
  107. public NetworkConnectionToClient connectionToClient
  108. {
  109. get => _connectionToClient;
  110. internal set
  111. {
  112. _connectionToClient?.RemoveOwnedObject(this);
  113. _connectionToClient = value;
  114. _connectionToClient?.AddOwnedObject(this);
  115. }
  116. }
  117. NetworkConnectionToClient _connectionToClient;
  118. /// <summary>All spawned NetworkIdentities by netId. Available on server and client.</summary>
  119. // server sees ALL spawned ones.
  120. // client sees OBSERVED spawned ones.
  121. public static readonly Dictionary<uint, NetworkIdentity> spawned =
  122. new Dictionary<uint, NetworkIdentity>();
  123. // get all NetworkBehaviour components
  124. public NetworkBehaviour[] NetworkBehaviours { get; private set; }
  125. #pragma warning disable 618
  126. [Obsolete(NetworkVisibilityObsoleteMessage.Message)]
  127. public NetworkVisibility visibility { get; private set; }
  128. #pragma warning restore 618
  129. // current visibility
  130. //
  131. // Default = use interest management
  132. // ForceHidden = useful to hide monsters while they respawn etc.
  133. // ForceShown = useful to have score NetworkIdentities that always broadcast
  134. // to everyone etc.
  135. //
  136. // TODO rename to 'visibility' after removing .visibility some day!
  137. [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.")]
  138. public Visibility visible = Visibility.Default;
  139. // broadcasting serializes all entities around a player for each player.
  140. // we don't want to serialize one entity twice in the same tick.
  141. // so we cache the last serialization and remember the timestamp so we
  142. // know which Update it was serialized.
  143. // (timestamp is the same while inside Update)
  144. // => this way we don't need to pool thousands of writers either.
  145. // => way easier to store them per object
  146. NetworkIdentitySerialization lastSerialization = new NetworkIdentitySerialization
  147. {
  148. ownerWriter = new NetworkWriter(),
  149. observersWriter = new NetworkWriter()
  150. };
  151. /// <summary>Prefab GUID used to spawn prefabs across the network.</summary>
  152. //
  153. // The AssetId trick:
  154. // Ideally we would have a serialized 'Guid m_AssetId' but Unity can't
  155. // serialize it because Guid's internal bytes are private
  156. //
  157. // UNET used 'NetworkHash128' originally, with byte0, ..., byte16
  158. // which works, but it just unnecessary extra code
  159. //
  160. // Using just the Guid string would work, but it's 32 chars long and
  161. // would then be sent over the network as 64 instead of 16 bytes
  162. //
  163. // => The solution is to serialize the string internally here and then
  164. // use the real 'Guid' type for everything else via .assetId
  165. public Guid assetId
  166. {
  167. get
  168. {
  169. #if UNITY_EDITOR
  170. // This is important because sometimes OnValidate does not run (like when adding view to prefab with no child links)
  171. if (string.IsNullOrEmpty(m_AssetId))
  172. SetupIDs();
  173. #endif
  174. // convert string to Guid and use .Empty to avoid exception if
  175. // we would use 'new Guid("")'
  176. return string.IsNullOrEmpty(m_AssetId) ? Guid.Empty : new Guid(m_AssetId);
  177. }
  178. internal set
  179. {
  180. string newAssetIdString = value == Guid.Empty ? string.Empty : value.ToString("N");
  181. string oldAssetIdString = m_AssetId;
  182. // they are the same, do nothing
  183. if (oldAssetIdString == newAssetIdString)
  184. {
  185. return;
  186. }
  187. // new is empty
  188. if (string.IsNullOrEmpty(newAssetIdString))
  189. {
  190. Debug.LogError($"Can not set AssetId to empty guid on NetworkIdentity '{name}', old assetId '{oldAssetIdString}'");
  191. return;
  192. }
  193. // old not empty
  194. if (!string.IsNullOrEmpty(oldAssetIdString))
  195. {
  196. Debug.LogError($"Can not Set AssetId on NetworkIdentity '{name}' because it already had an assetId, current assetId '{oldAssetIdString}', attempted new assetId '{newAssetIdString}'");
  197. return;
  198. }
  199. // old is empty
  200. m_AssetId = newAssetIdString;
  201. // Debug.Log($"Settings AssetId on NetworkIdentity '{name}', new assetId '{newAssetIdString}'");
  202. }
  203. }
  204. [SerializeField, HideInInspector] string m_AssetId;
  205. // Keep track of all sceneIds to detect scene duplicates
  206. static readonly Dictionary<ulong, NetworkIdentity> sceneIds =
  207. new Dictionary<ulong, NetworkIdentity>();
  208. /// <summary>Gets the NetworkIdentity from the sceneIds dictionary with the corresponding id</summary>
  209. public static NetworkIdentity GetSceneIdentity(ulong id) => sceneIds[id];
  210. // used when adding players
  211. internal void SetClientOwner(NetworkConnection conn)
  212. {
  213. // do nothing if it already has an owner
  214. if (connectionToClient != null && conn != connectionToClient)
  215. {
  216. Debug.LogError($"Object {this} netId={netId} already has an owner. Use RemoveClientAuthority() first", this);
  217. return;
  218. }
  219. // otherwise set the owner connection
  220. connectionToClient = (NetworkConnectionToClient)conn;
  221. }
  222. static uint nextNetworkId = 1;
  223. internal static uint GetNextNetworkId() => nextNetworkId++;
  224. /// <summary>Resets nextNetworkId = 1</summary>
  225. public static void ResetNextNetworkId() => nextNetworkId = 1;
  226. /// <summary>The delegate type for the clientAuthorityCallback.</summary>
  227. public delegate void ClientAuthorityCallback(NetworkConnection conn, NetworkIdentity identity, bool authorityState);
  228. /// <summary> A callback that can be populated to be notified when the client-authority state of objects changes.</summary>
  229. public static event ClientAuthorityCallback clientAuthorityCallback;
  230. // this is used when a connection is destroyed, since the "observers" property is read-only
  231. internal void RemoveObserverInternal(NetworkConnection conn)
  232. {
  233. observers?.Remove(conn.connectionId);
  234. }
  235. // hasSpawned should always be false before runtime
  236. [SerializeField, HideInInspector] bool hasSpawned;
  237. public bool SpawnedFromInstantiate { get; private set; }
  238. // NetworkBehaviour components are initialized in Awake once.
  239. // Changing them at runtime would get client & server out of sync.
  240. // BUT internal so tests can add them after creating the NetworkIdentity
  241. internal void InitializeNetworkBehaviours()
  242. {
  243. // Get all NetworkBehaviours
  244. // (never null. GetComponents returns [] if none found)
  245. NetworkBehaviours = GetComponents<NetworkBehaviour>();
  246. if (NetworkBehaviours.Length > byte.MaxValue)
  247. Debug.LogError($"Only {byte.MaxValue} NetworkBehaviour components are allowed for NetworkIdentity: {name} because we send the index as byte.", this);
  248. // initialize each one
  249. for (int i = 0; i < NetworkBehaviours.Length; ++i)
  250. {
  251. NetworkBehaviour component = NetworkBehaviours[i];
  252. component.netIdentity = this;
  253. component.ComponentIndex = i;
  254. }
  255. }
  256. // Awake is only called in Play mode.
  257. // internal so we can call it during unit tests too.
  258. internal void Awake()
  259. {
  260. // initialize NetworkBehaviour components.
  261. // Awake() is called immediately after initialization.
  262. // no one can overwrite it because NetworkIdentity is sealed.
  263. // => doing it here is the fastest and easiest solution.
  264. InitializeNetworkBehaviours();
  265. // initialize visibility component. only call GetComponent once.
  266. #pragma warning disable 618
  267. visibility = GetComponent<NetworkVisibility>();
  268. #pragma warning restore 618
  269. if (hasSpawned)
  270. {
  271. 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.");
  272. SpawnedFromInstantiate = true;
  273. Destroy(gameObject);
  274. }
  275. hasSpawned = true;
  276. }
  277. void OnValidate()
  278. {
  279. // OnValidate is not called when using Instantiate, so we can use
  280. // it to make sure that hasSpawned is false
  281. hasSpawned = false;
  282. #if UNITY_EDITOR
  283. SetupIDs();
  284. #endif
  285. }
  286. #if UNITY_EDITOR
  287. void AssignAssetID(string path)
  288. {
  289. // only set if not empty. fixes https://github.com/vis2k/Mirror/issues/2765
  290. if (!string.IsNullOrEmpty(path))
  291. m_AssetId = AssetDatabase.AssetPathToGUID(path);
  292. }
  293. void AssignAssetID(GameObject prefab) => AssignAssetID(AssetDatabase.GetAssetPath(prefab));
  294. // persistent sceneId assignment
  295. // (because scene objects have no persistent unique ID in Unity)
  296. //
  297. // original UNET used OnPostProcessScene to assign an index based on
  298. // FindObjectOfType<NetworkIdentity> order.
  299. // -> this didn't work because FindObjectOfType order isn't deterministic.
  300. // -> one workaround is to sort them by sibling paths, but it can still
  301. // get out of sync when we open scene2 in editor and we have
  302. // DontDestroyOnLoad objects that messed with the sibling index.
  303. //
  304. // we absolutely need a persistent id. challenges:
  305. // * it needs to be 0 for prefabs
  306. // => we set it to 0 in SetupIDs() if prefab!
  307. // * it needs to be only assigned in edit time, not at runtime because
  308. // only the objects that were in the scene since beginning should have
  309. // a scene id.
  310. // => Application.isPlaying check solves that
  311. // * it needs to detect duplicated sceneIds after duplicating scene
  312. // objects
  313. // => sceneIds dict takes care of that
  314. // * duplicating the whole scene file shouldn't result in duplicate
  315. // scene objects
  316. // => buildIndex is shifted into sceneId for that.
  317. // => if we have no scenes in build index then it doesn't matter
  318. // because by definition a build can't switch to other scenes
  319. // => if we do have scenes in build index then it will be != -1
  320. // note: the duplicated scene still needs to be opened once for it to
  321. // be set properly
  322. // * scene objects need the correct scene index byte even if the scene's
  323. // build index was changed or a duplicated scene wasn't opened yet.
  324. // => OnPostProcessScene is the only function that gets called for
  325. // each scene before runtime, so this is where we set the scene
  326. // byte.
  327. // * disabled scenes in build settings should result in same scene index
  328. // in editor and in build
  329. // => .gameObject.scene.buildIndex filters out disabled scenes by
  330. // default
  331. // * generated sceneIds absolutely need to set scene dirty and force the
  332. // user to resave.
  333. // => Undo.RecordObject does that perfectly.
  334. // * sceneIds should never be generated temporarily for unopened scenes
  335. // when building, otherwise editor and build get out of sync
  336. // => BuildPipeline.isBuildingPlayer check solves that
  337. void AssignSceneID()
  338. {
  339. // we only ever assign sceneIds at edit time, never at runtime.
  340. // by definition, only the original scene objects should get one.
  341. // -> if we assign at runtime then server and client would generate
  342. // different random numbers!
  343. if (Application.isPlaying)
  344. return;
  345. // no valid sceneId yet, or duplicate?
  346. bool duplicate = sceneIds.TryGetValue(sceneId, out NetworkIdentity existing) && existing != null && existing != this;
  347. if (sceneId == 0 || duplicate)
  348. {
  349. // clear in any case, because it might have been a duplicate
  350. sceneId = 0;
  351. // if a scene was never opened and we are building it, then a
  352. // sceneId would be assigned to build but not saved in editor,
  353. // resulting in them getting out of sync.
  354. // => don't ever assign temporary ids. they always need to be
  355. // permanent
  356. // => throw an exception to cancel the build and let the user
  357. // know how to fix it!
  358. if (BuildPipeline.isBuildingPlayer)
  359. 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.");
  360. // if we generate the sceneId then we MUST be sure to set dirty
  361. // in order to save the scene object properly. otherwise it
  362. // would be regenerated every time we reopen the scene, and
  363. // upgrading would be very difficult.
  364. // -> Undo.RecordObject is the new EditorUtility.SetDirty!
  365. // -> we need to call it before changing.
  366. Undo.RecordObject(this, "Generated SceneId");
  367. // generate random sceneId part (0x00000000FFFFFFFF)
  368. uint randomId = Utils.GetTrueRandomUInt();
  369. // only assign if not a duplicate of an existing scene id
  370. // (small chance, but possible)
  371. duplicate = sceneIds.TryGetValue(randomId, out existing) && existing != null && existing != this;
  372. if (!duplicate)
  373. {
  374. sceneId = randomId;
  375. //Debug.Log(name + " in scene=" + gameObject.scene.name + " sceneId assigned to: " + m_SceneId.ToString("X"));
  376. }
  377. }
  378. // add to sceneIds dict no matter what
  379. // -> even if we didn't generate anything new, because we still need
  380. // existing sceneIds in there to check duplicates
  381. sceneIds[sceneId] = this;
  382. }
  383. // copy scene path hash into sceneId for scene objects.
  384. // this is the only way for scene file duplication to not contain
  385. // duplicate sceneIds as it seems.
  386. // -> sceneId before: 0x00000000AABBCCDD
  387. // -> then we clear the left 4 bytes, so that our 'OR' uses 0x00000000
  388. // -> then we OR the hash into the 0x00000000 part
  389. // -> buildIndex is not enough, because Editor and Build have different
  390. // build indices if there are disabled scenes in build settings, and
  391. // if no scene is in build settings then Editor and Build have
  392. // different indices too (Editor=0, Build=-1)
  393. // => ONLY USE THIS FROM POSTPROCESSSCENE!
  394. public void SetSceneIdSceneHashPartInternal()
  395. {
  396. // Use `ToLower` to that because BuildPipeline.BuildPlayer is case insensitive but hash is case sensitive
  397. // If the scene in the project is `forest.unity` but `Forest.unity` is given to BuildPipeline then the
  398. // BuildPipeline will use `Forest.unity` for the build and create a different hash than the editor will.
  399. // Using ToLower will mean the hash will be the same for these 2 paths
  400. // Assets/Scenes/Forest.unity
  401. // Assets/Scenes/forest.unity
  402. string scenePath = gameObject.scene.path.ToLower();
  403. // get deterministic scene hash
  404. uint pathHash = (uint)scenePath.GetStableHashCode();
  405. // shift hash from 0x000000FFFFFFFF to 0xFFFFFFFF00000000
  406. ulong shiftedHash = (ulong)pathHash << 32;
  407. // OR into scene id
  408. sceneId = (sceneId & 0xFFFFFFFF) | shiftedHash;
  409. // log it. this is incredibly useful to debug sceneId issues.
  410. // Debug.Log(name + " in scene=" + gameObject.scene.name + " scene index hash(" + pathHash.ToString("X") + ") copied into sceneId: " + sceneId.ToString("X"));
  411. }
  412. void SetupIDs()
  413. {
  414. // is this a prefab?
  415. if (Utils.IsPrefab(gameObject))
  416. {
  417. // force 0 for prefabs
  418. sceneId = 0;
  419. AssignAssetID(gameObject);
  420. }
  421. // are we currently in prefab editing mode? aka prefab stage
  422. // => check prefabstage BEFORE SceneObjectWithPrefabParent
  423. // (fixes https://github.com/vis2k/Mirror/issues/976)
  424. // => if we don't check GetCurrentPrefabStage and only check
  425. // GetPrefabStage(gameObject), then the 'else' case where we
  426. // assign a sceneId and clear the assetId would still be
  427. // triggered for prefabs. in other words: if we are in prefab
  428. // stage, do not bother with anything else ever!
  429. else if (PrefabStageUtility.GetCurrentPrefabStage() != null)
  430. {
  431. // when modifying a prefab in prefab stage, Unity calls
  432. // OnValidate for that prefab and for all scene objects based on
  433. // that prefab.
  434. //
  435. // is this GameObject the prefab that we modify, and not just a
  436. // scene object based on the prefab?
  437. // * GetCurrentPrefabStage = 'are we editing ANY prefab?'
  438. // * GetPrefabStage(go) = 'are we editing THIS prefab?'
  439. if (PrefabStageUtility.GetPrefabStage(gameObject) != null)
  440. {
  441. // force 0 for prefabs
  442. sceneId = 0;
  443. //Debug.Log(name + " @ scene: " + gameObject.scene.name + " sceneid reset to 0 because CurrentPrefabStage=" + PrefabStageUtility.GetCurrentPrefabStage() + " PrefabStage=" + PrefabStageUtility.GetPrefabStage(gameObject));
  444. // get path from PrefabStage for this prefab
  445. #if UNITY_2020_1_OR_NEWER
  446. string path = PrefabStageUtility.GetPrefabStage(gameObject).assetPath;
  447. #else
  448. string path = PrefabStageUtility.GetPrefabStage(gameObject).prefabAssetPath;
  449. #endif
  450. AssignAssetID(path);
  451. }
  452. }
  453. // is this a scene object with prefab parent?
  454. else if (Utils.IsSceneObjectWithPrefabParent(gameObject, out GameObject prefab))
  455. {
  456. AssignSceneID();
  457. AssignAssetID(prefab);
  458. }
  459. else
  460. {
  461. AssignSceneID();
  462. // IMPORTANT: DO NOT clear assetId at runtime!
  463. // => fixes a bug where clicking any of the NetworkIdentity
  464. // properties (like ServerOnly/ForceHidden) at runtime would
  465. // call OnValidate
  466. // => OnValidate gets into this else case here because prefab
  467. // connection isn't known at runtime
  468. // => then we would clear the previously assigned assetId
  469. // => and NetworkIdentity couldn't be spawned on other clients
  470. // anymore because assetId was cleared
  471. if (!EditorApplication.isPlaying)
  472. {
  473. m_AssetId = "";
  474. }
  475. // don't log. would show a lot when pressing play in uMMORPG/uSurvival/etc.
  476. //else Debug.Log($"Avoided clearing assetId at runtime for {name} after (probably) clicking any of the NetworkIdentity properties.");
  477. }
  478. }
  479. #endif
  480. // OnDestroy is called for all SPAWNED NetworkIdentities
  481. // => scene objects aren't destroyed. it's not called for them.
  482. //
  483. // Note: Unity will Destroy all networked objects on Scene Change, so we
  484. // have to handle that here silently. That means we cannot have any
  485. // warning or logging in this method.
  486. void OnDestroy()
  487. {
  488. // Objects spawned from Instantiate are not allowed so are destroyed right away
  489. // we don't want to call NetworkServer.Destroy if this is the case
  490. if (SpawnedFromInstantiate)
  491. return;
  492. // If false the object has already been unspawned
  493. // if it is still true, then we need to unspawn it
  494. // if destroy is already called don't call it again
  495. if (isServer && !destroyCalled)
  496. {
  497. // Do not add logging to this (see above)
  498. NetworkServer.Destroy(gameObject);
  499. }
  500. if (isLocalPlayer)
  501. {
  502. // previously there was a bug where isLocalPlayer was
  503. // false in OnDestroy because it was dynamically defined as:
  504. // isLocalPlayer => NetworkClient.localPlayer == this
  505. // we fixed it by setting isLocalPlayer manually and never
  506. // resetting it.
  507. //
  508. // BUT now we need to be aware of a possible data race like in
  509. // our rooms example:
  510. // => GamePlayer is in world
  511. // => player returns to room
  512. // => GamePlayer is destroyed
  513. // => NetworkClient.localPlayer is set to RoomPlayer
  514. // => GamePlayer.OnDestroy is called 1 frame later
  515. // => GamePlayer.OnDestroy 'isLocalPlayer' is true, so here we
  516. // are trying to clear NetworkClient.localPlayer
  517. // => which would overwrite the new RoomPlayer local player
  518. //
  519. // FIXED by simply only clearing if NetworkClient.localPlayer
  520. // still points to US!
  521. // => see also: https://github.com/vis2k/Mirror/issues/2635
  522. if (NetworkClient.localPlayer == this)
  523. NetworkClient.localPlayer = null;
  524. }
  525. }
  526. internal void OnStartServer()
  527. {
  528. // do nothing if already spawned
  529. if (isServer)
  530. return;
  531. // set isServer flag
  532. isServer = true;
  533. // set isLocalPlayer earlier, in case OnStartLocalplayer is called
  534. // AFTER OnStartClient, in which case it would still be falsse here.
  535. // many projects will check isLocalPlayer in OnStartClient though.
  536. // TODO ideally set isLocalPlayer when NetworkClient.localPlayer is set?
  537. if (NetworkClient.localPlayer == this)
  538. {
  539. isLocalPlayer = true;
  540. }
  541. // If the instance/net ID is invalid here then this is an object instantiated from a prefab and the server should assign a valid ID
  542. // NOTE: this might not be necessary because the above m_IsServer
  543. // check already checks netId. BUT this case here checks only
  544. // netId, so it would still check cases where isServer=false
  545. // but netId!=0.
  546. if (netId != 0)
  547. {
  548. // This object has already been spawned, this method might be called again
  549. // if we try to respawn all objects. This can happen when we add a scene
  550. // in that case there is nothing else to do.
  551. return;
  552. }
  553. netId = GetNextNetworkId();
  554. observers = new Dictionary<int, NetworkConnection>();
  555. // Debug.Log("OnStartServer " + this + " NetId:" + netId + " SceneId:" + sceneId.ToString("X"));
  556. // add to spawned (note: the original EnableIsServer isn't needed
  557. // because we already set m_isServer=true above)
  558. spawned[netId] = this;
  559. // in host mode we set isClient true before calling OnStartServer,
  560. // otherwise isClient is false in OnStartServer.
  561. if (NetworkClient.active)
  562. {
  563. isClient = true;
  564. }
  565. foreach (NetworkBehaviour comp in NetworkBehaviours)
  566. {
  567. // an exception in OnStartServer should be caught, so that one
  568. // component's exception doesn't stop all other components from
  569. // being initialized
  570. // => this is what Unity does for Start() etc. too.
  571. // one exception doesn't stop all the other Start() calls!
  572. try
  573. {
  574. comp.OnStartServer();
  575. }
  576. catch (Exception e)
  577. {
  578. Debug.LogException(e, comp);
  579. }
  580. }
  581. }
  582. internal void OnStopServer()
  583. {
  584. foreach (NetworkBehaviour comp in NetworkBehaviours)
  585. {
  586. // an exception in OnStartServer should be caught, so that one
  587. // component's exception doesn't stop all other components from
  588. // being initialized
  589. // => this is what Unity does for Start() etc. too.
  590. // one exception doesn't stop all the other Start() calls!
  591. try
  592. {
  593. comp.OnStopServer();
  594. }
  595. catch (Exception e)
  596. {
  597. Debug.LogException(e, comp);
  598. }
  599. }
  600. }
  601. bool clientStarted;
  602. internal void OnStartClient()
  603. {
  604. if (clientStarted)
  605. return;
  606. clientStarted = true;
  607. isClient = true;
  608. // set isLocalPlayer earlier, in case OnStartLocalplayer is called
  609. // AFTER OnStartClient, in which case it would still be falsse here.
  610. // many projects will check isLocalPlayer in OnStartClient though.
  611. // TODO ideally set isLocalPlayer when NetworkClient.localPlayer is set?
  612. if (NetworkClient.localPlayer == this)
  613. {
  614. isLocalPlayer = true;
  615. }
  616. // Debug.Log("OnStartClient " + gameObject + " netId:" + netId);
  617. foreach (NetworkBehaviour comp in NetworkBehaviours)
  618. {
  619. // an exception in OnStartClient should be caught, so that one
  620. // component's exception doesn't stop all other components from
  621. // being initialized
  622. // => this is what Unity does for Start() etc. too.
  623. // one exception doesn't stop all the other Start() calls!
  624. try
  625. {
  626. // user implemented startup
  627. comp.OnStartClient();
  628. }
  629. catch (Exception e)
  630. {
  631. Debug.LogException(e, comp);
  632. }
  633. }
  634. }
  635. internal void OnStopClient()
  636. {
  637. foreach (NetworkBehaviour comp in NetworkBehaviours)
  638. {
  639. // an exception in OnStopClient should be caught, so that
  640. // one component's exception doesn't stop all other components
  641. // from being initialized
  642. // => this is what Unity does for Start() etc. too.
  643. // one exception doesn't stop all the other Start() calls!
  644. try
  645. {
  646. comp.OnStopClient();
  647. }
  648. catch (Exception e)
  649. {
  650. Debug.LogException(e, comp);
  651. }
  652. }
  653. }
  654. // TODO any way to make this not static?
  655. // introduced in https://github.com/vis2k/Mirror/commit/c7530894788bb843b0f424e8f25029efce72d8ca#diff-dc8b7a5a67840f75ccc884c91b9eb76ab7311c9ca4360885a7e41d980865bdc2
  656. // for PR https://github.com/vis2k/Mirror/pull/1263
  657. //
  658. // explanation:
  659. // we send the spawn message multiple times. Whenever an object changes
  660. // authority, we send the spawn message again for the object. This is
  661. // necessary because we need to reinitialize all variables when
  662. // ownership change due to sync to owner feature.
  663. // Without this static, the second time we get the spawn message we
  664. // would call OnStartLocalPlayer again on the same object
  665. static NetworkIdentity previousLocalPlayer = null;
  666. internal void OnStartLocalPlayer()
  667. {
  668. if (previousLocalPlayer == this)
  669. return;
  670. previousLocalPlayer = this;
  671. isLocalPlayer = true;
  672. foreach (NetworkBehaviour comp in NetworkBehaviours)
  673. {
  674. // an exception in OnStartLocalPlayer should be caught, so that
  675. // one component's exception doesn't stop all other components
  676. // from being initialized
  677. // => this is what Unity does for Start() etc. too.
  678. // one exception doesn't stop all the other Start() calls!
  679. try
  680. {
  681. comp.OnStartLocalPlayer();
  682. }
  683. catch (Exception e)
  684. {
  685. Debug.LogException(e, comp);
  686. }
  687. }
  688. }
  689. bool hadAuthority;
  690. internal void NotifyAuthority()
  691. {
  692. if (!hadAuthority && hasAuthority)
  693. OnStartAuthority();
  694. if (hadAuthority && !hasAuthority)
  695. OnStopAuthority();
  696. hadAuthority = hasAuthority;
  697. }
  698. internal void OnStartAuthority()
  699. {
  700. foreach (NetworkBehaviour comp in NetworkBehaviours)
  701. {
  702. // an exception in OnStartAuthority should be caught, so that one
  703. // component's exception doesn't stop all other components from
  704. // being initialized
  705. // => this is what Unity does for Start() etc. too.
  706. // one exception doesn't stop all the other Start() calls!
  707. try
  708. {
  709. comp.OnStartAuthority();
  710. }
  711. catch (Exception e)
  712. {
  713. Debug.LogException(e, comp);
  714. }
  715. }
  716. }
  717. internal void OnStopAuthority()
  718. {
  719. foreach (NetworkBehaviour comp in NetworkBehaviours)
  720. {
  721. // an exception in OnStopAuthority should be caught, so that one
  722. // component's exception doesn't stop all other components from
  723. // being initialized
  724. // => this is what Unity does for Start() etc. too.
  725. // one exception doesn't stop all the other Start() calls!
  726. try
  727. {
  728. comp.OnStopAuthority();
  729. }
  730. catch (Exception e)
  731. {
  732. Debug.LogException(e, comp);
  733. }
  734. }
  735. }
  736. // interest management /////////////////////////////////////////////////
  737. // obsoletes to still support ProximityChecker while transitioning to
  738. // global Interest Management
  739. // Deprecated 2021-02-17
  740. [Obsolete("Use NetworkServer.RebuildObservers(identity, initialize) instead.")]
  741. public void RebuildObservers(bool initialize) => NetworkServer.RebuildObservers(this, initialize);
  742. // vis2k: readstring bug prevention: https://github.com/vis2k/Mirror/issues/2617
  743. // -> OnSerialize writes length,componentData,length,componentData,...
  744. // -> OnDeserialize carefully extracts each data, then deserializes each component with separate readers
  745. // -> it will be impossible to read too many or too few bytes in OnDeserialize
  746. // -> we can properly track down errors
  747. bool OnSerializeSafely(NetworkBehaviour comp, NetworkWriter writer, bool initialState)
  748. {
  749. // write placeholder length bytes
  750. // (jumping back later is WAY faster than allocating a temporary
  751. // writer for the payload, then writing payload.size, payload)
  752. int headerPosition = writer.Position;
  753. writer.WriteInt(0);
  754. int contentPosition = writer.Position;
  755. // write payload
  756. bool result = false;
  757. try
  758. {
  759. result = comp.OnSerialize(writer, initialState);
  760. }
  761. catch (Exception e)
  762. {
  763. // show a detailed error and let the user know what went wrong
  764. Debug.LogError("OnSerialize failed for: object=" + name + " component=" + comp.GetType() + " sceneId=" + sceneId.ToString("X") + "\n\n" + e);
  765. }
  766. int endPosition = writer.Position;
  767. // fill in length now
  768. writer.Position = headerPosition;
  769. writer.WriteInt(endPosition - contentPosition);
  770. writer.Position = endPosition;
  771. // Debug.Log("OnSerializeSafely written for object=" + comp.name + " component=" + comp.GetType() + " sceneId=" + sceneId.ToString("X") + "header@" + headerPosition + " content@" + contentPosition + " end@" + endPosition + " contentSize=" + (endPosition - contentPosition));
  772. return result;
  773. }
  774. // serialize all components using dirtyComponentsMask
  775. // check ownerWritten/observersWritten to know if anything was written
  776. // We pass dirtyComponentsMask into this function so that we can check
  777. // if any Components are dirty before creating writers
  778. internal void OnSerializeAllSafely(bool initialState, NetworkWriter ownerWriter, NetworkWriter observersWriter)
  779. {
  780. // check if components are in byte.MaxRange just to be 100% sure
  781. // that we avoid overflows
  782. NetworkBehaviour[] components = NetworkBehaviours;
  783. if (components.Length > byte.MaxValue)
  784. throw new IndexOutOfRangeException($"{name} has more than {byte.MaxValue} components. This is not supported.");
  785. // serialize all components
  786. for (int i = 0; i < components.Length; ++i)
  787. {
  788. // is this component dirty?
  789. // -> always serialize if initialState so all components are included in spawn packet
  790. // -> note: IsDirty() is false if the component isn't dirty or sendInterval isn't elapsed yet
  791. NetworkBehaviour comp = components[i];
  792. if (initialState || comp.IsDirty())
  793. {
  794. // Debug.Log("OnSerializeAllSafely: " + name + " -> " + comp.GetType() + " initial=" + initialState);
  795. // remember start position in case we need to copy it into
  796. // observers writer too
  797. int startPosition = ownerWriter.Position;
  798. // write index as byte [0..255]
  799. ownerWriter.WriteByte((byte)i);
  800. // serialize into ownerWriter first
  801. // (owner always gets everything!)
  802. OnSerializeSafely(comp, ownerWriter, initialState);
  803. // copy into observersWriter too if SyncMode.Observers
  804. // -> we copy instead of calling OnSerialize again because
  805. // we don't know what magic the user does in OnSerialize.
  806. // -> it's not guaranteed that calling it twice gets the
  807. // same result
  808. // -> it's not guaranteed that calling it twice doesn't mess
  809. // with the user's OnSerialize timing code etc.
  810. // => so we just copy the result without touching
  811. // OnSerialize again
  812. if (comp.syncMode == SyncMode.Observers)
  813. {
  814. ArraySegment<byte> segment = ownerWriter.ToArraySegment();
  815. int length = ownerWriter.Position - startPosition;
  816. observersWriter.WriteBytes(segment.Array, startPosition, length);
  817. }
  818. }
  819. }
  820. }
  821. // get cached serialization for this tick (or serialize if none yet)
  822. // IMPORTANT: int tick avoids floating point inaccuracy over days/weeks
  823. internal NetworkIdentitySerialization GetSerializationAtTick(int tick)
  824. {
  825. // reserialize if tick is different than last changed.
  826. // NOTE: != instead of < because int.max+1 overflows at some point.
  827. if (lastSerialization.tick != tick)
  828. {
  829. // reset
  830. lastSerialization.ownerWriter.Position = 0;
  831. lastSerialization.observersWriter.Position = 0;
  832. // serialize
  833. OnSerializeAllSafely(false,
  834. lastSerialization.ownerWriter,
  835. lastSerialization.observersWriter);
  836. // set tick
  837. lastSerialization.tick = tick;
  838. //Debug.Log($"{name} (netId={netId}) serialized for tick={tickTimeStamp}");
  839. }
  840. // return it
  841. return lastSerialization;
  842. }
  843. void OnDeserializeSafely(NetworkBehaviour comp, NetworkReader reader, bool initialState)
  844. {
  845. // read header as 4 bytes and calculate this chunk's start+end
  846. int contentSize = reader.ReadInt();
  847. int chunkStart = reader.Position;
  848. int chunkEnd = reader.Position + contentSize;
  849. // call OnDeserialize and wrap it in a try-catch block so there's no
  850. // way to mess up another component's deserialization
  851. try
  852. {
  853. // Debug.Log("OnDeserializeSafely: " + comp.name + " component=" + comp.GetType() + " sceneId=" + sceneId.ToString("X") + " length=" + contentSize);
  854. comp.OnDeserialize(reader, initialState);
  855. }
  856. catch (Exception e)
  857. {
  858. // show a detailed error and let the user know what went wrong
  859. Debug.LogError($"OnDeserialize failed Exception={e.GetType()} (see below) object={name} component={comp.GetType()} sceneId={sceneId:X} length={contentSize}. Possible Reasons:\n" +
  860. $" * Do {comp.GetType()}'s OnSerialize and OnDeserialize calls write the same amount of data({contentSize} bytes)? \n" +
  861. $" * Was there an exception in {comp.GetType()}'s OnSerialize/OnDeserialize code?\n" +
  862. $" * Are the server and client the exact same project?\n" +
  863. $" * Maybe this OnDeserialize call was meant for another GameObject? The sceneIds can easily get out of sync if the Hierarchy was modified only in the client OR the server. Try rebuilding both.\n\n" +
  864. $"Exception {e}");
  865. }
  866. // now the reader should be EXACTLY at 'before + size'.
  867. // otherwise the component read too much / too less data.
  868. if (reader.Position != chunkEnd)
  869. {
  870. // warn the user
  871. int bytesRead = reader.Position - chunkStart;
  872. Debug.LogWarning("OnDeserialize was expected to read " + contentSize + " instead of " + bytesRead + " bytes for object:" + name + " component=" + comp.GetType() + " sceneId=" + sceneId.ToString("X") + ". Make sure that OnSerialize and OnDeserialize write/read the same amount of data in all cases.");
  873. // fix the position, so the following components don't all fail
  874. reader.Position = chunkEnd;
  875. }
  876. }
  877. internal void OnDeserializeAllSafely(NetworkReader reader, bool initialState)
  878. {
  879. if (NetworkBehaviours == null)
  880. {
  881. Debug.LogError($"NetworkBehaviours array is null on {gameObject.name}!\n" +
  882. $"Typically this can happen when a networked object is a child of a " +
  883. $"non-networked parent that's disabled, preventing Awake on the networked object " +
  884. $"from being invoked, where the NetworkBehaviours array is initialized.", gameObject);
  885. return;
  886. }
  887. // deserialize all components that were received
  888. NetworkBehaviour[] components = NetworkBehaviours;
  889. while (reader.Remaining > 0)
  890. {
  891. // read & check index [0..255]
  892. byte index = reader.ReadByte();
  893. if (index < components.Length)
  894. {
  895. // deserialize this component
  896. OnDeserializeSafely(components[index], reader, initialState);
  897. }
  898. }
  899. }
  900. // Helper function to handle Command/Rpc
  901. internal void HandleRemoteCall(int componentIndex, int functionHash, MirrorInvokeType invokeType, NetworkReader reader, NetworkConnectionToClient senderConnection = null)
  902. {
  903. // check if unity object has been destroyed
  904. if (this == null)
  905. {
  906. Debug.LogWarning($"{invokeType} [{functionHash}] received for deleted object [netId={netId}]");
  907. return;
  908. }
  909. // find the right component to invoke the function on
  910. if (componentIndex < 0 || componentIndex >= NetworkBehaviours.Length)
  911. {
  912. Debug.LogWarning($"Component [{componentIndex}] not found for [netId={netId}]");
  913. return;
  914. }
  915. NetworkBehaviour invokeComponent = NetworkBehaviours[componentIndex];
  916. if (!RemoteCallHelper.InvokeHandlerDelegate(functionHash, invokeType, reader, invokeComponent, senderConnection))
  917. {
  918. Debug.LogError($"Found no receiver for incoming {invokeType} [{functionHash}] on {gameObject.name}, the server and client should have the same NetworkBehaviour instances [netId={netId}].");
  919. }
  920. }
  921. // Runs on server
  922. internal CommandInfo GetCommandInfo(int componentIndex, int cmdHash)
  923. {
  924. // check if unity object has been destroyed
  925. if (this == null)
  926. {
  927. // error can be logged later
  928. return default;
  929. }
  930. // find the right component to invoke the function on
  931. if (0 <= componentIndex && componentIndex < NetworkBehaviours.Length)
  932. {
  933. NetworkBehaviour invokeComponent = NetworkBehaviours[componentIndex];
  934. return RemoteCallHelper.GetCommandInfo(cmdHash, invokeComponent);
  935. }
  936. else
  937. {
  938. // error can be logged later
  939. return default;
  940. }
  941. }
  942. // Called when NetworkIdentity is destroyed
  943. internal void ClearObservers()
  944. {
  945. if (observers != null)
  946. {
  947. foreach (NetworkConnection conn in observers.Values)
  948. {
  949. conn.RemoveFromObserving(this, true);
  950. }
  951. observers.Clear();
  952. }
  953. }
  954. internal void AddObserver(NetworkConnection conn)
  955. {
  956. if (observers == null)
  957. {
  958. Debug.LogError("AddObserver for " + gameObject + " observer list is null");
  959. return;
  960. }
  961. if (observers.ContainsKey(conn.connectionId))
  962. {
  963. // if we try to add a connectionId that was already added, then
  964. // we may have generated one that was already in use.
  965. return;
  966. }
  967. // Debug.Log("Added observer " + conn.address + " added for " + gameObject);
  968. observers[conn.connectionId] = conn;
  969. conn.AddToObserving(this);
  970. }
  971. /// <summary>Assign control of an object to a client via the client's NetworkConnection.</summary>
  972. // This causes hasAuthority to be set on the client that owns the object,
  973. // and NetworkBehaviour.OnStartAuthority will be called on that client.
  974. // This object then will be in the NetworkConnection.clientOwnedObjects
  975. // list for the connection.
  976. //
  977. // Authority can be removed with RemoveClientAuthority. Only one client
  978. // can own an object at any time. This does not need to be called for
  979. // player objects, as their authority is setup automatically.
  980. public bool AssignClientAuthority(NetworkConnection conn)
  981. {
  982. if (!isServer)
  983. {
  984. Debug.LogError("AssignClientAuthority can only be called on the server for spawned objects.");
  985. return false;
  986. }
  987. if (conn == null)
  988. {
  989. Debug.LogError("AssignClientAuthority for " + gameObject + " owner cannot be null. Use RemoveClientAuthority() instead.");
  990. return false;
  991. }
  992. if (connectionToClient != null && conn != connectionToClient)
  993. {
  994. Debug.LogError("AssignClientAuthority for " + gameObject + " already has an owner. Use RemoveClientAuthority() first.");
  995. return false;
  996. }
  997. SetClientOwner(conn);
  998. // The client will match to the existing object
  999. // update all variables and assign authority
  1000. NetworkServer.SendSpawnMessage(this, conn);
  1001. clientAuthorityCallback?.Invoke(conn, this, true);
  1002. return true;
  1003. }
  1004. /// <summary>Removes ownership for an object.</summary>
  1005. // Applies to objects that had authority set by AssignClientAuthority,
  1006. // or NetworkServer.Spawn with a NetworkConnection parameter included.
  1007. // Authority cannot be removed for player objects.
  1008. public void RemoveClientAuthority()
  1009. {
  1010. if (!isServer)
  1011. {
  1012. Debug.LogError("RemoveClientAuthority can only be called on the server for spawned objects.");
  1013. return;
  1014. }
  1015. if (connectionToClient?.identity == this)
  1016. {
  1017. Debug.LogError("RemoveClientAuthority cannot remove authority for a player object");
  1018. return;
  1019. }
  1020. if (connectionToClient != null)
  1021. {
  1022. clientAuthorityCallback?.Invoke(connectionToClient, this, false);
  1023. NetworkConnectionToClient previousOwner = connectionToClient;
  1024. // TODO why do we clear this twice?
  1025. connectionToClient = null;
  1026. // we need to resynchronize the entire object
  1027. // so just spawn it again,
  1028. // the client will not create a new instance, it will simply
  1029. // reset all variables and remove authority
  1030. NetworkServer.SendSpawnMessage(this, previousOwner);
  1031. // TODO why do we clear this twice?
  1032. connectionToClient = null;
  1033. }
  1034. }
  1035. // Reset is called when the user hits the Reset button in the
  1036. // Inspector's context menu or when adding the component the first time.
  1037. // This function is only called in editor mode.
  1038. //
  1039. // Reset() seems to be called only for Scene objects.
  1040. // we can't destroy them (they are always in the scene).
  1041. // instead we disable them and call Reset().
  1042. //
  1043. // OLD COMMENT:
  1044. // Marks the identity for future reset, this is because we cant reset
  1045. // the identity during destroy as people might want to be able to read
  1046. // the members inside OnDestroy(), and we have no way of invoking reset
  1047. // after OnDestroy is called.
  1048. internal void Reset()
  1049. {
  1050. // make sure to call this before networkBehavioursCache is cleared below
  1051. ResetSyncObjects();
  1052. hasSpawned = false;
  1053. clientStarted = false;
  1054. isClient = false;
  1055. isServer = false;
  1056. //isLocalPlayer = false; <- cleared AFTER ClearLocalPlayer below!
  1057. netId = 0;
  1058. connectionToServer = null;
  1059. connectionToClient = null;
  1060. ClearObservers();
  1061. // clear local player if it was the local player,
  1062. // THEN reset isLocalPlayer AFTERWARDS
  1063. if (isLocalPlayer)
  1064. {
  1065. // only clear NetworkClient.localPlayer IF IT POINTS TO US!
  1066. // see OnDestroy() comments. it does the same.
  1067. // (https://github.com/vis2k/Mirror/issues/2635)
  1068. if (NetworkClient.localPlayer == this)
  1069. NetworkClient.localPlayer = null;
  1070. }
  1071. isLocalPlayer = false;
  1072. }
  1073. // clear all component's dirty bits no matter what
  1074. internal void ClearAllComponentsDirtyBits()
  1075. {
  1076. foreach (NetworkBehaviour comp in NetworkBehaviours)
  1077. {
  1078. comp.ClearAllDirtyBits();
  1079. }
  1080. }
  1081. // Clear only dirty component's dirty bits. ignores components which
  1082. // may be dirty but not ready to be synced yet (because of syncInterval)
  1083. internal void ClearDirtyComponentsDirtyBits()
  1084. {
  1085. foreach (NetworkBehaviour comp in NetworkBehaviours)
  1086. {
  1087. if (comp.IsDirty())
  1088. {
  1089. comp.ClearAllDirtyBits();
  1090. }
  1091. }
  1092. }
  1093. void ResetSyncObjects()
  1094. {
  1095. // ResetSyncObjects is called by Reset, which is called by Unity.
  1096. // AddComponent() calls Reset().
  1097. // AddComponent() is called before Awake().
  1098. // so NetworkBehaviours may not be initialized yet.
  1099. if (NetworkBehaviours == null)
  1100. return;
  1101. foreach (NetworkBehaviour comp in NetworkBehaviours)
  1102. {
  1103. comp.ResetSyncObjects();
  1104. }
  1105. }
  1106. }
  1107. }