Skip to content

Commit

Permalink
Merge pull request #29 from DmitriySalnikov/reloadable
Browse files Browse the repository at this point in the history
Fixed bugs that did not allow working in the future version of Godot 4.2
  • Loading branch information
DmitriySalnikov authored Nov 17, 2023
2 parents 318422b + 723fff0 commit 725c92f
Show file tree
Hide file tree
Showing 20 changed files with 383 additions and 107 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/gdextension_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ jobs:
arch: [arm32, arm64, x86_32, x86_64]
target: [template_debug, template_release]

env:
ANDROID_NDK_ROOT: /usr/local/lib/android/sdk/ndk/23.2.8568313

steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
4 changes: 2 additions & 2 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
"${workspaceFolder}/godot-cpp/gen/include"
],
"defines": [
"_DEBUG",
"DEBUG_ENABLED",
"UNICODE",
"_UNICODE",
"GDEXTENSION_LIBRARY",
"NOMINMAX",
"TYPED_METHOD_BIND"
"TYPED_METHOD_BIND",
"HOT_RELOAD_ENABLED"
],
"cStandard": "c17",
"cppStandard": "c++17",
Expand Down
3 changes: 2 additions & 1 deletion addons/debug_draw_3d/debug_draw_3d.gdextension
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[configuration]

entry_symbol = "debug_draw_3d_library_init"
compatibility_minimum = "4.1"
compatibility_minimum = "4.1.3"
reloadable = false

[dependencies]

Expand Down
2 changes: 1 addition & 1 deletion dev_build_godot_cpp.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set api=

git apply --ignore-space-change --ignore-whitespace ../patches/godot_cpp_exclude_unused_classes.patch
git apply --ignore-space-change --ignore-whitespace ../patches/unity_build.patch
git apply --ignore-space-change --ignore-whitespace ../patches/debug_string.patch
::git apply --ignore-space-change --ignore-whitespace ../patches/debug_string.patch

