-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use custom callables for GDScript static methods
Fixes #79521
- Loading branch information
Showing
4 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/**************************************************************************/ | ||
/* gdscript_static_callable.cpp */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#include "gdscript_static_callable.h" | ||
|
||
#include "gdscript.h" | ||
|
||
#include "core/templates/hashfuncs.h" | ||
|
||
bool GDScriptStaticCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) { | ||
return p_a->hash() == p_b->hash(); | ||
} | ||
|
||
bool GDScriptStaticCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) { | ||
return p_a->hash() < p_b->hash(); | ||
} | ||
|
||
uint32_t GDScriptStaticCallable::hash() const { | ||
return h; | ||
} | ||
|
||
String GDScriptStaticCallable::get_as_text() const { | ||
String class_name = script->get_class(); | ||
if (script->get_path().is_resource_file()) { | ||
class_name += "(" + script->get_path().get_file() + ")"; | ||
} | ||
return class_name + "::" + String(method); | ||
} | ||
|
||
CallableCustom::CompareEqualFunc GDScriptStaticCallable::get_compare_equal_func() const { | ||
return compare_equal; | ||
} | ||
|
||
CallableCustom::CompareLessFunc GDScriptStaticCallable::get_compare_less_func() const { | ||
return compare_less; | ||
} | ||
|
||
bool GDScriptStaticCallable::is_valid() const { | ||
return script.is_valid() && script->has_method(method); | ||
} | ||
|
||
ObjectID GDScriptStaticCallable::get_object() const { | ||
return script->get_instance_id(); | ||
} | ||
|
||
StringName GDScriptStaticCallable::get_method() const { | ||
return method; | ||
} | ||
|
||
void GDScriptStaticCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { | ||
r_return_value = script->callp(method, p_arguments, p_argcount, r_call_error); | ||
} | ||
|
||
GDScriptStaticCallable::GDScriptStaticCallable(const Ref<GDScript> &p_script, const StringName &p_method) { | ||
script = p_script; | ||
method = p_method; | ||
h = method.hash(); | ||
h = hash_murmur3_one_64(script->get_instance_id(), h); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/**************************************************************************/ | ||
/* gdscript_static_callable.h */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#ifndef GDSCRIPT_STATIC_CALLABLE_H | ||
#define GDSCRIPT_STATIC_CALLABLE_H | ||
|
||
#include "core/object/ref_counted.h" | ||
#include "core/variant/callable.h" | ||
#include "core/variant/variant.h" | ||
|
||
class GDScript; | ||
|
||
class GDScriptStaticCallable : public CallableCustom { | ||
Ref<GDScript> script; | ||
StringName method; | ||
uint32_t h = 0; | ||
|
||
static bool compare_equal(const CallableCustom *p_a, const CallableCustom *p_b); | ||
static bool compare_less(const CallableCustom *p_a, const CallableCustom *p_b); | ||
|
||
public: | ||
uint32_t hash() const override; | ||
String get_as_text() const override; | ||
CompareEqualFunc get_compare_equal_func() const override; | ||
CompareLessFunc get_compare_less_func() const override; | ||
bool is_valid() const override; | ||
ObjectID get_object() const override; | ||
StringName get_method() const override; | ||
void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override; | ||
|
||
GDScriptStaticCallable(const Ref<GDScript> &p_script, const StringName &p_method); | ||
virtual ~GDScriptStaticCallable() = default; | ||
}; | ||
|
||
#endif // GDSCRIPT_STATIC_CALLABLE_H |