Game loading now no longer blocks universe thread and it can perform other work

This commit is contained in:
DBotThePony 2024-05-22 18:59:57 +07:00
parent 2f782d7825
commit 0b3aac6189
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -13,8 +13,10 @@ import kotlinx.coroutines.Runnable
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.future.asCompletableFuture
import kotlinx.coroutines.future.await
import kotlinx.coroutines.launch
import org.apache.logging.log4j.LogManager
import org.classdump.luna.compiler.CompilerChunkLoader
import org.classdump.luna.compiler.CompilerSettings
@ -344,7 +346,6 @@ object Starbound : BlockableEventLoop("Universe Thread"), Scheduler, ISBFileLoca
registerTypeAdapterFactory(JsonReference.Companion)
registerTypeAdapter(ColorReplacements.Companion)
registerTypeAdapterFactory(BlueprintLearnList.Companion)
registerTypeAdapter(RGBAColorTypeAdapter)
@ -619,7 +620,7 @@ object Starbound : BlockableEventLoop("Universe Thread"), Scheduler, ISBFileLoca
return files
}
private fun doInitialize() {
private suspend fun doInitialize() {
if (!initializing && !initialized) {
initializing = true
} else {
@ -665,21 +666,29 @@ object Starbound : BlockableEventLoop("Universe Thread"), Scheduler, ISBFileLoca
loaded = toLoad - tasks.size
loadingProgress = (total - tasks.size) / total
loadingProgressText = "Loading JSON assets, $loaded / $toLoad"
LockSupport.parkNanos(5_000_000L)
delay(10L)
}
Registries.finishLoad()
RecipeRegistry.finishLoad()
ItemRegistry.finishLoad()
Registries.validate()
initializing = false
initialized = true
// Suggest VM to reclaim memory after game has been loaded
System.gc()
}
private var initializationFuture: CompletableFuture<*>? = null
fun initializeGame(): CompletableFuture<*> {
return submit { doInitialize() }
return supplyAsync {
if (initializationFuture == null) {
initializationFuture = scope.launch { doInitialize() }.asCompletableFuture()
}
return@supplyAsync initializationFuture!!
}.thenCompose { it }
}
private var fontPath: File? = null