Exemple #1
0
        /// <summary>
        /// Convert C# exceptions into Lua errors
        /// </summary>
        /// <returns>num of things on stack</returns>
        /// <param name = "e">null for no pending exception</param>
        internal int SetPendingException(Exception e)
        {
            var caughtExcept = e;

            if (caughtExcept == null)
            {
                return(0);
            }

            try
            {
                _translator.ThrowError(_luaState, caughtExcept);
            }
            catch (Exception ex)
            {
                var err = ex.Message;
                if (err == "Object reference not set to an instance of an object.")
                {
                    throw new LuaException("脚本异常,请不要快速启动和关闭脚本");
                }
                else
                {
                    throw new LuaException(err);
                }
            }

            //_luaState.PushNil();
            return(1);
        }
Exemple #2
0
        /// <summary>
        /// Convert C# exceptions into Lua errors
        /// </summary>
        /// <returns>num of things on stack</returns>
        /// <param name = "e">null for no pending exception</param>
        internal int SetPendingException(Exception e)
        {
            var caughtExcept = e;

            if (caughtExcept == null)
            {
                return(0);
            }

            _translator.ThrowError(_luaState, caughtExcept);
            return(1);
        }
Exemple #3
0
        /// <summary>
        /// Convert C# exceptions into Lua errors
        /// </summary>
        /// <returns>num of things on stack</returns>
        /// <param name = "e">null for no pending exception</param>
        internal int SetPendingException(Exception e)
        {
            var caughtExcept = e;

            if (caughtExcept != null)
            {
                translator.ThrowError(luaState, caughtExcept);
                LuaLib.LuaPushNil(luaState);
                return(1);
            }
            else
            {
                return(0);
            }
        }
Exemple #4
0
		static int UnaryNegationLua (LuaState luaState, ObjectTranslator translator)
		{
			object obj1 = translator.GetRawNetObject (luaState, 1);

			if (obj1 == null) {
				translator.ThrowError (luaState, "Cannot negate a nil object");
				LuaLib.LuaPushNil (luaState);
				return 1;
			}

			Type type = obj1.GetType ();
			MethodInfo opUnaryNegation = type.GetMethod ("op_UnaryNegation");

			if (opUnaryNegation == null) {
				translator.ThrowError (luaState, "Cannot negate object (" + type.Name + " does not overload the operator -)");
				LuaLib.LuaPushNil (luaState);
				return 1;
			}
			obj1 = opUnaryNegation.Invoke (obj1, new object [] { obj1 });
			translator.Push (luaState, obj1);
			return 1;
		}
