コード例 #1
0
ファイル: Program.cs プロジェクト: RoyAwesome/lebot_dota2
        private static FunctionDB ParseScriptDump(string data)
        {
            FunctionDB parsedDB = new FunctionDB();
            //Parse the outputdata

            string[] Text = data.Split('\n');

            Parsing_E Parsing = Parsing_E.None;

            Function currentFunction = null;
            ConstantGroup currentConstants = null;

            string previousLine = "";

            foreach (string line in Text)
            {
                //remove all the [    Vscript   ]: text
                string l = line.Remove(0, line.IndexOf(':') + 1);
                l = l.Trim();

                //Three - means description, and the start of a new function definition
                if (l.StartsWith("---"))
                {
                    if(Parsing == Parsing_E.Enum)
                    {
                        //Commit the enum
                        //commit the changes
                        parsedDB.Constants.Add(currentConstants);

                        Console.WriteLine("Parsed " + currentConstants.EnumName);

                        currentConstants = null;

                        Parsing = Parsing_E.None;
                    }

                    if (Parsing != Parsing_E.None)
                    {
                        throw new Exception("Error: Found a new definition while already writing a function");
                    }

                    l = l.Replace("---", "").Trim();

                    if (l.StartsWith("[["))
                    {
                        currentFunction = new Function();

                        //Remove the comment lines
                        l = l.Replace("[[", "");
                        l = l.Replace(" ]]", "").Trim();

                        if (l.Contains(' ')) l = l.Substring(l.IndexOf(' ') + 1).Trim(); //If there is a space, that means we have a description.  Remove the function name
                                                                                         //from it and just use it
                        else l = ""; //If the above fails, the description only contains the function name.  Ignore it.

                        //Add this line as the current functions description
                        currentFunction.FunctionDescription = l;

                        Parsing = Parsing_E.Function;
                    }
                    else if(l.StartsWith("Enum"))
                    {
                        currentConstants = new ConstantGroup();

                        l = l.Replace("Enum", "").Trim();
                        currentConstants.EnumName = l;

                        Parsing = Parsing_E.Enum;
                    }

                }
                else if (Parsing == Parsing_E.Function && l.StartsWith("--"))
                {
                    if (currentFunction == null)
                    {
                        throw new Exception("Error: Parse a function when no function created");
                    }

                    //Possible cases
                    //@return type - Return value
                    //@param Name Type - Param name and type.  Can be multiple

                    l = l.Replace("--", "").Trim(); //Remove the comment stuff

                    if (l.StartsWith("@return"))
                    {
                        currentFunction.ReturnType = l.Replace("@return", "").Trim();
                    }
                    if (l.StartsWith("@param"))
                    {
                        string paramdat = l.Replace("@param", "").Trim();
                        string[] d = paramdat.Split(' ');
                        Param p = new Param();
                        p.Name = d[0];
                        p.Type = d[1];

                        currentFunction.Params.Add(p);
                    }

                }
                else if (Parsing == Parsing_E.Function && l.StartsWith("function"))
                {
                    if (currentFunction == null)
                    {
                        throw new Exception("Error: Trying to add on to a function without a function created");

                    }

                    //Pattern: function [ClassName:]FunctionName( param ...) end

                    //We want to pull out a class name and a function name name out of this

                    currentFunction.Example = l;

                    l = l.Replace("function", "").Trim();

                    if (l.Contains(":"))
                    {
                        //We have a classname
                        l = l.Substring(0, l.IndexOf("("));
                        var s = l.Split(':');
                        currentFunction.Class = s[0];
                        currentFunction.FunctionName = s[1];

                        //Check to see if the class exists
                        ClassType c = parsedDB.Classes.FirstOrDefault(x => x.ClassName == currentFunction.Class);
                        if (c == null)
                        {
                            //Create the class if it doesn't exist
                            c = new ClassType();
                            c.ClassName = currentFunction.Class;
                            parsedDB.Classes.Add(c);
                        }
                    }
                    else
                    {
                        l = l.Substring(0, l.IndexOf("("));
                        currentFunction.FunctionName = l;
                        currentFunction.Class = "Global";
                    }

                }
                else if(Parsing == Parsing_E.Enum && !string.IsNullOrEmpty(l)) //Modifer entry
                {
                    if(currentConstants == null)
                    {
                        throw new Exception("Tried to parse constant but group was null");
                    }

                    string[] Words = l.Split(' ');
                    ConstantEntry ce = new ConstantEntry()
                    {
                        Name = Words[0],
                        Value = Words[2]
                    };
                    if (l.Contains("--"))
                    {
                        string comment = l.Substring(l.IndexOf("--") + 2).Trim();
                        ce.Description = comment;
                    }

                    currentConstants.Entries.Add(ce);

                }
                else if (string.IsNullOrEmpty(l))
                {
                    if(previousLine == line && Parsing == Parsing_E.None)
                    {
                        continue; //Skip multiple blank lines
                    }
                    if (Parsing == Parsing_E.None)
                    {
                        throw new Exception("Tried to commit something when nothing was being worked on");
                    }

                    if(Parsing == Parsing_E.Function)
                    {
                        //Commit the changes
                        parsedDB.Functions.Add(currentFunction);

                        Console.WriteLine("Parsed " + currentFunction.GetQualifiedName());

                        currentFunction = null;
                    }
                    if(Parsing == Parsing_E.Enum)
                    {
                        //commit the changes
                        parsedDB.Constants.Add(currentConstants);

                        Console.WriteLine("Parsed " + currentConstants.EnumName);

                        currentConstants = null;
                    }

                    Parsing = Parsing_E.None;
                }

                previousLine = line;
            }

            return parsedDB;
        }
