Add core content definitions
This commit is contained in:
80
godot/scripts/debug/ContentBrowserController.cs
Normal file
80
godot/scripts/debug/ContentBrowserController.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Godot;
|
||||
using SideScrollerGame.Content;
|
||||
using SideScrollerGame.Content.Samples;
|
||||
using SideScrollerGame.Content.Validation;
|
||||
|
||||
namespace SideScrollerGame.Debug;
|
||||
|
||||
public partial class ContentBrowserController : Control
|
||||
{
|
||||
public override void _Ready()
|
||||
{
|
||||
ContentRegistry registry = SampleContent.CreateRegistry();
|
||||
ContentValidationResult validation = new ContentValidator().Validate(registry);
|
||||
string report = BuildReport(registry, validation);
|
||||
SetText(report);
|
||||
|
||||
if (!ShouldValidateAndQuit())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GD.Print(report);
|
||||
GD.Print(validation.HasErrors ? "Content validation failed" : "Content validation succeeded");
|
||||
GetTree().Quit(validation.HasErrors ? 1 : 0);
|
||||
}
|
||||
|
||||
private static string BuildReport(ContentRegistry registry, ContentValidationResult validation)
|
||||
{
|
||||
StringBuilder builder = new();
|
||||
builder.AppendLine("Loaded content definitions:");
|
||||
foreach (string id in registry.AllDefinitionIds())
|
||||
{
|
||||
builder.AppendLine(id);
|
||||
}
|
||||
|
||||
if (validation.Messages.Count > 0)
|
||||
{
|
||||
builder.AppendLine();
|
||||
builder.AppendLine("Validation messages:");
|
||||
foreach (ContentValidationMessage message in validation.Messages)
|
||||
{
|
||||
builder.AppendLine($"{message.Severity}: {message.Code}: {message.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return builder.ToString().TrimEnd();
|
||||
}
|
||||
|
||||
private static bool ShouldValidateAndQuit()
|
||||
{
|
||||
return IsHeadless() && OS.GetCmdlineUserArgs().Any(argument => argument.Equals(ValidateOnlyArgument, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private static bool IsHeadless()
|
||||
{
|
||||
return DisplayServer.GetName().Equals("headless", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private void SetText(string report)
|
||||
{
|
||||
Label? title = GetNodeOrNull<Label>("Title");
|
||||
if (title is not null)
|
||||
{
|
||||
title.Text = "Content Browser";
|
||||
}
|
||||
|
||||
Label? content = GetNodeOrNull<Label>("Scroll/Content");
|
||||
if (content is not null)
|
||||
{
|
||||
content.Text = report;
|
||||
}
|
||||
}
|
||||
|
||||
private const string ValidateOnlyArgument = "--content-validate-only";
|
||||
}
|
||||
Reference in New Issue
Block a user