Exemplo n.º 1
0
        internal static void _PushObject(LuaState luaState, object result)
        {
            try {
                if (null == result)
                {
                    LuaLib.LuaPushNil(luaState);
                }
                else if (result is LuaBase)             // LuaFunction or LuaTable
                {
                    LuaLib.LuaGetRef(luaState, (result as LuaBase).Reference);
                }
                else if (result is LuaString)           // LuaString
                {
                    var bytes = (result as LuaString).ToBytes();
                    LuaLib.LuaPushLString(luaState, bytes, bytes.Length);
                }
                else
                {
                    var t = result.GetType();
                    if (t.IsValueType)
                    {
                        if (t == typeof(bool))          // bool
                        {
                            LuaLib.LuaPushBoolean(luaState, (bool)result);
                        }
                        else if (t.IsPrimitive)         // number
                        {
                            LuaLib.LuaPushNumber(luaState, _ConvertToDouble(result));
                        }
                        else if (t.IsEnum)              // enum

                        {
                        }
                        else                            // structure
                        {
                            var size  = Marshal.SizeOf(result);
                            var bytes = new byte[size];
                            var ptr   = Marshal.AllocHGlobal(size);
                            Marshal.StructureToPtr(result, ptr, false);
                            Marshal.Copy(ptr, bytes, 0, size);
                            Marshal.FreeHGlobal(ptr);
                            LuaLib.LuaPushLString(luaState, bytes, size);
                        }
                    }
                    else if (t == typeof(string))       // string
                    {
                        LuaLib.LuaPushString(luaState, (string)result);
                    }
                    else if (t.IsArray)
                    {
                        if ("Byte[]" == t.Name)         // byte[] will be transferred through lstring
                        {
                            var bytes = result as byte[];
                            LuaLib.LuaPushLString(luaState, bytes, bytes.Length);
                        }
                        else                            // other array type
                        {
                            LuaLib.LuaNewTable(luaState);
                            var arr = result as Array;
                            for (var i = 0; i < arr.Length; ++i)
                            {
                                LuaLib.LuaPushNumber(luaState, i + 1);
                                _PushObject(luaState, arr.GetValue(i));
                                LuaLib.LuaSetTable(luaState, -3);
                            }
                        }
                    }
                    else if (result is IDictionary)     // KeyPair<TKey, TValue> related
                    {
                        LuaLib.LuaNewTable(luaState);
                        var dict = result as IDictionary;
                        var keys = dict.Keys;
                        foreach (var key in keys)
                        {
                            _PushObject(luaState, key);
                            _PushObject(luaState, dict[key]);
                            LuaLib.LuaSetTable(luaState, -3);
                        }
                    }
                    else if (result is IEnumerable)     // Array related
                    {
                        LuaLib.LuaNewTable(luaState);
                        var enumerable = result as IEnumerable;
                        var index      = 0;
                        foreach (var v in enumerable)
                        {
                            LuaLib.LuaPushNumber(luaState, index);
                            _PushObject(luaState, v);
                            LuaLib.LuaSetTable(luaState, -3);
                            ++index;
                        }
                    }
                    else                                // user data
                    {
                        int allocId = -1;
                        if (!LuaExecuter.objects.TryGetValue(result, out allocId))
                        {
                            allocId = (++LuaExecuter.objectAllocId);
                            LuaExecuter.objects.Add(result, allocId);
                            LuaExecuter.objectRefs.Add(result, 1);
                            LuaExecuter.backObjects.Add(allocId, result);
                        }
                        else
                        {
                            LuaExecuter.objectRefs[result] += 1;
                        }

                        LuaLib.LuaNetNewUData(luaState, allocId);
                        LuaLib.LuaLGetMetatable(luaState, "luaruntime");
                        LuaLib.LuaSetMetatable(luaState, -2);
                    }
                }
            } catch (Exception e) {
                Logger <ILuaRuntime> .X(e);
            }
        }