Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Runtime/Scripts/Internal/FFI/FFIClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ static void OnAfterAssemblyReload()
{
InitializeSdk();
}

// The Editor raises Application.quitting when Play mode stops, so Quit() disposes the
// client at the end of every Play session. With a domain reload the next session starts
// from fresh statics; with Enter Play Mode Options skipping the reload, _isDisposed stays
// true and every event from Rust is dropped, so connects time out waiting for
// ReadyForRoomEventRequest. Bring the client back up for the new session instead.
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ReinitializeOnEnterPlayMode()
{
if (!_isDisposed)
return;

_isDisposed = false;
AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload;
AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
InitializeSdk();
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
}
#else
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void Init()
Expand Down Expand Up @@ -259,9 +276,27 @@ public void Dispose()
}
);
ClearPendingCallbacks();
ClearEventSubscribers();
Utils.Debug("FFIServer - Disposed");
}

// Every subscriber holds native handles that the DisposeRequest just invalidated. Drop them so
// rooms and streams left over from an earlier Play session are not kept alive by this
// singleton once it is re-initialized without a domain reload.
private void ClearEventSubscribers()
{
DisconnectReceived = null;
PanicReceived = null;
RoomEventReceived = null;
TrackEventReceived = null;
RpcMethodInvocationReceived = null;
VideoStreamEventReceived = null;
AudioStreamEventReceived = null;
ByteStreamReaderEventReceived = null;
TextStreamReaderEventReceived = null;
DataTrackStreamEventReceived = null;
}

internal void RegisterPendingCallback<TCallback>(
ulong requestAsyncId,
Func<FfiEvent, TCallback?> selector,
Expand Down
Loading