Skip to content

Commit

Permalink
Merge pull request #43 from EvoEsports/named-slots
Browse files Browse the repository at this point in the history
Named slots
  • Loading branch information
araszka authored Dec 10, 2023
2 parents 054846c + 2d0daa5 commit 94bf01a
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 84 deletions.
34 changes: 34 additions & 0 deletions src/ManiaTemplates/Components/MtComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class MtComponent
public required Dictionary<string, MtComponentProperty> Properties { get; init; }
public required List<string> Namespaces { get; init; }
public required List<MtComponentScript> Scripts { get; init; }
public HashSet<string> Slots { get; init; } = new();

/// <summary>
/// Creates a MtComponent instance from template contents.
Expand All @@ -24,6 +25,7 @@ public static MtComponent FromTemplate(ManiaTemplateEngine engine, string templa
var namespaces = new List<string>();
var foundProperties = new Dictionary<string, MtComponentProperty>();
var maniaScripts = new List<MtComponentScript>();
var slots = new HashSet<string>();
var componentTemplate = "";
var hasSlot = false;
string? layer = null;
Expand Down Expand Up @@ -57,6 +59,7 @@ public static MtComponent FromTemplate(ManiaTemplateEngine engine, string templa
componentTemplate = node.InnerXml;
layer = ParseDisplayLayer(node);
hasSlot = NodeHasSlot(node);
slots = GetSlotNamesInTemplate(node);
break;

case "script":
Expand All @@ -70,6 +73,7 @@ public static MtComponent FromTemplate(ManiaTemplateEngine engine, string templa
{
TemplateContent = componentTemplate,
HasSlot = hasSlot,
Slots = slots,
ImportedComponents = foundComponents,
Properties = foundProperties,
Namespaces = namespaces,
Expand Down Expand Up @@ -222,6 +226,36 @@ private static bool NodeHasSlot(XmlNode node)
.Any(NodeHasSlot);
}

/// <summary>
/// Gets all slot names recursively.
/// </summary>
private static HashSet<string> GetSlotNamesInTemplate(XmlNode node)
{
var slotNames = new HashSet<string>();

if (node.Name == "slot")
{
slotNames.Add(node.Attributes?["name"]?.Value.ToLower() ?? "default");
}
else if (node.HasChildNodes)
{
foreach (XmlNode childNode in node.ChildNodes)
{
foreach (var slotName in GetSlotNamesInTemplate(childNode))
{
if (slotNames.Contains(slotName))
{
throw new DuplicateSlotException($"""A slot with the name "{slotName}" already exists.""");
}

slotNames.Add(slotName);
}
}
}

return slotNames;
}

public string Id()
{
return "MtContext" + GetHashCode().ToString().Replace("-", "N");
Expand Down
4 changes: 3 additions & 1 deletion src/ManiaTemplates/Components/MtComponentSlot.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using ManiaTemplates.ControlElements;
using ManiaTemplates.Lib;

namespace ManiaTemplates.Components;

public class MtComponentSlot
{
public required int Scope { get; init; }
public required string RenderMethod { get; init; }
public required string RenderMethodT4 { get; init; }
public required MtDataContext Context { get; init; }
public string Name { get; init; } = "default";
}
18 changes: 18 additions & 0 deletions src/ManiaTemplates/Exceptions/DuplicateSlotException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace ManiaTemplates.Exceptions;

public class DuplicateSlotException : Exception
{
public DuplicateSlotException()
{
}

public DuplicateSlotException(string message)
: base(message)
{
}

public DuplicateSlotException(string message, Exception inner)
: base(message, inner)
{
}
}
Loading

0 comments on commit 94bf01a

Please sign in to comment.