Skip to content

Commit bfc97c2

Browse files
committed
Breaking: Refactor & cleanup --
Replaced get set with private or internal value holders with auto property and clarified certain names
1 parent 78b8dc5 commit bfc97c2

13 files changed

+197
-365
lines changed

MLAPI-Editor/NetworkedObjectEditor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public override void OnInspectorGUI()
2525
if (NetworkingManager.singleton == null || (!NetworkingManager.singleton.isServer && !NetworkingManager.singleton.isClient))
2626
base.OnInspectorGUI(); //Only run this if we are NOT running server. This is where the ServerOnly box is drawn
2727

28-
if (!networkedObject.isSpawned && NetworkingManager.singleton != null && NetworkingManager.singleton.isServer)
28+
if (!networkedObject.IsSpawned && NetworkingManager.singleton != null && NetworkingManager.singleton.isServer)
2929
{
3030
EditorGUILayout.BeginHorizontal();
3131
EditorGUILayout.LabelField(new GUIContent("Spawn", "Spawns the object across the network"));
@@ -36,11 +36,11 @@ public override void OnInspectorGUI()
3636
}
3737
EditorGUILayout.EndHorizontal();
3838
}
39-
else if(networkedObject.isSpawned)
39+
else if(networkedObject.IsSpawned)
4040
{
41-
EditorGUILayout.LabelField("NetworkId: ", networkedObject.NetworkId.ToString(), EditorStyles.label);
41+
EditorGUILayout.LabelField("NetworkId: ", networkedObject.NetworKId.ToString(), EditorStyles.label);
4242
EditorGUILayout.LabelField("OwnerId: ", networkedObject.OwnerClientId.ToString(), EditorStyles.label);
43-
EditorGUILayout.LabelField("isSpawned: ", networkedObject.isSpawned.ToString(), EditorStyles.label);
43+
EditorGUILayout.LabelField("isSpawned: ", networkedObject.IsSpawned.ToString(), EditorStyles.label);
4444
EditorGUILayout.LabelField("isLocalPlayer: ", networkedObject.isLocalPlayer.ToString(), EditorStyles.label);
4545
EditorGUILayout.LabelField("isOwner: ", networkedObject.isOwner.ToString(), EditorStyles.label);
4646
EditorGUILayout.LabelField("isPoolObject: ", networkedObject.isPlayerObject.ToString(), EditorStyles.label);

MLAPI/Data/NetworkPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ internal NetworkPool(int prefabId, uint size, ushort poolIndex)
1616
for (int i = 0; i < size; i++)
1717
{
1818
GameObject go = MonoBehaviour.Instantiate(NetworkingManager.singleton.NetworkConfig.NetworkedPrefabs[prefabId].prefab, Vector3.zero, Quaternion.identity);
19-
go.GetComponent<NetworkedObject>()._isPooledObject = true;
20-
go.GetComponent<NetworkedObject>().poolId = poolId;
19+
go.GetComponent<NetworkedObject>().isPooledObject = true;
20+
go.GetComponent<NetworkedObject>().PoolId = poolId;
2121
go.GetComponent<NetworkedObject>().Spawn();
2222
go.name = "Pool Id: " + poolId + " #" + i;
2323
go.SetActive(false);

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 29 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,27 @@ public abstract class NetworkedBehaviour : MonoBehaviour
1818
/// <summary>
1919
/// Gets if the object is the the personal clients player object
2020
/// </summary>
21-
public bool isLocalPlayer
22-
{
23-
get
24-
{
25-
return networkedObject.isLocalPlayer;
26-
}
27-
}
21+
public bool isLocalPlayer => networkedObject.isLocalPlayer;
2822
/// <summary>
2923
/// Gets if the object is owned by the local player
3024
/// </summary>
31-
public bool isOwner
32-
{
33-
get
34-
{
35-
return networkedObject.isOwner;
36-
}
37-
}
25+
public bool isOwner => networkedObject.isOwner;
3826
/// <summary>
3927
/// Gets if we are executing as server
4028
/// </summary>
41-
protected bool isServer
42-
{
43-
get
44-
{
45-
return NetworkingManager.singleton.isServer;
46-
}
47-
}
29+
protected bool isServer => NetworkingManager.singleton.isServer;
4830
/// <summary>
4931
/// Gets if we are executing as client
5032
/// </summary>
51-
protected bool isClient
52-
{
53-
get
54-
{
55-
return NetworkingManager.singleton.isClient;
56-
}
57-
}
33+
protected bool isClient => NetworkingManager.singleton.isClient;
5834
/// <summary>
5935
/// Gets if we are executing as Host, I.E Server and Client
6036
/// </summary>
61-
protected bool isHost
62-
{
63-
get
64-
{
65-
return NetworkingManager.singleton.isHost;
66-
}
67-
}
68-
69-
protected bool isOwnedByNoone
70-
{
71-
get
72-
{
73-
return networkedObject.isOwnedByNoone;
74-
}
75-
}
37+
protected bool isHost => NetworkingManager.singleton.isHost;
38+
/// <summary>
39+
/// Gets wheter or not the object has a owner
40+
/// </summary>
41+
public bool hasOwner => networkedObject.hasOwner;
7642

7743
/// <summary>
7844
/// Gets the NetworkedObject that owns this NetworkedBehaviour instance
@@ -92,26 +58,13 @@ public NetworkedObject networkedObject
9258
/// <summary>
9359
/// Gets the NetworkId of the NetworkedObject that owns the NetworkedBehaviour instance
9460
/// </summary>
95-
public uint networkId
96-
{
97-
get
98-
{
99-
return networkedObject.NetworkId;
100-
}
101-
}
61+
public uint networkId => networkedObject.NetworKId;
10262
/// <summary>
10363
/// Gets the clientId that owns the NetworkedObject
10464
/// </summary>
105-
public uint ownerClientId
106-
{
107-
get
108-
{
109-
return networkedObject.OwnerClientId;
110-
}
111-
}
65+
public uint OwnerClientId => networkedObject.OwnerClientId;
11266

113-
//Change data type
114-
private Dictionary<string, int> registeredMessageHandlers = new Dictionary<string, int>();
67+
private readonly Dictionary<string, int> registeredMessageHandlers = new Dictionary<string, int>();
11568

11669
private void OnEnable()
11770
{
@@ -295,7 +248,7 @@ private void CacheAttributedMethods()
295248
/// <param name="methodParams">Method parameters to send</param>
296249
public void InvokeCommand(string methodName, params object[] methodParams)
297250
{
298-
if (ownerClientId != NetworkingManager.singleton.MyClientId && !isLocalPlayer)
251+
if (OwnerClientId != NetworkingManager.singleton.LocalClientId && !isLocalPlayer)
299252
{
300253
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Cannot invoke command for object without ownership");
301254
return;
@@ -427,7 +380,7 @@ public void InvokeTargetRpc(string methodName, params object[] methodParams)
427380
writer.WriteULong(hash);
428381
for (int i = 0; i < methodParams.Length; i++)
429382
FieldTypeHelper.WriteFieldType(writer, methodParams[i]);
430-
InternalMessageHandler.Send(ownerClientId, "MLAPI_RPC", messageChannelName[methodName], writer, networkId);
383+
InternalMessageHandler.Send(OwnerClientId, "MLAPI_RPC", messageChannelName[methodName], writer, networkId);
431384
}
432385
}
433386

@@ -754,7 +707,7 @@ internal void FlushSyncedVarsToClient(uint clientId)
754707
{
755708
if (!syncedVarFields[i].Target)
756709
syncCount++;
757-
else if (syncedVarFields[i].Target && ownerClientId == clientId)
710+
else if (syncedVarFields[i].Target && OwnerClientId == clientId)
758711
syncCount++;
759712
}
760713
if (syncCount == 0)
@@ -768,7 +721,7 @@ internal void FlushSyncedVarsToClient(uint clientId)
768721
for (int i = 0; i < syncedVarFields.Count; i++)
769722
{
770723
writer.WriteBool(mask[i]);
771-
if (syncedVarFields[i].Target && clientId != ownerClientId)
724+
if (syncedVarFields[i].Target && clientId != OwnerClientId)
772725
continue;
773726
FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo.GetValue(this));
774727
}
@@ -784,7 +737,7 @@ private ref bool[] GetDirtyMask(bool ignoreTarget, uint? clientId = null)
784737
syncMask[i] = (clientId == null && ignoreTarget && syncedVarFields[i].Dirty && !syncedVarFields[i].Target) ||
785738
(clientId == null && !ignoreTarget && syncedVarFields[i].Dirty) ||
786739
(clientId != null && !syncedVarFields[i].Target) ||
787-
(clientId != null && syncedVarFields[i].Target && ownerClientId == clientId.Value);
740+
(clientId != null && syncedVarFields[i].Target && OwnerClientId == clientId.Value);
788741
return ref syncMask;
789742
}
790743

@@ -848,7 +801,7 @@ internal void SyncVarUpdate()
848801
}
849802
else
850803
{
851-
if (!(isHost && ownerClientId == NetworkingManager.singleton.NetworkConfig.NetworkTransport.HostDummyId))
804+
if (!(isHost && OwnerClientId == NetworkingManager.singleton.NetworkConfig.NetworkTransport.HostDummyId))
852805
{
853806
//It's sync time. This is the target receivers packet.
854807
using (BitWriter writer = BitWriter.Get())
@@ -876,9 +829,9 @@ internal void SyncVarUpdate()
876829
}
877830
}
878831
}
879-
bool observing = !InternalMessageHandler.Send(ownerClientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer, networkId); //Send only to target
832+
bool observing = !InternalMessageHandler.Send(OwnerClientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer, networkId); //Send only to target
880833
if (!observing)
881-
OutOfSyncClients.Add(ownerClientId);
834+
OutOfSyncClients.Add(OwnerClientId);
882835
}
883836
}
884837

