示例#1
0
文件: Lua.cs 项目: iamjuarez/SharpLua
        // lockCallback, unlockCallback; used by debug code commented out for now

        public LuaInterface()
        {
            luaState = LuaDLL.luaL_newstate();  // steffenj: Lua 5.1.1 API change (lua_open is gone)
            //LuaDLL.luaopen_base(luaState);	// steffenj: luaopen_* no longer used
            LuaDLL.luaL_openlibs(luaState);     // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here)
            LuaDLL.lua_pushstring(luaState, "LUAINTERFACE LOADED");
            LuaDLL.lua_pushboolean(luaState, true);
            LuaDLL.lua_settable(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX);
            LuaDLL.lua_newtable(luaState);
            LuaDLL.lua_setglobal(luaState, "luanet");
            LuaDLL.lua_pushvalue(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX);
            LuaDLL.lua_getglobal(luaState, "luanet");
            LuaDLL.lua_pushstring(luaState, "getmetatable");
            LuaDLL.lua_getglobal(luaState, "getmetatable");
            LuaDLL.lua_settable(luaState, -3);
            LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX);
            translator = new ObjectTranslator(this, luaState);
            LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX);
            LuaDLL.luaL_dostring(luaState, ScriptStrings.InitLuaNet);   // steffenj: lua_dostring renamed to luaL_dostring

            tracebackFunction = new SharpLua.Lua.lua_CFunction(traceback);

            // We need to keep this in a managed reference so the delegate doesn't get garbage collected
            panicCallback = new SharpLua.Lua.lua_CFunction(PanicCallback);
            Lua.lua_CFunction oldpanicFunc =
                //LuaDLL.lua_atpanic(luaState, tracebackFunction);
                LuaDLL.lua_atpanic(luaState, panicCallback);

            LuaDLL.luaL_dostring(luaState, ScriptStrings.InitClrLib);
            LuaDLL.luaL_dostring(luaState, ScriptStrings.InitExtLib);

            luaState.SetInterface(this);
        }
示例#2
0
 /// <summary>
 /// Find the proxy for a C function
 /// </summary>
 public static LuaCFunctionProxy FindProxy(Lua.lua_CFunction function)
 {
     if (function == null)
     {
         return(null);
     }
     return(_Proxies.FirstOrDefault(p => p.UnmanagedFunction == function));
 }
示例#3
0
        /// <summary>
        /// Convert a lua_CFunction to LuaFunction
        /// </summary>
        public static LuaFunction ToFunction(this Lua.lua_CFunction function)
        {
            if (function == null)
            {
                return(null);
            }
            var proxy = LuaCFunctionProxy.GetProxy(function);

            return(proxy != null ? proxy.ManagedFunction : null);
        }
示例#4
0
    public static int __newindex(Lua.lua_State L)
    {
        Lua.lua_CFunction fnc = null;
        if (LunaTraits_LWF_Point.write_properties.TryGetValue(Lua.lua_tostring(L, 2).ToString(), out fnc))
        {
            Lua.lua_insert(L, 2);            // swap key and value
            Lua.lua_settop(L, 2);            // delete key
            return(fnc(L));
        }

        Lua.luaL_error(L, "__newindex doesn't allow defining non-property member");
        return(0);
    }
