Beispiel #1
0
        public static byte[] SerializeArrayThree <T>(T[,,] map, ValueSerializationType type)
        {
            IValueSerialization serialization = ValueSerializationFactory.GetSerialization(type);
            MemoryStream        mStream       = new MemoryStream();
            int width  = map.GetLength(0);
            int height = map.GetLength(1);
            int depth  = map.GetLength(2);

            byte[] widthBytes  = BitConverter.GetBytes(width);
            byte[] heightBytes = BitConverter.GetBytes(height);
            byte[] depthBytes  = BitConverter.GetBytes(depth);
            mStream.Write(widthBytes, 0, widthBytes.Length);
            mStream.Write(heightBytes, 0, heightBytes.Length);
            mStream.Write(depthBytes, 0, depthBytes.Length);
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    for (int z = 0; z < depth; z++)
                    {
                        byte[] buffer = serialization.GetBytes(map[x, y, z]);
                        mStream.Write(buffer, 0, buffer.Length);
                    }
                }
            }
            byte[] result = mStream.ToArray();
            mStream.Close();
            mStream.Dispose();
            return(result);
        }
Beispiel #2
0
        public static T[,] DeserializeArrayTwo <T>(byte[] data, ValueSerializationType type)
        {
            IValueSerialization serialization = ValueSerializationFactory.GetSerialization(type);
            MemoryStream        mStream       = new MemoryStream(data);

            byte[] widthBytes  = new byte[4];
            byte[] heightBytes = new byte[4];
            mStream.Read(widthBytes, 0, widthBytes.Length);
            mStream.Read(heightBytes, 0, heightBytes.Length);
            int width  = BitConverter.ToInt32(widthBytes, 0);
            int height = BitConverter.ToInt32(heightBytes, 0);

            T[,] result = new T[width, height];
            for (int x = 0; x < width; x++)
            {
                for (int z = 0; z < height; z++)
                {
                    byte[] buffer = new byte[serialization.Length];
                    mStream.Read(buffer, 0, buffer.Length);
                    T value = (T)serialization.GetValue(buffer);
                    result[x, z] = value;
                }
            }
            return(result);
        }
Beispiel #3
0
        protected NPMessageNamepathData(SerializationInfo info, StreamingContext context)
        {
            Namepath = (string)info.GetValue("Namepath", typeof(string));
            ValueSerializationType vst = (ValueSerializationType)info.GetValue("vst", typeof(ValueSerializationType));
            Type vtype;

            switch (vst)
            {
            case ValueSerializationType.FastArray:
                // Reding the dimensions.
                try
                {
                    int[] dims = (int[])info.GetValue("dims", typeof(int[]));

                    // reading the value.
                    vtype = (Type)info.GetValue("Value.Type", typeof(Type));
                    Array stored = (Array)info.GetValue("Value", vtype);

                    // converting to new array.
                    Array val = Array.CreateInstance(vtype.GetElementType(), dims);
                    //Array.Copy(stored, val, stored.Length);
                    Buffer.BlockCopy(stored, 0, val, 0, stored.Length * Marshal.SizeOf(vtype.GetElementType()));
                    Value = val;
                }
                catch (Exception ex)
                {
                    throw new Exception("Error at special serialization of NPMessageNamepathInfo", ex);
                }
                break;

            case ValueSerializationType.Normal:
                vtype = (Type)info.GetValue("Value.Type", typeof(Type));
                if (vtype != null)
                {
                    Value = info.GetValue("Value", vtype);
                }
                else
                {
                    Value = null;
                }
                break;

            default:
                throw new Exception("Cannot find value serialization type.");
            }
        }
Beispiel #4
0
 public static IValueSerialization GetSerialization(ValueSerializationType type)
 {
     return(_map[(byte)type]);
 }