From d8ff69d53c239fca97f212791ec38022a9a7913c Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Wed, 6 Sep 2023 21:02:52 +0200 Subject: [PATCH] Extract ScriptInstance to simplify includes This allows to include script_instance.h directly in the generated gdvirtual.gen.inc, and remove excessive includes from the codebase. This should also allow Resource to use GDVIRTUAL macros, which wasn't possible previously due to a circular dependency. --- core/debugger/engine_profiler.h | 4 +- core/io/packet_peer.h | 1 - core/io/resource_format_binary.cpp | 1 + core/io/resource_loader.cpp | 1 + core/io/resource_loader.h | 1 - core/io/resource_saver.h | 1 - core/io/stream_peer.h | 1 - core/math/a_star.h | 1 - core/math/a_star_grid_2d.h | 1 - core/object/make_virtuals.py | 2 + core/object/script_instance.cpp | 71 ++++++++++++++ core/object/script_instance.h | 98 +++++++++++++++++++ core/object/script_language.cpp | 34 ------- core/object/script_language.h | 61 +----------- core/os/main_loop.h | 1 - core/string/translation.h | 1 - core/variant/variant_parser.cpp | 1 + editor/debugger/editor_debugger_node.h | 1 + editor/debugger/script_editor_debugger.h | 1 + editor/editor_script.cpp | 1 + editor/editor_script.h | 2 +- editor/editor_translation_parser.h | 1 - editor/editor_vcs_interface.h | 2 +- editor/gui/scene_tree_editor.cpp | 1 + editor/import/resource_importer_scene.cpp | 1 + editor/plugin_config_dialog.cpp | 1 + .../animation_blend_tree_editor_plugin.h | 1 + editor/plugins/editor_preview_plugins.cpp | 1 + .../editor_resource_conversion_plugin.h | 1 - .../plugins/editor_resource_tooltip_plugins.h | 1 - ...packed_scene_translation_parser_plugin.cpp | 1 + editor/plugins/script_editor_plugin.h | 1 + editor/rename_dialog.cpp | 2 +- modules/gdscript/gdscript_rpc_callable.cpp | 1 + modules/mono/editor/code_completion.cpp | 1 + modules/multiplayer/scene_cache_interface.h | 1 + .../webrtc/webrtc_data_channel_extension.h | 1 - .../webrtc/webrtc_peer_connection_extension.h | 1 - platform/android/os_android.cpp | 1 + scene/gui/code_edit.h | 1 + scene/gui/rich_text_effect.h | 1 - scene/main/multiplayer_peer.h | 1 - scene/main/node.cpp | 1 + scene/property_utils.cpp | 1 + scene/resources/resource_format_text.cpp | 1 + scene/resources/style_box.h | 1 - scene/resources/syntax_highlighter.h | 1 - servers/audio/audio_effect.h | 1 - servers/audio/audio_stream.h | 1 - .../extensions/physics_server_2d_extension.h | 1 - servers/physics_server_3d.h | 1 - servers/rendering/shader_preprocessor.h | 1 + servers/text/text_server_extension.h | 1 - 53 files changed, 198 insertions(+), 122 deletions(-) create mode 100644 core/object/script_instance.cpp create mode 100644 core/object/script_instance.h diff --git a/core/debugger/engine_profiler.h b/core/debugger/engine_profiler.h index f74481c84b0..d3d0021e677 100644 --- a/core/debugger/engine_profiler.h +++ b/core/debugger/engine_profiler.h @@ -31,10 +31,8 @@ #ifndef ENGINE_PROFILER_H #define ENGINE_PROFILER_H -#include "core/object/ref_counted.h" - #include "core/object/gdvirtual.gen.inc" -#include "core/object/script_language.h" +#include "core/object/ref_counted.h" class EngineProfiler : public RefCounted { GDCLASS(EngineProfiler, RefCounted); diff --git a/core/io/packet_peer.h b/core/io/packet_peer.h index 7383ab84df0..86ebe32cb6f 100644 --- a/core/io/packet_peer.h +++ b/core/io/packet_peer.h @@ -37,7 +37,6 @@ #include "core/extension/ext_wrappers.gen.inc" #include "core/object/gdvirtual.gen.inc" -#include "core/object/script_language.h" #include "core/variant/native_ptr.h" class PacketPeer : public RefCounted { diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 551d3268b8c..ea97e5ecce3 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -36,6 +36,7 @@ #include "core/io/image.h" #include "core/io/marshalls.h" #include "core/io/missing_resource.h" +#include "core/object/script_language.h" #include "core/version.h" //#define print_bl(m_what) print_line(m_what) diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index df0253349c1..c1a38f0af80 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -33,6 +33,7 @@ #include "core/config/project_settings.h" #include "core/io/file_access.h" #include "core/io/resource_importer.h" +#include "core/object/script_language.h" #include "core/os/condition_variable.h" #include "core/os/os.h" #include "core/string/print_string.h" diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index 2701caa3f47..0c7d6c0feb2 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -33,7 +33,6 @@ #include "core/io/resource.h" #include "core/object/gdvirtual.gen.inc" -#include "core/object/script_language.h" #include "core/object/worker_thread_pool.h" #include "core/os/semaphore.h" #include "core/os/thread.h" diff --git a/core/io/resource_saver.h b/core/io/resource_saver.h index 572742d1294..4828df297ae 100644 --- a/core/io/resource_saver.h +++ b/core/io/resource_saver.h @@ -33,7 +33,6 @@ #include "core/io/resource.h" #include "core/object/gdvirtual.gen.inc" -#include "core/object/script_language.h" class ResourceFormatSaver : public RefCounted { GDCLASS(ResourceFormatSaver, RefCounted); diff --git a/core/io/stream_peer.h b/core/io/stream_peer.h index ba11144e335..29cdb826151 100644 --- a/core/io/stream_peer.h +++ b/core/io/stream_peer.h @@ -35,7 +35,6 @@ #include "core/extension/ext_wrappers.gen.inc" #include "core/object/gdvirtual.gen.inc" -#include "core/object/script_language.h" #include "core/variant/native_ptr.h" class StreamPeer : public RefCounted { diff --git a/core/math/a_star.h b/core/math/a_star.h index fc4bb09f03d..0758500c8a0 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -33,7 +33,6 @@ #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" /** diff --git a/core/math/a_star_grid_2d.h b/core/math/a_star_grid_2d.h index dd5f9d0575b..ecc9bb01f93 100644 --- a/core/math/a_star_grid_2d.h +++ b/core/math/a_star_grid_2d.h @@ -33,7 +33,6 @@ #include "core/object/gdvirtual.gen.inc" #include "core/object/ref_counted.h" -#include "core/object/script_language.h" #include "core/templates/list.h" #include "core/templates/local_vector.h" diff --git a/core/object/make_virtuals.py b/core/object/make_virtuals.py index 5be9650b324..38682d6d928 100644 --- a/core/object/make_virtuals.py +++ b/core/object/make_virtuals.py @@ -160,6 +160,8 @@ def run(target, source, env): #ifndef GDVIRTUAL_GEN_H #define GDVIRTUAL_GEN_H +#include "core/object/script_instance.h" + """ diff --git a/core/object/script_instance.cpp b/core/object/script_instance.cpp new file mode 100644 index 00000000000..303b127db16 --- /dev/null +++ b/core/object/script_instance.cpp @@ -0,0 +1,71 @@ +/**************************************************************************/ +/* script_instance.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 "script_instance.h" + +#include "core/object/script_language.h" + +Variant ScriptInstance::call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { + return callp(p_method, p_args, p_argcount, r_error); +} + +void ScriptInstance::get_property_state(List> &state) { + List pinfo; + get_property_list(&pinfo); + for (const PropertyInfo &E : pinfo) { + if (E.usage & PROPERTY_USAGE_STORAGE) { + Pair p; + p.first = E.name; + if (get(p.first, p.second)) { + state.push_back(p); + } + } + } +} + +void ScriptInstance::property_set_fallback(const StringName &, const Variant &, bool *r_valid) { + if (r_valid) { + *r_valid = false; + } +} + +Variant ScriptInstance::property_get_fallback(const StringName &, bool *r_valid) { + if (r_valid) { + *r_valid = false; + } + return Variant(); +} + +const Variant ScriptInstance::get_rpc_config() const { + return get_script()->get_rpc_config(); +} + +ScriptInstance::~ScriptInstance() { +} diff --git a/core/object/script_instance.h b/core/object/script_instance.h new file mode 100644 index 00000000000..df978a25ea2 --- /dev/null +++ b/core/object/script_instance.h @@ -0,0 +1,98 @@ +/**************************************************************************/ +/* script_instance.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 SCRIPT_INSTANCE_H +#define SCRIPT_INSTANCE_H + +#include "core/object/ref_counted.h" + +class Script; +class ScriptLanguage; + +class ScriptInstance { +public: + virtual bool set(const StringName &p_name, const Variant &p_value) = 0; + virtual bool get(const StringName &p_name, Variant &r_ret) const = 0; + virtual void get_property_list(List *p_properties) const = 0; + virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = nullptr) const = 0; + virtual void validate_property(PropertyInfo &p_property) const = 0; + + virtual bool property_can_revert(const StringName &p_name) const = 0; + virtual bool property_get_revert(const StringName &p_name, Variant &r_ret) const = 0; + + virtual Object *get_owner() { return nullptr; } + virtual void get_property_state(List> &state); + + virtual void get_method_list(List *p_list) const = 0; + virtual bool has_method(const StringName &p_method) const = 0; + + virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) = 0; + + template + Variant call(const StringName &p_method, VarArgs... p_args) { + Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported. + const Variant *argptrs[sizeof...(p_args) + 1]; + for (uint32_t i = 0; i < sizeof...(p_args); i++) { + argptrs[i] = &args[i]; + } + Callable::CallError cerr; + return callp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), cerr); + } + + virtual Variant call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error); // implement if language supports const functions + virtual void notification(int p_notification, bool p_reversed = false) = 0; + virtual String to_string(bool *r_valid) { + if (r_valid) { + *r_valid = false; + } + return String(); + } + + //this is used by script languages that keep a reference counter of their own + //you can make make Ref<> not die when it reaches zero, so deleting the reference + //depends entirely from the script + + virtual void refcount_incremented() {} + virtual bool refcount_decremented() { return true; } //return true if it can die + + virtual Ref