Beispiel #1
0
		LuaValue Invoke(LuaVararg args)
		{
			if (!IsMethod)
				throw new LuaException("Trying to invoke a ScriptMemberWrapper that isn't a method!");

			var mi = (MethodInfo)Member;
			var pi = mi.GetParameters();

			var clrArgs = new object[pi.Length];
			var argCount = args.Count;
			for (var i = 0; i < pi.Length; i++)
			{
				if (i >= argCount)
				{
					if (!pi[i].IsOptional)
						throw new LuaException("Argument '{0}' of '{1}' is not optional.".F(pi[i].LuaDocString(), Member.LuaDocString()));

					clrArgs[i] = pi[i].DefaultValue;
					continue;
				}

				if (!args[i].TryGetClrValue(pi[i].ParameterType, out clrArgs[i]))
					throw new LuaException("Unable to convert parameter {0} to {1}".F(i, pi[i].ParameterType.Name));
			}

			var ret = (mi.Invoke(Target, clrArgs));
			return ret.ToLuaValue(context);
		}
Beispiel #2
0
            public LuaVararg RunLua(LuaFunction func, string name = "[unknown]", params LuaValue[] args)
            {
                LuaVararg ret = null;

                try {
                    func.Environment = LuaEnvironment;

                    ret = func.Call(args);
                } catch (Exception e) {
                    ETGMod.ModLoader.LuaError.Invoke(this, LuaEventMethod.Loaded, e);
                    Logger.Error(e.Message);

                    if (e is LuaException)
                    {
                        for (int i = 0; i < ((LuaException)e).TracebackArray.Length; i++)
                        {
                            Logger.ErrorIndent("  " + ((LuaException)e).TracebackArray[i]);
                        }
                    }
                    else
                    {
                        var lines = e.StackTrace.Split('\n');
                        for (int i = 0; i < lines.Length; i++)
                        {
                            Logger.ErrorIndent(lines[i]);
                        }
                    }
                }

                return(ret);
            }
Beispiel #3
0
            public string Stringify(LuaVararg args)
            {
                var s = new StringBuilder();

                for (int i = 0; i < args.Count; i++)
                {
                    s.Append(args [i].ToString());
                }

                return(s.ToString());
            }
Beispiel #4
0
        public static object Get(LuaVararg args)
        {
            Console.WriteLine($"ARGS COUNT: {args.Count} {args[0]}");

            if (args.Count < 2)
            {
                throw new LuaException("Too few arguments (need at least an object and a single index)");
            }
            var target = args[0];

            var index = new object[args.Count - 1];

            for (int i = 0; i < index.Length; i++)
            {
                index[i] = args[i + 1].CLRMappedObject;
            }
            return(target.CLRMappedType.GetProperty("Item").GetValue(target.CLRMappedObject, index));
        }
Beispiel #5
0
        public static void Set(LuaVararg args)
        {
            if (args.Count < 3)
            {
                throw new LuaException("Too few arguments (need at least an object, a single index and a value)");
            }
            var target = args[0];

            var index = new object[args.Count - 2];

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

            var property = target.CLRMappedType.GetProperty("Item");

            var value           = args[args.Count - 1].CLRMappedObject;
            var value_converted = Convert.ChangeType(value, property.PropertyType);

            property.SetValue(target.CLRMappedObject, value_converted, index);
        }
Beispiel #6
0
        LuaValue Invoke(LuaVararg args)
        {
            if (!IsMethod)
            {
                throw new LuaException("Trying to invoke a ScriptMemberWrapper that isn't a method!");
            }

            var mi = (MethodInfo)Member;
            var pi = mi.GetParameters();

            object[] clrArgs  = new object[pi.Length];
            var      argCount = args.Count;

            for (var i = 0; i < pi.Length; i++)
            {
                if (i >= argCount)
                {
                    if (!pi[i].IsOptional)
                    {
                        throw new LuaException("Argument '{0}' of '{1}' is not optional.".F(pi[i].LuaDocString(), Member.LuaDocString()));
                    }

                    clrArgs[i] = pi[i].DefaultValue;
                    continue;
                }

                if (!args[i].TryGetClrValue(pi[i].ParameterType, out clrArgs[i]))
                {
                    throw new LuaException("Unable to convert parameter {0} to {1}".F(i, pi[i].ParameterType.Name));
                }
            }

            var ret = (mi.Invoke(Target, clrArgs));

            return(ret.ToLuaValue(context));
        }
        LuaValue Invoke(LuaVararg args)
        {
            object[] clrArgs = null;
            try
            {
                if (!IsMethod)
                {
                    throw new LuaException("Trying to invoke a ScriptMemberWrapper that isn't a method!");
                }

                var mi = (MethodInfo)Member;
                var pi = mi.GetParameters();

                clrArgs = new object[pi.Length];

                var argCount = args.Count;
                for (var i = 0; i < pi.Length; i++)
                {
                    if (i >= argCount)
                    {
                        if (!pi[i].IsOptional)
                        {
                            throw new LuaException($"Argument '{pi[i].LuaDocString()}' of '{Member.LuaDocString()}' is not optional.");
                        }

                        clrArgs[i] = pi[i].DefaultValue;
                        continue;
                    }

                    if (!args[i].TryGetClrValue(pi[i].ParameterType, out clrArgs[i]))
                    {
                        throw new LuaException($"Unable to convert parameter {i} to {pi[i].ParameterType.Name}");
                    }
                }

                return(mi.Invoke(Target, clrArgs).ToLuaValue(context));
            }
            finally
            {
                // Clean up all the Lua arguments that were given to us.
                foreach (var arg in args)
                {
                    arg.Dispose();
                }
                args.Dispose();

                // If we created any arrays of LuaValues to pass around, we need to dispose those too.
                if (clrArgs != null)
                {
                    foreach (var arg in clrArgs)
                    {
                        var table = arg as LuaValue[];
                        if (table == null)
                        {
                            continue;
                        }

                        foreach (var value in table)
                        {
                            value.Dispose();
                        }
                    }
                }
            }
        }
 public virtual MethodInfo ResolveOverload(ICollection <MemberInfo> possibleOverloads, LuaVararg arguments)
 {
     throw new NotImplementedException("Overload resolution is not yet supported.");
 }
