示例#1
0
        public ParameterReader(RPCMethod method, RuntimeTypeHandle messageInfoHandle)
        {
            _appendStream      = false;
            _appendMessageInfo = false;

            var infos = method.info.GetParameters();

            _count = infos.Length;
            var handles = new RuntimeTypeHandle[_count];

            for (int i = 0; i < infos.Length; i++)
            {
                handles[i] = infos[i].ParameterType.TypeHandle;
            }

            int last = _count - 1;

            if (last >= 0)
            {
                var parameterHandle = handles[last];
                if (typeof(BitStream).TypeHandle.Equals(parameterHandle))
                {
                    _appendStream = true;
                    last--;
                }
                else if (messageInfoHandle.Equals(parameterHandle))
                {
                    _appendMessageInfo = true;
                    last--;

                    if (last >= 0 && typeof(BitStream).TypeHandle.Equals(handles[last]))
                    {
                        _appendStream = true;
                        last--;
                    }
                }
            }

            _codecs = new BitStreamCodec[last + 1];

            for (int i = 0; i <= last; i++)
            {
                var handle = handles[i];
                var codec  = BitStreamCodec.Find(handle);

                if (!(codec.deserializer != null))
                {
                    Utility.Exception("Missing Deserializer for parameter ", i, ", ", handle, ", in ", method);
                }

                _codecs[i] = codec;
            }
        }
示例#2
0
        private static void WriteParameter(BitStream stream, object parameter)
        {
            var            typeHandle = Type.GetTypeHandle(parameter);
            BitStreamCodec codec      = BitStreamCodec.Find(typeHandle);

            if (!(codec.serializer != null))
            {
                Utility.Exception("Missing Serializer for type ", typeHandle);
            }

            stream._WriteObject(codec, parameter);
        }
        /// <summary>
        /// Serializes different types of variables. Recommended for Javascript code in Unity 2.6.
        /// </summary>
        /// <param name="typeHandle">The data type for the value that will be serialized</param>
        /// <param name="value">The actual data</param>
        /// <param name="codecOptions">Optional parameters forwared to the serializer</param>
        /// <remarks>
        /// Use this function when writing to the stream and the code is javascript in Unity 2.6..
        /// Use <see cref="M:uLink.BitStream.Write``1"/> if you can use generics, the code will
        /// be easier to debug and maintain.
        /// <para>All supported data types are documented in the uLink manual in the serialization section.
        /// </para>
        /// </remarks>
        public void WriteObject(RuntimeTypeHandle typeHandle, object value, params object[] codecOptions)
        {
            //if(!(isWriting)){Utility.Exception( "Can't write when BitStream is read only");}

            BitStreamCodec codec = BitStreamCodec.Find(typeHandle);

            if (!(codec.serializer != null))
            {
                Utility.Exception("Missing Serializer for type ", typeHandle);
            }

            _WriteObject(codec, value, codecOptions);
        }
        /// <summary>
        /// Deserializes different types of variables. Recommended for Javascript code in Unity 2.6.
        /// </summary>
        /// <param name="typeHandle">The data type for this value that will be serialized</param>
        /// <param name="codecOptions">Optional parameters forwared to the deserializer</param>
        /// <remarks>
        /// Use this function when reading from the stream and the code is javascript in Unity 2.6.
        /// Use <see cref="M:uLink.BitStream.Read``1(System.Object[])"/> if you can use generics, the code will
        /// be easier to debug and maintain.
        /// <para>
        /// The supported data types are documented in the uLink manual in the serialization section.
        /// </para>
        /// </remarks>
        public object ReadObject(RuntimeTypeHandle typeHandle, params object[] codecOptions)
        {
            //if(!(isReading)){Utility.Exception( "Can't read when BitStream is write only");}

            var codec = BitStreamCodec.Find(typeHandle);

            if (!(codec.deserializer != null))
            {
                Utility.Exception("Missing Deserializer for type ", typeHandle);
            }

            return(_ReadObject(codec, codecOptions));
        }
