Skip to content

Commit

Permalink
Disable JVM reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
CedNaru committed Apr 27, 2024
1 parent 6797d61 commit b22e2d8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 96 deletions.
93 changes: 8 additions & 85 deletions kt/godot-library/src/main/kotlin/godot/runtime/Bootstrap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,15 @@ import godot.core.variantMapper
import godot.registration.ClassRegistry
import godot.registration.Entry
import godot.util.err
import godot.util.info
import godot.util.warning
import java.io.File
import java.net.URL
import java.net.URLClassLoader
import java.nio.file.FileSystems
import java.nio.file.StandardWatchEventKinds
import java.nio.file.WatchService
import java.util.*
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit


internal class Bootstrap {
private val classRegistries: MutableList<ClassRegistry> = mutableListOf()
private lateinit var classloader: ClassLoader
private lateinit var serviceLoader: ServiceLoader<Entry>
private var executor: ScheduledExecutorService? = null
private var watchService: WatchService? = null
private var engineTypesRegistered: Boolean = false

/** projectRootDir is empty if not in editor (only used for reloading)
* userCodePath is empty if usercode loaded as part of the VM
Expand All @@ -38,67 +26,18 @@ internal class Bootstrap {
doInitGraal()
} else {
val userCodeFile = File(userCodePath)

if (userCodeFile.exists()) {
doInit(userCodeFile.toURI().toURL(), loader)
} else {
if (projectRootDir.isNotEmpty()) {
::warning
} else {
::err
}.invoke("No main.jar detected at $userCodeFile. No classes will be loaded. Build the gradle project to load classes")
}

if (projectRootDir.isNotEmpty()) {
watchService = FileSystems.getDefault().newWatchService()
val watchKey = getBuildLockDir(projectRootDir).toPath().register(
watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY,
)

executor = Executors.newSingleThreadScheduledExecutor { runnable ->
val thread = Thread(runnable)
thread.isDaemon = true
thread
}

executor!!.scheduleAtFixedRate({
val events = watchKey.pollEvents()
if (events.isNotEmpty()) {
if (File(getBuildLockDir(projectRootDir), "buildLock.lock").exists()) {
info("Build lock present. Not reloading...")
return@scheduleAtFixedRate
}
info("Changes detected, reloading classes ...")

if (::serviceLoader.isInitialized) {
clearClassesCache()
serviceLoader.reload()
}

if (userCodeFile.exists()) {
doInit(userCodeFile.toURI().toURL(), null) //no classloader so new main jar gets loaded
} else {
warning("No main.jar detected. No classes will be loaded. Build the project to load classes")
}
}
}, 3, 3, TimeUnit.SECONDS)
}
doInit(userCodeFile.toURI().toURL(), loader)
}
}

fun finish() {
executor?.shutdown()
watchService?.close()
Thread.currentThread().contextClassLoader = null
clearClassesCache()
serviceLoader.reload()
}

private fun doInit(mainJar: URL, classLoader: ClassLoader?) {
classloader = classLoader ?: URLClassLoader(arrayOf(mainJar), this::class.java.classLoader)
Thread.currentThread().contextClassLoader = classloader
serviceLoader = ServiceLoader.load(Entry::class.java, classloader)
initializeUsingEntry()
}
Expand All @@ -123,6 +62,7 @@ internal class Bootstrap {
// the entry with the most class registrars is always the "main" entry. All other entries are from dependencies
// reason: the "main" compilation generates the registration files from all class registrars (its own AND all from dependencies). Hence, it will always be the one with the highest registrar count
val mainEntry = entries.maxBy { entry -> entry.classRegistrarCount }
val classRegistries = mutableListOf<ClassRegistry>()

entries.forEach { entry ->
val isMainEntry = entry == mainEntry
Expand All @@ -131,23 +71,21 @@ internal class Bootstrap {
projectName = entry.projectName,
isDependency = !isMainEntry,
)
classRegistries.add(registry)

classRegistries.add(registry)
val context = Entry.Context(registry)

with(entry) {
if (!engineTypesRegistered && isMainEntry) {
if (isMainEntry) {
context.initEngineTypes()
for (clazz in context.getRegisteredClasses()) {
variantMapper[clazz] = VariantType.OBJECT
}
registerManagedEngineTypes(
TypeManager.engineTypeNames.toTypedArray(),
TypeManager.engineSingletonsNames.toTypedArray()
)
engineTypesRegistered = true
}

for (clazz in context.getRegisteredClasses()) {
variantMapper[clazz] = VariantType.OBJECT
}
context.init()
}
}
Expand All @@ -162,22 +100,7 @@ internal class Bootstrap {
// END: order matters!
}

private fun getBuildLockDir(projectDir: String): File {
val name = "${File(projectDir).name}_buildLockDir" //keep the same in the gradle plugin!
val tmpDir = System.getProperty("java.io.tmpdir")
val lockDir = File(tmpDir, name)

return if (lockDir.exists() && lockDir.isDirectory) {
lockDir
} else {
lockDir.delete()
lockDir.mkdirs()
lockDir
}
}

private fun clearClassesCache() {
classRegistries.clear()
TypeManager.clearUserTypes()
}

Expand Down
31 changes: 20 additions & 11 deletions src/gd_kotlin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,25 @@ bool GDKotlin::load_user_code() {
#else
String user_code_path {copy_new_file_to_user_dir(USER_CODE_FILE)};
#endif
if (!FileAccess::exists(user_code_path)) { return false; }
if (!FileAccess::exists(user_code_path)) {
String message {"No main.jar detected at $userCodeFile. No classes will be loaded. Build the gradle "
"project to load classes"};
#ifdef TOOLS_ENABLED
LOG_WARNING(vformat(message, user_code_path));
#elif defined DEBUG_ENABLED
LOG_ERROR(vformat(message, user_code_path));
#endif
return false;
}

LOG_VERBOSE(vformat("Loading usercode file at: %s", user_code_path));
// TODO: Rework this part when cpp reloading done, can't check what's happening in the Kotlin code from here.

ClassLoader* user_class_loader = ClassLoader::create_instance(
env,
ProjectSettings::get_singleton()->globalize_path(user_code_path),
bootstrap_class_loader->get_wrapped()
);

bootstrap->init(
env,
project_path,
Expand Down Expand Up @@ -317,12 +327,11 @@ void GDKotlin::unload_boostrap() {
bootstrap_class_loader = nullptr;
}


#define SET_LOADING_STATE(cond, new_state, target_state) \
if (state < State::new_state) { \
if (!cond) { return; } \
state = State::new_state; \
if (new_state == target_state) { return; } \
if (state < State::new_state) { \
if (!cond) { return; } \
state = State::new_state; \
if (new_state == target_state) { return; } \
}

void GDKotlin::initialize_up_to(State target_state) {
Expand All @@ -341,10 +350,10 @@ void GDKotlin::initialize_up_to(State target_state) {
}

#define UNSET_LOADING_STATE(function, new_state, target_state) \
if (state > State::new_state) { \
function; \
state = State::new_state; \
if (new_state == target_state) { return; } \
if (state > State::new_state) { \
function; \
state = State::new_state; \
if (new_state == target_state) { return; } \
}

void GDKotlin::finalize_down_to(State target_state) {
Expand Down

0 comments on commit b22e2d8

Please sign in to comment.