title win x64 debug dev
scons platform=windows target=editor arch=x86_64 dev_build=yes debug_symbols=yes generate_bindings=yes %api%
Expand Down
6 changes: 3 additions & 3 deletions examples_dd3d/DebugDrawDemoScene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ transform = Transform3D(10.7186, 0, 0, 0, 3.9777, 0, 0, 0, 7.05487, 10.6302, 1.9
[node name="LinePathAnim" type="AnimationPlayer" parent="."]
root_node = NodePath("../LinePath")
autoplay = "New Anim"
libraries = {
"": SubResource("AnimationLibrary_nj4nv")
}
autoplay = "New Anim"
[node name="LinePath" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.67362, -8)
Expand Down Expand Up @@ -451,11 +451,11 @@ transform = Transform3D(0.935992, 0.352021, 0, -0.352021, 0.935992, 0, 0, 0, 1,
target_position = Vector3(0, -3.464, 0)
[node name="AnimationPlayer" type="AnimationPlayer" parent="HitTest"]
autoplay = "New Anim"
playback_process_mode = 0
callback_mode_process = 0
libraries = {
"": SubResource("AnimationLibrary_vh8ml")
}
autoplay = "New Anim"
[node name="LagTest" type="CSGBox3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 10.4482, -1.49353, -0.450473)
Expand Down
61 changes: 51 additions & 10 deletions lib_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import os
import json
from patches import unity_tools
from pathlib import Path
Expand All @@ -11,12 +12,12 @@

def setup_options(env, arguments, gen_help):
from SCons.Variables import Variables, BoolVariable, EnumVariable, PathVariable

opts = Variables([], arguments)

opts.Add(BoolVariable("force_enabled_dd3d", "Keep the rendering code in the release build", False))
opts.Add(BoolVariable("lto", "Link-time optimization", False))
opts.Add(PathVariable("addon_output_dir", "Path to the output directory",
output_dir, PathVariable.PathIsDirCreate))
opts.Add(PathVariable("addon_output_dir", "Path to the output directory", output_dir, PathVariable.PathIsDirCreate))
opts.Update(env)

gen_help(env, opts)
Expand All @@ -38,6 +39,38 @@ def setup_defines_and_flags(env):
env.AppendUnique(LINKFLAGS=["-flto"])


def is_file_locked(file_path):
if not os.path.exists(file_path):
return False
try:
with open(file_path, "a") as f:
pass
except IOError:
return True
return False


def msvc_pdb_rename(env, lib_full_name):
new_name = (Path(env["addon_output_dir"]) / lib_full_name).as_posix()
max_files = 256

onlyfiles = [f for f in os.listdir(Path(env["addon_output_dir"])) if os.path.isfile(os.path.join(Path(env["addon_output_dir"]), f))]
for of in onlyfiles:
if of.endswith(".pdb") and of.startswith(lib_full_name):
try:
os.remove(Path(env["addon_output_dir"]) / of)
except:
pass

pdb_name = ""
for s in range(max_files):
pdb_name = "{}_{}.pdb".format(new_name, s)
if not is_file_locked(pdb_name):
break

env.Append(LINKFLAGS=["/PDB:" + pdb_name])


def get_sources(src):
res = [src_folder + "/" + file for file in src]
res = unity_tools.generate_unity_build(res, "dd3d_")
Expand Down Expand Up @@ -75,14 +108,22 @@ def get_library_object(env, arguments=None, gen_help=None):
if "release" in env["target"] and env["force_enabled_dd3d"]:
additional_tags += ".enabled"

library_full_name = "lib" + lib_name + ".{}.{}.{}{}{}".format(
env["platform"], env["target"], env["arch"], additional_tags,env["SHLIBSUFFIX"])

env.Default(env.SharedLibrary(
target=env.File(Path(env["addon_output_dir"]) / library_full_name),
source=get_sources(src),
SHLIBSUFFIX=env["SHLIBSUFFIX"]
))
library_full_name = "lib{}.{}.{}.{}{}".format(
lib_name, env["platform"], env["target"], env["arch"], additional_tags
)

# using the library with `reloadable = true` and with the debugger block the PDB file,
# so it needs to be renamed to something not blocked
if env.get("is_msvc", False) and env["target"] != "template_release":
msvc_pdb_rename(env, library_full_name)

env.Default(
env.SharedLibrary(
target=env.File(Path(env["addon_output_dir"]) / (library_full_name + env["SHLIBSUFFIX"])),
source=get_sources(src),
SHLIBSUFFIX=env["SHLIBSUFFIX"],
)
)

# Needed for easy reuse of this library in other build scripts
# TODO: not tested at the moment. Probably need some changes in the C++ code
Expand Down
6 changes: 0 additions & 6 deletions src/2d/debug_draw_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ DebugDraw2D::~DebugDraw2D() {
UNASSIGN_SINGLETON(DebugDraw2D);

#ifndef DISABLE_DEBUG_RENDERING
_font.unref();

data_graphs.reset();
grouped_text.reset();

Expand All @@ -84,18 +82,14 @@ DebugDraw2D::~DebugDraw2D() {
custom_canvas->queue_redraw();

if (!IS_EDITOR_HINT()) {
if (UtilityFunctions::is_instance_valid(_canvas_layer))
_canvas_layer->queue_free();
if (UtilityFunctions::is_instance_valid(default_canvas))
default_canvas->queue_free();

_canvas_layer = nullptr;
default_canvas = nullptr;
}
#endif

root_node = nullptr;
config.unref();
}

void DebugDraw2D::process(double delta) {
Expand Down
1 change: 0 additions & 1 deletion src/2d/debug_draw_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class DebugDraw2D : public Object {
DebugDrawManager *root_node = nullptr;

// 2d
CanvasLayer *_canvas_layer = nullptr;
bool _canvas_need_update = true;
Ref<Font> _font;

Expand Down
1 change: 0 additions & 1 deletion src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ DebugDraw3D::~DebugDraw3D() {
dgc.reset();
#endif

config.unref();
root_node = nullptr;
}

Expand Down
10 changes: 0 additions & 10 deletions src/3d/debug_geometry_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ DebugGeometryContainer::~DebugGeometryContainer() {

geometry_pool.clear_pool();
geometry_pool.clear_pool();

RenderingServer *rs = RS();
rs->free_rid(immediate_mesh_storage.instance);
immediate_mesh_storage.mesh.unref();
immediate_mesh_storage.material.unref();

for (auto &i : multi_mesh_storage) {
rs->free_rid(i.instance);
i.mesh.unref();
}
}

void DebugGeometryContainer::CreateMMI(InstanceType type, const String &name, Ref<ArrayMesh> mesh) {
Expand Down
33 changes: 24 additions & 9 deletions src/debug_draw_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ DebugDrawManager *DebugDrawManager::singleton = nullptr;

using namespace godot;

const char *DebugDrawManager::s_extension_unloading = "extension_unloading";

void DebugDrawManager::_bind_methods() {
#define REG_CLASS_NAME DebugDrawManager

// ClassDB::bind_method(D_METHOD(NAMEOF(get_title)), &DebugDrawGraph::get_title);
ClassDB::bind_method(D_METHOD(NAMEOF(_add_to_tree)), &DebugDrawManager::_add_to_tree);
ClassDB::bind_method(D_METHOD(NAMEOF(_integrate_into_engine)), &DebugDrawManager::_integrate_into_engine);
ClassDB::bind_method(D_METHOD(NAMEOF(_on_scene_changed)), &DebugDrawManager::_on_scene_changed);

ClassDB::bind_method(D_METHOD(NAMEOF(clear_all)), &DebugDrawManager::clear_all);

REG_PROP_BOOL(debug_enabled);

ADD_SIGNAL(MethodInfo(s_extension_unloading));

#undef REG_CLASS_NAME
}

Expand Down Expand Up @@ -76,7 +81,7 @@ void DebugDrawManager::_on_scene_changed(bool _is_scene_null) {
#endif
}

void DebugDrawManager::_integrate_into_engine() {
void DebugDrawManager::_add_to_tree() {
// Assigned here because it is created inside the `GDExtension Initialize` function.
// 1. Create in Initialize and assign Singleton
// 2. Call class constructor after Initialize to get default values of properties
Expand All @@ -86,6 +91,11 @@ void DebugDrawManager::_integrate_into_engine() {

SCENE_ROOT()->add_child(this);
SCENE_ROOT()->move_child(this, 0);

// Then wait for `_ready` to continue
}

void DebugDrawManager::_integrate_into_engine() {
set_process(true);

// Need to be call 'deferred'
Expand Down Expand Up @@ -152,19 +162,15 @@ void DebugDrawManager::_integrate_into_engine() {
f->store_string(Utils::get_scene_tree_as_string(res->get_parent()->get_parent()));
*/
} else {
// Create canvas item and canvas layer
auto _canvas_layer = memnew(CanvasLayer);
_canvas_layer->set_layer(64);
debug_draw_2d_singleton->_canvas_layer = _canvas_layer;
set_layer(64);

auto default_canvas = memnew(Control);
default_canvas->set_name("DebugDrawDefaultCanvas");
((Control *)default_canvas)->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
((Control *)default_canvas)->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
debug_draw_2d_singleton->default_canvas = default_canvas;

add_child(_canvas_layer);
_canvas_layer->add_child(default_canvas);
add_child(default_canvas);
}

debug_draw_2d_singleton->set_custom_canvas(debug_draw_2d_singleton->custom_canvas);
Expand Down Expand Up @@ -220,7 +226,7 @@ void DebugDrawManager::init() {
Engine::get_singleton()->register_singleton(NAMEOF(DebugDraw2D), debug_draw_2d_singleton);
Engine::get_singleton()->register_singleton("Dbg2", debug_draw_2d_singleton);

call_deferred(NAMEOF(_integrate_into_engine));
call_deferred(NAMEOF(_add_to_tree));
}

void DebugDrawManager::_process(double delta) {
Expand Down Expand Up @@ -248,10 +254,17 @@ void DebugDrawManager::_process(double delta) {
}
}

void DebugDrawManager::_ready() {
// HACK Call deferred to avoid setting the instance of `config` as a standard parameter value
call_deferred(NAMEOF(_integrate_into_engine));
}

void DebugDrawManager::_exit_tree() {
is_closing = true;

Engine::get_singleton()->unregister_singleton(NAMEOF(DebugDrawManager));
if (Engine::get_singleton()->has_singleton(NAMEOF(DebugDrawManager))) {
Engine::get_singleton()->unregister_singleton(NAMEOF(DebugDrawManager));
}

if (debug_draw_3d_singleton) {
Engine::get_singleton()->unregister_singleton(NAMEOF(DebugDraw3D));
Expand All @@ -266,6 +279,8 @@ void DebugDrawManager::_exit_tree() {
memdelete(debug_draw_2d_singleton);
debug_draw_2d_singleton = nullptr;
}

emit_signal(s_extension_unloading);
}

#ifdef TOOLS_ENABLED
Expand Down
5 changes: 5 additions & 0 deletions src/debug_draw_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ class DebugDrawManager : public CanvasLayer {
void _connect_scene_changed();
void _on_scene_changed(bool _is_scene_null);

void _add_to_tree();
void _integrate_into_engine();

#ifdef TOOLS_ENABLED
void _try_to_update_cs_bindings();
#endif

public:

static const char *s_extension_unloading;

DebugDrawManager();
~DebugDrawManager();

Expand All @@ -49,5 +53,6 @@ class DebugDrawManager : public CanvasLayer {

void init();
virtual void _process(double delta) override;
virtual void _ready() override;
virtual void _exit_tree() override;
};
7 changes: 5 additions & 2 deletions src/dev_debug_draw_3d_Library.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<WarningLevel>Level3</WarningLevel>
<SDLCheck>false</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<PreprocessorDefinitions>WIN64;NOMINMAX;TYPED_METHOD_BIND;DEBUG_ENABLED;GDEXTENSION_LIBRARY;TOOLS_ENABLED;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN64;NOMINMAX;TYPED_METHOD_BIND;HOT_RELOAD_ENABLED;DEBUG_ENABLED;GDEXTENSION_LIBRARY;TOOLS_ENABLED;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<ExceptionHandling>Sync</ExceptionHandling>
<WholeProgramOptimization>true</WholeProgramOptimization>
Expand All @@ -111,14 +111,15 @@
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<SuppressStartupBanner>true</SuppressStartupBanner>
<IgnoreSpecificDefaultLibraries>%(IgnoreSpecificDefaultLibraries)</IgnoreSpecificDefaultLibraries>
<ProgramDatabaseFile>$(OutDir)$(TargetName)_vs.pdb</ProgramDatabaseFile>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='editor_dev|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>false</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<PreprocessorDefinitions>WIN64;NOMINMAX;TYPED_METHOD_BIND;DEBUG_ENABLED;GDEXTENSION_LIBRARY;TOOLS_ENABLED;DEV_ENABLED;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN64;NOMINMAX;TYPED_METHOD_BIND;HOT_RELOAD_ENABLED;DEBUG_ENABLED;GDEXTENSION_LIBRARY;TOOLS_ENABLED;DEV_ENABLED;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<ExceptionHandling>Sync</ExceptionHandling>
<WholeProgramOptimization>
Expand All @@ -139,6 +140,7 @@
<LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
<SuppressStartupBanner>true</SuppressStartupBanner>
<IgnoreSpecificDefaultLibraries>%(IgnoreSpecificDefaultLibraries)</IgnoreSpecificDefaultLibraries>
<ProgramDatabaseFile>$(OutDir)$(TargetName)_vs.pdb</ProgramDatabaseFile>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='template_release|x64'">
Expand Down Expand Up @@ -166,6 +168,7 @@
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
<IgnoreSpecificDefaultLibraries>%(IgnoreSpecificDefaultLibraries)</IgnoreSpecificDefaultLibraries>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<ProgramDatabaseFile>$(OutDir)$(TargetName)_vs.pdb</ProgramDatabaseFile>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
Loading

0 comments on commit 725c92f

Please sign in to comment.