コード例 #1
0
        public static void SearchSetLuaObjectEx(ScriptState luaState, string fullPath, object o, string metatableName)
        {
            var oldTop  = LuaCore.GetTop(luaState);
            var runtime = LuaRuntimePool.GetRuntime(luaState);
            var paths   = GetSearchPaths(fullPath);

            if (paths.Length == 1)
            {
                runtime.ObjectMgr.PushObjectEx(luaState, o, metatableName);
                LuaCore.SetGlobal(luaState, paths[0]);
            }
            else
            {
                LuaCore.GetGlobal(luaState, paths[0]);
                for (int i = 1; i < paths.Length - 1; i++)
                {
                    LuaCore.PushString(luaState, paths[i]);
                    LuaCore.GetTable(luaState, -2);
                }
                LuaCore.PushString(luaState, paths[paths.Length - 1]);
                runtime.ObjectMgr.PushObjectEx(luaState, o, metatableName);
                LuaCore.SetTable(luaState, -3);
            }
            LuaCore.SetTop(luaState, oldTop);
        }
コード例 #2
0
ファイル: BaseMetatable.cs プロジェクト: HarryCU/QwLua
        protected static BaseHandler GetItemHandler(ScriptState luaState, object data, bool getter)
        {
            if (data == null)
            {
                return(null);
            }
            var methodName = getter ? "get_Item" : "set_Item";

            var type    = data.GetType();
            var hasItem = ReflectionHelper.HasMethod(type, methodName, ReflectionHelper.DefBindingFlags | BindingFlags.Static);

            if (!hasItem)
            {
                return(null);
            }
            var runtime = LuaRuntimePool.GetRuntime(luaState);
            var key     = ScriptHelper.GenerateKey(type, methodName);
            var handler = runtime.ClassMgr.Cache[key] as BaseHandler;

            if (handler == null)
            {
                var method = ReflectionHelper.GetMethod(type.GetMethod(methodName));
                handler = new ItemHandler(luaState);
                handler.Initilaze(new object[] { method, getter });
                runtime.ClassMgr.Cache.Add(key, handler);
            }
            handler.ChangeInstance(data);
            return(handler);
        }
コード例 #3
0
        public static bool NewTable(ScriptState luaState, string fullPath)
        {
            var runtime   = LuaRuntimePool.GetRuntime(luaState);
            var oldTop    = LuaCore.GetTop(luaState);
            var paths     = GetSearchPaths(fullPath);
            var tableName = paths[0];

            if (paths.Length > 1)
            {
                LuaCore.GetGlobal(luaState, paths[0]);
                if (!TableNotFound(runtime, luaState, -1, paths[0]))
                {
                    return(false);
                }
                for (int i = 1; i < paths.Length - 1; i++)
                {
                    LuaCore.PushString(luaState, paths[i]);
                    LuaCore.GetTable(luaState, -2);
                    if (!TableNotFound(runtime, luaState, -1, paths[i]))
                    {
                        return(false);
                    }
                }
                tableName = paths[paths.Length - 1];
            }
            LuaCore.CreateTable(luaState);
            LuaCore.SetGlobal(luaState, tableName);
            LuaCore.SetTop(luaState, oldTop);
            return(true);
        }
コード例 #4
0
        public static object GetObject(ScriptTypes luaType, ScriptState luaState, int index)
        {
            switch (luaType)
            {
            case ScriptTypes.Boolean:
                return(LuaCore.ToBool(luaState, index));

            case ScriptTypes.Number:
                return(LuaCore.ToNumber(luaState, index));

            case ScriptTypes.String:
                return(LuaCore.ToString(luaState, index));

            case ScriptTypes.Table:
                return(new Table(luaState, index));

            case ScriptTypes.Function:
                return(new ScriptFunction(luaState, index));

            case ScriptTypes.UserData:
                var runtime = LuaRuntimePool.GetRuntime(luaState);
                return(runtime.ObjectMgr.GetObject(luaState, index));
            }
            return(null);
        }
コード例 #5
0
        public IList <object> Call(params object[] args)
        {
            var topIndex = LuaCore.GetTop(_luaState);

            var runtime = LuaRuntimePool.GetRuntime(_luaState);

            if (_func != null)
            {
                DataHelper.PushObject(_luaState, _func);
            }
            else
            {
                LuaCore.GlobalRawGet(_luaState, _reference);
            }
            foreach (var arg in args)
            {
                DataHelper.PushObject(_luaState, arg);
            }
            if (LuaCore.Call(_luaState, args.Length, -1, 0) == 0)
            {
                return(runtime.ObjectMgr.PopResults(_luaState, topIndex));
            }
            runtime.ThrowExceptionFromError(_luaState, topIndex);
            return(null);
        }
