Enforced get/set rules on all library properties

This commit is contained in:
Albin Corén 2018-03-31 09:23:11 +02:00
parent 7b42bbfd3a
commit 7079765b2c
5 changed files with 110 additions and 40 deletions

View File

@ -14,8 +14,8 @@ namespace MLAPI.Data
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
{ {
GameObject go = Object.Instantiate(NetworkingManager.singleton.SpawnablePrefabs[prefabIndex], Vector3.zero, Quaternion.identity); GameObject go = Object.Instantiate(NetworkingManager.singleton.SpawnablePrefabs[prefabIndex], Vector3.zero, Quaternion.identity);
go.GetComponent<NetworkedObject>().isPooledObject = true; go.GetComponent<NetworkedObject>()._isPooledObject = true;
go.GetComponent<NetworkedObject>().PoolId = poolId; go.GetComponent<NetworkedObject>().poolId = poolId;
go.GetComponent<NetworkedObject>().Spawn(); go.GetComponent<NetworkedObject>().Spawn();
go.name = "Pool Id: " + poolId + " #" + i; go.name = "Pool Id: " + poolId + " #" + i;
go.SetActive(false); go.SetActive(false);

View File

@ -67,7 +67,7 @@ namespace MLAPI
} }
} }
/// <summary> /// <summary>
/// The NetworkedObject that owns this NetworkedBehaviour instance /// Gets the NetworkedObject that owns this NetworkedBehaviour instance
/// </summary> /// </summary>
public NetworkedObject networkedObject public NetworkedObject networkedObject
{ {
@ -82,7 +82,7 @@ namespace MLAPI
} }
private NetworkedObject _networkedObject = null; private NetworkedObject _networkedObject = null;
/// <summary> /// <summary>
/// The NetworkId of the NetworkedObject that owns the NetworkedBehaviour instance /// Gets the NetworkId of the NetworkedObject that owns the NetworkedBehaviour instance
/// </summary> /// </summary>
public uint networkId public uint networkId
{ {
@ -92,7 +92,7 @@ namespace MLAPI
} }
} }
/// <summary> /// <summary>
/// The clientId that owns the NetworkedObject /// Gets the clientId that owns the NetworkedObject
/// </summary> /// </summary>
public int ownerClientId public int ownerClientId
{ {

View File

@ -9,39 +9,81 @@ namespace MLAPI
public class NetworkedObject : MonoBehaviour public class NetworkedObject : MonoBehaviour
{ {
/// <summary> /// <summary>
/// The unique ID of this object that is synced across the network /// Gets the unique ID of this object that is synced across the network
/// </summary> /// </summary>
[HideInInspector] [HideInInspector]
public uint NetworkId; public uint NetworkId
{
get
{
return networkId;
}
}
internal uint networkId;
/// <summary> /// <summary>
/// The clientId of the owner of this NetworkedObject /// Gets the clientId of the owner of this NetworkedObject
/// </summary> /// </summary>
[HideInInspector] [HideInInspector]
public int OwnerClientId = -2; public int OwnerClientId
{
get
{
return ownerClientId;
}
}
internal int ownerClientId = -2;
/// <summary> /// <summary>
/// The index of the prefab used to spawn this in the spawnablePrefabs list /// The index of the prefab used to spawn this in the spawnablePrefabs list
/// </summary> /// </summary>
[HideInInspector] [HideInInspector]
public int SpawnablePrefabIndex; public int SpawnablePrefabIndex
{
get
{
return spawnablePrefabIndex;
}
}
internal int spawnablePrefabIndex;
/// <summary> /// <summary>
/// Gets if this object is a player object /// Gets if this object is a player object
/// </summary> /// </summary>
[HideInInspector] [HideInInspector]
public bool isPlayerObject = false; public bool isPlayerObject
{
get
{
return _isPlayerObject;
}
}
internal bool _isPlayerObject = false;
/// <summary> /// <summary>
/// Gets or sets if this object should be replicated across the network /// Gets or sets if this object should be replicated across the network. Can only be changed before the object is spawned
/// </summary> /// </summary>
public bool ServerOnly = false; public bool ServerOnly = false;
/// <summary> /// <summary>
/// Gets if this object is part of a pool /// Gets if this object is part of a pool
/// </summary> /// </summary>
[HideInInspector] [HideInInspector]
public bool isPooledObject = false; public bool isPooledObject
{
get
{
return _isPooledObject;
}
}
internal bool _isPooledObject = false;
/// <summary> /// <summary>
/// Gets the poolId this object is part of /// Gets the poolId this object is part of
/// </summary> /// </summary>
[HideInInspector] [HideInInspector]
public ushort PoolId; public ushort PoolId
{
get
{
return poolId;
}
}
internal ushort poolId;
/// <summary> /// <summary>
/// Gets if the object is the the personal clients player object /// Gets if the object is the the personal clients player object
/// </summary> /// </summary>

View File

@ -17,7 +17,14 @@ namespace MLAPI
/// <summary> /// <summary>
/// A syncronized time, represents the time in seconds since the server application started. Is replicated across all clients /// A syncronized time, represents the time in seconds since the server application started. Is replicated across all clients
/// </summary> /// </summary>
public static float NetworkTime; public float NetworkTime
{
get
{
return networkTime;
}
}
internal float networkTime;
/// <summary> /// <summary>
/// Gets or sets if the NetworkingManager should be marked as DontDestroyOnLoad /// Gets or sets if the NetworkingManager should be marked as DontDestroyOnLoad
/// </summary> /// </summary>
@ -37,12 +44,26 @@ namespace MLAPI
/// <summary> /// <summary>
/// The singleton instance of the NetworkingManager /// The singleton instance of the NetworkingManager
/// </summary> /// </summary>
public static NetworkingManager singleton; public static NetworkingManager singleton
{
get
{
return _singleton;
}
}
private static NetworkingManager _singleton;
/// <summary> /// <summary>
/// The clientId the server calls the local client by, only valid for clients /// The clientId the server calls the local client by, only valid for clients
/// </summary> /// </summary>
[HideInInspector] [HideInInspector]
public int MyClientId; public int MyClientId
{
get
{
return myClientId;
}
}
internal int myClientId;
internal Dictionary<int, NetworkedClient> connectedClients; internal Dictionary<int, NetworkedClient> connectedClients;
/// <summary> /// <summary>
/// Gets a dictionary of connected clients /// Gets a dictionary of connected clients
@ -74,7 +95,14 @@ namespace MLAPI
/// Gets if we are connected as a client /// Gets if we are connected as a client
/// </summary> /// </summary>
[HideInInspector] [HideInInspector]
public bool IsClientConnected; public bool IsClientConnected
{
get
{
return _isClientConnected;
}
}
internal bool _isClientConnected;
/// <summary> /// <summary>
/// The callback to invoke once a client connects /// The callback to invoke once a client connects
/// </summary> /// </summary>
@ -110,7 +138,7 @@ namespace MLAPI
Debug.LogWarning("MLAPI: All SpawnablePrefabs need a NetworkedObject component. Please add one to the prefab " + SpawnablePrefabs[i].gameObject.name); Debug.LogWarning("MLAPI: All SpawnablePrefabs need a NetworkedObject component. Please add one to the prefab " + SpawnablePrefabs[i].gameObject.name);
continue; continue;
} }
netObject.SpawnablePrefabIndex = i; netObject.spawnablePrefabIndex = i;
} }
} }
if (DefaultPlayerPrefab != null) if (DefaultPlayerPrefab != null)
@ -126,7 +154,7 @@ namespace MLAPI
private ConnectionConfig Init(NetworkingConfiguration netConfig) private ConnectionConfig Init(NetworkingConfiguration netConfig)
{ {
NetworkConfig = netConfig; NetworkConfig = netConfig;
NetworkTime = 0f; networkTime = 0f;
lastSendTickTime = 0; lastSendTickTime = 0;
lastEventTickTime = 0; lastEventTickTime = 0;
lastReceiveTickTime = 0; lastReceiveTickTime = 0;
@ -360,7 +388,7 @@ namespace MLAPI
Destroy(this); Destroy(this);
return; return;
} }
singleton = this; _singleton = this;
if (DontDestroy) if (DontDestroy)
DontDestroyOnLoad(gameObject); DontDestroyOnLoad(gameObject);
if (RunInBackground) if (RunInBackground)
@ -369,7 +397,7 @@ namespace MLAPI
private void OnDestroy() private void OnDestroy()
{ {
singleton = null; _singleton = null;
Shutdown(); Shutdown();
} }
@ -420,7 +448,7 @@ namespace MLAPI
return; return;
} }
else else
IsClientConnected = false; _isClientConnected = false;
if (OnClientDisconnectCallback != null) if (OnClientDisconnectCallback != null)
OnClientDisconnectCallback.Invoke(clientId); OnClientDisconnectCallback.Invoke(clientId);
@ -481,7 +509,7 @@ namespace MLAPI
if (isServer) if (isServer)
OnClientDisconnect(clientId); OnClientDisconnect(clientId);
else else
IsClientConnected = false; _isClientConnected = false;
if (OnClientDisconnectCallback != null) if (OnClientDisconnectCallback != null)
OnClientDisconnectCallback.Invoke(clientId); OnClientDisconnectCallback.Invoke(clientId);
@ -497,7 +525,7 @@ namespace MLAPI
NetworkedObject.InvokeSyncvarUpdate(); NetworkedObject.InvokeSyncvarUpdate();
lastEventTickTime = Time.time; lastEventTickTime = Time.time;
} }
NetworkTime += Time.deltaTime; networkTime += Time.deltaTime;
} }
} }
@ -673,7 +701,7 @@ namespace MLAPI
{ {
using (BinaryReader messageReader = new BinaryReader(messageReadStream)) using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{ {
MyClientId = messageReader.ReadInt32(); myClientId = messageReader.ReadInt32();
uint sceneIndex = 0; uint sceneIndex = 0;
if(NetworkConfig.EnableSceneSwitching) if(NetworkConfig.EnableSceneSwitching)
{ {
@ -709,7 +737,7 @@ namespace MLAPI
int msDelay = NetworkTransport.GetRemoteDelayTimeMS(hostId, clientId, remoteStamp, out error); int msDelay = NetworkTransport.GetRemoteDelayTimeMS(hostId, clientId, remoteStamp, out error);
if ((NetworkError)error != NetworkError.Ok) if ((NetworkError)error != NetworkError.Ok)
msDelay = 0; msDelay = 0;
NetworkTime = netTime + (msDelay / 1000f); networkTime = netTime + (msDelay / 1000f);
connectedClients.Add(MyClientId, new NetworkedClient() { ClientId = MyClientId }); connectedClients.Add(MyClientId, new NetworkedClient() { ClientId = MyClientId });
int clientCount = messageReader.ReadInt32(); int clientCount = messageReader.ReadInt32();
@ -745,7 +773,7 @@ namespace MLAPI
} }
} }
} }
IsClientConnected = true; _isClientConnected = true;
if (OnClientConnectedCallback != null) if (OnClientConnectedCallback != null)
OnClientConnectedCallback.Invoke(clientId); OnClientConnectedCallback.Invoke(clientId);
} }
@ -879,7 +907,7 @@ namespace MLAPI
//We are new owner. //We are new owner.
SpawnManager.spawnedObjects[netId].InvokeBehaviourOnGainedOwnership(); SpawnManager.spawnedObjects[netId].InvokeBehaviourOnGainedOwnership();
} }
SpawnManager.spawnedObjects[netId].OwnerClientId = ownerClientId; SpawnManager.spawnedObjects[netId].ownerClientId = ownerClientId;
} }
} }
} }

