diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/mono/SCsub | 139 | ||||
-rw-r--r-- | modules/mono/config.py | 80 | ||||
-rw-r--r-- | modules/mono/csharp_script.cpp | 15 | ||||
-rw-r--r-- | modules/mono/editor/GodotSharpTools/Build/BuildSystem.cs | 66 | ||||
-rw-r--r-- | modules/mono/editor/bindings_generator.cpp | 1152 | ||||
-rw-r--r-- | modules/mono/editor/bindings_generator.h | 7 | ||||
-rw-r--r-- | modules/mono/editor/godotsharp_builds.cpp | 60 | ||||
-rw-r--r-- | modules/mono/editor/godotsharp_builds.h | 9 | ||||
-rw-r--r-- | modules/mono/editor/godotsharp_editor.cpp | 65 | ||||
-rw-r--r-- | modules/mono/editor/godotsharp_editor.h | 23 | ||||
-rw-r--r-- | modules/mono/editor/mono_bottom_panel.cpp | 15 | ||||
-rw-r--r-- | modules/mono/editor/mono_bottom_panel.h | 2 | ||||
-rw-r--r-- | modules/mono/godotsharp_dirs.cpp | 9 | ||||
-rw-r--r-- | modules/mono/mono_gd/gd_mono.cpp | 9 | ||||
-rw-r--r-- | modules/mono/mono_reg_utils.py | 92 | ||||
-rw-r--r-- | modules/opus/audio_stream_opus.cpp | 6 | ||||
-rw-r--r-- | modules/opus/audio_stream_opus.h | 2 | ||||
-rw-r--r-- | modules/vorbis/audio_stream_ogg_vorbis.cpp | 8 | ||||
-rw-r--r-- | modules/vorbis/audio_stream_ogg_vorbis.h | 2 |
19 files changed, 1054 insertions, 707 deletions
diff --git a/modules/mono/SCsub b/modules/mono/SCsub index caf4fdb3ca..13212636e3 100644 --- a/modules/mono/SCsub +++ b/modules/mono/SCsub @@ -53,68 +53,149 @@ if env['tools']: vars = Variables() vars.Add(BoolVariable('mono_glue', 'Build with the mono glue sources', True)) +vars.Add(BoolVariable('xbuild_fallback', 'If MSBuild is not found, fallback to xbuild', False)) vars.Update(env) # Glue sources if env['mono_glue']: env.add_source_files(env.modules_sources, 'glue/*.cpp') else: - env.Append(CPPDEFINES = [ 'MONO_GLUE_DISABLED' ]) + env.Append(CPPDEFINES=['MONO_GLUE_DISABLED']) if ARGUMENTS.get('yolo_copy', False): - env.Append(CPPDEFINES = [ 'YOLO_COPY' ]) + env.Append(CPPDEFINES=['YOLO_COPY']) + # Build GodotSharpTools solution + import os -import subprocess -import mono_reg_utils as monoreg + + +def find_msbuild_unix(filename): + import os.path + import sys + + hint_dirs = ['/opt/novell/mono/bin'] + if sys.platform == "darwin": + hint_dirs = ['/Library/Frameworks/Mono.framework/Versions/Current/bin'] + hint_dirs + + for hint_dir in hint_dirs: + hint_path = os.path.join(hint_dir, filename) + if os.path.isfile(hint_path): + return hint_path + + for hint_dir in os.environ["PATH"].split(os.pathsep): + hint_dir = hint_dir.strip('"') + hint_path = os.path.join(hint_dir, filename) + if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK): + return hint_path + + return None + + +def find_msbuild_windows(): + import mono_reg_utils as monoreg + + msbuild_tools_path = monoreg.find_msbuild_tools_path_reg() + + if msbuild_tools_path: + return (os.path.join(msbuild_tools_path, 'MSBuild.exe'), '') + else: + bits = env['bits'] + + if bits == '32': + if os.getenv('MONO32_PREFIX'): + mono_root = os.getenv('MONO32_PREFIX') + else: + mono_root = monoreg.find_mono_root_dir(bits) + else: + if os.getenv('MONO64_PREFIX'): + mono_root = os.getenv('MONO64_PREFIX') + else: + mono_root = monoreg.find_mono_root_dir(bits) + + if mono_root: + msbuild_mono = os.path.join(mono_root, 'bin', 'msbuild.bat') + + if os.path.isfile(msbuild_mono): + return (msbuild_mono, os.path.join(mono_root, 'lib', 'mono', '4.5')) + + return None def mono_build_solution(source, target, env): + import subprocess + import mono_reg_utils as monoreg + from shutil import copyfile + + framework_path_override = '' + if os.name == 'nt': - msbuild_tools_path = monoreg.find_msbuild_tools_path_reg() - if not msbuild_tools_path: - raise RuntimeError('Cannot find MSBuild Tools Path in the registry') - msbuild_path = os.path.join(msbuild_tools_path, 'MSBuild.exe') + msbuild_info = find_msbuild_windows() + if msbuild_info is None: + raise RuntimeError('Cannot find MSBuild executable') + msbuild_path = msbuild_windows[0] + framework_path_override = msbuild_windows[1] else: - msbuild_path = 'msbuild' + msbuild_path = find_msbuild_unix('msbuild') + if msbuild_path is None: + xbuild_fallback = env['xbuild_fallback'] + + if xbuild_fallback and os.name == 'nt': + print("Option 'xbuild_fallback' not supported on Windows") + xbuild_fallback = False + + if xbuild_fallback: + print('Cannot find MSBuild executable, trying with xbuild') + print('Warning: xbuild is deprecated') + + msbuild_path = find_msbuild_unix('xbuild') + + if msbuild_path is None: + raise RuntimeError('Cannot find xbuild executable') + else: + raise RuntimeError('Cannot find MSBuild executable') + + print('MSBuild path: ' + msbuild_path) - output_path = os.path.abspath(os.path.join(str(target[0]), os.pardir)) + build_config = 'Release' msbuild_args = [ msbuild_path, os.path.abspath(str(source[0])), - '/p:Configuration=Release', - '/p:OutputPath=' + output_path + '/p:Configuration=' + build_config, ] + if framework_path_override: + msbuild_args += ['/p:FrameworkPathOverride=' + framework_path_override] + msbuild_env = os.environ.copy() # Needed when running from Developer Command Prompt for VS if 'PLATFORM' in msbuild_env: del msbuild_env['PLATFORM'] - msbuild_alt_paths = [ 'xbuild' ] - - while True: - try: - subprocess.check_call(msbuild_args, env = msbuild_env) - break - except subprocess.CalledProcessError: - raise RuntimeError('GodotSharpTools build failed') - except OSError: - if os.name != 'nt': - if not msbuild_alt_paths: - raise RuntimeError('Could not find commands msbuild or xbuild') - # Try xbuild - msbuild_args[0] = msbuild_alt_paths.pop(0) - else: - raise RuntimeError('Could not find command MSBuild.exe') + try: + subprocess.check_call(msbuild_args, env=msbuild_env) + except subprocess.CalledProcessError: + raise RuntimeError('GodotSharpTools build failed') + + src_dir = os.path.abspath(os.path.join(str(source[0]), os.pardir, 'bin', build_config)) + dst_dir = os.path.abspath(os.path.join(str(target[0]), os.pardir)) + + if not os.path.isdir(dst_dir): + if os.path.exists(dst_dir): + raise RuntimeError('Target directory is a file') + os.makedirs(dst_dir) + + asm_file = 'GodotSharpTools.dll' + + copyfile(os.path.join(src_dir, asm_file), os.path.join(dst_dir, asm_file)) mono_sln_builder = Builder(action = mono_build_solution) -env.Append(BUILDERS = { 'MonoBuildSolution' : mono_sln_builder }) +env.Append(BUILDERS={'MonoBuildSolution': mono_sln_builder}) env.MonoBuildSolution( os.path.join(Dir('#bin').abspath, 'GodotSharpTools.dll'), 'editor/GodotSharpTools/GodotSharpTools.sln' diff --git a/modules/mono/config.py b/modules/mono/config.py index 0833d30ce1..44eef45f76 100644 --- a/modules/mono/config.py +++ b/modules/mono/config.py @@ -2,7 +2,6 @@ import imp import os import sys -from shutil import copyfile from SCons.Script import BoolVariable, Environment, Variables @@ -16,8 +15,7 @@ def find_file_in_dir(directory, files, prefix='', extension=''): for curfile in files: if os.path.isfile(os.path.join(directory, prefix + curfile + extension)): return curfile - - return None + return '' def can_build(platform): @@ -31,6 +29,22 @@ def is_enabled(): return False +def copy_file_no_replace(src_dir, dst_dir, name): + from shutil import copyfile + + src_path = os.path.join(src_dir, name) + dst_path = os.path.join(dst_dir, name) + need_copy = True + + if not os.path.isdir(dst_dir): + os.mkdir(dst_dir) + elif os.path.exists(dst_path): + need_copy = False + + if need_copy: + copyfile(src_path, dst_path) + + def configure(env): env.use_ptrcall = True @@ -38,6 +52,8 @@ def configure(env): envvars.Add(BoolVariable('mono_static', 'Statically link mono', False)) envvars.Update(env) + bits = env['bits'] + mono_static = env['mono_static'] mono_lib_names = ['mono-2.0-sgen', 'monosgen-2.0'] @@ -46,18 +62,18 @@ def configure(env): if mono_static: raise RuntimeError('mono-static: Not supported on Windows') - if env['bits'] == '32': + if bits == '32': if os.getenv('MONO32_PREFIX'): mono_root = os.getenv('MONO32_PREFIX') elif os.name == 'nt': - mono_root = monoreg.find_mono_root_dir() + mono_root = monoreg.find_mono_root_dir(bits) else: if os.getenv('MONO64_PREFIX'): mono_root = os.getenv('MONO64_PREFIX') elif os.name == 'nt': - mono_root = monoreg.find_mono_root_dir() + mono_root = monoreg.find_mono_root_dir(bits) - if mono_root is None: + if not mono_root: raise RuntimeError('Mono installation directory not found') mono_lib_path = os.path.join(mono_root, 'lib') @@ -67,7 +83,7 @@ def configure(env): mono_lib_name = find_file_in_dir(mono_lib_path, mono_lib_names, extension='.lib') - if mono_lib_name is None: + if not mono_lib_name: raise RuntimeError('Could not find mono library in: ' + mono_lib_path) if os.getenv('VCINSTALLDIR'): @@ -79,28 +95,23 @@ def configure(env): mono_dll_name = find_file_in_dir(mono_bin_path, mono_lib_names, extension='.dll') - mono_dll_src = os.path.join(mono_bin_path, mono_dll_name + '.dll') - mono_dll_dst = os.path.join('bin', mono_dll_name + '.dll') - copy_mono_dll = True - - if not os.path.isdir('bin'): - os.mkdir('bin') - elif os.path.exists(mono_dll_dst): - copy_mono_dll = False + if not mono_dll_name: + raise RuntimeError('Could not find mono shared library in: ' + mono_bin_path) - if copy_mono_dll: - copyfile(mono_dll_src, mono_dll_dst) + copy_file_no_replace(mono_bin_path, 'bin', mono_dll_name + '.dll') else: - mono_root = None + sharedlib_ext = '.dylib' if sys.platform == 'darwin' else '.so' - if env['bits'] == '32': + mono_root = '' + + if bits == '32': if os.getenv('MONO32_PREFIX'): mono_root = os.getenv('MONO32_PREFIX') else: if os.getenv('MONO64_PREFIX'): mono_root = os.getenv('MONO64_PREFIX') - if mono_root is not None: + if mono_root: mono_lib_path = os.path.join(mono_root, 'lib') env.Append(LIBPATH=mono_lib_path) @@ -108,7 +119,7 @@ def configure(env): mono_lib = find_file_in_dir(mono_lib_path, mono_lib_names, prefix='lib', extension='.a') - if mono_lib is None: + if not mono_lib: raise RuntimeError('Could not find mono library in: ' + mono_lib_path) env.Append(CPPFLAGS=['-D_REENTRANT']) @@ -130,12 +141,37 @@ def configure(env): elif sys.platform == "linux" or sys.platform == "linux2": env.Append(LIBS=['m', 'rt', 'dl', 'pthread']) + if not mono_static: + mono_so_name = find_file_in_dir(mono_lib_path, mono_lib_names, prefix='lib', extension=sharedlib_ext) + + if not mono_so_name: + raise RuntimeError('Could not find mono shared library in: ' + mono_lib_path) + + copy_file_no_replace(mono_lib_path, 'bin', 'lib' + mono_so_name + sharedlib_ext) else: if mono_static: raise RuntimeError('mono-static: Not supported with pkg-config. Specify a mono prefix manually') env.ParseConfig('pkg-config monosgen-2 --cflags --libs') + mono_lib_path = '' + mono_so_name = '' + + tmpenv = Environment() + tmpenv.ParseConfig('pkg-config monosgen-2 --libs-only-L') + + for hint_dir in tmpenv['LIBPATH']: + name_found = find_file_in_dir(hint_dir, mono_lib_names, prefix='lib', extension=sharedlib_ext) + if name_found: + mono_lib_path = hint_dir + mono_so_name = name_found + break + + if not mono_so_name: + raise RuntimeError('Could not find mono shared library in: ' + str(tmpenv['LIBPATH'])) + + copy_file_no_replace(mono_lib_path, 'bin', 'lib' + mono_so_name + sharedlib_ext) + env.Append(LINKFLAGS='-rdynamic') diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index 8d4e19469a..3d91a6de6c 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -477,6 +477,7 @@ void CSharpLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft (void)p_script; // UNUSED #ifdef TOOLS_ENABLED + MonoReloadNode::get_singleton()->restart_reload_timer(); reload_assemblies_if_needed(p_soft_reload); #endif } @@ -488,13 +489,17 @@ void CSharpLanguage::reload_assemblies_if_needed(bool p_soft_reload) { GDMonoAssembly *proj_assembly = gdmono->get_project_assembly(); + String name = ProjectSettings::get_singleton()->get("application/config/name"); + if (name.empty()) { + name = "UnnamedProject"; + } + if (proj_assembly) { String proj_asm_path = proj_assembly->get_path(); if (!FileAccess::exists(proj_assembly->get_path())) { // Maybe it wasn't loaded from the default path, so check this as well - String proj_asm_name = ProjectSettings::get_singleton()->get("application/config/name"); - proj_asm_path = GodotSharpDirs::get_res_temp_assemblies_dir().plus_file(proj_asm_name); + proj_asm_path = GodotSharpDirs::get_res_temp_assemblies_dir().plus_file(name); if (!FileAccess::exists(proj_asm_path)) return; // No assembly to load } @@ -502,8 +507,7 @@ void CSharpLanguage::reload_assemblies_if_needed(bool p_soft_reload) { if (FileAccess::get_modified_time(proj_asm_path) <= proj_assembly->get_modified_time()) return; // Already up to date } else { - String proj_asm_name = ProjectSettings::get_singleton()->get("application/config/name"); - if (!FileAccess::exists(GodotSharpDirs::get_res_temp_assemblies_dir().plus_file(proj_asm_name))) + if (!FileAccess::exists(GodotSharpDirs::get_res_temp_assemblies_dir().plus_file(name))) return; // No assembly to load } } @@ -621,6 +625,9 @@ void CSharpLanguage::reload_assemblies_if_needed(bool p_soft_reload) { //if instance states were saved, set them! } + + if (Engine::get_singleton()->is_editor_hint()) + EditorNode::get_singleton()->get_property_editor()->update_tree(); } #endif diff --git a/modules/mono/editor/GodotSharpTools/Build/BuildSystem.cs b/modules/mono/editor/GodotSharpTools/Build/BuildSystem.cs index 5544233eb7..04da0600cc 100644 --- a/modules/mono/editor/GodotSharpTools/Build/BuildSystem.cs +++ b/modules/mono/editor/GodotSharpTools/Build/BuildSystem.cs @@ -4,6 +4,7 @@ using System.Collections.Specialized; using System.Diagnostics; using System.IO; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Security; using Microsoft.Build.Framework; @@ -12,22 +13,27 @@ namespace GodotSharpTools.Build public class BuildInstance : IDisposable { [MethodImpl(MethodImplOptions.InternalCall)] - internal extern static void godot_icall_BuildInstance_ExitCallback(string solution, string config, int exitCode); + private extern static void godot_icall_BuildInstance_ExitCallback(string solution, string config, int exitCode); [MethodImpl(MethodImplOptions.InternalCall)] - internal extern static string godot_icall_BuildInstance_get_MSBuildPath(); + private extern static void godot_icall_BuildInstance_get_MSBuildInfo(ref string msbuildPath, ref string frameworkPath); - private static string MSBuildPath + private struct MSBuildInfo { - get - { - string ret = godot_icall_BuildInstance_get_MSBuildPath(); + public string path; + public string frameworkPathOverride; + } - if (ret == null) - throw new FileNotFoundException("Cannot find the MSBuild executable."); + private static MSBuildInfo GetMSBuildInfo() + { + MSBuildInfo msbuildInfo = new MSBuildInfo(); - return ret; - } + godot_icall_BuildInstance_get_MSBuildInfo(ref msbuildInfo.path, ref msbuildInfo.frameworkPathOverride); + + if (msbuildInfo.path == null) + throw new FileNotFoundException("Cannot find the MSBuild executable."); + + return msbuildInfo; } private string solution; @@ -48,9 +54,19 @@ namespace GodotSharpTools.Build public bool Build(string loggerAssemblyPath, string loggerOutputDir, string[] customProperties = null) { - string compilerArgs = BuildArguments(loggerAssemblyPath, loggerOutputDir, customProperties); + MSBuildInfo msbuildInfo = GetMSBuildInfo(); + + List<string> customPropertiesList = new List<string>(); + + if (customProperties != null) + customPropertiesList.AddRange(customProperties); + + if (msbuildInfo.frameworkPathOverride != null) + customPropertiesList.Add("FrameworkPathOverride=" + msbuildInfo.frameworkPathOverride); - ProcessStartInfo startInfo = new ProcessStartInfo(MSBuildPath, compilerArgs); + string compilerArgs = BuildArguments(loggerAssemblyPath, loggerOutputDir, customPropertiesList); + + ProcessStartInfo startInfo = new ProcessStartInfo(msbuildInfo.path, compilerArgs); // No console output, thanks startInfo.RedirectStandardOutput = true; @@ -82,9 +98,19 @@ namespace GodotSharpTools.Build if (process != null) throw new InvalidOperationException("Already in use"); - string compilerArgs = BuildArguments(loggerAssemblyPath, loggerOutputDir, customProperties); + MSBuildInfo msbuildInfo = GetMSBuildInfo(); + + List<string> customPropertiesList = new List<string>(); + + if (customProperties != null) + customPropertiesList.AddRange(customProperties); + + if (msbuildInfo.frameworkPathOverride.Length > 0) + customPropertiesList.Add("FrameworkPathOverride=" + msbuildInfo.frameworkPathOverride); - ProcessStartInfo startInfo = new ProcessStartInfo("msbuild", compilerArgs); + string compilerArgs = BuildArguments(loggerAssemblyPath, loggerOutputDir, customPropertiesList); + + ProcessStartInfo startInfo = new ProcessStartInfo(msbuildInfo.path, compilerArgs); // No console output, thanks startInfo.RedirectStandardOutput = true; @@ -101,10 +127,13 @@ namespace GodotSharpTools.Build process.Start(); + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + return true; } - private string BuildArguments(string loggerAssemblyPath, string loggerOutputDir, string[] customProperties) + private string BuildArguments(string loggerAssemblyPath, string loggerOutputDir, List<string> customProperties) { string arguments = string.Format(@"""{0}"" /v:normal /t:Build ""/p:{1}"" ""/l:{2},{3};{4}""", solution, @@ -114,12 +143,9 @@ namespace GodotSharpTools.Build loggerOutputDir ); - if (customProperties != null) + foreach (string customProperty in customProperties) { - foreach (string customProperty in customProperties) - { - arguments += " /p:" + customProperty; - } + arguments += " \"/p:" + customProperty + "\""; } return arguments; diff --git a/modules/mono/editor/bindings_generator.cpp b/modules/mono/editor/bindings_generator.cpp index 6cb4d09a51..eb504ec021 100644 --- a/modules/mono/editor/bindings_generator.cpp +++ b/modules/mono/editor/bindings_generator.cpp @@ -439,7 +439,7 @@ Error BindingsGenerator::generate_cs_core_project(const String &p_output_dir, bo } if (verbose_output) - OS::get_singleton()->print("Core API solution and C# project generated successfully!\n"); + OS::get_singleton()->print("The solution and C# project for the Core API was generated successfully\n"); return OK; } @@ -534,7 +534,7 @@ Error BindingsGenerator::generate_cs_editor_project(const String &p_output_dir, } if (verbose_output) - OS::get_singleton()->print("Editor API solution and C# project generated successfully!\n"); + OS::get_singleton()->print("The solution and C# project for the Editor API was generated successfully\n"); return OK; } @@ -543,8 +543,6 @@ Error BindingsGenerator::generate_cs_editor_project(const String &p_output_dir, // e.g.: warning CS0108: 'SpriteBase3D.FLAG_MAX' hides inherited member 'GeometryInstance.FLAG_MAX'. Use the new keyword if hiding was intended. Error BindingsGenerator::_generate_cs_type(const TypeInterface &itype, const String &p_output_file) { - int method_bind_count = 0; - bool is_derived_type = itype.base_name.length(); List<InternalCall> &custom_icalls = itype.api_type == ClassDB::API_EDITOR ? editor_custom_icalls : core_custom_icalls; @@ -554,51 +552,51 @@ Error BindingsGenerator::_generate_cs_type(const TypeInterface &itype, const Str String ctor_method(ICALL_PREFIX + itype.proxy_name + "_Ctor"); - List<String> cs_file; + List<String> output; - cs_file.push_back("using System;\n"); // IntPtr + output.push_back("using System;\n"); // IntPtr if (itype.requires_collections) - cs_file.push_back("using System.Collections.Generic;\n"); // Dictionary + output.push_back("using System.Collections.Generic;\n"); // Dictionary - cs_file.push_back("\nnamespace " BINDINGS_NAMESPACE "\n" OPEN_BLOCK); + output.push_back("\nnamespace " BINDINGS_NAMESPACE "\n" OPEN_BLOCK); const DocData::ClassDoc *class_doc = itype.class_doc; if (class_doc && class_doc->description.size()) { - cs_file.push_back(INDENT1 "/// <summary>\n"); + output.push_back(INDENT1 "/// <summary>\n"); Vector<String> description_lines = class_doc->description.split("\n"); for (int i = 0; i < description_lines.size(); i++) { if (description_lines[i].size()) { - cs_file.push_back(INDENT1 "/// "); - cs_file.push_back(description_lines[i].strip_edges().xml_escape()); - cs_file.push_back("\n"); + output.push_back(INDENT1 "/// "); + output.push_back(description_lines[i].strip_edges().xml_escape()); + output.push_back("\n"); } } - cs_file.push_back(INDENT1 "/// </summary>\n"); + output.push_back(INDENT1 "/// </summary>\n"); } - cs_file.push_back(INDENT1 "public "); - cs_file.push_back(itype.is_singleton ? "static class " : "class "); - cs_file.push_back(itype.proxy_name); + output.push_back(INDENT1 "public "); + output.push_back(itype.is_singleton ? "static class " : "class "); + output.push_back(itype.proxy_name); if (itype.is_singleton || !itype.is_object_type) { - cs_file.push_back("\n"); + output.push_back("\n"); } else if (!is_derived_type) { - cs_file.push_back(" : IDisposable\n"); + output.push_back(" : IDisposable\n"); } else if (obj_types.has(itype.base_name)) { - cs_file.push_back(" : "); - cs_file.push_back(obj_types[itype.base_name].proxy_name); - cs_file.push_back("\n"); + output.push_back(" : "); + output.push_back(obj_types[itype.base_name].proxy_name); + output.push_back("\n"); } else { - ERR_PRINTS("Base type ' " + itype.base_name + "' does not exist"); + ERR_PRINTS("Base type '" + itype.base_name + "' does not exist, for class " + itype.name); return ERR_INVALID_DATA; } - cs_file.push_back(INDENT1 "{"); + output.push_back(INDENT1 "{"); if (class_doc) { @@ -608,270 +606,165 @@ Error BindingsGenerator::_generate_cs_type(const TypeInterface &itype, const Str const DocData::ConstantDoc &const_doc = class_doc->constants[i]; if (const_doc.description.size()) { - cs_file.push_back(MEMBER_BEGIN "/// <summary>\n"); + output.push_back(MEMBER_BEGIN "/// <summary>\n"); Vector<String> description_lines = const_doc.description.split("\n"); for (int i = 0; i < description_lines.size(); i++) { if (description_lines[i].size()) { - cs_file.push_back(INDENT2 "/// "); - cs_file.push_back(description_lines[i].strip_edges().xml_escape()); - cs_file.push_back("\n"); + output.push_back(INDENT2 "/// "); + output.push_back(description_lines[i].strip_edges().xml_escape()); + output.push_back("\n"); } } - cs_file.push_back(INDENT2 "/// </summary>"); + output.push_back(INDENT2 "/// </summary>"); } - cs_file.push_back(MEMBER_BEGIN "public const int "); - cs_file.push_back(const_doc.name); - cs_file.push_back(" = "); - cs_file.push_back(const_doc.value); - cs_file.push_back(";"); + output.push_back(MEMBER_BEGIN "public const int "); + output.push_back(const_doc.name); + output.push_back(" = "); + output.push_back(const_doc.value); + output.push_back(";"); } if (class_doc->constants.size()) - cs_file.push_back("\n"); + output.push_back("\n"); // Add properties - const Vector<DocData::PropertyDoc> &properties = itype.class_doc->properties; + const Vector<DocData::PropertyDoc> &properties = class_doc->properties; for (int i = 0; i < properties.size(); i++) { const DocData::PropertyDoc &prop_doc = properties[i]; - - const MethodInterface *setter = itype.find_method_by_name(prop_doc.setter); - - // Search it in base types too - const TypeInterface *current_type = &itype; - while (!setter && current_type->base_name.length()) { - Map<String, TypeInterface>::Element *base_match = obj_types.find(current_type->base_name); - ERR_FAIL_NULL_V(base_match, ERR_BUG); - current_type = &base_match->get(); - setter = current_type->find_method_by_name(prop_doc.setter); - } - - const MethodInterface *getter = itype.find_method_by_name(prop_doc.getter); - - // Search it in base types too - current_type = &itype; - while (!getter && current_type->base_name.length()) { - Map<String, TypeInterface>::Element *base_match = obj_types.find(current_type->base_name); - ERR_FAIL_NULL_V(base_match, ERR_BUG); - current_type = &base_match->get(); - getter = current_type->find_method_by_name(prop_doc.getter); - } - - ERR_FAIL_COND_V(!setter && !getter, ERR_BUG); - - bool is_valid = false; - int prop_index = ClassDB::get_property_index(itype.name, prop_doc.name, &is_valid); - ERR_FAIL_COND_V(!is_valid, ERR_BUG); - - if (setter) { - int setter_argc = prop_index != -1 ? 2 : 1; - ERR_FAIL_COND_V(setter->arguments.size() != setter_argc, ERR_BUG); - } - - if (getter) { - int getter_argc = prop_index != -1 ? 1 : 0; - ERR_FAIL_COND_V(getter->arguments.size() != getter_argc, ERR_BUG); - } - - if (getter && setter) { - ERR_FAIL_COND_V(getter->return_type != setter->arguments.back()->get().type, ERR_BUG); - } - - // Let's not trust PropertyDoc::type - String proptype_name = getter ? getter->return_type : setter->arguments.back()->get().type; - - const TypeInterface *prop_itype = _get_type_by_name_or_null(proptype_name); - if (!prop_itype) { - // Try with underscore prefix - prop_itype = _get_type_by_name_or_null("_" + proptype_name); - } - - ERR_FAIL_NULL_V(prop_itype, ERR_BUG); - - String prop_proxy_name = escape_csharp_keyword(snake_to_pascal_case(prop_doc.name)); - - // Prevent property and enclosing type from sharing the same name - if (prop_proxy_name == itype.proxy_name) { - if (verbose_output) { - WARN_PRINTS("Name of property `" + prop_proxy_name + "` is ambiguous with the name of its class `" + - itype.proxy_name + "`. Renaming property to `" + prop_proxy_name + "_`"); - } - - prop_proxy_name += "_"; - } - - if (prop_doc.description.size()) { - cs_file.push_back(MEMBER_BEGIN "/// <summary>\n"); - - Vector<String> description_lines = prop_doc.description.split("\n"); - - for (int i = 0; i < description_lines.size(); i++) { - if (description_lines[i].size()) { - cs_file.push_back(INDENT2 "/// "); - cs_file.push_back(description_lines[i].strip_edges().xml_escape()); - cs_file.push_back("\n"); - } - } - - cs_file.push_back(INDENT2 "/// </summary>"); + Error prop_err = _generate_cs_property(itype, prop_doc, output); + if (prop_err != OK) { + ERR_EXPLAIN("Failed to generate property '" + prop_doc.name + "' for class '" + itype.name + "'"); + ERR_FAIL_V(prop_err); } - - cs_file.push_back(MEMBER_BEGIN "public "); - - if (itype.is_singleton) - cs_file.push_back("static "); - - cs_file.push_back(prop_itype->cs_type); - cs_file.push_back(" "); - cs_file.push_back(prop_proxy_name.replace("/", "__")); - cs_file.push_back("\n" INDENT2 OPEN_BLOCK); - - if (getter) { - cs_file.push_back(INDENT3 "get\n" OPEN_BLOCK_L3); - cs_file.push_back("return "); - cs_file.push_back(getter->proxy_name + "("); - if (prop_index != -1) - cs_file.push_back(itos(prop_index)); - cs_file.push_back(");\n" CLOSE_BLOCK_L3); - } - - if (setter) { - cs_file.push_back(INDENT3 "set\n" OPEN_BLOCK_L3); - cs_file.push_back(setter->proxy_name + "("); - if (prop_index != -1) - cs_file.push_back(itos(prop_index) + ", "); - cs_file.push_back("value);\n" CLOSE_BLOCK_L3); - } - - cs_file.push_back(CLOSE_BLOCK_L2); } if (class_doc->properties.size()) - cs_file.push_back("\n"); + output.push_back("\n"); } if (!itype.is_object_type) { - cs_file.push_back(MEMBER_BEGIN "private const string " BINDINGS_NATIVE_NAME_FIELD " = \"" + itype.name + "\";\n"); - cs_file.push_back(MEMBER_BEGIN "private bool disposed = false;\n"); - cs_file.push_back(MEMBER_BEGIN "internal IntPtr " BINDINGS_PTR_FIELD ";\n"); + output.push_back(MEMBER_BEGIN "private const string " BINDINGS_NATIVE_NAME_FIELD " = \"" + itype.name + "\";\n"); + output.push_back(MEMBER_BEGIN "private bool disposed = false;\n"); + output.push_back(MEMBER_BEGIN "internal IntPtr " BINDINGS_PTR_FIELD ";\n"); - cs_file.push_back(MEMBER_BEGIN "internal static IntPtr " CS_SMETHOD_GETINSTANCE "("); - cs_file.push_back(itype.proxy_name); - cs_file.push_back(" instance)\n" OPEN_BLOCK_L2 "return instance == null ? IntPtr.Zero : instance." BINDINGS_PTR_FIELD ";\n" CLOSE_BLOCK_L2); + output.push_back(MEMBER_BEGIN "internal static IntPtr " CS_SMETHOD_GETINSTANCE "("); + output.push_back(itype.proxy_name); + output.push_back(" instance)\n" OPEN_BLOCK_L2 "return instance == null ? IntPtr.Zero : instance." BINDINGS_PTR_FIELD ";\n" CLOSE_BLOCK_L2); // Add Destructor - cs_file.push_back(MEMBER_BEGIN "~"); - cs_file.push_back(itype.proxy_name); - cs_file.push_back("()\n" OPEN_BLOCK_L2 "Dispose(false);\n" CLOSE_BLOCK_L2); + output.push_back(MEMBER_BEGIN "~"); + output.push_back(itype.proxy_name); + output.push_back("()\n" OPEN_BLOCK_L2 "Dispose(false);\n" CLOSE_BLOCK_L2); // Add the Dispose from IDisposable - cs_file.push_back(MEMBER_BEGIN "public void Dispose()\n" OPEN_BLOCK_L2 "Dispose(true);\n" INDENT3 "GC.SuppressFinalize(this);\n" CLOSE_BLOCK_L2); + output.push_back(MEMBER_BEGIN "public void Dispose()\n" OPEN_BLOCK_L2 "Dispose(true);\n" INDENT3 "GC.SuppressFinalize(this);\n" CLOSE_BLOCK_L2); // Add the virtual Dispose - cs_file.push_back(MEMBER_BEGIN "public virtual void Dispose(bool disposing)\n" OPEN_BLOCK_L2 - "if (disposed) return;\n" INDENT3 - "if (" BINDINGS_PTR_FIELD " != IntPtr.Zero)\n" OPEN_BLOCK_L3 "NativeCalls.godot_icall_"); - cs_file.push_back(itype.proxy_name); - cs_file.push_back("_Dtor(" BINDINGS_PTR_FIELD ");\n" INDENT5 BINDINGS_PTR_FIELD " = IntPtr.Zero;\n" CLOSE_BLOCK_L3 INDENT3 - "GC.SuppressFinalize(this);\n" INDENT3 "disposed = true;\n" CLOSE_BLOCK_L2); - - cs_file.push_back(MEMBER_BEGIN "internal "); - cs_file.push_back(itype.proxy_name); - cs_file.push_back("(IntPtr " BINDINGS_PTR_FIELD ")\n" OPEN_BLOCK_L2 "this." BINDINGS_PTR_FIELD " = " BINDINGS_PTR_FIELD ";\n" CLOSE_BLOCK_L2); - - cs_file.push_back(MEMBER_BEGIN "public IntPtr NativeInstance\n" OPEN_BLOCK_L2 - "get { return " BINDINGS_PTR_FIELD "; }\n" CLOSE_BLOCK_L2); + output.push_back(MEMBER_BEGIN "public virtual void Dispose(bool disposing)\n" OPEN_BLOCK_L2 + "if (disposed) return;\n" INDENT3 + "if (" BINDINGS_PTR_FIELD " != IntPtr.Zero)\n" OPEN_BLOCK_L3 "NativeCalls.godot_icall_"); + output.push_back(itype.proxy_name); + output.push_back("_Dtor(" BINDINGS_PTR_FIELD ");\n" INDENT5 BINDINGS_PTR_FIELD " = IntPtr.Zero;\n" CLOSE_BLOCK_L3 INDENT3 + "GC.SuppressFinalize(this);\n" INDENT3 "disposed = true;\n" CLOSE_BLOCK_L2); + + output.push_back(MEMBER_BEGIN "internal "); + output.push_back(itype.proxy_name); + output.push_back("(IntPtr " BINDINGS_PTR_FIELD ")\n" OPEN_BLOCK_L2 "this." BINDINGS_PTR_FIELD " = " BINDINGS_PTR_FIELD ";\n" CLOSE_BLOCK_L2); + + output.push_back(MEMBER_BEGIN "public IntPtr NativeInstance\n" OPEN_BLOCK_L2 + "get { return " BINDINGS_PTR_FIELD "; }\n" CLOSE_BLOCK_L2); } else if (itype.is_singleton) { // Add the type name and the singleton pointer as static fields - cs_file.push_back(MEMBER_BEGIN "private const string " BINDINGS_NATIVE_NAME_FIELD " = \""); - cs_file.push_back(itype.name); - cs_file.push_back("\";\n"); + output.push_back(MEMBER_BEGIN "private const string " BINDINGS_NATIVE_NAME_FIELD " = \""); + output.push_back(itype.name); + output.push_back("\";\n"); - cs_file.push_back(INDENT2 "internal static IntPtr " BINDINGS_PTR_FIELD " = "); - cs_file.push_back(itype.api_type == ClassDB::API_EDITOR ? CS_CLASS_NATIVECALLS_EDITOR : CS_CLASS_NATIVECALLS); - cs_file.push_back("." ICALL_PREFIX); - cs_file.push_back(itype.name); - cs_file.push_back(SINGLETON_ICALL_SUFFIX "();\n"); + output.push_back(INDENT2 "internal static IntPtr " BINDINGS_PTR_FIELD " = "); + output.push_back(itype.api_type == ClassDB::API_EDITOR ? CS_CLASS_NATIVECALLS_EDITOR : CS_CLASS_NATIVECALLS); + output.push_back("." ICALL_PREFIX); + output.push_back(itype.name); + output.push_back(SINGLETON_ICALL_SUFFIX "();\n"); } else { // Add member fields - cs_file.push_back(MEMBER_BEGIN "private const string " BINDINGS_NATIVE_NAME_FIELD " = \""); - cs_file.push_back(itype.name); - cs_file.push_back("\";\n"); + output.push_back(MEMBER_BEGIN "private const string " BINDINGS_NATIVE_NAME_FIELD " = \""); + output.push_back(itype.name); + output.push_back("\";\n"); // Only the base class stores the pointer to the native object // This pointer is expected to be and must be of type Object* if (!is_derived_type) { - cs_file.push_back(MEMBER_BEGIN "private bool disposed = false;\n"); - cs_file.push_back(INDENT2 "internal IntPtr " BINDINGS_PTR_FIELD ";\n"); - cs_file.push_back(INDENT2 "internal bool " CS_FIELD_MEMORYOWN ";\n"); + output.push_back(MEMBER_BEGIN "private bool disposed = false;\n"); + output.push_back(INDENT2 "internal IntPtr " BINDINGS_PTR_FIELD ";\n"); + output.push_back(INDENT2 "internal bool " CS_FIELD_MEMORYOWN ";\n"); } // Add default constructor if (itype.is_instantiable) { - cs_file.push_back(MEMBER_BEGIN "public "); - cs_file.push_back(itype.proxy_name); - cs_file.push_back("() : this("); - cs_file.push_back(itype.memory_own ? "true" : "false"); + output.push_back(MEMBER_BEGIN "public "); + output.push_back(itype.proxy_name); + output.push_back("() : this("); + output.push_back(itype.memory_own ? "true" : "false"); // The default constructor may also be called by the engine when instancing existing native objects // The engine will initialize the pointer field of the managed side before calling the constructor // This is why we only allocate a new native object from the constructor if the pointer field is not set - cs_file.push_back(")\n" OPEN_BLOCK_L2 "if (" BINDINGS_PTR_FIELD " == IntPtr.Zero)\n" INDENT4 BINDINGS_PTR_FIELD " = "); - cs_file.push_back(itype.api_type == ClassDB::API_EDITOR ? CS_CLASS_NATIVECALLS_EDITOR : CS_CLASS_NATIVECALLS); - cs_file.push_back("." + ctor_method); - cs_file.push_back("(this);\n" CLOSE_BLOCK_L2); + output.push_back(")\n" OPEN_BLOCK_L2 "if (" BINDINGS_PTR_FIELD " == IntPtr.Zero)\n" INDENT4 BINDINGS_PTR_FIELD " = "); + output.push_back(itype.api_type == ClassDB::API_EDITOR ? CS_CLASS_NATIVECALLS_EDITOR : CS_CLASS_NATIVECALLS); + output.push_back("." + ctor_method); + output.push_back("(this);\n" CLOSE_BLOCK_L2); } else { // Hide the constructor - cs_file.push_back(MEMBER_BEGIN "internal "); - cs_file.push_back(itype.proxy_name); - cs_file.push_back("() {}\n"); + output.push_back(MEMBER_BEGIN "internal "); + output.push_back(itype.proxy_name); + output.push_back("() {}\n"); } // Add.. em.. trick constructor. Sort of. - cs_file.push_back(MEMBER_BEGIN "internal "); - cs_file.push_back(itype.proxy_name); + output.push_back(MEMBER_BEGIN "internal "); + output.push_back(itype.proxy_name); if (is_derived_type) { - cs_file.push_back("(bool " CS_FIELD_MEMORYOWN ") : base(" CS_FIELD_MEMORYOWN ") {}\n"); + output.push_back("(bool " CS_FIELD_MEMORYOWN ") : base(" CS_FIELD_MEMORYOWN ") {}\n"); } else { - cs_file.push_back("(bool " CS_FIELD_MEMORYOWN ")\n" OPEN_BLOCK_L2 - "this." CS_FIELD_MEMORYOWN " = " CS_FIELD_MEMORYOWN ";\n" CLOSE_BLOCK_L2); + output.push_back("(bool " CS_FIELD_MEMORYOWN ")\n" OPEN_BLOCK_L2 + "this." CS_FIELD_MEMORYOWN " = " CS_FIELD_MEMORYOWN ";\n" CLOSE_BLOCK_L2); } // Add methods if (!is_derived_type) { - cs_file.push_back(MEMBER_BEGIN "public IntPtr NativeInstance\n" OPEN_BLOCK_L2 - "get { return " BINDINGS_PTR_FIELD "; }\n" CLOSE_BLOCK_L2); + output.push_back(MEMBER_BEGIN "public IntPtr NativeInstance\n" OPEN_BLOCK_L2 + "get { return " BINDINGS_PTR_FIELD "; }\n" CLOSE_BLOCK_L2); - cs_file.push_back(MEMBER_BEGIN "internal static IntPtr " CS_SMETHOD_GETINSTANCE "(Object instance)\n" OPEN_BLOCK_L2 - "return instance == null ? IntPtr.Zero : instance." BINDINGS_PTR_FIELD ";\n" CLOSE_BLOCK_L2); + output.push_back(MEMBER_BEGIN "internal static IntPtr " CS_SMETHOD_GETINSTANCE "(Object instance)\n" OPEN_BLOCK_L2 + "return instance == null ? IntPtr.Zero : instance." BINDINGS_PTR_FIELD ";\n" CLOSE_BLOCK_L2); } if (!is_derived_type) { // Add destructor - cs_file.push_back(MEMBER_BEGIN "~"); - cs_file.push_back(itype.proxy_name); - cs_file.push_back("()\n" OPEN_BLOCK_L2 "Dispose(false);\n" CLOSE_BLOCK_L2); + output.push_back(MEMBER_BEGIN "~"); + output.push_back(itype.proxy_name); + output.push_back("()\n" OPEN_BLOCK_L2 "Dispose(false);\n" CLOSE_BLOCK_L2); // Add the Dispose from IDisposable - cs_file.push_back(MEMBER_BEGIN "public void Dispose()\n" OPEN_BLOCK_L2 "Dispose(true);\n" INDENT3 "GC.SuppressFinalize(this);\n" CLOSE_BLOCK_L2); + output.push_back(MEMBER_BEGIN "public void Dispose()\n" OPEN_BLOCK_L2 "Dispose(true);\n" INDENT3 "GC.SuppressFinalize(this);\n" CLOSE_BLOCK_L2); // Add the virtual Dispose - cs_file.push_back(MEMBER_BEGIN "public virtual void Dispose(bool disposing)\n" OPEN_BLOCK_L2 - "if (disposed) return;\n" INDENT3 - "if (" BINDINGS_PTR_FIELD " != IntPtr.Zero)\n" OPEN_BLOCK_L3 - "if (" CS_FIELD_MEMORYOWN ")\n" OPEN_BLOCK_L4 CS_FIELD_MEMORYOWN - " = false;\n" INDENT5 CS_CLASS_NATIVECALLS "." ICALL_OBJECT_DTOR - "(" BINDINGS_PTR_FIELD ");\n" INDENT5 BINDINGS_PTR_FIELD - " = IntPtr.Zero;\n" CLOSE_BLOCK_L4 CLOSE_BLOCK_L3 INDENT3 - "GC.SuppressFinalize(this);\n" INDENT3 "disposed = true;\n" CLOSE_BLOCK_L2); + output.push_back(MEMBER_BEGIN "public virtual void Dispose(bool disposing)\n" OPEN_BLOCK_L2 + "if (disposed) return;\n" INDENT3 + "if (" BINDINGS_PTR_FIELD " != IntPtr.Zero)\n" OPEN_BLOCK_L3 + "if (" CS_FIELD_MEMORYOWN ")\n" OPEN_BLOCK_L4 CS_FIELD_MEMORYOWN + " = false;\n" INDENT5 CS_CLASS_NATIVECALLS "." ICALL_OBJECT_DTOR + "(" BINDINGS_PTR_FIELD ");\n" INDENT5 BINDINGS_PTR_FIELD + " = IntPtr.Zero;\n" CLOSE_BLOCK_L4 CLOSE_BLOCK_L3 INDENT3 + "GC.SuppressFinalize(this);\n" INDENT3 "disposed = true;\n" CLOSE_BLOCK_L2); Map<String, TypeInterface>::Element *array_itype = builtin_types.find("Array"); @@ -883,409 +776,387 @@ Error BindingsGenerator::_generate_cs_type(const TypeInterface &itype, const Str Map<String, TypeInterface>::Element *object_itype = obj_types.find("Object"); if (!object_itype) { - ERR_PRINT("BUG: Array type interface not found!"); + ERR_PRINT("BUG: Object type interface not found!"); return ERR_BUG; } - cs_file.push_back(MEMBER_BEGIN "public " CS_CLASS_SIGNALAWAITER " ToSignal("); - cs_file.push_back(object_itype->get().cs_type); - cs_file.push_back(" source, string signal)\n" OPEN_BLOCK_L2 - "return new " CS_CLASS_SIGNALAWAITER "(source, signal, this);\n" CLOSE_BLOCK_L2); + output.push_back(MEMBER_BEGIN "public " CS_CLASS_SIGNALAWAITER " ToSignal("); + output.push_back(object_itype->get().cs_type); + output.push_back(" source, string signal)\n" OPEN_BLOCK_L2 + "return new " CS_CLASS_SIGNALAWAITER "(source, signal, this);\n" CLOSE_BLOCK_L2); } } Map<String, String>::Element *extra_member = extra_members.find(itype.name); if (extra_member) - cs_file.push_back(extra_member->get()); + output.push_back(extra_member->get()); + int method_bind_count = 0; for (const List<MethodInterface>::Element *E = itype.methods.front(); E; E = E->next()) { const MethodInterface &imethod = E->get(); + Error method_err = _generate_cs_method(itype, imethod, method_bind_count, output); + if (method_err != OK) { + ERR_EXPLAIN("Failed to generate method '" + imethod.name + "' for class '" + itype.name + "'"); + ERR_FAIL_V(method_err); + } + } - const TypeInterface *return_type = _get_type_by_name_or_placeholder(imethod.return_type); + if (itype.is_singleton) { + InternalCall singleton_icall = InternalCall(itype.api_type, ICALL_PREFIX + itype.name + SINGLETON_ICALL_SUFFIX, "IntPtr"); - String method_bind_field = "method_bind_" + itos(method_bind_count); + if (!find_icall_by_name(singleton_icall.name, custom_icalls)) + custom_icalls.push_back(singleton_icall); + } - String icall_params = method_bind_field + ", " + sformat(itype.cs_in, "this"); - String arguments_sig; - String cs_in_statements; + if (itype.is_instantiable) { + InternalCall ctor_icall = InternalCall(itype.api_type, ctor_method, "IntPtr", itype.proxy_name + " obj"); - List<String> default_args_doc; + if (!find_icall_by_name(ctor_icall.name, custom_icalls)) + custom_icalls.push_back(ctor_icall); + } - // Retrieve information from the arguments - for (const List<ArgumentInterface>::Element *F = imethod.arguments.front(); F; F = F->next()) { - const ArgumentInterface &iarg = F->get(); - const TypeInterface *arg_type = _get_type_by_name_or_placeholder(iarg.type); + output.push_back(INDENT1 CLOSE_BLOCK CLOSE_BLOCK); - // Add the current arguments to the signature - // If the argument has a default value which is not a constant, we will make it Nullable - { - if (F != imethod.arguments.front()) - arguments_sig += ", "; + return _save_file(p_output_file, output); +} - if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) - arguments_sig += "Nullable<"; +Error BindingsGenerator::_generate_cs_property(const BindingsGenerator::TypeInterface &p_itype, const DocData::PropertyDoc &p_prop_doc, List<String> &p_output) { - arguments_sig += arg_type->cs_type; + const MethodInterface *setter = p_itype.find_method_by_name(p_prop_doc.setter); - if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) - arguments_sig += "> "; - else - arguments_sig += " "; + // Search it in base types too + const TypeInterface *current_type = &p_itype; + while (!setter && current_type->base_name.length()) { + Map<String, TypeInterface>::Element *base_match = obj_types.find(current_type->base_name); + ERR_FAIL_NULL_V(base_match, ERR_BUG); + current_type = &base_match->get(); + setter = current_type->find_method_by_name(p_prop_doc.setter); + } - arguments_sig += iarg.name; + const MethodInterface *getter = p_itype.find_method_by_name(p_prop_doc.getter); - if (iarg.default_argument.size()) { - if (iarg.def_param_mode != ArgumentInterface::CONSTANT) - arguments_sig += " = null"; - else - arguments_sig += " = " + sformat(iarg.default_argument, arg_type->cs_type); - } - } + // Search it in base types too + current_type = &p_itype; + while (!getter && current_type->base_name.length()) { + Map<String, TypeInterface>::Element *base_match = obj_types.find(current_type->base_name); + ERR_FAIL_NULL_V(base_match, ERR_BUG); + current_type = &base_match->get(); + getter = current_type->find_method_by_name(p_prop_doc.getter); + } - icall_params += ", "; + ERR_FAIL_COND_V(!setter && !getter, ERR_BUG); - if (iarg.default_argument.size() && iarg.def_param_mode != ArgumentInterface::CONSTANT) { - // The default value of an argument must be constant. Otherwise we make it Nullable and do the following: - // Type arg_in = arg.HasValue ? arg.Value : <non-const default value>; - String arg_in = iarg.name; - arg_in += "_in"; + bool is_valid = false; + int prop_index = ClassDB::get_property_index(p_itype.name, p_prop_doc.name, &is_valid); + ERR_FAIL_COND_V(!is_valid, ERR_BUG); - cs_in_statements += arg_type->cs_type; - cs_in_statements += " "; - cs_in_statements += arg_in; - cs_in_statements += " = "; - cs_in_statements += iarg.name; + if (setter) { + int setter_argc = prop_index != -1 ? 2 : 1; + ERR_FAIL_COND_V(setter->arguments.size() != setter_argc, ERR_BUG); + } - if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) - cs_in_statements += ".HasValue ? "; - else - cs_in_statements += " != null ? "; + if (getter) { + int getter_argc = prop_index != -1 ? 1 : 0; + ERR_FAIL_COND_V(getter->arguments.size() != getter_argc, ERR_BUG); + } - cs_in_statements += iarg.name; + if (getter && setter) { + ERR_FAIL_COND_V(getter->return_type != setter->arguments.back()->get().type, ERR_BUG); + } - if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) - cs_in_statements += ".Value : "; - else - cs_in_statements += " : "; + // Let's not trust PropertyDoc::type + String proptype_name = getter ? getter->return_type : setter->arguments.back()->get().type; - String def_arg = sformat(iarg.default_argument, arg_type->cs_type); + const TypeInterface *prop_itype = _get_type_by_name_or_null(proptype_name); + if (!prop_itype) { + // Try with underscore prefix + prop_itype = _get_type_by_name_or_null("_" + proptype_name); + } - cs_in_statements += def_arg; - cs_in_statements += ";\n" INDENT3; + ERR_FAIL_NULL_V(prop_itype, ERR_BUG); - icall_params += arg_type->cs_in.empty() ? arg_in : sformat(arg_type->cs_in, arg_in); + String prop_proxy_name = escape_csharp_keyword(snake_to_pascal_case(p_prop_doc.name)); - default_args_doc.push_back(INDENT2 "/// <param name=\"" + iarg.name + "\">If the param is null, then the default value is " + def_arg + "</param>\n"); - } else { - icall_params += arg_type->cs_in.empty() ? iarg.name : sformat(arg_type->cs_in, iarg.name); - } + // Prevent property and enclosing type from sharing the same name + if (prop_proxy_name == p_itype.proxy_name) { + if (verbose_output) { + WARN_PRINTS("Name of property `" + prop_proxy_name + "` is ambiguous with the name of its class `" + + p_itype.proxy_name + "`. Renaming property to `" + prop_proxy_name + "_`"); } - // Generate method - { - if (!imethod.is_virtual && !imethod.requires_object_call) { - cs_file.push_back(MEMBER_BEGIN "private "); - cs_file.push_back(itype.is_singleton ? "static IntPtr " : "IntPtr "); - cs_file.push_back(method_bind_field + " = " CS_CLASS_NATIVECALLS "." ICALL_GET_METHODBIND "(" BINDINGS_NATIVE_NAME_FIELD ", \""); - cs_file.push_back(imethod.name); - cs_file.push_back("\");\n"); - } + prop_proxy_name += "_"; + } - if (imethod.method_doc && imethod.method_doc->description.size()) { - cs_file.push_back(MEMBER_BEGIN "/// <summary>\n"); + if (p_prop_doc.description.size()) { + p_output.push_back(MEMBER_BEGIN "/// <summary>\n"); - Vector<String> description_lines = imethod.method_doc->description.split("\n"); + Vector<String> description_lines = p_prop_doc.description.split("\n"); - for (int i = 0; i < description_lines.size(); i++) { - if (description_lines[i].size()) { - cs_file.push_back(INDENT2 "/// "); - cs_file.push_back(description_lines[i].strip_edges().xml_escape()); - cs_file.push_back("\n"); - } - } + for (int i = 0; i < description_lines.size(); i++) { + if (description_lines[i].size()) { + p_output.push_back(INDENT2 "/// "); + p_output.push_back(description_lines[i].strip_edges().xml_escape()); + p_output.push_back("\n"); + } + } - for (List<String>::Element *E = default_args_doc.front(); E; E = E->next()) { - cs_file.push_back(E->get().xml_escape()); - } + p_output.push_back(INDENT2 "/// </summary>"); + } - cs_file.push_back(INDENT2 "/// </summary>"); - } + p_output.push_back(MEMBER_BEGIN "public "); - if (!imethod.is_internal) { - cs_file.push_back(MEMBER_BEGIN "[GodotMethod(\""); - cs_file.push_back(imethod.name); - cs_file.push_back("\")]"); - } + if (p_itype.is_singleton) + p_output.push_back("static "); - cs_file.push_back(MEMBER_BEGIN); - cs_file.push_back(imethod.is_internal ? "internal " : "public "); + p_output.push_back(prop_itype->cs_type); + p_output.push_back(" "); + p_output.push_back(prop_proxy_name.replace("/", "__")); + p_output.push_back("\n" INDENT2 OPEN_BLOCK); - if (itype.is_singleton) { - cs_file.push_back("static "); - } else if (imethod.is_virtual) { - cs_file.push_back("virtual "); - } + if (getter) { + p_output.push_back(INDENT3 "get\n" OPEN_BLOCK_L3); + p_output.push_back("return "); + p_output.push_back(getter->proxy_name + "("); + if (prop_index != -1) + p_output.push_back(itos(prop_index)); + p_output.push_back(");\n" CLOSE_BLOCK_L3); + } - cs_file.push_back(return_type->cs_type + " "); - cs_file.push_back(imethod.proxy_name + "("); - cs_file.push_back(arguments_sig + ")\n" OPEN_BLOCK_L2); + if (setter) { + p_output.push_back(INDENT3 "set\n" OPEN_BLOCK_L3); + p_output.push_back(setter->proxy_name + "("); + if (prop_index != -1) + p_output.push_back(itos(prop_index) + ", "); + p_output.push_back("value);\n" CLOSE_BLOCK_L3); + } - if (imethod.is_virtual) { - // Godot virtual method must be overridden, therefore we return a default value by default. + p_output.push_back(CLOSE_BLOCK_L2); - if (return_type->name == "void") { - cs_file.push_back("return;\n" CLOSE_BLOCK_L2); - } else { - cs_file.push_back("return default("); - cs_file.push_back(return_type->cs_type); - cs_file.push_back(");\n" CLOSE_BLOCK_L2); - } + return OK; +} - continue; - } +Error BindingsGenerator::_generate_cs_method(const BindingsGenerator::TypeInterface &p_itype, const BindingsGenerator::MethodInterface &p_imethod, int &p_method_bind_count, List<String> &p_output) { - if (imethod.requires_object_call) { - // Fallback to Godot's object.Call(string, params) + const TypeInterface *return_type = _get_type_by_name_or_placeholder(p_imethod.return_type); - cs_file.push_back(CS_METHOD_CALL "(\""); - cs_file.push_back(imethod.name); - cs_file.push_back("\""); + String method_bind_field = "method_bind_" + itos(p_method_bind_count); - for (const List<ArgumentInterface>::Element *F = imethod.arguments.front(); F; F = F->next()) { - cs_file.push_back(", "); - cs_file.push_back(F->get().name); - } + String icall_params = method_bind_field + ", " + sformat(p_itype.cs_in, "this"); + String arguments_sig; + String cs_in_statements; - cs_file.push_back(");\n" CLOSE_BLOCK_L2); + List<String> default_args_doc; - continue; - } + // Retrieve information from the arguments + for (const List<ArgumentInterface>::Element *F = p_imethod.arguments.front(); F; F = F->next()) { + const ArgumentInterface &iarg = F->get(); + const TypeInterface *arg_type = _get_type_by_name_or_placeholder(iarg.type); + + // Add the current arguments to the signature + // If the argument has a default value which is not a constant, we will make it Nullable + { + if (F != p_imethod.arguments.front()) + arguments_sig += ", "; - const Map<const MethodInterface *, const InternalCall *>::Element *match = method_icalls_map.find(&E->get()); - ERR_FAIL_NULL_V(match, ERR_BUG); + if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) + arguments_sig += "Nullable<"; - const InternalCall *im_icall = match->value(); + arguments_sig += arg_type->cs_type; - String im_call = im_icall->editor_only ? CS_CLASS_NATIVECALLS_EDITOR : CS_CLASS_NATIVECALLS; - im_call += "." + im_icall->name + "(" + icall_params + ");\n"; + if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) + arguments_sig += "> "; + else + arguments_sig += " "; - if (imethod.arguments.size()) - cs_file.push_back(cs_in_statements); + arguments_sig += iarg.name; - if (return_type->name == "void") { - cs_file.push_back(im_call); - } else if (return_type->cs_out.empty()) { - cs_file.push_back("return " + im_call); - } else { - cs_file.push_back(return_type->im_type_out); - cs_file.push_back(" " LOCAL_RET " = "); - cs_file.push_back(im_call); - cs_file.push_back(INDENT3); - cs_file.push_back(sformat(return_type->cs_out, LOCAL_RET) + "\n"); + if (iarg.default_argument.size()) { + if (iarg.def_param_mode != ArgumentInterface::CONSTANT) + arguments_sig += " = null"; + else + arguments_sig += " = " + sformat(iarg.default_argument, arg_type->cs_type); } - - cs_file.push_back(CLOSE_BLOCK_L2); } - method_bind_count++; - } + icall_params += ", "; - if (itype.is_singleton) { - InternalCall singleton_icall = InternalCall(itype.api_type, ICALL_PREFIX + itype.name + SINGLETON_ICALL_SUFFIX, "IntPtr"); + if (iarg.default_argument.size() && iarg.def_param_mode != ArgumentInterface::CONSTANT) { + // The default value of an argument must be constant. Otherwise we make it Nullable and do the following: + // Type arg_in = arg.HasValue ? arg.Value : <non-const default value>; + String arg_in = iarg.name; + arg_in += "_in"; - if (!find_icall_by_name(singleton_icall.name, custom_icalls)) - custom_icalls.push_back(singleton_icall); - } + cs_in_statements += arg_type->cs_type; + cs_in_statements += " "; + cs_in_statements += arg_in; + cs_in_statements += " = "; + cs_in_statements += iarg.name; - if (itype.is_instantiable) { - InternalCall ctor_icall = InternalCall(itype.api_type, ctor_method, "IntPtr", itype.proxy_name + " obj"); + if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) + cs_in_statements += ".HasValue ? "; + else + cs_in_statements += " != null ? "; - if (!find_icall_by_name(ctor_icall.name, custom_icalls)) - custom_icalls.push_back(ctor_icall); - } + cs_in_statements += iarg.name; - cs_file.push_back(INDENT1 CLOSE_BLOCK CLOSE_BLOCK); + if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) + cs_in_statements += ".Value : "; + else + cs_in_statements += " : "; - return _save_file(p_output_file, cs_file); -} + String def_arg = sformat(iarg.default_argument, arg_type->cs_type); -Error BindingsGenerator::generate_glue(const String &p_output_dir) { + cs_in_statements += def_arg; + cs_in_statements += ";\n" INDENT3; - verbose_output = true; + icall_params += arg_type->cs_in.empty() ? arg_in : sformat(arg_type->cs_in, arg_in); - bool dir_exists = DirAccess::exists(p_output_dir); - ERR_EXPLAIN("The output directory does not exist."); - ERR_FAIL_COND_V(!dir_exists, ERR_FILE_BAD_PATH); + default_args_doc.push_back(INDENT2 "/// <param name=\"" + iarg.name + "\">If the param is null, then the default value is " + def_arg + "</param>\n"); + } else { + icall_params += arg_type->cs_in.empty() ? iarg.name : sformat(arg_type->cs_in, iarg.name); + } + } - List<String> cpp_file; + // Generate method + { + if (!p_imethod.is_virtual && !p_imethod.requires_object_call) { + p_output.push_back(MEMBER_BEGIN "private "); + p_output.push_back(p_itype.is_singleton ? "static IntPtr " : "IntPtr "); + p_output.push_back(method_bind_field + " = " CS_CLASS_NATIVECALLS "." ICALL_GET_METHODBIND "(" BINDINGS_NATIVE_NAME_FIELD ", \""); + p_output.push_back(p_imethod.name); + p_output.push_back("\");\n"); + } - cpp_file.push_back("#include \"" GLUE_HEADER_FILE "\"\n" - "\n"); + if (p_imethod.method_doc && p_imethod.method_doc->description.size()) { + p_output.push_back(MEMBER_BEGIN "/// <summary>\n"); - List<const InternalCall *> generated_icall_funcs; + Vector<String> description_lines = p_imethod.method_doc->description.split("\n"); - for (Map<String, TypeInterface>::Element *type_elem = obj_types.front(); type_elem; type_elem = type_elem->next()) { - const TypeInterface &itype = type_elem->get(); + for (int i = 0; i < description_lines.size(); i++) { + if (description_lines[i].size()) { + p_output.push_back(INDENT2 "/// "); + p_output.push_back(description_lines[i].strip_edges().xml_escape()); + p_output.push_back("\n"); + } + } - List<InternalCall> &custom_icalls = itype.api_type == ClassDB::API_EDITOR ? editor_custom_icalls : core_custom_icalls; + for (List<String>::Element *E = default_args_doc.front(); E; E = E->next()) { + p_output.push_back(E->get().xml_escape()); + } - OS::get_singleton()->print(String("Generating " + itype.name + "...\n").utf8()); + p_output.push_back(INDENT2 "/// </summary>"); + } - String ctor_method(ICALL_PREFIX + itype.proxy_name + "_Ctor"); + if (!p_imethod.is_internal) { + p_output.push_back(MEMBER_BEGIN "[GodotMethod(\""); + p_output.push_back(p_imethod.name); + p_output.push_back("\")]"); + } - for (const List<MethodInterface>::Element *E = itype.methods.front(); E; E = E->next()) { - const MethodInterface &imethod = E->get(); + p_output.push_back(MEMBER_BEGIN); + p_output.push_back(p_imethod.is_internal ? "internal " : "public "); - if (imethod.is_virtual) - continue; + if (p_itype.is_singleton) { + p_output.push_back("static "); + } else if (p_imethod.is_virtual) { + p_output.push_back("virtual "); + } - bool ret_void = imethod.return_type == "void"; + p_output.push_back(return_type->cs_type + " "); + p_output.push_back(p_imethod.proxy_name + "("); + p_output.push_back(arguments_sig + ")\n" OPEN_BLOCK_L2); - const TypeInterface *return_type = _get_type_by_name_or_placeholder(imethod.return_type); + if (p_imethod.is_virtual) { + // Godot virtual method must be overridden, therefore we return a default value by default. - String argc_str = itos(imethod.arguments.size()); + if (return_type->name == "void") { + p_output.push_back("return;\n" CLOSE_BLOCK_L2); + } else { + p_output.push_back("return default("); + p_output.push_back(return_type->cs_type); + p_output.push_back(");\n" CLOSE_BLOCK_L2); + } - String c_func_sig = "MethodBind* " CS_PARAM_METHODBIND ", " + itype.c_type_in + " " CS_PARAM_INSTANCE; - String c_in_statements; - String c_args_var_content; + return OK; // Won't increment method bind count + } - // Get arguments information - int i = 0; - for (const List<ArgumentInterface>::Element *F = imethod.arguments.front(); F; F = F->next()) { - const ArgumentInterface &iarg = F->get(); - const TypeInterface *arg_type = _get_type_by_name_or_placeholder(iarg.type); + if (p_imethod.requires_object_call) { + // Fallback to Godot's object.Call(string, params) - String c_param_name = "arg" + itos(i + 1); + p_output.push_back(CS_METHOD_CALL "(\""); + p_output.push_back(p_imethod.name); + p_output.push_back("\""); - if (imethod.is_vararg) { - if (i < imethod.arguments.size() - 1) { - c_in_statements += sformat(arg_type->c_in.size() ? arg_type->c_in : TypeInterface::DEFAULT_VARARG_C_IN, "Variant", c_param_name); - c_in_statements += "\t" C_LOCAL_PTRCALL_ARGS ".set(0, "; - c_in_statements += sformat("&%s_in", c_param_name); - c_in_statements += ");\n"; - } - } else { - if (i > 0) - c_args_var_content += ", "; - if (arg_type->c_in.size()) - c_in_statements += sformat(arg_type->c_in, arg_type->c_type, c_param_name); - c_args_var_content += sformat(arg_type->c_arg_in, c_param_name); - } + for (const List<ArgumentInterface>::Element *F = p_imethod.arguments.front(); F; F = F->next()) { + p_output.push_back(", "); + p_output.push_back(F->get().name); + } - c_func_sig += ", "; - c_func_sig += arg_type->c_type_in; - c_func_sig += " "; - c_func_sig += c_param_name; + p_output.push_back(");\n" CLOSE_BLOCK_L2); - i++; - } + return OK; // Won't increment method bind count + } - const Map<const MethodInterface *, const InternalCall *>::Element *match = method_icalls_map.find(&E->get()); - ERR_FAIL_NULL_V(match, ERR_BUG); + const Map<const MethodInterface *, const InternalCall *>::Element *match = method_icalls_map.find(&p_imethod); + ERR_FAIL_NULL_V(match, ERR_BUG); - const InternalCall *im_icall = match->value(); - String icall_method = im_icall->name; + const InternalCall *im_icall = match->value(); - if (!generated_icall_funcs.find(im_icall)) { - generated_icall_funcs.push_back(im_icall); + String im_call = im_icall->editor_only ? CS_CLASS_NATIVECALLS_EDITOR : CS_CLASS_NATIVECALLS; + im_call += "." + im_icall->name + "(" + icall_params + ");\n"; - if (im_icall->editor_only) - cpp_file.push_back("#ifdef TOOLS_ENABLED\n"); + if (p_imethod.arguments.size()) + p_output.push_back(cs_in_statements); - // Generate icall function + if (return_type->name == "void") { + p_output.push_back(im_call); + } else if (return_type->cs_out.empty()) { + p_output.push_back("return " + im_call); + } else { + p_output.push_back(return_type->im_type_out); + p_output.push_back(" " LOCAL_RET " = "); + p_output.push_back(im_call); + p_output.push_back(INDENT3); + p_output.push_back(sformat(return_type->cs_out, LOCAL_RET) + "\n"); + } - cpp_file.push_back(ret_void ? "void " : return_type->c_type_out + " "); - cpp_file.push_back(icall_method); - cpp_file.push_back("("); - cpp_file.push_back(c_func_sig); - cpp_file.push_back(") " OPEN_BLOCK); + p_output.push_back(CLOSE_BLOCK_L2); + } - String fail_ret = ret_void ? "" : ", " + (return_type->c_type_out.ends_with("*") ? "NULL" : return_type->c_type_out + "()"); + p_method_bind_count++; + return OK; +} - if (!ret_void) { - String ptrcall_return_type; - String initialization; +Error BindingsGenerator::generate_glue(const String &p_output_dir) { - if (return_type->is_object_type) { - ptrcall_return_type = return_type->is_reference ? "Ref<Reference>" : return_type->c_type; - initialization = return_type->is_reference ? "" : " = NULL"; - } else { - ptrcall_return_type = return_type->c_type; - } + verbose_output = true; - cpp_file.push_back("\t" + ptrcall_return_type); - cpp_file.push_back(" " LOCAL_RET); - cpp_file.push_back(initialization + ";\n"); - cpp_file.push_back("\tERR_FAIL_NULL_V(" CS_PARAM_INSTANCE); - cpp_file.push_back(fail_ret); - cpp_file.push_back(");\n"); - } else { - cpp_file.push_back("\tERR_FAIL_NULL(" CS_PARAM_INSTANCE ");\n"); - } + bool dir_exists = DirAccess::exists(p_output_dir); + ERR_EXPLAIN("The output directory does not exist."); + ERR_FAIL_COND_V(!dir_exists, ERR_FILE_BAD_PATH); - if (imethod.arguments.size()) { - if (imethod.is_vararg) { - String err_fail_macro = ret_void ? "ERR_FAIL_COND" : "ERR_FAIL_COND_V"; - String vararg_arg = "arg" + argc_str; - String real_argc_str = itos(imethod.arguments.size() - 1); // Arguments count without vararg - - cpp_file.push_back("\tVector<Variant> varargs;\n" - "\tint vararg_length = mono_array_length("); - cpp_file.push_back(vararg_arg); - cpp_file.push_back(");\n\tint total_length = "); - cpp_file.push_back(real_argc_str); - cpp_file.push_back(" + vararg_length;\n\t"); - cpp_file.push_back(err_fail_macro); - cpp_file.push_back("(varargs.resize(vararg_length) != OK"); - cpp_file.push_back(fail_ret); - cpp_file.push_back(");\n\tVector<Variant*> " C_LOCAL_PTRCALL_ARGS ";\n\t"); - cpp_file.push_back(err_fail_macro); - cpp_file.push_back("(call_args.resize(total_length) != OK"); - cpp_file.push_back(fail_ret); - cpp_file.push_back(");\n"); - cpp_file.push_back(c_in_statements); - cpp_file.push_back("\tfor (int i = 0; i < vararg_length; i++) " OPEN_BLOCK - "\t\tMonoObject* elem = mono_array_get("); - cpp_file.push_back(vararg_arg); - cpp_file.push_back(", MonoObject*, i);\n" - "\t\tvarargs.set(i, GDMonoMarshal::mono_object_to_variant(elem));\n" - "\t\t" C_LOCAL_PTRCALL_ARGS ".set("); - cpp_file.push_back(real_argc_str); - cpp_file.push_back(" + i, &varargs[i]);\n\t" CLOSE_BLOCK); - } else { - cpp_file.push_back(c_in_statements); - cpp_file.push_back("\tconst void* " C_LOCAL_PTRCALL_ARGS "["); - cpp_file.push_back(argc_str + "] = { "); - cpp_file.push_back(c_args_var_content + " };\n"); - } - } + List<String> output; - if (imethod.is_vararg) { - cpp_file.push_back("\tVariant::CallError vcall_error;\n\t"); + output.push_back("#include \"" GLUE_HEADER_FILE "\"\n" + "\n"); - if (!ret_void) - cpp_file.push_back(LOCAL_RET " = "); + generated_icall_funcs.clear(); - cpp_file.push_back(CS_PARAM_METHODBIND "->call(" CS_PARAM_INSTANCE ", "); - cpp_file.push_back(imethod.arguments.size() ? "(const Variant**)" C_LOCAL_PTRCALL_ARGS ".ptr()" : "NULL"); - cpp_file.push_back(", total_length, vcall_error);\n"); - } else { - cpp_file.push_back("\t" CS_PARAM_METHODBIND "->ptrcall(" CS_PARAM_INSTANCE ", "); - cpp_file.push_back(imethod.arguments.size() ? C_LOCAL_PTRCALL_ARGS ", " : "NULL, "); - cpp_file.push_back(!ret_void ? "&" LOCAL_RET ");\n" : "NULL);\n"); - } + for (Map<String, TypeInterface>::Element *type_elem = obj_types.front(); type_elem; type_elem = type_elem->next()) { + const TypeInterface &itype = type_elem->get(); - if (!ret_void) { - if (return_type->c_out.empty()) - cpp_file.push_back("\treturn " LOCAL_RET ";\n"); - else - cpp_file.push_back(sformat(return_type->c_out, return_type->c_type_out, LOCAL_RET, return_type->name)); - } + List<InternalCall> &custom_icalls = itype.api_type == ClassDB::API_EDITOR ? editor_custom_icalls : core_custom_icalls; + + OS::get_singleton()->print(String("Generating " + itype.name + "...\n").utf8()); - cpp_file.push_back(CLOSE_BLOCK "\n"); + String ctor_method(ICALL_PREFIX + itype.proxy_name + "_Ctor"); - if (im_icall->editor_only) - cpp_file.push_back("#endif // TOOLS_ENABLED\n"); + for (const List<MethodInterface>::Element *E = itype.methods.front(); E; E = E->next()) { + const MethodInterface &imethod = E->get(); + Error method_err = _generate_glue_method(itype, imethod, output); + if (method_err != OK) { + ERR_EXPLAIN("Failed to generate method '" + imethod.name + "' for class '" + itype.name + "'"); + ERR_FAIL_V(method_err); } } @@ -1296,11 +1167,11 @@ Error BindingsGenerator::generate_glue(const String &p_output_dir) { if (!find_icall_by_name(singleton_icall.name, custom_icalls)) custom_icalls.push_back(singleton_icall); - cpp_file.push_back("Object* "); - cpp_file.push_back(singleton_icall_name); - cpp_file.push_back("() " OPEN_BLOCK "\treturn ProjectSettings::get_singleton()->get_singleton_object(\""); - cpp_file.push_back(itype.proxy_name); - cpp_file.push_back("\");\n" CLOSE_BLOCK "\n"); + output.push_back("Object* "); + output.push_back(singleton_icall_name); + output.push_back("() " OPEN_BLOCK "\treturn ProjectSettings::get_singleton()->get_singleton_object(\""); + output.push_back(itype.proxy_name); + output.push_back("\");\n" CLOSE_BLOCK "\n"); } if (itype.is_instantiable) { @@ -1309,36 +1180,36 @@ Error BindingsGenerator::generate_glue(const String &p_output_dir) { if (!find_icall_by_name(ctor_icall.name, custom_icalls)) custom_icalls.push_back(ctor_icall); - cpp_file.push_back("Object* "); - cpp_file.push_back(ctor_method); - cpp_file.push_back("(MonoObject* obj) " OPEN_BLOCK - "\t" C_MACRO_OBJECT_CONSTRUCT "(instance, \""); - cpp_file.push_back(itype.name); - cpp_file.push_back("\");\n" - "\t" C_METHOD_TIE_MANAGED_TO_UNMANAGED "(obj, instance);\n" - "\treturn instance;\n" CLOSE_BLOCK "\n"); + output.push_back("Object* "); + output.push_back(ctor_method); + output.push_back("(MonoObject* obj) " OPEN_BLOCK + "\t" C_MACRO_OBJECT_CONSTRUCT "(instance, \""); + output.push_back(itype.name); + output.push_back("\");\n" + "\t" C_METHOD_TIE_MANAGED_TO_UNMANAGED "(obj, instance);\n" + "\treturn instance;\n" CLOSE_BLOCK "\n"); } } - cpp_file.push_back("namespace GodotSharpBindings\n" OPEN_BLOCK); - cpp_file.push_back("uint64_t get_core_api_hash() { return "); - cpp_file.push_back(itos(GDMono::get_singleton()->get_api_core_hash()) + "; }\n"); - cpp_file.push_back("#ifdef TOOLS_ENABLED\n" - "uint64_t get_editor_api_hash() { return "); - cpp_file.push_back(itos(GDMono::get_singleton()->get_api_editor_hash()) + - "; }\n#endif // TOOLS_ENABLED\n"); - cpp_file.push_back("void register_generated_icalls() " OPEN_BLOCK); - -#define ADD_INTERNAL_CALL_REGISTRATION(m_icall) \ - { \ - cpp_file.push_back("\tmono_add_internal_call("); \ - cpp_file.push_back("\"" BINDINGS_NAMESPACE "."); \ - cpp_file.push_back(m_icall.editor_only ? CS_CLASS_NATIVECALLS_EDITOR : CS_CLASS_NATIVECALLS); \ - cpp_file.push_back("::"); \ - cpp_file.push_back(m_icall.name); \ - cpp_file.push_back("\", (void*)"); \ - cpp_file.push_back(m_icall.name); \ - cpp_file.push_back(");\n"); \ + output.push_back("namespace GodotSharpBindings\n" OPEN_BLOCK); + output.push_back("uint64_t get_core_api_hash() { return "); + output.push_back(itos(GDMono::get_singleton()->get_api_core_hash()) + "; }\n"); + output.push_back("#ifdef TOOLS_ENABLED\n" + "uint64_t get_editor_api_hash() { return "); + output.push_back(itos(GDMono::get_singleton()->get_api_editor_hash()) + + "; }\n#endif // TOOLS_ENABLED\n"); + output.push_back("void register_generated_icalls() " OPEN_BLOCK); + +#define ADD_INTERNAL_CALL_REGISTRATION(m_icall) \ + { \ + output.push_back("\tmono_add_internal_call("); \ + output.push_back("\"" BINDINGS_NAMESPACE "."); \ + output.push_back(m_icall.editor_only ? CS_CLASS_NATIVECALLS_EDITOR : CS_CLASS_NATIVECALLS); \ + output.push_back("::"); \ + output.push_back(m_icall.name); \ + output.push_back("\", (void*)"); \ + output.push_back(m_icall.name); \ + output.push_back(");\n"); \ } bool tools_sequence = false; @@ -1347,11 +1218,11 @@ Error BindingsGenerator::generate_glue(const String &p_output_dir) { if (tools_sequence) { if (!E->get().editor_only) { tools_sequence = false; - cpp_file.push_back("#endif\n"); + output.push_back("#endif\n"); } } else { if (E->get().editor_only) { - cpp_file.push_back("#ifdef TOOLS_ENABLED\n"); + output.push_back("#ifdef TOOLS_ENABLED\n"); tools_sequence = true; } } @@ -1361,24 +1232,23 @@ Error BindingsGenerator::generate_glue(const String &p_output_dir) { if (tools_sequence) { tools_sequence = false; - cpp_file.push_back("#endif\n"); + output.push_back("#endif\n"); } - cpp_file.push_back("#ifdef TOOLS_ENABLED\n"); + output.push_back("#ifdef TOOLS_ENABLED\n"); for (const List<InternalCall>::Element *E = editor_custom_icalls.front(); E; E = E->next()) ADD_INTERNAL_CALL_REGISTRATION(E->get()); - cpp_file.push_back("#endif // TOOLS_ENABLED\n"); + output.push_back("#endif // TOOLS_ENABLED\n"); for (const List<InternalCall>::Element *E = method_icalls.front(); E; E = E->next()) { - if (tools_sequence) { if (!E->get().editor_only) { tools_sequence = false; - cpp_file.push_back("#endif\n"); + output.push_back("#endif\n"); } } else { if (E->get().editor_only) { - cpp_file.push_back("#ifdef TOOLS_ENABLED\n"); + output.push_back("#ifdef TOOLS_ENABLED\n"); tools_sequence = true; } } @@ -1388,18 +1258,18 @@ Error BindingsGenerator::generate_glue(const String &p_output_dir) { if (tools_sequence) { tools_sequence = false; - cpp_file.push_back("#endif\n"); + output.push_back("#endif\n"); } #undef ADD_INTERNAL_CALL_REGISTRATION - cpp_file.push_back(CLOSE_BLOCK "}\n"); + output.push_back(CLOSE_BLOCK "}\n"); - Error save_err = _save_file(path_join(p_output_dir, "mono_glue.gen.cpp"), cpp_file); + Error save_err = _save_file(path_join(p_output_dir, "mono_glue.gen.cpp"), output); if (save_err != OK) return save_err; - OS::get_singleton()->print("Mono glue generated successfully!\n"); + OS::get_singleton()->print("Mono glue generated successfully\n"); return OK; } @@ -1420,6 +1290,163 @@ Error BindingsGenerator::_save_file(const String &p_path, const List<String> &p_ return OK; } +Error BindingsGenerator::_generate_glue_method(const BindingsGenerator::TypeInterface &p_itype, const BindingsGenerator::MethodInterface &p_imethod, List<String> &p_output) { + + if (p_imethod.is_virtual) + return OK; // Ignore + + bool ret_void = p_imethod.return_type == "void"; + + const TypeInterface *return_type = _get_type_by_name_or_placeholder(p_imethod.return_type); + + String argc_str = itos(p_imethod.arguments.size()); + + String c_func_sig = "MethodBind* " CS_PARAM_METHODBIND ", " + p_itype.c_type_in + " " CS_PARAM_INSTANCE; + String c_in_statements; + String c_args_var_content; + + // Get arguments information + int i = 0; + for (const List<ArgumentInterface>::Element *F = p_imethod.arguments.front(); F; F = F->next()) { + const ArgumentInterface &iarg = F->get(); + const TypeInterface *arg_type = _get_type_by_name_or_placeholder(iarg.type); + + String c_param_name = "arg" + itos(i + 1); + + if (p_imethod.is_vararg) { + if (i < p_imethod.arguments.size() - 1) { + c_in_statements += sformat(arg_type->c_in.size() ? arg_type->c_in : TypeInterface::DEFAULT_VARARG_C_IN, "Variant", c_param_name); + c_in_statements += "\t" C_LOCAL_PTRCALL_ARGS ".set(0, "; + c_in_statements += sformat("&%s_in", c_param_name); + c_in_statements += ");\n"; + } + } else { + if (i > 0) + c_args_var_content += ", "; + if (arg_type->c_in.size()) + c_in_statements += sformat(arg_type->c_in, arg_type->c_type, c_param_name); + c_args_var_content += sformat(arg_type->c_arg_in, c_param_name); + } + + c_func_sig += ", "; + c_func_sig += arg_type->c_type_in; + c_func_sig += " "; + c_func_sig += c_param_name; + + i++; + } + + const Map<const MethodInterface *, const InternalCall *>::Element *match = method_icalls_map.find(&p_imethod); + ERR_FAIL_NULL_V(match, ERR_BUG); + + const InternalCall *im_icall = match->value(); + String icall_method = im_icall->name; + + if (!generated_icall_funcs.find(im_icall)) { + generated_icall_funcs.push_back(im_icall); + + if (im_icall->editor_only) + p_output.push_back("#ifdef TOOLS_ENABLED\n"); + + // Generate icall function + + p_output.push_back(ret_void ? "void " : return_type->c_type_out + " "); + p_output.push_back(icall_method); + p_output.push_back("("); + p_output.push_back(c_func_sig); + p_output.push_back(") " OPEN_BLOCK); + + String fail_ret = ret_void ? "" : ", " + (return_type->c_type_out.ends_with("*") ? "NULL" : return_type->c_type_out + "()"); + + if (!ret_void) { + String ptrcall_return_type; + String initialization; + + if (return_type->is_object_type) { + ptrcall_return_type = return_type->is_reference ? "Ref<Reference>" : return_type->c_type; + initialization = return_type->is_reference ? "" : " = NULL"; + } else { + ptrcall_return_type = return_type->c_type; + } + + p_output.push_back("\t" + ptrcall_return_type); + p_output.push_back(" " LOCAL_RET); + p_output.push_back(initialization + ";\n"); + p_output.push_back("\tERR_FAIL_NULL_V(" CS_PARAM_INSTANCE); + p_output.push_back(fail_ret); + p_output.push_back(");\n"); + } else { + p_output.push_back("\tERR_FAIL_NULL(" CS_PARAM_INSTANCE ");\n"); + } + + if (p_imethod.arguments.size()) { + if (p_imethod.is_vararg) { + String err_fail_macro = ret_void ? "ERR_FAIL_COND" : "ERR_FAIL_COND_V"; + String vararg_arg = "arg" + argc_str; + String real_argc_str = itos(p_imethod.arguments.size() - 1); // Arguments count without vararg + + p_output.push_back("\tVector<Variant> varargs;\n" + "\tint vararg_length = mono_array_length("); + p_output.push_back(vararg_arg); + p_output.push_back(");\n\tint total_length = "); + p_output.push_back(real_argc_str); + p_output.push_back(" + vararg_length;\n\t"); + p_output.push_back(err_fail_macro); + p_output.push_back("(varargs.resize(vararg_length) != OK"); + p_output.push_back(fail_ret); + p_output.push_back(");\n\tVector<Variant*> " C_LOCAL_PTRCALL_ARGS ";\n\t"); + p_output.push_back(err_fail_macro); + p_output.push_back("(call_args.resize(total_length) != OK"); + p_output.push_back(fail_ret); + p_output.push_back(");\n"); + p_output.push_back(c_in_statements); + p_output.push_back("\tfor (int i = 0; i < vararg_length; i++) " OPEN_BLOCK + "\t\tMonoObject* elem = mono_array_get("); + p_output.push_back(vararg_arg); + p_output.push_back(", MonoObject*, i);\n" + "\t\tvarargs.set(i, GDMonoMarshal::mono_object_to_variant(elem));\n" + "\t\t" C_LOCAL_PTRCALL_ARGS ".set("); + p_output.push_back(real_argc_str); + p_output.push_back(" + i, &varargs[i]);\n\t" CLOSE_BLOCK); + } else { + p_output.push_back(c_in_statements); + p_output.push_back("\tconst void* " C_LOCAL_PTRCALL_ARGS "["); + p_output.push_back(argc_str + "] = { "); + p_output.push_back(c_args_var_content + " };\n"); + } + } + + if (p_imethod.is_vararg) { + p_output.push_back("\tVariant::CallError vcall_error;\n\t"); + + if (!ret_void) + p_output.push_back(LOCAL_RET " = "); + + p_output.push_back(CS_PARAM_METHODBIND "->call(" CS_PARAM_INSTANCE ", "); + p_output.push_back(p_imethod.arguments.size() ? "(const Variant**)" C_LOCAL_PTRCALL_ARGS ".ptr()" : "NULL"); + p_output.push_back(", total_length, vcall_error);\n"); + } else { + p_output.push_back("\t" CS_PARAM_METHODBIND "->ptrcall(" CS_PARAM_INSTANCE ", "); + p_output.push_back(p_imethod.arguments.size() ? C_LOCAL_PTRCALL_ARGS ", " : "NULL, "); + p_output.push_back(!ret_void ? "&" LOCAL_RET ");\n" : "NULL);\n"); + } + + if (!ret_void) { + if (return_type->c_out.empty()) + p_output.push_back("\treturn " LOCAL_RET ";\n"); + else + p_output.push_back(sformat(return_type->c_out, return_type->c_type_out, LOCAL_RET, return_type->name)); + } + + p_output.push_back(CLOSE_BLOCK "\n"); + + if (im_icall->editor_only) + p_output.push_back("#endif // TOOLS_ENABLED\n"); + } + + return OK; +} + const BindingsGenerator::TypeInterface *BindingsGenerator::_get_type_by_name_or_null(const String &p_name) { const Map<String, TypeInterface>::Element *match = builtin_types.find(p_name); @@ -2077,7 +2104,8 @@ void BindingsGenerator::handle_cmdline_args(const List<String> &p_cmdline_args) const List<String>::Element *path_elem = elem->next(); if (path_elem) { - get_singleton().generate_glue(path_elem->get()); + if (get_singleton().generate_glue(path_elem->get()) != OK) + ERR_PRINT("Mono glue generation failed"); elem = elem->next(); } else { ERR_PRINTS("--generate-mono-glue: No output directory specified"); @@ -2090,7 +2118,8 @@ void BindingsGenerator::handle_cmdline_args(const List<String> &p_cmdline_args) const List<String>::Element *path_elem = elem->next(); if (path_elem) { - get_singleton().generate_cs_core_project(path_elem->get()); + if (get_singleton().generate_cs_core_project(path_elem->get()) != OK) + ERR_PRINT("Generation of solution and C# project for the Core API failed"); elem = elem->next(); } else { ERR_PRINTS(cs_core_api_option + ": No output directory specified"); @@ -2104,7 +2133,8 @@ void BindingsGenerator::handle_cmdline_args(const List<String> &p_cmdline_args) if (path_elem) { if (path_elem->next()) { - get_singleton().generate_cs_editor_project(path_elem->get(), path_elem->next()->get()); + if (get_singleton().generate_cs_editor_project(path_elem->get(), path_elem->next()->get()) != OK) + ERR_PRINT("Generation of solution and C# project for the Editor API failed"); elem = path_elem->next(); } else { ERR_PRINTS(cs_editor_api_option + ": No hint path for the Core API dll specified"); diff --git a/modules/mono/editor/bindings_generator.h b/modules/mono/editor/bindings_generator.h index 437a566556..dfa3aa9911 100644 --- a/modules/mono/editor/bindings_generator.h +++ b/modules/mono/editor/bindings_generator.h @@ -368,6 +368,8 @@ class BindingsGenerator { List<InternalCall> method_icalls; Map<const MethodInterface *, const InternalCall *> method_icalls_map; + List<const InternalCall *> generated_icall_funcs; + List<InternalCall> core_custom_icalls; List<InternalCall> editor_custom_icalls; @@ -404,6 +406,11 @@ class BindingsGenerator { Error _generate_cs_type(const TypeInterface &itype, const String &p_output_file); + Error _generate_cs_property(const TypeInterface &p_itype, const DocData::PropertyDoc &p_prop_doc, List<String> &p_output); + Error _generate_cs_method(const TypeInterface &p_itype, const MethodInterface &p_imethod, int &p_method_bind_count, List<String> &p_output); + + Error _generate_glue_method(const TypeInterface &p_itype, const MethodInterface &p_imethod, List<String> &p_output); + Error _save_file(const String &path, const List<String> &content); BindingsGenerator(); diff --git a/modules/mono/editor/godotsharp_builds.cpp b/modules/mono/editor/godotsharp_builds.cpp index d3808769fb..aaea1bc839 100644 --- a/modules/mono/editor/godotsharp_builds.cpp +++ b/modules/mono/editor/godotsharp_builds.cpp @@ -71,7 +71,7 @@ String _find_build_engine_on_unix(const String &p_name) { } #endif -MonoString *godot_icall_BuildInstance_get_MSBuildPath() { +void godot_icall_BuildInstance_get_MSBuildInfo(MonoString **r_msbuild_path, MonoString **r_framework_path) { GodotSharpBuilds::BuildTool build_tool = GodotSharpBuilds::BuildTool(int(EditorSettings::get_singleton()->get("mono/builds/build_tool"))); @@ -84,11 +84,17 @@ MonoString *godot_icall_BuildInstance_get_MSBuildPath() { if (!msbuild_tools_path.ends_with("\\")) msbuild_tools_path += "\\"; - return GDMonoMarshal::mono_string_from_godot(msbuild_tools_path + "MSBuild.exe"); + *r_msbuild_path = GDMonoMarshal::mono_string_from_godot(msbuild_tools_path + "MSBuild.exe"); + + // FrameworkPathOverride + *r_framework_path = GDMonoMarshal::mono_string_from_godot(GDMono::get_singleton()->get_mono_reg_info().assembly_dir); + + return; } - OS::get_singleton()->print("Cannot find System's MSBuild. Trying with Mono's...\n"); - } + if (OS::get_singleton()->is_stdout_verbose()) + OS::get_singleton()->print("Cannot find System's MSBuild. Trying with Mono's...\n"); + } // fall through case GodotSharpBuilds::MSBUILD_MONO: { String msbuild_path = GDMono::get_singleton()->get_mono_reg_info().bin_dir.plus_file("msbuild.bat"); @@ -96,17 +102,10 @@ MonoString *godot_icall_BuildInstance_get_MSBuildPath() { WARN_PRINTS("Cannot find msbuild ('mono/builds/build_tool'). Tried with path: " + msbuild_path); } - return GDMonoMarshal::mono_string_from_godot(msbuild_path); - } - case GodotSharpBuilds::XBUILD: { - String xbuild_path = GDMono::get_singleton()->get_mono_reg_info().bin_dir.plus_file("xbuild.bat"); - - if (!FileAccess::exists(xbuild_path)) { - WARN_PRINTS("Cannot find xbuild ('mono/builds/build_tool'). Tried with path: " + xbuild_path); - } + *r_msbuild_path = GDMonoMarshal::mono_string_from_godot(msbuild_path); - return GDMonoMarshal::mono_string_from_godot(xbuild_path); - } + return; + } break; default: ERR_EXPLAIN("You don't deserve to live"); CRASH_NOW(); @@ -118,25 +117,27 @@ MonoString *godot_icall_BuildInstance_get_MSBuildPath() { if (build_tool != GodotSharpBuilds::XBUILD) { if (msbuild_path.empty()) { WARN_PRINT("Cannot find msbuild ('mono/builds/build_tool')."); - return NULL; + return; } } else { if (xbuild_path.empty()) { WARN_PRINT("Cannot find xbuild ('mono/builds/build_tool')."); - return NULL; + return; } } - return GDMonoMarshal::mono_string_from_godot(build_tool != GodotSharpBuilds::XBUILD ? msbuild_path : xbuild_path); + *r_msbuild_path = GDMonoMarshal::mono_string_from_godot(build_tool != GodotSharpBuilds::XBUILD ? msbuild_path : xbuild_path); + + return; #else - return NULL; + return; #endif } void GodotSharpBuilds::_register_internal_calls() { mono_add_internal_call("GodotSharpTools.Build.BuildSystem::godot_icall_BuildInstance_ExitCallback", (void *)godot_icall_BuildInstance_ExitCallback); - mono_add_internal_call("GodotSharpTools.Build.BuildInstance::godot_icall_BuildInstance_get_MSBuildPath", (void *)godot_icall_BuildInstance_get_MSBuildPath); + mono_add_internal_call("GodotSharpTools.Build.BuildInstance::godot_icall_BuildInstance_get_MSBuildInfo", (void *)godot_icall_BuildInstance_get_MSBuildInfo); } void GodotSharpBuilds::show_build_error_dialog(const String &p_message) { @@ -269,7 +270,7 @@ bool GodotSharpBuilds::make_api_sln(GodotSharpBuilds::APIType p_api_type) { return true; } -bool godotsharp_build_callback() { +bool GodotSharpBuilds::build_project_blocking() { if (!FileAccess::exists(GodotSharpDirs::get_project_sln_path())) return true; // No solution to build @@ -348,14 +349,27 @@ GodotSharpBuilds::GodotSharpBuilds() { singleton = this; - EditorNode::get_singleton()->add_build_callback(&godotsharp_build_callback); + EditorNode::get_singleton()->add_build_callback(&GodotSharpBuilds::build_project_blocking); // Build tool settings EditorSettings *ed_settings = EditorSettings::get_singleton(); if (!ed_settings->has_setting("mono/builds/build_tool")) { - ed_settings->set_setting("mono/builds/build_tool", MSBUILD); + ed_settings->set_setting("mono/builds/build_tool", +#ifdef WINDOWS_ENABLED + // TODO: Default to MSBUILD_MONO if its csc.exe issue is fixed in the installed mono version + MSBUILD +#else + MSBUILD_MONO +#endif + ); } - ed_settings->add_property_hint(PropertyInfo(Variant::INT, "mono/builds/build_tool", PROPERTY_HINT_ENUM, "MSBuild (System),MSBuild (Mono),xbuild")); + ed_settings->add_property_hint(PropertyInfo(Variant::INT, "mono/builds/build_tool", PROPERTY_HINT_ENUM, +#ifdef WINDOWS_ENABLED + "MSBuild (Mono),MSBuild (System)" +#else + "MSBuild (Mono),xbuild (Deprecated)" +#endif + )); } GodotSharpBuilds::~GodotSharpBuilds() { diff --git a/modules/mono/editor/godotsharp_builds.h b/modules/mono/editor/godotsharp_builds.h index 6d5fa3b44a..7d2f38a774 100644 --- a/modules/mono/editor/godotsharp_builds.h +++ b/modules/mono/editor/godotsharp_builds.h @@ -67,9 +67,12 @@ public: }; enum BuildTool { - MSBUILD, MSBUILD_MONO, - XBUILD +#ifdef WINDOWS_ENABLED + MSBUILD +#else + XBUILD // Deprecated +#endif }; _FORCE_INLINE_ static GodotSharpBuilds *get_singleton() { return singleton; } @@ -89,6 +92,8 @@ public: static bool make_api_sln(APIType p_api_type); + static bool build_project_blocking(); + GodotSharpBuilds(); ~GodotSharpBuilds(); }; diff --git a/modules/mono/editor/godotsharp_editor.cpp b/modules/mono/editor/godotsharp_editor.cpp index 30e7653256..39c57ee0be 100644 --- a/modules/mono/editor/godotsharp_editor.cpp +++ b/modules/mono/editor/godotsharp_editor.cpp @@ -46,21 +46,6 @@ #include "../utils/mono_reg_utils.h" #endif -class MonoReloadNode : public Node { - GDCLASS(MonoReloadNode, Node) - -protected: - void _notification(int p_what) { - switch (p_what) { - case MainLoop::NOTIFICATION_WM_FOCUS_IN: { - CSharpLanguage::get_singleton()->reload_assemblies_if_needed(true); - } break; - default: { - } break; - }; - } -}; - GodotSharpEditor *GodotSharpEditor::singleton = NULL; bool GodotSharpEditor::_create_project_solution() { @@ -71,6 +56,10 @@ bool GodotSharpEditor::_create_project_solution() { String path = OS::get_singleton()->get_resource_dir(); String name = ProjectSettings::get_singleton()->get("application/config/name"); + if (name.empty()) { + name = "UnnamedProject"; + } + String guid = CSharpProject::generate_game_project(path, name); if (guid.length()) { @@ -254,3 +243,49 @@ GodotSharpEditor::~GodotSharpEditor() { monodevel_instance = NULL; } } + +MonoReloadNode *MonoReloadNode::singleton = NULL; + +void MonoReloadNode::_reload_timer_timeout() { + + CSharpLanguage::get_singleton()->reload_assemblies_if_needed(false); +} + +void MonoReloadNode::restart_reload_timer() { + + reload_timer->stop(); + reload_timer->start(); +} + +void MonoReloadNode::_bind_methods() { + + ClassDB::bind_method(D_METHOD("_reload_timer_timeout"), &MonoReloadNode::_reload_timer_timeout); +} + +void MonoReloadNode::_notification(int p_what) { + switch (p_what) { + case MainLoop::NOTIFICATION_WM_FOCUS_IN: { + restart_reload_timer(); + CSharpLanguage::get_singleton()->reload_assemblies_if_needed(true); + } break; + default: { + } break; + }; +} + +MonoReloadNode::MonoReloadNode() { + + singleton = this; + + reload_timer = memnew(Timer); + add_child(reload_timer); + reload_timer->set_one_shot(false); + reload_timer->set_wait_time(EDITOR_DEF("mono/assembly_watch_interval_sec", 0.5)); + reload_timer->connect("timeout", this, "_reload_timer_timeout"); + reload_timer->start(); +} + +MonoReloadNode::~MonoReloadNode() { + + singleton = NULL; +} diff --git a/modules/mono/editor/godotsharp_editor.h b/modules/mono/editor/godotsharp_editor.h index 1ecb8c7a94..7b4b50b172 100644 --- a/modules/mono/editor/godotsharp_editor.h +++ b/modules/mono/editor/godotsharp_editor.h @@ -84,4 +84,27 @@ public: ~GodotSharpEditor(); }; +class MonoReloadNode : public Node { + GDCLASS(MonoReloadNode, Node) + + Timer *reload_timer; + + void _reload_timer_timeout(); + + static MonoReloadNode *singleton; + +protected: + static void _bind_methods(); + + void _notification(int p_what); + +public: + _FORCE_INLINE_ static MonoReloadNode *get_singleton() { return singleton; } + + void restart_reload_timer(); + + MonoReloadNode(); + ~MonoReloadNode(); +}; + #endif // GODOTSHARP_EDITOR_H diff --git a/modules/mono/editor/mono_bottom_panel.cpp b/modules/mono/editor/mono_bottom_panel.cpp index 8d6a618ee8..31dc09856a 100644 --- a/modules/mono/editor/mono_bottom_panel.cpp +++ b/modules/mono/editor/mono_bottom_panel.cpp @@ -139,6 +139,14 @@ void MonoBottomPanel::_errors_toggled(bool p_pressed) { build_tab->_update_issues_list(); } +void MonoBottomPanel::_build_project_pressed() { + + GodotSharpBuilds::get_singleton()->build_project_blocking(); + + MonoReloadNode::get_singleton()->restart_reload_timer(); + CSharpLanguage::get_singleton()->reload_assemblies_if_needed(true); +} + void MonoBottomPanel::_notification(int p_what) { switch (p_what) { @@ -153,6 +161,7 @@ void MonoBottomPanel::_notification(int p_what) { void MonoBottomPanel::_bind_methods() { + ClassDB::bind_method(D_METHOD("_build_project_pressed"), &MonoBottomPanel::_build_project_pressed); ClassDB::bind_method(D_METHOD("_warnings_toggled", "pressed"), &MonoBottomPanel::_warnings_toggled); ClassDB::bind_method(D_METHOD("_errors_toggled", "pressed"), &MonoBottomPanel::_errors_toggled); ClassDB::bind_method(D_METHOD("_build_tab_item_selected", "idx"), &MonoBottomPanel::_build_tab_item_selected); @@ -187,6 +196,12 @@ MonoBottomPanel::MonoBottomPanel(EditorNode *p_editor) { toolbar_hbc->set_h_size_flags(SIZE_EXPAND_FILL); panel_builds_tab->add_child(toolbar_hbc); + ToolButton *build_project_btn = memnew(ToolButton); + build_project_btn->set_text("Build Project"); + build_project_btn->set_focus_mode(FOCUS_NONE); + build_project_btn->connect("pressed", this, "_build_project_pressed"); + toolbar_hbc->add_child(build_project_btn); + toolbar_hbc->add_spacer(); warnings_btn = memnew(ToolButton); diff --git a/modules/mono/editor/mono_bottom_panel.h b/modules/mono/editor/mono_bottom_panel.h index 83da5b9809..5cc4aa3240 100644 --- a/modules/mono/editor/mono_bottom_panel.h +++ b/modules/mono/editor/mono_bottom_panel.h @@ -61,6 +61,8 @@ class MonoBottomPanel : public VBoxContainer { void _warnings_toggled(bool p_pressed); void _errors_toggled(bool p_pressed); + void _build_project_pressed(); + static MonoBottomPanel *singleton; protected: diff --git a/modules/mono/godotsharp_dirs.cpp b/modules/mono/godotsharp_dirs.cpp index 6bcf0e2355..7cc2168b70 100644 --- a/modules/mono/godotsharp_dirs.cpp +++ b/modules/mono/godotsharp_dirs.cpp @@ -122,7 +122,14 @@ private: #ifdef TOOLS_ENABLED mono_solutions_dir = mono_user_dir.plus_file("solutions"); build_logs_dir = mono_user_dir.plus_file("build_logs"); - String base_path = String("res://") + ProjectSettings::get_singleton()->get("application/config/name"); + + String name = ProjectSettings::get_singleton()->get("application/config/name"); + if (name.empty()) { + name = "UnnamedProject"; + } + + String base_path = String("res://") + name; + sln_filepath = ProjectSettings::get_singleton()->globalize_path(base_path + ".sln"); csproj_filepath = ProjectSettings::get_singleton()->globalize_path(base_path + ".csproj"); #endif diff --git a/modules/mono/mono_gd/gd_mono.cpp b/modules/mono/mono_gd/gd_mono.cpp index 4b5f5eb137..904a8ae2c7 100644 --- a/modules/mono/mono_gd/gd_mono.cpp +++ b/modules/mono/mono_gd/gd_mono.cpp @@ -369,9 +369,12 @@ bool GDMono::_load_project_assembly() { if (project_assembly) return true; - String project_assembly_name = ProjectSettings::get_singleton()->get("application/config/name"); + String name = ProjectSettings::get_singleton()->get("application/config/name"); + if (name.empty()) { + name = "UnnamedProject"; + } - bool success = _load_assembly(project_assembly_name, &project_assembly); + bool success = _load_assembly(name, &project_assembly); if (success) mono_assembly_set_main(project_assembly->get_assembly()); @@ -622,6 +625,8 @@ GDMono::~GDMono() { if (gdmono_log) memdelete(gdmono_log); + + singleton = NULL; } _GodotSharp *_GodotSharp::singleton = NULL; diff --git a/modules/mono/mono_reg_utils.py b/modules/mono/mono_reg_utils.py index e9988625f5..22c4aeaf4c 100644 --- a/modules/mono/mono_reg_utils.py +++ b/modules/mono/mono_reg_utils.py @@ -1,4 +1,5 @@ import os +import platform if os.name == 'nt': import sys @@ -11,8 +12,7 @@ if os.name == 'nt': def _reg_open_key(key, subkey): try: return winreg.OpenKey(key, subkey) - except (WindowsError, EnvironmentError) as e: - import platform + except WindowsError, OSError: if platform.architecture()[0] == '32bit': bitness_sam = winreg.KEY_WOW64_64KEY else: @@ -20,39 +20,93 @@ def _reg_open_key(key, subkey): return winreg.OpenKey(key, subkey, 0, winreg.KEY_READ | bitness_sam) -def _find_mono_in_reg(subkey): +def _reg_open_key_bits(key, subkey, bits): + sam = winreg.KEY_READ + + if platform.architecture()[0] == '32bit': + if bits == '64': + # Force 32bit process to search in 64bit registry + sam |= winreg.KEY_WOW64_64KEY + else: + if bits == '32': + # Force 64bit process to search in 32bit registry + sam |= winreg.KEY_WOW64_32KEY + + return winreg.OpenKey(key, subkey, 0, sam) + + +def _find_mono_in_reg(subkey, bits): try: - with _reg_open_key(winreg.HKEY_LOCAL_MACHINE, subkey) as hKey: + with _reg_open_key_bits(winreg.HKEY_LOCAL_MACHINE, subkey, bits) as hKey: value, regtype = winreg.QueryValueEx(hKey, 'SdkInstallRoot') return value - except (WindowsError, EnvironmentError) as e: + except WindowsError, OSError: return None -def _find_mono_in_reg_old(subkey): + +def _find_mono_in_reg_old(subkey, bits): try: - with _reg_open_key(winreg.HKEY_LOCAL_MACHINE, subkey) as hKey: + with _reg_open_key_bits(winreg.HKEY_LOCAL_MACHINE, subkey, bits) as hKey: default_clr, regtype = winreg.QueryValueEx(hKey, 'DefaultCLR') if default_clr: - return _find_mono_in_reg(subkey + '\\' + default_clr) + return _find_mono_in_reg(subkey + '\\' + default_clr, bits) return None except (WindowsError, EnvironmentError): return None -def find_mono_root_dir(): - dir = _find_mono_in_reg(r'SOFTWARE\Mono') - if dir: - return dir - dir = _find_mono_in_reg_old(r'SOFTWARE\Novell\Mono') - if dir: - return dir - return None +def find_mono_root_dir(bits): + root_dir = _find_mono_in_reg(r'SOFTWARE\Mono', bits) + if root_dir is not None: + return root_dir + root_dir = _find_mono_in_reg_old(r'SOFTWARE\Novell\Mono', bits) + if root_dir is not None: + return root_dir + return '' def find_msbuild_tools_path_reg(): + import subprocess + + vswhere = os.getenv('PROGRAMFILES(X86)') + if not vswhere: + vswhere = os.getenv('PROGRAMFILES') + vswhere += r'\Microsoft Visual Studio\Installer\vswhere.exe' + + vswhere_args = ['-latest', '-requires', 'Microsoft.Component.MSBuild'] + try: - with _reg_open_key(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0') as hKey: + lines = subprocess.check_output([vswhere] + vswhere_args).splitlines() + + for line in lines: + parts = line.split(':', 1) + + if len(parts) < 2 or parts[0] != 'installationPath': + continue + + val = parts[1].strip() + + if not val: + raise ValueError('Value of `installationPath` entry is empty') + + return os.path.join(val, "MSBuild\\15.0\\Bin") + + raise ValueError('Cannot find `installationPath` entry') + except ValueError as e: + print('Error reading output from vswhere: ' + e.message) + except WindowsError: + pass # Fine, vswhere not found + except subprocess.CalledProcessError, OSError: + pass + + # Try to find 14.0 in the Registry + + try: + subkey = r'SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0' + with _reg_open_key(winreg.HKEY_LOCAL_MACHINE, subkey) as hKey: value, regtype = winreg.QueryValueEx(hKey, 'MSBuildToolsPath') return value - except (WindowsError, EnvironmentError) as e: - return None + except WindowsError, OSError: + return '' + + return '' diff --git a/modules/opus/audio_stream_opus.cpp b/modules/opus/audio_stream_opus.cpp index c7748b9b21..06eab4c94d 100644 --- a/modules/opus/audio_stream_opus.cpp +++ b/modules/opus/audio_stream_opus.cpp @@ -267,7 +267,7 @@ void AudioStreamPlaybackOpus::seek(float p_time) { frames_mixed = osrate * p_time; } -int AudioStreamPlaybackOpus::mix(int16_t *p_bufer, int p_frames) { +int AudioStreamPlaybackOpus::mix(int16_t *p_buffer, int p_frames) { if (!playing) return 0; @@ -281,7 +281,7 @@ int AudioStreamPlaybackOpus::mix(int16_t *p_bufer, int p_frames) { break; } - int ret = op_read(opus_file, (opus_int16 *)p_bufer, todo * stream_channels, ¤t_section); + int ret = op_read(opus_file, (opus_int16 *)p_buffer, todo * stream_channels, ¤t_section); if (ret < 0) { playing = false; ERR_EXPLAIN("Error reading Opus File: " + file); @@ -325,7 +325,7 @@ int AudioStreamPlaybackOpus::mix(int16_t *p_bufer, int p_frames) { frames_mixed += ret; - p_bufer += ret * stream_channels; + p_buffer += ret * stream_channels; p_frames -= ret; } diff --git a/modules/opus/audio_stream_opus.h b/modules/opus/audio_stream_opus.h index 7b7740a804..f8d8f585cf 100644 --- a/modules/opus/audio_stream_opus.h +++ b/modules/opus/audio_stream_opus.h @@ -107,7 +107,7 @@ public: virtual int get_minimum_buffer_size() const; - virtual int mix(int16_t *p_bufer, int p_frames); + virtual int mix(int16_t *p_buffer, int p_frames); AudioStreamPlaybackOpus(); ~AudioStreamPlaybackOpus(); diff --git a/modules/vorbis/audio_stream_ogg_vorbis.cpp b/modules/vorbis/audio_stream_ogg_vorbis.cpp index 0afb889199..6235799fc2 100644 --- a/modules/vorbis/audio_stream_ogg_vorbis.cpp +++ b/modules/vorbis/audio_stream_ogg_vorbis.cpp @@ -92,7 +92,7 @@ long AudioStreamPlaybackOGGVorbis::_ov_tell_func(void *_f) { return fa->get_position(); } -int AudioStreamPlaybackOGGVorbis::mix(int16_t *p_bufer, int p_frames) { +int AudioStreamPlaybackOGGVorbis::mix(int16_t *p_buffer, int p_frames) { if (!playing) return 0; @@ -109,9 +109,9 @@ int AudioStreamPlaybackOGGVorbis::mix(int16_t *p_bufer, int p_frames) { //printf("to mix %i - mix me %i bytes\n",to_mix,to_mix*stream_channels*sizeof(int16_t)); #ifdef BIG_ENDIAN_ENABLED - long ret = ov_read(&vf, (char *)p_bufer, todo * stream_channels * sizeof(int16_t), 1, 2, 1, ¤t_section); + long ret = ov_read(&vf, (char *)p_buffer, todo * stream_channels * sizeof(int16_t), 1, 2, 1, ¤t_section); #else - long ret = ov_read(&vf, (char *)p_bufer, todo * stream_channels * sizeof(int16_t), 0, 2, 1, ¤t_section); + long ret = ov_read(&vf, (char *)p_buffer, todo * stream_channels * sizeof(int16_t), 0, 2, 1, ¤t_section); #endif if (ret < 0) { @@ -162,7 +162,7 @@ int AudioStreamPlaybackOGGVorbis::mix(int16_t *p_bufer, int p_frames) { frames_mixed += ret; - p_bufer += ret * stream_channels; + p_buffer += ret * stream_channels; p_frames -= ret; } diff --git a/modules/vorbis/audio_stream_ogg_vorbis.h b/modules/vorbis/audio_stream_ogg_vorbis.h index 929b2651e9..79eadec56e 100644 --- a/modules/vorbis/audio_stream_ogg_vorbis.h +++ b/modules/vorbis/audio_stream_ogg_vorbis.h @@ -103,7 +103,7 @@ public: virtual int get_mix_rate() const { return stream_srate; } virtual int get_minimum_buffer_size() const { return 0; } - virtual int mix(int16_t *p_bufer, int p_frames); + virtual int mix(int16_t *p_buffer, int p_frames); AudioStreamPlaybackOGGVorbis(); ~AudioStreamPlaybackOGGVorbis(); |