Skip to content

Commit 58f2b0e

Browse files
committed
more cleanup
1 parent 8e7cda2 commit 58f2b0e

8 files changed

Lines changed: 104 additions & 64 deletions

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public static class BasisNetworkPreloadManager
2929
/// </summary>
3030
public static readonly ConcurrentDictionary<string, PreloadedResource> PreloadedResources = new();
3131

32+
private static CancellationTokenSource _cts = new CancellationTokenSource();
33+
3234
public class PreloadedResource
3335
{
3436
public LocalLoadResource LoadResource;
@@ -74,7 +76,7 @@ private static async Task HandlePreload(LocalLoadResource resource)
7476
};
7577

7678
BasisProgressReport report = new BasisProgressReport();
77-
CancellationToken cancel = default;
79+
CancellationToken cancel = _cts.Token;
7880

7981
await BasisLoadHandler.EnsureInitializationComplete();
8082

@@ -262,6 +264,9 @@ private static async Task UnloadAllSceneContent()
262264
/// </summary>
263265
public static void Reset()
264266
{
267+
_cts.Cancel();
268+
_cts.Dispose();
269+
_cts = new CancellationTokenSource();
265270
PreloadedResources.Clear();
266271
}
267272
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using static SerializableBasis;
1818
public static class BasisNetworkSpawnItem
1919
{
20+
private static CancellationTokenSource _loadCts = new CancellationTokenSource();
2021
public static bool RequestSceneLoad(string UnlockPassword, string CombinedURL, bool Persist, bool Admin, out LocalLoadResource localLoadResource, byte loadStrategy = 0)
2122
{
2223
if (string.IsNullOrEmpty(CombinedURL) || string.IsNullOrEmpty(UnlockPassword))
@@ -136,6 +137,7 @@ public static void RequestUnload(UnLoadResource UnLoadResource)
136137

137138
public static async Task<Scene> SpawnScene(LocalLoadResource localLoadResource)
138139
{
140+
_loadCts.Token.ThrowIfCancellationRequested();
139141
BasisDebug.Log($"Spawning scene with NetID: {localLoadResource.LoadedNetID}", BasisDebug.LogTag.Networking);
140142

141143
BasisLoadableBundle loadBundle = new BasisLoadableBundle
@@ -148,6 +150,7 @@ public static async Task<Scene> SpawnScene(LocalLoadResource localLoadResource)
148150
};
149151

150152
Scene scene = await BasisSceneLoad.LoadSceneAssetBundle(loadBundle);
153+
_loadCts.Token.ThrowIfCancellationRequested();
151154
BasisDebug.Log($"LoadSceneAssetBundle Complete now Starting Scene Traversal", BasisDebug.LogTag.Networking);
152155
SceneTraverseNetIdAssign(scene, localLoadResource);
153156

@@ -197,7 +200,7 @@ public static async Task<GameObject> SpawnGameObject(LocalLoadResource localLoad
197200
var position = new Vector3(localLoadResource.PositionX, localLoadResource.PositionY, localLoadResource.PositionZ);
198201
var rotation = new Quaternion(localLoadResource.QuaternionX, localLoadResource.QuaternionY, localLoadResource.QuaternionZ, localLoadResource.QuaternionW);
199202
var scale = new Vector3(localLoadResource.ScaleX, localLoadResource.ScaleY, localLoadResource.ScaleZ);
200-
GameObject reference = await BasisLoadHandler.LoadGameObjectBundle(BasisDeviceManagement.Instance.CreationGameobject, loadBundle, true, BasisProgressReport, new CancellationToken(),
203+
GameObject reference = await BasisLoadHandler.LoadGameObjectBundle(BasisDeviceManagement.Instance.CreationGameobject, loadBundle, true, BasisProgressReport, _loadCts.Token,
201204
position,
202205
rotation,
203206
scale,
@@ -260,6 +263,9 @@ public static async Task DestroyGameobject(UnLoadResource resource)
260263

261264
public static async Task Reset()
262265
{
266+
_loadCts.Cancel();
267+
_loadCts.Dispose();
268+
_loadCts = new CancellationTokenSource();
263269
await BasisRuntimeSpawnRegistry.ClearAllNetworking();
264270
}
265271
}

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

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -225,29 +225,36 @@ public static async Task LoadResourceMessage(NetPacketReader reader, DeliveryMet
225225
LocalLoadResource LocalLoadResource = new LocalLoadResource();
226226
LocalLoadResource.Deserialize(reader);
227227

228-
// Check the load strategy before spawning
229-
switch (LocalLoadResource.LoadStrategy)
228+
try
230229
{
231-
case 2: // Synchronized - download, report readiness, wait for spawn signal
232-
await BasisNetworkPreloadManager.HandleSynchronizedPreload(LocalLoadResource);
233-
return;
234-
}
230+
// Check the load strategy before spawning
231+
switch (LocalLoadResource.LoadStrategy)
232+
{
233+
case 2: // Synchronized - download, report readiness, wait for spawn signal
234+
await BasisNetworkPreloadManager.HandleSynchronizedPreload(LocalLoadResource);
235+
return;
236+
}
235237

236-
// LoadStrategy 0 (Immediate) - existing behavior
237-
switch (LocalLoadResource.Mode)
238+
// LoadStrategy 0 (Immediate) - existing behavior
239+
switch (LocalLoadResource.Mode)
240+
{
241+
case 0:
242+
await BasisNetworkSpawnItem.SpawnGameObject(LocalLoadResource, BundledContentHolder.Selector.Prop);
243+
break;
244+
case 1:
245+
await BasisNetworkSpawnItem.SpawnScene(LocalLoadResource);
246+
break;
247+
case 2:
248+
await BasisNetworkSpawnItem.SpawnGameObject(LocalLoadResource, BundledContentHolder.Selector.Avatar);
249+
break;
250+
default:
251+
BNL.LogError($"tried to Load Mode {LocalLoadResource.Mode}");
252+
break;
253+
}
254+
}
255+
catch (OperationCanceledException)
238256
{
239-
case 0:
240-
await BasisNetworkSpawnItem.SpawnGameObject(LocalLoadResource, BundledContentHolder.Selector.Prop);
241-
break;
242-
case 1:
243-
await BasisNetworkSpawnItem.SpawnScene(LocalLoadResource);
244-
break;
245-
case 2:
246-
await BasisNetworkSpawnItem.SpawnGameObject(LocalLoadResource, BundledContentHolder.Selector.Avatar);
247-
break;
248-
default:
249-
BNL.LogError($"tried to Load Mode {LocalLoadResource.Mode}");
250-
break;
257+
BasisDebug.Log($"Load cancelled for {LocalLoadResource.LoadedNetID} (disconnected)", BasisDebug.LogTag.Networking);
251258
}
252259
}
253260

@@ -259,7 +266,14 @@ public static async Task SpawnPreloadedMessage(NetPacketReader reader, DeliveryM
259266
{
260267
SpawnPreloadedMessage spawnMsg = new SpawnPreloadedMessage();
261268
spawnMsg.Deserialize(reader);
262-
await BasisNetworkPreloadManager.HandleSpawnPreloaded(spawnMsg);
269+
try
270+
{
271+
await BasisNetworkPreloadManager.HandleSpawnPreloaded(spawnMsg);
272+
}
273+
catch (OperationCanceledException)
274+
{
275+
BasisDebug.Log($"Spawn cancelled for preloaded {spawnMsg.LoadedNetID} (disconnected)", BasisDebug.LogTag.Networking);
276+
}
263277
}
264278
public static async Task UnloadResourceMessage(NetPacketReader reader, DeliveryMethod Method)
265279
{

Basis/Packages/com.basis.framework/Networking/Recievers/BasisShoutAudioDriver.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ public static void ReceiveShoutAudio(ushort playerId, AudioSegmentDataMessage au
188188
}
189189

190190
entry.Receiver.Insert(audioData);
191+
192+
// Notify the player's nameplate that audio was received so it shows the talking state
193+
if (BasisNetworkPlayers.RemotePlayers.TryGetValue(playerId, out BasisNetworkReceiver receiver))
194+
{
195+
receiver.Player.AudioReceived?.Invoke();
196+
}
191197
}
192198

193199
/// <summary>

Basis/Packages/com.basis.server/BasisNetworkServer/BasisNetworkPreloadResourceManagement.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,54 @@ private static void UnloadAllSceneResources(NetPeer[] peerSnapshot)
207207
NetworkServer.ReturnWriter(writer);
208208
}
209209

210+
/// <summary>
211+
/// Removes a disconnected peer from all active synchronized load sessions.
212+
/// Decrements the expected peer count and triggers the spawn signal if
213+
/// all remaining peers have already reported.
214+
/// </summary>
215+
public static void RemovePeer(int peerId)
216+
{
217+
List<string> completedSessions = null;
218+
219+
foreach (var kvp in ActiveSessions)
220+
{
221+
var session = kvp.Value;
222+
session.ReadyPeers.Remove(peerId);
223+
session.FailedPeers.Remove(peerId);
224+
225+
if (session.TotalPeerCount > 0)
226+
{
227+
session.TotalPeerCount--;
228+
}
229+
230+
if (session.TotalPeerCount <= 0)
231+
{
232+
// No peers left, just clean up
233+
session.TimeoutCts?.Cancel();
234+
session.TimeoutCts?.Dispose();
235+
ActiveSessions.TryRemove(kvp.Key, out _);
236+
}
237+
else if (session.IsComplete)
238+
{
239+
completedSessions ??= new List<string>();
240+
completedSessions.Add(kvp.Key);
241+
}
242+
}
243+
244+
if (completedSessions != null)
245+
{
246+
foreach (var netId in completedSessions)
247+
{
248+
if (ActiveSessions.TryGetValue(netId, out var session))
249+
{
250+
BNL.Log($"PreloadResourceManagement: All remaining peers reported for {netId} after peer {peerId} disconnected, sending spawn signal");
251+
session.TimeoutCts.Cancel();
252+
BroadcastSpawnSignal(netId);
253+
}
254+
}
255+
}
256+
}
257+
210258
/// <summary>
211259
/// Cleans up all active sessions. Called on server reset.
212260
/// </summary>

Basis/Packages/com.basis.server/BasisNetworkServer/BasisServerHandleEvents.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public static void HandlePeerDisconnected(NetPeer peer, DisconnectInfo info)
7979
BasisServerReductionSystemEvents.RemovePlayer(id);
8080
BasisNetworkPIPCamera.RemovePlayer(id);
8181
BasisNetworkContentShare.RemovePlayerSpheres(id);
82+
BasisNetworkPreloadResourceManagement.RemovePeer(id);
8283

8384
if (NetworkServer.AuthenticatedPeers.TryRemove(id, out _))
8485
{

Basis/ProjectSettings/ProjectSettings.asset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,7 @@ PlayerSettings:
947947
captureStartupLogs: {}
948948
activeInputHandler: 1
949949
windowsGamepadBackendHint: 0
950+
enableDirectStorage: 0
950951
cloudProjectId: 119fdfcf-36b9-4c45-a3e2-7da2705a7ef7
951952
framebufferDepthMemorylessMode: 2
952953
qualitySettingsNames: []

Basis/permissions.xml

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)