コード例 #6
0
ファイル: BaseMetatable.cs プロジェクト: HarryCU/QwLua
        protected static BaseHandler GetMember(ScriptState luaState, object o, string memberName)
        {
            var runtime = LuaRuntimePool.GetRuntime(luaState);
            var type    = o as Type;

            if (type != null)
            {
                // index(t, k) remove k param
                // call static method、field、protery
                LuaCore.RemoveValue(luaState, 1);
            }
            else
            {
                type = o.GetType();
            }

            var key     = ScriptHelper.GenerateKey(type, memberName);
            var handler = runtime.ClassMgr.Cache[key] as BaseHandler;

            if (handler == null)
            {
                #region Create Handler

                const BindingFlags flags = ReflectionHelper.DefBindingFlags | BindingFlags.Static;
                var members = type.GetMember(memberName, flags);
                if (members.Length > 0)
                {
                    var member = members[0];
                    switch (member.MemberType)
                    {
                    case MemberTypes.Field:
                        handler = new FieldHandler(luaState);
                        handler.Initilaze(new[] { ReflectionHelper.GetField((FieldInfo)member) });
                        break;

                    case MemberTypes.Property:
                        handler = new PropertyHandler(luaState);
                        handler.Initilaze(new[] { ReflectionHelper.GetProperty((PropertyInfo)member) });
                        break;

                    case MemberTypes.Method:
                        handler = new MethodHandler(luaState);
                        handler.Initilaze(new MethodFinder(type, memberName, members));
                        break;
                    }
                }
                if (handler != null)
                {
                    runtime.ClassMgr.Cache.Add(key, handler);
                }

                #endregion
            }
            if (handler != null)
            {
                handler.ChangeInstance(o);
            }
            return(handler);
        }
コード例 #7
0
        public override int Reg2Env()
        {
            var luaState = State;
            var runtime  = LuaRuntimePool.GetRuntime(luaState);
            var wrapper  = new ScriptFunctionHandler(new MethodWrapper(Finder).Invoker);

            runtime.ObjectMgr.PushObjectEx(luaState, wrapper, FunctionMetatable.GlobalName);
            return(1);
        }
コード例 #8
0
            private static int GcInternal(ScriptState luaState)
            {
                int index;
                var runtime = LuaRuntimePool.GetRuntime(luaState);

                runtime.ObjectMgr.TryGetObject(luaState, 1, out index);
                if (index >= 0)
                {
                    runtime.ObjectMgr.Remove(luaState, index);
                }
                return(0);
            }
コード例 #9
0
ファイル: MethodWrapper.cs プロジェクト: HarryCU/QwLua
        private int Call(ScriptState luaState)
        {
            var finder  = Finder;
            var runtime = LuaRuntimePool.GetRuntime(luaState);

            object instance = null;
            int    argIndex = 1;

            if (!finder.IsStatic)
            {
                instance = DataHelper.GetObject(luaState, 1);
                argIndex = 2;
            }
            var            n        = LuaCore.GetTop(luaState);
            IList <object> args     = new List <object>(argIndex);
            IList <object> argTypes = new List <object>(argIndex);

            for (; argIndex <= n; argIndex++)
            {
                var arg = DataHelper.GetObject(luaState, argIndex);
                args.Add(arg);
                argTypes.Add(arg == null ? typeof(object) : arg.GetType());
            }
            var m = finder.Search(argTypes);

            if (m == null)
            {
                string memberKey = finder.CreateKey(argTypes);
                runtime.ThrowError("未能找到" + memberKey + "的方法!");
            }
            argTypes.Clear();
            object result = null;

            try
            {
                if (m != null)
                {
                    result = m.Invoke(instance, args.ToArray());
                }
            }
            catch (Exception ex)
            {
                runtime.ThrowError(ex);
            }
            finally
            {
                args.Clear();
            }
            DataHelper.PushObject(luaState, result);
            return(1);
        }
コード例 #10
0
        public override int Reg2Env()
        {
            var luaState = State;
            var runtime  = LuaRuntimePool.GetRuntime(luaState);

            if (_getter)
            {
                var index = LuaCore.ToInt(luaState, -1);
                var val   = _method.Invoke(Instance, index);
                runtime.ObjectMgr.PushObject(luaState, val);
                return(1);
            }
            DataHelper.PushObject(luaState, null);
            return(0);
        }
コード例 #11
0
            private static int IndexInternal(ScriptState luaState)
            {
                var type          = DataHelper.GetNetObject <Type>(luaState, 1);
                var enumFieldName = LuaCore.ToString(luaState, -1);

                if (!string.IsNullOrEmpty(enumFieldName) && type != null && type.IsEnum)
                {
                    LuaCore.RemoveValue(luaState, 1);
                    var runtime = LuaRuntimePool.GetRuntime(luaState);
                    var value   = Enum.Parse(type, enumFieldName);
                    runtime.ObjectMgr.PushObject(luaState, value);
                    return(1);
                }
                LuaCore.PushNull(luaState);
                return(1);
            }
