Skip to content

Commit c67ad4f

Browse files
committed
feat: refactor jvm_instance.h for gdextension
1 parent f5c5add commit c67ad4f

19 files changed

+581
-354
lines changed

src/api/resource_format/java_archive.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef GODOT_JVM_JAVA_ARCHIVE_H
22
#define GODOT_JVM_JAVA_ARCHIVE_H
33

4-
#include <core/io/resource.h>
4+
#include <classes/resource.hpp>
55

66
namespace godot {
77
class JavaArchive : public Resource {
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11

22
#include "java_archive_resource_format_loader.h"
33

4+
#include <classes/resource_uid.hpp>
5+
#include "engine/utilities.h"
46
#include "godot_jvm.h"
57
#include "hash.h"
68
#include "java_archive.h"
7-
#include "lifecycle/paths.h"
89
#include "logging.h"
10+
#include "paths.h"
911

1012
using namespace godot;
1113

12-
void JavaArchiveFormatLoader::get_recognized_extensions(List<String>* p_extensions) const {
13-
p_extensions->push_back("jar");
14-
p_extensions->push_back("dex");
14+
PackedStringArray JavaArchiveFormatLoader::_get_recognized_extensions() const {
15+
PackedStringArray recognized_extensions;
16+
recognized_extensions.push_back("jar");
17+
recognized_extensions.push_back("dex");
18+
return recognized_extensions;
1519
}
1620

17-
String JavaArchiveFormatLoader::get_resource_type(const String& p_path) const {
21+
String JavaArchiveFormatLoader::_get_resource_type(const String& p_path) const {
1822
String ext = p_path.get_extension().to_lower();
1923
if(ext == "jar" || ext == "dex"){ return "JavaArchive";}
2024
return "";
2125
}
2226

23-
bool JavaArchiveFormatLoader::handles_type(const String& p_type) const {
24-
return p_type == "JavaArchive";
27+
bool JavaArchiveFormatLoader::_handles_type(const StringName& p_type) const {
28+
return p_type == SNAME("JavaArchive");
2529
}
2630

27-
Ref<Resource> JavaArchiveFormatLoader::load(
31+
Variant JavaArchiveFormatLoader::_load(
2832
const String& p_path,
2933
const String& p_original_path,
30-
Error* r_error,
3134
bool p_use_sub_threads,
32-
float* r_progress,
33-
ResourceFormatLoader::CacheMode p_cache_mode
34-
) {
35+
int32_t p_cache_mode
36+
) const {
3537
JVM_LOG_VERBOSE(vformat("Loading Java Archive at: %s", p_path));
3638
Ref<JavaArchive> ref;
3739
ref.instantiate();
@@ -44,16 +46,12 @@ Ref<Resource> JavaArchiveFormatLoader::load(
4446
return ref;
4547
}
4648

47-
ResourceUID::ID JavaArchiveFormatLoader::get_resource_uid(const String& p_path) const {
49+
int64_t JavaArchiveFormatLoader::_get_resource_uid(const String& p_path) const {
4850
String ext = p_path.get_extension().to_lower();
49-
ResourceUID::ID id = ResourceUID::INVALID_ID;
51+
int64_t id = ResourceUID::INVALID_ID;
5052
if(ext == "jar" || ext == "dex"){
51-
id = (p_path + UUID_HASH_SEED).hash64();
53+
id = (int64_t) hash64(p_path + UUID_HASH_SEED);
5254
id &= 0x7FFFFFFFFFFFFFFF;
5355
}
5456
return id;
55-
}
56-
57-
bool JavaArchiveFormatLoader::has_custom_uid_support() const {
58-
return true;
59-
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#ifndef GODOT_JVM_JAVA_ARCHIVE_LOADER_H
22
#define GODOT_JVM_JAVA_ARCHIVE_LOADER_H
33

4-
#include <core/io/resource_loader.h>
4+
#include <classes/resource.hpp>
5+
#include <classes/resource_format_loader.hpp>
6+
#include <templates/list.hpp>
7+
#include <variant/string.hpp>
8+
59
namespace godot {
610
class JavaArchiveFormatLoader : public ResourceFormatLoader {
711
public:
@@ -10,12 +14,11 @@ namespace godot {
1014
JavaArchiveFormatLoader(const JavaArchiveFormatLoader&) = delete;
1115
void operator=(const JavaArchiveFormatLoader&) = delete;
1216

13-
void get_recognized_extensions(List<String>* p_extensions) const override;
14-
String get_resource_type(const String& p_path) const override;
15-
bool handles_type(const String& p_type) const override;
16-
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;
17-
bool has_custom_uid_support() const override;
18-
ResourceUID::ID get_resource_uid(const String& p_path) const override;
17+
PackedStringArray _get_recognized_extensions() const override;
18+
String _get_resource_type(const String& p_path) const override;
19+
bool _handles_type(const StringName& p_type) const override;
20+
Variant _load(const String& p_path, const String& p_original_path, bool p_use_sub_threads, int32_t p_cache_mode) const override;
21+
int64_t _get_resource_uid(const String& p_path) const override;
1922
};
2023
}
2124
#endif// GODOT_JVM_JAVA_ARCHIVE_LOADER_H
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
#include "jvm_resource_format_loader.h"
22

3-
#include "hash.h"
43
#include "api/language/names.h"
54
#include "api/script/jvm_script.h"
65
#include "api/script/jvm_script_manager.h"
76
#include "api/script/language/gdj_script.h"
87
#include "api/script/language/java_script.h"
98
#include "api/script/language/kotlin_script.h"
109
#include "api/script/language/scala_script.h"
10+
#include <classes/file_access.hpp>
11+
#include <classes/resource_uid.hpp>
12+
#include "engine/utilities.h"
13+
#include "hash.h"
1114

1215
using namespace godot;
1316

14-
void JvmResourceFormatLoader::get_recognized_extensions(List<String>* p_extensions) const {
15-
p_extensions->push_back(GODOT_JVM_REGISTRATION_FILE_EXTENSION);
16-
p_extensions->push_back(GODOT_KOTLIN_SCRIPT_EXTENSION);
17-
p_extensions->push_back(GODOT_JAVA_SCRIPT_EXTENSION);
18-
p_extensions->push_back(GODOT_SCALA_SCRIPT_EXTENSION);
17+
PackedStringArray JvmResourceFormatLoader::_get_recognized_extensions() const {
18+
PackedStringArray extensions;
19+
extensions.push_back(GODOT_JVM_REGISTRATION_FILE_EXTENSION);
20+
extensions.push_back(GODOT_KOTLIN_SCRIPT_EXTENSION);
21+
extensions.push_back(GODOT_JAVA_SCRIPT_EXTENSION);
22+
extensions.push_back(GODOT_SCALA_SCRIPT_EXTENSION);
23+
return extensions;
1924
}
2025

21-
String JvmResourceFormatLoader::get_resource_type(const String& p_path) const {
26+
String JvmResourceFormatLoader::_get_resource_type(const String& p_path) const {
2227
String ext = p_path.get_extension().to_lower();
2328

2429
if (ext == GODOT_JVM_REGISTRATION_FILE_EXTENSION) {
@@ -33,22 +38,22 @@ String JvmResourceFormatLoader::get_resource_type(const String& p_path) const {
3338
return "";
3439
}
3540

36-
bool JvmResourceFormatLoader::handles_type(const String& p_type) const {
37-
return p_type == "Script"
38-
|| p_type == GODOT_JVM_SCRIPT_NAME
39-
|| p_type == GODOT_KOTLIN_SCRIPT_NAME
40-
|| p_type == GODOT_JAVA_SCRIPT_NAME
41-
|| p_type == GODOT_SCALA_SCRIPT_NAME;
41+
bool JvmResourceFormatLoader::_handles_type(const StringName& p_type) const {
42+
return p_type == SNAME("Script")
43+
|| p_type == SNAME(GODOT_JVM_SCRIPT_NAME)
44+
|| p_type == SNAME(GODOT_KOTLIN_SCRIPT_NAME)
45+
|| p_type == SNAME(GODOT_JAVA_SCRIPT_NAME)
46+
|| p_type == SNAME(GODOT_SCALA_SCRIPT_NAME);
4247
}
4348

4449
Error JvmResourceFormatLoader::read_all_file_utf8(const String& p_path, String& r_content) {
4550
Vector<uint8_t> source_file;
46-
Error err;
47-
Ref<FileAccess> file_access {FileAccess::open(p_path, FileAccess::READ, &err)};
51+
Ref<FileAccess> file_access {FileAccess::open(p_path, FileAccess::READ)};
52+
Error err = FileAccess::get_open_error();
4853
JVM_ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'.");
4954

5055
uint64_t len = file_access->get_length();
51-
source_file.resize(len + 1);
56+
source_file.resize((int64_t) len + 1);
5257
uint8_t* w = source_file.ptrw();
5358
uint64_t r = file_access->get_buffer(w, len);
5459
ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN);
@@ -61,7 +66,7 @@ Error JvmResourceFormatLoader::read_all_file_utf8(const String& p_path, String&
6166
return OK;
6267
}
6368

64-
Ref<Resource> JvmResourceFormatLoader::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) {
69+
Variant JvmResourceFormatLoader::_load(const String& p_path, const String& p_original_path, bool p_use_sub_threads, int32_t p_cache_mode) {
6570
Ref<JvmScript> jvm_script;
6671

6772
String extension = p_path.get_extension();
@@ -79,40 +84,30 @@ Ref<Resource> JvmResourceFormatLoader::load(const String& p_path, const String&
7984
} else if (extension == GODOT_SCALA_SCRIPT_EXTENSION) {
8085
jvm_script = JvmScriptManager::get_instance()->get_or_create_source_script<ScalaScript>(p_path, &script_is_new, r_error);
8186
} else {
82-
if (r_error) { *r_error = Error::ERR_FILE_UNRECOGNIZED; }
8387
return nullptr;
8488
}
8589

86-
if (jvm_script.is_valid()) {
8790
#ifdef TOOLS_ENABLED
88-
if (!script_is_new && is_source) {
89-
MessageQueue::get_singleton()->push_callable(
90-
callable_mp(JvmScriptManager::get_instance(), &JvmScriptManager::invalidate_source).bind(Ref<SourceScript>(jvm_script))
91-
);
92-
}
93-
#endif
94-
} else {
95-
if (r_error) { *r_error = Error::ERR_UNAVAILABLE; }
91+
if (jvm_script.is_valid() && !script_is_new && is_source) {
92+
callable_mp(JvmScriptManager::get_instance(), &JvmScriptManager::invalidate_source).bind(Ref<SourceScript>(jvm_script))
93+
.call_deferred();
9694
}
95+
#endif
9796

9897
return jvm_script;
9998
}
10099

101-
ResourceUID::ID JvmResourceFormatLoader::get_resource_uid(const String& p_path) const {
100+
int64_t JvmResourceFormatLoader::_get_resource_uid(const String& p_path) const {
102101
String extension = p_path.get_extension();
103-
ResourceUID::ID id = ResourceUID::INVALID_ID;
102+
int64_t id = ResourceUID::INVALID_ID;
104103
if (extension == GODOT_JVM_REGISTRATION_FILE_EXTENSION) {
105-
id = (JvmScript::get_script_file_name(p_path) + UUID_HASH_SEED).hash64();
104+
id = (int64_t) hash64(JvmScript::get_script_file_name(p_path) + UUID_HASH_SEED);
106105
id &= 0x7FFFFFFFFFFFFFFF;
107106
} else if (extension == GODOT_KOTLIN_SCRIPT_EXTENSION || extension == GODOT_JAVA_SCRIPT_EXTENSION || extension == GODOT_SCALA_SCRIPT_EXTENSION) {
108107
String source;
109108
Error error;
110-
id = (String(SourceScript::parse_source_to_fqdn(p_path, source, &error)) + UUID_HASH_SEED).hash64();
109+
id = (int64_t) hash64(String(SourceScript::parse_source_to_fqdn(p_path, source, &error)) + UUID_HASH_SEED);
111110
id &= 0x7FFFFFFFFFFFFFFF;
112111
}
113112
return id;
114-
}
115-
116-
bool JvmResourceFormatLoader::has_custom_uid_support() const {
117-
return true;
118-
}
113+
}

src/api/resource_format/jvm_resource_format_loader.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#ifndef GODOT_JVM_KT_RESOURCE_LOADER_H
22
#define GODOT_JVM_KT_RESOURCE_LOADER_H
33

4-
#include <core/io/resource_loader.h>
4+
#include <classes/resource_format_loader.hpp>
5+
56
namespace godot {
67
class JvmResourceFormatLoader : public ResourceFormatLoader {
78
public:
@@ -10,12 +11,11 @@ namespace godot {
1011
JvmResourceFormatLoader(const JvmResourceFormatLoader&) = delete;
1112
void operator=(const JvmResourceFormatLoader&) = delete;
1213

13-
void get_recognized_extensions(List<String>* p_extensions) const override;
14-
String get_resource_type(const String& p_path) const override;
15-
bool handles_type(const String& p_type) const override;
16-
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;
17-
bool has_custom_uid_support() const override;
18-
ResourceUID::ID get_resource_uid(const String& p_path) const override;
14+
PackedStringArray _get_recognized_extensions() const override;
15+
String _get_resource_type(const String& p_path) const override;
16+
bool _handles_type(const StringName& p_type) const override;
17+
Variant _load(const String& p_path, const String& p_original_path, bool p_use_sub_threads, int32_t p_cache_mode) override;
18+
int64_t _get_resource_uid(const String& p_path) const override;
1919

2020
static Error read_all_file_utf8(const String& p_path, String& r_content);
2121
};

src/api/resource_format/jvm_resource_format_saver.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@
33
#include "api/language/names.h"
44
#include "api/script/jvm_script.h"
55
#include "api/script/jvm_script_manager.h"
6+
#include <classes/file_access.hpp>
67

78
using namespace godot;
89

9-
void JvmResourceFormatSaver::get_recognized_extensions(const Ref<Resource>& p_resource, List<String>* p_extensions) const {
10-
if (recognize(p_resource)) {
11-
p_extensions->push_back(GODOT_KOTLIN_SCRIPT_EXTENSION);
12-
p_extensions->push_back(GODOT_JVM_REGISTRATION_FILE_EXTENSION);
13-
p_extensions->push_back(GODOT_JAVA_SCRIPT_EXTENSION);
14-
}
10+
PackedStringArray JvmResourceFormatSaver::_get_recognized_extensions(const Ref<Resource>& p_resource) const {
11+
PackedStringArray extensions;
12+
extensions.push_back(GODOT_KOTLIN_SCRIPT_EXTENSION);
13+
extensions.push_back(GODOT_JVM_REGISTRATION_FILE_EXTENSION);
14+
extensions.push_back(GODOT_JAVA_SCRIPT_EXTENSION);
15+
return extensions;
1516
}
1617

17-
bool JvmResourceFormatSaver::recognize(const Ref<Resource>& p_resource) const {
18+
bool JvmResourceFormatSaver::_recognize(const Ref<Resource>& p_resource) const {
1819
return Object::cast_to<JvmScript>(p_resource.ptr()) != nullptr;
1920
}
2021

21-
Error JvmResourceFormatSaver::save(const Ref<Resource>& p_resource, const String& p_path, uint32_t p_flags) {
22+
Error JvmResourceFormatSaver::_save(const Ref<Resource>& p_resource, const String& p_path, uint32_t p_flags) {
2223
Ref<JvmScript> jvm_script = p_resource;
2324
ERR_FAIL_COND_V(jvm_script.is_null(), ERR_INVALID_PARAMETER);
2425

2526
String extension = p_path.get_extension();
26-
if (!FileAccess::exists(p_path) && extension == GODOT_JVM_REGISTRATION_FILE_EXTENSION) {
27+
if (!FileAccess::file_exists(p_path) && extension == GODOT_JVM_REGISTRATION_FILE_EXTENSION) {
2728
JVM_LOG_WARNING("It's not recommended to create .gdj files directly as they are generated automatically from "
2829
"jvm source files "
2930
"when building your project.\n"
@@ -32,8 +33,8 @@ Error JvmResourceFormatSaver::save(const Ref<Resource>& p_resource, const String
3233
}
3334

3435
{
35-
Error err;
36-
Ref<FileAccess> file {FileAccess::open(p_path, FileAccess::WRITE, &err)};
36+
Ref<FileAccess> file {FileAccess::open(p_path, FileAccess::WRITE)};
37+
Error err = FileAccess::get_open_error();
3738
JVM_ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save Script file '" + p_path + "'.");
3839
file->store_string(jvm_script->get_source_code());
3940

@@ -42,9 +43,8 @@ Error JvmResourceFormatSaver::save(const Ref<Resource>& p_resource, const String
4243

4344
#ifdef TOOLS_ENABLED
4445
if (extension == GODOT_KOTLIN_SCRIPT_EXTENSION || extension == GODOT_JAVA_SCRIPT_EXTENSION) {
45-
MessageQueue::get_singleton()->push_callable(
46-
callable_mp(JvmScriptManager::get_instance(), &JvmScriptManager::invalidate_source).bind(Ref<SourceScript>(jvm_script))
47-
);
46+
callable_mp(JvmScriptManager::get_instance(), &JvmScriptManager::invalidate_source).bind(Ref<SourceScript>(jvm_script))
47+
.call_deferred();
4848
}
4949
#endif
5050

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef GODOT_JVM_JVM_RESOURCE_FORMAT_SAVER_H
22
#define GODOT_JVM_JVM_RESOURCE_FORMAT_SAVER_H
33

4-
#include <core/io/resource_saver.h>
4+
#include <classes/resource_format_saver.hpp>
55

66
namespace godot {
77
class JvmResourceFormatSaver : public ResourceFormatSaver {
@@ -10,9 +10,9 @@ namespace godot {
1010
JvmResourceFormatSaver(const JvmResourceFormatSaver&) = delete;
1111
void operator=(const JvmResourceFormatSaver&) = delete;
1212

13-
void get_recognized_extensions(const Ref<Resource>& p_resource, List<String>* p_extensions) const override;
14-
bool recognize(const Ref<Resource>& p_resource) const override;
15-
Error save(const Ref<Resource>& p_resource, const String& p_path, uint32_t p_flags) override;
13+
PackedStringArray _get_recognized_extensions(const Ref<Resource>& p_resource) const override;
14+
bool _recognize(const Ref<Resource>& p_resource) const override;
15+
Error _save(const Ref<Resource>& p_resource, const String& p_path, uint32_t p_flags) override;
1616
};
1717
}
1818
#endif// GODOT_JVM_JVM_RESOURCE_FORMAT_SAVER_H

0 commit comments

Comments
 (0)