Exemplo n.º 1
0
 public static void SetGlobal(IntPtr L, string name, LuaValue value)
 {
     value.Push(L);
     Lua.lua_setglobal(L, name);
 }
Exemplo n.º 2
0
 /// <summary>
 /// expects table on top
 /// </summary>
 public static void SetField(IntPtr L, LuaValue key, LuaValue value)
 {
     Check(L, -1);
     key.Push(L);
     value.Push(L);
     Lua.lua_settable(L, -3);
 }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
 /// <summary>
 /// expects table on top
 /// returns field from table on top of stack
 /// </summary>
 public static LuaValue GetField(IntPtr L, LuaValue key)
 {
     Check(L, -1);
     key.Push(L);
     Lua.lua_gettable(L, -2);
     var ret = Read(L, -2);
     Pop(L, 1);
     return ret;
 }
Exemplo n.º 5
0
 public static void SetGlobal(IntPtr L, string name, LuaValue value)
 {
     value.Push(L);
     Lua.lua_setglobal(L, name);
 }