Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add layer support #37

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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