示例#1
0
		static int print (LuaState state)
		{
			IntPtr L = state;
			int n = Lua.LuaGetTop(L);  /* number of arguments */
			int i;
			uint len;
			for (i=1; i<=n; i++) {
				int type = Lua.LuaType(L, i);
				switch (type) {
						
					case 0:
						Console.Write("nil");
						break;
					case 4:
						string s = Lua.LuaToLString (L, i, out len).ToString ((int)len);
						Console.Write(s);
						break;
					case 3:
						double number = Lua.LuaNetToNumber (L, i);
						Console.Write (number);
						break;
				}
			}
			Console.WriteLine();
			return 0;
		}
示例#2
0
        public int lua_getfield(LuaState state)
        {
            if (NLua.LuaLib.LuaGetTop(state) != 2)
            {
                NLua.LuaLib.LuaLError(state, "Invalid arguments for 'get_field'.");
                return 0;
            }

            var n = this.m_Lua.Pop(); // name of field
            var o = this.m_Lua.Pop(); // object

            if (!(n is string) || (o == null))
            {
                NLua.LuaLib.LuaLError(state, "Invalid arguments for 'get_field'.");
                return 0;
            }

            var f = o.GetType().GetField((string)n, ObjectHelper.BindFlags);
            if (f == null)
            {
                NLua.LuaLib.LuaPushNil(state);
                return 1;
            }

            this.m_Lua.Push(f.GetValue(o));
            return 1;
        }
 static int print(LuaState L)
 {
     int n = Lua.LuaGetTop(L);  /* number of arguments */
     int i;
     uint len;
     for (i=1; i<=n; i++) {
         string s = Lua.LuaToLString (L, i, out len).ToString ((int)len);
         Console.Write (s);
     }
     Console.WriteLine();
     return 0;
 }
示例#4
0
文件: core.cs 项目: raymanyu/KeraLua
		static int TestUnicodeString (LuaState state)
		{
			uint len;
			CharPtr ptrParam = Lua.LuaToLString (state, 1, out len);
			string param = ptrParam.ToString ();

			Assert.AreEqual (core.UnicodeString, param, "#1 ToString()");

			string param2 = ptrParam.ToString ((int)len);
			
			Assert.AreEqual (core.UnicodeString, param2, "#2 ToString(len)");
			return 0;
		}
示例#5
0
        public int CreateDirectory(LuaState state)
        {
            if (NLua.LuaLib.LuaGetTop(state) != 1)
            {
                NLua.LuaLib.LuaLError(state, "Invalid arguments for 'create_dir'.");
                return 0;
            }

            try
            {
                var dir = NLua.LuaLib.LuaToString(state, -1);
                NLua.LuaLib.LuaPushBoolean(state, System.IO.Directory.CreateDirectory(dir).Exists);
                return 1;
            }
            catch
            {
                NLua.LuaLib.LuaPushBoolean(state, false);
                return 1;
            }
        }
示例#6
0
 public int DirectoryExists(LuaState state)
 {
     var dir = NLua.LuaLib.LuaToString(state, -1);
     NLua.LuaLib.LuaPushBoolean(state, System.IO.Directory.Exists(dir));
     return 1;
 }
示例#7
0
        public int GetDirectoryFiles(LuaState state)
        {
            var dir = NLua.LuaLib.LuaToString(state, -1);
            if (!System.IO.Directory.Exists(dir))
            {
                NLua.LuaLib.LuaPushNil(state);
                return 1;
            }

            NLua.LuaLib.LuaNewTable(state);
            var top = NLua.LuaLib.LuaGetTop(state);
            var index = 1;

            var files = System.IO.Directory.EnumerateFiles(dir, "*", System.IO.SearchOption.TopDirectoryOnly);
            foreach (var f in files)
            {
                NLua.LuaLib.LuaPushNumber(state, index);
                NLua.LuaLib.LuaPushString(state, f);
                NLua.LuaLib.LuaSetTable(state, top);
                index++;
            }

            return 1;
        }
示例#8
0
 public int FileExists(LuaState state)
 {
     var f = NLua.LuaLib.LuaToString(state, -1);
     NLua.LuaLib.LuaPushBoolean(state, System.IO.File.Exists(f));
     return 1;
 }