示例#5
0
    public static int __index(Lua.lua_State L)
    {
        if (Lua.lua_gettop(L) == 2 && Luna.get_uniqueid(L, 1) ==
            LunaTraits_LWF_Movie.uniqueID)
        {
            LWF.Movie o =
                Luna_LWF_Movie.check(L, 1);
            string name = Lua.lua_tostring(L, 2).ToString();
            if (o.lwf.GetFieldLua(o, name))
            {
                return(1);
            }
            LWF.Movie movie = o.SearchMovieInstance(name, false);
            if (movie != null)
            {
                Lua.lua_pop(L, 1);
                Luna_LWF_Movie.push(L, movie, false);
                return(1);
            }
            LWF.Button button = o.SearchButtonInstance(name, false);
            if (button != null)
            {
                Lua.lua_pop(L, 1);
                Luna_LWF_Button.push(L, button, false);
                return(1);
            }
        }


        {
            Lua.lua_CFunction fnc = null;
            if (LunaTraits_LWF_Movie.properties.TryGetValue(Lua.lua_tostring(L, 2).ToString(), out fnc))
            {
                Lua.lua_pop(L, 1);                // remove self
                return(fnc(L));
            }
        }

        int mt = Lua.lua_getmetatable(L, 1);

        if (mt == 0)
        {
            Lua.luaL_error(L, "__index");             //end
        }
        Lua.lua_pushstring(L, Lua.lua_tostring(L, 2));
        Lua.lua_rawget(L, -2);
        return(1);
    }
示例#6
0
        /// <summary>
        /// Find or create a proxy for a C function
        /// </summary>
        public static LuaCFunctionProxy GetProxy(Lua.lua_CFunction function)
        {
            if (function == null)
            {
                return(null);
            }
            var result = FindProxy(function);

            if (result == null)
            {
                result = new LuaCFunctionProxy()
                {
                    UnmanagedFunction = function
                };
                result.ManagedFunction = result.InvokeUnmanagedFunction;
                _Proxies.Add(result);
            }
            return(result);
        }
示例#7
0
    public static int __index(Lua.lua_State L)
    {
        {
            Lua.lua_CFunction fnc = null;
            if (LunaTraits_LWF_Point.properties.TryGetValue(Lua.lua_tostring(L, 2).ToString(), out fnc))
            {
                Lua.lua_pop(L, 1);                // remove self
                return(fnc(L));
            }
        }

        int mt = Lua.lua_getmetatable(L, 1);

        if (mt == 0)
        {
            Lua.luaL_error(L, "__index");             //end
        }
        Lua.lua_pushstring(L, Lua.lua_tostring(L, 2));
        Lua.lua_rawget(L, -2);
        return(1);
    }
示例#8
0
    public static int __newindex(Lua.lua_State L)
    {
        Lua.lua_CFunction fnc = null;
        if (LunaTraits_LWF_Movie.write_properties.TryGetValue(Lua.lua_tostring(L, 2).ToString(), out fnc))
        {
            Lua.lua_insert(L, 2);            // swap key and value
            Lua.lua_settop(L, 2);            // delete key
            return(fnc(L));
        }
        if (Lua.lua_gettop(L) == 3 && Luna.get_uniqueid(L, 1) ==
            LunaTraits_LWF_Movie.uniqueID)
        {
            LWF.Movie o =
                Luna_LWF_Movie.check(L, 1);
            string name = Lua.lua_tostring(L, 2).ToString();
            if (o.lwf.SetFieldLua(o, name))
            {
                return(0);
            }
        }

        Lua.luaL_error(L, "__newindex doesn't allow defining non-property member");
        return(0);
    }
示例#9
0
 public static void lua_pushstdcallcfunction(Lua.lua_State luaState, Lua.lua_CFunction function)
 {
     Lua.lua_pushcfunction(luaState, function);
 }
示例#10
0
 public static void lua_atpanic(Lua.lua_State luaState, Lua.lua_CFunction panicf)
 {
     Lua.lua_atpanic(luaState, (Lua.lua_CFunction)panicf);
 }
 public RegType(Lua.CharPtr name, Lua.lua_CFunction func)
 {
     this.name = name;
     this.mfunc = func;
 }
示例#12
0
 public RegType(Lua.CharPtr name, Lua.lua_CFunction func)
 {
     this.name  = name;
     this.mfunc = func;
 }
