diff options
author | Andreas Haas <Hinsbart@users.noreply.github.com> | 2017-10-03 00:13:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-03 00:13:40 +0200 |
commit | b0194a33f65970948ef66819913bf3034a3a22e8 (patch) | |
tree | 35e21b53b10bbd525506bb5b72b7f89214e1234f /modules/mono/editor | |
parent | 5cd68abf8896fd86a33c048d6fece61c3cd3f8e5 (diff) | |
parent | d5caf71c3fcdeb422d1b0ea97a836fcdb57a8713 (diff) |
Merge pull request #11739 from neikeq/tengo-el-mono
Moved mono module here
Diffstat (limited to 'modules/mono/editor')
25 files changed, 5421 insertions, 0 deletions
diff --git a/modules/mono/editor/GodotSharpTools/Build/BuildSystem.cs b/modules/mono/editor/GodotSharpTools/Build/BuildSystem.cs new file mode 100644 index 0000000000..256e64ddde --- /dev/null +++ b/modules/mono/editor/GodotSharpTools/Build/BuildSystem.cs @@ -0,0 +1,335 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Diagnostics; +using System.IO; +using System.Runtime.CompilerServices; +using System.Security; +using Microsoft.Build.Framework; + +namespace GodotSharpTools.Build +{ + public class BuildInstance : IDisposable + { + [MethodImpl(MethodImplOptions.InternalCall)] + internal 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 static string MSBuildPath + { + get { return godot_icall_BuildInstance_get_MSBuildPath(); } + } + + private string solution; + private string config; + + private Process process; + + private int exitCode; + public int ExitCode { get { return exitCode; } } + + public bool IsRunning { get { return process != null && !process.HasExited; } } + + public BuildInstance(string solution, string config) + { + this.solution = solution; + this.config = config; + } + + public bool Build(string loggerAssemblyPath, string loggerOutputDir, string[] customProperties = null) + { + string compilerArgs = BuildArguments(loggerAssemblyPath, loggerOutputDir, customProperties); + + ProcessStartInfo startInfo = new ProcessStartInfo(MSBuildPath, compilerArgs); + + // No console output, thanks + startInfo.RedirectStandardOutput = true; + startInfo.RedirectStandardError = true; + startInfo.UseShellExecute = false; + + // Needed when running from Developer Command Prompt for VS + RemovePlatformVariable(startInfo.EnvironmentVariables); + + using (Process process = new Process()) + { + process.StartInfo = startInfo; + + process.Start(); + + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + process.WaitForExit(); + + exitCode = process.ExitCode; + } + + return true; + } + + public bool BuildAsync(string loggerAssemblyPath, string loggerOutputDir, string[] customProperties = null) + { + if (process != null) + throw new InvalidOperationException("Already in use"); + + string compilerArgs = BuildArguments(loggerAssemblyPath, loggerOutputDir, customProperties); + + ProcessStartInfo startInfo = new ProcessStartInfo("msbuild", compilerArgs); + + // No console output, thanks + startInfo.RedirectStandardOutput = true; + startInfo.RedirectStandardError = true; + startInfo.UseShellExecute = false; + + // Needed when running from Developer Command Prompt for VS + RemovePlatformVariable(startInfo.EnvironmentVariables); + + process = new Process(); + process.StartInfo = startInfo; + process.EnableRaisingEvents = true; + process.Exited += new EventHandler(BuildProcess_Exited); + + process.Start(); + + return true; + } + + private string BuildArguments(string loggerAssemblyPath, string loggerOutputDir, string[] customProperties) + { + string arguments = string.Format("{0} /v:normal /t:Build /p:{1} /l:{2},{3};{4}", + solution, + "Configuration=" + config, + typeof(GodotBuildLogger).FullName, + loggerAssemblyPath, + loggerOutputDir + ); + + if (customProperties != null) + { + foreach (string customProperty in customProperties) + { + arguments += " /p:" + customProperty; + } + } + + return arguments; + } + + private void RemovePlatformVariable(StringDictionary environmentVariables) + { + // EnvironmentVariables is case sensitive? Seriously? + + List<string> platformEnvironmentVariables = new List<string>(); + + foreach (string env in environmentVariables.Keys) + { + if (env.ToUpper() == "PLATFORM") + platformEnvironmentVariables.Add(env); + } + + foreach (string env in platformEnvironmentVariables) + environmentVariables.Remove(env); + } + + private void BuildProcess_Exited(object sender, System.EventArgs e) + { + exitCode = process.ExitCode; + + godot_icall_BuildInstance_ExitCallback(solution, config, exitCode); + + Dispose(); + } + + public void Dispose() + { + if (process != null) + { + process.Dispose(); + process = null; + } + } + } + + public class GodotBuildLogger : ILogger + { + public string Parameters { get; set; } + public LoggerVerbosity Verbosity { get; set; } + + public void Initialize(IEventSource eventSource) + { + if (null == Parameters) + throw new LoggerException("Log directory was not set."); + + string[] parameters = Parameters.Split(';'); + + string logDir = parameters[0]; + + if (String.IsNullOrEmpty(logDir)) + throw new LoggerException("Log directory was not set."); + + if (parameters.Length > 1) + throw new LoggerException("Too many parameters passed."); + + string logFile = Path.Combine(logDir, "msbuild_log.txt"); + string issuesFile = Path.Combine(logDir, "msbuild_issues.csv"); + + try + { + if (!Directory.Exists(logDir)) + Directory.CreateDirectory(logDir); + + this.logStreamWriter = new StreamWriter(logFile); + this.issuesStreamWriter = new StreamWriter(issuesFile); + } + catch (Exception ex) + { + if + ( + ex is UnauthorizedAccessException + || ex is ArgumentNullException + || ex is PathTooLongException + || ex is DirectoryNotFoundException + || ex is NotSupportedException + || ex is ArgumentException + || ex is SecurityException + || ex is IOException + ) + { + throw new LoggerException("Failed to create log file: " + ex.Message); + } + else + { + // Unexpected failure + throw; + } + } + + eventSource.ProjectStarted += new ProjectStartedEventHandler(eventSource_ProjectStarted); + eventSource.TaskStarted += new TaskStartedEventHandler(eventSource_TaskStarted); + eventSource.MessageRaised += new BuildMessageEventHandler(eventSource_MessageRaised); + eventSource.WarningRaised += new BuildWarningEventHandler(eventSource_WarningRaised); + eventSource.ErrorRaised += new BuildErrorEventHandler(eventSource_ErrorRaised); + eventSource.ProjectFinished += new ProjectFinishedEventHandler(eventSource_ProjectFinished); + } + + void eventSource_ErrorRaised(object sender, BuildErrorEventArgs e) + { + string line = String.Format("{0}({1},{2}): error {3}: {4}", e.File, e.LineNumber, e.ColumnNumber, e.Code, e.Message); + + if (e.ProjectFile.Length > 0) + line += string.Format(" [{0}]", e.ProjectFile); + + WriteLine(line); + + string errorLine = String.Format(@"error,{0},{1},{2},{3},{4},{5}", + e.File.CsvEscape(), e.LineNumber, e.ColumnNumber, + e.Code.CsvEscape(), e.Message.CsvEscape(), e.ProjectFile.CsvEscape()); + issuesStreamWriter.WriteLine(errorLine); + } + + void eventSource_WarningRaised(object sender, BuildWarningEventArgs e) + { + string line = String.Format("{0}({1},{2}): warning {3}: {4}", e.File, e.LineNumber, e.ColumnNumber, e.Code, e.Message, e.ProjectFile); + + if (e.ProjectFile != null && e.ProjectFile.Length > 0) + line += string.Format(" [{0}]", e.ProjectFile); + + WriteLine(line); + + string warningLine = String.Format(@"warning,{0},{1},{2},{3},{4},{5}", + e.File.CsvEscape(), e.LineNumber, e.ColumnNumber, + e.Code.CsvEscape(), e.Message.CsvEscape(), e.ProjectFile != null ? e.ProjectFile.CsvEscape() : string.Empty); + issuesStreamWriter.WriteLine(warningLine); + } + + void eventSource_MessageRaised(object sender, BuildMessageEventArgs e) + { + // BuildMessageEventArgs adds Importance to BuildEventArgs + // Let's take account of the verbosity setting we've been passed in deciding whether to log the message + if ((e.Importance == MessageImportance.High && IsVerbosityAtLeast(LoggerVerbosity.Minimal)) + || (e.Importance == MessageImportance.Normal && IsVerbosityAtLeast(LoggerVerbosity.Normal)) + || (e.Importance == MessageImportance.Low && IsVerbosityAtLeast(LoggerVerbosity.Detailed)) + ) + { + WriteLineWithSenderAndMessage(String.Empty, e); + } + } + + void eventSource_TaskStarted(object sender, TaskStartedEventArgs e) + { + // TaskStartedEventArgs adds ProjectFile, TaskFile, TaskName + // To keep this log clean, this logger will ignore these events. + } + + void eventSource_ProjectStarted(object sender, ProjectStartedEventArgs e) + { + WriteLine(e.Message); + indent++; + } + + void eventSource_ProjectFinished(object sender, ProjectFinishedEventArgs e) + { + indent--; + WriteLine(e.Message); + } + + /// <summary> + /// Write a line to the log, adding the SenderName + /// </summary> + private void WriteLineWithSender(string line, BuildEventArgs e) + { + if (0 == String.Compare(e.SenderName, "MSBuild", true /*ignore case*/)) + { + // Well, if the sender name is MSBuild, let's leave it out for prettiness + WriteLine(line); + } + else + { + WriteLine(e.SenderName + ": " + line); + } + } + + /// <summary> + /// Write a line to the log, adding the SenderName and Message + /// (these parameters are on all MSBuild event argument objects) + /// </summary> + private void WriteLineWithSenderAndMessage(string line, BuildEventArgs e) + { + if (0 == String.Compare(e.SenderName, "MSBuild", true /*ignore case*/)) + { + // Well, if the sender name is MSBuild, let's leave it out for prettiness + WriteLine(line + e.Message); + } + else + { + WriteLine(e.SenderName + ": " + line + e.Message); + } + } + + private void WriteLine(string line) + { + for (int i = indent; i > 0; i--) + { + logStreamWriter.Write("\t"); + } + logStreamWriter.WriteLine(line); + } + + public void Shutdown() + { + logStreamWriter.Close(); + issuesStreamWriter.Close(); + } + + public bool IsVerbosityAtLeast(LoggerVerbosity checkVerbosity) + { + return this.Verbosity >= checkVerbosity; + } + + private StreamWriter logStreamWriter; + private StreamWriter issuesStreamWriter; + private int indent; + } +} diff --git a/modules/mono/editor/GodotSharpTools/Editor/MonoDevelopInstance.cs b/modules/mono/editor/GodotSharpTools/Editor/MonoDevelopInstance.cs new file mode 100644 index 0000000000..303be3b732 --- /dev/null +++ b/modules/mono/editor/GodotSharpTools/Editor/MonoDevelopInstance.cs @@ -0,0 +1,58 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Diagnostics; + +namespace GodotSharpTools.Editor +{ + public class MonoDevelopInstance + { + private Process process; + private string solutionFile; + + public void Execute(string[] files) + { + bool newWindow = process == null || process.HasExited; + + List<string> args = new List<string>(); + + args.Add("--ipc-tcp"); + + if (newWindow) + args.Add("\"" + Path.GetFullPath(solutionFile) + "\""); + + foreach (var file in files) + { + int semicolonIndex = file.IndexOf(';'); + + string filePath = semicolonIndex < 0 ? file : file.Substring(0, semicolonIndex); + string cursor = semicolonIndex < 0 ? string.Empty : file.Substring(semicolonIndex); + + args.Add("\"" + Path.GetFullPath(filePath.NormalizePath()) + cursor + "\""); + } + + if (newWindow) + { + ProcessStartInfo startInfo = new ProcessStartInfo(MonoDevelopFile, string.Join(" ", args)); + process = Process.Start(startInfo); + } + else + { + Process.Start(MonoDevelopFile, string.Join(" ", args)); + } + } + + public MonoDevelopInstance(string solutionFile) + { + this.solutionFile = solutionFile; + } + + private static string MonoDevelopFile + { + get + { + return "monodevelop"; + } + } + } +} diff --git a/modules/mono/editor/GodotSharpTools/GodotSharpTools.csproj b/modules/mono/editor/GodotSharpTools/GodotSharpTools.csproj new file mode 100644 index 0000000000..981083a3c2 --- /dev/null +++ b/modules/mono/editor/GodotSharpTools/GodotSharpTools.csproj @@ -0,0 +1,45 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{A8CDAD94-C6D4-4B19-A7E7-76C53CC92984}</ProjectGuid> + <OutputType>Library</OutputType> + <RootNamespace>GodotSharpTools</RootNamespace> + <AssemblyName>GodotSharpTools</AssemblyName> + <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug</OutputPath> + <DefineConstants>DEBUG;</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <ConsolePause>false</ConsolePause> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>full</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release</OutputPath> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <ConsolePause>false</ConsolePause> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + <Reference Include="Microsoft.Build" /> + <Reference Include="Microsoft.Build.Framework" /> + </ItemGroup> + <ItemGroup> + <Compile Include="StringExtensions.cs" /> + <Compile Include="Build\BuildSystem.cs" /> + <Compile Include="Editor\MonoDevelopInstance.cs" /> + <Compile Include="Project\ProjectExtensions.cs" /> + <Compile Include="Project\ProjectGenerator.cs" /> + <Compile Include="Project\ProjectUtils.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> +</Project>
\ No newline at end of file diff --git a/modules/mono/editor/GodotSharpTools/GodotSharpTools.sln b/modules/mono/editor/GodotSharpTools/GodotSharpTools.sln new file mode 100644 index 0000000000..7eabcdff5d --- /dev/null +++ b/modules/mono/editor/GodotSharpTools/GodotSharpTools.sln @@ -0,0 +1,17 @@ +
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2012
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GodotSharpTools", "GodotSharpTools.csproj", "{A8CDAD94-C6D4-4B19-A7E7-76C53CC92984}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {A8CDAD94-C6D4-4B19-A7E7-76C53CC92984}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {A8CDAD94-C6D4-4B19-A7E7-76C53CC92984}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {A8CDAD94-C6D4-4B19-A7E7-76C53CC92984}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {A8CDAD94-C6D4-4B19-A7E7-76C53CC92984}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+EndGlobal
diff --git a/modules/mono/editor/GodotSharpTools/GodotSharpTools.userprefs b/modules/mono/editor/GodotSharpTools/GodotSharpTools.userprefs new file mode 100644 index 0000000000..0cbafdc20d --- /dev/null +++ b/modules/mono/editor/GodotSharpTools/GodotSharpTools.userprefs @@ -0,0 +1,14 @@ +<Properties StartupItem="GodotSharpTools.csproj"> + <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> + <MonoDevelop.Ide.Workbench ActiveDocument="Build/BuildSystem.cs"> + <Files> + <File FileName="Build/ProjectExtensions.cs" Line="1" Column="1" /> + <File FileName="Build/ProjectGenerator.cs" Line="1" Column="1" /> + <File FileName="Build/BuildSystem.cs" Line="37" Column="14" /> + </Files> + </MonoDevelop.Ide.Workbench> + <MonoDevelop.Ide.DebuggingService.Breakpoints> + <BreakpointStore /> + </MonoDevelop.Ide.DebuggingService.Breakpoints> + <MonoDevelop.Ide.DebuggingService.PinnedWatches /> +</Properties>
\ No newline at end of file diff --git a/modules/mono/editor/GodotSharpTools/Project/ProjectExtensions.cs b/modules/mono/editor/GodotSharpTools/Project/ProjectExtensions.cs new file mode 100644 index 0000000000..6a97731539 --- /dev/null +++ b/modules/mono/editor/GodotSharpTools/Project/ProjectExtensions.cs @@ -0,0 +1,49 @@ +using System; +using Microsoft.Build.Construction; + +namespace GodotSharpTools.Project +{ + public static class ProjectExtensions + { + public static bool HasItem(this ProjectRootElement root, string itemType, string include) + { + string includeNormalized = include.NormalizePath(); + + foreach (var itemGroup in root.ItemGroups) + { + if (itemGroup.Condition.Length != 0) + continue; + + foreach (var item in itemGroup.Items) + { + if (item.ItemType == itemType) + { + if (item.Include.NormalizePath() == includeNormalized) + return true; + } + } + } + + return false; + } + + public static void AddItemChecked(this ProjectRootElement root, string itemType, string include) + { + if (!root.HasItem(itemType, include)) + { + root.AddItem(itemType, include); + } + } + + public static Guid GetGuid(this ProjectRootElement root) + { + foreach (var property in root.Properties) + { + if (property.Name == "ProjectGuid") + return Guid.Parse(property.Value); + } + + return Guid.Empty; + } + } +} diff --git a/modules/mono/editor/GodotSharpTools/Project/ProjectGenerator.cs b/modules/mono/editor/GodotSharpTools/Project/ProjectGenerator.cs new file mode 100644 index 0000000000..6bf54a0156 --- /dev/null +++ b/modules/mono/editor/GodotSharpTools/Project/ProjectGenerator.cs @@ -0,0 +1,216 @@ +using System; +using System.IO; +using Microsoft.Build.Construction; + +namespace GodotSharpTools.Project +{ + public static class ProjectGenerator + { + public static string GenCoreApiProject(string dir, string[] compileItems) + { + string path = Path.Combine(dir, CoreApiProject + ".csproj"); + + ProjectPropertyGroupElement mainGroup; + var root = CreateLibraryProject(CoreApiProject, out mainGroup); + + mainGroup.AddProperty("DocumentationFile", Path.Combine("$(OutputPath)", "$(AssemblyName).xml")); + mainGroup.SetProperty("RootNamespace", "Godot"); + + GenAssemblyInfoFile(root, dir, CoreApiProject, + new string[] { "[assembly: InternalsVisibleTo(\"" + EditorApiProject + "\")]" }, + new string[] { "System.Runtime.CompilerServices" }); + + foreach (var item in compileItems) + { + root.AddItem("Compile", item.RelativeToPath(dir).Replace("/", "\\")); + } + + root.Save(path); + + return root.GetGuid().ToString().ToUpper(); + } + + public static string GenEditorApiProject(string dir, string coreApiHintPath, string[] compileItems) + { + string path = Path.Combine(dir, EditorApiProject + ".csproj"); + + ProjectPropertyGroupElement mainGroup; + var root = CreateLibraryProject(EditorApiProject, out mainGroup); + + mainGroup.AddProperty("DocumentationFile", Path.Combine("$(OutputPath)", "$(AssemblyName).xml")); + mainGroup.SetProperty("RootNamespace", "Godot"); + + GenAssemblyInfoFile(root, dir, EditorApiProject); + + foreach (var item in compileItems) + { + root.AddItem("Compile", item.RelativeToPath(dir).Replace("/", "\\")); + } + + var coreApiRef = root.AddItem("Reference", CoreApiProject); + coreApiRef.AddMetadata("HintPath", coreApiHintPath); + coreApiRef.AddMetadata("Private", "False"); + + root.Save(path); + + return root.GetGuid().ToString().ToUpper(); + } + + public static string GenGameProject(string dir, string name, string[] compileItems) + { + string path = Path.Combine(dir, name + ".csproj"); + + ProjectPropertyGroupElement mainGroup; + var root = CreateLibraryProject(name, out mainGroup); + + mainGroup.SetProperty("OutputPath", Path.Combine(".mono", "temp", "bin", "$(Configuration)")); + mainGroup.SetProperty("BaseIntermediateOutputPath", Path.Combine(".mono", "temp", "obj")); + mainGroup.SetProperty("IntermediateOutputPath", Path.Combine("$(BaseIntermediateOutputPath)", "$(Configuration)")); + + var toolsGroup = root.AddPropertyGroup(); + toolsGroup.Condition = " '$(Configuration)|$(Platform)' == 'Tools|AnyCPU' "; + toolsGroup.AddProperty("DebugSymbols", "true"); + toolsGroup.AddProperty("DebugType", "full"); + toolsGroup.AddProperty("Optimize", "false"); + toolsGroup.AddProperty("DefineConstants", "DEBUG;TOOLS;"); + toolsGroup.AddProperty("ErrorReport", "prompt"); + toolsGroup.AddProperty("WarningLevel", "4"); + toolsGroup.AddProperty("ConsolePause", "false"); + + var coreApiRef = root.AddItem("Reference", CoreApiProject); + coreApiRef.AddMetadata("HintPath", Path.Combine("$(ProjectDir)", ".mono", "assemblies", CoreApiProject + ".dll")); + coreApiRef.AddMetadata("Private", "False"); + + var editorApiRef = root.AddItem("Reference", EditorApiProject); + editorApiRef.Condition = " '$(Configuration)' == 'Tools' "; + editorApiRef.AddMetadata("HintPath", Path.Combine("$(ProjectDir)", ".mono", "assemblies", EditorApiProject + ".dll")); + editorApiRef.AddMetadata("Private", "False"); + + GenAssemblyInfoFile(root, dir, name); + + foreach (var item in compileItems) + { + root.AddItem("Compile", item.RelativeToPath(dir).Replace("/", "\\")); + } + + root.Save(path); + + return root.GetGuid().ToString().ToUpper(); + } + + public static void GenAssemblyInfoFile(ProjectRootElement root, string dir, string name, string[] assemblyLines = null, string[] usingDirectives = null) + { + + string propertiesDir = Path.Combine(dir, "Properties"); + if (!Directory.Exists(propertiesDir)) + Directory.CreateDirectory(propertiesDir); + + string usingDirectivesText = string.Empty; + + if (usingDirectives != null) + { + foreach (var usingDirective in usingDirectives) + usingDirectivesText += "\nusing " + usingDirective + ";"; + } + + string assemblyLinesText = string.Empty; + + if (assemblyLines != null) + { + foreach (var assemblyLine in assemblyLines) + assemblyLinesText += string.Join("\n", assemblyLines) + "\n"; + } + + string content = string.Format(assemblyInfoTemplate, usingDirectivesText, name, assemblyLinesText); + + string assemblyInfoFile = Path.Combine(propertiesDir, "AssemblyInfo.cs"); + + File.WriteAllText(assemblyInfoFile, content); + + root.AddItem("Compile", assemblyInfoFile.RelativeToPath(dir).Replace("/", "\\")); + } + + public static ProjectRootElement CreateLibraryProject(string name, out ProjectPropertyGroupElement mainGroup) + { + var root = ProjectRootElement.Create(); + root.DefaultTargets = "Build"; + + mainGroup = root.AddPropertyGroup(); + mainGroup.AddProperty("Configuration", "Debug").Condition = " '$(Configuration)' == '' "; + mainGroup.AddProperty("Platform", "AnyCPU").Condition = " '$(Platform)' == '' "; + mainGroup.AddProperty("ProjectGuid", "{" + Guid.NewGuid().ToString().ToUpper() + "}"); + mainGroup.AddProperty("OutputType", "Library"); + mainGroup.AddProperty("OutputPath", Path.Combine("bin", "$(Configuration)")); + mainGroup.AddProperty("RootNamespace", name); + mainGroup.AddProperty("AssemblyName", name); + mainGroup.AddProperty("TargetFrameworkVersion", "v4.5"); + + var debugGroup = root.AddPropertyGroup(); + debugGroup.Condition = " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "; + debugGroup.AddProperty("DebugSymbols", "true"); + debugGroup.AddProperty("DebugType", "full"); + debugGroup.AddProperty("Optimize", "false"); + debugGroup.AddProperty("DefineConstants", "DEBUG;"); + debugGroup.AddProperty("ErrorReport", "prompt"); + debugGroup.AddProperty("WarningLevel", "4"); + debugGroup.AddProperty("ConsolePause", "false"); + + var releaseGroup = root.AddPropertyGroup(); + releaseGroup.Condition = " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "; + releaseGroup.AddProperty("DebugType", "full"); + releaseGroup.AddProperty("Optimize", "true"); + releaseGroup.AddProperty("ErrorReport", "prompt"); + releaseGroup.AddProperty("WarningLevel", "4"); + releaseGroup.AddProperty("ConsolePause", "false"); + + // References + var referenceGroup = root.AddItemGroup(); + referenceGroup.AddItem("Reference", "System"); + + root.AddImport(Path.Combine("$(MSBuildBinPath)", "Microsoft.CSharp.targets").Replace("/", "\\")); + + return root; + } + + private static void AddItems(ProjectRootElement elem, string groupName, params string[] items) + { + var group = elem.AddItemGroup(); + + foreach (var item in items) + { + group.AddItem(groupName, item); + } + } + + public const string CoreApiProject = "GodotSharp"; + public const string EditorApiProject = "GodotSharpEditor"; + + private const string assemblyInfoTemplate = +@"using System.Reflection;{0} + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle(""{1}"")] +[assembly: AssemblyDescription("""")] +[assembly: AssemblyConfiguration("""")] +[assembly: AssemblyCompany("""")] +[assembly: AssemblyProduct("""")] +[assembly: AssemblyCopyright("""")] +[assembly: AssemblyTrademark("""")] +[assembly: AssemblyCulture("""")] + +// The assembly version has the format ""{{Major}}.{{Minor}}.{{Build}}.{{Revision}}"". +// The form ""{{Major}}.{{Minor}}.*"" will automatically update the build and revision, +// and ""{{Major}}.{{Minor}}.{{Build}}.*"" will update just the revision. + +[assembly: AssemblyVersion(""1.0.*"")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("""")] +{2}"; + } +} diff --git a/modules/mono/editor/GodotSharpTools/Project/ProjectUtils.cs b/modules/mono/editor/GodotSharpTools/Project/ProjectUtils.cs new file mode 100644 index 0000000000..a50b4fb064 --- /dev/null +++ b/modules/mono/editor/GodotSharpTools/Project/ProjectUtils.cs @@ -0,0 +1,17 @@ +using System; +using System.IO; +using Microsoft.Build.Construction; + +namespace GodotSharpTools.Project +{ + public static class ProjectUtils + { + public static void AddItemToProjectChecked(string projectPath, string itemType, string include) + { + var dir = Directory.GetParent(projectPath).FullName; + var root = ProjectRootElement.Open(projectPath); + root.AddItemChecked(itemType, include.RelativeToPath(dir).Replace("/", "\\")); + root.Save(); + } + } +} diff --git a/modules/mono/editor/GodotSharpTools/Properties/AssemblyInfo.cs b/modules/mono/editor/GodotSharpTools/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..7115d8fc71 --- /dev/null +++ b/modules/mono/editor/GodotSharpTools/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("GodotSharpTools")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("ignacio")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/modules/mono/editor/GodotSharpTools/StringExtensions.cs b/modules/mono/editor/GodotSharpTools/StringExtensions.cs new file mode 100644 index 0000000000..b66c86f8ce --- /dev/null +++ b/modules/mono/editor/GodotSharpTools/StringExtensions.cs @@ -0,0 +1,52 @@ +using System; +using System.IO; + +namespace GodotSharpTools +{ + public static class StringExtensions + { + public static string RelativeToPath(this string path, string dir) + { + // Make sure the directory ends with a path separator + dir = Path.Combine(dir, " ").TrimEnd(); + + if (Path.DirectorySeparatorChar == '\\') + dir = dir.Replace("/", "\\") + "\\"; + + Uri fullPath = new Uri(Path.GetFullPath(path), UriKind.Absolute); + Uri relRoot = new Uri(Path.GetFullPath(dir), UriKind.Absolute); + + return relRoot.MakeRelativeUri(fullPath).ToString(); + } + + public static string NormalizePath(this string path) + { + bool rooted = path.IsAbsolutePath(); + + path = path.Replace('\\', '/'); + + string[] parts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); + + path = string.Join(Path.DirectorySeparatorChar.ToString(), parts).Trim(); + + return rooted ? Path.DirectorySeparatorChar.ToString() + path : path; + } + + private static readonly string driveRoot = Path.GetPathRoot(Environment.CurrentDirectory); + + public static bool IsAbsolutePath(this string path) + { + return path.StartsWith("/") || path.StartsWith("\\") || path.StartsWith(driveRoot); + } + + public static string CsvEscape(this string value, char delimiter = ',') + { + bool hasSpecialChar = value.IndexOfAny(new char[] { '\"', '\n', '\r', delimiter }) != -1; + + if (hasSpecialChar) + return "\"" + value.Replace("\"", "\"\"") + "\""; + + return value; + } + } +} diff --git a/modules/mono/editor/bindings_generator.cpp b/modules/mono/editor/bindings_generator.cpp new file mode 100644 index 0000000000..123f00ea10 --- /dev/null +++ b/modules/mono/editor/bindings_generator.cpp @@ -0,0 +1,2151 @@ +/*************************************************************************/ +/* bindings_generator.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#include "bindings_generator.h" + +#ifdef DEBUG_METHODS_ENABLED + +#include "global_constants.h" +#include "io/compression.h" +#include "os/dir_access.h" +#include "os/file_access.h" +#include "os/os.h" +#include "project_settings.h" +#include "ucaps.h" + +#include "../glue/cs_compressed.gen.h" +#include "../godotsharp_defs.h" +#include "../mono_gd/gd_mono_marshal.h" +#include "../utils/path_utils.h" +#include "../utils/string_utils.h" +#include "csharp_project.h" +#include "net_solution.h" + +#define CS_INDENT " " + +#define INDENT1 CS_INDENT +#define INDENT2 INDENT1 INDENT1 +#define INDENT3 INDENT2 INDENT1 +#define INDENT4 INDENT3 INDENT1 +#define INDENT5 INDENT4 INDENT1 + +#define MEMBER_BEGIN "\n" INDENT2 + +#define OPEN_BLOCK "{\n" +#define CLOSE_BLOCK "}\n" + +#define OPEN_BLOCK_L2 INDENT2 OPEN_BLOCK INDENT3 +#define OPEN_BLOCK_L3 INDENT3 OPEN_BLOCK INDENT4 +#define OPEN_BLOCK_L4 INDENT4 OPEN_BLOCK INDENT5 +#define CLOSE_BLOCK_L2 INDENT2 CLOSE_BLOCK +#define CLOSE_BLOCK_L3 INDENT3 CLOSE_BLOCK +#define CLOSE_BLOCK_L4 INDENT4 CLOSE_BLOCK + +#define LOCAL_RET "ret" + +#define CS_CLASS_NATIVECALLS "NativeCalls" +#define CS_CLASS_NATIVECALLS_EDITOR "EditorNativeCalls" +#define CS_FIELD_MEMORYOWN "memoryOwn" +#define CS_PARAM_METHODBIND "method" +#define CS_PARAM_INSTANCE "ptr" +#define CS_SMETHOD_GETINSTANCE "GetPtr" +#define CS_FIELD_SINGLETON "instance" +#define CS_PROP_SINGLETON "Instance" +#define CS_CLASS_SIGNALAWAITER "SignalAwaiter" +#define CS_METHOD_CALL "Call" + +#define GLUE_HEADER_FILE "glue_header.h" +#define ICALL_PREFIX "godot_icall_" +#define SINGLETON_ICALL_SUFFIX "_get_singleton" +#define ICALL_GET_METHODBIND ICALL_PREFIX "ClassDB_get_method" +#define ICALL_CONNECT_SIGNAL_AWAITER ICALL_PREFIX "Object_connect_signal_awaiter" +#define ICALL_OBJECT_DTOR ICALL_PREFIX "Object_Dtor" +#define C_LOCAL_PTRCALL_ARGS "call_args" +#define C_MACRO_OBJECT_CONSTRUCT "GODOTSHARP_INSTANCE_OBJECT" + +#define C_NS_MONOUTILS "GDMonoUtils" +#define C_NS_MONOINTERNALS "GDMonoInternals" +#define C_METHOD_TIE_MANAGED_TO_UNMANAGED C_NS_MONOINTERNALS "::tie_managed_to_unmanaged" +#define C_METHOD_UNMANAGED_GET_MANAGED C_NS_MONOUTILS "::unmanaged_get_managed" + +#define C_NS_MONOMARSHAL "GDMonoMarshal" +#define C_METHOD_MANAGED_TO_VARIANT C_NS_MONOMARSHAL "::mono_object_to_variant" +#define C_METHOD_MANAGED_FROM_VARIANT C_NS_MONOMARSHAL "::variant_to_mono_object" +#define C_METHOD_MONOSTR_TO_GODOT C_NS_MONOMARSHAL "::mono_string_to_godot" +#define C_METHOD_MONOSTR_FROM_GODOT C_NS_MONOMARSHAL "::mono_string_from_godot" +#define C_METHOD_MONOARRAY_TO(m_type) C_NS_MONOMARSHAL "::mono_array_to_" #m_type +#define C_METHOD_MONOARRAY_FROM(m_type) C_NS_MONOMARSHAL "::" #m_type "_to_mono_array" +#define C_METHOD_MANAGED_TO_DICT C_NS_MONOMARSHAL "::mono_object_to_Dictionary" +#define C_METHOD_MANAGED_FROM_DICT C_NS_MONOMARSHAL "::Dictionary_to_mono_object" + +const char *BindingsGenerator::TypeInterface::DEFAULT_VARARG_C_IN = "\t%0 %1_in = %1;\n"; + +bool BindingsGenerator::verbose_output = false; + +static bool is_csharp_keyword(const String &p_name) { + + // Reserved keywords + + return p_name == "abstract" || p_name == "as" || p_name == "base" || p_name == "bool" || + p_name == "break" || p_name == "byte" || p_name == "case" || p_name == "catch" || + p_name == "char" || p_name == "checked" || p_name == "class" || p_name == "const" || + p_name == "continue" || p_name == "decimal" || p_name == "default" || p_name == "delegate" || + p_name == "do" || p_name == "double" || p_name == "else" || p_name == "enum" || + p_name == "event" || p_name == "explicit" || p_name == "extern" || p_name == "false" || + p_name == "finally" || p_name == "fixed" || p_name == "float" || p_name == "for" || + p_name == "forech" || p_name == "goto" || p_name == "if" || p_name == "implicit" || + p_name == "in" || p_name == "int" || p_name == "interface" || p_name == "internal" || + p_name == "is" || p_name == "lock" || p_name == "long" || p_name == "namespace" || + p_name == "new" || p_name == "null" || p_name == "object" || p_name == "operator" || + p_name == "out" || p_name == "override" || p_name == "params" || p_name == "private" || + p_name == "protected" || p_name == "public" || p_name == "readonly" || p_name == "ref" || + p_name == "return" || p_name == "sbyte" || p_name == "sealed" || p_name == "short" || + p_name == "sizeof" || p_name == "stackalloc" || p_name == "static" || p_name == "string" || + p_name == "struct" || p_name == "switch" || p_name == "this" || p_name == "throw" || + p_name == "true" || p_name == "try" || p_name == "typeof" || p_name == "uint" || p_name == "ulong" || + p_name == "unchecked" || p_name == "unsafe" || p_name == "ushort" || p_name == "using" || + p_name == "virtual" || p_name == "volatile" || p_name == "void" || p_name == "while"; +} + +static bool is_singleton_black_listed(const String &p_type) { + + return p_type == "IP_Unix" || p_type == "InputDefault" || p_type == "AudioServerSW" || p_type == "PhysicsServerSW" || + p_type == "Physics2DServerSW" || p_type == "SpatialSoundServerSW" || p_type == "SpatialSound2DServerSW"; +} + +inline static String escape_csharp_keyword(const String &p_name) { + + return is_csharp_keyword(p_name) ? "@" + p_name : p_name; +} + +static String snake_to_pascal_case(const String &p_identifier) { + + String ret; + Vector<String> parts = p_identifier.split("_", true); + + for (int i = 0; i < parts.size(); i++) { + String part = parts[i]; + + if (part.length()) { + part[0] = _find_upper(part[0]); + ret += part; + } else { + if (i == 0 || i == (parts.size() - 1)) { + // Preserve underscores at the beginning and end + ret += "_"; + } else { + // Preserve contiguous underscores + if (parts[i - 1].length()) { + ret += "__"; + } else { + ret += "_"; + } + } + } + } + + return ret; +} + +static String snake_to_camel_case(const String &p_identifier) { + + String ret; + Vector<String> parts = p_identifier.split("_", true); + + for (int i = 0; i < parts.size(); i++) { + String part = parts[i]; + + if (part.length()) { + if (i != 0) + part[0] = _find_upper(part[0]); + ret += part; + } else { + if (i == 0 || i == (parts.size() - 1)) { + // Preserve underscores at the beginning and end + ret += "_"; + } else { + // Preserve contiguous underscores + if (parts[i - 1].length()) { + ret += "__"; + } else { + ret += "_"; + } + } + } + } + + return ret; +} + +void BindingsGenerator::_generate_header_icalls() { + + core_custom_icalls.clear(); + + core_custom_icalls.push_back(InternalCall(ICALL_GET_METHODBIND, "IntPtr", "string type, string method")); + core_custom_icalls.push_back(InternalCall(ICALL_OBJECT_DTOR, "void", "IntPtr ptr")); + + core_custom_icalls.push_back(InternalCall(ICALL_CONNECT_SIGNAL_AWAITER, "Error", + "IntPtr source, string signal, IntPtr target, " CS_CLASS_SIGNALAWAITER " awaiter")); + + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "NodePath_Ctor", "IntPtr", "string path")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "NodePath_Dtor", "void", "IntPtr ptr")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "NodePath_operator_String", "string", "IntPtr ptr")); + + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "RID_Ctor", "IntPtr", "IntPtr from")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "RID_Dtor", "void", "IntPtr ptr")); + + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "String_md5_buffer", "byte[]", "string str")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "String_md5_text", "string", "string str")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "String_rfind", "int", "string str, string what, int from")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "String_rfindn", "int", "string str, string what, int from")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "String_sha256_buffer", "byte[]", "string str")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "String_sha256_text", "string", "string str")); + + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_bytes2var", "object", "byte[] bytes")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_convert", "object", "object what, int type")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_hash", "int", "object var")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_instance_from_id", "Object", "int instance_id")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_print", "void", "object[] what")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_printerr", "void", "object[] what")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_printraw", "void", "object[] what")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_prints", "void", "object[] what")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_printt", "void", "object[] what")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_seed", "void", "int seed")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_str", "string", "object[] what")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_str2var", "object", "string str")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_type_exists", "bool", "string type")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_var2bytes", "byte[]", "object what")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_var2str", "string", "object var")); + core_custom_icalls.push_back(InternalCall(ICALL_PREFIX "Godot_weakref", "WeakRef", "IntPtr obj")); +} + +void BindingsGenerator::_generate_method_icalls(const TypeInterface &p_itype) { + + if (p_itype.base_name.length() && obj_types[p_itype.base_name].is_singleton && is_singleton_black_listed(p_itype.name)) + return; + + for (const List<MethodInterface>::Element *E = p_itype.methods.front(); E; E = E->next()) { + const MethodInterface &imethod = E->get(); + + if (imethod.is_virtual) + continue; + + const TypeInterface *return_type = _get_type_by_name_or_placeholder(imethod.return_type); + + String im_sig = "IntPtr " CS_PARAM_METHODBIND ", IntPtr " CS_PARAM_INSTANCE; + String im_unique_sig = imethod.return_type + ",IntPtr,IntPtr"; + + // Get arguments information + int i = 0; + for (const List<ArgumentInterface>::Element *F = imethod.arguments.front(); F; F = F->next()) { + const TypeInterface *arg_type = _get_type_by_name_or_placeholder(F->get().type); + + im_sig += ", "; + im_sig += arg_type->im_type_in; + im_sig += " arg"; + im_sig += itos(i + 1); + + im_unique_sig += ","; + im_unique_sig += get_unique_sig(*arg_type); + + i++; + } + + // godot_icall_{argc}_{icallcount} + String icall_method = ICALL_PREFIX + itos(imethod.arguments.size()) + "_" + itos(method_icalls.size()); + + InternalCall im_icall = InternalCall(p_itype.api_type, icall_method, return_type->im_type_out, im_sig, im_unique_sig); + + List<InternalCall>::Element *match = method_icalls.find(im_icall); + + if (match) { + if (p_itype.api_type != ClassDB::API_EDITOR) + match->get().editor_only = false; + method_icalls_map.insert(&E->get(), &match->get()); + } else { + List<InternalCall>::Element *added = method_icalls.push_back(im_icall); + method_icalls_map.insert(&E->get(), &added->get()); + } + } +} + +Error BindingsGenerator::generate_cs_core_project(const String &p_output_dir, bool p_verbose_output) { + + verbose_output = p_verbose_output; + + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + ERR_FAIL_COND_V(!da, ERR_CANT_CREATE); + + if (!DirAccess::exists(p_output_dir)) { + Error err = da->make_dir_recursive(p_output_dir); + ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE); + } + + da->change_dir(p_output_dir); + da->make_dir("Core"); + da->make_dir("ObjectType"); + + String core_dir = path_join(p_output_dir, "Core"); + String obj_type_dir = path_join(p_output_dir, "ObjectType"); + + Vector<String> compile_items; + + NETSolution solution(API_ASSEMBLY_NAME); + + if (!solution.set_path(p_output_dir)) + return ERR_FILE_NOT_FOUND; + + for (Map<String, TypeInterface>::Element *E = obj_types.front(); E; E = E->next()) { + const TypeInterface &itype = E->get(); + + if (itype.api_type == ClassDB::API_EDITOR) + continue; + + String output_file = path_join(obj_type_dir, E->get().proxy_name + ".cs"); + Error err = _generate_cs_type(E->get(), output_file); + + if (err == ERR_SKIP) + continue; + + if (err != OK) + return err; + + compile_items.push_back(output_file); + } + +#define GENERATE_BUILTIN_TYPE(m_name) \ + { \ + String output_file = path_join(core_dir, #m_name ".cs"); \ + Error err = _generate_cs_type(builtin_types[#m_name], output_file); \ + if (err != OK) \ + return err; \ + compile_items.push_back(output_file); \ + } + + GENERATE_BUILTIN_TYPE(NodePath); + GENERATE_BUILTIN_TYPE(RID); + +#undef GENERATE_BUILTIN_TYPE + + // Generate source for GlobalConstants + + String constants_source; + int global_constants_count = GlobalConstants::get_global_constant_count(); + + if (global_constants_count > 0) { + Map<String, DocData::ClassDoc>::Element *match = EditorHelp::get_doc_data()->class_list.find("@Global Scope"); + + ERR_EXPLAIN("Could not find `@Global Scope` in DocData"); + ERR_FAIL_COND_V(!match, ERR_BUG); + + const DocData::ClassDoc &global_scope_doc = match->value(); + + for (int i = 0; i < global_constants_count; i++) { + const DocData::ConstantDoc &const_doc = global_scope_doc.constants[i]; + + if (i > 0) + constants_source += MEMBER_BEGIN; + + if (const_doc.description.size()) { + constants_source += "/// <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()) { + constants_source += INDENT2 "/// "; + constants_source += description_lines[i].strip_edges().xml_escape(); + constants_source += "\n"; + } + } + + constants_source += INDENT2 "/// </summary>" MEMBER_BEGIN; + } + + constants_source += "public const int "; + constants_source += GlobalConstants::get_global_constant_name(i); + constants_source += " = "; + constants_source += itos(GlobalConstants::get_global_constant_value(i)); + constants_source += ";"; + } + } + + // Generate sources from compressed files + + Map<String, CompressedFile> compressed_files; + get_compressed_files(compressed_files); + + for (Map<String, CompressedFile>::Element *E = compressed_files.front(); E; E = E->next()) { + const String &file_name = E->key(); + const CompressedFile &file_data = E->value(); + + String output_file = path_join(core_dir, file_name); + + Vector<uint8_t> data; + data.resize(file_data.uncompressed_size); + Compression::decompress(data.ptr(), file_data.uncompressed_size, file_data.data, file_data.compressed_size, Compression::MODE_DEFLATE); + + if (file_name.get_basename() == BINDINGS_GLOBAL_SCOPE_CLASS) { + // GD.cs must be formatted to include the generated global constants + String data_str = String::utf8(reinterpret_cast<const char *>(data.ptr()), data.size()); + + Dictionary format_keys; + format_keys["GodotGlobalConstants"] = constants_source; + data_str = data_str.format(format_keys, "/*{_}*/"); + + CharString data_utf8 = data_str.utf8(); + data.resize(data_utf8.length()); + copymem(data.ptr(), reinterpret_cast<const uint8_t *>(data_utf8.get_data()), data_utf8.length()); + } + + FileAccessRef file = FileAccess::open(output_file, FileAccess::WRITE); + ERR_FAIL_COND_V(!file, ERR_FILE_CANT_WRITE); + file->store_buffer(data.ptr(), data.size()); + file->close(); + + compile_items.push_back(output_file); + } + + List<String> cs_icalls_content; + + cs_icalls_content.push_back("using System;\n" + "using System.Runtime.CompilerServices;\n" + "using System.Collections.Generic;\n" + "\n"); + cs_icalls_content.push_back("namespace " BINDINGS_NAMESPACE "\n" OPEN_BLOCK); + cs_icalls_content.push_back(INDENT1 "internal static class " CS_CLASS_NATIVECALLS "\n" INDENT1 OPEN_BLOCK); + +#define ADD_INTERNAL_CALL(m_icall) \ + if (!m_icall.editor_only) { \ + cs_icalls_content.push_back(INDENT2 "[MethodImpl(MethodImplOptions.InternalCall)]\n"); \ + cs_icalls_content.push_back(INDENT2 "internal extern static "); \ + cs_icalls_content.push_back(m_icall.im_type_out + " "); \ + cs_icalls_content.push_back(m_icall.name + "("); \ + cs_icalls_content.push_back(m_icall.im_sig + ");\n"); \ + } + + for (const List<InternalCall>::Element *E = core_custom_icalls.front(); E; E = E->next()) + ADD_INTERNAL_CALL(E->get()); + for (const List<InternalCall>::Element *E = method_icalls.front(); E; E = E->next()) + ADD_INTERNAL_CALL(E->get()); + +#undef ADD_INTERNAL_CALL + + cs_icalls_content.push_back(INDENT1 CLOSE_BLOCK CLOSE_BLOCK); + + String internal_methods_file = path_join(core_dir, CS_CLASS_NATIVECALLS ".cs"); + + Error err = _save_file(internal_methods_file, cs_icalls_content); + if (err != OK) + return err; + + compile_items.push_back(internal_methods_file); + + String guid = CSharpProject::generate_core_api_project(p_output_dir, compile_items); + + solution.add_new_project(API_ASSEMBLY_NAME, guid); + + Error sln_error = solution.save(); + if (sln_error != OK) { + ERR_PRINT("Could not to save .NET solution."); + return sln_error; + } + + return OK; +} + +Error BindingsGenerator::generate_cs_editor_project(const String &p_output_dir, const String &p_core_dll_path, bool p_verbose_output) { + + verbose_output = p_verbose_output; + + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + ERR_FAIL_COND_V(!da, ERR_CANT_CREATE); + + if (!DirAccess::exists(p_output_dir)) { + Error err = da->make_dir_recursive(p_output_dir); + ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE); + } + + da->change_dir(p_output_dir); + da->make_dir("Core"); + da->make_dir("ObjectType"); + + String core_dir = path_join(p_output_dir, "Core"); + String obj_type_dir = path_join(p_output_dir, "ObjectType"); + + Vector<String> compile_items; + + NETSolution solution(EDITOR_API_ASSEMBLY_NAME); + + if (!solution.set_path(p_output_dir)) + return ERR_FILE_NOT_FOUND; + + for (Map<String, TypeInterface>::Element *E = obj_types.front(); E; E = E->next()) { + const TypeInterface &itype = E->get(); + + if (itype.api_type != ClassDB::API_EDITOR) + continue; + + String output_file = path_join(obj_type_dir, E->get().proxy_name + ".cs"); + Error err = _generate_cs_type(E->get(), output_file); + + if (err == ERR_SKIP) + continue; + + if (err != OK) + return err; + + compile_items.push_back(output_file); + } + + List<String> cs_icalls_content; + + cs_icalls_content.push_back("using System;\n" + "using System.Runtime.CompilerServices;\n" + "using System.Collections.Generic;\n" + "\n"); + cs_icalls_content.push_back("namespace " BINDINGS_NAMESPACE "\n" OPEN_BLOCK); + cs_icalls_content.push_back(INDENT1 "internal static class " CS_CLASS_NATIVECALLS_EDITOR "\n" INDENT1 OPEN_BLOCK); + +#define ADD_INTERNAL_CALL(m_icall) \ + if (m_icall.editor_only) { \ + cs_icalls_content.push_back(INDENT2 "[MethodImpl(MethodImplOptions.InternalCall)]\n"); \ + cs_icalls_content.push_back(INDENT2 "internal extern static "); \ + cs_icalls_content.push_back(m_icall.im_type_out + " "); \ + cs_icalls_content.push_back(m_icall.name + "("); \ + cs_icalls_content.push_back(m_icall.im_sig + ");\n"); \ + } + + for (const List<InternalCall>::Element *E = editor_custom_icalls.front(); E; E = E->next()) + ADD_INTERNAL_CALL(E->get()); + for (const List<InternalCall>::Element *E = method_icalls.front(); E; E = E->next()) + ADD_INTERNAL_CALL(E->get()); + +#undef ADD_INTERNAL_CALL + + cs_icalls_content.push_back(INDENT1 CLOSE_BLOCK CLOSE_BLOCK); + + String internal_methods_file = path_join(core_dir, CS_CLASS_NATIVECALLS_EDITOR ".cs"); + + Error err = _save_file(internal_methods_file, cs_icalls_content); + if (err != OK) + return err; + + compile_items.push_back(internal_methods_file); + + String guid = CSharpProject::generate_editor_api_project(p_output_dir, p_core_dll_path, compile_items); + + solution.add_new_project(EDITOR_API_ASSEMBLY_NAME, guid); + + Error sln_error = solution.save(); + if (sln_error != OK) { + ERR_PRINT("Could not to save .NET solution."); + return sln_error; + } + + return OK; +} + +// TODO: there are constants that hide inherited members. must explicitly use `new` to avoid warnings +// 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(); + + if (is_derived_type && obj_types[itype.base_name].is_singleton && is_singleton_black_listed(itype.name)) + return ERR_SKIP; + + List<InternalCall> &custom_icalls = itype.api_type == ClassDB::API_EDITOR ? editor_custom_icalls : core_custom_icalls; + + if (verbose_output) + OS::get_singleton()->print(String("Generating " + itype.proxy_name + ".cs...\n").utf8()); + + String ctor_method(ICALL_PREFIX + itype.proxy_name + "_Ctor"); + + List<String> cs_file; + + cs_file.push_back("using System;\n"); // IntPtr + + if (itype.requires_collections) + cs_file.push_back("using System.Collections.Generic;\n"); // Dictionary + + cs_file.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"); + + 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"); + } + } + + cs_file.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); + + if (itype.is_singleton || !itype.is_object_type) { + cs_file.push_back("\n"); + } else if (!is_derived_type) { + cs_file.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"); + } else { + ERR_PRINTS("Base type ' " + itype.base_name + "' does not exist"); + return ERR_INVALID_DATA; + } + + cs_file.push_back(INDENT1 "{"); + + if (class_doc) { + + // Add constants + + for (int i = 0; i < class_doc->constants.size(); i++) { + const DocData::ConstantDoc &const_doc = class_doc->constants[i]; + + if (const_doc.description.size()) { + cs_file.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"); + } + } + + cs_file.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(";"); + } + + if (class_doc->constants.size()) + cs_file.push_back("\n"); + + // Add properties + + const Vector<DocData::PropertyDoc> &properties = itype.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>"); + } + + 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"); + } + + 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"); + + 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); + + // 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); + + // 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); + + // 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 bool HasValidHandle()\n" OPEN_BLOCK_L2 + "return " BINDINGS_PTR_FIELD " == IntPtr.Zero;\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"); + + 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"); + } 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"); + + // 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"); + } + + // 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"); + + // 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); + } else { + // Hide the constructor + cs_file.push_back(MEMBER_BEGIN "internal "); + cs_file.push_back(itype.proxy_name); + cs_file.push_back("() {}\n"); + } + + // Add.. em.. trick constructor. Sort of. + cs_file.push_back(MEMBER_BEGIN "internal "); + cs_file.push_back(itype.proxy_name); + if (is_derived_type) { + cs_file.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); + } + + // Add methods + + if (!is_derived_type) { + cs_file.push_back(MEMBER_BEGIN "public bool HasValidHandle()\n" OPEN_BLOCK_L2 + "return " BINDINGS_PTR_FIELD " == IntPtr.Zero;\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); + } + + 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); + + // 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); + + // 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); + + Map<String, TypeInterface>::Element *array_itype = builtin_types.find("Array"); + + if (!array_itype) { + ERR_PRINT("BUG: Array type interface not found!"); + return ERR_BUG; + } + + cs_file.push_back(MEMBER_BEGIN "private void _AwaitedSignalCallback("); + cs_file.push_back(array_itype->get().cs_type); + cs_file.push_back(" args, SignalAwaiter awaiter)\n" OPEN_BLOCK_L2 "awaiter.SignalCallback(args);\n" CLOSE_BLOCK_L2); + + Map<String, TypeInterface>::Element *object_itype = obj_types.find("Object"); + + if (!object_itype) { + ERR_PRINT("BUG: Array 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); + } + } + + Map<String, String>::Element *extra_member = extra_members.find(itype.name); + if (extra_member) + cs_file.push_back(extra_member->get()); + + for (const List<MethodInterface>::Element *E = itype.methods.front(); E; E = E->next()) { + const MethodInterface &imethod = E->get(); + + const TypeInterface *return_type = _get_type_by_name_or_placeholder(imethod.return_type); + + String method_bind_field = "method_bind_" + itos(method_bind_count); + + String icall_params = method_bind_field + ", " + sformat(itype.cs_in, "this"); + String arguments_sig; + String cs_in_statements; + + List<String> default_args_doc; + + // 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); + + // 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 += ", "; + + if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) + arguments_sig += "Nullable<"; + + arguments_sig += arg_type->cs_type; + + if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) + arguments_sig += "> "; + else + arguments_sig += " "; + + arguments_sig += iarg.name; + + 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); + } + } + + icall_params += ", "; + + 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"; + + cs_in_statements += arg_type->cs_type; + cs_in_statements += " "; + cs_in_statements += arg_in; + cs_in_statements += " = "; + cs_in_statements += iarg.name; + + if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) + cs_in_statements += ".HasValue ? "; + else + cs_in_statements += " != null ? "; + + cs_in_statements += iarg.name; + + if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) + cs_in_statements += ".Value : "; + else + cs_in_statements += " : "; + + String def_arg = sformat(iarg.default_argument, arg_type->cs_type); + + cs_in_statements += def_arg; + cs_in_statements += ";\n" INDENT3; + + icall_params += arg_type->cs_in.empty() ? arg_in : sformat(arg_type->cs_in, arg_in); + + 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); + } + } + + // 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"); + } + + if (imethod.method_doc && imethod.method_doc->description.size()) { + cs_file.push_back(MEMBER_BEGIN "/// <summary>\n"); + + Vector<String> description_lines = imethod.method_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 (List<String>::Element *E = default_args_doc.front(); E; E = E->next()) { + cs_file.push_back(E->get().xml_escape()); + } + + cs_file.push_back(INDENT2 "/// </summary>"); + } + + if (!imethod.is_internal) { + cs_file.push_back(MEMBER_BEGIN "[GodotMethod(\""); + cs_file.push_back(imethod.name); + cs_file.push_back("\")]"); + } + + cs_file.push_back(MEMBER_BEGIN); + cs_file.push_back(imethod.is_internal ? "internal " : "public "); + + if (itype.is_singleton) { + cs_file.push_back("static "); + } else if (imethod.is_virtual) { + cs_file.push_back("virtual "); + } + + 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 (imethod.is_virtual) { + // Godot virtual method must be overridden, therefore we return a default value by default. + + 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); + } + + continue; + } + + if (imethod.requires_object_call) { + // Fallback to Godot's object.Call(string, params) + + cs_file.push_back(CS_METHOD_CALL "(\""); + cs_file.push_back(imethod.name); + cs_file.push_back("\""); + + for (const List<ArgumentInterface>::Element *F = imethod.arguments.front(); F; F = F->next()) { + cs_file.push_back(", "); + cs_file.push_back(F->get().name); + } + + cs_file.push_back(");\n" CLOSE_BLOCK_L2); + + continue; + } + + const Map<const MethodInterface *, const InternalCall *>::Element *match = method_icalls_map.find(&E->get()); + ERR_FAIL_NULL_V(match, ERR_BUG); + + const InternalCall *im_icall = match->value(); + + String im_call = im_icall->editor_only ? CS_CLASS_NATIVECALLS_EDITOR : CS_CLASS_NATIVECALLS; + im_call += "." + im_icall->name + "(" + icall_params + ");\n"; + + if (imethod.arguments.size()) + cs_file.push_back(cs_in_statements); + + 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"); + } + + cs_file.push_back(CLOSE_BLOCK_L2); + } + + method_bind_count++; + } + + if (itype.is_singleton) { + InternalCall singleton_icall = InternalCall(itype.api_type, ICALL_PREFIX + itype.name + SINGLETON_ICALL_SUFFIX, "IntPtr"); + + if (!find_icall_by_name(singleton_icall.name, custom_icalls)) + custom_icalls.push_back(singleton_icall); + } + + if (itype.is_instantiable) { + InternalCall ctor_icall = InternalCall(itype.api_type, ctor_method, "IntPtr", itype.proxy_name + " obj"); + + if (!find_icall_by_name(ctor_icall.name, custom_icalls)) + custom_icalls.push_back(ctor_icall); + } + + cs_file.push_back(INDENT1 CLOSE_BLOCK CLOSE_BLOCK); + + return _save_file(p_output_file, cs_file); +} + +Error BindingsGenerator::generate_glue(const String &p_output_dir) { + + verbose_output = true; + + 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); + + List<String> cpp_file; + + cpp_file.push_back("#include \"" GLUE_HEADER_FILE "\"\n" + "\n"); + + List<const InternalCall *> generated_icall_funcs; + + for (Map<String, TypeInterface>::Element *type_elem = obj_types.front(); type_elem; type_elem = type_elem->next()) { + const TypeInterface &itype = type_elem->get(); + + if (itype.base_name.length() && obj_types[itype.base_name].is_singleton && is_singleton_black_listed(itype.name)) + continue; + + 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()); + + String ctor_method(ICALL_PREFIX + itype.proxy_name + "_Ctor"); + + for (const List<MethodInterface>::Element *E = itype.methods.front(); E; E = E->next()) { + const MethodInterface &imethod = E->get(); + + if (imethod.is_virtual) + continue; + + bool ret_void = imethod.return_type == "void"; + + const TypeInterface *return_type = _get_type_by_name_or_placeholder(imethod.return_type); + + String argc_str = itos(imethod.arguments.size()); + + String c_func_sig = "MethodBind* " CS_PARAM_METHODBIND ", " + 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 = 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 (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); + } + + 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(&E->get()); + 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) + cpp_file.push_back("#ifdef TOOLS_ENABLED\n"); + + // Generate icall function + + 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); + + 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; + } + + 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"); + } + + 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"); + } + } + + if (imethod.is_vararg) { + cpp_file.push_back("\tVariant::CallError vcall_error;\n\t"); + + if (!ret_void) + cpp_file.push_back(LOCAL_RET " = "); + + 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"); + } + + 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)); + } + + cpp_file.push_back(CLOSE_BLOCK "\n"); + + if (im_icall->editor_only) + cpp_file.push_back("#endif // TOOLS_ENABLED\n"); + } + } + + if (itype.is_singleton) { + String singleton_icall_name = ICALL_PREFIX + itype.name + SINGLETON_ICALL_SUFFIX; + InternalCall singleton_icall = InternalCall(itype.api_type, singleton_icall_name, "IntPtr"); + + 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"); + } + + if (itype.is_instantiable) { + InternalCall ctor_icall = InternalCall(itype.api_type, ctor_method, "IntPtr", itype.proxy_name + " obj"); + + 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"); + } + } + + 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"); \ + } + + bool tools_sequence = false; + for (const List<InternalCall>::Element *E = core_custom_icalls.front(); E; E = E->next()) { + + if (tools_sequence) { + if (!E->get().editor_only) { + tools_sequence = false; + cpp_file.push_back("#endif\n"); + } + } else { + if (E->get().editor_only) { + cpp_file.push_back("#ifdef TOOLS_ENABLED\n"); + tools_sequence = true; + } + } + + ADD_INTERNAL_CALL_REGISTRATION(E->get()); + } + + if (tools_sequence) { + tools_sequence = false; + cpp_file.push_back("#endif\n"); + } + + cpp_file.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"); + + 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"); + } + } else { + if (E->get().editor_only) { + cpp_file.push_back("#ifdef TOOLS_ENABLED\n"); + tools_sequence = true; + } + } + + ADD_INTERNAL_CALL_REGISTRATION(E->get()); + } + + if (tools_sequence) { + tools_sequence = false; + cpp_file.push_back("#endif\n"); + } + +#undef ADD_INTERNAL_CALL_REGISTRATION + + cpp_file.push_back(CLOSE_BLOCK "}\n"); + + return _save_file(path_join(p_output_dir, "mono_glue.gen.cpp"), cpp_file); +} + +Error BindingsGenerator::_save_file(const String &p_path, const List<String> &p_content) { + + FileAccessRef file = FileAccess::open(p_path, FileAccess::WRITE); + + ERR_FAIL_COND_V(!file, ERR_FILE_CANT_WRITE); + + for (const List<String>::Element *E = p_content.front(); E; E = E->next()) { + file->store_string(E->get()); + } + + file->close(); + + 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); + + if (match) + return &match->get(); + + match = obj_types.find(p_name); + + if (match) + return &match->get(); + + return NULL; +} + +const BindingsGenerator::TypeInterface *BindingsGenerator::_get_type_by_name_or_placeholder(const String &p_name) { + + const TypeInterface *found = _get_type_by_name_or_null(p_name); + + if (found) + return found; + + ERR_PRINTS(String() + "Type not found. Creating placeholder: " + p_name); + + const Map<String, TypeInterface>::Element *match = placeholder_types.find(p_name); + + if (match) + return &match->get(); + + TypeInterface placeholder; + TypeInterface::create_placeholder_type(placeholder, p_name); + + return &placeholder_types.insert(placeholder.name, placeholder)->get(); +} + +void BindingsGenerator::_populate_object_type_interfaces() { + + obj_types.clear(); + + List<StringName> class_list; + ClassDB::get_class_list(&class_list); + class_list.sort_custom<StringName::AlphCompare>(); + + StringName refclass_name = String("Reference"); + + while (class_list.size()) { + StringName type_cname = class_list.front()->get(); + + ClassDB::APIType api_type = ClassDB::get_api_type(type_cname); + + if (api_type == ClassDB::API_NONE) { + class_list.pop_front(); + continue; + } + + TypeInterface itype = TypeInterface::create_object_type(type_cname, api_type); + + itype.base_name = ClassDB::get_parent_class(type_cname); + itype.is_singleton = ProjectSettings::get_singleton()->has_singleton(itype.proxy_name); + itype.is_instantiable = ClassDB::can_instance(type_cname) && !itype.is_singleton; + itype.is_reference = ClassDB::is_parent_class(type_cname, refclass_name); + itype.memory_own = itype.is_reference; + + itype.c_out = "\treturn "; + itype.c_out += C_METHOD_UNMANAGED_GET_MANAGED; + itype.c_out += itype.is_reference ? "(%1.ptr());\n" : "(%1);\n"; + + itype.cs_in = itype.is_singleton ? BINDINGS_PTR_FIELD : "Object." CS_SMETHOD_GETINSTANCE "(%0)"; + + itype.c_type = "Object*"; + itype.c_type_in = itype.c_type; + itype.c_type_out = "MonoObject*"; + itype.cs_type = itype.proxy_name; + itype.im_type_in = "IntPtr"; + itype.im_type_out = itype.proxy_name; + + List<MethodInfo> virtual_method_list; + ClassDB::get_virtual_methods(type_cname, &virtual_method_list, true); + + List<MethodInfo> method_list; + ClassDB::get_method_list(type_cname, &method_list, true); + method_list.sort(); + + for (List<MethodInfo>::Element *E = method_list.front(); E; E = E->next()) { + const MethodInfo &method_info = E->get(); + + int argc = method_info.arguments.size(); + + if (method_info.name.empty()) + continue; + + MethodInterface imethod; + imethod.name = method_info.name; + + if (method_info.flags & METHOD_FLAG_VIRTUAL) + imethod.is_virtual = true; + + PropertyInfo return_info = method_info.return_val; + + MethodBind *m = imethod.is_virtual ? NULL : ClassDB::get_method(type_cname, method_info.name); + + imethod.is_vararg = m && m->is_vararg(); + + if (!m && !imethod.is_virtual) { + if (virtual_method_list.find(method_info)) { + // A virtual method without the virtual flag. This is a special case. + + // This type of method can only be found in Object derived types. + ERR_FAIL_COND(!itype.is_object_type); + + // There is no method bind, so let's fallback to Godot's object.Call(string, params) + imethod.requires_object_call = true; + + // The method Object.free is registered as a virtual method, but without the virtual flag. + // This is because this method is not supposed to be overridden, but called. + // We assume the return type is void. + imethod.return_type = "void"; + + // Actually, more methods like this may be added in the future, + // which could actually will return something differnet. + // Let's put this to notify us if that ever happens. + if (itype.name != "Object" || imethod.name != "free") { + WARN_PRINTS("Notification: New unexpected virtual non-overridable method found.\n" + "We only expected Object.free, but found " + + itype.name + "." + imethod.name); + } + } else { + ERR_PRINTS("Missing MethodBind for non-virtual method: " + itype.name + "." + imethod.name); + } + } else if (return_info.type == Variant::INT && return_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { + //imethod.return_type = return_info.class_name; + imethod.return_type = "int"; + } else if (return_info.class_name != StringName()) { + imethod.return_type = return_info.class_name; + } else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) { + imethod.return_type = return_info.hint_string; + } else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) { + imethod.return_type = "Variant"; + } else if (return_info.type == Variant::NIL) { + imethod.return_type = "void"; + } else { + imethod.return_type = Variant::get_type_name(return_info.type); + } + + if (!itype.requires_collections && imethod.return_type == "Dictionary") + itype.requires_collections = true; + + for (int i = 0; i < argc; i++) { + PropertyInfo arginfo = method_info.arguments[i]; + + ArgumentInterface iarg; + iarg.name = arginfo.name; + + if (arginfo.type == Variant::INT && arginfo.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { + //iarg.type = arginfo.class_name; + iarg.type = "int"; + } else if (arginfo.class_name != StringName()) { + iarg.type = arginfo.class_name; + } else if (arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE) { + iarg.type = arginfo.hint_string; + } else if (arginfo.type == Variant::NIL) { + iarg.type = "Variant"; + } else { + iarg.type = Variant::get_type_name(arginfo.type); + } + + iarg.name = escape_csharp_keyword(snake_to_camel_case(iarg.name)); + + if (!itype.requires_collections && iarg.type == "Dictionary") + itype.requires_collections = true; + + if (m && m->has_default_argument(i)) { + _default_argument_from_variant(m->get_default_argument(i), iarg); + } + + imethod.add_argument(iarg); + } + + if (imethod.is_vararg) { + ArgumentInterface ivararg; + ivararg.type = "VarArg"; + ivararg.name = "@args"; + imethod.add_argument(ivararg); + } + + imethod.proxy_name = escape_csharp_keyword(snake_to_pascal_case(imethod.name)); + + // Prevent naming the property and its enclosing type from sharing the same name + if (imethod.proxy_name == itype.proxy_name) { + if (verbose_output) { + WARN_PRINTS("Name of method `" + imethod.proxy_name + "` is ambiguous with the name of its class `" + + itype.proxy_name + "`. Renaming method to `" + imethod.proxy_name + "_`"); + } + + imethod.proxy_name += "_"; + } + + if (itype.class_doc) { + for (int i = 0; i < itype.class_doc->methods.size(); i++) { + if (itype.class_doc->methods[i].name == imethod.name) { + imethod.method_doc = &itype.class_doc->methods[i]; + break; + } + } + } + + if (!imethod.is_virtual && imethod.name[0] == '_') { + const Vector<DocData::PropertyDoc> &properties = itype.class_doc->properties; + + for (int i = 0; i < properties.size(); i++) { + const DocData::PropertyDoc &prop_doc = properties[i]; + + if (prop_doc.getter == imethod.name || prop_doc.setter == imethod.name) { + imethod.is_internal = true; + itype.methods.push_back(imethod); + break; + } + } + } else { + itype.methods.push_back(imethod); + } + } + + obj_types.insert(itype.name, itype); + + class_list.pop_front(); + } +} + +void BindingsGenerator::_default_argument_from_variant(const Variant &p_val, ArgumentInterface &r_iarg) { + + r_iarg.default_argument = p_val; + + switch (p_val.get_type()) { + case Variant::NIL: + if (ClassDB::class_exists(r_iarg.type)) { + // Object type + r_iarg.default_argument = "null"; + } else { + // Variant + r_iarg.default_argument = "null"; + } + break; + // Atomic types + case Variant::BOOL: + r_iarg.default_argument = bool(p_val) ? "true" : "false"; + break; + case Variant::INT: + break; // Keep it + case Variant::REAL: +#ifndef REAL_T_IS_DOUBLE + r_iarg.default_argument += "f"; +#endif + break; + case Variant::STRING: + case Variant::NODE_PATH: + r_iarg.default_argument = "\"" + r_iarg.default_argument + "\""; + break; + case Variant::TRANSFORM: + if (p_val.operator Transform() == Transform()) + r_iarg.default_argument.clear(); + r_iarg.default_argument = "new %s(" + r_iarg.default_argument + ")"; + r_iarg.def_param_mode = ArgumentInterface::NULLABLE_VAL; + break; + case Variant::PLANE: + case Variant::RECT3: + case Variant::COLOR: + r_iarg.default_argument = "new Color(1, 1, 1, 1)"; + r_iarg.def_param_mode = ArgumentInterface::NULLABLE_VAL; + break; + case Variant::VECTOR2: + case Variant::RECT2: + case Variant::VECTOR3: + r_iarg.default_argument = "new %s" + r_iarg.default_argument; + r_iarg.def_param_mode = ArgumentInterface::NULLABLE_VAL; + break; + case Variant::OBJECT: + if (p_val.is_zero()) { + r_iarg.default_argument = "null"; + break; + } + case Variant::DICTIONARY: + case Variant::_RID: + r_iarg.default_argument = "new %s()"; + r_iarg.def_param_mode = ArgumentInterface::NULLABLE_REF; + break; + case Variant::ARRAY: + case Variant::POOL_BYTE_ARRAY: + case Variant::POOL_INT_ARRAY: + case Variant::POOL_REAL_ARRAY: + case Variant::POOL_STRING_ARRAY: + case Variant::POOL_VECTOR2_ARRAY: + case Variant::POOL_VECTOR3_ARRAY: + case Variant::POOL_COLOR_ARRAY: + r_iarg.default_argument = "new %s {}"; + r_iarg.def_param_mode = ArgumentInterface::NULLABLE_REF; + break; + case Variant::TRANSFORM2D: + case Variant::BASIS: + case Variant::QUAT: + r_iarg.default_argument = Variant::get_type_name(p_val.get_type()) + ".Identity"; + r_iarg.def_param_mode = ArgumentInterface::NULLABLE_VAL; + break; + default: {} + } + + if (r_iarg.def_param_mode == ArgumentInterface::CONSTANT && r_iarg.type == "Variant" && r_iarg.default_argument != "null") + r_iarg.def_param_mode = ArgumentInterface::NULLABLE_REF; +} + +void BindingsGenerator::_populate_builtin_type_interfaces() { + + builtin_types.clear(); + + TypeInterface itype; + +#define INSERT_STRUCT_TYPE(m_type, m_type_in) \ + { \ + itype = TypeInterface::create_value_type(#m_type); \ + itype.c_in = "\tMARSHALLED_IN(" #m_type ", %1, %1_in);\n"; \ + itype.c_out = "\tMARSHALLED_OUT(" #m_type ", %1, ret_out)\n" \ + "\treturn mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(%2), ret_out);\n"; \ + itype.c_arg_in = "&%s_in"; \ + itype.c_type_in = m_type_in; \ + itype.cs_in = "ref %s"; \ + itype.cs_out = "return (" #m_type ")%0;"; \ + itype.im_type_out = "object"; \ + builtin_types.insert(#m_type, itype); \ + } + + INSERT_STRUCT_TYPE(Vector2, "real_t*") + INSERT_STRUCT_TYPE(Rect2, "real_t*") + INSERT_STRUCT_TYPE(Transform2D, "real_t*") + INSERT_STRUCT_TYPE(Vector3, "real_t*") + INSERT_STRUCT_TYPE(Basis, "real_t*") + INSERT_STRUCT_TYPE(Quat, "real_t*") + INSERT_STRUCT_TYPE(Transform, "real_t*") + INSERT_STRUCT_TYPE(Rect3, "real_t*") + INSERT_STRUCT_TYPE(Color, "real_t*") + INSERT_STRUCT_TYPE(Plane, "real_t*") + +#undef INSERT_STRUCT_TYPE + +#define INSERT_PRIMITIVE_TYPE(m_type) \ + { \ + itype = TypeInterface::create_value_type(#m_type); \ + itype.c_arg_in = "&%s"; \ + itype.c_type_in = #m_type; \ + itype.c_type_out = #m_type; \ + itype.im_type_in = #m_type; \ + itype.im_type_out = #m_type; \ + builtin_types.insert(#m_type, itype); \ + } + + INSERT_PRIMITIVE_TYPE(bool) + //INSERT_PRIMITIVE_TYPE(int) + + // int + itype = TypeInterface::create_value_type("int"); + itype.c_arg_in = "&%s_in"; + //* ptrcall only supports int64_t and uint64_t + itype.c_in = "\t%0 %1_in = (%0)%1;\n"; + itype.c_out = "\treturn (%0)%1;\n"; + itype.c_type = "int64_t"; + //*/ + itype.c_type_in = itype.name; + itype.c_type_out = itype.name; + itype.im_type_in = itype.name; + itype.im_type_out = itype.name; + builtin_types.insert(itype.name, itype); + +#undef INSERT_PRIMITIVE_TYPE + + // real_t + itype = TypeInterface(); +#ifdef REAL_T_IS_DOUBLE + itype.name = "double"; +#else + itype.name = "float"; +#endif + itype.proxy_name = itype.name; + itype.c_arg_in = "&%s_in"; + //* ptrcall only supports double + itype.c_in = "\t%0 %1_in = (%0)%1;\n"; + itype.c_out = "\treturn (%0)%1;\n"; + itype.c_type = "double"; + //*/ + itype.c_type_in = "real_t"; + itype.c_type_out = "real_t"; + itype.cs_type = itype.proxy_name; + itype.im_type_in = itype.proxy_name; + itype.im_type_out = itype.proxy_name; + builtin_types.insert(itype.name, itype); + + // String + itype = TypeInterface(); + itype.name = "String"; + itype.proxy_name = "string"; + itype.c_in = "\t%0 %1_in = " C_METHOD_MONOSTR_TO_GODOT "(%1);\n"; + itype.c_out = "\treturn " C_METHOD_MONOSTR_FROM_GODOT "(%1);\n"; + itype.c_arg_in = "&%s_in"; + itype.c_type = itype.name; + itype.c_type_in = "MonoString*"; + itype.c_type_out = "MonoString*"; + itype.cs_type = itype.proxy_name; + itype.im_type_in = itype.proxy_name; + itype.im_type_out = itype.proxy_name; + builtin_types.insert(itype.name, itype); + + // NodePath + itype = TypeInterface(); + itype.name = "NodePath"; + itype.proxy_name = "NodePath"; + itype.c_out = "\treturn memnew(NodePath(%1));\n"; + itype.c_type = itype.name; + itype.c_type_in = itype.c_type + "*"; + itype.c_type_out = itype.c_type + "*"; + itype.cs_type = itype.proxy_name; + itype.cs_in = "NodePath." CS_SMETHOD_GETINSTANCE "(%0)"; + itype.cs_out = "return new NodePath(%0);"; + itype.im_type_in = "IntPtr"; + itype.im_type_out = "IntPtr"; + _populate_builtin_type(itype, Variant::NODE_PATH); + extra_members.insert(itype.name, MEMBER_BEGIN "public NodePath() : this(string.Empty) {}\n" MEMBER_BEGIN "public NodePath(string path)\n" OPEN_BLOCK_L2 + "this." BINDINGS_PTR_FIELD " = NativeCalls.godot_icall_NodePath_Ctor(path);\n" CLOSE_BLOCK_L2 + MEMBER_BEGIN "public static implicit operator NodePath(string from)\n" OPEN_BLOCK_L2 "return new NodePath(from);\n" CLOSE_BLOCK_L2 + MEMBER_BEGIN "public static implicit operator string(NodePath from)\n" OPEN_BLOCK_L2 + "return NativeCalls." ICALL_PREFIX "NodePath_operator_String(NodePath." CS_SMETHOD_GETINSTANCE "(from));\n" CLOSE_BLOCK_L2); + builtin_types.insert(itype.name, itype); + + // RID + itype = TypeInterface(); + itype.name = "RID"; + itype.proxy_name = "RID"; + itype.c_out = "\treturn memnew(RID(%1));\n"; + itype.c_type = itype.name; + itype.c_type_in = itype.c_type + "*"; + itype.c_type_out = itype.c_type + "*"; + itype.cs_type = itype.proxy_name; + itype.cs_in = "RID." CS_SMETHOD_GETINSTANCE "(%0)"; + itype.cs_out = "return new RID(%0);"; + itype.im_type_in = "IntPtr"; + itype.im_type_out = "IntPtr"; + _populate_builtin_type(itype, Variant::_RID); + extra_members.insert(itype.name, MEMBER_BEGIN "internal RID()\n" OPEN_BLOCK_L2 + "this." BINDINGS_PTR_FIELD " = IntPtr.Zero;\n" CLOSE_BLOCK_L2); + builtin_types.insert(itype.name, itype); + + // Variant + itype = TypeInterface(); + itype.name = "Variant"; + itype.proxy_name = "object"; + itype.c_in = "\t%0 %1_in = " C_METHOD_MANAGED_TO_VARIANT "(%1);\n"; + itype.c_out = "\treturn " C_METHOD_MANAGED_FROM_VARIANT "(%1);\n"; + itype.c_arg_in = "&%s_in"; + itype.c_type = itype.name; + itype.c_type_in = "MonoObject*"; + itype.c_type_out = "MonoObject*"; + itype.cs_type = itype.proxy_name; + itype.im_type_in = "object"; + itype.im_type_out = itype.proxy_name; + builtin_types.insert(itype.name, itype); + + // VarArg (fictitious type to represent variable arguments) + itype = TypeInterface(); + itype.name = "VarArg"; + itype.proxy_name = "object[]"; + itype.c_in = "\t%0 %1_in = " C_METHOD_MONOARRAY_TO(Array) "(%1);\n"; + itype.c_arg_in = "&%s_in"; + itype.c_type = "Array"; + itype.c_type_in = "MonoArray*"; + itype.cs_type = "params object[]"; + itype.im_type_in = "object[]"; + builtin_types.insert(itype.name, itype); + +#define INSERT_ARRAY_FULL(m_name, m_type, m_proxy_t) \ + { \ + itype = TypeInterface(); \ + itype.name = #m_name; \ + itype.proxy_name = #m_proxy_t "[]"; \ + itype.c_in = "\t%0 %1_in = " C_METHOD_MONOARRAY_TO(m_type) "(%1);\n"; \ + itype.c_out = "\treturn " C_METHOD_MONOARRAY_FROM(m_type) "(%1);\n"; \ + itype.c_arg_in = "&%s_in"; \ + itype.c_type = #m_type; \ + itype.c_type_in = "MonoArray*"; \ + itype.c_type_out = "MonoArray*"; \ + itype.cs_type = itype.proxy_name; \ + itype.im_type_in = itype.proxy_name; \ + itype.im_type_out = itype.proxy_name; \ + builtin_types.insert(itype.name, itype); \ + } + +#define INSERT_ARRAY(m_type, m_proxy_t) INSERT_ARRAY_FULL(m_type, m_type, m_proxy_t) + + INSERT_ARRAY(Array, object); + INSERT_ARRAY(PoolIntArray, int); + INSERT_ARRAY_FULL(PoolByteArray, PoolByteArray, byte); + +#ifdef REAL_T_IS_DOUBLE + INSERT_ARRAY(PoolRealArray, double); +#else + INSERT_ARRAY(PoolRealArray, float); +#endif + + INSERT_ARRAY(PoolStringArray, string); + + INSERT_ARRAY(PoolColorArray, Color); + INSERT_ARRAY(PoolVector2Array, Vector2); + INSERT_ARRAY(PoolVector3Array, Vector3); + +#undef INSERT_ARRAY + + // Dictionary + itype = TypeInterface(); + itype.name = "Dictionary"; + itype.proxy_name = "Dictionary<object, object>"; + itype.c_in = "\t%0 %1_in = " C_METHOD_MANAGED_TO_DICT "(%1);\n"; + itype.c_out = "\treturn " C_METHOD_MANAGED_FROM_DICT "(%1);\n"; + itype.c_arg_in = "&%s_in"; + itype.c_type = itype.name; + itype.c_type_in = "MonoObject*"; + itype.c_type_out = "MonoObject*"; + itype.cs_type = itype.proxy_name; + itype.im_type_in = itype.proxy_name; + itype.im_type_out = itype.proxy_name; + builtin_types.insert(itype.name, itype); + + // void (fictitious type to represent the return type of methods that do not return anything) + itype = TypeInterface(); + itype.name = "void"; + itype.proxy_name = itype.name; + itype.c_type = itype.name; + itype.c_type_in = itype.c_type; + itype.c_type_out = itype.c_type; + itype.cs_type = itype.proxy_name; + itype.im_type_in = itype.proxy_name; + itype.im_type_out = itype.proxy_name; + builtin_types.insert(itype.name, itype); + + // Error + itype = TypeInterface(); + itype.name = "Error"; + itype.proxy_name = "Error"; + itype.c_type = itype.name; + itype.c_type_in = itype.c_type; + itype.c_type_out = itype.c_type; + itype.cs_type = itype.proxy_name; + itype.cs_in = "(int)%0"; + itype.cs_out = "return (Error)%s;"; + itype.im_type_in = "int"; + itype.im_type_out = "int"; + builtin_types.insert(itype.name, itype); +} + +void BindingsGenerator::_populate_builtin_type(TypeInterface &r_itype, Variant::Type vtype) { + + Variant::CallError cerror; + Variant v = Variant::construct(vtype, NULL, 0, cerror); + + List<MethodInfo> method_list; + v.get_method_list(&method_list); + method_list.sort(); + + for (List<MethodInfo>::Element *E = method_list.front(); E; E = E->next()) { + MethodInfo &mi = E->get(); + MethodInterface imethod; + + imethod.name = mi.name; + imethod.proxy_name = mi.name; + + for (int i = 0; i < mi.arguments.size(); i++) { + ArgumentInterface iarg; + PropertyInfo pi = mi.arguments[i]; + + iarg.name = pi.name; + + if (pi.type == Variant::NIL) + iarg.type = "Variant"; + else + iarg.type = Variant::get_type_name(pi.type); + + if (!r_itype.requires_collections && iarg.type == "Dictionary") + r_itype.requires_collections = true; + + if ((mi.default_arguments.size() - mi.arguments.size() + i) >= 0) + _default_argument_from_variant(Variant::construct(pi.type, NULL, 0, cerror), iarg); + + imethod.add_argument(iarg); + } + + if (mi.return_val.type == Variant::NIL) { + if (mi.return_val.name != "") + imethod.return_type = "Variant"; + } else { + imethod.return_type = Variant::get_type_name(mi.return_val.type); + } + + if (!r_itype.requires_collections && imethod.return_type == "Dictionary") + r_itype.requires_collections = true; + + if (r_itype.class_doc) { + for (int i = 0; i < r_itype.class_doc->methods.size(); i++) { + if (r_itype.class_doc->methods[i].name == imethod.name) { + imethod.method_doc = &r_itype.class_doc->methods[i]; + break; + } + } + } + + r_itype.methods.push_back(imethod); + } +} + +BindingsGenerator::BindingsGenerator() { + + EditorHelp::generate_doc(); + + _populate_object_type_interfaces(); + _populate_builtin_type_interfaces(); + _generate_header_icalls(); + + for (Map<String, TypeInterface>::Element *E = obj_types.front(); E; E = E->next()) + _generate_method_icalls(E->get()); + + _generate_method_icalls(builtin_types["NodePath"]); + _generate_method_icalls(builtin_types["RID"]); +} + +void BindingsGenerator::handle_cmdline_args(const List<String> &p_cmdline_args) { + + int options_count = 3; + + String mono_glue_option = "--generate-mono-glue"; + String cs_core_api_option = "--generate-cs-core-api"; + String cs_editor_api_option = "--generate-cs-editor-api"; + + verbose_output = true; + + const List<String>::Element *elem = p_cmdline_args.front(); + + while (elem && options_count) { + + if (elem->get() == mono_glue_option) { + + const List<String>::Element *path_elem = elem->next(); + + if (path_elem) { + get_singleton().generate_glue(path_elem->get()); + elem = elem->next(); + } else { + ERR_PRINTS("--generate-mono-glue: No output directory specified"); + } + + --options_count; + + } else if (elem->get() == cs_core_api_option) { + + const List<String>::Element *path_elem = elem->next(); + + if (path_elem) { + get_singleton().generate_cs_core_project(path_elem->get()); + elem = elem->next(); + } else { + ERR_PRINTS(cs_core_api_option + ": No output directory specified"); + } + + --options_count; + + } else if (elem->get() == cs_editor_api_option) { + + const List<String>::Element *path_elem = elem->next(); + + if (path_elem) { + if (path_elem->next()) { + get_singleton().generate_cs_editor_project(path_elem->get(), path_elem->next()->get()); + elem = path_elem->next(); + } else { + ERR_PRINTS(cs_editor_api_option + ": No hint path for the Core API dll specified"); + } + } else { + ERR_PRINTS(cs_editor_api_option + ": No output directory specified"); + } + + --options_count; + } + + elem = elem->next(); + } + + verbose_output = false; +} + +#endif diff --git a/modules/mono/editor/bindings_generator.h b/modules/mono/editor/bindings_generator.h new file mode 100644 index 0000000000..437a566556 --- /dev/null +++ b/modules/mono/editor/bindings_generator.h @@ -0,0 +1,429 @@ +/*************************************************************************/ +/* bindings_generator.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef BINDINGS_GENERATOR_H +#define BINDINGS_GENERATOR_H + +#include "class_db.h" +#include "editor/doc/doc_data.h" +#include "editor/editor_help.h" + +#ifdef DEBUG_METHODS_ENABLED + +#include "ustring.h" + +class BindingsGenerator { + struct ArgumentInterface { + enum DefaultParamMode { + CONSTANT, + NULLABLE_VAL, + NULLABLE_REF + }; + + String type; + String name; + String default_argument; + DefaultParamMode def_param_mode; + + ArgumentInterface() { + def_param_mode = CONSTANT; + } + }; + + struct MethodInterface { + String name; + + /** + * Name of the C# method + */ + String proxy_name; + + /** + * [TypeInterface::name] of the return type + */ + String return_type; + + /** + * Determines if the method has a variable number of arguments (VarArg) + */ + bool is_vararg; + + /** + * Virtual methods ("virtual" as defined by the Godot API) are methods that by default do nothing, + * but can be overridden by the user to add custom functionality. + * e.g.: _ready, _process, etc. + */ + bool is_virtual; + + /** + * Determines if the call should fallback to Godot's object.Call(string, params) in C#. + */ + bool requires_object_call; + + /** + * Determines if the method visibility is `internal` (visible only to files in the same assembly). + * Currently, we only use this for methods that are not meant to be exposed, + * but are required by properties as getters or setters. + * Methods that are not meant to be exposed are those that begin with underscore and are not virtual. + */ + bool is_internal; + + List<ArgumentInterface> arguments; + + const DocData::MethodDoc *method_doc; + + void add_argument(const ArgumentInterface &argument) { + arguments.push_back(argument); + } + + MethodInterface() { + return_type = "void"; + is_vararg = false; + is_virtual = false; + requires_object_call = false; + is_internal = false; + method_doc = NULL; + } + }; + + struct TypeInterface { + /** + * Identifier name for this type. + * Also used to format [c_out]. + */ + String name; + + /** + * Identifier name of the base class. + */ + String base_name; + + /** + * Name of the C# class + */ + String proxy_name; + + ClassDB::APIType api_type; + + bool is_object_type; + bool is_singleton; + bool is_reference; + + /** + * Used only by Object-derived types. + * Determines if this type is not virtual (incomplete). + * e.g.: CanvasItem cannot be instantiated. + */ + bool is_instantiable; + + /** + * Used only by Object-derived types. + * Determines if the C# class owns the native handle and must free it somehow when disposed. + * e.g.: Reference types must notify when the C# instance is disposed, for proper refcounting. + */ + bool memory_own; + + /** + * Determines if the file must have a using directive for System.Collections.Generic + * e.g.: When the generated class makes use of Dictionary + */ + bool requires_collections; + + // !! The comments of the following fields make reference to other fields via square brackets, e.g.: [field_name] + // !! When renaming those fields, make sure to rename their references in the comments + + // --- C INTERFACE --- + + static const char *DEFAULT_VARARG_C_IN; + + /** + * One or more statements that manipulate the parameter before being passed as argument of a ptrcall. + * If the statement adds a local that must be passed as the argument instead of the parameter, + * the name of that local must be specified with [c_arg_in]. + * For variadic methods, this field is required and, if empty, [DEFAULT_VARARG_C_IN] is used instead. + * Formatting elements: + * %0: [c_type] of the parameter + * %1: name of the parameter + */ + String c_in; + + /** + * Determines the name of the variable that will be passed as argument to a ptrcall. + * By default the value equals the name of the parameter, + * this varies for types that require special manipulation via [c_in]. + * Formatting elements: + * %0 or %s: name of the parameter + */ + String c_arg_in; + + /** + * One or more statements that determine how a variable of this type is returned from a function. + * It must contain the return statement(s). + * Formatting elements: + * %0: [c_type_out] of the return type + * %1: name of the variable to be returned + * %2: [name] of the return type + */ + String c_out; + + /** + * The actual expected type, as seen (in most cases) in Variant copy constructors + * Used for the type of the return variable and to format [c_in]. + * The value must be the following depending of the type: + * Object-derived types: Object* + * Other types: [name] + * -- Exceptions -- + * VarArg (fictitious type to represent variable arguments): Array + * float: double (because ptrcall only supports double) + * int: int64_t (because ptrcall only supports int64_t and uint64_t) + * Reference types override this for the type of the return variable: Ref<Reference> + */ + String c_type; + + /** + * Determines the type used for parameters in function signatures. + */ + String c_type_in; + + /** + * Determines the return type used for function signatures. + * Also used to construct a default value to return in case of errors, + * and to format [c_out]. + */ + String c_type_out; + + // --- C# INTERFACE --- + + /** + * An expression that overrides the way the parameter is passed to the internal call. + * If empty, the parameter is passed as is. + * Formatting elements: + * %0 or %s: name of the parameter + */ + String cs_in; + + /** + * One or more statements that determine how a variable of this type is returned from a method. + * It must contain the return statement(s). + * Formatting elements: + * %0 or %s: name of the variable to be returned + */ + String cs_out; + + /** + * Type used for method signatures, both for parameters and the return type. + * Same as [proxy_name] except for variable arguments (VarArg). + */ + String cs_type; + + /** + * Type used for parameters of internal call methods. + */ + String im_type_in; + + /** + * Type used for the return type of internal call methods. + * If [cs_out] is not empty and the method return type is not void, + * it is also used for the type of the return variable. + */ + String im_type_out; + + const DocData::ClassDoc *class_doc; + + List<MethodInterface> methods; + + const MethodInterface *find_method_by_name(const String &p_name) const { + + for (const List<MethodInterface>::Element *E = methods.front(); E; E = E->next()) { + if (E->get().name == p_name) + return &E->get(); + } + + return NULL; + } + + static TypeInterface create_value_type(const String &p_name) { + TypeInterface itype; + + itype.name = p_name; + itype.proxy_name = p_name; + + itype.c_type = itype.name; + itype.c_type_in = "void*"; + itype.c_type_out = "MonoObject*"; + itype.cs_type = itype.proxy_name; + itype.im_type_in = "ref " + itype.proxy_name; + itype.im_type_out = itype.proxy_name; + itype.class_doc = &EditorHelp::get_doc_data()->class_list[itype.proxy_name]; + + return itype; + } + + static TypeInterface create_object_type(const String &p_name, ClassDB::APIType p_api_type) { + TypeInterface itype; + + itype.name = p_name; + itype.proxy_name = p_name.begins_with("_") ? p_name.substr(1, p_name.length()) : p_name; + itype.api_type = p_api_type; + itype.is_object_type = true; + itype.class_doc = &EditorHelp::get_doc_data()->class_list[itype.proxy_name]; + + return itype; + } + + static void create_placeholder_type(TypeInterface &r_itype, const String &p_name) { + r_itype.name = p_name; + r_itype.proxy_name = p_name; + + r_itype.c_type = r_itype.name; + r_itype.c_type_in = "MonoObject*"; + r_itype.c_type_out = "MonoObject*"; + r_itype.cs_type = r_itype.proxy_name; + r_itype.im_type_in = r_itype.proxy_name; + r_itype.im_type_out = r_itype.proxy_name; + } + + TypeInterface() { + + api_type = ClassDB::API_NONE; + + is_object_type = false; + is_singleton = false; + is_reference = false; + is_instantiable = false; + + memory_own = false; + requires_collections = false; + + c_arg_in = "%s"; + + class_doc = NULL; + } + }; + + struct InternalCall { + String name; + String im_type_out; // Return type for the C# method declaration. Also used as companion of [unique_siq] + String im_sig; // Signature for the C# method declaration + String unique_sig; // Unique signature to avoid duplicates in containers + bool editor_only; + + InternalCall() {} + + InternalCall(const String &p_name, const String &p_im_type_out, const String &p_im_sig = String(), const String &p_unique_sig = String()) { + name = p_name; + im_type_out = p_im_type_out; + im_sig = p_im_sig; + unique_sig = p_unique_sig; + editor_only = false; + } + + InternalCall(ClassDB::APIType api_type, const String &p_name, const String &p_im_type_out, const String &p_im_sig = String(), const String &p_unique_sig = String()) { + name = p_name; + im_type_out = p_im_type_out; + im_sig = p_im_sig; + unique_sig = p_unique_sig; + editor_only = api_type == ClassDB::API_EDITOR; + } + + inline bool operator==(const InternalCall &p_a) const { + return p_a.unique_sig == unique_sig; + } + }; + + static bool verbose_output; + + Map<String, TypeInterface> placeholder_types; + Map<String, TypeInterface> builtin_types; + Map<String, TypeInterface> obj_types; + + Map<String, String> extra_members; + + List<InternalCall> method_icalls; + Map<const MethodInterface *, const InternalCall *> method_icalls_map; + + List<InternalCall> core_custom_icalls; + List<InternalCall> editor_custom_icalls; + + const List<InternalCall>::Element *find_icall_by_name(const String &p_name, const List<InternalCall> &p_list) { + + const List<InternalCall>::Element *it = p_list.front(); + while (it) { + if (it->get().name == p_name) return it; + it = it->next(); + } + return NULL; + } + + inline String get_unique_sig(const TypeInterface &p_type) { + if (p_type.is_reference) + return "Ref"; + else if (p_type.is_object_type) + return "Obj"; + + return p_type.name; + } + + void _generate_header_icalls(); + void _generate_method_icalls(const TypeInterface &p_itype); + + const TypeInterface *_get_type_by_name_or_null(const String &p_name); + const TypeInterface *_get_type_by_name_or_placeholder(const String &p_name); + + void _default_argument_from_variant(const Variant &p_var, ArgumentInterface &r_iarg); + void _populate_builtin_type(TypeInterface &r_type, Variant::Type vtype); + + void _populate_object_type_interfaces(); + void _populate_builtin_type_interfaces(); + + Error _generate_cs_type(const TypeInterface &itype, const String &p_output_file); + + Error _save_file(const String &path, const List<String> &content); + + BindingsGenerator(); + + BindingsGenerator(const BindingsGenerator &); + BindingsGenerator &operator=(const BindingsGenerator &); + +public: + Error generate_cs_core_project(const String &p_output_dir, bool p_verbose_output = true); + Error generate_cs_editor_project(const String &p_output_dir, const String &p_core_dll_path, bool p_verbose_output = true); + Error generate_glue(const String &p_output_dir); + + static BindingsGenerator &get_singleton() { + static BindingsGenerator singleton; + return singleton; + } + + static void handle_cmdline_args(const List<String> &p_cmdline_args); +}; + +#endif + +#endif // BINDINGS_GENERATOR_H diff --git a/modules/mono/editor/csharp_project.cpp b/modules/mono/editor/csharp_project.cpp new file mode 100644 index 0000000000..bde5f0fd0b --- /dev/null +++ b/modules/mono/editor/csharp_project.cpp @@ -0,0 +1,120 @@ +/*************************************************************************/ +/* csharp_project.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#include "csharp_project.h" + +#include "os/os.h" +#include "project_settings.h" + +#include "../mono_gd/gd_mono_class.h" +#include "../mono_gd/gd_mono_marshal.h" + +namespace CSharpProject { + +String generate_core_api_project(const String &p_dir, const Vector<String> &p_files) { + + _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN) + + GDMonoClass *klass = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Project", "ProjectGenerator"); + + Variant dir = p_dir; + Variant compile_items = p_files; + const Variant *args[2] = { &dir, &compile_items }; + MonoObject *ex = NULL; + MonoObject *ret = klass->get_method("GenCoreApiProject", 2)->invoke(NULL, args, &ex); + + if (ex) { + mono_print_unhandled_exception(ex); + ERR_FAIL_V(String()); + } + + return ret ? GDMonoMarshal::mono_string_to_godot((MonoString *)ret) : ""; +} + +String generate_editor_api_project(const String &p_dir, const String &p_core_dll_path, const Vector<String> &p_files) { + + _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN) + + GDMonoClass *klass = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Project", "ProjectGenerator"); + + Variant dir = p_dir; + Variant core_dll_path = p_core_dll_path; + Variant compile_items = p_files; + const Variant *args[3] = { &dir, &core_dll_path, &compile_items }; + MonoObject *ex = NULL; + MonoObject *ret = klass->get_method("GenEditorApiProject", 3)->invoke(NULL, args, &ex); + + if (ex) { + mono_print_unhandled_exception(ex); + ERR_FAIL_V(String()); + } + + return ret ? GDMonoMarshal::mono_string_to_godot((MonoString *)ret) : ""; +} + +String generate_game_project(const String &p_dir, const String &p_name, const Vector<String> &p_files) { + + _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN) + + GDMonoClass *klass = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Project", "ProjectGenerator"); + + Variant dir = p_dir; + Variant name = p_name; + Variant compile_items = p_files; + const Variant *args[3] = { &dir, &name, &compile_items }; + MonoObject *ex = NULL; + MonoObject *ret = klass->get_method("GenGameProject", 3)->invoke(NULL, args, &ex); + + if (ex) { + mono_print_unhandled_exception(ex); + ERR_FAIL_V(String()); + } + + return ret ? GDMonoMarshal::mono_string_to_godot((MonoString *)ret) : ""; +} + +void add_item(const String &p_project_path, const String &p_item_type, const String &p_include) { + + _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN) + + GDMonoClass *klass = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Project", "ProjectUtils"); + + Variant project_path = p_project_path; + Variant item_type = p_item_type; + Variant include = p_include; + const Variant *args[3] = { &project_path, &item_type, &include }; + MonoObject *ex = NULL; + klass->get_method("AddItemToProjectChecked", 3)->invoke(NULL, args, &ex); + + if (ex) { + mono_print_unhandled_exception(ex); + ERR_FAIL(); + } +} +} // CSharpProject diff --git a/modules/mono/editor/csharp_project.h b/modules/mono/editor/csharp_project.h new file mode 100644 index 0000000000..4832d2251e --- /dev/null +++ b/modules/mono/editor/csharp_project.h @@ -0,0 +1,44 @@ +/*************************************************************************/ +/* csharp_project.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef CSHARP_PROJECT_H +#define CSHARP_PROJECT_H + +#include "ustring.h" + +namespace CSharpProject { + +String generate_core_api_project(const String &p_dir, const Vector<String> &p_files = Vector<String>()); +String generate_editor_api_project(const String &p_dir, const String &p_core_dll_path, const Vector<String> &p_files = Vector<String>()); +String generate_game_project(const String &p_dir, const String &p_name, const Vector<String> &p_files = Vector<String>()); + +void add_item(const String &p_project_path, const String &p_item_type, const String &p_include); +} + +#endif // CSHARP_PROJECT_H diff --git a/modules/mono/editor/godotsharp_builds.cpp b/modules/mono/editor/godotsharp_builds.cpp new file mode 100644 index 0000000000..e7d9c83421 --- /dev/null +++ b/modules/mono/editor/godotsharp_builds.cpp @@ -0,0 +1,440 @@ +/*************************************************************************/ +/* godotsharp_builds.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#include "godotsharp_builds.h" + +#include "../godotsharp_dirs.h" +#include "../mono_gd/gd_mono_class.h" +#include "../mono_gd/gd_mono_marshal.h" +#include "../utils/path_utils.h" +#include "bindings_generator.h" +#include "godotsharp_editor.h" +#include "main/main.h" + +void godot_icall_BuildInstance_ExitCallback(MonoString *p_solution, MonoString *p_config, int p_exit_code) { + + String solution = GDMonoMarshal::mono_string_to_godot(p_solution); + String config = GDMonoMarshal::mono_string_to_godot(p_config); + GodotSharpBuilds::get_singleton()->build_exit_callback(MonoBuildInfo(solution, config), p_exit_code); +} + +MonoString *godot_icall_BuildInstance_get_MSBuildPath() { + + GodotSharpBuilds::BuildTool build_tool = GodotSharpBuilds::BuildTool(int(EditorSettings::get_singleton()->get("mono/builds/build_tool"))); + +#ifdef WINDOWS_ENABLED + switch (build_tool) { + case GodotSharpBuilds::MSBUILD: { + static String msbuild_tools_path = MonoRegUtils::find_msbuild_tools_path(); + + if (msbuild_tools_path.length()) { + if (!msbuild_tools_path.ends_with("\\")) + msbuild_tools_path += "\\"; + + return GDMonoMarshal::mono_string_from_godot(msbuild_tools_path + "MSBuild.exe"); + } + + OS::get_singleton()->print("Cannot find System's MSBuild. Trying with Mono's...\n"); + } + case GodotSharpBuilds::MSBUILD_MONO: { + String msbuild_path = GDMono::get_singleton()->get_mono_reg_info().bin_dir.plus_file("msbuild.bat"); + + if (!FileAccess::exists(msbuild_path)) { + 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); + } + + return GDMonoMarshal::mono_string_from_godot(xbuild_path); + } + default: + ERR_EXPLAIN("You don't deserve to live"); + CRASH_NOW(); + } +#else + static bool msbuild_found = path_which("msbuild").length(); + + if (build_tool != GodotSharpBuilds::XBUILD && !msbuild_found) { + WARN_PRINT("Cannot find msbuild ('mono/builds/build_tool')."); + } + + return GDMonoMarshal::mono_string_from_godot(build_tool != GodotSharpBuilds::XBUILD ? "msbuild" : "xbuild"); +#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); +} + +void GodotSharpBuilds::show_build_error_dialog(const String &p_message) { + + GodotSharpEditor::get_singleton()->show_error_dialog(p_message, "Build error"); + MonoBottomPanel::get_singleton()->show_build_tab(); +} + +bool GodotSharpBuilds::build_api_sln(const String &p_name, const String &p_api_sln_dir, const String &p_config) { + + String api_sln_file = p_api_sln_dir.plus_file(p_name + ".sln"); + String api_assembly_dir = p_api_sln_dir.plus_file("bin").plus_file(p_config); + String api_assembly_file = api_assembly_dir.plus_file(p_name + ".dll"); + + if (!FileAccess::exists(api_assembly_file)) { + MonoBuildInfo api_build_info(api_sln_file, p_config); + api_build_info.custom_props.push_back("NoWarn=1591"); // Ignore missing documentation warnings + + if (!GodotSharpBuilds::get_singleton()->build(api_build_info)) { + show_build_error_dialog("Failed to build " + p_name + " solution."); + return false; + } + } + + return true; +} + +bool GodotSharpBuilds::copy_api_assembly(const String &p_src_dir, const String &p_dst_dir, const String &p_assembly_name) { + + String assembly_file = p_assembly_name + ".dll"; + String assembly_src = p_src_dir.plus_file(assembly_file); + String assembly_dst = p_dst_dir.plus_file(assembly_file); + + if (!FileAccess::exists(assembly_dst) || FileAccess::get_modified_time(assembly_src) > FileAccess::get_modified_time(assembly_dst)) { + DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + + String xml_file = p_assembly_name + ".xml"; + if (da->copy(p_src_dir.plus_file(xml_file), p_dst_dir.plus_file(xml_file)) != OK) + WARN_PRINTS("Failed to copy " + xml_file); + + String pdb_file = p_assembly_name + ".pdb"; + if (da->copy(p_src_dir.plus_file(pdb_file), p_dst_dir.plus_file(pdb_file)) != OK) + WARN_PRINTS("Failed to copy " + pdb_file); + + Error err = da->copy(assembly_src, assembly_dst); + + memdelete(da); + + if (err != OK) { + show_build_error_dialog("Failed to copy " API_ASSEMBLY_NAME ".dll"); + return false; + } + } + + return true; +} + +bool GodotSharpBuilds::make_api_sln(GodotSharpBuilds::APIType p_api_type) { + + String api_name = p_api_type == API_CORE ? API_ASSEMBLY_NAME : EDITOR_API_ASSEMBLY_NAME; + String api_build_config = "Release"; + + EditorProgress pr("mono_build_release_" + api_name, "Building " + api_name + " solution...", 4); + + pr.step("Generating " + api_name + " solution"); + + uint64_t core_hash = GDMono::get_singleton()->get_api_core_hash(); + uint64_t editor_hash = GDMono::get_singleton()->get_api_editor_hash(); + + String core_api_sln_dir = GodotSharpDirs::get_mono_solutions_dir().plus_file(API_ASSEMBLY_NAME "_" + itos(core_hash)); + String editor_api_sln_dir = GodotSharpDirs::get_mono_solutions_dir().plus_file(EDITOR_API_ASSEMBLY_NAME "_" + itos(editor_hash)); + + String api_sln_dir = p_api_type == API_CORE ? core_api_sln_dir : editor_api_sln_dir; + String api_sln_file = api_sln_dir.plus_file(api_name + ".sln"); + + if (!DirAccess::exists(api_sln_dir) || !FileAccess::exists(api_sln_file)) { + String core_api_assembly; + + if (p_api_type == API_EDITOR) { + core_api_assembly = core_api_sln_dir.plus_file("bin") + .plus_file(api_build_config) + .plus_file(API_ASSEMBLY_NAME ".dll"); + } + +#ifndef DEBUG_METHODS_ENABLED +#error "How am I supposed to generate the bindings?" +#endif + + BindingsGenerator &gen = BindingsGenerator::get_singleton(); + bool gen_verbose = OS::get_singleton()->is_stdout_verbose(); + + Error err = p_api_type == API_CORE ? + gen.generate_cs_core_project(api_sln_dir, gen_verbose) : + gen.generate_cs_editor_project(api_sln_dir, core_api_assembly, gen_verbose); + + if (err != OK) { + show_build_error_dialog("Failed to generate " + api_name + " solution. Error: " + itos(err)); + return false; + } + } + + pr.step("Building " + api_name + " solution"); + + if (!GodotSharpBuilds::build_api_sln(api_name, api_sln_dir, api_build_config)) + return false; + + pr.step("Copying " + api_name + " assembly"); + + String res_assemblies_dir = GodotSharpDirs::get_res_assemblies_dir(); + + // Create assemblies directory if needed + if (!DirAccess::exists(res_assemblies_dir)) { + DirAccess *da = DirAccess::create_for_path(res_assemblies_dir); + Error err = da->make_dir_recursive(res_assemblies_dir); + memdelete(da); + + if (err != OK) { + show_build_error_dialog("Failed to create assemblies directory. Error: " + itos(err)); + return false; + } + } + + // Copy the built assembly to the assemblies directory + String api_assembly_dir = api_sln_dir.plus_file("bin").plus_file(api_build_config); + if (!GodotSharpBuilds::copy_api_assembly(api_assembly_dir, res_assemblies_dir, api_name)) + return false; + + pr.step("Done"); + + return true; +} + +bool godotsharp_build_callback() { + + if (!FileAccess::exists(GodotSharpDirs::get_project_sln_path())) + return true; // No solution to build + + if (!GodotSharpBuilds::make_api_sln(GodotSharpBuilds::API_CORE)) + return false; + + if (!GodotSharpBuilds::make_api_sln(GodotSharpBuilds::API_EDITOR)) + return false; + + EditorProgress pr("mono_project_debug_build", "Building project solution...", 2); + + pr.step("Building project solution"); + + MonoBuildInfo build_info(GodotSharpDirs::get_project_sln_path(), "Tools"); + if (!GodotSharpBuilds::get_singleton()->build(build_info)) { + GodotSharpBuilds::show_build_error_dialog("Failed to build project solution"); + return false; + } + + pr.step("Done"); + + return true; +} + +GodotSharpBuilds *GodotSharpBuilds::singleton = NULL; + +void GodotSharpBuilds::build_exit_callback(const MonoBuildInfo &p_build_info, int p_exit_code) { + + BuildProcess *match = builds.getptr(p_build_info); + ERR_FAIL_COND(!match); + + BuildProcess &bp = *match; + bp.on_exit(p_exit_code); +} + +void GodotSharpBuilds::restart_build(MonoBuildTab *p_build_tab) { +} + +void GodotSharpBuilds::stop_build(MonoBuildTab *p_build_tab) { +} + +bool GodotSharpBuilds::build(const MonoBuildInfo &p_build_info) { + + BuildProcess *match = builds.getptr(p_build_info); + + if (match) { + BuildProcess &bp = *match; + bp.start(true); + return bp.exit_code == 0; + } else { + BuildProcess bp = BuildProcess(p_build_info); + bp.start(true); + builds.set(p_build_info, bp); + return bp.exit_code == 0; + } +} + +bool GodotSharpBuilds::build_async(const MonoBuildInfo &p_build_info, GodotSharpBuild_ExitCallback p_callback) { + + BuildProcess *match = builds.getptr(p_build_info); + + if (match) { + BuildProcess &bp = *match; + bp.start(); + return !bp.exited; // failed to start + } else { + BuildProcess bp = BuildProcess(p_build_info, p_callback); + bp.start(); + builds.set(p_build_info, bp); + return !bp.exited; // failed to start + } +} + +GodotSharpBuilds::GodotSharpBuilds() { + + singleton = this; + + EditorNode::get_singleton()->add_build_callback(&godotsharp_build_callback); + + // Build tool settings + EditorSettings *ed_settings = EditorSettings::get_singleton(); + if (!ed_settings->has("mono/builds/build_tool")) { + ed_settings->set("mono/builds/build_tool", MSBUILD); + } + ed_settings->add_property_hint(PropertyInfo(Variant::INT, "mono/builds/build_tool", PROPERTY_HINT_ENUM, "MSBuild (System),MSBuild (Mono),xbuild")); +} + +GodotSharpBuilds::~GodotSharpBuilds() { + + singleton = NULL; +} + +void GodotSharpBuilds::BuildProcess::on_exit(int p_exit_code) { + + exited = true; + exit_code = p_exit_code; + build_tab->on_build_exit(p_exit_code == 0 ? MonoBuildTab::RESULT_SUCCESS : MonoBuildTab::RESULT_ERROR); + build_instance.unref(); + + if (exit_callback) + exit_callback(exit_code); +} + +void GodotSharpBuilds::BuildProcess::start(bool p_blocking) { + + _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN) + + exit_code = -1; + + String logs_dir = GodotSharpDirs::get_build_logs_dir().plus_file(build_info.solution.md5_text() + "_" + build_info.configuration); + + if (build_tab) { + build_tab->on_build_start(); + } else { + build_tab = memnew(MonoBuildTab(build_info, logs_dir)); + MonoBottomPanel::get_singleton()->add_build_tab(build_tab); + } + + if (p_blocking) { + // Required in order to update the build tasks list + Main::iteration(); + } + + if (!exited) { + ERR_PRINT("BuildProcess::start called, but process still running"); + exited = true; + build_tab->on_build_exec_failed("!exited"); + return; + } + + exited = false; + + // Remove old issues file + + String issues_file = "msbuild_issues.csv"; + DirAccessRef d = DirAccess::create_for_path(logs_dir); + if (d->file_exists(issues_file)) { + Error err = d->remove(issues_file); + if (err != OK) { + ERR_PRINTS("Cannot remove file: " + logs_dir.plus_file(issues_file)); + exited = true; + build_tab->on_build_exec_failed("Cannot remove file: " + issues_file); + return; + } + } + + GDMonoClass *klass = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Build", "BuildInstance"); + + MonoObject *mono_object = mono_object_new(mono_domain_get(), klass->get_raw()); + + // Construct + + Variant solution = build_info.solution; + Variant config = build_info.configuration; + + const Variant *ctor_args[2] = { &solution, &config }; + + MonoObject *ex = NULL; + GDMonoMethod *ctor = klass->get_method(".ctor", 2); + ctor->invoke(mono_object, ctor_args, &ex); + + if (ex) { + exited = true; + build_tab->on_build_exec_failed("The build constructor threw an exception.\n" + GDMonoUtils::get_exception_name_and_message(ex)); + ERR_FAIL(); + } + + // Call Build + + Variant logger_assembly = OS::get_singleton()->get_executable_path().get_base_dir().plus_file(EDITOR_TOOLS_ASSEMBLY_NAME) + ".dll"; + Variant logger_output_dir = logs_dir; + Variant custom_props = build_info.custom_props; + + const Variant *args[3] = { &logger_assembly, &logger_output_dir, &custom_props }; + + ex = NULL; + GDMonoMethod *build_method = klass->get_method(p_blocking ? "Build" : "BuildAsync", 3); + build_method->invoke(mono_object, args, &ex); + + if (ex) { + exited = true; + build_tab->on_build_exec_failed("The build method threw an exception.\n" + GDMonoUtils::get_exception_name_and_message(ex)); + ERR_FAIL(); + } + + // Build returned + + if (p_blocking) { + exited = true; + exit_code = klass->get_field("exitCode")->get_int_value(mono_object); + build_tab->on_build_exit(exit_code == 0 ? MonoBuildTab::RESULT_SUCCESS : MonoBuildTab::RESULT_ERROR); + } else { + build_instance = MonoGCHandle::create_strong(mono_object); + exited = false; + } +} + +GodotSharpBuilds::BuildProcess::BuildProcess(const MonoBuildInfo &p_build_info, GodotSharpBuild_ExitCallback p_callback) { + + build_info = p_build_info; + build_tab = NULL; + exit_callback = p_callback; + exited = true; + exit_code = -1; +} diff --git a/modules/mono/editor/godotsharp_builds.h b/modules/mono/editor/godotsharp_builds.h new file mode 100644 index 0000000000..6d5fa3b44a --- /dev/null +++ b/modules/mono/editor/godotsharp_builds.h @@ -0,0 +1,96 @@ +/*************************************************************************/ +/* godotsharp_builds.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef GODOTSHARP_BUILDS_H +#define GODOTSHARP_BUILDS_H + +#include "mono_bottom_panel.h" +#include "mono_build_info.h" + +typedef void (*GodotSharpBuild_ExitCallback)(int); + +class GodotSharpBuilds { + +private: + struct BuildProcess { + Ref<MonoGCHandle> build_instance; + MonoBuildInfo build_info; + MonoBuildTab *build_tab; + GodotSharpBuild_ExitCallback exit_callback; + bool exited; + int exit_code; + + void on_exit(int p_exit_code); + void start(bool p_blocking = false); + + BuildProcess() {} + BuildProcess(const MonoBuildInfo &p_build_info, GodotSharpBuild_ExitCallback p_callback = NULL); + }; + + HashMap<MonoBuildInfo, BuildProcess, MonoBuildInfo::Hasher> builds; + + static GodotSharpBuilds *singleton; + + friend class GDMono; + static void _register_internal_calls(); + +public: + enum APIType { + API_CORE, + API_EDITOR + }; + + enum BuildTool { + MSBUILD, + MSBUILD_MONO, + XBUILD + }; + + _FORCE_INLINE_ static GodotSharpBuilds *get_singleton() { return singleton; } + + static void show_build_error_dialog(const String &p_message); + + void build_exit_callback(const MonoBuildInfo &p_build_info, int p_exit_code); + + void restart_build(MonoBuildTab *p_build_tab); + void stop_build(MonoBuildTab *p_build_tab); + + bool build(const MonoBuildInfo &p_build_info); + bool build_async(const MonoBuildInfo &p_build_info, GodotSharpBuild_ExitCallback p_callback = NULL); + + static bool build_api_sln(const String &p_name, const String &p_api_sln_dir, const String &p_config); + static bool copy_api_assembly(const String &p_src_dir, const String &p_dst_dir, const String &p_assembly_name); + + static bool make_api_sln(APIType p_api_type); + + GodotSharpBuilds(); + ~GodotSharpBuilds(); +}; + +#endif // GODOTSHARP_BUILDS_H diff --git a/modules/mono/editor/godotsharp_editor.cpp b/modules/mono/editor/godotsharp_editor.cpp new file mode 100644 index 0000000000..5aaf029495 --- /dev/null +++ b/modules/mono/editor/godotsharp_editor.cpp @@ -0,0 +1,256 @@ +/*************************************************************************/ +/* godotsharp_editor.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#include "godotsharp_editor.h" + +#include "core/os/os.h" +#include "core/project_settings.h" +#include "scene/gui/control.h" +#include "scene/main/node.h" + +#include "../csharp_script.h" +#include "../godotsharp_dirs.h" +#include "../mono_gd/gd_mono.h" +#include "../utils/path_utils.h" +#include "bindings_generator.h" +#include "csharp_project.h" +#include "net_solution.h" + +#ifdef WINDOWS_ENABLED +#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() { + + EditorProgress pr("create_csharp_solution", "Generating solution...", 2); + + pr.step("Generating C# project..."); + + String path = OS::get_singleton()->get_resource_dir(); + String name = ProjectSettings::get_singleton()->get("application/config/name"); + String guid = CSharpProject::generate_game_project(path, name); + + if (guid.length()) { + + NETSolution solution(name); + + if (!solution.set_path(path)) { + show_error_dialog("Failed to create solution."); + return false; + } + + Vector<String> extra_configs; + extra_configs.push_back("Tools"); + + solution.add_new_project(name, guid, extra_configs); + + Error sln_error = solution.save(); + + if (sln_error != OK) { + show_error_dialog("Failed to save solution."); + return false; + } + + if (!GodotSharpBuilds::make_api_sln(GodotSharpBuilds::API_CORE)) + return false; + + if (!GodotSharpBuilds::make_api_sln(GodotSharpBuilds::API_EDITOR)) + return false; + + pr.step("Done"); + + // Here, after all calls to progress_task_step + call_deferred("_remove_create_sln_menu_option"); + + } else { + show_error_dialog("Failed to create C# project."); + } + + return true; +} + +void GodotSharpEditor::_remove_create_sln_menu_option() { + + menu_popup->remove_item(menu_popup->get_item_index(MENU_CREATE_SLN)); + + if (menu_popup->get_item_count() == 0) + menu_button->hide(); + + bottom_panel_btn->show(); +} + +void GodotSharpEditor::_menu_option_pressed(int p_id) { + + switch (p_id) { + case MENU_CREATE_SLN: { + + _create_project_solution(); + } break; + default: + ERR_FAIL(); + } +} + +void GodotSharpEditor::_bind_methods() { + + ClassDB::bind_method(D_METHOD("_create_project_solution"), &GodotSharpEditor::_create_project_solution); + ClassDB::bind_method(D_METHOD("_remove_create_sln_menu_option"), &GodotSharpEditor::_remove_create_sln_menu_option); + ClassDB::bind_method(D_METHOD("_menu_option_pressed", "id"), &GodotSharpEditor::_menu_option_pressed); +} + +void GodotSharpEditor::show_error_dialog(const String &p_message, const String &p_title) { + + error_dialog->set_title(p_title); + error_dialog->set_text(p_message); + error_dialog->popup_centered_minsize(); +} + +Error GodotSharpEditor::open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col) { + + ExternalEditor editor = ExternalEditor(int(EditorSettings::get_singleton()->get("mono/editor/external_editor"))); + + switch (editor) { + case EDITOR_CODE: { + List<String> args; + args.push_back(ProjectSettings::get_singleton()->get_resource_path()); + + String script_path = ProjectSettings::get_singleton()->globalize_path(p_script->get_path()); + + if (p_line >= 0) { + args.push_back("-g"); + args.push_back(script_path + ":" + itos(p_line) + ":" + itos(p_col)); + } else { + args.push_back(script_path); + } + + static String program = path_which("code"); + + Error err = OS::get_singleton()->execute(program.length() ? program : "code", args, false); + + if (err != OK) { + ERR_PRINT("GodotSharp: Could not execute external editor"); + return err; + } + } break; + case EDITOR_MONODEVELOP: { + if (!monodevel_instance) + monodevel_instance = memnew(MonoDevelopInstance(GodotSharpDirs::get_project_sln_path())); + + String script_path = ProjectSettings::get_singleton()->globalize_path(p_script->get_path()); + monodevel_instance->execute(script_path); + } break; + case EDITOR_VISUAL_STUDIO: + // TODO + // devenv <PathToSolutionFolder> + // devenv /edit <PathToCsFile> /command "edit.goto <Line>" + // HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7 + default: + return ERR_UNAVAILABLE; + } + + return OK; +} + +bool GodotSharpEditor::overrides_external_editor() { + + return ExternalEditor(int(EditorSettings::get_singleton()->get("mono/editor/external_editor"))) != EDITOR_NONE; +} + +GodotSharpEditor::GodotSharpEditor(EditorNode *p_editor) { + + singleton = this; + + monodevel_instance = NULL; + + editor = p_editor; + + error_dialog = memnew(AcceptDialog); + editor->get_gui_base()->add_child(error_dialog); + + bottom_panel_btn = editor->add_bottom_panel_item("Mono", memnew(MonoBottomPanel(editor))); + + godotsharp_builds = memnew(GodotSharpBuilds); + + editor->add_child(memnew(MonoReloadNode)); + + menu_button = memnew(MenuButton); + menu_button->set_text("Mono"); + menu_popup = menu_button->get_popup(); + + String sln_path = GodotSharpDirs::get_project_sln_path(); + String csproj_path = GodotSharpDirs::get_project_csproj_path(); + + if (!FileAccess::exists(sln_path) || !FileAccess::exists(csproj_path)) { + bottom_panel_btn->hide(); + menu_popup->add_item("Create C# solution", MENU_CREATE_SLN); + } + + menu_popup->connect("id_pressed", this, "_menu_option_pressed"); + + if (menu_popup->get_item_count() == 0) + menu_button->hide(); + + editor->get_menu_hb()->add_child(menu_button); + + // External editor settings + EditorSettings *ed_settings = EditorSettings::get_singleton(); + if (!ed_settings->has("mono/editor/external_editor")) { + ed_settings->set("mono/editor/external_editor", EDITOR_NONE); + } + ed_settings->add_property_hint(PropertyInfo(Variant::INT, "mono/editor/external_editor", PROPERTY_HINT_ENUM, "None,MonoDevelop,Visual Studio,Visual Studio Code")); +} + +GodotSharpEditor::~GodotSharpEditor() { + + singleton = NULL; + + memdelete(godotsharp_builds); + + if (monodevel_instance) { + memdelete(monodevel_instance); + monodevel_instance = NULL; + } +} diff --git a/modules/mono/editor/godotsharp_editor.h b/modules/mono/editor/godotsharp_editor.h new file mode 100644 index 0000000000..1ecb8c7a94 --- /dev/null +++ b/modules/mono/editor/godotsharp_editor.h @@ -0,0 +1,87 @@ +/*************************************************************************/ +/* godotsharp_editor.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef GODOTSHARP_EDITOR_H +#define GODOTSHARP_EDITOR_H + +#include "godotsharp_builds.h" + +#include "monodevelop_instance.h" + +class GodotSharpEditor : public Node { + GDCLASS(GodotSharpEditor, Object) + + EditorNode *editor; + + MenuButton *menu_button; + PopupMenu *menu_popup; + + AcceptDialog *error_dialog; + + ToolButton *bottom_panel_btn; + + GodotSharpBuilds *godotsharp_builds; + + MonoDevelopInstance *monodevel_instance; + + bool _create_project_solution(); + + void _remove_create_sln_menu_option(); + + void _menu_option_pressed(int p_id); + + static GodotSharpEditor *singleton; + +protected: + static void _bind_methods(); + +public: + enum MenuOptions { + MENU_CREATE_SLN + }; + + enum ExternalEditor { + EDITOR_NONE, + EDITOR_MONODEVELOP, + EDITOR_VISUAL_STUDIO, + EDITOR_CODE, + }; + + _FORCE_INLINE_ static GodotSharpEditor *get_singleton() { return singleton; } + + void show_error_dialog(const String &p_message, const String &p_title = "Error"); + + Error open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col); + bool overrides_external_editor(); + + GodotSharpEditor(EditorNode *p_editor); + ~GodotSharpEditor(); +}; + +#endif // GODOTSHARP_EDITOR_H diff --git a/modules/mono/editor/mono_bottom_panel.cpp b/modules/mono/editor/mono_bottom_panel.cpp new file mode 100644 index 0000000000..07109eaac7 --- /dev/null +++ b/modules/mono/editor/mono_bottom_panel.cpp @@ -0,0 +1,441 @@ +/*************************************************************************/ +/* mono_bottom_panel.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#include "mono_bottom_panel.h" + +#include "../csharp_script.h" +#include "godotsharp_editor.h" + +MonoBottomPanel *MonoBottomPanel::singleton = NULL; + +void MonoBottomPanel::_update_build_tabs_list() { + + build_tabs_list->clear(); + + int current_tab = build_tabs->get_current_tab(); + + bool no_current_tab = current_tab < 0 || current_tab >= build_tabs->get_tab_count(); + + for (int i = 0; i < build_tabs->get_child_count(); i++) { + + MonoBuildTab *tab = Object::cast_to<MonoBuildTab>(build_tabs->get_child(i)); + + if (tab) { + String item_name = tab->build_info.solution.get_file().get_basename(); + item_name += " [" + tab->build_info.configuration + "]"; + + build_tabs_list->add_item(item_name, tab->get_icon_texture()); + + String item_tooltip = String("Solution: ") + tab->build_info.solution; + item_tooltip += String("\nConfiguration: ") + tab->build_info.configuration; + item_tooltip += String("\nStatus: "); + + if (tab->build_exited) { + item_tooltip += tab->build_result == MonoBuildTab::RESULT_SUCCESS ? "Succeeded" : "Errored"; + } else { + item_tooltip += "Running"; + } + + if (!tab->build_exited || !tab->build_result == MonoBuildTab::RESULT_SUCCESS) { + item_tooltip += "\nErrors: " + itos(tab->error_count); + } + + item_tooltip += "\nWarnings: " + itos(tab->warning_count); + + build_tabs_list->set_item_tooltip(i, item_tooltip); + + if (no_current_tab || current_tab == i) { + build_tabs_list->select(i); + _build_tab_item_selected(i); + } + } + } +} + +void MonoBottomPanel::add_build_tab(MonoBuildTab *p_build_tab) { + + build_tabs->add_child(p_build_tab); + raise_build_tab(p_build_tab); +} + +void MonoBottomPanel::raise_build_tab(MonoBuildTab *p_build_tab) { + + ERR_FAIL_COND(p_build_tab->get_parent() != build_tabs); + build_tabs->move_child(p_build_tab, 0); + _update_build_tabs_list(); +} + +void MonoBottomPanel::show_build_tab() { + + for (int i = 0; i < panel_tabs->get_tab_count(); i++) { + if (panel_tabs->get_tab_control(i) == panel_builds_tab) { + panel_tabs->set_current_tab(i); + editor->make_bottom_panel_item_visible(this); + return; + } + } + + ERR_PRINT("Builds tab not found"); +} + +void MonoBottomPanel::_build_tab_item_selected(int p_idx) { + + ERR_FAIL_INDEX(p_idx, build_tabs->get_tab_count()); + build_tabs->set_current_tab(p_idx); +} + +void MonoBottomPanel::_build_tab_changed(int p_idx) { + + if (p_idx < 0 || p_idx >= build_tabs->get_tab_count()) { + warnings_btn->set_visible(false); + errors_btn->set_visible(false); + } else { + warnings_btn->set_visible(true); + errors_btn->set_visible(true); + } +} + +void MonoBottomPanel::_warnings_toggled(bool p_pressed) { + + int current_tab = build_tabs->get_current_tab(); + ERR_FAIL_INDEX(current_tab, build_tabs->get_tab_count()); + MonoBuildTab *build_tab = Object::cast_to<MonoBuildTab>(build_tabs->get_child(current_tab)); + build_tab->warnings_visible = p_pressed; + build_tab->_update_issues_list(); +} + +void MonoBottomPanel::_errors_toggled(bool p_pressed) { + + int current_tab = build_tabs->get_current_tab(); + ERR_FAIL_INDEX(current_tab, build_tabs->get_tab_count()); + MonoBuildTab *build_tab = Object::cast_to<MonoBuildTab>(build_tabs->get_child(current_tab)); + build_tab->errors_visible = p_pressed; + build_tab->_update_issues_list(); +} + +void MonoBottomPanel::_notification(int p_what) { + + switch (p_what) { + + case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { + panel_tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles")); + panel_tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles")); + panel_tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles")); + } break; + } +} + +void MonoBottomPanel::_bind_methods() { + + 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); + ClassDB::bind_method(D_METHOD("_build_tab_changed", "idx"), &MonoBottomPanel::_build_tab_changed); +} + +MonoBottomPanel::MonoBottomPanel(EditorNode *p_editor) { + + singleton = this; + + editor = p_editor; + + set_v_size_flags(SIZE_EXPAND_FILL); + set_anchors_and_margins_preset(Control::PRESET_WIDE); + + panel_tabs = memnew(TabContainer); + panel_tabs->set_tab_align(TabContainer::ALIGN_LEFT); + panel_tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles")); + panel_tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles")); + panel_tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles")); + panel_tabs->set_custom_minimum_size(Size2(0, 228) * EDSCALE); + panel_tabs->set_v_size_flags(SIZE_EXPAND_FILL); + add_child(panel_tabs); + + { // Builds + panel_builds_tab = memnew(VBoxContainer); + panel_builds_tab->set_name(TTR("Builds")); + panel_builds_tab->set_h_size_flags(SIZE_EXPAND_FILL); + panel_tabs->add_child(panel_builds_tab); + + HBoxContainer *toolbar_hbc = memnew(HBoxContainer); + toolbar_hbc->set_h_size_flags(SIZE_EXPAND_FILL); + panel_builds_tab->add_child(toolbar_hbc); + + toolbar_hbc->add_spacer(); + + warnings_btn = memnew(ToolButton); + warnings_btn->set_text("Warnings"); + warnings_btn->set_toggle_mode(true); + warnings_btn->set_pressed(true); + warnings_btn->set_visible(false); + warnings_btn->set_focus_mode(FOCUS_NONE); + warnings_btn->connect("toggled", this, "_warnings_toggled"); + toolbar_hbc->add_child(warnings_btn); + + errors_btn = memnew(ToolButton); + errors_btn->set_text("Errors"); + errors_btn->set_toggle_mode(true); + errors_btn->set_pressed(true); + errors_btn->set_visible(false); + errors_btn->set_focus_mode(FOCUS_NONE); + errors_btn->connect("toggled", this, "_errors_toggled"); + toolbar_hbc->add_child(errors_btn); + + HSplitContainer *hsc = memnew(HSplitContainer); + hsc->set_h_size_flags(SIZE_EXPAND_FILL); + hsc->set_v_size_flags(SIZE_EXPAND_FILL); + panel_builds_tab->add_child(hsc); + + build_tabs_list = memnew(ItemList); + build_tabs_list->set_h_size_flags(SIZE_EXPAND_FILL); + build_tabs_list->connect("item_selected", this, "_build_tab_item_selected"); + hsc->add_child(build_tabs_list); + + build_tabs = memnew(TabContainer); + build_tabs->set_tab_align(TabContainer::ALIGN_LEFT); + build_tabs->set_h_size_flags(SIZE_EXPAND_FILL); + build_tabs->set_tabs_visible(false); + build_tabs->connect("tab_changed", this, "_build_tab_changed"); + hsc->add_child(build_tabs); + } +} + +MonoBottomPanel::~MonoBottomPanel() { + + singleton = NULL; +} + +void MonoBuildTab::_load_issues_from_file(const String &p_csv_file) { + + FileAccessRef f = FileAccess::open(p_csv_file, FileAccess::READ); + + if (!f) + return; + + while (!f->eof_reached()) { + Vector<String> csv_line = f->get_csv_line(); + + if (csv_line.size() == 1 && csv_line[0].empty()) + return; + + ERR_CONTINUE(csv_line.size() != 7); + + BuildIssue issue; + issue.warning = csv_line[0] == "warning"; + issue.file = csv_line[1]; + issue.line = csv_line[2].to_int(); + issue.column = csv_line[3].to_int(); + issue.code = csv_line[4]; + issue.message = csv_line[5]; + issue.project_file = csv_line[6]; + + if (issue.warning) + warning_count += 1; + else + error_count += 1; + + issues.push_back(issue); + } +} + +void MonoBuildTab::_update_issues_list() { + + issues_list->clear(); + + Ref<Texture> warning_icon = get_icon("Warning", "EditorIcons"); + Ref<Texture> error_icon = get_icon("Error", "EditorIcons"); + + for (int i = 0; i < issues.size(); i++) { + + const BuildIssue &issue = issues[i]; + + if (!(issue.warning ? warnings_visible : errors_visible)) + continue; + + String tooltip; + tooltip += String("Message: ") + issue.message; + tooltip += String("\nCode: ") + issue.code; + tooltip += String("\nType: ") + (issue.warning ? "warning" : "error"); + + String text; + + if (issue.file.length()) { + String sline = String::num_int64(issue.line); + String scolumn = String::num_int64(issue.column); + + text += issue.file + "("; + text += sline + ","; + text += scolumn + "): "; + + tooltip += "\nFile: " + issue.file; + tooltip += "\nLine: " + sline; + tooltip += "\nColumn: " + scolumn; + } + + if (issue.project_file.length()) { + tooltip += "\nProject: " + issue.project_file; + } + + text += issue.message; + + int line_break_idx = text.find("\n"); + issues_list->add_item(line_break_idx == -1 ? text : text.substr(0, line_break_idx), + issue.warning ? warning_icon : error_icon); + int index = issues_list->get_item_count() - 1; + issues_list->set_item_tooltip(index, tooltip); + issues_list->set_item_metadata(index, i); + } +} + +Ref<Texture> MonoBuildTab::get_icon_texture() const { + + // FIXME these icons were removed... find something better + + if (build_exited) { + if (build_result == RESULT_ERROR) { + return get_icon("DependencyChangedHl", "EditorIcons"); + } else { + return get_icon("DependencyOkHl", "EditorIcons"); + } + } else { + return get_icon("GraphTime", "EditorIcons"); + } +} + +MonoBuildInfo MonoBuildTab::get_build_info() { + + return build_info; +} + +void MonoBuildTab::on_build_start() { + + build_exited = false; + + issues.clear(); + warning_count = 0; + error_count = 0; + _update_issues_list(); + + MonoBottomPanel::get_singleton()->raise_build_tab(this); +} + +void MonoBuildTab::on_build_exit(BuildResult result) { + + build_exited = true; + build_result = result; + + _load_issues_from_file(logs_dir.plus_file("msbuild_issues.csv")); + _update_issues_list(); + + MonoBottomPanel::get_singleton()->raise_build_tab(this); +} + +void MonoBuildTab::on_build_exec_failed(const String &p_cause, const String &p_detailed) { + + build_exited = true; + build_result = RESULT_ERROR; + + issues_list->clear(); + + String tooltip; + + tooltip += "Message: " + (p_detailed.length() ? p_detailed : p_cause); + tooltip += "\nType: error"; + + int line_break_idx = p_cause.find("\n"); + issues_list->add_item(line_break_idx == -1 ? p_cause : p_cause.substr(0, line_break_idx), + get_icon("Error", "EditorIcons")); + int index = issues_list->get_item_count() - 1; + issues_list->set_item_tooltip(index, tooltip); + + MonoBottomPanel::get_singleton()->raise_build_tab(this); +} + +void MonoBuildTab::restart_build() { + + ERR_FAIL_COND(!build_exited); + GodotSharpBuilds::get_singleton()->restart_build(this); +} + +void MonoBuildTab::stop_build() { + + ERR_FAIL_COND(build_exited); + GodotSharpBuilds::get_singleton()->stop_build(this); +} + +void MonoBuildTab::_issue_activated(int p_idx) { + + ERR_FAIL_INDEX(p_idx, issues.size()); + + const BuildIssue &issue = issues[p_idx]; + + if (issue.project_file.empty() && issue.file.empty()) + return; + + String project_dir = issue.project_file.length() ? issue.project_file.get_base_dir() : build_info.solution.get_base_dir(); + + String file = project_dir.simplify_path().plus_file(issue.file.simplify_path()); + + if (!FileAccess::exists(file)) + return; + + file = ProjectSettings::get_singleton()->localize_path(file); + + if (file.begins_with("res://")) { + Ref<Script> script = ResourceLoader::load(file, CSharpLanguage::get_singleton()->get_type()); + + if (script.is_valid() && ScriptEditor::get_singleton()->edit(script, issue.line, issue.column)) { + EditorNode::get_singleton()->call("_editor_select", EditorNode::EDITOR_SCRIPT); + } + } +} + +void MonoBuildTab::_bind_methods() { + + ClassDB::bind_method("_issue_activated", &MonoBuildTab::_issue_activated); +} + +MonoBuildTab::MonoBuildTab(const MonoBuildInfo &p_build_info, const String &p_logs_dir) { + + build_info = p_build_info; + logs_dir = p_logs_dir; + + build_exited = false; + + issues_list = memnew(ItemList); + issues_list->set_v_size_flags(SIZE_EXPAND_FILL); + issues_list->connect("item_activated", this, "_issue_activated"); + add_child(issues_list); + + error_count = 0; + warning_count = 0; + + errors_visible = true; + warnings_visible = true; +} diff --git a/modules/mono/editor/mono_bottom_panel.h b/modules/mono/editor/mono_bottom_panel.h new file mode 100644 index 0000000000..909fa4b385 --- /dev/null +++ b/modules/mono/editor/mono_bottom_panel.h @@ -0,0 +1,145 @@ +/*************************************************************************/ +/* mono_bottom_panel.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef MONO_BOTTOM_PANEL_H +#define MONO_BOTTOM_PANEL_H + +#include "editor/editor_node.h" +#include "scene/gui/control.h" + +#include "mono_build_info.h" + +class MonoBuildTab; + +class MonoBottomPanel : public VBoxContainer { + + GDCLASS(MonoBottomPanel, VBoxContainer) + + EditorNode *editor; + + TabContainer *panel_tabs; + + VBoxContainer *panel_builds_tab; + + ItemList *build_tabs_list; + TabContainer *build_tabs; + + Button *warnings_btn; + Button *errors_btn; + + void _update_build_tabs_list(); + + void _build_tab_item_selected(int p_idx); + void _build_tab_changed(int p_idx); + + void _warnings_toggled(bool p_pressed); + void _errors_toggled(bool p_pressed); + + static MonoBottomPanel *singleton; + +protected: + void _notification(int p_what); + + static void _bind_methods(); + +public: + _FORCE_INLINE_ static MonoBottomPanel *get_singleton() { return singleton; } + + void add_build_tab(MonoBuildTab *p_build_tab); + void raise_build_tab(MonoBuildTab *p_build_tab); + + void show_build_tab(); + + MonoBottomPanel(EditorNode *p_editor = NULL); + ~MonoBottomPanel(); +}; + +class MonoBuildTab : public VBoxContainer { + + GDCLASS(MonoBuildTab, VBoxContainer) + +public: + enum BuildResult { + RESULT_ERROR, + RESULT_SUCCESS + }; + + struct BuildIssue { + bool warning; + String file; + int line; + int column; + String code; + String message; + String project_file; + }; + +private: + friend class MonoBottomPanel; + + bool build_exited; + BuildResult build_result; + + Vector<BuildIssue> issues; + ItemList *issues_list; + + int error_count; + int warning_count; + + bool errors_visible; + bool warnings_visible; + + String logs_dir; + + MonoBuildInfo build_info; + + void _load_issues_from_file(const String &p_csv_file); + void _update_issues_list(); + + void _issue_activated(int p_idx); + +protected: + static void _bind_methods(); + +public: + Ref<Texture> get_icon_texture() const; + + MonoBuildInfo get_build_info(); + + void on_build_start(); + void on_build_exit(BuildResult result); + void on_build_exec_failed(const String &p_cause, const String &p_detailed = String()); + + void restart_build(); + void stop_build(); + + MonoBuildTab(const MonoBuildInfo &p_build_info, const String &p_logs_dir); +}; + +#endif // MONO_BOTTOM_PANEL_H diff --git a/modules/mono/editor/mono_build_info.h b/modules/mono/editor/mono_build_info.h new file mode 100644 index 0000000000..f3b3e43b6d --- /dev/null +++ b/modules/mono/editor/mono_build_info.h @@ -0,0 +1,64 @@ +/*************************************************************************/ +/* mono_build_info.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef MONO_BUILD_INFO_H +#define MONO_BUILD_INFO_H + +#include "../mono_gd/gd_mono_utils.h" + +struct MonoBuildInfo { + + struct Hasher { + static _FORCE_INLINE_ uint32_t hash(const MonoBuildInfo &p_key) { + uint32_t hash = 0; + + GDMonoUtils::hash_combine(hash, p_key.solution.hash()); + GDMonoUtils::hash_combine(hash, p_key.configuration.hash()); + + return hash; + } + }; + + String solution; + String configuration; + Vector<String> custom_props; + + MonoBuildInfo() {} + + MonoBuildInfo(const String &p_solution, const String &p_config) { + solution = p_solution; + configuration = p_config; + } + + bool operator==(const MonoBuildInfo &p_b) const { + return p_b.solution == solution && p_b.configuration == configuration; + } +}; + +#endif // MONO_BUILD_INFO_H diff --git a/modules/mono/editor/monodevelop_instance.cpp b/modules/mono/editor/monodevelop_instance.cpp new file mode 100644 index 0000000000..a34d82ffcb --- /dev/null +++ b/modules/mono/editor/monodevelop_instance.cpp @@ -0,0 +1,81 @@ +/*************************************************************************/ +/* monodevelop_instance.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#include "monodevelop_instance.h" + +#include "../mono_gd/gd_mono.h" +#include "../mono_gd/gd_mono_class.h" + +void MonoDevelopInstance::execute(const Vector<String> &p_files) { + + ERR_FAIL_NULL(execute_method); + ERR_FAIL_COND(gc_handle.is_null()); + + MonoObject *ex = NULL; + + Variant files = p_files; + const Variant *args[1] = { &files }; + execute_method->invoke(gc_handle->get_target(), args, &ex); + + if (ex) { + mono_print_unhandled_exception(ex); + ERR_FAIL(); + } +} + +void MonoDevelopInstance::execute(const String &p_file) { + + Vector<String> files; + files.push_back(p_file); + execute(files); +} + +MonoDevelopInstance::MonoDevelopInstance(const String &p_solution) { + + _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN) + + GDMonoClass *klass = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Editor", "MonoDevelopInstance"); + + MonoObject *obj = mono_object_new(TOOLS_DOMAIN, klass->get_raw()); + + GDMonoMethod *ctor = klass->get_method(".ctor", 1); + MonoObject *ex = NULL; + + Variant solution = p_solution; + const Variant *args[1] = { &solution }; + ctor->invoke(obj, args, &ex); + + if (ex) { + mono_print_unhandled_exception(ex); + ERR_FAIL(); + } + + gc_handle = MonoGCHandle::create_strong(obj); + execute_method = klass->get_method("Execute", 1); +} diff --git a/modules/mono/editor/monodevelop_instance.h b/modules/mono/editor/monodevelop_instance.h new file mode 100644 index 0000000000..9eb154eba1 --- /dev/null +++ b/modules/mono/editor/monodevelop_instance.h @@ -0,0 +1,50 @@ +/*************************************************************************/ +/* monodevelop_instance.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef MONODEVELOP_INSTANCE_H +#define MONODEVELOP_INSTANCE_H + +#include "reference.h" + +#include "../mono_gc_handle.h" +#include "../mono_gd/gd_mono_method.h" + +class MonoDevelopInstance { + + Ref<MonoGCHandle> gc_handle; + GDMonoMethod *execute_method; + +public: + void execute(const Vector<String> &p_files); + void execute(const String &p_files); + + MonoDevelopInstance(const String &p_solution); +}; + +#endif // MONODEVELOP_INSTANCE_H diff --git a/modules/mono/editor/net_solution.cpp b/modules/mono/editor/net_solution.cpp new file mode 100644 index 0000000000..fa60c310db --- /dev/null +++ b/modules/mono/editor/net_solution.cpp @@ -0,0 +1,130 @@ +/*************************************************************************/ +/* net_solution.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#include "net_solution.h" + +#include "os/dir_access.h" +#include "os/file_access.h" + +#include "../utils/path_utils.h" +#include "../utils/string_utils.h" +#include "csharp_project.h" + +#define SOLUTION_TEMPLATE \ + "Microsoft Visual Studio Solution File, Format Version 12.00\n" \ + "# Visual Studio 2012\n" \ + "%0\n" \ + "Global\n" \ + "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n" \ + "%1\n" \ + "\tEndGlobalSection\n" \ + "\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\n" \ + "%2\n" \ + "\tEndGlobalSection\n" \ + "EndGlobal\n" + +#define PROJECT_DECLARATION "Project(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"%0\", \"%1\", \"{%2}\"\nEndProject" + +#define SOLUTION_PLATFORMS_CONFIG "\t\%0|Any CPU = %0|Any CPU" + +#define PROJECT_PLATFORMS_CONFIG \ + "\t\t{%0}.%1|Any CPU.ActiveCfg = %1|Any CPU\n" \ + "\t\t{%0}.%1|Any CPU.Build.0 = %1|Any CPU" + +void NETSolution::add_new_project(const String &p_name, const String &p_guid, const Vector<String> &p_extra_configs) { + if (projects.has(p_name)) + WARN_PRINT("Overriding existing project."); + + ProjectInfo procinfo; + procinfo.guid = p_guid; + + procinfo.configs.push_back("Debug"); + procinfo.configs.push_back("Release"); + + for (int i = 0; i < p_extra_configs.size(); i++) { + procinfo.configs.push_back(p_extra_configs[i]); + } + + projects[p_name] = procinfo; +} + +Error NETSolution::save() { + bool dir_exists = DirAccess::exists(path); + ERR_EXPLAIN("The directory does not exist."); + ERR_FAIL_COND_V(!dir_exists, ERR_FILE_BAD_PATH); + + String projs_decl; + String sln_platform_cfg; + String proj_platform_cfg; + + for (Map<String, ProjectInfo>::Element *E = projects.front(); E; E = E->next()) { + const String &name = E->key(); + const ProjectInfo &procinfo = E->value(); + + projs_decl += sformat(PROJECT_DECLARATION, name, name + ".csproj", procinfo.guid); + + for (int i = 0; i < procinfo.configs.size(); i++) { + const String &config = procinfo.configs[i]; + + if (i != 0) { + sln_platform_cfg += "\n"; + proj_platform_cfg += "\n"; + } + + sln_platform_cfg += sformat(SOLUTION_PLATFORMS_CONFIG, config); + proj_platform_cfg += sformat(PROJECT_PLATFORMS_CONFIG, procinfo.guid, config); + } + } + + String content = sformat(SOLUTION_TEMPLATE, projs_decl, sln_platform_cfg, proj_platform_cfg); + + FileAccessRef file = FileAccess::open(path_join(path, name + ".sln"), FileAccess::WRITE); + ERR_FAIL_COND_V(!file, ERR_FILE_CANT_WRITE); + file->store_string(content); + file->close(); + + return OK; +} + +bool NETSolution::set_path(const String &p_existing_path) { + if (p_existing_path.is_abs_path()) { + path = p_existing_path; + } else { + String abspath; + if (!rel_path_to_abs(p_existing_path, abspath)) + return false; + path = abspath; + } + + return true; +} + +NETSolution::NETSolution(const String &p_name) { + name = p_name; +} diff --git a/modules/mono/editor/net_solution.h b/modules/mono/editor/net_solution.h new file mode 100644 index 0000000000..d7ccebb7df --- /dev/null +++ b/modules/mono/editor/net_solution.h @@ -0,0 +1,57 @@ +/*************************************************************************/ +/* net_solution.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef NET_SOLUTION_H +#define NET_SOLUTION_H + +#include "map.h" +#include "ustring.h" + +struct NETSolution { + String name; + + void add_new_project(const String &p_name, const String &p_guid, const Vector<String> &p_extra_configs = Vector<String>()); + + Error save(); + + bool set_path(const String &p_existing_path); + + NETSolution(const String &p_name); + +private: + struct ProjectInfo { + String guid; + Vector<String> configs; + }; + + String path; + Map<String, ProjectInfo> projects; +}; + +#endif // NET_SOLUTION_H |