Example #1
0
        public void LoadAPI(object apiObject)
        {
            if (LuaFunctions == null)
                return;

            MethodInfo[] mi = apiObject.GetType().GetMethods();
            for (int i = 0; i < mi.Length; i++)
            {
                foreach (Attribute attr in Attribute.GetCustomAttributes(mi[i]))
                {
                    if (attr.GetType() == typeof(LuaFunction))
                    {
                        LuaFunction a = (LuaFunction)attr;
                        List<string> arguments = new List<string>();
                        List<string> argDocs = new List<string>();

                        string name = a.Name;
                        string desc = a.Description;
                        string[] docs = a.Args;
                        if (docs == null)
                            docs = new string[] { };

                        ParameterInfo[] argInfo = mi[i].GetParameters();

                        if (argInfo != null && (argInfo.Length != docs.Length))
                        {
                            Console.WriteLine(MessageType.Warning, "Lua: Warning: {0}() args.Length mismatch requires {1}, had {2}", name, docs.Length, argInfo.Length);
                            break;
                        }

                        for (int j = 0; j < argInfo.Length; j++)
                        {
                            argDocs.Add(argInfo[j].Name + ": " + docs[j]);
                            arguments.Add(argInfo[j].ParameterType.Name.ToLower() + " " + argInfo[j].Name);
                        }

                        LuaFunctionDescriptor lfd = new LuaFunctionDescriptor(name, desc, arguments.ToArray(), argDocs.ToArray());
                        try
                        {
                            LuaFunctions.Add(name, lfd);
                        }
                        catch
                        {
                            Console.WriteLine(MessageType.Warning, "Lua: Duplicate registered functions: {0}", name);
                        }

                        if (name[0] != '#' && !WraithMod.DedicatedServer)
                        {
                            RegisterFunction(name, apiObject, mi[i]);
                        }
                        Console.WriteLine("Lua: Registered function {0}", lfd.Header);
                    }
                }
            }
            Console.WriteLine("Lua: API loaded");
        }
Example #2
0
 public void LoadCommands()
 {
     foreach (string str in WraithMod.Lua.GetScripts(CommandsPath))
     {
         LuaFunctionDescriptor lfd = new LuaFunctionDescriptor();
         List<string> args = new List<string>();
         List<string> argDocs = new List<string>();
         StreamReader sr = new StreamReader(str);
         string s = "";
         bool first = true;
         while ((s = sr.ReadLine()).Substring(0, 2) == "--")
         {
             string[] headerData = s.Substring(2).Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
             if (headerData.Length < 1) continue;
             if (first)
             {
                 if (headerData.Length == 1)
                 {
                     lfd.Name = headerData[0];
                 }
                 else
                 {
                     for (int i = 1; i < headerData.Length; i++)
                     {
                         lfd.Description += headerData[i];
                         if (i != headerData.Length - 1)
                         {
                             lfd.Description += ',';
                         }
                     }
                     lfd.Name = headerData[0].Trim();
                 }
                 first = false;
             }
             else
             {
                 if (headerData.Length == 1)
                 {
                     args.Add(headerData[0].Trim());
                     argDocs.Add(headerData[0].Trim());
                 }
                 else if (headerData.Length == 2)
                 {
                     string docs = "";
                     for (int i = 1; i < headerData.Length; i++)
                     {
                         docs += headerData[i];
                         if (i == headerData.Length - 1)
                         {
                             docs += ',';
                         }
                     }
                     args.Add(headerData[0].Trim());
                     argDocs.Add(headerData[0].Trim() + " " + docs.Trim());
                 }
                 else if (headerData.Length == 3)
                 {
                     string docs = headerData[1] + " - ";
                     for (int i = 2; i < headerData.Length; i++)
                     {
                         docs += headerData[i];
                         if (i != headerData.Length - 1)
                         {
                             docs += ',';
                         }
                     }
                     args.Add(headerData[0].Trim());
                     argDocs.Add(headerData[0].Trim() + " " + docs.Trim());
                 }
             }
         }
         sr.Close();
         string[] splitter = str.Split(new char[] { '/', '\\' });
         string[] splitter2 = splitter[splitter.Length - 1].Split('.');
         for (int i = 0; i < splitter2.Length - 1; i++)
         {
             lfd.Command += splitter2[i];
             if (i != splitter2.Length - 2)
                 lfd.Command += '.';
         }
         if (lfd.Name == "")
             lfd.Name = lfd.Command;
         lfd.Args = args.ToArray();
         lfd.ArgDocs = argDocs.ToArray();
         lfd.MakeDocumentation(true);
         CommandList.Add(lfd);
         Console.WriteLine("Commands: Loaded command {0}", lfd.Header);
     }
 }