View File

@ -34,7 +34,7 @@ namespace MLAPI.NetworkingManagerComponents
{ {
NetworkedObject netObject = SpawnManager.spawnedObjects[netId]; NetworkedObject netObject = SpawnManager.spawnedObjects[netId];
NetworkingManager.singleton.connectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == netId); NetworkingManager.singleton.connectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == netId);
netObject.OwnerClientId = -2; netObject.ownerClientId = -2;
using (MemoryStream stream = new MemoryStream(8)) using (MemoryStream stream = new MemoryStream(8))
{ {
using (BinaryWriter writer = new BinaryWriter(stream)) using (BinaryWriter writer = new BinaryWriter(stream))
@ -51,7 +51,7 @@ namespace MLAPI.NetworkingManagerComponents
NetworkedObject netObject = SpawnManager.spawnedObjects[netId]; NetworkedObject netObject = SpawnManager.spawnedObjects[netId];
NetworkingManager.singleton.connectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == netId); NetworkingManager.singleton.connectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == netId);
NetworkingManager.singleton.connectedClients[clientId].OwnedObjects.Add(netObject); NetworkingManager.singleton.connectedClients[clientId].OwnedObjects.Add(netObject);
netObject.OwnerClientId = clientId; netObject.ownerClientId = clientId;
using (MemoryStream stream = new MemoryStream(8)) using (MemoryStream stream = new MemoryStream(8))
{ {
using (BinaryWriter writer = new BinaryWriter(stream)) using (BinaryWriter writer = new BinaryWriter(stream))
@ -72,16 +72,16 @@ namespace MLAPI.NetworkingManagerComponents
Debug.LogWarning("MLAPI: Please add a NetworkedObject component to the root of all spawnable objects"); Debug.LogWarning("MLAPI: Please add a NetworkedObject component to the root of all spawnable objects");
netObject = go.AddComponent<NetworkedObject>(); netObject = go.AddComponent<NetworkedObject>();
} }
netObject.SpawnablePrefabIndex = spawnablePrefabIndex; netObject.spawnablePrefabIndex = spawnablePrefabIndex;
if (netManager.isServer) if (netManager.isServer)
{ {
netObject.NetworkId = GetNetworkObjectId(); netObject.networkId = GetNetworkObjectId();
} }
else else
{ {
netObject.NetworkId = networkId; netObject.networkId = networkId;
} }
netObject.OwnerClientId = ownerId; netObject.ownerClientId = ownerId;
spawnedObjects.Add(netObject.NetworkId, netObject); spawnedObjects.Add(netObject.NetworkId, netObject);
netObject.InvokeBehaviourNetworkSpawn(); netObject.InvokeBehaviourNetworkSpawn();
@ -97,16 +97,16 @@ namespace MLAPI.NetworkingManagerComponents
Debug.LogWarning("MLAPI: Please add a NetworkedObject component to the root of the player prefab"); Debug.LogWarning("MLAPI: Please add a NetworkedObject component to the root of the player prefab");
netObject = go.AddComponent<NetworkedObject>(); netObject = go.AddComponent<NetworkedObject>();
} }
netObject.OwnerClientId = clientId; netObject.ownerClientId = clientId;
if (NetworkingManager.singleton.isServer) if (NetworkingManager.singleton.isServer)
{ {
netObject.NetworkId = GetNetworkObjectId(); netObject.networkId = GetNetworkObjectId();
} }
else else
{ {
netObject.NetworkId = networkId; netObject.networkId = networkId;
} }
netObject.isPlayerObject = true; netObject._isPlayerObject = true;
netManager.connectedClients[clientId].PlayerObject = go; netManager.connectedClients[clientId].PlayerObject = go;
spawnedObjects.Add(netObject.NetworkId, netObject); spawnedObjects.Add(netObject.NetworkId, netObject);
netObject.InvokeBehaviourNetworkSpawn(); netObject.InvokeBehaviourNetworkSpawn();
@ -179,7 +179,7 @@ namespace MLAPI.NetworkingManagerComponents
netObject.isSpawned = true; netObject.isSpawned = true;
if (clientOwnerId != null) if (clientOwnerId != null)
{ {
netObject.OwnerClientId = clientOwnerId.Value; netObject.ownerClientId = clientOwnerId.Value;
NetworkingManager.singleton.connectedClients[clientOwnerId.Value].OwnedObjects.Add(netObject); NetworkingManager.singleton.connectedClients[clientOwnerId.Value].OwnedObjects.Add(netObject);
} }
using (MemoryStream stream = new MemoryStream(13)) using (MemoryStream stream = new MemoryStream(13))