object ConvertToTemplate(Template.OptionType type, string value) { try { switch (type) { case Template.OptionType.Int: return(Convert.ToInt32(value)); case Template.OptionType.Bool: return(Convert.ToBoolean(value)); case Template.OptionType.Double: return(Convert.ToDouble(value)); case Template.OptionType.Enum: return(Enum.Parse(typeof(string), value, true)); case Template.OptionType.StringArray: return(ConvertToArray(ArrayType.String, value)); case Template.OptionType.IntArray: return(ConvertToArray(ArrayType.Int, value)); case Template.OptionType.DoubleArray: return(ConvertToArray(ArrayType.Double, value)); case Template.OptionType.Point: return(ConvertToArray(ArrayType.Point, value)); case Template.OptionType.RGB: return(ConvertToArray(ArrayType.RGB, value)); } } //return string if error occured at conversion catch { } return(value); }
public PropertyOption(List <EditorIniEntry> options, Template.Option templateOption, bool children) { this.Name = templateOption.Name; this.Category = templateOption.Category; this.Description = templateOption.Description; this.Type = templateOption.Type; if (templateOption.Multiple) { this.Attributes = new Attribute[] { new EditorAttribute(typeof(UITypeEditor), typeof(UITypeEditor)), new TypeConverterAttribute(typeof(PropertyOptionCollectionConverter)) }; this.Value = new PropertySubOptions(templateOption.Name, options, children); } else { this.Value = options.Count > 0 ? options[0].Value : string.Empty; } }
/// <summary> /// A basic function to check whether a value is of the right type as indicated by Template.xml. Defaults to a string. /// </summary> /// <param name="val">The value casted to a string</param> /// <param name="type">The thing we want to see if it can parse as</param> /// <returns>True if it is in the right format, false otherwise.</returns> private bool CanCast(string val, Template.OptionType type) { val = val.Replace(" ", string.Empty); if (val.Length == 0) // Allow people to delete values { return(true); } switch (type) { // Freelancer is a bit weird. 0, 1, true, and false are all allowed for Bool. case Template.OptionType.Bool: return(val == "true" || val == "false" || val == "0" || val == "1"); // Default case case Template.OptionType.String: return(true); // We parse long because sometimes it would be uint and other times int. I couldn't be arsed writing a case for both. case Template.OptionType.Int: return(long.TryParse(val, out long i)); // Points in Freelancer are almost always floats, rather than ints. case Template.OptionType.Point: if (val.Count(s => s == ',') != 1) { return(false); } string[] point = val.Split(','); return(float.TryParse(point[0], out float f) && float.TryParse(point[1], out float ff)); case Template.OptionType.Double: return(double.TryParse(val, out double d)); case Template.OptionType.Path: return(!(val.Contains(Path.GetInvalidFileNameChars().ToString()) || val.Contains(Path.GetInvalidPathChars().ToString()))); case Template.OptionType.Vector: if (val.Count(s => s == ',') != 2) { return(false); } string[] vec = val.Split(','); return(double.TryParse(vec[0], out double dd) && double.TryParse(vec[1], out double ddd) && double.TryParse(vec[2], out double dddd)); case Template.OptionType.Rgb: if (val.Count(s => s == ',') != 2) { return(false); } string[] rgb = val.Split(','); return(byte.TryParse(rgb[0], out byte b) && byte.TryParse(rgb[1], out byte bb) && byte.TryParse(rgb[2], out byte bbb)); case Template.OptionType.StringArray: return(val.Contains(',')); case Template.OptionType.IntArray: if (!val.Contains(',')) { return(false); } var intArr = val.Split(','); foreach (var intVal in intArr) { if (!long.TryParse(intVal, out long iVal)) { return(false); } } return(true); case Template.OptionType.DoubleArray: if (!val.Contains(',')) { return(false); } var douArr = val.Split(','); foreach (var douVal in douArr) { if (!double.TryParse(douVal, out double dVal)) { return(false); } } return(true); default: return(false); } }