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++)
{
GameObject go = Object.Instantiate(NetworkingManager.singleton.SpawnablePrefabs[prefabIndex], Vector3.zero, Quaternion.identity);
go.GetComponent<NetworkedObject>().isPooledObject = true;
go.GetComponent<NetworkedObject>().PoolId = poolId;
go.GetComponent<NetworkedObject>()._isPooledObject = true;
go.GetComponent<NetworkedObject>().poolId = poolId;
go.GetComponent<NetworkedObject>().Spawn();
go.name = "Pool Id: " + poolId + " #" + i;
go.SetActive(false);

View File

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

View File

@ -9,39 +9,81 @@ namespace MLAPI
public class NetworkedObject : MonoBehaviour
{
/// <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>
[HideInInspector]
public uint NetworkId;
public uint NetworkId
{
get
{
return networkId;
}
}
internal uint networkId;
/// <summary>
/// The clientId of the owner of this NetworkedObject
/// Gets the clientId of the owner of this NetworkedObject
/// </summary>
[HideInInspector]
public int OwnerClientId = -2;
public int OwnerClientId
{
get
{
return ownerClientId;
}
}
internal int ownerClientId = -2;
/// <summary>
/// The index of the prefab used to spawn this in the spawnablePrefabs list
/// </summary>
[HideInInspector]
public int SpawnablePrefabIndex;
public int SpawnablePrefabIndex
{
get
{
return spawnablePrefabIndex;
}
}
internal int spawnablePrefabIndex;
/// <summary>
/// Gets if this object is a player object
/// </summary>
[HideInInspector]
public bool isPlayerObject = false;
public bool isPlayerObject
{
get
{
return _isPlayerObject;
}
}
internal bool _isPlayerObject = false;
/// <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>
public bool ServerOnly = false;
/// <summary>
/// Gets if this object is part of a pool
/// </summary>
[HideInInspector]
public bool isPooledObject = false;
public bool isPooledObject
{
get
{
return _isPooledObject;
}
}
internal bool _isPooledObject = false;
/// <summary>
/// Gets the poolId this object is part of
/// </summary>
[HideInInspector]
public ushort PoolId;
public ushort PoolId
{
get
{
return poolId;
}
}
internal ushort poolId;
/// <summary>
/// Gets if the object is the the personal clients player object
/// </summary>

View File

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