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 <lukasolson@greyblockgames.com>

* Updated method names

Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com>

* Updated more string sync dictionaries

Ped_Networking.cs
VehicleController.cs
Player.cs
SyncedServerData.cs

Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com>

* Updated NetworkTime fields in NetStats.cs

Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com>

* Updated syncData types

Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com>

* Implemented conditional compilation to replace old isHeadless

Signed-off-by: Lukas Olson <lukasolson@greyblockgames.com>

* 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 b1c9d38e3a

* Reverted JoinGameWindow.cs to commit b1c9d38e3a

* 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 <lukasolson@greyblockgames.com>
This commit is contained in:
Lukas 2022-04-02 11:15:08 -07:00 committed by GitHub
parent 42df0daa8c
commit 0445f29dfd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 178 additions and 151 deletions

@ -1 +1 @@
Subproject commit ee34eb4807d5d5d3b0121689acb286876633ffd1
Subproject commit 36591bb183cea0cb1c29f138fb5b226eca46f697

View file

@ -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);
}
}

View file

@ -32,11 +32,9 @@ namespace SanAndreasUnity.Behaviours
public static int NumStateChangesReceived { get; private set; }
public class SyncDictionaryStringUint : Mirror.SyncDictionary<string, uint> { }
public readonly SyncDictionary<string, uint> syncDictionaryStringUint = new SyncDictionary<string, uint>();
public SyncDictionaryStringUint syncDictionaryStringUint = new SyncDictionaryStringUint();
private SyncedBag.StringSyncDictionary _syncDictionary = new SyncedBag.StringSyncDictionary();
private readonly SyncDictionary<string, string> _syncDictionary = new SyncDictionary<string, string>();
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;

View file

@ -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

View file

@ -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<string, uint>.Operation op, string key, uint value)
{
// switch (op)
// {

View file

@ -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
{

View file

@ -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<WheelSyncData> { }
WheelSyncList m_net_wheelsData = new WheelSyncList();
readonly SyncList<WheelSyncData> m_net_wheelsData = new SyncList<WheelSyncData>();
private readonly SyncedBag.StringSyncDictionary m_syncDictionary = new SyncedBag.StringSyncDictionary();
private readonly SyncDictionary<string, string> m_syncDictionary = new SyncDictionary<string, string>();
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)));
}
}
}

View file

@ -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.
}
}
}
}

View file

@ -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);
}
}
}
}
}

View file

@ -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<uint, NetworkIdentity> 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
/// </summary>
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<NetworkIdentity>();
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<NetworkIdentity>();
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;
}
}

View file

@ -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));
}
}

View file

@ -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<string, string> m_syncDictionary = new SyncDictionary<string, string>();
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);

View file

@ -9,12 +9,10 @@ namespace SanAndreasUnity.Net
{
public class SyncedBag
{
public class StringSyncDictionary : SyncDictionary<string, string>
{
}
private readonly SyncDictionary<string, string> m_syncDictionary;
private readonly StringSyncDictionary m_syncDictionary;
private readonly Dictionary<string, List<System.Action<string>>> m_callbacks = new Dictionary<string, List<Action<string>>>();
private readonly Dictionary<string, List<System.Action<string>>> m_callbacks =
new Dictionary<string, List<Action<string>>>();
private struct ArrayWrapper<T> // use struct so that it doesn't allocate memory
{
@ -27,24 +25,22 @@ namespace SanAndreasUnity.Net
}
public SyncedBag(StringSyncDictionary syncDictionary)
public SyncedBag(SyncDictionary<string, string> syncDictionary)
{
m_syncDictionary = syncDictionary;
m_syncDictionary.Callback += DictionaryCallback;
}
private void DictionaryCallback(StringSyncDictionary.Operation op, string key, string item)
private void DictionaryCallback(SyncDictionary<string, string>.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<string, string>.Operation.OP_ADD:
case SyncDictionary<string, string>.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<Action<string>>{callback});
m_callbacks.Add(key, new List<Action<string>> { callback });
}
}
@ -178,4 +174,4 @@ namespace SanAndreasUnity.Net
SetString(key, JsonUtility.ToJson(new ArrayWrapper<float>(array)));
}
}
}
}

View file

@ -8,9 +8,9 @@ namespace SanAndreasUnity.Net
{
public static SyncedServerData Instance { get; private set; }
SyncedBag.StringSyncDictionary _syncDictionary = new SyncedBag.StringSyncDictionary();
readonly SyncDictionary<string, string> _syncDictionary = new SyncDictionary<string, string>();
public static SyncedBag Data { get; private set; } = new SyncedBag(new SyncedBag.StringSyncDictionary());
public static SyncedBag Data { get; private set; } = new SyncedBag(new SyncDictionary<string, string>());
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<string, string>());
}
public override void OnStartClient()

View file

@ -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}");
}
}
}
}

View file

@ -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<ServerInfo> _serversFromMasterServer = new List<ServerInfo>();
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<Mirror.NetworkDiscoveryHUD>();
m_netDiscoveryHUD.connectAction = this.ConnectFromDiscovery;
m_netDiscoveryHUD.drawGUI = false;
m_netDiscoveryHUD = Mirror.NetworkManager.singleton.GetComponentOrThrow<NetworkDiscoveryHUD>();
// 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
{

View file

@ -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