Skip to content

Commit 166cda8

Browse files
committed
feat: Added exceptions to the public API
1 parent 2080bca commit 166cda8

File tree

6 files changed

+142
-39
lines changed

6 files changed

+142
-39
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
3+
namespace MLAPI.Exceptions
4+
{
5+
/// <summary>
6+
/// Exception thrown when the operation can only be done on the server
7+
/// </summary>
8+
public class NotServerException : Exception
9+
{
10+
/// <summary>
11+
/// Constructs a ObjectNotSpawnedException
12+
/// </summary>
13+
public NotServerException()
14+
{
15+
16+
}
17+
18+
/// <summary>
19+
/// Constructs a NotServerException with a message
20+
/// </summary>
21+
/// <param name="message">The exception message</param>
22+
public NotServerException(string message) : base(message)
23+
{
24+
25+
}
26+
27+
/// <summary>
28+
/// Constructs a NotServerException with a message and a inner exception
29+
/// </summary>
30+
/// <param name="message">The exception message</param>
31+
/// <param name="inner">The inner exception</param>
32+
public NotServerException(string message, Exception inner) : base(message, inner)
33+
{
34+
35+
}
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
3+
namespace MLAPI.Exceptions
4+
{
5+
/// <summary>
6+
/// Exception thrown when an object is not yet spawned
7+
/// </summary>
8+
public class SpawnStateException : Exception
9+
{
10+
/// <summary>
11+
/// Constructs a SpawnStateException
12+
/// </summary>
13+
public SpawnStateException()
14+
{
15+
16+
}
17+
18+
/// <summary>
19+
/// Constructs a SpawnStateException with a message
20+
/// </summary>
21+
/// <param name="message">The exception message</param>
22+
public SpawnStateException(string message) : base(message)
23+
{
24+
25+
}
26+
27+
/// <summary>
28+
/// Constructs a SpawnStateException with a message and a inner exception
29+
/// </summary>
30+
/// <param name="message">The exception message</param>
31+
/// <param name="inner">The inner exception</param>
32+
public SpawnStateException(string message, Exception inner) : base(message, inner)
33+
{
34+
35+
}
36+
}
37+
}

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using MLAPI.Components;
55
using MLAPI.Data;
6+
using MLAPI.Exceptions;
67
using MLAPI.Internal;
78
using MLAPI.Logging;
89
using MLAPI.Serialization;
@@ -157,6 +158,11 @@ internal set
157158
/// <returns>Observers enumerator</returns>
158159
public HashSet<ulong>.Enumerator GetObservers()
159160
{
161+
if (!IsSpawned)
162+
{
163+
throw new SpawnStateException("Object is not spawned");
164+
}
165+
160166
return observers.GetEnumerator();
161167
}
162168

@@ -167,6 +173,11 @@ public HashSet<ulong>.Enumerator GetObservers()
167173
/// <returns>True if the client knows about the object</returns>
168174
public bool IsNetworkVisibleTo(ulong clientId)
169175
{
176+
if (!IsSpawned)
177+
{
178+
throw new SpawnStateException("Object is not spawned");
179+
}
180+
170181
return observers.Contains(clientId);
171182
}
172183

@@ -177,10 +188,14 @@ public bool IsNetworkVisibleTo(ulong clientId)
177188
/// <param name="payload">An optional payload to send as part of the spawn</param>
178189
public void NetworkShow(ulong clientId, Stream payload = null)
179190
{
191+
if (!IsSpawned)
192+
{
193+
throw new SpawnStateException("Object is not spawned");
194+
}
195+
180196
if (!NetworkingManager.Singleton.IsServer)
181197
{
182-
if (LogHelper.CurrentLogLevel <= LogLevel.Error) LogHelper.LogError("Can only call NetworkShow on the server");
183-
return;
198+
throw new NotServerException("Only server can change visibility");
184199
}
185200

186201
if (!observers.Contains(clientId))
@@ -198,10 +213,14 @@ public void NetworkShow(ulong clientId, Stream payload = null)
198213
/// <param name="clientId">The client to hide the object for</param>
199214
public void NetworkHide(ulong clientId)
200215
{
216+
if (!IsSpawned)
217+
{
218+
throw new SpawnStateException("Object is not spawned");
219+
}
220+
201221
if (!NetworkingManager.Singleton.IsServer)
202222
{
203-
if (LogHelper.CurrentLogLevel <= LogLevel.Error) LogHelper.LogError("Can only call NetworkHide on the server");
204-
return;
223+
throw new NotServerException("Only server can change visibility");
205224
}
206225

207226
if (observers.Contains(clientId) && clientId != NetworkingManager.Singleton.ServerClientId)
@@ -252,7 +271,7 @@ public void Spawn(Stream spawnPayload = null, bool destroyWithScene = false)
252271
/// Unspawns this GameObject and destroys it for other clients. This should be used if the object should be kept on the server
253272
/// </summary>
254273
public void UnSpawn()
255-
{
274+
{
256275
SpawnManager.UnSpawnObject(this);
257276
}
258277

@@ -305,15 +324,15 @@ public void SpawnAsPlayerObject(ulong clientId, Stream spawnPayload = null, bool
305324
/// </summary>
306325
public void RemoveOwnership()
307326
{
308-
SpawnManager.RemoveOwnership(NetworkId);
327+
SpawnManager.RemoveOwnership(this);
309328
}
310329
/// <summary>
311330
/// Changes the owner of the object. Can only be called from server
312331
/// </summary>
313332
/// <param name="newOwnerClientId">The new owner clientId</param>
314333
public void ChangeOwnership(ulong newOwnerClientId)
315334
{
316-
SpawnManager.ChangeOwnership(NetworkId, newOwnerClientId);
335+
SpawnManager.ChangeOwnership(this, newOwnerClientId);
317336
}
318337

319338
internal void InvokeBehaviourOnLostOwnership()

MLAPI/NetworkingManagerComponents/Core/LagCompensationManager.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using MLAPI.Exceptions;
34
using MLAPI.Logging;
45

56
namespace MLAPI.Components
@@ -21,11 +22,11 @@ public static class LagCompensationManager
2122
/// <param name="action">The action to invoke when time is turned back</param>
2223
public static void Simulate(float secondsAgo, Action action)
2324
{
24-
if(!NetworkingManager.Singleton.IsServer)
25+
if (!NetworkingManager.Singleton.IsServer)
2526
{
26-
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Lag compensation simulations are only to be ran on the server");
27-
return;
27+
throw new NotServerException("Only the server can perform lag compensation");
2828
}
29+
2930
for (int i = 0; i < simulationObjects.Count; i++)
3031
{
3132
simulationObjects[i].ReverseTransform(secondsAgo);
@@ -48,9 +49,9 @@ public static void Simulate(ulong clientId, Action action)
4849
{
4950
if (!NetworkingManager.Singleton.IsServer)
5051
{
51-
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Lag compensation simulations are only to be ran on the server");
52-
return;
52+
throw new NotServerException("Only the server can perform lag compensation");
5353
}
54+
5455
float millisecondsDelay = NetworkingManager.Singleton.NetworkConfig.NetworkTransport.GetCurrentRtt(clientId) / 2f;
5556
Simulate(millisecondsDelay * 1000f, action);
5657
}

MLAPI/NetworkingManagerComponents/Core/NetworkSceneManager.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.IO;
44
using MLAPI.Data;
5+
using MLAPI.Exceptions;
56
using MLAPI.Internal;
67
using MLAPI.Logging;
78
using MLAPI.Serialization;
@@ -53,7 +54,11 @@ internal static void SetCurrentSceneIndex()
5354
/// <param name="sceneName">The name of the scene to switch to</param>
5455
public static SceneSwitchProgress SwitchScene(string sceneName)
5556
{
56-
if (isSwitching)
57+
if (!NetworkingManager.Singleton.IsServer)
58+
{
59+
throw new NotServerException("Only server can start a scene switch");
60+
}
61+
else if (isSwitching)
5762
{
5863
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Scene switch already in progress");
5964
return null;

MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
3-
using JetBrains.Annotations;
4-
using MLAPI.Configuration;
54
using MLAPI.Data;
5+
using MLAPI.Exceptions;
66
using MLAPI.Internal;
77
using MLAPI.Logging;
8-
using MLAPI.NetworkedVar;
98
using MLAPI.Serialization;
109
using UnityEngine;
11-
using UnityEngine.SceneManagement;
1210

1311
namespace MLAPI.Components
1412
{
@@ -165,55 +163,64 @@ public static NetworkedObject GetPlayerObject(ulong clientId)
165163
return NetworkingManager.Singleton.ConnectedClients[clientId].PlayerObject;
166164
}
167165

168-
internal static void RemoveOwnership(ulong networkId)
166+
internal static void RemoveOwnership(NetworkedObject netObject)
169167
{
170168
if (!NetworkingManager.Singleton.IsServer)
171169
{
172-
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("You can only remove ownership from Server");
173-
return;
170+
throw new NotServerException("Only the server can change ownership");
171+
}
172+
173+
if (!netObject.IsSpawned)
174+
{
175+
throw new SpawnStateException("Object is not spawned");
174176
}
175177

176-
NetworkedObject netObject = SpawnManager.SpawnedObjects[networkId];
177178
for (int i = NetworkingManager.Singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects.Count - 1; i > -1; i--)
178179
{
179-
if (NetworkingManager.Singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects[i].NetworkId == networkId)
180+
if (NetworkingManager.Singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects[i] == netObject)
180181
NetworkingManager.Singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAt(i);
181182
}
183+
182184
netObject._ownerClientId = null;
183185

184186
using (PooledBitStream stream = PooledBitStream.Get())
185187
{
186188
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
187189
{
188-
writer.WriteUInt64Packed(networkId);
190+
writer.WriteUInt64Packed(netObject.NetworkId);
189191
writer.WriteUInt64Packed(netObject.OwnerClientId);
190192

191193
InternalMessageHandler.Send(MLAPIConstants.MLAPI_CHANGE_OWNER, "MLAPI_INTERNAL", stream, SecuritySendFlags.None, netObject);
192194
}
193195
}
194196
}
195197

196-
internal static void ChangeOwnership(ulong networkId, ulong clientId)
198+
internal static void ChangeOwnership(NetworkedObject netObject, ulong clientId)
197199
{
198200
if (!NetworkingManager.Singleton.IsServer)
199201
{
200-
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("You can only change ownership from Server");
201-
return;
202+
throw new NotServerException("Only the server can change ownership");
202203
}
203-
NetworkedObject netObject = SpawnManager.SpawnedObjects[networkId];
204+
205+
if (!netObject.IsSpawned)
206+
{
207+
throw new SpawnStateException("Object is not spawned");
208+
}
209+
204210
for (int i = NetworkingManager.Singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects.Count - 1; i > -1; i--)
205211
{
206-
if (NetworkingManager.Singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects[i].NetworkId == networkId)
212+
if (NetworkingManager.Singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects[i] == netObject)
207213
NetworkingManager.Singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAt(i);
208214
}
215+
209216
NetworkingManager.Singleton.ConnectedClients[clientId].OwnedObjects.Add(netObject);
210217
netObject.OwnerClientId = clientId;
211218

212219
using (PooledBitStream stream = PooledBitStream.Get())
213220
{
214221
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
215222
{
216-
writer.WriteUInt64Packed(networkId);
223+
writer.WriteUInt64Packed(netObject.NetworkId);
217224
writer.WriteUInt64Packed(clientId);
218225

219226
InternalMessageHandler.Send(MLAPIConstants.MLAPI_CHANGE_OWNER, "MLAPI_INTERNAL", stream, SecuritySendFlags.None, netObject);
@@ -259,14 +266,12 @@ internal static void SpawnNetworkedObjectLocally(NetworkedObject netObject, ulon
259266
{
260267
if (netObject == null)
261268
{
262-
if (LogHelper.CurrentLogLevel <= LogLevel.Error) LogHelper.LogError("Cannot spawn null object");
263-
return;
269+
throw new ArgumentNullException(nameof(netObject), "Cannot spawn null object");
264270
}
265271

266272
if (netObject.IsSpawned)
267273
{
268-
if (LogHelper.CurrentLogLevel <= LogLevel.Error) LogHelper.LogError("Cannot spawn already spawned object");
269-
return;
274+
throw new SpawnStateException("Object is already spawned");
270275
}
271276

272277

@@ -393,13 +398,12 @@ internal static void UnSpawnObject(NetworkedObject netObject)
393398
{
394399
if (!netObject.IsSpawned)
395400
{
396-
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Cannot unspawn objects that are not spawned");
397-
return;
401+
throw new SpawnStateException("Object is not spawned");
398402
}
399-
else if (!NetworkingManager.Singleton.IsServer)
403+
404+
if (!NetworkingManager.Singleton.IsServer)
400405
{
401-
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Only server can unspawn objects");
402-
return;
406+
throw new NotServerException("Only server unspawn objects");
403407
}
404408

405409
OnDestroyObject(netObject.NetworkId, false);

0 commit comments

Comments
 (0)