Compare commits

...

4 Commits

Author SHA1 Message Date
DE YU
0a11563d80
Merge 7e745753fc into 533c616cb8 2024-10-22 22:13:37 +02:00
Clay John
533c616cb8
Merge pull request #98391 from RandomShaper/rd_thread_switch
Some checks are pending
🔗 GHA / 📊 Static checks (push) Waiting to run
🔗 GHA / 🤖 Android (push) Blocked by required conditions
🔗 GHA / 🍏 iOS (push) Blocked by required conditions
🔗 GHA / 🐧 Linux (push) Blocked by required conditions
🔗 GHA / 🍎 macOS (push) Blocked by required conditions
🔗 GHA / 🏁 Windows (push) Blocked by required conditions
🔗 GHA / 🌐 Web (push) Blocked by required conditions
🔗 GHA / 🪲 Godot CPP (push) Blocked by required conditions
Implement thread ownership change for RenderingDevice
2024-10-22 13:10:32 -07:00
Pedro J. Estébanez
d5d509bbd6 Implement thread ownership change for RenderingDevice 2024-10-21 20:56:42 +02:00
DE-YU_H14
7e745753fc Editor: Stop loading dotnet project assembly when TOOLS is not defined to prevent data loss 2024-10-14 05:43:16 +08:00
7 changed files with 59 additions and 11 deletions

View File

@ -52,6 +52,12 @@ namespace GodotPlugins.Game
return false.ToGodotBool();
}
}
#if TOOLS
#pragma warning disable CS0169
private static bool ToolsDefined;
#pragma warning restore CS0169
#endif
}
}
";

View File

