当前位置: 首页>>代码示例>>C#>>正文


C# Lua.NewTable方法代码示例

本文整理汇总了C#中NLua.Lua.NewTable方法的典型用法代码示例。如果您正苦于以下问题:C# Lua.NewTable方法的具体用法?C# Lua.NewTable怎么用?C# Lua.NewTable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NLua.Lua的用法示例。


在下文中一共展示了Lua.NewTable方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InitializeLua

        /// <summary>
        /// Initializes the Lua environment
        /// </summary>
        private void InitializeLua()
        {
            // Create the Lua environment
            LuaEnvironment = new NLua.Lua();

            // Filter useless or potentially malicious libraries/functions
            LuaEnvironment["os"] = null;
            LuaEnvironment["io"] = null;
            LuaEnvironment["require"] = null;
            LuaEnvironment["dofile"] = null;
            LuaEnvironment["package"] = null;
            LuaEnvironment["luanet"] = null;
            LuaEnvironment["load"] = null;

            // Read util methods
            setmetatable = LuaEnvironment["setmetatable"] as LuaFunction;

            // Create metatables
            //Type mytype = GetType();
            LuaEnvironment.NewTable("tmp");
            overloadselectormeta = LuaEnvironment["tmp"] as LuaTable;
            //LuaEnvironment.RegisterFunction("tmp.__index", mytype.GetMethod("FindOverload", BindingFlags.Public | BindingFlags.Static));
            LuaEnvironment.NewTable("tmp");
            // Ideally I'd like for this to be implemented C# side, but using C#-bound methods as metamethods seems dodgy
            LuaEnvironment.LoadString(
            @"function tmp:__index( key )
            local sftbl = rawget( self, '_sftbl' )
            local field = sftbl[ key ]
            if (field) then return field:GetValue( nil ) end
            end
            function tmp:__newindex( key, value )
            local sftbl = rawget( self, '_sftbl' )
            local field = sftbl[ key ]
            if (field) then field:SetValue( nil, value ) end
            end
            ", "LuaExtension").Call();
            //LuaEnvironment.RegisterFunction("tmp.__index", mytype.GetMethod("ReadStaticProperty", BindingFlags.NonPublic | BindingFlags.Static));
            //LuaEnvironment.RegisterFunction("tmp.__newindex", mytype.GetMethod("WriteStaticProperty", BindingFlags.NonPublic | BindingFlags.Static));
            typetablemeta = LuaEnvironment["tmp"] as LuaTable;
            LuaEnvironment["tmp"] = null;

            LuaEnvironment.NewTable("tmp");
            LuaEnvironment.LoadString(
            @"function tmp:__index( key )
            if (type( key ) == 'table') then
            local baseType = rawget( self, '_type' )
            return util.SpecializeType( baseType, key )
            end
            end
            ", "LuaExtension").Call();
            generictypetablemeta = LuaEnvironment["tmp"] as LuaTable;
            LuaEnvironment["tmp"] = null;

            LuaEnvironment.NewTable("libraryMetaTable");
            LuaEnvironment.LoadString(
            @"function libraryMetaTable:__index( key )
            local ptbl = rawget( self, '_properties' )
            local property = ptbl[ key ]
            if (property) then return property:GetValue( rawget( self, '_object' ), null ) end
            end
            function libraryMetaTable:__newindex( key, value )
            local ptbl = rawget( self, '_properties' )
            local property = ptbl[ key ]
            if (property) then property:SetValue( rawget( self, '_object' ), value ) end
            end
            ", "LuaExtension").Call();
            libraryMetaTable = LuaEnvironment["libraryMetaTable"] as LuaTable;
            LuaEnvironment["libraryMetaTable"] = null;

            LuaEnvironment.NewTable("tmp");
            PluginMetatable = LuaEnvironment["tmp"] as LuaTable;
            LuaEnvironment.LoadString(
            @"function tmp:__newindex( key, value )
            if (type( value ) ~= 'function') then return rawset( self, key, value ) end
            local activeAttrib = rawget( self, '_activeAttrib' )
            if (not activeAttrib) then return rawset( self, key, value ) end
            if (activeAttrib == self) then
            print( 'PluginMetatable.__newindex - self._activeAttrib was somehow self!' )
            rawset( self, key, value )
            return
            end
            local attribArr = rawget( self, '_attribArr' )
            if (not attribArr) then
            attribArr = {}
            rawset( self, '_attribArr', attribArr )
            end
            activeAttrib._func = value
            attribArr[#attribArr + 1] = activeAttrib
            rawset( self, '_activeAttrib', nil )
            end
            ", "LuaExtension").Call();
            LuaEnvironment["tmp"] = null;
        }
开发者ID:RuanPitout88,项目名称:Oxide,代码行数:96,代码来源:LuaExtension.cs

示例2: BubbleLua

 public BubbleLua(Lua lua, bool init = true)
 {
     Lua = lua;
     if (!init)
         return;
     Lua.NewTable ("bubble");
     Bubble = (LuaTable)Lua ["bubble"];
     lua.DoString (EmbeddedResources.GetString ("BubbleEngine.LuaAPI.bubbleinternal.lua"));
 }
开发者ID:CallumDev,项目名称:BubbleEngine,代码行数:9,代码来源:BubbleLua.cs

示例3: Main

        public static void Main(string[] args)
        {
            if(args.Length > 0)
            {
                // For attaching from the debugger
                // Thread.Sleep(20000);

                using(Lua lua = new Lua())
                {
                    //lua.OpenLibs();			// steffenj: Lua 5.1.1 API change (all libs already opened in Lua constructor!)
                    lua.NewTable("arg");
                    LuaTable argc = (LuaTable)lua["arg"];
                    argc[-1] = "LuaRunner";
                    argc[0] = args[0];

                    for(int i = 1; i < args.Length; i++)
                        argc[i] = args[i];

                    argc["n"] = args.Length - 1;

                    try
                    {
                        //Console.WriteLine("DoFile(" + args[0] + ");");
                        lua.DoFile(args[0]);
                    }
                    catch(Exception e)
                    {
                        // steffenj: BEGIN error message improved, output is now in decending order of importance (message, where, stacktrace)
                        // limit size of strack traceback message to roughly 1 console screen height
                        string trace = e.StackTrace;

                        if(e.StackTrace.Length > 1300)
                            trace = e.StackTrace.Substring(0, 1300) + " [...] (traceback cut short)";

                        Console.WriteLine();
                        Console.WriteLine(e.Message);
                        Console.WriteLine(e.Source + " raised a " + e.GetType().ToString());
                        Console.WriteLine(trace);

                        // wait for keypress if there is an error
                        Console.ReadKey();
                        // steffenj: END error message improved
                    }
                }
            }
            else
            {
                Console.WriteLine("LuaRunner -- runs Lua scripts with CLR access");
                Console.WriteLine("Usage: luarunner <script.lua> [{<arg>}]");
            }
        }
开发者ID:Azhei,项目名称:NLua,代码行数:51,代码来源:LuaNetRunner.cs

示例4: Load

        /// <summary>
        /// Loads Oxide
        /// </summary>
        private void Load()
        {
            // Initialise SSL
            System.Net.ServicePointManager.Expect100Continue = false;
            System.Net.ServicePointManager.ServerCertificateValidationCallback = (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain,
                                       System.Net.Security.SslPolicyErrors sslPolicyErrors) => { return true; };
            System.Net.ServicePointManager.DefaultConnectionLimit = 200;

            // Determine the absolute path of the server instance
            serverpath = Path.GetDirectoryName(Path.GetFullPath(Application.dataPath));
            string[] cmdline = Environment.GetCommandLineArgs();
            for (int i = 0; i < cmdline.Length - 1; i++)
            {
                string arg = cmdline[i].ToLower();
                if (arg == "-serverinstancedir" || arg == "-oxidedir")
                {
                    try
                    {
                        serverpath = Path.GetFullPath(cmdline[++i]);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("Failed to read server instance directory from command line!", ex);
                    }
                }
            }

            // Ensure directories exist
            if (!Directory.Exists(serverpath)) Directory.CreateDirectory(serverpath);
            if (!Directory.Exists(GetPath("plugins"))) Directory.CreateDirectory(GetPath("plugins"));
            if (!Directory.Exists(GetPath("data"))) Directory.CreateDirectory(GetPath("data"));
            if (!Directory.Exists(GetPath("logs"))) Directory.CreateDirectory(GetPath("logs"));
            Logger.Message(string.Format("Loading at {0}...", serverpath));

            // Initialise the Unity component
            oxideobject = new GameObject("Oxide");
            oxidecomponent = oxideobject.AddComponent<OxideComponent>();
            oxidecomponent.Oxide = this;

            // Hook things that we can't hook using the IL injector
            var serverinit = UnityEngine.Object.FindObjectOfType(Type.GetType("ServerInit, Assembly-CSharp")) as MonoBehaviour;
            serverinit.gameObject.AddComponent<ServerInitHook>();

            // Initialise needed maps and collections
            datafiles = new Dictionary<string, Datafile>();
            timers = new HashSet<Timer>();
            webrequests = new HashSet<AsyncWebRequest>();

            // Initialise the lua state
            lua = new Lua();
            lua["os"] = null;
            lua["io"] = null;
            lua["require"] = null;
            lua["dofile"] = null;
            lua["package"] = null;
            lua["luanet"] = null;
            lua["load"] = null;

            // Register functions
            lua.NewTable("cs");
            RegisterFunction("cs.print", "lua_Print");
            RegisterFunction("cs.error", "lua_Error");
            RegisterFunction("cs.callplugins", "lua_CallPlugins");
            RegisterFunction("cs.findplugin", "lua_FindPlugin");
            RegisterFunction("cs.requeststatic", "lua_RequestStatic");
            RegisterFunction("cs.registerstaticmethod", "lua_RegisterStaticMethod");
            RegisterFunction("cs.requeststaticproperty", "lua_RequestStaticProperty");
            RegisterFunction("cs.requestproperty", "lua_RequestProperty");
            RegisterFunction("cs.requeststaticfield", "lua_RequestStaticField");
            RegisterFunction("cs.requestfield", "lua_RequestField");
            RegisterFunction("cs.requestenum", "lua_RequestEnum");
            RegisterFunction("cs.readproperty", "lua_ReadProperty");
            RegisterFunction("cs.readfield", "lua_ReadField");
            RegisterFunction("cs.castreadproperty", "lua_CastReadProperty");
            RegisterFunction("cs.castreadfield", "lua_CastReadField");
            RegisterFunction("cs.readulongpropertyasuint", "lua_ReadULongPropertyAsUInt");
            RegisterFunction("cs.readulongpropertyasstring", "lua_ReadULongPropertyAsString");
            RegisterFunction("cs.readulongfieldasuint", "lua_ReadULongFieldAsUInt");
            RegisterFunction("cs.readulongfieldasstring", "lua_ReadULongFieldAsString");
            RegisterFunction("cs.readpropertyandsetonarray", "lua_ReadPropertyAndSetOnArray");
            RegisterFunction("cs.readfieldandsetonarray", "lua_ReadFieldAndSetOnArray");
            RegisterFunction("cs.reloadplugin", "lua_ReloadPlugin");
            RegisterFunction("cs.getdatafile", "lua_GetDatafile");
            RegisterFunction("cs.getdatafilelist", "lua_GetDatafileList"); // LMP
            RegisterFunction("cs.removedatafile", "lua_RemoveDatafile"); // LMP
            RegisterFunction("cs.dump", "lua_Dump");
            RegisterFunction("cs.createarrayfromtable", "lua_CreateArrayFromTable");
            RegisterFunction("cs.createtablefromarray", "lua_CreateTableFromArray");
            RegisterFunction("cs.gettype", "lua_GetType");
            RegisterFunction("cs.makegenerictype", "lua_MakeGenericType");
            RegisterFunction("cs.new", "lua_New");
            RegisterFunction("cs.newarray", "lua_NewArray");
            RegisterFunction("cs.convertandsetonarray", "lua_ConvertAndSetOnArray");
            RegisterFunction("cs.getelementtype", "lua_GetElementType");
            RegisterFunction("cs.newtimer", "lua_NewTimer");
            RegisterFunction("cs.sendwebrequest", "lua_SendWebRequest");
            RegisterFunction("cs.postwebrequest", "lua_PostWebRequest");
//.........这里部分代码省略.........
开发者ID:khwoo1004,项目名称:Oxide,代码行数:101,代码来源:Main.cs

示例5: Main

        private Main()
        {
            try
            {
                // Determine the absolute path of the server instance
                serverpath = Path.GetDirectoryName(Path.GetFullPath(Application.dataPath));
                string[] cmdline = Environment.GetCommandLineArgs();
                for (int i = 0; i < cmdline.Length - 1; i++)
                {
                    string arg = cmdline[i].ToLower();
                    if (arg == "-serverinstancedir" || arg == "-oxidedir")
                    {
                        try
                        {
                            serverpath = Path.GetFullPath(cmdline[++i]);
                        }
                        catch (Exception ex)
                        {
                            Logger.Error("Failed to read server instance directory from command line!", ex);
                        }
                    }
                }

                // Ensure directories exist
                if (!Directory.Exists(serverpath)) Directory.CreateDirectory(serverpath);
                if (!Directory.Exists(GetPath("plugins"))) Directory.CreateDirectory(GetPath("plugins"));
                if (!Directory.Exists(GetPath("data"))) Directory.CreateDirectory(GetPath("data"));
                if (!Directory.Exists(GetPath("logs"))) Directory.CreateDirectory(GetPath("logs"));
                Logger.Message(string.Format("Loading at {0}...", serverpath));

                // Initialise the Unity component
                oxideobject = new GameObject("Oxide");
                oxidecomponent = oxideobject.AddComponent<OxideComponent>();
                oxidecomponent.Oxide = this;

                // Hook things that we can't hook using the IL injector
                var serverinit = UnityEngine.Object.FindObjectOfType(Type.GetType("ServerInit, Assembly-CSharp")) as MonoBehaviour;
                serverinit.gameObject.AddComponent<ServerInitHook>();

                // Initialise needed maps and collections
                datafiles = new Dictionary<string, Datafile>();
                plugins = new Dictionary<string, LuaTable>();
                timers = new HashSet<Timer>();
                webrequests = new HashSet<AsyncWebRequest>();

                // Initialise the lua state
                lua = new Lua();
                lua["os"] = null;
                lua["io"] = null;
                lua["require"] = null;
                lua["dofile"] = null;
                lua["package"] = null;
                lua["luanet"] = null;
                lua["load"] = null;

                // Register functions
                lua.NewTable("cs");
                RegisterFunction("cs.print", "lua_Print");
                RegisterFunction("cs.error", "lua_Error");
                RegisterFunction("cs.callplugins", "lua_CallPlugins");
                RegisterFunction("cs.findplugin", "lua_FindPlugin");
                RegisterFunction("cs.requeststatic", "lua_RequestStatic");
                RegisterFunction("cs.registerstaticmethod", "lua_RegisterStaticMethod");
                RegisterFunction("cs.requeststaticproperty", "lua_RequestStaticProperty");
                RegisterFunction("cs.requestproperty", "lua_RequestProperty");
                RegisterFunction("cs.requeststaticfield", "lua_RequestStaticField");
                RegisterFunction("cs.requestfield", "lua_RequestField");
                RegisterFunction("cs.requestenum", "lua_RequestEnum");
                RegisterFunction("cs.readproperty", "lua_ReadProperty");
                RegisterFunction("cs.readfield", "lua_ReadField");
                RegisterFunction("cs.castreadproperty", "lua_CastReadProperty");
                RegisterFunction("cs.castreadfield", "lua_CastReadField");
                RegisterFunction("cs.readulongpropertyasuint", "lua_ReadULongPropertyAsUInt");
                RegisterFunction("cs.readulongpropertyasstring", "lua_ReadULongPropertyAsString");
                RegisterFunction("cs.readulongfieldasuint", "lua_ReadULongFieldAsUInt");
                RegisterFunction("cs.readulongfieldasstring", "lua_ReadULongFieldAsString");
                RegisterFunction("cs.reloadplugin", "lua_ReloadPlugin");
                RegisterFunction("cs.getdatafile", "lua_GetDatafile");
                RegisterFunction("cs.dump", "lua_Dump");
                RegisterFunction("cs.createarrayfromtable", "lua_CreateArrayFromTable");
                RegisterFunction("cs.createtablefromarray", "lua_CreateTableFromArray");
                RegisterFunction("cs.gettype", "lua_GetType");
                RegisterFunction("cs.makegenerictype", "lua_MakeGenericType");
                RegisterFunction("cs.new", "lua_New");
                RegisterFunction("cs.newarray", "lua_NewArray");
                RegisterFunction("cs.convertandsetonarray", "lua_ConvertAndSetOnArray");
                RegisterFunction("cs.getelementtype", "lua_GetElementType");
                RegisterFunction("cs.newtimer", "lua_NewTimer");
                RegisterFunction("cs.sendwebrequest", "lua_SendWebRequest");
                RegisterFunction("cs.throwexception", "lua_ThrowException");
                RegisterFunction("cs.gettimestamp", "lua_GetTimestamp");
                RegisterFunction("cs.loadstring", "lua_LoadString");

                // Register constants
                lua.NewTable("bf");
                lua["bf.public_instance"] = BindingFlags.Public | BindingFlags.Instance;
                lua["bf.private_instance"] = BindingFlags.NonPublic | BindingFlags.Instance;
                lua["bf.public_static"] = BindingFlags.Public | BindingFlags.Static;
                lua["bf.private_static"] = BindingFlags.NonPublic | BindingFlags.Static;

//.........这里部分代码省略.........
开发者ID:JonathanPorta,项目名称:Oxide,代码行数:101,代码来源:Main.cs

示例6: Main

        public static void Main(string[] args)
        {
            try {

                using (Lua lua = new Lua ()) {
                    //lua.OpenLibs();			// steffenj: Lua 5.1.1 API change (all libs already opened in Lua constructor!)
                    lua.NewTable ("arg");
                    LuaTable argc = (LuaTable)lua ["arg"];

                    argc [0] = "NLua";

                    for (int i = 0; i < args.Length; i++) {
                        argc [i + 1] = args [i];
                    }

                    argc ["n"] = args.Length;

                    lua.LoadCLRPackage ();

                    try {
                        lua.DoString (lua_script,"lua");
                    } catch (Exception e) {
                        // limit size of stack traceback message to roughly 1 console screen height
                        string trace = e.StackTrace;

                        if (e.StackTrace.Length > 1300)
                            trace = e.StackTrace.Substring (0, 1300) + " [...] (traceback cut short)";

                        Console.WriteLine ();
                        Console.WriteLine (e.Message);
                        Console.WriteLine (e.Source + " raised a " + e.GetType ().ToString ());
                        Console.WriteLine (trace);

                        // wait for key press if there is an error
                        Console.ReadKey ();
                    }
                }
            } catch (Exception e) {
                Console.WriteLine ();
                Console.WriteLine (e.Message);
                Console.WriteLine (e.Source + " raised a " + e.GetType ().ToString ());

                Console.ReadKey ();
            }
        }
开发者ID:mMellowz,项目名称:revcore,代码行数:45,代码来源:LuaNetRunner.cs


注:本文中的NLua.Lua.NewTable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。