Beispiel #9
0
        LuaValue Invoke(LuaVararg args)
        {
            object[] clrArgs = null;
            try
            {
                if (!IsMethod)
                    throw new LuaException("Trying to invoke a ScriptMemberWrapper that isn't a method!");

                var mi = (MethodInfo)Member;
                var pi = mi.GetParameters();

                clrArgs = new object[pi.Length];

                var argCount = args.Count;
                for (var i = 0; i < pi.Length; i++)
                {
                    if (i >= argCount)
                    {
                        if (!pi[i].IsOptional)
                            throw new LuaException("Argument '{0}' of '{1}' is not optional.".F(pi[i].LuaDocString(), Member.LuaDocString()));

                        clrArgs[i] = pi[i].DefaultValue;
                        continue;
                    }

                    if (!args[i].TryGetClrValue(pi[i].ParameterType, out clrArgs[i]))
                        throw new LuaException("Unable to convert parameter {0} to {1}".F(i, pi[i].ParameterType.Name));
                }

                return mi.Invoke(Target, clrArgs).ToLuaValue(context);
            }
            finally
            {
                // Clean up all the Lua arguments that were given to us.
                foreach (var arg in args)
                    arg.Dispose();
                args.Dispose();

                // If we created any arrays of LuaValues to pass around, we need to dispose those too.
                if (clrArgs != null)
                {
                    foreach (var arg in clrArgs)
                    {
                        if (!(arg is LuaValue[]))
                            continue;
                        foreach (var value in (LuaValue[])arg)
                            value.Dispose();
                    }
                }
            }
        }
Beispiel #10
0
        LuaValue Invoke(LuaVararg args)
        {
            object[] clrArgs = null;
            try
            {
                if (!IsMethod)
                {
                    throw new LuaException("Trying to invoke a ScriptMemberWrapper that isn't a method !");
                }

                var methodInfo     = (MethodInfo)MemberInfo;
                var parameterInfos = methodInfo.GetParameters();

                clrArgs = new object[parameterInfos.Length];

                var argCount = args.Count;
                for (var i = 0; i < parameterInfos.Length; i++)
                {
                    if (i >= argCount)
                    {
                        if (!parameterInfos[i].IsOptional)
                        {
                            throw new LuaException("Argument '{0}' of '{1}' is not optional.".F(parameterInfos[i].LuaDocString(), MemberInfo.LuaDocString()));
                        }

                        clrArgs[i] = parameterInfos[i].DefaultValue;
                        continue;
                    }

                    if (!args[i].TryGetClrValue(parameterInfos[i].ParameterType, out clrArgs[i]))
                    {
                        throw new LuaException("Unable to convert parameter {0} to {1}".F(i, parameterInfos[i].ParameterType.Name));
                    }
                }
                return(methodInfo.Invoke(Target, clrArgs).ToLuaValue(context));
            }
            finally
            {
                //clean up all the lua arguments that were given to us
                foreach (var arg in args)
                {
                    arg.Dispose();
                }
                args.Dispose();

                if (clrArgs != null)
                {
                    foreach (var arg in clrArgs)
                    {
                        if (!(arg is LuaValue[]))
                        {
                            continue;
                        }
                        foreach (var value in (LuaValue[])arg)
                        {
                            value.Dispose();
                        }
                    }
                }
            }
        }
Beispiel #11
0
 public virtual MethodInfo ResolveOverload(ICollection<MemberInfo> possibleOverloads, LuaVararg arguments)
 {
     throw new NotImplementedException("Overload resolution is not yet supported.");
 }