Skip to content
This repository was archived by the owner on Jan 31, 2026. It is now read-only.
Closed
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
29 changes: 13 additions & 16 deletions bHapticsManager/bHapticsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,21 @@ public override void OnEngineInit()

void WorldFocusedHandler(World world)
{
if (world != null)
// Check if initialization is needed and atomically set _initialized to true
if (world != null && !Interlocked.CompareExchange(ref _initialized, true, false))
{
// Check if initialization is needed and atomically set _initialized to true
if (!Interlocked.CompareExchange(ref _initialized, true, false))
try
{
try
{
world.RunSynchronously(() => InitializeHaptics());
// Unsubscribe from the event after first successful initialization
engine.WorldManager.WorldFocused -= WorldFocusedHandler;
ResoniteMod.Debug("WorldFocused event handler unsubscribed after initialization");
}
catch (Exception ex)
{
Error($"Failed to initialize haptics on world focus: {ex}");
// Reset _initialized to allow retry on next world focus
Interlocked.Exchange(ref _initialized, false);
}
world.RunSynchronously(() => InitializeHaptics());
// Unsubscribe from the event after first successful initialization
engine.WorldManager.WorldFocused -= WorldFocusedHandler;
ResoniteMod.Debug("WorldFocused event handler unsubscribed after initialization");
}
catch (Exception ex)
{
Error($"Failed to initialize haptics on world focus: {ex}");
// Reset _initialized to allow retry on next world focus
Interlocked.Exchange(ref _initialized, false);
}
}
}
Expand Down