// Returns a string representation of a value command parameter. public static string ToString(CommandParam param) { string result = ""; if (param.Type == CommandParamType.Array) { result += "("; CommandParam child = param.Children; while (child != null) { result += ToString(child); if (child.NextParam != null) result += ", "; child = child.NextParam; } result += ")"; } else { if (param.Type == CommandParamType.String || param.Type == CommandParamType.Any) result += "\"" + param.StringValue + "\""; else if (param.Type == CommandParamType.Integer) result += param.IntValue; else if (param.Type == CommandParamType.Float) result += param.FloatValue; else if (param.Type == CommandParamType.Boolean) result += (param.BoolValue ? "true" : "false"); } return result; }
public CommandParam(CommandParamType type) { this.name = ""; this.parent = null; this.children = null; this.nextParam = null; this.type = type; this.count = 0; this.value = null; this.charIndex = -1; this.lineIndex = -1; }
public CommandReferenceParam(CommandParamType type) { this.type = type; this.nextParam = null; this.parent = null; this.children = null; this.defaultValue = null; this.value = null; this.name = ""; if (type == CommandParamType.Array) value = (int) 0; }
// Returns true if the given user passed-in parameters matches this // command's format, outputting new parameters specified in that format. public bool HasParameters(CommandParam userParameters, out CommandParam newParameters) { if (parameterOverloads.Count == 0) { newParameters = new CommandParam(userParameters); return true; } for (int i = 0; i < parameterOverloads.Count; i++) { if (AreParametersMatching(parameterOverloads[i], userParameters, out newParameters)) return true; } newParameters = null; return false; }
public CommandParam(CommandParam copy) { this.name = copy.name; this.type = copy.Type; this.parent = null; this.nextParam = null; this.value = copy.value; this.stringValue = copy.stringValue; this.charIndex = copy.charIndex; this.lineIndex = copy.lineIndex; // Copy array children. this.count = 0; if (type == CommandParamType.Array) { foreach (CommandParam copyChild in copy.GetChildren()) AddChild(new CommandParam(copyChild)); } }
private object ParsePropertyValue(CommandParam param, PropertyType type) { if (type == PropertyType.String) { if (param.IsValidType(CommandParamType.String)) return param.StringValue; } else if (type == PropertyType.Integer) { if (param.IsValidType(CommandParamType.Integer)) return param.IntValue; } else if (type == PropertyType.Float) { if (param.IsValidType(CommandParamType.Float)) return param.FloatValue; } else if (type == PropertyType.Boolean) { if (param.IsValidType(CommandParamType.Boolean)) return param.BoolValue; } else if (type == PropertyType.List) ThrowParseError("Lists are unsupported as a property type"); ThrowParseError("The property value '" + param.StringValue + "' is not of type " + type.ToString()); return null; }
private void ParseProperty(CommandParam param) { string name = param.GetString(1); // Parse the property type and value. PropertyType type; if (!Enum.TryParse<PropertyType>(param.GetString(0), true, out type)) ThrowParseError("Unknown property type " + name); object value = ParsePropertyValue(param[2], type); // Set the property. Property property = baseTileData.Properties.SetGeneric(name, value); // Set the property's documentation. if (param.ChildCount > 3 && property != null) { string editorType = ""; string editorSubType = ""; if (param[4].Type == CommandParamType.Array) { editorType = param[4].GetString(0); editorSubType = param[4].GetString(1); } else { editorType = param.GetString(4); } property.Documentation = new PropertyDocumentation() { ReadableName = param.GetString(3), EditorType = editorType, EditorSubType = editorSubType, Category = param.GetString(5), Description = param.GetString(6), IsEditable = true, IsHidden = param.GetBool(7, false), }; } }
//----------------------------------------------------------------------------- // Script Commands //----------------------------------------------------------------------------- private void CommandProperties(CommandParam parameters) { foreach (CommandParam child in parameters.GetChildren()) ParseProperty(child); }
private static void CompleteToken(ParseData data) { bool isVariadic = false; if (data.Token.EndsWith("...")) { data.Token = data.Token.Substring(0, data.Token.Length - 3); isVariadic = true; } if (data.Token.Length > 0 || isVariadic) { if (data.Token.Length > 0) data.Tokens.Add(data.Token); if (data.IsParsingValue && data.Token.Length > 0) { CommandParam p; if (data.IsParsingDefaultValue) { CommandReferenceParam refParam = data.RefParamStack .Peek().GetChildren().ElementAt(data.ValParamStack.Peek().ChildCount); p = new CommandParam(refParam); p.SetValueByParse(data.Tokens[0]); } else { p = new CommandParam(data.Tokens[0]); } data.ValParamStack.Peek().AddChild(p); data.Tokens.Clear(); } if (!data.IsParsingValue) { if (data.Token.Length > 0 && data.Tokens.Count == 2) { data.RefParamStack.Peek().AddChild(new CommandReferenceParam() { Type = ParseCommandParamType(data.Tokens[0]), Name = data.Tokens[1], IsVariadic = isVariadic }); data.Tokens.Clear(); } else if (isVariadic) { data.RefParamStack.Peek().GetChildren().Last().IsVariadic = true; } } data.Token = ""; } }
// Add a parameter child. public CommandParam AddChild(CommandParam child) { if (type == CommandParamType.Array) { if (Children == null) { Children = child; } else { CommandParam lastChild = Children; while (lastChild.NextParam != null) lastChild = lastChild.NextParam; lastChild.NextParam = child; } count++; } return child; }
//----------------------------------------------------------------------------- // Constructor //----------------------------------------------------------------------------- // Recursively check whether the parameters are matching, outputting the new parameters. private static bool AreParametersMatching(CommandReferenceParam reference, CommandParam userParams, out CommandParam newParameters) { newParameters = null; if (reference == null) { newParameters = new CommandParam(userParams); return true; } if (!userParams.IsValidType(reference.Type)) { newParameters = null; return false; } // Make sure arrays are matching. if (reference.Type == CommandParamType.Array) { newParameters = new CommandParam(CommandParamType.Array); newParameters.Name = reference.Name; // Find the child index of the first parameter with a default value. int defaultIndex = 0; for (CommandReferenceParam p = reference.Children; p != null; p = p.NextParam) { if (p.DefaultValue != null) break; defaultIndex++; } // Check if the reference is variadic on the last parameter. CommandReferenceParam lastRefChild = reference.GetChildren().LastOrDefault(); bool isVariadic = false; if (lastRefChild != null && lastRefChild.IsVariadic) isVariadic = true; // Verify the user parameter's child count is within the valid range. if (userParams.ChildCount < defaultIndex || (userParams.ChildCount > reference.ChildCount && !isVariadic)) { newParameters = null; return false; } // Verify each child paremeter matches the reference. CommandReferenceParam referenceChild = reference.Children; CommandParam userChild = userParams.Children; int count = reference.ChildCount; if (isVariadic) count = Math.Max(count, userParams.ChildCount); for (int i = 0; i < count; i++) { CommandParam newChild; if (i < userParams.ChildCount) { if (!AreParametersMatching(referenceChild, userChild, out newChild)) { newParameters = null; return false; } userChild = userChild.NextParam; } else { newChild = new CommandParam(referenceChild.DefaultValue); } newParameters.AddChild(newChild); if (referenceChild.NextParam != null) referenceChild = referenceChild.NextParam; } } else { if (userParams.Type == CommandParamType.Array) { newParameters = new CommandParam(CommandParamType.Array); foreach (CommandParam userChild in userParams.GetChildren()) newParameters.AddChild(new CommandParam(userChild)); } else { newParameters = new CommandParam(reference); newParameters.SetValueByParse(userParams.StringValue); } } return true; }