示例#1
0
        private static void AddCallbacks()
        {
            PropertyCallbacks = new Dictionary <string, PropertyProcessCallback>();

            PropertyProcessCallback NumericalCallback = (str, scope) => {
                if (!IsNumeric(str))
                {
                    throw new ParserException("Property value must be numerical");
                }

                return(new string[] { str });
            };

            PropertyCallbacks.Add("int", NumericalCallback);
            PropertyCallbacks.Add("double", NumericalCallback);
            PropertyCallbacks.Add("float", NumericalCallback);

            PropertyCallbacks.Add("string", (str, scope) => {
                bool isString = (str.StartsWith("\"") && str.EndsWith("\""));

                if (!isString)
                {
                    throw new ParserException("Property value must be a string");
                }

                return(new string[] { str });
            });

            PropertyCallbacks.Add("bool", (str, scope) =>
            {
                bool isBool = (str == "true" || str == "false");

                if (!isBool)
                {
                    throw new ParserException("Property value must be a bool");
                }

                return(new string[] { str });
            });

            PropertyCallbacks.Add("UDim2", (str, scope) => {
                string[] vals = str.Split(',');

                ValidateStringArray(ref vals, scope, 4);

                return(vals);
            });

            PropertyCallbacks.Add("Color3", (str, scope) => {
                if (str.StartsWith("#"))
                {
                    try
                    {
                        Color col = ColorTranslator.FromHtml(str);

                        return(new string[] { col.R.ToString(), col.G.ToString(), col.B.ToString() });
                    }
                    catch (Exception)
                    {
                        throw new ParserException("Failed to convert to Color3");
                    }
                }
                else if (str.Contains("-"))
                {
                    if (!RobloxEnum.Enums["Color"].HasEnumItem(str))
                    {
                        throw new ParserException($"No inbuilt color {str}");
                    }

                    return(new string[] { JSONWriter.Quotify($"inbuilt-Color:{str}") });
                }
                else
                {
                    var vals = str.Split(',');

                    ValidateStringArray(ref vals, scope, 3);

                    return(vals);
                }
            });

            PropertyCallbacks.Add("Proportion", (str, scope) => {
                if (!(str.EndsWith("%") || str.EndsWith("px")))
                {
                    throw new ParserException("Failed to convert to proportion, must end in either a '%' sign or 'px'");
                }

                return(new string[] { JSONWriter.Quotify(str) });
            });

            PropertyCallbacks.Add("Style", (str, scope) => {
                RSSParser Current = RSSParser.CurrentParser;

                if (Current.Styles == null)
                {
                    throw new ParserException($"No Style called {str}");
                }

                foreach (var style in Current.Styles)
                {
                    if (style.styleName == str)
                    {
                        return new string[] { JSONWriter.Quotify(str) }
                    }
                }
                ;

                throw new ParserException($"No Style called {str}");
            });
        }