Skip to content

Commit

Permalink
Merge pull request #96335 from raulsntos/dotnet/export-plugin
Browse files Browse the repository at this point in the history
C#: Use export platform to determine the target OS and log errors
  • Loading branch information
akien-mga committed Aug 30, 2024
2 parents 6489013 + 4c14421 commit ace4529
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,6 @@ private static bool PublishProjectBlocking(BuildInfo buildInfo)
success = Publish(buildInfo);
}

if (!success)
{
ShowBuildErrorDialog("Failed to publish .NET project");
}

return success;
}

Expand Down
60 changes: 31 additions & 29 deletions modules/mono/editor/GodotTools/GodotTools/Export/ExportPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,19 @@ public override string[] _GetExportFeatures(EditorExportPlatform platform, bool
};
}

private string? _maybeLastExportError;
private void AddExceptionMessage(EditorExportPlatform platform, Exception exception)
{
string? exceptionMessage = exception.Message;
if (string.IsNullOrEmpty(exceptionMessage))
{
exceptionMessage = $"Exception thrown: {exception.GetType().Name}";
}

platform.AddMessage(EditorExportPlatform.ExportMessageType.Error, "Export .NET Project", exceptionMessage);

// We also print exceptions as we receive them to stderr.
Console.Error.WriteLine(exception);
}

// With this method we can override how a file is exported in the PCK
public override void _ExportFile(string path, string type, string[] features)
Expand All @@ -92,8 +104,8 @@ public override void _ExportFile(string path, string type, string[] features)

if (!ProjectContainsDotNet())
{
_maybeLastExportError = $"This project contains C# files but no solution file was found at the following path: {GodotSharpDirs.ProjectSlnPath}\n" +
"A solution file is required for projects with C# files. Please ensure that the solution file exists in the specified location and try again.";
GetExportPlatform().AddMessage(EditorExportPlatform.ExportMessageType.Error, "Export .NET Project", $"This project contains C# files but no solution file was found at the following path: {GodotSharpDirs.ProjectSlnPath}\n" +
"A solution file is required for projects with C# files. Please ensure that the solution file exists in the specified location and try again.");
throw new InvalidOperationException($"{path} is a C# file but no solution file exists.");
}

Expand Down Expand Up @@ -124,16 +136,7 @@ public override void _ExportBegin(string[] features, bool isDebug, string path,
}
catch (Exception e)
{
_maybeLastExportError = e.Message;

// 'maybeLastExportError' cannot be null or empty if there was an error, so we
// must consider the possibility of exceptions being thrown without a message.
if (string.IsNullOrEmpty(_maybeLastExportError))
_maybeLastExportError = $"Exception thrown: {e.GetType().Name}";

GD.PushError($"Failed to export project: {_maybeLastExportError}");
Console.Error.WriteLine(e);
// TODO: Do something on error once _ExportBegin supports failing.
AddExceptionMessage(GetExportPlatform(), e);
}
}

Expand All @@ -144,7 +147,9 @@ private void _ExportBeginImpl(string[] features, bool isDebug, string path, long
if (!ProjectContainsDotNet())
return;

if (!DeterminePlatformFromFeatures(features, out string? platform))
string osName = GetExportPlatform().GetOsName();

if (!TryDeterminePlatformFromOSName(osName, out string? platform))
throw new NotSupportedException("Target platform not supported.");

if (!new[] { OS.Platforms.Windows, OS.Platforms.LinuxBSD, OS.Platforms.MacOS, OS.Platforms.Android, OS.Platforms.iOS }
Expand Down Expand Up @@ -445,25 +450,22 @@ public override void _ExportEnd()
Directory.Delete(folder, recursive: true);
}
_tempFolders.Clear();

// TODO: The following is just a workaround until the export plugins can be made to abort with errors

// We check for empty as well, because it's set to empty after hot-reloading
if (!string.IsNullOrEmpty(_maybeLastExportError))
{
string lastExportError = _maybeLastExportError;
_maybeLastExportError = null;

GodotSharpEditor.Instance.ShowErrorDialog(lastExportError, "Failed to export C# project");
}
}

private static bool DeterminePlatformFromFeatures(IEnumerable<string> features, [NotNullWhen(true)] out string? platform)
/// <summary>
/// Tries to determine the platform from the export preset's platform OS name.
/// </summary>
/// <param name="osName">Name of the export operating system.</param>
/// <param name="platform">Platform name for the recognized supported platform.</param>
/// <returns>
/// <see langword="true"/> when the platform OS name is recognized as a supported platform,
/// <see langword="false"/> otherwise.
/// </returns>
private static bool TryDeterminePlatformFromOSName(string osName, [NotNullWhen(true)] out string? platform)
{
foreach (var feature in features)
if (OS.PlatformFeatureMap.TryGetValue(osName, out platform))
{
if (OS.PlatformFeatureMap.TryGetValue(feature, out platform))
return true;
return true;
}

platform = null;
Expand Down

0 comments on commit ace4529

Please sign in to comment.