Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ class EosSdkBridge {

@Volatile private var sdkPoisoned = false

private val sdkLock = Any()

private var sdkOwner: EosSdkBridge? = null

private var logCallbackInstalled = false

internal fun markSdkRetired() {
sdkRetired = true
}
Expand Down Expand Up @@ -149,6 +155,7 @@ class EosSdkBridge {
starting.set(false)
return false
}
synchronized(sdkLock) { sdkOwner = this }
tickThread = worker
running = true
worker.start()
Expand Down Expand Up @@ -220,7 +227,12 @@ class EosSdkBridge {
sdkInitialized = true

logTarget = this
EosLogging.setCallback { msg -> logTarget?.onLog(msg.category, msg.level, msg.message) }
synchronized(sdkLock) {
if (!logCallbackInstalled) {
EosLogging.setCallback { msg -> logTarget?.onLog(msg.category, msg.level, msg.message) }
logCallbackInstalled = true
}
}
EosLogging.setLogLevel(EosLogCategory.AllCategories, EosLogLevel.Info)

val mode = EosMode.current
Expand Down Expand Up @@ -354,7 +366,7 @@ class EosSdkBridge {

park(idleTicks)
}
runCatching { pump() }.onFailure { logger.warn("The final EOS tick before shutdown failed", it) }
if (!abandoned) runCatching { pump() }.onFailure { logger.warn("The final EOS tick before shutdown failed", it) }
rejectPendingCalls()
teardown(retireSdk = !abandoned && !keepSdk)
}
Expand Down Expand Up @@ -446,20 +458,33 @@ class EosSdkBridge {

detachHandlers()

this.platform?.let { platform ->
this.platform = null
runCatching { platform.close() }.onFailure { logger.warn("Closing the EOS platform failed", it) }
}
synchronized(sdkLock) {
val ownsSdk = sdkOwner === this && !abandoned
if (ownsSdk) sdkOwner = null

if (sdkInitialized) {
sdkInitialized = false
when {
!retireSdk -> Unit
sdkPoisoned ->
logger.warn("Not shutting the EOS SDK down; an abandoned platform from this process is still open")
else -> {
sdkRetired = true
runCatching { Eos.shutdown() }.onFailure { logger.warn("Shutting the EOS SDK down failed", it) }
this.platform?.let { platform ->
this.platform = null
if (!ownsSdk) {
logger.warn("Leaving a replaced EOS platform open until the game exits; closing it would free the callbacks of the platform that replaced it")
return@let
}
if (logCallbackInstalled) {
runCatching { EosLogging.setCallback(null) }.onFailure { logger.warn("Clearing the EOS log callback failed", it) }
logCallbackInstalled = false
}
runCatching { platform.close() }.onFailure { logger.warn("Closing the EOS platform failed", it) }
}

if (sdkInitialized) {
sdkInitialized = false
when {
!retireSdk -> Unit
sdkPoisoned || !ownsSdk ->
logger.warn("Not shutting the EOS SDK down; another EOS platform from this process is still open")
else -> {
sdkRetired = true
runCatching { Eos.shutdown() }.onFailure { logger.warn("Shutting the EOS SDK down failed", it) }
}
}
}
}
Expand Down