Skip to content

Commit

Permalink
Add C# build on focus
Browse files Browse the repository at this point in the history
This commit adds a new `build_on_focus` setting that, when enabled, allows GodotTools to automatically build the C# project when any .cs files have been changed by an external IDE after refocusing on the Godot editor.
  • Loading branch information
RobProductions committed Mar 5, 2025
1 parent 937fccf commit 999463c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
23 changes: 23 additions & 0 deletions modules/mono/editor/GodotTools/GodotTools/Build/BuildManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,29 @@ public static bool EditorBuildCallback()
return BuildProjectBlocking("Debug");
}

public static bool IsBuildInProgress()
{
return _buildInProgress != null;
}

public static bool CSProjectFilesChanged()
{
if (!File.Exists(GodotSharpDirs.ProjectCsProjPath))
return false; // No CS project available to check.

if (File.GetLastWriteTime(GodotSharpDirs.ProjectCsProjPath) > LastValidBuildDateTime)
return true; // Project file has been updated.

string projectPath = ProjectSettings.GlobalizePath("res://");
foreach (string filePath in Directory.EnumerateFiles(projectPath, "*.cs", SearchOption.AllDirectories))
{
if (File.GetLastWriteTime(filePath) > LastValidBuildDateTime)
return true;
}

return false;
}

public static void Initialize()
{
}
Expand Down
24 changes: 24 additions & 0 deletions modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public partial class GodotSharpEditor : EditorPlugin, ISerializationListener
public static class Settings
{
public const string ExternalEditor = "dotnet/editor/external_editor";
public const string BuildOnFocus = "dotnet/editor/build_on_focus";
public const string CustomExecPath = "dotnet/editor/custom_exec_path";
public const string CustomExecPathArgs = "dotnet/editor/custom_exec_path_args";
public const string VerbosityLevel = "dotnet/build/verbosity_level";
Expand Down Expand Up @@ -461,6 +462,28 @@ private void BuildStateChanged()
_bottomPanelBtn.Icon = MSBuildPanel.GetBuildStateIcon();
}

public void HotReloadScripts(bool windowFocused)
{
if (Internal.IsAssembliesReloadingNeeded())
{
BuildManager.UpdateLastValidBuildDateTime();
Internal.ReloadAssemblies(softReload: false);
}

if (windowFocused && _editorSettings.GetSetting(Settings.BuildOnFocus).As<bool>())
{
if (!BuildManager.IsBuildInProgress())
{
if (!File.Exists(GodotSharpDirs.ProjectCsProjPath) || BuildManager.CSProjectFilesChanged())
{
// Build scripts if CS Proj needs to be created
// or if any CS script files have changed since last build.
BuildProjectPressed();
}
}
}
}

public override void _EnablePlugin()
{
base._EnablePlugin();
Expand Down Expand Up @@ -554,6 +577,7 @@ public override void _EnablePlugin()

// External editor settings
EditorDef(Settings.ExternalEditor, Variant.From(ExternalEditorId.None));
EditorDef(Settings.BuildOnFocus, false);
EditorDef(Settings.CustomExecPath, "");
EditorDef(Settings.CustomExecPathArgs, "");
EditorDef(Settings.VerbosityLevel, Variant.From(VerbosityLevelId.Normal));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@ public override void _Notification(int what)
{
RestartTimer();

if (Internal.IsAssembliesReloadingNeeded())
{
BuildManager.UpdateLastValidBuildDateTime();
Internal.ReloadAssemblies(softReload: false);
}
GodotSharpEditor.Instance.HotReloadScripts(true);
}
}

private void TimerTimeout()
{
if (Internal.IsAssembliesReloadingNeeded())
{
BuildManager.UpdateLastValidBuildDateTime();
Internal.ReloadAssemblies(softReload: false);
}
GodotSharpEditor.Instance.HotReloadScripts(false);
}

[UsedImplicitly]
Expand Down

0 comments on commit 999463c

Please sign in to comment.