コード例 #2
0
ファイル: IRCBot.cs プロジェクト: RoyAwesome/lebot_dota2
        public string ModifyClass(IrcCommand command)
        {
            if (command.Parameters.Length == 0 || command.Parameters[0] == "help")
            {
                return "'class <string classname> <action> <property> [data]' OR 'class find <searchtext>' OR 'class properties'";
            }
            if (command.Parameters[0] == "properties")
            {
                PropertyInfo[] props = typeof(ClassType).GetProperties();
                string availableProperties = "";
                for (int i = 0; i < props.Length; i++)
                {
                    availableProperties += props[i].Name;
                    if (i != props.Length - 1) availableProperties += ", ";
                }

                return "properties: [" + availableProperties + "]";
            }

            FunctionDB database = JsonConvert.DeserializeObject<FunctionDB>(File.ReadAllText(Settings.DatabaseFilename));

            string ClassName = command.Parameters[0];

            if (ClassName == "find")
            {
                string searchText = command.Parameters[1];

                string[] results = database.Classes.Where(x => x.ClassName.Contains(searchText, StringComparison.OrdinalIgnoreCase)).Select(x => x.ClassName).ToArray();

                if (results.Length == 0) return "No Function Found";
                if (results.Length < 5)
                {
                    for (int i = 0; i < results.Length - 1; i++)
                    {
                        SendMessage(command.Destination, results[i]);

                    }
                    return results[results.Length - 1];
                }
                else
                {
                    return "Results: " + string.Join("\n", results).Haste();
                }
            }

            if (ClassName == "add")
            {
                if (!command.Parameters[1].StartsWith("http://hastebin.com/raw/")) return "Please give me a raw hastebin link with the json blob to add";

                string json = QuickDownload(command.Parameters[1]);
                ClassType f = JsonConvert.DeserializeObject<ClassType>(json);
                //f.LastUpdate = DateTime.Now;

                database.Classes.Add(f);
                database.Save(Settings.DatabaseFilename);
                return "Added " + f.ClassName;
            }
            if (ClassName == "<delete>")
            {
                ClassType c = database.Classes.FirstOrDefault(x => x.ClassName == command.Parameters[1]);
                if (c == null) return "Class not found";

                database.Classes.Remove(c);
                int funcs = database.Functions.RemoveAll(x => x.Class == c.ClassName);
                database.Save(Settings.DatabaseFilename);

                return "Removed " + c.ClassName + " and " + funcs + " functions";

            }
            if (ClassName == "<rebuild>")
            {
                if (!AccessCheck(command)) return "No Access";

                List<string> BrokenClasses = new List<string>();

                foreach (Function f in database.Functions)
                {
                    if (database.Classes.FirstOrDefault(x => x.ClassName == f.Class) == null)
                    {
                        BrokenClasses.Add(f.Class);
                        ClassType c = new ClassType();
                        c.ClassName = f.Class;
                        database.Classes.Add(c);
                    }
                }

                if (BrokenClasses.Count == 0) return "No Broken classes found";

                database.Save(Settings.DatabaseFilename);

                string bc = string.Join("\n", BrokenClasses);

                return "Found " + BrokenClasses.Count + " broken classes: " + bc.Haste();

            }

            ClassType clazz = database.Classes.FirstOrDefault(x => x.ClassName == ClassName);

            if (clazz == null) return "Class Not Found";

            string action = command.Parameters[1].ToLower();

            if (action == "modify" && !AccessCheck(command))
            {
                return "No Permision";
            }

            string property = command.Parameters[2].ToLower();

            string[] cpy = new string[command.Parameters.Length - 3];
            Array.Copy(command.Parameters, 3, cpy, 0, cpy.Length);

            string data = string.Join(" ", cpy);

            if (data.StartsWith("http://hastebin.com/raw/"))
            {
                data = QuickDownload(data);
            }

            PropertyInfo prop = typeof(ClassType).GetProperty(property, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);

            if (prop == null) return "Property " + property + " Not found";

            if (action == "modify") prop.SetValue(clazz, data);
            if (action == "view") return prop.GetValue(clazz).ToString();

            if (action == "modify") database.Save(Settings.DatabaseFilename);

            return "[" + action + "] " + ClassName + "'s " + property + " has been set";
        }