示例#13
0
        /// <summary>
        /// creates a lua state and registers functions commonly used in lua defs
        /// </summary>
        static IntPtr GetLuaStpringState(Dictionary <string, IArchiveFileData> fileMap)
        {
            var L = Lua.luaL_newstate();

            Lua.luaL_openlibs(L);

            // it seems CA makes lowerkeys global, so lets do the same

            // push the system table
            CLua.DoStringPushReturn(L, fileMap["gamedata/system.lua"].Text, new ConstantResults(1));
            // get the lowerkeys field from the system table and push it
            Lua.lua_pushstring(L, "lowerkeys");
            Lua.lua_gettable(L, -2);
            // set the lowerkeys function as global
            Lua.lua_setglobal(L, "lowerkeys");

            Lua.lua_CFunction VFS_Include = l =>
            {
                var path = CLua.ExpectArgs(l, 1)[0].ToString();
                IArchiveFileData file;
                if (!fileMap.TryGetValue(path, out file))
                {
                    throw new Exception("path not found: " + path);
                }
                CLua.DoStringPushReturn(l, file.Text, new ConstantResults(1));
                return(1);
            };
            Lua.lua_CFunction VFS_LoadFile = l =>
            {
                var path = CLua.ExpectArgs(l, 1)[0].ToString();
                IArchiveFileData file;
                if (!fileMap.TryGetValue(path, out file))
                {
                    throw new Exception("path not found: " + path);
                }
                return(CLua.ReturnValues(l, new LuaString(file.Text)));
            };
            Lua.lua_CFunction VFS_FileExists = l =>
            {
                var path = CLua.ExpectArgs(l, 1)[0].ToString();
                return(CLua.ReturnValues(l, new LuaBoolean(fileMap.ContainsKey(path))));
            };

            Lua.lua_CFunction VFS_DirList = l =>
            {
                var args = CLua.ExpectArgs(l, 2);
                var path = args[0].ToString().ToLower();
                var mask = args[1].ToString().ToLower().Substring(1);

                var i     = 0;
                var files = from s in fileMap.Keys
                            where s.StartsWith(path) && s.EndsWith(mask)
                            let arrayIndex = new LuaNumber(i++)
                                             select new KeyValuePair <LuaValue, LuaValue>(arrayIndex, new LuaString(s));
                return(CLua.ReturnValues(l, new LuaTable(files)));
            };
            Lua.lua_CFunction Spring_TimeCheck = l =>
            {
                var desc = LuaValue.Read(l, 1).ToString();
                Lua.lua_pushvalue(l, 2);
                var sw = Stopwatch.StartNew();
                CLua.TraceCallPushReturn(l, new ConstantResults(0));
                Trace.TraceInformation(desc + " " + sw.Elapsed);
                // call function on top, push return values on stack
                return(0);
            };

            Lua.lua_CFunction Spring_Echo = l => CLua.Print(s => Trace.WriteLine(s), l);


            // morphs defs crash if they can't figure out what kind of commander we're using (why do we need morph defs anyway, here?)
            Lua.lua_CFunction Spring_GetModOptions = l =>
            {
                var data = new KeyValuePair <LuaValue, LuaValue>(new LuaString("commtype"),
                                                                 new LuaString("default"));
                return(CLua.ReturnValues(l, new LuaTable(new[] { data })));
            };

            var springFunctions = new List <KeyValuePair <LuaValue, LuaValue> >
            {
                new KeyValuePair <LuaValue, LuaValue>(new LuaString("TimeCheck"), new LuaFunction(Spring_TimeCheck)),
                new KeyValuePair <LuaValue, LuaValue>(new LuaString("Echo"), new LuaFunction(Spring_Echo)),
                new KeyValuePair <LuaValue, LuaValue>(new LuaString("GetModOptions"), new LuaFunction(Spring_GetModOptions)),
            };

            CLua.SetGlobal(L, "Spring", new LuaTable(springFunctions));

            var vfsFunctions = new List <KeyValuePair <LuaValue, LuaValue> >
            {
                new KeyValuePair <LuaValue, LuaValue>(new LuaString("Include"), new LuaFunction(VFS_Include)),
                new KeyValuePair <LuaValue, LuaValue>(new LuaString("LoadFile"), new LuaFunction(VFS_LoadFile)),
                new KeyValuePair <LuaValue, LuaValue>(new LuaString("FileExists"), new LuaFunction(VFS_FileExists)),
                new KeyValuePair <LuaValue, LuaValue>(new LuaString("DirList"), new LuaFunction(VFS_DirList)),
            };

            CLua.SetGlobal(L, "VFS", new LuaTable(vfsFunctions));
            return(L);
        }
