Skip to content

Commit 2556cf3

Browse files
committed
Add Steam transport and networking changes
1 parent 8a8d6d2 commit 2556cf3

45 files changed

Lines changed: 1747 additions & 1211 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Basis/Packages/com.basis.bundlemanagement/BasisTrackedBundleWrapper.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class BasisTrackedBundleWrapper
1010
[SerializeField]
1111
public AssetBundle AssetBundle;
1212
private int _requestedTimes = 0;
13+
private int _unloadState = 0;
1314
public bool DidErrorOccur = false;
1415
public static TimeSpan TimeSpan = TimeSpan.FromSeconds(BasisBeeConstants.TimeUntilMemoryRemoval);
1516
/// <summary>
@@ -45,25 +46,38 @@ public async Task<bool> UnloadIfReady()
4546
{
4647
if (AssetBundle == null)
4748
{
49+
if (Volatile.Read(ref _unloadState) != 0)
50+
{
51+
return false;
52+
}
53+
4854
BasisDebug.LogError("Asset Bundle was null this should never occur");
4955
return false;
5056
}
5157
if (Volatile.Read(ref _requestedTimes) <= 0)
5258
{
59+
if (Interlocked.CompareExchange(ref _unloadState, 1, 0) != 0)
60+
{
61+
return false;
62+
}
63+
5364
await Task.Delay(TimeSpan);
5465
if (Volatile.Read(ref _requestedTimes) <= 0)
5566
{
5667
if (AssetBundle == null)
5768
{
58-
BasisDebug.LogError("Already Unloaded this bundle, check logic could be ok if you loaded this a few times and unloaded it quickly aswell.");
69+
Interlocked.Exchange(ref _unloadState, 2);
5970
return false;
6071
}
6172
BasisDebug.Log("Unloading Bundle " + AssetBundle.name);
6273
AssetBundle.Unload(true);
74+
AssetBundle = null;
75+
Interlocked.Exchange(ref _unloadState, 2);
6376
return true;
6477
}
6578
else
6679
{
80+
Interlocked.Exchange(ref _unloadState, 0);
6781
BasisDebug.Log("Stopping Unload Process, Bundle was Incremented again after Requested Time");
6882
return false;
6983
}

Basis/Packages/com.basis.framework/Drivers/Common/BasisObjectSyncDriver.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public static void OnDestroy()
5353

5454
public static void TransmitOwnedPickups(double currentTime)
5555
{
56+
if (!Basis.Scripts.Networking.BasisNetworkManagement.NetworkRunning
57+
|| !Basis.Scripts.Networking.BasisNetworkConnection.LocalPlayerIsConnected
58+
|| Basis.Scripts.Networking.BasisNetworkConnection.LocalPlayerPeer == null)
59+
{
60+
return;
61+
}
62+
5663
if (currentTime - _lastUpdateTime < TargetMilliseconds) return;
5764

5865
_lastUpdateTime = currentTime;
@@ -219,6 +226,14 @@ public static void RemoveRemoteOwner(BasisObjectSyncNetworking obj)
219226
{
220227
if (obj != null) RemoteOwnedObjectSyncs.Remove(obj);
221228
}
229+
230+
public static void Reset()
231+
{
232+
_remoteJobHandle.Complete();
233+
OwnedObjectSyncs.Clear();
234+
RemoteOwnedObjectSyncs.Clear();
235+
_lastUpdateTime = 0;
236+
}
222237
#endregion
223238
}
224239

Basis/Packages/com.basis.framework/Networking/BasisNetworkBehaviour/BasisNetworkBehaviour.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ private void LowLevelOwnershipTransfer(string uniqueEntityID, ushort NetIdNewOwn
155155
/// <param name="Recipients">if null everyone but self, you can include yourself to make it loop back over the network</param>
156156
public void SendCustomNetworkEvent(byte[] buffer = null, DeliveryMethod DeliveryMethod = DeliveryMethod.Unreliable, ushort[] Recipients = null)
157157
{
158+
if (!BasisNetworkManagement.NetworkRunning
159+
|| !BasisNetworkConnection.LocalPlayerIsConnected
160+
|| BasisNetworkConnection.LocalPlayerPeer == null)
161+
{
162+
return;
163+
}
164+
158165
if (HasNetworkID)
159166
{
160167
// BasisDebug.Log("Sening Out Custom Network Event");

Basis/Packages/com.basis.framework/Networking/BasisNetworkConnection.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,38 @@ namespace Basis.Scripts.Networking
2020
/// </summary>
2121
public static class BasisNetworkConnection
2222
{
23+
private static readonly object BnlSubscriptionLock = new object();
2324
public static NetPeer LocalPlayerPeer { get; set; }
2425
public static NetworkClient NetworkClient { get; set; } = new NetworkClient();
2526
public static bool LocalPlayerIsConnected { get; set; }
2627
public static bool SuppressNextDisconnectUi { get; set; }
2728
public static event Action<NetPeer> OnConnectedToServer;
2829
public static event Action<NetPeer, DisconnectInfo> OnDisconnectedFromServer;
2930
public static BasisNetworkServerRunner BasisNetworkServerRunner = null;
31+
private static bool HasRegisteredBnlLogging;
3032
private static void LogErrorOutput(string msg) => BasisDebug.LogError(msg, BasisDebug.LogTag.Networking);
3133
private static void LogWarningOutput(string msg) => BasisDebug.LogWarning(msg);
3234
private static void LogOutput(string msg) => BasisDebug.Log(msg, BasisDebug.LogTag.Networking);
35+
private static void EnsureBnlLoggingRegistered()
36+
{
37+
if (HasRegisteredBnlLogging)
38+
{
39+
return;
40+
}
41+
42+
lock (BnlSubscriptionLock)
43+
{
44+
if (HasRegisteredBnlLogging)
45+
{
46+
return;
47+
}
48+
49+
BNL.LogOutput += LogOutput;
50+
BNL.LogWarningOutput += LogWarningOutput;
51+
BNL.LogErrorOutput += LogErrorOutput;
52+
HasRegisteredBnlLogging = true;
53+
}
54+
}
3355
public static bool TryGetLocalPlayerID(out ushort localId)
3456
{
3557
localId = 0;
@@ -89,7 +111,7 @@ public static async Task ResetConnectionStateAsync(BasisNetworkManagement manage
89111
Task rebootWait = WaitForRebootCompleteAsync(cts.Token);
90112

91113
SuppressNextDisconnectUi = true;
92-
await BasisNetworkLifeCycle.Destroy(management);
114+
NetworkClient?.Disconnect();
93115
await rebootWait;
94116

95117
if (management != null)
@@ -103,9 +125,7 @@ public static void Connect(ushort port, string ipString, string primitivePasswor
103125
}
104126
public static void Connect(ushort port, string ipString, string primitivePassword, bool isHostMode, NetworkTransportType transportType, bool useSteamRelay, ulong steamLobbyId, ulong steamHostSteamId, int steamVirtualPort)
105127
{
106-
BNL.LogOutput += LogOutput;
107-
BNL.LogWarningOutput += LogWarningOutput;
108-
BNL.LogErrorOutput += LogErrorOutput;
128+
EnsureBnlLoggingRegistered();
109129

110130
var uuid = BasisDIDAuthIdentityClient.GetOrSaveDID();
111131

Basis/Packages/com.basis.framework/Networking/BasisNetworkEvents.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,12 @@ public static void HandleDisconnectionReason(DisconnectInfo disconnectInfo)
475475
if (disconnectInfo.AdditionalData.TryGetString(out string Reason))
476476
{
477477
BasisMainMenu.Open();
478-
BasisMainMenu.Instance.OpenDialogue("Server Connection", Reason, "ok", value =>
478+
if (BasisMainMenu.Instance != null)
479479
{
480-
});
480+
BasisMainMenu.Instance.OpenDialogue("Server Connection", Reason, "ok", value =>
481+
{
482+
});
483+
}
481484
BasisDebug.LogError(Reason);
482485
}
483486
else
@@ -503,9 +506,12 @@ public static void HandleDisconnectionReason(DisconnectInfo disconnectInfo)
503506
BasisDebug.LogError(disconnectInfo.Reason.ToString());
504507
#else
505508
BasisMainMenu.Open();
506-
BasisMainMenu.Instance.OpenDialogue("Server Disconnected", disconnectInfo.Reason.ToString(), "ok", value =>
507-
{
508-
});
509+
if (BasisMainMenu.Instance != null)
510+
{
511+
BasisMainMenu.Instance.OpenDialogue("Server Disconnected", disconnectInfo.Reason.ToString(), "ok", value =>
512+
{
513+
});
514+
}
509515

510516
BasisDebug.LogError(disconnectInfo.Reason.ToString());
511517
#endif

Basis/Packages/com.basis.framework/Networking/BasisNetworkLifeCycle.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public static async Task RebootManagement(BasisNetworkManagement Management, boo
6969
}
7070

7171
BasisRemoteNetworkDriver.Apply();//complete in-flight jobs before clearing players
72+
BasisObjectSyncDriver.Reset();//stop owned pickup sync before scene/object teardown
7273
BasisNetworkPlayers.ClearAllRegistries();//remove players
7374
Basis.Scripts.Networking.Receivers.BasisShoutAudioDriver.DeInitialize();//remove shout audio sources
7475
await BasisNetworkSpawnItem.Reset();//remove items
@@ -113,6 +114,7 @@ public static async Task Destroy(BasisNetworkManagement Management)
113114
BasisNetworkConnection.BasisNetworkServerRunner = null;
114115
}
115116
BasisRemoteNetworkDriver.Shutdown();//complete in-flight jobs before disposing anything
117+
BasisObjectSyncDriver.Reset();//stop owned pickup sync before scene/object teardown
116118
BasisNetworkPlayers.ClearAllRegistries();//remove players
117119
Basis.Scripts.Networking.Receivers.BasisShoutAudioDriver.DeInitialize();//remove shout audio sources
118120
await BasisNetworkSpawnItem.Reset();//remove items

Basis/Packages/com.basis.framework/Networking/BasisNetworkServerRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ private void StartServer(string UUIDTomarkAsAdmin)
5050
public void Stop()
5151
{
5252
cancellationTokenSource?.Cancel();
53-
NetworkServer.Server?.Stop();
53+
NetworkServer.StopServer();
5454
}
5555
}

Basis/Packages/com.basis.framework/Networking/Handles/BasisNetworkGenericMessages.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ public static void HandleServerAvatarDataMessage(NetPacketReader reader, Deliver
189189
}
190190
public static void OnNetworkMessageSend(ushort messageIndex, byte[] buffer = null, DeliveryMethod deliveryMethod = DeliveryMethod.Unreliable, ushort[] recipients = null)
191191
{
192+
if (!BasisNetworkManagement.NetworkRunning
193+
|| !BasisNetworkConnection.LocalPlayerIsConnected
194+
|| BasisNetworkConnection.LocalPlayerPeer == null)
195+
{
196+
return;
197+
}
198+
192199
NetDataWriter netDataWriter = threadLocalWriter.Value;
193200
netDataWriter.Reset(); // clear previous data
194201

Basis/Packages/com.basis.framework/Resource Management/BasisRuntimeSpawnRegistry.cs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class SpawnInstance
6666

6767
// LoadedNetID -> instance (fast removal / lookup)
6868
private static readonly Dictionary<string, SpawnInstance> _byNetId = new();
69+
private static readonly ConcurrentDictionary<string, byte> _removalsInFlight = new();
6970

7071
public static bool HasAny(string url)
7172
=> !string.IsNullOrWhiteSpace(url)
@@ -192,55 +193,53 @@ public static async Task<bool> RemoveByLoadedNetId(string loadedNetId)
192193
{
193194
if (string.IsNullOrWhiteSpace(loadedNetId)) return false;
194195
if (!_byNetId.TryGetValue(loadedNetId, out var inst) || inst == null) return false;
196+
if (!_removalsInFlight.TryAdd(loadedNetId, 0))
197+
{
198+
return false;
199+
}
195200

196-
// Despawn first (main thread!)
197-
switch (inst.SpawnMode)
201+
try
198202
{
199-
case SpawnMode.GameObject:
200-
case SpawnMode.Avatar: // treat avatar as GO unless you add a separate avatar store
201-
{
202-
if (SpawnedGameobjects.TryGetValue(loadedNetId, out var go) && go != null)
203+
// Despawn first (main thread!)
204+
switch (inst.SpawnMode)
205+
{
206+
case SpawnMode.GameObject:
207+
case SpawnMode.Avatar: // treat avatar as GO unless you add a separate avatar store
203208
{
204-
if (inst.SpawnMethod == SpawnMethod.Embedded)
205-
{
206-
Addressables.ReleaseInstance(go);
207-
}
208-
else
209+
if (SpawnedGameobjects.TryGetValue(loadedNetId, out var go) && go != null)
209210
{
210-
GameObject.Destroy(go);
211+
if (inst.SpawnMethod == SpawnMethod.Embedded)
212+
{
213+
Addressables.ReleaseInstance(go);
214+
}
215+
else
216+
{
217+
GameObject.Destroy(go);
218+
}
211219
}
212-
}
213220

214-
break;
215-
}
216-
case SpawnMode.Scene:
217-
{
218-
if (SpawnedScenes.TryGetValue(loadedNetId, out var scene) && scene.IsValid())
221+
break;
222+
}
223+
case SpawnMode.Scene:
219224
{
220-
/*
221-
if (inst.SpawnMethod == SpawnMethod.Embedded)
222-
{
223-
Addressables.UnloadSceneAsync(scene, true);
224-
}
225-
else
226-
{
227-
SceneManager.UnloadSceneAsync(scene);
228-
}
229-
*/
230-
if (scene.IsValid() && scene.isLoaded)
225+
if (SpawnedScenes.TryGetValue(loadedNetId, out var scene) && scene.IsValid() && scene.isLoaded)
231226
{
232227
await SceneManager.UnloadSceneAsync(scene);
233228
}
234-
}
235229

230+
break;
231+
}
232+
default:
236233
break;
237-
}
238-
default:
239-
break;
240-
}
234+
}
241235

242-
// Now remove bookkeeping + runtime refs
243-
return RemoveByLoadedNetId_RegistryOnly(loadedNetId);
236+
// Now remove bookkeeping + runtime refs
237+
return RemoveByLoadedNetId_RegistryOnly(loadedNetId);
238+
}
239+
finally
240+
{
241+
_removalsInFlight.TryRemove(loadedNetId, out _);
242+
}
244243
}
245244
private static bool RemoveByLoadedNetId_RegistryOnly(string loadedNetId)
246245
{
@@ -328,6 +327,7 @@ public static void ClearAll()
328327
{
329328
_map.Clear();
330329
_byNetId.Clear();
330+
_removalsInFlight.Clear();
331331

332332
SpawnedGameobjects.Clear();
333333
SpawnedScenes.Clear();

Basis/Packages/com.basis.framework/Scene Management/BasisSceneFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public static class BasisSceneFactory
2626

2727
public static void Initalize()
2828
{
29+
BasisScene.Ready -= Initalize;
30+
BasisScene.Destroyed -= BasisSceneDestroyed;
2931
BasisScene.Ready += Initalize;
3032
BasisScene.Destroyed += BasisSceneDestroyed;
3133
SceneManager.sceneUnloaded -= OnSceneUnloaded;

0 commit comments

Comments
 (0)