Skip to content

Commit

Permalink
Add layer support
Browse files Browse the repository at this point in the history
- Usage: Set layer="" on template tag inside component
  • Loading branch information
araszka committed Aug 10, 2023
1 parent 8436a99 commit 4a1827f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
18 changes: 16 additions & 2 deletions src/ManiaTemplates/Components/MtComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class MtComponent
{
public required string TemplateContent { get; init; }
public required bool HasSlot { get; init; }
public string? DisplayLayer { get; init; }
public required MtComponentMap ImportedComponents { get; init; }
public required Dictionary<string, MtComponentProperty> Properties { get; init; }
public required List<string> Namespaces { get; init; }
Expand All @@ -25,6 +26,7 @@ public static MtComponent FromTemplate(ManiaTemplateEngine engine, string templa
var maniaScripts = new List<MtComponentScript>();
var componentTemplate = "";
var hasSlot = false;
string? layer = null;
var rootNode = FindComponentNode(templateContent);

foreach (XmlNode node in rootNode.ChildNodes)
Expand Down Expand Up @@ -53,6 +55,7 @@ public static MtComponent FromTemplate(ManiaTemplateEngine engine, string templa

case "template":
componentTemplate = node.InnerXml;
layer = ParseDisplayLayer(node);
hasSlot = NodeHasSlot(node);
break;

Expand All @@ -70,7 +73,8 @@ public static MtComponent FromTemplate(ManiaTemplateEngine engine, string templa
ImportedComponents = foundComponents,
Properties = foundProperties,
Namespaces = namespaces,
Scripts = maniaScripts
Scripts = maniaScripts,
DisplayLayer = layer
};
}

Expand Down Expand Up @@ -116,7 +120,8 @@ private static MtComponentImport ParseImportNode(ManiaTemplateEngine engine, Xml

if (resource == null)
{
throw new MissingAttributeException($"Missing required attribute 'component' for element '{node.OuterXml}'.");
throw new MissingAttributeException(
$"Missing required attribute 'component' for element '{node.OuterXml}'.");
}

tag ??= resource.Split('.')[^2];
Expand All @@ -128,6 +133,15 @@ private static MtComponentImport ParseImportNode(ManiaTemplateEngine engine, Xml
};
}

/// <summary>
/// Gets the layer value of a template node in a component file.
/// </summary>
private static string? ParseDisplayLayer(XmlNode node)
{
return (from XmlAttribute attribute in node.Attributes! where attribute.Name == "layer" select attribute.Value)
.FirstOrDefault();
}

/// <summary>
/// Get the namespace of a XML-node, or throw an exception if none present.
/// </summary>
Expand Down
26 changes: 9 additions & 17 deletions src/ManiaTemplates/Lib/MtTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public string BuildManialink(MtComponent rootComponent, string className, int ve
_maniaTemplateLanguage.Context(@"template language=""C#"""), //Might not be needed
_maniaTemplateLanguage.Context(@"import namespace=""System.Collections.Generic"""),
CreateImportStatements(),
ManiaLinkStart(className, version),
ManiaLinkStart(className, version, rootComponent.DisplayLayer),
"<#",
"RenderBody(() => DoNothing());",
"#>",
Expand Down Expand Up @@ -609,20 +609,6 @@ private string ExtractManiaScriptDirectives(string maniaScriptSource)
structMatcher = structMatcher.NextMatch();
}

// var globalVariableMatcher = ManiaScriptGlobalVariableRegex.Match(output);
// while (globalVariableMatcher.Success)
// {
// var match = globalVariableMatcher.ToString();
// if (!_maniaScriptIncludes.Contains(match))
// {
// _maniaScriptIncludes.Add(match);
// }
//
// output = output.Replace(match, "");
//
// globalVariableMatcher = globalVariableMatcher.NextMatch();
// }

return output;
}

Expand Down Expand Up @@ -928,9 +914,15 @@ private static string WrapStringInQuotes(string str)
/// name = Shown in in-game debugger.
/// version = Version for the markup language of Trackmania.
/// </summary>
private static string ManiaLinkStart(string name, int version = 3)
private static string ManiaLinkStart(string name, int version = 3, string? displayLayer = null)
{
return $@"<manialink version=""{version}"" id=""{name}"" name=""EvoSC#-{name}"">";
var layer = "";
if (displayLayer != null)
{
layer += $@" layer=""{displayLayer}""";
}

return $@"<manialink version=""{version}"" id=""{name}"" name=""EvoSC#-{name}""{layer}>";
}

/// <summary>
Expand Down

0 comments on commit 4a1827f

Please sign in to comment.