コード例 #12
0
            private static int CallInternal(ScriptState luaState)
            {
                var type = DataHelper.GetNetObject <Type>(luaState, 1);

                if (type != null)
                {
                    LuaCore.RemoveValue(luaState, 1);
                    var runtime  = LuaRuntimePool.GetRuntime(luaState);
                    var args     = GetCtorArguments(luaState);
                    int argCount = args.Count;
                    var instance = type.New(args.ToArray());
                    args.Clear();
                    if (instance == null)
                    {
                        #region Show Error

                        var builder = new StringBuilder(256);
                        builder.Append("引擎未找到");
                        builder.Append(type.FullName).Append("(");
                        int i = 0;
                        foreach (var o in args)
                        {
                            builder.Append(o.GetType().FullName);
                            if (i != argCount - 1)
                            {
                                builder.Append(",");
                            }
                            i++;
                        }
                        builder.Append(")构造函数");
                        runtime.ThrowError(builder.ToString());
                        builder.Clear();

                        #endregion
                    }
                    else
                    {
                        runtime.ObjectMgr.PushObjectEx(luaState, instance, GlobalName);
                        return(1);
                    }
                }
                LuaCore.PushNull(luaState);
                return(1);
            }
コード例 #13
0
ファイル: BaseMetatable.cs プロジェクト: HarryCU/QwLua
        protected static BaseHandler GetArrayHandler(ScriptState luaState, int index, object data, bool getter)
        {
            if (!LuaCore.RawIsLuaType(luaState, ScriptTypes.Number, index))
            {
                return(null);
            }
            var type = data as Type;

            if (type != null)
            {
                return(null);
            }
            type = data.GetType();
            var runtime    = LuaRuntimePool.GetRuntime(luaState);
            var methodName = getter ? "get_Item" : "set_Item";
            var key        = ScriptHelper.GenerateKey(type, methodName);
            var handler    = runtime.ClassMgr.Cache[key] as BaseHandler;

            if (handler == null)
            {
                var isArray = type.IsArray;
                if (!isArray)
                {
                    isArray = ReflectionHelper.HasMethod(type, methodName, ReflectionHelper.DefBindingFlags | BindingFlags.Static);
                }
                if (isArray)
                {
                    var method = ReflectionHelper.GetMethod(type.GetMethod(methodName));
                    handler = new ArrayHandler(luaState);
                    handler.Initilaze(new object[] { method, getter });
                    runtime.ClassMgr.Cache.Add(key, handler);
                }
            }
            if (handler != null)
            {
                handler.ChangeInstance(data);
            }
            return(handler);
        }
コード例 #14
0
        private int Call(ScriptState luaState)
        {
            var            runtime = LuaRuntimePool.GetRuntime(luaState);
            var            n       = LuaCore.GetTop(luaState);
            IList <object> args    = new List <object>(n);

            for (int i = 1; i <= n; i++)
            {
                args.Add(DataHelper.GetObject(luaState, i));
            }
            object result = null;

            try
            {
                result = _func(args);
            }
            catch (Exception ex)
            {
                runtime.ThrowError(ex);
            }
            DataHelper.PushObject(luaState, result);
            return(1);
        }
コード例 #15
0
        public static void PushObject(ScriptState luaState, object o)
        {
            if (o == null)
            {
                LuaCore.PushNull(luaState);
                return;
            }
            var s = o as string;

            if (s != null)
            {
                var str = s;
                LuaCore.PushString(luaState, str);
                return;
            }
            var table = o as Table;

            if (table != null)
            {
                table.Push(luaState);
                return;
            }

            var runtime   = LuaRuntimePool.GetRuntime(luaState);
            var objectMgr = runtime.ObjectMgr;

            var func = o as ScriptFunctionHandler;

            if (func != null)
            {
                objectMgr.PushObjectEx(luaState, func, FunctionMetatable.GlobalName);
                return;
            }
            var type = o as Type;

            if (type != null)
            {
                objectMgr.PushObjectEx(luaState, type, ObjectMetatable.GlobalName);
                return;
            }
            if (o is sbyte || o is byte || o is short || o is ushort ||
                o is int || o is uint || o is long || o is float ||
                o is ulong || o is decimal || o is double)
            {
                double d = Convert.ToDouble(o);
                LuaCore.PushNumber(luaState, d);
            }
            else if (o is char)
            {
                double d = (char)o;
                LuaCore.PushNumber(luaState, d);
            }
            else if (o is bool)
            {
                var b = (bool)o;
                LuaCore.PushBool(luaState, b);
            }
            else
            {
                var funcResult = o as IFunctionResult;
                if (funcResult != null)
                {
                    for (int i = 0; i < funcResult.ArgumentLength; i++)
                    {
                        PushObject(luaState, funcResult.Data[i]);
                    }
                    return;
                }
                objectMgr.PushObjectEx(luaState, o, ObjectMetatable.GlobalName);
            }
        }
コード例 #16
0
        public static T GetNetObject <T>(ScriptState luaState, int index)
        {
            var runtime = LuaRuntimePool.GetRuntime(luaState);

            return(runtime.ObjectMgr.GetObject <T>(luaState, index));
        }