Example #1
0
        /// <summary>
        /// Lua方法处理器
        /// </summary>
        /// <returns>返回值</returns>
        /// <param name="methodName">方法名称</param>
        /// <param name="arguments">参数列表</param>
        private IntPtr luaMethodHandler(string methodName, IntPtr args, int size)
        {
            if (_methodHandlers.ContainsKey(methodName))
            {
                //反序列化参数列表
                LuaObjectDecoder decoder = new LuaObjectDecoder(args, size, this);
                int argSize = decoder.readInt32();

                List <LuaValue> argumentsList = new List <LuaValue> ();
                for (int i = 0; i < argSize; i++)
                {
                    LuaValue value = decoder.readObject() as LuaValue;
                    argumentsList.Add(value);
                }

                LuaMethodHandler handler  = _methodHandlers [methodName];
                LuaValue         retValue = handler(argumentsList);

                if (retValue == null)
                {
                    retValue = new LuaValue();
                }

                LuaObjectEncoder encoder = new LuaObjectEncoder(this);
                encoder.writeObject(retValue);

                byte[] bytes = encoder.bytes;
                IntPtr retPtr;
                retPtr = Marshal.AllocHGlobal(bytes.Length);
                Marshal.Copy(bytes, 0, retPtr, bytes.Length);

                return(retPtr);
            }

            return(IntPtr.Zero);
        }
Example #2
0
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="encoder">对象编码器.</param>
        public override void serialization(LuaObjectEncoder encoder)
        {
            base.serialization(encoder);

            if (_context != null)
            {
                encoder.writeInt32(_context.objectId);
            }
            else
            {
                encoder.writeInt32(0);
            }

            encoder.writeString(_tableId);
            encoder.writeInt16((Int16)type);

            switch (type)
            {
            case LuaValueType.Number:
            {
                encoder.writeDouble(toNumber());
                break;
            }

            case LuaValueType.Integer:
            {
                encoder.writeInt32(toInteger());
                break;
            }

            case LuaValueType.String:
            {
                encoder.writeString(toString());
                break;
            }

            case LuaValueType.Data:
            {
                encoder.writeBytes(toData());
                break;
            }

            case LuaValueType.Array:
            {
                List <LuaValue> list = toArray();

                encoder.writeInt32(list.Count);
                foreach (LuaValue value in list)
                {
                    encoder.writeObject(value);
                }
                break;
            }

            case LuaValueType.Map:
            {
                Dictionary <string, LuaValue> map = toMap();
                encoder.writeInt32(map.Count);

                foreach (KeyValuePair <string, LuaValue> kv in map)
                {
                    encoder.writeString(kv.Key);
                    encoder.writeObject(kv.Value);
                }
                break;
            }

            case LuaValueType.Object:
            {
                encoder.writeObject(_value);
                break;
            }

            case LuaValueType.Boolean:
            {
                encoder.writeByte(Convert.ToByte(toBoolean()));
                break;
            }

            case LuaValueType.Function:
            {
                encoder.writeObject(toFunction());
                break;
            }

            case LuaValueType.Ptr:
            {
                encoder.writeObject(toPointer());
                break;
            }

            case LuaValueType.Tuple:
            {
                encoder.writeObject(toTuple());
                break;
            }

            default:
                break;
            }
        }