NetworkIdentity.cs 59 KB

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