示例#9
0
        private int lua_AddonNewIndex(LuaState state)
        {
            // Ensure we have a string Key..
            if (NLua.LuaLib.LuaType(state, -2) != NLua.LuaTypes.String)
            {
                NLua.LuaLib.LuaRawSet(state, 1);
                return 0;
            }

            // Obtain the key..
            var key = NLua.LuaLib.LuaToString(state, -2);
            if (string.Compare(key, "author", StringComparison.InvariantCultureIgnoreCase) == 0 ||
                string.Compare(key, "name", StringComparison.InvariantCultureIgnoreCase) == 0 ||
                string.Compare(key, "version", StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                // Ensure the value is valid..
                if (NLua.LuaLib.LuaType(state, -1) != NLua.LuaTypes.String)
                {
                    NLua.LuaLib.LuaPushString(state, string.Format("Invalid value for _addon.{0}! Expected a string.", key));
                    NLua.LuaLib.LuaError(state);
                    return 0;
                }
            }

            NLua.LuaLib.LuaRawSet(state, 1);
            return 0;
        }
示例#10
0
        private int lua_AddonNewCommand(LuaState state)
        {
            // Validate we have two arguments..
            var top = NLua.LuaLib.LuaGetTop(state);
            if (top != 3)
            {
                NLua.LuaLib.LuaPop(state, top);
                NLua.LuaLib.LuaLError(state, "Invalid arguments for commands.register_command!");
                return 0;
            }

            // Validate the argument types..
            if (NLua.LuaLib.LuaType(state, -3) != LuaTypes.String ||
                NLua.LuaLib.LuaType(state, -2) != LuaTypes.String ||
                NLua.LuaLib.LuaType(state, -1) != LuaTypes.Function)
            {
                NLua.LuaLib.LuaPop(state, top);
                NLua.LuaLib.LuaLError(state, "Invalid arguments for commands.register_command!");
                return 0;
            }

            // Obtain the arguments..
            var name = NLua.LuaLib.LuaToString(state, -3);
            var permissions = NLua.LuaLib.LuaToString(state, -2);

            // Ensure the command table exists..
            NLua.LuaLib.LuaGetGlobal(state, "__addon_commands");
            if (NLua.LuaLib.LuaType(state, -1) != NLua.LuaTypes.Table)
            {
                // Create the missing command table..
                NLua.LuaLib.LuaPop(state, 1);
                NLua.LuaLib.LuaNewTable(state);
                NLua.LuaLib.LuaSetGlobal(state, "__addon_commands");
                NLua.LuaLib.LuaGetGlobal(state, "__addon_commands");
            }

            // Register this command to our plugin..
            if (!this.Addons.RegisterCommand(this.FileName.ToLower(), permissions, name))
            {
                NLua.LuaLib.LuaPop(state, top + 1);
                NLua.LuaLib.LuaLError(state, string.Format("Failed to register command '{0}'!", name));
                return 0;
            }

            // Remove the permission from stack..
            NLua.LuaLib.LuaRemove(state, -3);

            // Insert the data into the command table..
            NLua.LuaLib.LuaInsert(state, -3);
            NLua.LuaLib.LuaRawSet(state, 1);
            NLua.LuaLib.LuaPop(state, 1);

            return 0;
        }
示例#11
0
 static int Add(LuaState state)
 {
     Lua.Push(Lua.PopType<double>() + Lua.PopType<double>());
     return 1;
 }
示例#12
0
 static int GetVersion(LuaState state)
 {
     Lua.Push("0.1");
     return 1;
 }
示例#13
0
 /// <summary>
 /// Called when something goes absolutely horribly wrong in Lua, and tries
 /// to produce some debug output. Lua calls abort afterwards, so sending
 /// the output to the internal console is not an option.
 /// </summary>
 private static int panic(LuaState state)
 {
     if(LuaCore.LuaGetTop(state) < 1)
     {
         Console.Error.WriteLine("Fatal lua error (no details provided).");
     }
     else
     {
         uint tmp;
         Console.Error.WriteLine("Fatal lua error: {0}", LuaCore.LuaToLString(state, 1, out tmp));
     }
     Console.Error.Flush();
     return 0;
 }
示例#14
0
        private static int print(LuaState state)
        {
            var top = LuaCore.LuaGetTop(state);

            if(top == 0)
            {
                ConsoleInfo.Instance.AddLine("nil", FontStyle.ConsoleEvent);
                return 0;
            }

            for(var i = 1; i <= top; i++)
            {
                string str;
                switch(LuaCore.LuaType(state, i))
                {
                    case LUA_TNONE:
                        str = "<none>";
                        break;
                    case LUA_TNIL:
                        str = "nil";
                        break;
                    case LUA_TBOOLEAN:
                        str = LuaCore.LuaToBoolean(state, i) == 1 ? "true" : "false";
                        break;
                    case LUA_TLIGHTUSERDATA:
                        str = "<userdata>";
                        break;
                    case LUA_TNUMBER:
                    case LUA_TSTRING:
                        uint tmp;
                        str = LuaCore.LuaToLString(state, i, out tmp).ToString(); // TODO: Maybe won't work
                        break;
                    case LUA_TTABLE:
                        str = "<table>";
                        break;
                    case LUA_TFUNCTION:
                        str = "<function>";
                        break;
                    case LUA_TUSERDATA:
                        str = "<userdata>";
                        break;
                    case LUA_TTHREAD:
                        str = "<thread>";
                        break;
                    default:
                        str = "<invalid>";
                        break;
                }

                ConsoleInfo.Instance.AddLine(str, FontStyle.ConsoleEvent);
            }
            return 0;
        }
示例#15
0
        public int lua_setfield(LuaState state)
        {
            if (NLua.LuaLib.LuaGetTop(state) != 3)
            {
                NLua.LuaLib.LuaLError(state, "Invalid arguments for 'set_field'.");
                return 0;
            }

            var v = this.m_Lua.Pop(); // value
            var n = this.m_Lua.Pop(); // name of field
            var o = this.m_Lua.Pop(); // object

            if (!(n is string) || (o == null))
            {
                NLua.LuaLib.LuaLError(state, "Invalid arguments for 'set_field'.");
                return 0;
            }

            var f = o.GetType().GetField((string)n, ObjectHelper.BindFlags);
            if (f == null)
            {
                NLua.LuaLib.LuaPushBoolean(state, false);
                return 1;
            }

            try
            {
                f.SetValue(o, Convert.ChangeType(v, f.FieldType));
                NLua.LuaLib.LuaPushBoolean(state, true);
                return 1;
            }
            catch
            {
                NLua.LuaLib.LuaPushBoolean(state, false);
                return 1;
            }
        }
示例#16
0
		public void Setup()
		{
			state = Lua.LuaLNewState ();
			Lua.LuaLOpenLibs (state);
			Lua.LuaPushStdCallCFunction (state,  core.func_print);
			Lua.LuaNetSetGlobal (state, "print");			
		}
示例#17
0
		public void TearDown ()
		{
			Lua.LuaClose (state);
			state = IntPtr.Zero;
		}
示例#18
0
        private int lua_AddonNewEvent(LuaState state)
        {
            // Validate we have two arguments..
            var top = NLua.LuaLib.LuaGetTop(state);
            if (top != 2)
            {
                NLua.LuaLib.LuaPop(state, top);
                NLua.LuaLib.LuaLError(state, "Invalid arguments for events.register_event!");
                return 0;
            }

            // Validate we have the proper types..
            var type1 = NLua.LuaLib.LuaType(state, -1);
            var type2 = NLua.LuaLib.LuaType(state, -2);
            if (type1 != NLua.LuaTypes.Function || type2 != NLua.LuaTypes.String)
            {
                NLua.LuaLib.LuaPop(state, top);
                NLua.LuaLib.LuaLError(state, "Invalid arguments for events.register_event!");
                return 0;
            }

            // Ensure the event table exists..
            NLua.LuaLib.LuaGetGlobal(state, "__addon_events");
            if (NLua.LuaLib.LuaType(state, -1) != NLua.LuaTypes.Table)
            {
                // Create the missing events table..
                NLua.LuaLib.LuaPop(state, 1);
                NLua.LuaLib.LuaNewTable(state);
                NLua.LuaLib.LuaSetGlobal(state, "__addon_events");
                NLua.LuaLib.LuaGetGlobal(state, "__addon_events");
            }

            // Insert the data into the events table..
            NLua.LuaLib.LuaInsert(state, -3);
            NLua.LuaLib.LuaRawSet(state, 1);
            NLua.LuaLib.LuaPop(state, 1);

            return 0;
        }