@ -131,19 +131,20 @@ namespace GodotPlugins
[StructLayout(LayoutKind.Sequential)]
private struct PluginsCallbacks
{
public unsafe delegate* unmanaged<char*, godot_string*, godot_bool> LoadProjectAssemblyCallback;
public unsafe delegate* unmanaged<char*, godot_string*, godot_string*, godot_bool*, godot_bool> LoadProjectAssemblyCallback;
public unsafe delegate* unmanaged<char*, IntPtr, int, IntPtr> LoadToolsAssemblyCallback;
public unsafe delegate* unmanaged<godot_bool> UnloadProjectPluginCallback;
}
[UnmanagedCallersOnly]
private static unsafe godot_bool LoadProjectAssembly(char* nAssemblyPath, godot_string* outLoadedAssemblyPath)
private static unsafe godot_bool LoadProjectAssembly(char* nAssemblyPath, godot_string* outLoadedAssemblyPath, godot_string* outFailedReason, godot_bool* shouldRetry)
{
try
{
if (_projectLoadContext != null)
return godot_bool.True; // Already loaded
*shouldRetry = godot_bool.False;
string assemblyPath = new(nAssemblyPath);
(var projectAssembly, _projectLoadContext) = LoadPlugin(assemblyPath, isCollectible: _editorHint);
@ -151,6 +152,20 @@ namespace GodotPlugins
string loadedAssemblyPath = _projectLoadContext.AssemblyLoadedPath ?? assemblyPath;
*outLoadedAssemblyPath = Marshaling.ConvertStringToNative(loadedAssemblyPath);
// This generated field is here to ensure that
// the developer do not drop the TOOLS compiler preprocessor
// from their csproj, which breaks the C# Editor Functionality.
var isToolsDefinedField =
projectAssembly
.GetType("GodotPlugins.Game.Main")?
.GetField("ToolsDefined", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
if (isToolsDefinedField == null)
{
*outFailedReason = Marshaling.ConvertStringToNative("The csproj was missing the `TOOLS` compiler symbol, which is required for C# editor functionality. Please modify the csproj file and rebuild the C# project, check https://github.com/godotengine/godot/issues/98124 for more information.");
return godot_bool.False;
}
ScriptManagerBridge.LookupScriptsInAssembly(projectAssembly);
return godot_bool.True;
@ -158,6 +173,7 @@ namespace GodotPlugins
catch (Exception e)
{
Console.Error.WriteLine(e);
*outFailedReason = Marshaling.ConvertStringToNative(e.Message);
return godot_bool.False;
}
}

View File

@ -605,9 +605,15 @@ void GDMono::_try_load_project_assembly() {
// Load the project's main assembly. This doesn't necessarily need to succeed.
// The game may not be using .NET at all, or if the project does use .NET and
// we're running in the editor, it may just happen to be it wasn't built yet.
if (!_load_project_assembly()) {
String error_message;
bool should_retry;
if (!_load_project_assembly(&error_message, &should_retry)) {
if (OS::get_singleton()->is_stdout_verbose()) {
print_error(".NET: Failed to load project assembly");
if (error_message.is_empty()) {
print_error(".NET: Failed to load project assembly");
} else {
print_error(".NET: Failed to load project assembly: " + error_message);
}
}
}
}
@ -624,7 +630,9 @@ void GDMono::_init_godot_api_hashes() {
}
#ifdef TOOLS_ENABLED
bool GDMono::_load_project_assembly() {
bool GDMono::_load_project_assembly(String *p_error_message, bool *p_should_retry) {
*p_should_retry = true;
String assembly_name = path::get_csharp_project_name();
String assembly_path = GodotSharpDirs::get_res_temp_assemblies_dir()
@ -632,11 +640,12 @@ bool GDMono::_load_project_assembly() {
assembly_path = ProjectSettings::get_singleton()->globalize_path(assembly_path);
if (!FileAccess::exists(assembly_path)) {
*p_error_message = String("Assembly Path \"" + assembly_path + "\" does not exist");
return false;
}
String loaded_assembly_path;
bool success = plugin_callbacks.LoadProjectAssemblyCallback(assembly_path.utf16(), &loaded_assembly_path);
bool success = plugin_callbacks.LoadProjectAssemblyCallback(assembly_path.utf16(), &loaded_assembly_path, p_error_message, p_should_retry);
if (success) {
project_assembly_path = loaded_assembly_path.simplify_path();
@ -679,10 +688,19 @@ Error GDMono::reload_project_assemblies() {
// Load the project's main assembly. Here, during hot-reloading, we do
// consider failing to load the project's main assembly to be an error.
if (!_load_project_assembly()) {
ERR_PRINT_ED(".NET: Failed to load project assembly.");
String error_message;
bool should_retry;
if (!_load_project_assembly(&error_message, &should_retry)) {
if (error_message.is_empty()) {
ERR_PRINT_ED(".NET: Failed to load project assembly.");
} else {
ERR_PRINT_ED(".NET: Failed to load project assembly: " + error_message);
}
if (!should_retry) {
project_load_failure_count = (int)GLOBAL_GET("dotnet/project/assembly_reload_attempts");
}
reload_failure();
return ERR_CANT_OPEN;
return FAILED;
}
if (project_load_failure_count > 0) {

View File

@ -47,7 +47,7 @@ namespace gdmono {
#ifdef TOOLS_ENABLED
struct PluginCallbacks {
using FuncLoadProjectAssemblyCallback = bool(GD_CLR_STDCALL *)(const char16_t *, String *);
using FuncLoadProjectAssemblyCallback = bool(GD_CLR_STDCALL *)(const char16_t *, String *, String *, bool *);
using FuncLoadToolsAssemblyCallback = Object *(GD_CLR_STDCALL *)(const char16_t *, const void **, int32_t);
using FuncUnloadProjectPluginCallback = bool(GD_CLR_STDCALL *)();
FuncLoadProjectAssemblyCallback LoadProjectAssemblyCallback = nullptr;
@ -73,7 +73,7 @@ class GDMono {
#endif
#ifdef TOOLS_ENABLED
bool _load_project_assembly();
bool _load_project_assembly(String *p_error_message, bool *p_should_retry);
void _try_load_project_assembly();
#endif

View File

@ -7260,6 +7260,10 @@ void RenderingDevice::_bind_methods() {
BIND_ENUM_CONSTANT(DEBUG_PASS);
}
void RenderingDevice::make_current() {
render_thread_id = Thread::get_caller_id();
}
RenderingDevice::~RenderingDevice() {
finalize();

View File

@ -1496,6 +1496,8 @@ public:
static RenderingDevice *get_singleton();
void make_current();
RenderingDevice();
~RenderingDevice();

View File

@ -370,6 +370,8 @@ Size2i RenderingServerDefault::get_maximum_viewport_size() const {
void RenderingServerDefault::_assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id) {
server_thread = Thread::get_caller_id();
server_task_id = p_pump_task_id;
// This is needed because the main RD is created on the main thread.
RenderingDevice::get_singleton()->make_current();
}
void RenderingServerDefault::_thread_exit() {