示例#14
0
 public LuaFunction(Lua.lua_CFunction value)
 {
     Value = value;
 }
示例#15
0
 public static void RegisterGlobalFunction(IntPtr L, string name, Lua.lua_CFunction f)
 {
     new LuaFunction(f).Push(L);
     Lua.lua_setglobal(L, name);
 }
示例#16
0
        public static void Register(Lua.lua_State L, bool loadAll)
        {
#if REFLECTION_STYLA
            String name = RemoveChar(typeof(T).FullName, '.');
#else
#if !NETFX_CORE
            Object[] attrsClass = typeof(T).GetCustomAttributes(typeof(LuaClass), true);
#else
            Object[] attrsClass = typeof(T).GetTypeInfo().GetCustomAttributes(typeof(LuaClass), true).ToArray();
#endif
            if (attrsClass.Length == 0)
            {
                return;
            }

            String name = ((LuaClass)attrsClass[0]).GetName();
#endif
            //

            Lua.lua_newtable(L);
            int methods = Lua.lua_gettop(L);

            Lua.luaL_newmetatable(L, name);
            int metatable = Lua.lua_gettop(L);

            Lua.luaL_newmetatable(L, "DO NOT TRASH");
            Lua.lua_pop(L, 1);

            // store method table in globals so that
            // scripts can add functions written in Lua.
            Lua.lua_pushvalue(L, methods);
            Lua.lua_setfield(L, Lua.LUA_GLOBALSINDEX, name);

            // hide metatable from Lua getmetatable()
            Lua.lua_pushvalue(L, methods);
            Lua.lua_setfield(L, metatable, "__metatable");

            Lua.lua_pushvalue(L, methods);
            Lua.lua_setfield(L, metatable, "__index");

            Lua.lua_pushcfunction(L, tostring_T);
            Lua.lua_setfield(L, metatable, "__tostring");

            Lua.lua_pushcfunction(L, gc_T);
            Lua.lua_setfield(L, metatable, "__gc");

            Lua.lua_newtable(L);                        // mt for method table
            Lua.lua_setmetatable(L, methods);

#if !NETFX_CORE
            MethodInfo[] methodInfos = typeof(T).GetMethods(BindingFlags.Public | /*BindingFlags.DeclaredOnly | */ BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
#else
            MethodInfo[] methodInfos = typeof(T).GetMethods(BindingFlags.Public, "LGView");
#endif

            Lua.lua_CFunction Fthunk  = new Lua.lua_CFunction(thunk);
            Lua.lua_CFunction SFthunk = new Lua.lua_CFunction(Sthunk);

            //List of LuaGlobalManual
            Dictionary <String, Lua.lua_CFunction> dictGlobal    = new Dictionary <String, Lua.lua_CFunction>();
            Dictionary <String, String>            dictAddedFunc = new Dictionary <String, String>();

            foreach (MethodInfo mi in methodInfos)
            {
                if (mi.IsStatic && mi.Name.Contains("__tostring"))
#if NETFX_CORE
                { dictGlobal.Add("__tostring", (Lua.lua_CFunction)mi.CreateDelegate(typeof(Lua.lua_CFunction))); }
#else
                { dictGlobal.Add("__tostring", (Lua.lua_CFunction)Delegate.CreateDelegate(typeof(Lua.lua_CFunction), mi)); }