From 0445f29dfd6c245a81b038fca65875e2d1fcd4ce Mon Sep 17 00:00:00 2001 From: Lukas Date: Sat, 2 Apr 2022 11:15:08 -0700 Subject: [PATCH] Feature/mirror upgrade (#127) * Updated SyncDictionary usage in SyncedBag.cs - Removed the extra class that is no longer needed for SyncDictionary's - Removed OP codes (OP_DIRTY) switch case that no longer exists (superseded by OP_SET) Signed-off-by: Lukas Olson * Updated method names Signed-off-by: Lukas Olson * Updated more string sync dictionaries Ped_Networking.cs VehicleController.cs Player.cs SyncedServerData.cs Signed-off-by: Lukas Olson * Updated NetworkTime fields in NetStats.cs Signed-off-by: Lukas Olson * Updated syncData types Signed-off-by: Lukas Olson * Implemented conditional compilation to replace old isHeadless Signed-off-by: Lukas Olson * Updated hooks * A few more syncDictionary upgrades * Moved away from obsolete NetworkIdentity.spawned * Updated SetDirtyBit to SetSyncVarDirtyBit in DeadBody.cs * Updated ScriptingDefineSymbols for Mirror * Updated JoinGameWindow.cs * Use latest MirrorLite commit for submodule * Use latest MirrorLite commit for submodule * Reverted EditorSettings.asset to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Reverted JoinGameWindow.cs to commit b1c9d38e3ac5113e899ac34ad07bcb8a2890d18b * Changed method for headless mode in NetCmdLineHandler.cs Changed from compiler defs to SanAndreasUnity helpers for determining headless mode in NetCmdLineHandler.cs * Re-Added ConfigureHeadlessFrameRate override on CustomNetworkManager.cs * Started updating JoinGameWindow.cs - Commented out GUI errors - Updated type 'DiscoveryInfo' to 'ServerResponse' in method ConnectFromDiscovery() params. - Updated Connect() 'port' parameter to use type 'int' rather than 'ushort' as per Mirror conventions. Co-authored-by: Lukas Olson --- Assets/MirrorLite | 2 +- Assets/Scripts/Behaviours/Ped/DeadBody.cs | 4 +- .../Scripts/Behaviours/Ped/Ped_Networking.cs | 14 ++- .../Behaviours/Ped/States/BaseVehicleState.cs | 5 +- .../Behaviours/Ped/States/RollState.cs | 5 +- .../Ped/States/VehicleSittingState.cs | 1 + .../Behaviours/Vehicles/VehicleController.cs | 86 +++++++++++-------- .../Networking/CustomNetworkManager.cs | 38 ++++---- .../Scripts/Networking/NetCmdLineHandler.cs | 24 +++--- Assets/Scripts/Networking/NetManager.cs | 55 ++++++++---- Assets/Scripts/Networking/NetworkRigidBody.cs | 8 +- Assets/Scripts/Networking/Player.cs | 8 +- Assets/Scripts/Networking/SyncedBag.cs | 22 ++--- Assets/Scripts/Networking/SyncedServerData.cs | 6 +- Assets/Scripts/Stats/NetStats.cs | 16 ++-- Assets/Scripts/UI/JoinGameWindow.cs | 33 ++++--- ProjectSettings/ProjectSettings.asset | 2 +- 17 files changed, 178 insertions(+), 151 deletions(-) diff --git a/Assets/MirrorLite b/Assets/MirrorLite index ee34eb48..36591bb1 160000 --- a/Assets/MirrorLite +++ b/Assets/MirrorLite @@ -1 +1 @@ -Subproject commit ee34eb4807d5d5d3b0121689acb286876633ffd1 +Subproject commit 36591bb183cea0cb1c29f138fb5b226eca46f697 diff --git a/Assets/Scripts/Behaviours/Ped/DeadBody.cs b/Assets/Scripts/Behaviours/Ped/DeadBody.cs index 6133e4f3..6768c740 100644 --- a/Assets/Scripts/Behaviours/Ped/DeadBody.cs +++ b/Assets/Scripts/Behaviours/Ped/DeadBody.cs @@ -182,7 +182,7 @@ namespace SanAndreasUnity.Behaviours.Peds public override void OnDeserialize(NetworkReader reader, bool initialState) { if (initialState) - m_net_modelId = reader.ReadInt32(); + m_net_modelId = reader.ReadInt(); byte flags = reader.ReadByte(); @@ -254,7 +254,7 @@ namespace SanAndreasUnity.Behaviours.Peds { if (NetStatus.IsServer) { - this.SetDirtyBit(1); + this.SetSyncVarDirtyBit(1); } } diff --git a/Assets/Scripts/Behaviours/Ped/Ped_Networking.cs b/Assets/Scripts/Behaviours/Ped/Ped_Networking.cs index ef94ba99..1a927af1 100644 --- a/Assets/Scripts/Behaviours/Ped/Ped_Networking.cs +++ b/Assets/Scripts/Behaviours/Ped/Ped_Networking.cs @@ -32,11 +32,9 @@ namespace SanAndreasUnity.Behaviours public static int NumStateChangesReceived { get; private set; } - public class SyncDictionaryStringUint : Mirror.SyncDictionary { } + public readonly SyncDictionary syncDictionaryStringUint = new SyncDictionary(); - public SyncDictionaryStringUint syncDictionaryStringUint = new SyncDictionaryStringUint(); - - private SyncedBag.StringSyncDictionary _syncDictionary = new SyncedBag.StringSyncDictionary(); + private readonly SyncDictionary _syncDictionary = new SyncDictionary(); public SyncedBag SyncedBag { get; } [SyncVar] Vector3 m_net_movementInput; @@ -189,7 +187,7 @@ namespace SanAndreasUnity.Behaviours } - void Net_OnIdChanged(int newId) + void Net_OnIdChanged(int oldId, int newId) { //Debug.LogFormat("ped (net id {0}) changed model id to {1}", this.netId, newId); @@ -199,7 +197,7 @@ namespace SanAndreasUnity.Behaviours this.TryToLoadNewModel(newId); } - void Net_OnStateChanged(string newStateName) + void Net_OnStateChanged(string oldStateName, string newStateName) { if (this.isServer) return; @@ -241,7 +239,7 @@ namespace SanAndreasUnity.Behaviours } - void Net_OnWeaponChanged(int newSlot) + void Net_OnWeaponChanged(int oldSlot, int newSlot) { if (NetStatus.IsServer) @@ -261,7 +259,7 @@ namespace SanAndreasUnity.Behaviours } - void Net_OnMouthSoundIdChanged(Audio.SoundId newSoundId) + void Net_OnMouthSoundIdChanged(Audio.SoundId oldSoundId, Audio.SoundId newSoundId) { if (NetStatus.IsServer) return; diff --git a/Assets/Scripts/Behaviours/Ped/States/BaseVehicleState.cs b/Assets/Scripts/Behaviours/Ped/States/BaseVehicleState.cs index 84147979..acab12c1 100644 --- a/Assets/Scripts/Behaviours/Ped/States/BaseVehicleState.cs +++ b/Assets/Scripts/Behaviours/Ped/States/BaseVehicleState.cs @@ -1,3 +1,4 @@ +using Mirror; using UnityEngine; using SanAndreasUnity.Utilities; using SanAndreasUnity.Behaviours.Vehicles; @@ -52,8 +53,8 @@ namespace SanAndreasUnity.Behaviours.Peds.States // extract vehicle and seat from data - int magicNumber = reader.ReadInt32(); - m_currentVehicleNetId = reader.ReadUInt32(); + int magicNumber = reader.ReadInt(); + m_currentVehicleNetId = reader.ReadUInt(); this.CurrentVehicleSeatAlignment = (Vehicle.SeatAlignment) reader.ReadSByte(); // assign current vehicle diff --git a/Assets/Scripts/Behaviours/Ped/States/RollState.cs b/Assets/Scripts/Behaviours/Ped/States/RollState.cs index 7a00fa39..ce64acbb 100644 --- a/Assets/Scripts/Behaviours/Ped/States/RollState.cs +++ b/Assets/Scripts/Behaviours/Ped/States/RollState.cs @@ -1,3 +1,4 @@ +using Mirror; using UnityEngine; using SanAndreasUnity.Utilities; using SanAndreasUnity.Importing.Animation; @@ -69,12 +70,12 @@ namespace SanAndreasUnity.Behaviours.Peds.States string magicWord = reader.ReadString(); if (magicWord != "roll") Debug.LogErrorFormat("wrong magic word when switching to roll state: {0}", magicWord); - m_rollLeft = reader.ReadBoolean(); + m_rollLeft = reader.ReadBool(); m_ped.SwitchState(this.GetType()); } - void OnDictChanged(Ped.SyncDictionaryStringUint.Operation op, string key, uint value) + void OnDictChanged(SyncDictionary.Operation op, string key, uint value) { // switch (op) // { diff --git a/Assets/Scripts/Behaviours/Ped/States/VehicleSittingState.cs b/Assets/Scripts/Behaviours/Ped/States/VehicleSittingState.cs index 12f3e477..40a69a25 100644 --- a/Assets/Scripts/Behaviours/Ped/States/VehicleSittingState.cs +++ b/Assets/Scripts/Behaviours/Ped/States/VehicleSittingState.cs @@ -3,6 +3,7 @@ using SanAndreasUnity.Utilities; using SanAndreasUnity.Behaviours.Vehicles; using SanAndreasUnity.Importing.Animation; using System.Linq; +using Mirror; namespace SanAndreasUnity.Behaviours.Peds.States { diff --git a/Assets/Scripts/Behaviours/Vehicles/VehicleController.cs b/Assets/Scripts/Behaviours/Vehicles/VehicleController.cs index 9ddac150..1aae77bd 100644 --- a/Assets/Scripts/Behaviours/Vehicles/VehicleController.cs +++ b/Assets/Scripts/Behaviours/Vehicles/VehicleController.cs @@ -7,19 +7,26 @@ using System.Collections.Generic; namespace SanAndreasUnity.Behaviours.Vehicles { - public class VehicleController : NetworkBehaviour { private Vehicle m_vehicle; bool IsControlledByLocalPlayer => m_vehicle.IsControlledByLocalPlayer; [SyncVar] int m_net_id; - [SyncVar(hook = nameof(OnNetColorsChanged))] string m_net_carColors; + + [SyncVar(hook = nameof(OnNetColorsChanged))] + string m_net_carColors; + [SyncVar] float m_net_acceleration; [SyncVar] float m_net_steering; [SyncVar] float m_net_braking; - [SyncVar(hook=nameof(OnNetPositionChanged))] Vector3 m_net_position; - [SyncVar(hook=nameof(OnNetRotationChanged))] Quaternion m_net_rotation; + + [SyncVar(hook = nameof(OnNetPositionChanged))] + Vector3 m_net_position; + + [SyncVar(hook = nameof(OnNetRotationChanged))] + Quaternion m_net_rotation; + [SyncVar] Vector3 m_net_linearVelocity; [SyncVar] Vector3 m_net_angularVelocity; [SyncVar] float m_net_health; @@ -28,24 +35,24 @@ namespace SanAndreasUnity.Behaviours.Vehicles { public float brakeTorque; public float motorTorque; + public float steerAngle; + //public float travel; public float localPosY; public float rpm; } - class WheelSyncList : SyncList { } - WheelSyncList m_net_wheelsData = new WheelSyncList(); + readonly SyncList m_net_wheelsData = new SyncList(); - private readonly SyncedBag.StringSyncDictionary m_syncDictionary = new SyncedBag.StringSyncDictionary(); + private readonly SyncDictionary m_syncDictionary = new SyncDictionary(); public SyncedBag ExtraData { get; } // is it better to place syncvars in Vehicle class ? - that way, there is no need for hooks // - or we could assign/read syncvars in Update() - private VehicleController() { ExtraData = new SyncedBag(m_syncDictionary); @@ -76,15 +83,17 @@ namespace SanAndreasUnity.Behaviours.Vehicles if (!NetStatus.IsServer) { - F.RunExceptionSafe( () => { + F.RunExceptionSafe(() => + { // load vehicle on clients Color32[] colors = DeserializeColors(m_net_carColors); - m_vehicle = Vehicle.Create(this.gameObject, m_net_id, null, this.transform.position, this.transform.rotation); + m_vehicle = Vehicle.Create(this.gameObject, m_net_id, null, this.transform.position, + this.transform.rotation); m_vehicle.SetColors(colors); - + // update rigid body status this.EnableOrDisableRigidBody(); @@ -136,7 +145,8 @@ namespace SanAndreasUnity.Behaviours.Vehicles string[] splits = colorString.Split(';'); if (splits.Length != 4) - throw new System.ArgumentException($"Failed to deserialize color - expected 4 components, found {splits.Length}"); + throw new System.ArgumentException( + $"Failed to deserialize color - expected 4 components, found {splits.Length}"); return new Color32( byte.Parse(splits[0], System.Globalization.CultureInfo.InvariantCulture), @@ -155,9 +165,8 @@ namespace SanAndreasUnity.Behaviours.Vehicles private void Update() { - // if syncvars are used for updating transform, then disable NetworkTransform, and vice versa - m_vehicle.NetTransform.enabled = ! VehicleManager.Instance.syncVehicleTransformUsingSyncVars; + m_vehicle.NetTransform.enabled = !VehicleManager.Instance.syncVehicleTransformUsingSyncVars; // update status of rigid body this.EnableOrDisableRigidBody(); @@ -174,17 +183,17 @@ namespace SanAndreasUnity.Behaviours.Vehicles this.ResetInput(); return; } - + if (null == Ped.Instance || driverSeat.OccupyingPed != Ped.Instance) return; - + // local ped is occupying driver seat float oldAcc = m_vehicle.Accelerator; float oldBrake = m_vehicle.Braking; float oldSteer = m_vehicle.Steering; - if (!GameManager.CanPlayerReadInput()) + if (!GameManager.CanPlayerReadInput()) this.ResetInput(); else this.ReadInput(); @@ -203,7 +212,6 @@ namespace SanAndreasUnity.Behaviours.Vehicles m_vehicle.Braking = oldBrake; m_vehicle.Steering = oldSteer; } - } void ProcessSyncvars() @@ -220,10 +228,13 @@ namespace SanAndreasUnity.Behaviours.Vehicles m_net_health = m_vehicle.Health; // wheels - m_net_wheelsData.Flush(); // remove current list of changes - this ensures that only the current wheel state is sent, and prevents memory leak bug in Mirror + m_net_wheelsData + .ClearChanges(); // remove current list of changes - this ensures that only the current wheel state is sent, and prevents memory leak bug in Mirror m_net_wheelsData.Clear(); - foreach (var wheel in m_vehicle.Wheels) { - m_net_wheelsData.Add(new WheelSyncData() { + foreach (var wheel in m_vehicle.Wheels) + { + m_net_wheelsData.Add(new WheelSyncData() + { brakeTorque = wheel.Collider.brakeTorque, motorTorque = wheel.Collider.motorTorque, steerAngle = wheel.Collider.steerAngle, @@ -236,7 +247,8 @@ namespace SanAndreasUnity.Behaviours.Vehicles else { // apply input - if (!this.IsControlledByLocalPlayer || (this.IsControlledByLocalPlayer && !VehicleManager.Instance.controlInputOnLocalPlayer)) + if (!this.IsControlledByLocalPlayer || (this.IsControlledByLocalPlayer && + !VehicleManager.Instance.controlInputOnLocalPlayer)) { m_vehicle.Accelerator = m_net_acceleration; m_vehicle.Steering = m_net_steering; @@ -244,9 +256,11 @@ namespace SanAndreasUnity.Behaviours.Vehicles } // update wheels - if (!this.IsControlledByLocalPlayer || (this.IsControlledByLocalPlayer && !VehicleManager.Instance.controlWheelsOnLocalPlayer)) + if (!this.IsControlledByLocalPlayer || (this.IsControlledByLocalPlayer && + !VehicleManager.Instance.controlWheelsOnLocalPlayer)) { - for (int i=0; i < m_vehicle.Wheels.Count && i < m_net_wheelsData.Count; i++) { + for (int i = 0; i < m_vehicle.Wheels.Count && i < m_net_wheelsData.Count; i++) + { var w = m_vehicle.Wheels[i]; var data = m_net_wheelsData[i]; @@ -256,6 +270,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles w.Collider.motorTorque = data.motorTorque; w.Collider.steerAngle = data.steerAngle; } + //w.Travel = data.travel; w.Child.SetLocalY(data.localPosY); Vehicle.UpdateWheelRotation(w, data.rpm, data.steerAngle); @@ -314,37 +329,39 @@ namespace SanAndreasUnity.Behaviours.Vehicles F.EnableRigidBody(m_vehicle.RigidBody); return; } - - if (VehicleManager.Instance.whenToDisableRigidBody.Matches(this.IsControlledByLocalPlayer, !NetStatus.IsServer)) + + if (VehicleManager.Instance.whenToDisableRigidBody.Matches(this.IsControlledByLocalPlayer, + !NetStatus.IsServer)) F.DisableRigidBody(m_vehicle.RigidBody); else F.EnableRigidBody(m_vehicle.RigidBody); - } - void OnNetPositionChanged(Vector3 pos) + void OnNetPositionChanged(Vector3 oldPos, Vector3 newPos) { if (NetStatus.IsServer) return; - if (VehicleManager.Instance.syncVehicleTransformUsingSyncVars) { + if (VehicleManager.Instance.syncVehicleTransformUsingSyncVars) + { if (m_vehicle != null && m_vehicle.RigidBody != null) - m_vehicle.RigidBody.MovePosition(pos); + m_vehicle.RigidBody.MovePosition(newPos); } } - void OnNetRotationChanged(Quaternion rot) + void OnNetRotationChanged(Quaternion oldRot, Quaternion newRot) { if (NetStatus.IsServer) return; - if (VehicleManager.Instance.syncVehicleTransformUsingSyncVars) { + if (VehicleManager.Instance.syncVehicleTransformUsingSyncVars) + { if (m_vehicle != null && m_vehicle.RigidBody != null) - m_vehicle.RigidBody.MoveRotation(rot); + m_vehicle.RigidBody.MoveRotation(newRot); } } - void OnNetColorsChanged(string stringColors) + void OnNetColorsChanged(string oldColors, string stringColors) { if (NetStatus.IsServer) return; @@ -352,6 +369,5 @@ namespace SanAndreasUnity.Behaviours.Vehicles if (m_vehicle != null) F.RunExceptionSafe(() => m_vehicle.SetColors(DeserializeColors(stringColors))); } - } } \ No newline at end of file diff --git a/Assets/Scripts/Networking/CustomNetworkManager.cs b/Assets/Scripts/Networking/CustomNetworkManager.cs index c88f8ef1..730d2d9f 100644 --- a/Assets/Scripts/Networking/CustomNetworkManager.cs +++ b/Assets/Scripts/Networking/CustomNetworkManager.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using UnityEngine; using Mirror; @@ -6,42 +7,38 @@ using SanAndreasUnity.Behaviours; namespace SanAndreasUnity.Net { - public class CustomNetworkManager : NetworkManager { - - public override void OnClientConnect(NetworkConnection conn) + public override void OnClientConnect() { if (NetStatus.IsServer) { // just do default action - base.OnClientConnect(conn); + base.OnClientConnect(); return; } // default method: if no scene was loaded, do Ready/AddPlayer - // we won't do this until loading process finishes if (Loader.HasLoaded) - base.OnClientConnect(conn); + base.OnClientConnect(); } - public override void OnClientSceneChanged(NetworkConnection conn) + public override void OnClientSceneChanged() { if (NetStatus.IsServer) { // just do default action - base.OnClientSceneChanged(conn); + base.OnClientSceneChanged(); return; } // default method: do Ready/AddPlayer - // we won't do this until loading process finishes if (Loader.HasLoaded) - base.OnClientSceneChanged(conn); + base.OnClientSceneChanged(); } void OnLoaderFinished() @@ -59,26 +56,25 @@ namespace SanAndreasUnity.Net return; } + // make client ready - if (ClientScene.ready) + if (NetworkClient.ready) Debug.LogErrorFormat("Client was made ready before loader finished"); else - ClientScene.Ready(NetworkClient.connection); + { + NetworkClient.Ready(); + } // add player if specified - if (autoCreatePlayer && ClientScene.localPlayer == null) + if (autoCreatePlayer && NetworkClient.localPlayer == null) { - ClientScene.AddPlayer(); + NetworkClient.AddPlayer(); } } - public override void ConfigureServerFrameRate() + public override void ConfigureHeadlessFrameRate() { - // don't set frame rate - // it will be done by other scripts - + //Overriden so that other scripts can set the framerate without Mirror overriding it. } - } - -} +} \ No newline at end of file diff --git a/Assets/Scripts/Networking/NetCmdLineHandler.cs b/Assets/Scripts/Networking/NetCmdLineHandler.cs index edb52b8d..c40e4f88 100644 --- a/Assets/Scripts/Networking/NetCmdLineHandler.cs +++ b/Assets/Scripts/Networking/NetCmdLineHandler.cs @@ -8,25 +8,28 @@ namespace SanAndreasUnity.Net { public class NetCmdLineHandler : MonoBehaviour { - public int numFramesToWait = 5; IEnumerator Start() { - if (!Mirror.NetworkManager.isHeadless) + if (F.IsInHeadlessMode) + { yield break; - - for (int i=0; i < this.numFramesToWait; i++) + } + + + + for (int i = 0; i < this.numFramesToWait; i++) yield return null; - ushort portNum = (ushort) NetManager.defaultListenPortNumber; + ushort portNum = (ushort)NetManager.defaultListenPortNumber; CmdLineUtils.GetUshortArgument("portNum", ref portNum); string sceneName = "Main"; CmdLineUtils.GetArgument("scene", ref sceneName); - ushort maxNumPlayers = (ushort) NetManager.maxNumPlayers; + ushort maxNumPlayers = (ushort)NetManager.maxNumPlayers; CmdLineUtils.GetUshortArgument("maxNumPlayers", ref maxNumPlayers); string serverIp = "127.0.0.1"; @@ -45,7 +48,8 @@ namespace SanAndreasUnity.Net if (CmdLineUtils.HasArgument("startServer")) { - Debug.LogFormat("Starting server in headless mode, params: {0}, {1}, {2}", portNum, sceneName, maxNumPlayers); + Debug.LogFormat("Starting server in headless mode, params: {0}, {1}, {2}", portNum, sceneName, + maxNumPlayers); NetManager.StartServer(portNum, sceneName, maxNumPlayers, true, false); } else if (CmdLineUtils.HasArgument("startClient")) @@ -53,10 +57,6 @@ namespace SanAndreasUnity.Net Debug.LogFormat("Starting client in headless mode, params: {0}, {1}", serverIp, portNum); NetManager.StartClient(serverIp, portNum); } - } - - } - -} +} \ No newline at end of file diff --git a/Assets/Scripts/Networking/NetManager.cs b/Assets/Scripts/Networking/NetManager.cs index 076c0be1..3ef53995 100644 --- a/Assets/Scripts/Networking/NetManager.cs +++ b/Assets/Scripts/Networking/NetManager.cs @@ -1,4 +1,6 @@ -using UnityEngine; +using System; +using System.Collections.Generic; +using UnityEngine; using Mirror; using SanAndreasUnity.Utilities; @@ -37,7 +39,27 @@ namespace SanAndreasUnity.Net private NetworkServerStatus m_lastServerStatus = NetworkServerStatus.Stopped; public event System.Action onServerStatusChanged = delegate {}; - public static int NumSpawnedNetworkObjects => NetworkIdentity.spawned.Count; + + public static Dictionary ActiveSpawnedList + { + get + { + if (NetworkServer.active) + { + return NetworkServer.spawned; + } + else if (NetworkClient.active) + { + return NetworkClient.spawned; + } + + + throw new Exception( + "NetManager.ActiveSpawnedList was accessed before NetworkServer/NetworkClient were active."); + } + } + + public static int NumSpawnedNetworkObjects => ActiveSpawnedList.Count; public static double NetworkTime => Mirror.NetworkTime.time; @@ -150,7 +172,7 @@ namespace SanAndreasUnity.Net /// public static void StopNetwork() { - // NetworkManager.singleton.StopHost (); + // NetworkManager.singleton.StopHost (); NetworkManager.singleton.StopServer (); NetworkManager.singleton.StopClient (); @@ -190,15 +212,15 @@ namespace SanAndreasUnity.Net if (string.IsNullOrEmpty (ip)) throw new System.ArgumentException ("IP address empty"); - // System.Net.IPAddress.Parse (); + // System.Net.IPAddress.Parse (); } private static void CheckIfOnlineSceneIsAssigned() { - // we won't use scene management from NetworkManager - // if (string.IsNullOrEmpty (NetManager.onlineScene)) - // throw new System.Exception ("Online scene is not assigned"); + // we won't use scene management from NetworkManager + // if (string.IsNullOrEmpty (NetManager.onlineScene)) + // throw new System.Exception ("Online scene is not assigned"); } @@ -231,12 +253,12 @@ namespace SanAndreasUnity.Net var netIdentity = go.GetComponentOrThrow(); - if (netIdentity.clientAuthorityOwner == player.connectionToClient) // already has authority + if (netIdentity.connectionToClient == player.connectionToClient) // already has authority return; // first remove existing authority client - if (netIdentity.clientAuthorityOwner != null) - netIdentity.RemoveClientAuthority(netIdentity.clientAuthorityOwner); + if (netIdentity.connectionToClient != null) + netIdentity.RemoveClientAuthority(); // assign new authority client netIdentity.AssignClientAuthority(player.connectionToClient); @@ -249,8 +271,8 @@ namespace SanAndreasUnity.Net var netIdentity = go.GetComponentOrThrow(); - if (netIdentity.clientAuthorityOwner != null) - netIdentity.RemoveClientAuthority(netIdentity.clientAuthorityOwner); + if (netIdentity.connectionToClient != null) + netIdentity.RemoveClientAuthority(); } @@ -261,13 +283,8 @@ namespace SanAndreasUnity.Net public static GameObject GetNetworkObjectById(uint netId) { - NetworkIdentity networkIdentity; - if (NetworkIdentity.spawned.TryGetValue(netId, out networkIdentity)) - { - if (networkIdentity != null) - return networkIdentity.gameObject; - } - return null; + if (!ActiveSpawnedList.TryGetValue(netId, out var networkIdentity)) return null; + return networkIdentity != null ? networkIdentity.gameObject : null; } } diff --git a/Assets/Scripts/Networking/NetworkRigidBody.cs b/Assets/Scripts/Networking/NetworkRigidBody.cs index 248832cf..57b58729 100644 --- a/Assets/Scripts/Networking/NetworkRigidBody.cs +++ b/Assets/Scripts/Networking/NetworkRigidBody.cs @@ -129,7 +129,7 @@ namespace SanAndreasUnity.Net this.UpdateClient(); } - void OnNetPositionChanged(Vector3 pos) + void OnNetPositionChanged(Vector3 oldPos, Vector3 newPos) { if (NetStatus.IsServer) return; @@ -137,10 +137,10 @@ namespace SanAndreasUnity.Net if (null == this.Rigidbody) return; - this.Rigidbody.MovePosition(pos); + this.Rigidbody.MovePosition(newPos); } - void OnNetRotationChanged(Vector3 eulers) + void OnNetRotationChanged(Vector3 oldEulers, Vector3 newEulers) { if (NetStatus.IsServer) return; @@ -148,7 +148,7 @@ namespace SanAndreasUnity.Net if (null == this.Rigidbody) return; - this.Rigidbody.MoveRotation(Quaternion.Euler(eulers)); + this.Rigidbody.MoveRotation(Quaternion.Euler(newEulers)); } } diff --git a/Assets/Scripts/Networking/Player.cs b/Assets/Scripts/Networking/Player.cs index d42f05a4..5e678413 100644 --- a/Assets/Scripts/Networking/Player.cs +++ b/Assets/Scripts/Networking/Player.cs @@ -43,7 +43,7 @@ namespace SanAndreasUnity.Net public string DescriptionForLogging => "(netId=" + this.netId + ", addr=" + (this.connectionToClient != null ? this.connectionToClient.address : "") + ")"; - private readonly SyncedBag.StringSyncDictionary m_syncDictionary = new SyncedBag.StringSyncDictionary(); + private readonly SyncDictionary m_syncDictionary = new SyncDictionary(); public SyncedBag ExtraData { get; } @@ -113,16 +113,16 @@ namespace SanAndreasUnity.Net F.InvokeEventExceptionSafe(onStart, this); } - public override void OnNetworkDestroy() + public override void OnStopClient() { - base.OnNetworkDestroy(); + base.OnStopClient(); // log some info about this if (!this.isLocalPlayer) Debug.LogFormat("Player {0} disconnected, time: {1}", this.DescriptionForLogging, F.CurrentDateForLogging); } - void OnOwnedGameObjectChanged(GameObject newGo) + void OnOwnedGameObjectChanged(GameObject oldGo, GameObject newGo) { Debug.LogFormat("Owned game object changed for player (net id {0})", this.netId); diff --git a/Assets/Scripts/Networking/SyncedBag.cs b/Assets/Scripts/Networking/SyncedBag.cs index dc8a742e..acca4ffe 100644 --- a/Assets/Scripts/Networking/SyncedBag.cs +++ b/Assets/Scripts/Networking/SyncedBag.cs @@ -9,12 +9,10 @@ namespace SanAndreasUnity.Net { public class SyncedBag { - public class StringSyncDictionary : SyncDictionary - { - } + private readonly SyncDictionary m_syncDictionary; - private readonly StringSyncDictionary m_syncDictionary; - private readonly Dictionary>> m_callbacks = new Dictionary>>(); + private readonly Dictionary>> m_callbacks = + new Dictionary>>(); private struct ArrayWrapper // use struct so that it doesn't allocate memory { @@ -27,24 +25,22 @@ namespace SanAndreasUnity.Net } - public SyncedBag(StringSyncDictionary syncDictionary) + public SyncedBag(SyncDictionary syncDictionary) { m_syncDictionary = syncDictionary; m_syncDictionary.Callback += DictionaryCallback; } - private void DictionaryCallback(StringSyncDictionary.Operation op, string key, string item) + private void DictionaryCallback(SyncDictionary.Operation op, string key, string item) { if (NetUtils.IsServer) return; switch (op) { - case StringSyncDictionary.Operation.OP_ADD: - case StringSyncDictionary.Operation.OP_SET: - case StringSyncDictionary.Operation.OP_DIRTY: - + case SyncDictionary.Operation.OP_ADD: + case SyncDictionary.Operation.OP_SET: if (m_callbacks.TryGetValue(key, out var list)) { // don't leave garbage @@ -67,7 +63,7 @@ namespace SanAndreasUnity.Net } else { - m_callbacks.Add(key, new List>{callback}); + m_callbacks.Add(key, new List> { callback }); } } @@ -178,4 +174,4 @@ namespace SanAndreasUnity.Net SetString(key, JsonUtility.ToJson(new ArrayWrapper(array))); } } -} +} \ No newline at end of file diff --git a/Assets/Scripts/Networking/SyncedServerData.cs b/Assets/Scripts/Networking/SyncedServerData.cs index d2cc997f..03c67571 100644 --- a/Assets/Scripts/Networking/SyncedServerData.cs +++ b/Assets/Scripts/Networking/SyncedServerData.cs @@ -8,9 +8,9 @@ namespace SanAndreasUnity.Net { public static SyncedServerData Instance { get; private set; } - SyncedBag.StringSyncDictionary _syncDictionary = new SyncedBag.StringSyncDictionary(); + readonly SyncDictionary _syncDictionary = new SyncDictionary(); - public static SyncedBag Data { get; private set; } = new SyncedBag(new SyncedBag.StringSyncDictionary()); + public static SyncedBag Data { get; private set; } = new SyncedBag(new SyncDictionary()); public static event System.Action onInitialSyncDataAvailable = delegate {}; @@ -49,7 +49,7 @@ namespace SanAndreasUnity.Net private void OnDisable() { // clear data for next server start - Data = new SyncedBag(new SyncedBag.StringSyncDictionary()); + Data = new SyncedBag(new SyncDictionary()); } public override void OnStartClient() diff --git a/Assets/Scripts/Stats/NetStats.cs b/Assets/Scripts/Stats/NetStats.cs index 22aaaa86..674bac15 100644 --- a/Assets/Scripts/Stats/NetStats.cs +++ b/Assets/Scripts/Stats/NetStats.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Linq; using UnityEngine; using Mirror; @@ -9,15 +8,13 @@ namespace SanAndreasUnity.Stats { public class NetStats : MonoBehaviour { - void Start() { - Utilities.Stats.RegisterStat(new Utilities.Stats.Entry(){category = "NET", onGUI = OnStatGUI}); + Utilities.Stats.RegisterStat(new Utilities.Stats.Entry() { category = "NET", onGUI = OnStatGUI }); } void OnStatGUI() { - GUILayout.Label("Time: " + NetworkTime.time); if (NetStatus.IsServer) @@ -33,15 +30,14 @@ namespace SanAndreasUnity.Stats Utilities.GUIUtils.DrawHorizontalLine(1, 1, Color.black); GUILayout.Label("Ping: " + NetworkTime.rtt); GUILayout.Label("Ping send frequency: " + NetworkTime.PingFrequency); - GUILayout.Label("Rtt sd: " + NetworkTime.rttSd); - GUILayout.Label("Rtt var: " + NetworkTime.rttVar); + GUILayout.Label("Rtt sd: " + NetworkTime.rttStandardDeviation); + GUILayout.Label("Rtt var: " + NetworkTime.rttVariance); GUILayout.Label("Server ip: " + NetworkClient.serverIp); - GUILayout.Label("Time since last message: " + (Time.unscaledTime - NetworkClient.connection.lastMessageTime)); + GUILayout.Label("Time since last message: " + + (Time.unscaledTime - NetworkClient.connection.lastMessageTime)); } GUILayout.Label($"Num spawned network objects: {NetManager.NumSpawnedNetworkObjects}"); - } - } -} +} \ No newline at end of file diff --git a/Assets/Scripts/UI/JoinGameWindow.cs b/Assets/Scripts/UI/JoinGameWindow.cs index c8dcf9f2..f8d44366 100644 --- a/Assets/Scripts/UI/JoinGameWindow.cs +++ b/Assets/Scripts/UI/JoinGameWindow.cs @@ -4,6 +4,7 @@ using SanAndreasUnity.Utilities; using SanAndreasUnity.Net; using System.Linq; using System.Threading.Tasks; +using Mirror.Discovery; namespace SanAndreasUnity.UI { @@ -18,7 +19,7 @@ namespace SanAndreasUnity.UI int m_currentTabIndex = InternetTabIndex; - Mirror.NetworkDiscoveryHUD m_netDiscoveryHUD; + NetworkDiscoveryHUD m_netDiscoveryHUD; private List _serversFromMasterServer = new List(); private Vector2 _masterServerScrollViewPos; @@ -44,9 +45,11 @@ namespace SanAndreasUnity.UI float height = width * 9f / 16f; this.windowRect = GUIUtils.GetCenteredRect(new Vector2(width, height)); - m_netDiscoveryHUD = Mirror.NetworkManager.singleton.GetComponentOrThrow(); - m_netDiscoveryHUD.connectAction = this.ConnectFromDiscovery; - m_netDiscoveryHUD.drawGUI = false; + m_netDiscoveryHUD = Mirror.NetworkManager.singleton.GetComponentOrThrow(); + + // TODO: NetworkDiscoveryHud connection actions & draw gui + // m_netDiscoveryHUD.connectAction = this.ConnectFromDiscovery; + // m_netDiscoveryHUD.drawGUI = false; } void Update() @@ -85,8 +88,9 @@ namespace SanAndreasUnity.UI } else if (LanTabIndex == m_currentTabIndex) { - m_netDiscoveryHUD.width = (int) this.WindowSize.x - 30; - m_netDiscoveryHUD.DisplayServers(); + //TODO: NetDiscovery Hud window resizing + //m_netDiscoveryHUD.width = (int) this.WindowSize.x - 30; + //m_netDiscoveryHUD.DisplayServers(); } else if (InternetTabIndex == m_currentTabIndex) { @@ -161,9 +165,10 @@ namespace SanAndreasUnity.UI { if (LanTabIndex == m_currentTabIndex) { - GUI.enabled = ! m_netDiscoveryHUD.IsRefreshing; - buttonText = m_netDiscoveryHUD.IsRefreshing ? ( "Refreshing." + new string('.', (int) ((Time.time * 2) % 3)) ) : "Refresh LAN"; - buttonAction = () => m_netDiscoveryHUD.Refresh(); + //TODO: NetDiscovery Hud visibility & button stuff + // GUI.enabled = ! m_netDiscoveryHUD.IsRefreshing; + // buttonText = m_netDiscoveryHUD.IsRefreshing ? ( "Refreshing." + new string('.', (int) ((Time.time * 2) % 3)) ) : "Refresh LAN"; + // buttonAction = () => m_netDiscoveryHUD.Refresh(); } else if (InternetTabIndex == m_currentTabIndex) { @@ -203,20 +208,20 @@ namespace SanAndreasUnity.UI void ConnectDirectly() { - this.Connect(m_ipStr, ushort.Parse(m_portStr)); + this.Connect(m_ipStr, int.Parse(m_portStr)); } - void ConnectFromDiscovery(Mirror.NetworkDiscovery.DiscoveryInfo info) + void ConnectFromDiscovery(ServerResponse info) { - this.Connect(info.EndPoint.Address.ToString(), ushort.Parse( info.KeyValuePairs[Mirror.NetworkDiscovery.kPortKey] )); + this.Connect(info.EndPoint.Address.ToString(), info.EndPoint.Port); } void ConnectToServerFromMasterServer(ServerInfo serverInfo) { - Connect(serverInfo.IP, (ushort) serverInfo.Port); + Connect(serverInfo.IP, serverInfo.Port); } - void Connect(string ip, ushort port) + void Connect(string ip, int port) { try { diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 4e4817e9..aae45797 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -677,7 +677,7 @@ PlayerSettings: webGLThreadsSupport: 0 webGLDecompressionFallback: 0 scriptingDefineSymbols: - 1: CLIENT1;UNITY_POST_PROCESSING_STACK_V2;MIRROR;MIRROR_1726_OR_NEWER;MIRROR_3_0_OR_NEWER + 1: CLIENT1;UNITY_POST_PROCESSING_STACK_V2;MIRROR;MIRROR_1726_OR_NEWER;MIRROR_3_0_OR_NEWER;MIRROR_17_0_OR_NEWER;MIRROR_18_0_OR_NEWER;MIRROR_24_0_OR_NEWER;MIRROR_26_0_OR_NEWER;MIRROR_27_0_OR_NEWER;MIRROR_28_0_OR_NEWER;MIRROR_29_0_OR_NEWER;MIRROR_30_0_OR_NEWER;MIRROR_30_5_2_OR_NEWER;MIRROR_32_1_2_OR_NEWER;MIRROR_32_1_4_OR_NEWER;MIRROR_35_0_OR_NEWER;MIRROR_35_1_OR_NEWER;MIRROR_37_0_OR_NEWER;MIRROR_38_0_OR_NEWER;MIRROR_39_0_OR_NEWER;MIRROR_40_0_OR_NEWER;MIRROR_41_0_OR_NEWER;MIRROR_42_0_OR_NEWER;MIRROR_43_0_OR_NEWER;MIRROR_44_0_OR_NEWER;MIRROR_46_0_OR_NEWER;MIRROR_47_0_OR_NEWER;MIRROR_53_0_OR_NEWER;MIRROR_55_0_OR_NEWER;MIRROR_57_0_OR_NEWER;MIRROR_58_0_OR_NEWER;MIRROR_65_0_OR_NEWER 4: UNITY_POST_PROCESSING_STACK_V2 7: CLIENT1;UNITY_POST_PROCESSING_STACK_V2;MIRROR;MIRROR_1726_OR_NEWER;MIRROR_3_0_OR_NEWER 13: UNITY_POST_PROCESSING_STACK_V2