示例#5
0
        private static void Add(RuntimeTypeHandle typeHandle, BitStreamCodec codec, bool replaceIfExists)
        {
            if (!replaceIfExists && _codecs.ContainsKey(typeHandle))
            {
#if PIKKO_BUILD
                Log.Error("Can't add BitStreamCodec because it already exists for type {0}", typeHandle);
#else
                Log.Error(NetworkLogFlags.BitStreamCodec, "Can't add BitStreamCodec because it already exists for type ", typeHandle);
#endif
                return;
            }

#if PIKKO_BUILD
            Log.Debug("Adding BitStreamCodec for type {0}", typeHandle);
#else
            Log.Debug(NetworkLogFlags.BitStreamCodec, "Adding BitStreamCodec for type ", typeHandle);
#endif

            _codecs[typeHandle] = codec;
        }
示例#6
0
        public ParameterWriter(object[] parameters)
        {
            int count = parameters.Length;

            _handles = new RuntimeTypeHandle[count];

            if (!(parameters[count - 1] != null))
            {
                Utility.Exception("Can't serialize parameter ", count - 1, " because it is null!");
            }
            _appendStream = typeof(BitStream).TypeHandle.Equals(Type.GetTypeHandle(parameters[count - 1]));
            if (_appendStream)
            {
                count--;
                _handles[count] = typeof(BitStream).TypeHandle;
            }

            _codecs = new BitStreamCodec[count];

            for (int i = 0; i < count; i++)
            {
                object param = parameters[i];

                if (!(param != null))
                {
                    Utility.Exception("Can't serialize parameter ", i, " because it is null!");
                }

                var handle = Type.GetTypeHandle(param);
                _handles[i] = handle;

                var codec = BitStreamCodec.Find(handle);
                _codecs[i] = codec;

                if (!(codec.serializer != null))
                {
                    Utility.Exception("Missing Serializer for parameter type ", handle, " with value ", param);
                }
            }
        }
示例#7
0
        private static BitStreamCodec _CreateAndAddDefaultCodec(RuntimeTypeHandle typeHandle)
        {
            BitStreamCodec codec;

            // perhaps the type's static constructor might register the codec, if it hasn't already been called.
            RuntimeHelpers.RunClassConstructor(typeHandle);
            if (_codecs.TryGetValue(typeHandle, out codec))
            {
                return(codec);
            }

            var type = Type.GetTypeFromHandle(typeHandle);

            if (type.IsEnum)
            {
                var underlyingTypeHandle = Enum.GetUnderlyingType(type).TypeHandle;

#if PIKKO_BUILD
                Log.Debug("Adding default BitStreamCodec for enum type {0} from underlying type {1}", type, underlyingTypeHandle);
#else
                Log.Debug(NetworkLogFlags.BitStreamCodec, "Adding default BitStreamCodec for enum type ", type, " from underlying type ", underlyingTypeHandle);
#endif

                codec = Find(underlyingTypeHandle);
            }
            else
            {
#if PIKKO_BUILD
                Log.Debug("Creating and adding default BitStreamCodec for type {0}", type);
#else
                Log.Debug(NetworkLogFlags.BitStreamCodec, "Creating and adding default BitStreamCodec for type ", type);
#endif

                codec = new BitStreamCodec(_CreateDefaultDeserializer(type), _CreateDefaultSerializer(type), BitStreamTypeCode.Undefined);
            }

            _codecs.Add(typeHandle, codec);
            return(codec);
        }
 public void _WriteObject(BitStreamCodec codec, object value, params object[] codecOptions)
 {
     codec.serializer(this, value, codecOptions);
 }
 public object _ReadObject(BitStreamCodec codec, params object[] codecOptions)
 {
     return(codec.deserializer(this, codecOptions));
 }