Skip to content

Commit

Permalink
Add Jar loading
Browse files Browse the repository at this point in the history
  • Loading branch information
CedNaru committed May 9, 2024
1 parent 1c4ab7c commit 47f2f34
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 6 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "register_types.h"
#include "resource_format/jvm_resource_format_loader.h"
#include "resource_format/jvm_resource_format_saver.h"
#include "resource_format/java_archive_resource_format_loader.h"
#include "script/jvm_script.h"
#include "script/language/gdj_script.h"
#include "script/language/java_script.h"
Expand All @@ -22,6 +23,8 @@
Ref<JvmResourceFormatLoader> resource_format_loader;
Ref<JvmResourceFormatSaver> resource_format_saver;

Ref<JavaArchiveFormatLoader> java_archive_format_loader;

#ifdef TOOLS_ENABLED
static void editor_init() {
Ref<KotlinEditorExportPlugin> export_plugin;
Expand Down Expand Up @@ -55,6 +58,11 @@ void initialize_kotlin_jvm_module(ModuleInitializationLevel p_level) {
ResourceLoader::add_resource_format_loader(resource_format_loader);
resource_format_saver.instantiate();
ResourceSaver::add_resource_format_saver(resource_format_saver);

java_archive_format_loader.instantiate();
ResourceLoader::add_resource_format_loader(java_archive_format_loader);

MessageQueue::get_singleton()->push_callable(callable_mp(GDKotlin::get_instance(), &GDKotlin::initialize_up_to));

Check failure on line 65 in register_types.cpp

View workflow job for this annotation

GitHub Actions / 🪟 Build Windows / Build editor release (target=editor)

'create_custom_callable_function_pointer': no matching overloaded function found

Check failure on line 65 in register_types.cpp

View workflow job for this annotation

GitHub Actions / 🪟 Build Windows / Build editor release (target=editor)

'CallQueue::push_callable': no matching overloaded function found

Check failure on line 65 in register_types.cpp

View workflow job for this annotation

GitHub Actions / 🪟 Build Windows / Build editor debug (target=editor, debug_symbols=true )

'create_custom_callable_function_pointer': no matching overloaded function found

Check failure on line 65 in register_types.cpp

View workflow job for this annotation

GitHub Actions / 🪟 Build Windows / Build editor debug (target=editor, debug_symbols=true )

'CallQueue::push_callable': no matching overloaded function found

Check failure on line 65 in register_types.cpp

View workflow job for this annotation

GitHub Actions / 🪟 Build Windows / Build editor dev (target=editor, dev_build=yes, debug_symbols=true )

'create_custom_callable_function_pointer': no matching overloaded function found

Check failure on line 65 in register_types.cpp

View workflow job for this annotation

GitHub Actions / 🪟 Build Windows / Build editor dev (target=editor, dev_build=yes, debug_symbols=true )

'CallQueue::push_callable': no matching overloaded function found

Check failure on line 65 in register_types.cpp

View workflow job for this annotation

GitHub Actions / 🪟 Build Windows / Build release template

'create_custom_callable_function_pointer': no matching overloaded function found

Check failure on line 65 in register_types.cpp

View workflow job for this annotation

GitHub Actions / 🪟 Build Windows / Build release template

'CallQueue::push_callable': no matching overloaded function found

Check failure on line 65 in register_types.cpp

View workflow job for this annotation

GitHub Actions / 🪟 Build Windows / Build debug template

'create_custom_callable_function_pointer': no matching overloaded function found

Check failure on line 65 in register_types.cpp

View workflow job for this annotation

GitHub Actions / 🪟 Build Windows / Build debug template

'CallQueue::push_callable': no matching overloaded function found
}

#ifdef TOOLS_ENABLED
Expand All @@ -70,7 +78,9 @@ void uninitialize_kotlin_jvm_module(ModuleInitializationLevel p_level) {
if (Engine::get_singleton()->is_project_manager_hint()) { return; }
#endif

if (p_level != MODULE_INITIALIZATION_LEVEL_SERVERS) { return; }
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { return; }

ResourceLoader::remove_resource_format_loader((java_archive_format_loader));

ResourceLoader::remove_resource_format_loader((resource_format_loader));
ResourceSaver::remove_resource_format_saver(resource_format_saver);
Expand Down
10 changes: 6 additions & 4 deletions src/gd_kotlin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ bool GDKotlin::load_user_code() {
return true;
} else {
#ifdef TOOLS_ENABLED
String user_code_path {String(BUILD_DIRECTORY) + String(USER_CODE_FILE)};
String user_code_path {String(RES_DIRECTORY).path_join(JVM_DIRECTORY).path_join(USER_CODE_FILE)};
#else
String user_code_path {copy_new_file_to_user_dir(USER_CODE_FILE)};
#endif
Expand All @@ -280,7 +280,7 @@ bool GDKotlin::load_user_code() {
return false;
}

LOG_VERBOSE(vformat("Loading usercode file at: %s", user_code_path));
jar = ResourceLoader::load(user_code_path);

ClassLoader* user_class_loader = ClassLoader::create_instance(
env,
Expand Down Expand Up @@ -335,8 +335,10 @@ void GDKotlin::unload_boostrap() {
}

void GDKotlin::initialize_up_to(State target_state) {
fetch_user_configuration();
set_jvm_options();
if(state == State::NOT_STARTED){
fetch_user_configuration();
set_jvm_options();
}

#ifdef DYNAMIC_JVM
SET_LOADING_STATE(load_dynamic_lib(), JVM_LIBRARY_LOADED, target_state)
Expand Down
2 changes: 2 additions & 0 deletions src/gd_kotlin.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "lifecycle/jvm_manager.h"
#include "lifecycle/jvm_options.h"
#include "lifecycle/jvm_user_configuration.h"
#include "resource_format/java_archive.h"

#include <core/string/ustring.h>

Expand All @@ -28,6 +29,7 @@ class GDKotlin {

ClassLoader* bootstrap_class_loader {nullptr};
Bootstrap* bootstrap {nullptr};
Ref<JavaArchive> jar;

void fetch_user_configuration();
void set_jvm_options();
Expand Down
1 change: 1 addition & 0 deletions src/lifecycle/paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ static constexpr const char* ENTRY_DIRECTORY {"res://build/generated/ksp"};
static constexpr const char* BUILD_DIRECTORY {"res://build/libs/"};
static constexpr const char* USER_DIRECTORY {"user://"};
static constexpr const char* RES_DIRECTORY {"res://"};
static constexpr const char* JVM_DIRECTORY {"jvm"};

static constexpr const char* JVM_CONFIGURATION_PATH {"res://godot_kotlin_configuration.json"};

Expand Down
13 changes: 13 additions & 0 deletions src/resource_format/java_archive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef GODOT_JVM_JAVA_ARCHIVE_H
#define GODOT_JVM_JAVA_ARCHIVE_H

#include <core/io/resource.h>

class JavaArchive : public Resource {
GDCLASS(JavaArchive, Resource);

protected:
static void _bind_methods() {};
};

#endif// GODOT_JVM_JAVA_ARCHIVE_H
34 changes: 34 additions & 0 deletions src/resource_format/java_archive_resource_format_loader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

#include "java_archive_resource_format_loader.h"

#include "java_archive.h"
#include "logging.h"

void JavaArchiveFormatLoader::get_recognized_extensions(List<String>* p_extensions) const {
p_extensions->push_back("jar");
p_extensions->push_back("dex");
}

String JavaArchiveFormatLoader::get_resource_type(const String& p_path) const {
String ext = p_path.get_extension().to_lower();
if(ext == "jar" || ext == "dex"){ return "JavaArchive";}
return "";
}

bool JavaArchiveFormatLoader::handles_type(const String& p_type) const {
return p_type == "JavaArchive";
}

Ref<Resource> JavaArchiveFormatLoader::load(
const String& p_path,
const String& p_original_path,
Error* r_error,
bool p_use_sub_threads,
float* r_progress,
ResourceFormatLoader::CacheMode p_cache_mode
) {
LOG_VERBOSE(vformat("Loading usercode file at: %s", p_path));
Ref<JavaArchive> ref;
ref.instantiate();
return ref;
}
19 changes: 19 additions & 0 deletions src/resource_format/java_archive_resource_format_loader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef GODOT_JVM_JAVA_ARCHIVE_LOADER_H
#define GODOT_JVM_JAVA_ARCHIVE_LOADER_H

#include <core/io/resource_loader.h>

class JavaArchiveFormatLoader : public ResourceFormatLoader {
public:
JavaArchiveFormatLoader() = default;
~JavaArchiveFormatLoader() = default;
JavaArchiveFormatLoader(const JavaArchiveFormatLoader&) = delete;
void operator=(const JavaArchiveFormatLoader&) = delete;

void get_recognized_extensions(List<String>* p_extensions) const override;
String get_resource_type(const String& p_path) const override;
bool handles_type(const String& p_type) const override;
Ref<Resource> load(const String& p_path, const String& p_original_path, Error* r_error, bool p_use_sub_threads, float* r_progress, CacheMode p_cache_mode) override;
};

#endif// GODOT_JVM_JAVA_ARCHIVE_LOADER_H

0 comments on commit 47f2f34

Please sign in to comment.