From 4870321482acd2fa76d32a613618975cdb124e89 Mon Sep 17 00:00:00 2001 From: Cactus Puppy Date: Sat, 12 Oct 2024 00:50:36 -0700 Subject: [PATCH] fix: allow expressions which resolve to a constant string to be considered valid for ConstStringParameter --- .../Parameters/AlternateParameterTypes.cs | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/Deltinteger/Deltinteger/Parse/Parameters/AlternateParameterTypes.cs b/Deltinteger/Deltinteger/Parse/Parameters/AlternateParameterTypes.cs index 71dc701ec..416c8407a 100644 --- a/Deltinteger/Deltinteger/Parse/Parameters/AlternateParameterTypes.cs +++ b/Deltinteger/Deltinteger/Parse/Parameters/AlternateParameterTypes.cs @@ -106,9 +106,23 @@ public ConstStringParameter(string name, string documentation, ITypeSupplier typ public override object Validate(ParseInfo parseInfo, IExpression value, DocRange valueRange, object additionalData) { - StringAction str = value as StringAction; - if (str == null && valueRange != null) parseInfo.Script.Diagnostics.Error("Expected string constant.", valueRange); - return str?.Value; + // ConstantExpressionResolver.Resolve's callback will be called after this function runs, + // so we store the value in an object reference whose value will be set later. + var promise = new ConstStringResolver(); + + // Resolve the expression. + ConstantExpressionResolver.Resolve(value, expr => + { + // If the resulting expression is a string, + if (expr is StringAction stringAction) + // Resolve the value. + promise.Resolve(stringAction.Value); + // Otherwise, add an error. + else if (valueRange != null) + parseInfo.Script.Diagnostics.Error("Expected string constant", valueRange); + }); + + return promise?.Value; } public override IWorkshopTree Parse(ActionSet actionSet, IExpression expression, object additionalParameterData) => null; @@ -142,6 +156,12 @@ public override object Validate(ParseInfo parseInfo, IExpression value, DocRange public override IWorkshopTree Parse(ActionSet actionSet, IExpression expression, object additionalParameterData) => null; } + class ConstStringResolver + { + public string Value { get; private set; } + public void Resolve(string value) => Value = value; + } + class ConstHeroValueResolver { public string Hero { get; private set; } @@ -352,4 +372,4 @@ public override object Validate(ParseInfo parseInfo, IExpression value, DocRange return GetFile(parseInfo, filepath, uri => new LoadedFile(uri)).GetContent(); } } -} \ No newline at end of file +}