@@ -907,7 +860,7 @@ internal void SyncVarUpdate()
907860
InvokeSyncvarMethodOnServer(syncedVarFields[i].HookMethod);
908861
}
909862
}
910-
List<uint> stillDirtyIds = InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer, ownerClientId, networkId, null, null); // Send to everyone except target.
863+
List<uint> stillDirtyIds = InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer, OwnerClientId, networkId, null, null); // Send to everyone except target.
911864
if (stillDirtyIds != null)
912865
{
913866
for (int i = 0; i < stillDirtyIds.Count; i++)
@@ -1116,7 +1069,7 @@ protected void SendToLocalClient(string messageType, string channelName, byte[]
11161069
using (BitWriter writer = BitWriter.Get())
11171070
{
11181071
writer.WriteByteArray(data);
1119-
InternalMessageHandler.Send(ownerClientId, messageType, channelName, writer, fromNetId);
1072+
InternalMessageHandler.Send(OwnerClientId, messageType, channelName, writer, fromNetId);
11201073
}
11211074
}
11221075

@@ -1145,7 +1098,7 @@ protected void SendToLocalClient(string messageType, string channelName, BitWrit
11451098
return;
11461099
}
11471100
uint? fromNetId = respectObservers ? (uint?)networkId : null;
1148-
InternalMessageHandler.Send(ownerClientId, messageType, channelName, writer, fromNetId);
1101+
InternalMessageHandler.Send(OwnerClientId, messageType, channelName, writer, fromNetId);
11491102
}
11501103

