Game loading now no longer blocks universe thread and it can perform other work
This commit is contained in:
parent
2f782d7825
commit
0b3aac6189
@ -13,8 +13,10 @@ import kotlinx.coroutines.Runnable
|
|||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.asCoroutineDispatcher
|
import kotlinx.coroutines.asCoroutineDispatcher
|
||||||
import kotlinx.coroutines.async
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.future.asCompletableFuture
|
import kotlinx.coroutines.future.asCompletableFuture
|
||||||
import kotlinx.coroutines.future.await
|
import kotlinx.coroutines.future.await
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import org.apache.logging.log4j.LogManager
|
import org.apache.logging.log4j.LogManager
|
||||||
import org.classdump.luna.compiler.CompilerChunkLoader
|
import org.classdump.luna.compiler.CompilerChunkLoader
|
||||||
import org.classdump.luna.compiler.CompilerSettings
|
import org.classdump.luna.compiler.CompilerSettings
|
||||||
@ -344,7 +346,6 @@ object Starbound : BlockableEventLoop("Universe Thread"), Scheduler, ISBFileLoca
|
|||||||
|
|
||||||
registerTypeAdapterFactory(JsonReference.Companion)
|
registerTypeAdapterFactory(JsonReference.Companion)
|
||||||
|
|
||||||
registerTypeAdapter(ColorReplacements.Companion)
|
|
||||||
registerTypeAdapterFactory(BlueprintLearnList.Companion)
|
registerTypeAdapterFactory(BlueprintLearnList.Companion)
|
||||||
|
|
||||||
registerTypeAdapter(RGBAColorTypeAdapter)
|
registerTypeAdapter(RGBAColorTypeAdapter)
|
||||||
@ -619,7 +620,7 @@ object Starbound : BlockableEventLoop("Universe Thread"), Scheduler, ISBFileLoca
|
|||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun doInitialize() {
|
private suspend fun doInitialize() {
|
||||||
if (!initializing && !initialized) {
|
if (!initializing && !initialized) {
|
||||||
initializing = true
|
initializing = true
|
||||||
} else {
|
} else {
|
||||||
@ -665,21 +666,29 @@ object Starbound : BlockableEventLoop("Universe Thread"), Scheduler, ISBFileLoca
|
|||||||
loaded = toLoad - tasks.size
|
loaded = toLoad - tasks.size
|
||||||
loadingProgress = (total - tasks.size) / total
|
loadingProgress = (total - tasks.size) / total
|
||||||
loadingProgressText = "Loading JSON assets, $loaded / $toLoad"
|
loadingProgressText = "Loading JSON assets, $loaded / $toLoad"
|
||||||
LockSupport.parkNanos(5_000_000L)
|
delay(10L)
|
||||||
}
|
}
|
||||||
|
|
||||||
Registries.finishLoad()
|
|
||||||
RecipeRegistry.finishLoad()
|
|
||||||
ItemRegistry.finishLoad()
|
ItemRegistry.finishLoad()
|
||||||
|
|
||||||
Registries.validate()
|
Registries.validate()
|
||||||
|
|
||||||
initializing = false
|
initializing = false
|
||||||
initialized = true
|
initialized = true
|
||||||
|
|
||||||
|
// Suggest VM to reclaim memory after game has been loaded
|
||||||
|
System.gc()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var initializationFuture: CompletableFuture<*>? = null
|
||||||
|
|
||||||
fun initializeGame(): CompletableFuture<*> {
|
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
|
private var fontPath: File? = null
|
||||||
|
Loading…
Reference in New Issue
Block a user