Skip to content

Commit

Permalink
Merge pull request #51970 from reduz/implement-gdvirtuals-everywhere
Browse files Browse the repository at this point in the history
Replace BIND_VMETHOD by new GDVIRTUAL syntax
  • Loading branch information
reduz authored Aug 22, 2021
2 parents 2a5c64f + 3682978 commit a73b5fa
Show file tree
Hide file tree
Showing 104 changed files with 1,347 additions and 1,185 deletions.
1 change: 1 addition & 0 deletions core/core_constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ void register_global_constants() {
BIND_CORE_ENUM_CONSTANT(METHOD_FLAG_VIRTUAL);
BIND_CORE_ENUM_CONSTANT(METHOD_FLAG_FROM_SCRIPT);
BIND_CORE_ENUM_CONSTANT(METHOD_FLAG_STATIC);
BIND_CORE_ENUM_CONSTANT(METHOD_FLAG_OBJECT_CORE);
BIND_CORE_ENUM_CONSTANT(METHOD_FLAGS_DEFAULT);

BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_NIL", Variant::NIL);
Expand Down
2 changes: 1 addition & 1 deletion core/extension/extension_api_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() {
ClassDB::get_method_list(class_name, &method_list, true);
for (const MethodInfo &F : method_list) {
StringName method_name = F.name;
if (F.flags & METHOD_FLAG_VIRTUAL) {
if ((F.flags & METHOD_FLAG_VIRTUAL) && !(F.flags & METHOD_FLAG_OBJECT_CORE)) {
//virtual method
const MethodInfo &mi = F;
Dictionary d2;
Expand Down
9 changes: 4 additions & 5 deletions core/io/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,8 @@ Node *Resource::get_local_scene() const {
}

void Resource::setup_local_to_scene() {
if (get_script_instance()) {
get_script_instance()->call("_setup_local_to_scene");
}
// Can't use GDVIRTUAL in Resource, so this will have to be done with a signal
emit_signal(SNAME("setup_local_to_scene_requested"));
}

Node *(*Resource::_get_local_scene_func)() = nullptr;
Expand Down Expand Up @@ -422,12 +421,12 @@ void Resource::_bind_methods() {

ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
ADD_SIGNAL(MethodInfo("changed"));
ADD_SIGNAL(MethodInfo("setup_local_to_scene_requested"));

ADD_GROUP("Resource", "resource_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path");
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "resource_name"), "set_name", "get_name");

BIND_VMETHOD(MethodInfo("_setup_local_to_scene"));
}

Resource::Resource() :
Expand Down
88 changes: 42 additions & 46 deletions core/io/resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,28 @@ bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_
}

bool ResourceFormatLoader::handles_type(const String &p_type) const {
if (get_script_instance() && get_script_instance()->has_method("_handles_type")) {
// I guess custom loaders for custom resources should use "Resource"
return get_script_instance()->call("_handles_type", p_type);
bool success;
if (GDVIRTUAL_CALL(_handles_type, p_type, success)) {
return success;
}

return false;
}

String ResourceFormatLoader::get_resource_type(const String &p_path) const {
if (get_script_instance() && get_script_instance()->has_method("_get_resource_type")) {
return get_script_instance()->call("_get_resource_type", p_path);
String ret;

if (GDVIRTUAL_CALL(_get_resource_type, p_path, ret)) {
return ret;
}

return "";
}

ResourceUID::ID ResourceFormatLoader::get_resource_uid(const String &p_path) const {
if (get_script_instance() && get_script_instance()->has_method("_get_resource_uid")) {
return get_script_instance()->call("_get_resource_uid", p_path);
int64_t uid;
if (GDVIRTUAL_CALL(_get_resource_uid, p_path, uid)) {
return uid;
}

return ResourceUID::INVALID_ID;
Expand All @@ -105,27 +108,26 @@ void ResourceLoader::get_recognized_extensions_for_type(const String &p_type, Li
}

bool ResourceFormatLoader::exists(const String &p_path) const {
bool success;
if (GDVIRTUAL_CALL(_exists, p_path, success)) {
return success;
}
return FileAccess::exists(p_path); //by default just check file
}

void ResourceFormatLoader::get_recognized_extensions(List<String> *p_extensions) const {
if (get_script_instance() && get_script_instance()->has_method("_get_recognized_extensions")) {
PackedStringArray exts = get_script_instance()->call("_get_recognized_extensions");

{
const String *r = exts.ptr();
for (int i = 0; i < exts.size(); ++i) {
p_extensions->push_back(r[i]);
}
PackedStringArray exts;
if (GDVIRTUAL_CALL(_get_recognized_extensions, exts)) {
const String *r = exts.ptr();
for (int i = 0; i < exts.size(); ++i) {
p_extensions->push_back(r[i]);
}
}
}

RES ResourceFormatLoader::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) {
// Check user-defined loader if there's any. Hard fail if it returns an error.
if (get_script_instance() && get_script_instance()->has_method("_load")) {
Variant res = get_script_instance()->call("_load", p_path, p_original_path, p_use_sub_threads, p_cache_mode);

Variant res;
if (GDVIRTUAL_CALL(_load, p_path, p_original_path, p_use_sub_threads, p_cache_mode, res)) {
if (res.get_type() == Variant::INT) { // Error code, abort.
if (r_error) {
*r_error = (Error)res.operator int64_t();
Expand All @@ -143,48 +145,42 @@ RES ResourceFormatLoader::load(const String &p_path, const String &p_original_pa
}

void ResourceFormatLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
if (get_script_instance() && get_script_instance()->has_method("_get_dependencies")) {
PackedStringArray deps = get_script_instance()->call("_get_dependencies", p_path, p_add_types);

{
const String *r = deps.ptr();
for (int i = 0; i < deps.size(); ++i) {
p_dependencies->push_back(r[i]);
}
PackedStringArray deps;
if (GDVIRTUAL_CALL(_get_dependencies, p_path, p_add_types, deps)) {
const String *r = deps.ptr();
for (int i = 0; i < deps.size(); ++i) {
p_dependencies->push_back(r[i]);
}
}
}

Error ResourceFormatLoader::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
if (get_script_instance() && get_script_instance()->has_method("_rename_dependencies")) {
Dictionary deps_dict;
for (Map<String, String>::Element *E = p_map.front(); E; E = E->next()) {
deps_dict[E->key()] = E->value();
}
Dictionary deps_dict;
for (Map<String, String>::Element *E = p_map.front(); E; E = E->next()) {
deps_dict[E->key()] = E->value();
}

int64_t res = get_script_instance()->call("_rename_dependencies", deps_dict);
return (Error)res;
int64_t err;
if (GDVIRTUAL_CALL(_rename_dependencies, p_path, deps_dict, err)) {
return (Error)err;
}

return OK;
}

void ResourceFormatLoader::_bind_methods() {
{
MethodInfo info = MethodInfo(Variant::NIL, "_load", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::STRING, "original_path"), PropertyInfo(Variant::BOOL, "use_sub_threads"), PropertyInfo(Variant::INT, "cache_mode"));
info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
BIND_VMETHOD(info);
}

BIND_VMETHOD(MethodInfo(Variant::PACKED_STRING_ARRAY, "_get_recognized_extensions"));
BIND_VMETHOD(MethodInfo(Variant::BOOL, "_handles_type", PropertyInfo(Variant::STRING_NAME, "typename")));
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_resource_type", PropertyInfo(Variant::STRING, "path")));
BIND_VMETHOD(MethodInfo("_get_dependencies", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::STRING, "add_types")));
BIND_VMETHOD(MethodInfo(Variant::INT, "_rename_dependencies", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::STRING, "renames")));

BIND_ENUM_CONSTANT(CACHE_MODE_IGNORE);
BIND_ENUM_CONSTANT(CACHE_MODE_REUSE);
BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE);

GDVIRTUAL_BIND(_get_recognized_extensions);
GDVIRTUAL_BIND(_handles_type, "type");
GDVIRTUAL_BIND(_get_resource_type, "path");
GDVIRTUAL_BIND(_get_resource_uid, "path");
GDVIRTUAL_BIND(_get_dependencies, "path", "add_types");
GDVIRTUAL_BIND(_rename_dependencies, "path", "renames");
GDVIRTUAL_BIND(_exists, "path");
GDVIRTUAL_BIND(_load, "path", "original_path", "use_sub_threads", "cache_mode");
}

///////////////////////////////////
Expand Down
12 changes: 12 additions & 0 deletions core/io/resource_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#define RESOURCE_LOADER_H

#include "core/io/resource.h"
#include "core/object/gdvirtual.gen.inc"
#include "core/object/script_language.h"
#include "core/os/semaphore.h"
#include "core/os/thread.h"

Expand All @@ -48,6 +50,16 @@ class ResourceFormatLoader : public RefCounted {
protected:
static void _bind_methods();

GDVIRTUAL0RC(Vector<String>, _get_recognized_extensions)
GDVIRTUAL1RC(bool, _handles_type, StringName)
GDVIRTUAL1RC(String, _get_resource_type, String)
GDVIRTUAL1RC(ResourceUID::ID, _get_resource_uid, String)
GDVIRTUAL2RC(Vector<String>, _get_dependencies, String, bool)
GDVIRTUAL2RC(int64_t, _rename_dependencies, String, Dictionary)
GDVIRTUAL1RC(bool, _exists, String)

GDVIRTUAL4RC(Variant, _load, String, String, bool, int)

public:
virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
virtual bool exists(const String &p_path) const;
Expand Down
35 changes: 14 additions & 21 deletions core/io/resource_saver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,44 +42,37 @@ ResourceSavedCallback ResourceSaver::save_callback = nullptr;
ResourceSaverGetResourceIDForPath ResourceSaver::save_get_id_for_path = nullptr;

Error ResourceFormatSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
if (get_script_instance() && get_script_instance()->has_method("_save")) {
return (Error)get_script_instance()->call("_save", p_path, p_resource, p_flags).operator int64_t();
int64_t res;
if (GDVIRTUAL_CALL(_save, p_path, p_resource, p_flags, res)) {
return (Error)res;
}

return ERR_METHOD_NOT_FOUND;
}

bool ResourceFormatSaver::recognize(const RES &p_resource) const {
if (get_script_instance() && get_script_instance()->has_method("_recognize")) {
return get_script_instance()->call("_recognize", p_resource);
bool success;
if (GDVIRTUAL_CALL(_recognize, p_resource, success)) {
return success;
}

return false;
}

void ResourceFormatSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
if (get_script_instance() && get_script_instance()->has_method("_get_recognized_extensions")) {
PackedStringArray exts = get_script_instance()->call("_get_recognized_extensions", p_resource);

{
const String *r = exts.ptr();
for (int i = 0; i < exts.size(); ++i) {
p_extensions->push_back(r[i]);
}
PackedStringArray exts;
if (GDVIRTUAL_CALL(_get_recognized_extensions, p_resource, exts)) {
const String *r = exts.ptr();
for (int i = 0; i < exts.size(); ++i) {
p_extensions->push_back(r[i]);
}
}
}

void ResourceFormatSaver::_bind_methods() {
{
PropertyInfo arg0 = PropertyInfo(Variant::STRING, "path");
PropertyInfo arg1 = PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
PropertyInfo arg2 = PropertyInfo(Variant::INT, "flags");
BIND_VMETHOD(MethodInfo(Variant::INT, "_save", arg0, arg1, arg2));
}

BIND_VMETHOD(MethodInfo(Variant::PACKED_STRING_ARRAY, "_get_recognized_extensions", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
BIND_VMETHOD(MethodInfo(Variant::BOOL, "_recognize", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
GDVIRTUAL_BIND(_save, "path", "resource", "flags");
GDVIRTUAL_BIND(_recognize, "resource");
GDVIRTUAL_BIND(_get_recognized_extensions, "resource");
}

Error ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
Expand Down
6 changes: 6 additions & 0 deletions core/io/resource_saver.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@
#define RESOURCE_SAVER_H

#include "core/io/resource.h"
#include "core/object/gdvirtual.gen.inc"
#include "core/object/script_language.h"

class ResourceFormatSaver : public RefCounted {
GDCLASS(ResourceFormatSaver, RefCounted);

protected:
static void _bind_methods();

GDVIRTUAL3R(int64_t, _save, String, RES, uint32_t)
GDVIRTUAL1RC(bool, _recognize, RES)
GDVIRTUAL1RC(Vector<String>, _get_recognized_extensions, RES)

public:
virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
virtual bool recognize(const RES &p_resource) const;
Expand Down
28 changes: 16 additions & 12 deletions core/math/a_star.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,9 @@ bool AStar::_solve(Point *begin_point, Point *end_point) {
}

real_t AStar::_estimate_cost(int p_from_id, int p_to_id) {
if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost)) {
return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id);
real_t scost;
if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_to_id, scost)) {
return scost;
}

Point *from_point;
Expand All @@ -398,8 +399,9 @@ real_t AStar::_estimate_cost(int p_from_id, int p_to_id) {
}

real_t AStar::_compute_cost(int p_from_id, int p_to_id) {
if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost)) {
return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id);
real_t scost;
if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) {
return scost;
}

Point *from_point;
Expand Down Expand Up @@ -557,8 +559,8 @@ void AStar::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar::get_point_path);
ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar::get_id_path);

BIND_VMETHOD(MethodInfo(Variant::FLOAT, "_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
BIND_VMETHOD(MethodInfo(Variant::FLOAT, "_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
GDVIRTUAL_BIND(_estimate_cost, "from_id", "to_id")
GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
}

AStar::~AStar() {
Expand Down Expand Up @@ -654,8 +656,9 @@ Vector2 AStar2D::get_closest_position_in_segment(const Vector2 &p_point) const {
}

real_t AStar2D::_estimate_cost(int p_from_id, int p_to_id) {
if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost)) {
return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id);
real_t scost;
if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_to_id, scost)) {
return scost;
}

AStar::Point *from_point;
Expand All @@ -670,8 +673,9 @@ real_t AStar2D::_estimate_cost(int p_from_id, int p_to_id) {
}

real_t AStar2D::_compute_cost(int p_from_id, int p_to_id) {
if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost)) {
return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id);
real_t scost;
if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) {
return scost;
}

AStar::Point *from_point;
Expand Down Expand Up @@ -875,6 +879,6 @@ void AStar2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar2D::get_point_path);
ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar2D::get_id_path);

BIND_VMETHOD(MethodInfo(Variant::FLOAT, "_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
BIND_VMETHOD(MethodInfo(Variant::FLOAT, "_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
GDVIRTUAL_BIND(_estimate_cost, "from_id", "to_id")
GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
}
8 changes: 8 additions & 0 deletions core/math/a_star.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
#ifndef A_STAR_H
#define A_STAR_H

#include "core/object/gdvirtual.gen.inc"
#include "core/object/ref_counted.h"
#include "core/object/script_language.h"
#include "core/templates/oa_hash_map.h"

/**
Expand Down Expand Up @@ -122,6 +124,9 @@ class AStar : public RefCounted {
virtual real_t _estimate_cost(int p_from_id, int p_to_id);
virtual real_t _compute_cost(int p_from_id, int p_to_id);

GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t)
GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t)

public:
int get_available_point_id() const;

Expand Down Expand Up @@ -169,6 +174,9 @@ class AStar2D : public RefCounted {
virtual real_t _estimate_cost(int p_from_id, int p_to_id);
virtual real_t _compute_cost(int p_from_id, int p_to_id);

GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t)
GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t)

public:
int get_available_point_id() const;

Expand Down
Loading

0 comments on commit a73b5fa

Please sign in to comment.