11511104
/// <summary>
@@ -1187,7 +1140,7 @@ protected void SendToLocalClientTarget(string messageType, string channelName, b
11871140
using (BitWriter writer = BitWriter.Get())
11881141
{
11891142
writer.WriteByteArray(data);
1190-
InternalMessageHandler.Send(ownerClientId, messageType, channelName, writer, null, networkId, networkedObject.GetOrderIndex(this));
1143+
InternalMessageHandler.Send(OwnerClientId, messageType, channelName, writer, null, networkId, networkedObject.GetOrderIndex(this));
11911144
}
11921145
}
11931146

@@ -1214,7 +1167,7 @@ protected void SendToLocalClientTarget(string messageType, string channelName, B
12141167
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
12151168
return;
12161169
}
1217-
InternalMessageHandler.Send(ownerClientId, messageType, channelName, writer, null, networkId, networkedObject.GetOrderIndex(this));
1170+
InternalMessageHandler.Send(OwnerClientId, messageType, channelName, writer, null, networkId, networkedObject.GetOrderIndex(this));
12181171
}
12191172

12201173
/// <summary>gh
@@ -1257,7 +1210,7 @@ protected void SendToNonLocalClients(string messageType, string channelName, byt
12571210
using (BitWriter writer = BitWriter.Get())
12581211
{
12591212
writer.WriteByteArray(data);
1260-
InternalMessageHandler.Send(messageType, channelName, writer, ownerClientId, fromNetId, null, null);
1213+
InternalMessageHandler.Send(messageType, channelName, writer, OwnerClientId, fromNetId, null, null);
12611214
}
12621215
}
12631216

@@ -1286,7 +1239,7 @@ protected void SendToNonLocalClients(string messageType, string channelName, Bit
12861239
return;
12871240
}
12881241
uint? fromNetId = respectObservers ? (uint?)networkId : null;
1289-
InternalMessageHandler.Send(messageType, channelName, writer, ownerClientId, fromNetId, null, null);
1242+
InternalMessageHandler.Send(messageType, channelName, writer, OwnerClientId, fromNetId, null, null);
12901243
}
12911244

12921245
/// <summary>
@@ -1330,7 +1283,7 @@ protected void SendToNonLocalClientsTarget(string messageType, string channelNam
13301283
using (BitWriter writer = BitWriter.Get())
13311284
{
13321285
writer.WriteByteArray(data);
1333-
InternalMessageHandler.Send(messageType, channelName, writer, ownerClientId, fromNetId, networkId, networkedObject.GetOrderIndex(this));
1286+
InternalMessageHandler.Send(messageType, channelName, writer, OwnerClientId, fromNetId, networkId, networkedObject.GetOrderIndex(this));
13341287
}
13351288
}
13361289

@@ -1359,7 +1312,7 @@ protected void SendToNonLocalClientsTarget(string messageType, string channelNam
13591312
return;
13601313
}
13611314
uint? fromNetId = respectObservers ? (uint?)networkId : null;
1362-
InternalMessageHandler.Send(messageType, channelName, writer, ownerClientId, fromNetId, networkId, networkedObject.GetOrderIndex(this));
1315+
InternalMessageHandler.Send(messageType, channelName, writer, OwnerClientId, fromNetId, networkId, networkedObject.GetOrderIndex(this));
13631316
}
13641317

13651318
/// <summary>

0 commit comments

Comments
 (0)