Skip to content

Commit

Permalink
Merge pull request #507 from CactusPuppy/475-workshopsettingcombo-and…
Browse files Browse the repository at this point in the history
…-workshopsettinghero-only-accept-string-literals-for-category-and-name-arguments

475 workshopsettingcombo and workshopsettinghero only accept string literals for category and name arguments
  • Loading branch information
CactusPuppy authored Oct 12, 2024
2 parents 2cb84e3 + 4870321 commit ba5f3d7
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -352,4 +372,4 @@ public override object Validate(ParseInfo parseInfo, IExpression value, DocRange
return GetFile(parseInfo, filepath, uri => new LoadedFile(uri)).GetContent();
}
}
}
}

0 comments on commit ba5f3d7

Please sign in to comment.