Exemple #5
0
		static int MatchOperator (LuaState luaState, string operation, ObjectTranslator translator)
		{
			var validOperator = new MethodCache ();

			object target = GetTargetObject (luaState, operation, translator);

			if (target == null) {
				translator.ThrowError (luaState, "Cannot call " + operation + " on a nil object");
				LuaLib.LuaPushNil (luaState);
				return 1;
			}

			Type type = target.GetType ();
			var operators = type.GetMethods (operation, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

			foreach (var op in operators) {
				bool isOk = translator.MatchParameters (luaState, op, ref validOperator);

				if (!isOk)
					continue;

				object result;
				if (op.IsStatic)
					result = op.Invoke (null, validOperator.args);
				else
					result = op.Invoke (target, validOperator.args);
				translator.Push (luaState, result);
				return 1;
			}

			translator.ThrowError (luaState, "Cannot call (" + operation + ") on object type " + type.Name);
			LuaLib.LuaPushNil (luaState);
			return 1;
		}
Exemple #6
0
        private int GetMethodInternal(LuaState luaState)
        {
            object obj = translator.GetRawNetObject(luaState, 1);

            if (obj == null)
            {
                translator.ThrowError(luaState, "trying to index an invalid object reference");
                LuaLib.LuaPushNil(luaState);
                return(1);
            }

            object index = translator.GetObject(luaState, 2);
            //var indexType = index.GetType();
            string methodName = index as string;                        // will be null if not a string arg
            var    objType    = obj.GetType();

            // Handle the most common case, looking up the method by name.

            // CP: This will fail when using indexers and attempting to get a value with the same name as a property of the object,
            // ie: xmlelement['item'] <- item is a property of xmlelement
            try {
                if (!string.IsNullOrEmpty(methodName) && IsMemberPresent(objType, methodName))
                {
                    return(GetMember(luaState, objType, obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase));
                }
            } catch {
            }

            // Try to access by array if the type is right and index is an int (lua numbers always come across as double)
            if (objType.IsArray && index is double)
            {
                int intIndex = (int)((double)index);

                if (objType.UnderlyingSystemType == typeof(float[]))
                {
                    float[] arr = ((float[])obj);
                    translator.Push(luaState, arr [intIndex]);
                }
                else if (objType.UnderlyingSystemType == typeof(double[]))
                {
                    double[] arr = ((double[])obj);
                    translator.Push(luaState, arr [intIndex]);
                }
                else if (objType.UnderlyingSystemType == typeof(int[]))
                {
                    int[] arr = ((int[])obj);
                    translator.Push(luaState, arr [intIndex]);
                }
                else
                {
                    object[] arr = (object[])obj;
                    translator.Push(luaState, arr [intIndex]);
                }
            }
            else
            {
                // Try to use get_Item to index into this .net object
                var methods = objType.GetMethods();

                foreach (var mInfo in methods)
                {
                    if (mInfo.Name == "get_Item")
                    {
                        //check if the signature matches the input
                        if (mInfo.GetParameters().Length == 1)
                        {
                            var getter      = mInfo;
                            var actualParms = (getter != null) ? getter.GetParameters() : null;

                            if (actualParms == null || actualParms.Length != 1)
                            {
                                translator.ThrowError(luaState, "method not found (or no indexer): " + index);
                                LuaLib.LuaPushNil(luaState);
                            }
                            else
                            {
                                // Get the index in a form acceptable to the getter
                                index = translator.GetAsType(luaState, 2, actualParms [0].ParameterType);
                                object[] args = new object[1];

                                // Just call the indexer - if out of bounds an exception will happen
                                args [0] = index;

                                try {
                                    object result = getter.Invoke(obj, args);
                                    translator.Push(luaState, result);
                                } catch (TargetInvocationException e) {
                                    // Provide a more readable description for the common case of key not found
                                    if (e.InnerException is KeyNotFoundException)
                                    {
                                        translator.ThrowError(luaState, "key '" + index + "' not found ");
                                    }
                                    else
                                    {
                                        translator.ThrowError(luaState, "exception indexing '" + index + "' " + e.Message);
                                    }

                                    LuaLib.LuaPushNil(luaState);
                                }
                            }
                        }
                    }
                }
            }

            LuaLib.LuaPushBoolean(luaState, false);
            return(2);
        }
Exemple #7
0
		static int AddLua (LuaState luaState, ObjectTranslator translator)
		{
			object obj1 = translator.GetRawNetObject (luaState, 1);
			object obj2 = translator.GetRawNetObject (luaState, 2);

			if (obj1 == null || obj2 == null) {
				translator.ThrowError (luaState, "Cannot add a nil object");
				LuaLib.LuaPushNil (luaState);
				return 1;
			}

			Type type = obj1.GetType ();
			MethodInfo opAddition = type.GetMethod ("op_Addition");

			if (opAddition == null) {
				translator.ThrowError (luaState, "Cannot add object (" + type.Name+ " does not overload the operator +)");
				LuaLib.LuaPushNil (luaState);
				return 1;
			}
			obj1 = opAddition.Invoke (obj1, new object[] { obj1, obj2 });
			translator.Push (luaState, obj1);
			return 1;
		}