From 1364266bf81a95808ad52326a13028c58435e0a6 Mon Sep 17 00:00:00 2001 From: Tim Nugent Date: Fri, 21 Feb 2025 10:36:58 +1100 Subject: [PATCH] added type unwrapped TryGetProperty calls to markup. This allows you directly get the value of properties without have to bounce it through a markup value type first. --- CHANGELOG.md | 1 + .../YarnSpinner.Markup/MarkupParseResult.cs | 176 ++++++++++++++++-- 2 files changed, 164 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67ef34e75..9c0a8ea3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Language Server: Nodes that are part of a node group now show their condition complexity as a code lens. - Added a new method, `Dialogue.HasSalientContent(nodegroup)`, which returns a bool if there is any salient content for the requested nodegroup. - Added `min(a,b)` and `max(a,b)` to the standard library. +- Added unwrapped `TryGetProperty` calls to markup, allowing direct access to the type if you know it ahead of time. ### Changed diff --git a/YarnSpinner/YarnSpinner.Markup/MarkupParseResult.cs b/YarnSpinner/YarnSpinner.Markup/MarkupParseResult.cs index 451c3225b..39357ce64 100644 --- a/YarnSpinner/YarnSpinner.Markup/MarkupParseResult.cs +++ b/YarnSpinner/YarnSpinner.Markup/MarkupParseResult.cs @@ -434,7 +434,10 @@ public override string ToString() return sb.ToString(); } - public bool TryGetProperty(string name, out MarkupValue result) + // these methods and the ones for MarkupAttributeMarker are identical + // don't have time for v3 release but later we should merge these two types + // they are too similar to be different + private bool TryGetPropertyInternal(string name, out MarkupValue result) { foreach (var prop in this.Properties) { @@ -445,6 +448,86 @@ public bool TryGetProperty(string name, out MarkupValue result) } } + result = default; + return false; + } + public bool TryGetProperty(string name, out MarkupValue result) + { + var worked = TryGetPropertyInternal(name, out var value); + + result = value; + return worked; + } + public bool TryGetProperty(string name, out float result) + { + if (TryGetPropertyInternal(name, out var property)) + { + switch (property.Type) + { + case MarkupValueType.Float: + { + result = property.FloatValue; + return true; + } + case MarkupValueType.Integer: + { + result = property.IntegerValue; + return true; + } + } + } + + result = default; + return false; + } + public bool TryGetProperty(string name, out int result) + { + if (TryGetPropertyInternal(name, out var property)) + { + switch (property.Type) + { + case MarkupValueType.Integer: + { + result = property.IntegerValue; + return true; + } + } + } + + result = default; + return false; + } + public bool TryGetProperty(string name, out string? result) + { + if (TryGetPropertyInternal(name, out var property)) + { + switch (property.Type) + { + case MarkupValueType.String: + { + result = property.StringValue; + return true; + } + } + } + + result = default; + return false; + } + public bool TryGetProperty(string name, out bool result) + { + if (TryGetPropertyInternal(name, out var property)) + { + switch (property.Type) + { + case MarkupValueType.Bool: + { + result = property.BoolValue; + return true; + } + } + } + result = default; return false; } @@ -656,6 +739,20 @@ internal MarkupAttributeMarker(string name, int position, int sourcePosition, Li /// internal int SourcePosition { get; set; } + private bool TryGetPropertyInternal(string name, out MarkupValue result) + { + foreach (var prop in this.Properties) + { + if (prop.Name.Equals(name, System.StringComparison.OrdinalIgnoreCase)) + { + result = prop.Value; + return true; + } + } + + result = default; + return false; + } /// /// Gets the property associated with the specified key, if /// present. @@ -682,25 +779,78 @@ public bool TryGetProperty(string name, out MarkupValue result) result = default; return false; } - - public override bool Equals(object obj) + public bool TryGetProperty(string name, out float result) { - throw new NotImplementedException(); - } + if (TryGetPropertyInternal(name, out var property)) + { + switch (property.Type) + { + case MarkupValueType.Float: + { + result = property.FloatValue; + return true; + } + case MarkupValueType.Integer: + { + result = property.IntegerValue; + return true; + } + } + } - public override int GetHashCode() - { - throw new NotImplementedException(); + result = default; + return false; } + public bool TryGetProperty(string name, out int result) + { + if (TryGetPropertyInternal(name, out var property)) + { + switch (property.Type) + { + case MarkupValueType.Integer: + { + result = property.IntegerValue; + return true; + } + } + } - public static bool operator ==(MarkupAttributeMarker left, MarkupAttributeMarker right) + result = default; + return false; + } + public bool TryGetProperty(string name, out string? result) { - return left.Equals(right); + if (TryGetPropertyInternal(name, out var property)) + { + switch (property.Type) + { + case MarkupValueType.String: + { + result = property.StringValue; + return true; + } + } + } + + result = default; + return false; } - - public static bool operator !=(MarkupAttributeMarker left, MarkupAttributeMarker right) + public bool TryGetProperty(string name, out bool result) { - return !(left == right); + if (TryGetPropertyInternal(name, out var property)) + { + switch (property.Type) + { + case MarkupValueType.Bool: + { + result = property.BoolValue; + return true; + } + } + } + + result = default; + return false; } } }