コード例 #1
0
ファイル: Commands.cs プロジェクト: lytedev/wraithmod
        public void RunCommand(object o)
        {
            string input = o.ToString();

            string[] args = Input.ParseArguments(input, true);
            if (args[0].ToLower().Trim() == "help")
            {
                CommandHelp();
                return;
            }
            LuaFunctionDescriptor command = null;

            foreach (LuaFunctionDescriptor lfd in CommandList)
            {
                if (Input.CommandMatch(lfd.Name, args[0], false, false, false, false))
                {
                    command = lfd;
                    break;
                }
            }
            if (command == null)
            {
                foreach (LuaFunctionDescriptor lfd in CommandList)
                {
                    if (Input.CommandMatch(lfd.Name, args[0]))
                    {
                        command = lfd;
                        break;
                    }
                }
            }
            if (command == null)
            {
                Console.WriteLine(MessageType.Warning, "Commands: Could not find command {0}", args[0]);
                return;
            }
            string file = CommandsPath + command.Command + ".lua";

            try
            {
                string toRun            = "";
                int    optionalCommands = 0;
                foreach (string s in command.Args)
                {
                    try
                    {
                        if (s[0] == '*' || s[1] == '*')
                        {
                            optionalCommands++;
                        }
                    }
                    catch
                    {
                    }
                }
                if (command.Args.Length - (args.Length - 1 - optionalCommands) < optionalCommands)
                {
                    Console.WriteLine(MessageType.Warning, "Commands: Invalid number of arguments given: {0} ({1} needed and {2} optional)", args.Length - 1, command.Args.Length - optionalCommands, optionalCommands);
                    return;
                }
                for (int i = 0; i < command.Args.Length; i++)
                {
                    string argName = command.Args[i];
                    int    offset  = 0;
                    if (argName[0] == '*' || argName[1] == '*')
                    {
                        offset++;
                    }
                    string argVal = "";
                    try
                    {
                        argVal = args[i + 1];
                    }
                    catch
                    {
                        break;
                    }
                    bool isString = false;
                    if (argName[0] == '$' || argName[1] == '$')
                    {
                        isString = true;
                        offset++;
                    }
                    argName = argName.Substring(offset);
                    try
                    {
                        if (!isString)
                        {
                            toRun += argName + " = " + argVal + "\n";
                        }
                        else
                        {
                            toRun += argName + " = \"" + argVal + "\"\n";
                        }
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        // Must have been optional...?
                    }
                }
                StreamReader sr = new StreamReader(file);
                toRun += sr.ReadToEnd();
                sr.Close();
                WraithMod.Lua.RunCommand(toRun, !WraithMod.Debug);
            }
            catch (Exception e) // 65 - 122
            {
                Console.WriteLine(MessageType.Error, "Commands: Could not run the script for command {0}: {1}\n  {2}", command.Name, file, e.Message);
                return;
            }
        }
コード例 #2
0
ファイル: Commands.cs プロジェクト: lytedev/wraithmod
 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);
     }
 }
コード例 #3
0
ファイル: LuaHelper.cs プロジェクト: Jakosa/alaris
        /// <summary>
        /// Registers Lua functions found in the specified target.
        /// </summary>
        /// <param name="luaFunctions">Global lua function table.</param>
        /// <param name="target">Object (class,struct) to search in.</param>
        /// <param name="vm">The Lua virtual machine.</param>
        public static void RegisterLuaFunctions(LuaInterface.Lua vm, ref Dictionary<string, LuaFunctionDescriptor> luaFunctions, object target)
        {
            if (vm == null || luaFunctions == null)
                return;

            var type = target.GetType();

            foreach(var method in type.GetMethods())
            {
                foreach(var attribute in Attribute.GetCustomAttributes(method))
                {
                    var luaFunctionAttribute = attribute as LuaFunctionAttribute;
                    if (luaFunctionAttribute == null) continue;
                    var attr = luaFunctionAttribute;

                    var parameters = new List<string>();

                    var paramInfo = method.GetParameters();

                    if(attr.FunctionParameters != null && paramInfo.Length != attr.FunctionParameters.Length)
                    {
                        Console.Error.WriteLine("Function {0} (exported as {1}): argument number mismatch. Declared {2}, but requires {3}.",
                                                method.Name,
                                                attr.FunctionName,
                                                attr.FunctionParameters.Length,
                                                paramInfo.Length);

                        break;
                    }

                    // build parameter doc hashtable.
                    if (attr.FunctionParameters != null)
                        parameters.AddRange(paramInfo.Select((t, i) => string.Format("{0} - {1}", t.Name, attr.FunctionParameters[i])));

                    var descriptor = new LuaFunctionDescriptor(attr.FunctionName, attr.FunctionDocumentation,
                                                               parameters);

                    luaFunctions.Add(attr.FunctionName, descriptor);

                    vm.RegisterFunction(attr.FunctionName, target, method);
                }
            }
        }
コード例 #4
0
ファイル: Lua.cs プロジェクト: lytedev/wraithmod
        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");
        }
コード例 #5
0
        public void Help(string command)
        {
            // Check if the command is a function name
            if (_lua.Functions.Contains(command))
            {
                LuaFunctionDescriptor func = (LuaFunctionDescriptor)_lua.Functions[command];

                // Display basic help message
                txtResult.Text += "Help for " + command + ":\n=========";

                for (int i = 0; i <= command.Length; i++)
                {
                    txtResult.Text += "=";
                }

                txtResult.Text += "\n" + func.FunctionFullDocumentation + "\n\n";

                return;
            }

            // Check if the command is a package name
            if (command.IndexOf(".") == -1)
            {
                // Check if the package exists
                if (_lua.Packages.ContainsKey(command))
                {
                    LuaPackageDescriptor pkg = (LuaPackageDescriptor)_lua.Packages[command];

                    DisplayPackageHelp(pkg);

                    return;
                }
                else
                {
                    txtResult.Text += "No such function or package: " + command + "\n\n";

                    return;
                }
            }

            // Determine the path to the function name
            string[] parts = command.Split('.');

            // Check if the package exists
            if (!_lua.Packages.ContainsKey(parts[0]))
            {
                txtResult.Text += "No such function or package: " + command + "\n\n";

                return;
            }

            LuaPackageDescriptor desc = (LuaPackageDescriptor)_lua.Packages[parts[0]];

            // Check if the function exists within the package
            if (!desc.HasFunction(parts[1]))
            {
                txtResult.Text += "Package " + parts[0] + " doesn't have a " + parts[1] + " function.\n\n";

                return;
            }

            // Display basic help message
            txtResult.Text += "Help for " + command + ":\n=========";

            for (int i = 0; i <= command.Length; i++)
            {
                txtResult.Text += "=";
            }

            txtResult.Text += "\n" + desc.PackageName + "." + desc.WriteHelp(parts[1]) + "\n\n";
        }