private static ICollection<Sc2ReplayAttribute> ParseAttributes(IMpqArchive replay)
        {
            var file = replay.ReadFile("replay.attributes.events");

            using (var stream = new MemoryStream(file))
            {
                stream.Skip(5); // there is a 5-byte header; should we validate this?

                var attributeCount = stream.ReadInt32();
                var attributes = new List<Sc2ReplayAttribute>();

                for (var i = 0; i < attributeCount; i++)
                {
                    var attr = new Sc2ReplayAttribute
                               {
                                   Header = stream.ReadInt32(),
                                   Type = (Sc2ReplayAttributeType) stream.ReadInt32(),
                                   PlayerIndex = stream.ReadByte(),
                                   Value = stream.ReadBytes(4).Reverse().ToArray(),
                               };

                    attributes.Add(attr);
                }

                return attributes;
            }
        }
Example #2
0
 public void Read(MemoryStream ms)
 {
     base.Read(ms, "MCNK");
     flags = (uint) ms.ReadInt32();
     indexX = (uint)ms.ReadInt32();
     indexY = (uint)ms.ReadInt32();
     numLayers = (uint)ms.ReadInt32();
     numDoodads = (uint)ms.ReadInt32();
     mcvtOff = (uint)ms.ReadInt32();
     mcnrOff = (uint)ms.ReadInt32();
     mclyOff = (uint)ms.ReadInt32();
     mcrfOff = (uint)ms.ReadInt32();
     mcalOff = (uint)ms.ReadInt32();
     mcalSize = (uint)ms.ReadInt32();
     mcshOff = (uint)ms.ReadInt32();
     mcshSize = (uint)ms.ReadInt32();
     areaId = (uint)ms.ReadInt32();
     numWmos = (uint)ms.ReadInt32();
     holes = (uint)ms.ReadInt32();
     texmap0 = (ulong)ms.ReadInt64();
     texmap1 = (ulong)ms.ReadInt64();
     predTex = (uint)ms.ReadInt32();
     noEffectDoodad = (uint)ms.ReadInt32();
     msceOff = (uint)ms.ReadInt32();
     numSoundEmitters = (uint)ms.ReadInt32();
     mclqOff = (uint)ms.ReadInt32();
     mclqSize = (uint)ms.ReadInt32();
     float x = ms.ReadFloat();
     float y = ms.ReadFloat();
     float z = ms.ReadFloat();
     position = new Vector3(x, y, z);
     mccvOff = (uint)ms.ReadInt32();
     ms.Read(pad, 0, pad.Length);
 }
Example #3
0
            public void Read(MemoryStream ms)
            {
                id = (uint)ms.ReadInt32();
                position.X = ms.ReadFloat(); position.Y = ms.ReadFloat(); position.Z = ms.ReadFloat();
                rotation.X = ms.ReadFloat(); rotation.Y = ms.ReadFloat(); rotation.Z = ms.ReadFloat(); rotation.W = ms.ReadFloat();
                scale = ms.ReadFloat();
                color = (uint) ms.ReadInt32();

                file = "";
            }
Example #4
0
 protected override void ParseData(MemoryStream ms)
 {
     Width = Helper.ConvertEndian(ms.ReadInt32());
     Height = Helper.ConvertEndian(ms.ReadInt32());
     BitDepth = Convert.ToByte(ms.ReadByte());
     ColorType = Convert.ToByte(ms.ReadByte());
     CompressionMethod = Convert.ToByte(ms.ReadByte());
     FilterMethod = Convert.ToByte(ms.ReadByte());
     InterlaceMethod = Convert.ToByte(ms.ReadByte());
 }
Example #5
0
 public uint Read(MemoryStream ms)
 {
     base.Read(ms, "MPHD");
     flags = (uint)ms.ReadInt32();
     ms.Read(pad, 0, pad.Length);
     return size;
 }
Example #6
0
 public virtual uint Read(MemoryStream ms, string ID)
 {
     ms.Read(id, 0, id.Length);
     size = (uint)ms.ReadInt32();
     if (ID != "")
         Util.checkChunkId(id, ID);
     return size;
 }
Example #7
0
        public void ReadInt32()
        {
            Int32 testValue = 2048;
            using( var stream = new MemoryStream() )
            {
                var bytes = BitConverter.GetBytes( testValue );
                stream.Write( bytes, 0, bytes.Length );
                stream.Seek( 0, SeekOrigin.Begin );

                var value = stream.ReadInt32();
                Assert.AreEqual( value, testValue );
            }
        }
Example #8
0
        public uint Read(MemoryStream ms)
        {
            base.Read(ms, "MOGP");
            nameIndex = (uint)ms.ReadInt32();
            descriptionIndex = (uint)ms.ReadInt32();
            flags = (uint)ms.ReadInt32();
            bboxMin.X = ms.ReadFloat(); bboxMin.Y = ms.ReadFloat(); bboxMin.Z = ms.ReadFloat();
            bboxMax.X = ms.ReadFloat(); bboxMax.Y = ms.ReadFloat(); bboxMax.Z = ms.ReadFloat();
            moprIndex = (ushort)ms.ReadShort();
            numMoprItems = (ushort)ms.ReadShort();
            numBatchesA = (ushort)ms.ReadShort();
            numBatchesB = (ushort)ms.ReadShort();
            numBatchesC = (ushort)ms.ReadShort();
            ms.Read(fogIndices, 0, fogIndices.Length);
            liquid = (uint)ms.ReadInt32();
            wmoGroupId = (uint)ms.ReadInt32();
            ms.Read(unknown, 0, unknown.Length);

            return size;
        }
Example #9
0
            public uint Read(MemoryStream ms, long data_offset = 0)
            {
                long save_point;
                infos_offset = (uint)ms.ReadInt32();
                numLayers = (uint)ms.ReadInt32();
                renderMap_offset = (uint)ms.ReadInt32();

                save_point = ms.Position;
                ms.Position = data_offset + infos_offset;
                if (infos_offset != 0 && numLayers != 0)
                {
                    for (int i = 0; i < numLayers; i++)
                    {
                        Information_s info = new Information_s();
                        info.Read(ms, data_offset);
                        infos.Add(info);
                    }
                }

                if (renderMap_offset != 0)
                {
                    ms.Position = data_offset + renderMap_offset;
                    renderMap = (ulong)ms.ReadInt64();
                }

                ms.Position = save_point;

                return 0;
            }
Example #10
0
 public uint Read(MemoryStream ms)
 {
     id = (uint)ms.ReadInt32();
     uid = (uint)ms.ReadInt32();
     pos.X = ms.ReadFloat(); pos.Y = ms.ReadFloat(); pos.Z = ms.ReadFloat();
     rot.X = ms.ReadFloat(); rot.Y = ms.ReadFloat(); rot.Z = ms.ReadFloat();
     scale = (ushort)ms.ReadShort();
     flags = (ushort)ms.ReadShort();
     return 0;
 }
Example #11
0
        public M2(mpq.Wrapper mpq_h, string name)
        {
            MemoryStream ms = new MemoryStream(mpq_h.GetFile(name));

            // read in chunk by chunk
            ms.Read(id, 0, id.Length);
            ms.Read(version, 0, id.Length);
            nameLength = (uint) ms.ReadInt32();
            nameOff = (uint)ms.ReadInt32();
            flags = (uint)ms.ReadInt32();

            ms.Read(pad0, 0, pad0.Length);

            numVertices = (uint) ms.ReadInt32();
            verticesOff = (uint)ms.ReadInt32();

            ms.Read(pad1, 0, pad1.Length);

            numBoundingTriangles = (uint)ms.ReadInt32();
            boundingTriangleOff = (uint)ms.ReadInt32();
            numBoundingVertices = (uint)ms.ReadInt32();
            boundingVerticesOff = (uint)ms.ReadInt32();
            numBoundingNormals = (uint)ms.ReadInt32();
            boundingNormalsOff = (uint)ms.ReadInt32();

            isCollide = numBoundingTriangles > 0;

            // ignore non collidable M2s
            if (!isCollide)
                return;

            // get M2 model name
            ms.Position = nameOff;
            byte[] _b = new byte[nameLength];
            ms.Read(_b, 0, (int) nameLength);
            System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
            _m2name = enc.GetString(_b);

            if (numVertices > 0)
            {
                ms.Position = verticesOff;
                for (int i = 0; i < numVertices; i++)
                {
                    Vector3 v;
                    v.X = ms.ReadFloat(); v.Y = ms.ReadFloat(); v.Z = ms.ReadFloat();
                    _vertices.Add(v);
                }
            }

            // get bounding triangles
            if (numBoundingTriangles > 0)
            {
                ms.Position = boundingTriangleOff;
                for (int i = 0; i < numBoundingTriangles; i++)
                {
                    // in the file those are 16bit, so read short
                    _boundingIndices.Add(ms.ReadShort());
                }
            }

            // get bounding vertices
            if (numBoundingVertices > 0)
            {
                ms.Position = boundingVerticesOff;
                for (int i = 0; i < numBoundingVertices; i++)
                {
                    Vector3 v;
                    v.X = ms.ReadFloat(); v.Y = ms.ReadFloat(); v.Z = ms.ReadFloat();
                    _boundingVertices.Add(v);
                }
            }

            // get bounding normals
            if (numBoundingNormals > 0)
            {
                ms.Position = boundingNormalsOff;
                for (int i = 0; i < numBoundingNormals; i++)
                {
                    Vector3 v;
                    v.X = ms.ReadFloat(); v.Y = ms.ReadFloat(); v.Z = ms.ReadFloat();
                    _boundingNormals.Add(v);
                }
            }

            bbox = BoundingBox.CreateFromPoints(_boundingVertices);
        }
Example #12
0
 public uint Read(MemoryStream ms)
 {
     base.Read(ms, "MWID");
     if (size > 0)
     {
         for (int i = 0; i < size / (sizeof(uint)); i++)
             _mwmoIndices.Add((uint)ms.ReadInt32());
     }
     return size;
 }
Example #13
0
        void parseObjectReferences(MemoryStream ms)
        {
            // get all object reference MCNKs
            for (int i = 0; i < 256; i++)
            {
                ObjectReference_s _or = new ObjectReference_s();

                // there is always a MCNK chunk first
                _or.mcnkChunk.Read(ms, "MCNK");
                long data_off = ms.Position;
                long chunk_end;
                long mcnk_end = ms.Position + _or.mcnkChunk.size ;

                while (ms.Position < mcnk_end)
                {
                    string hdr = Util.hdr(ms) ;
                    switch (hdr)
                    {
                        case "MCRD":
                            {
                                // read doodad indices
                                _or.mcrdChunk.Read(ms, "MCRD");
                                chunk_end = ms.Position + _or.mcrdChunk.size;
                                while (ms.Position < chunk_end)
                                    _or.doodadIndices.Add(ms.ReadInt32());
                            }
                            break;
                        case "MCRW":
                            {

                                // read WMO indices
                                _or.mcrwChunk.Read(ms, "MCRW");
                                chunk_end = ms.Position + _or.mcrwChunk.size;
                                while (ms.Position < chunk_end)
                                    _or.wmoIndices.Add(ms.ReadInt32());
                            }
                            break;
                        default:
                            throw new Exception("Not exptected chunk: " + hdr);
                    }
                }

                _objectRefs.Add(_or);
            }
            long left = ms.Length - ms.Position;
            if (left != 0)
                throw new Exception("still data left");
        }
        /// <summary>
        /// Resumes a rollover log
        /// </summary>
        /// <param name="fileName">the name of the log file to load.</param>
        public RolloverLogFile(string fileName)
        {
            FileName = fileName;
            SourceFiles.Clear();
            IsValid = false;

            try
            {
                byte[] data = File.ReadAllBytes(fileName);
                if (data.Length < Header.Length + 1 + 20) //Header + Version + SHA1
                {
                    Log.Publish(MessageLevel.Warning, "Failed to load file.", "Expected file length is not long enough", fileName);
                    return;
                }
                for (int x = 0; x < Header.Length; x++)
                {
                    if (data[x] != Header[x])
                    {
                        Log.Publish(MessageLevel.Warning, "Failed to load file.", "Incorrect File Header", fileName);
                        return;
                    }
                }

                byte[] hash = new byte[20];
                Array.Copy(data, data.Length - 20, hash, 0, 20);
                using (var sha = new SHA1Managed())
                {
                    var checksum = sha.ComputeHash(data, 0, data.Length - 20);
                    if (!hash.SequenceEqual(checksum))
                    {
                        Log.Publish(MessageLevel.Warning, "Failed to load file.", "Hashsum failed.", fileName);
                        return;
                    }
                }

                var stream = new MemoryStream(data);

                stream.Position = Header.Length;

                int version = stream.ReadNextByte();
                switch (version)
                {
                    case 1:
                        int count = stream.ReadInt32();
                        while (count > 0)
                        {
                            count--;
                            SourceFiles.Add(stream.ReadGuid());
                        }
                        DestinationFile = stream.ReadGuid();
                        IsValid = true;
                        return;
                    default:
                        Log.Publish(MessageLevel.Warning, "Failed to load file.", "Version Not Recgonized.", fileName);
                        SourceFiles.Clear();
                        return;
                }
            }
            catch (Exception ex)
            {
                Log.Publish(MessageLevel.Warning, "Failed to load file.", "Unexpected Error", fileName, ex);
                SourceFiles.Clear();
                return;
            }
        }
Example #15
0
        public static byte[] Decompress(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
                throw new ArgumentNullException();
            if (count < 0)
                throw new FormatException();
            if (offset + count > buffer.Length)
                throw new IndexOutOfRangeException();

            using (MemoryStream buffStream = new MemoryStream(buffer, offset, count))
            {
                InflaterInputStream zipStream;
                uint magicStream = buffStream.ReadUInt32();
                if (magicStream != magic)
                {
                    throw new InvalidDataException("found an invalid zlib block");
                }

                uint buffMaxSegmentSize = buffStream.ReadUInt32();
                if (buffMaxSegmentSize != maxSegmentSize)
                {
                    throw new FormatException();
                }

                uint totComprSize = buffStream.ReadUInt32();
                uint totUncomprSize = buffStream.ReadUInt32();

                byte[] outputBuffer = new byte[totUncomprSize];
                int numOfSegm = (int)Math.Ceiling((double)totUncomprSize / (double)maxSegmentSize);
                int headSegm = 16;
                int dataSegm = headSegm + (numOfSegm * 8);
                int buffOff = 0;

                for (int i = 0; i < numOfSegm; i++)
                {
                    buffStream.Seek(headSegm, SeekOrigin.Begin);
                    int comprSegm = buffStream.ReadInt32();
                    int uncomprSegm = buffStream.ReadInt32();
                    headSegm = (int)buffStream.Position;

                    buffStream.Seek(dataSegm, SeekOrigin.Begin);
                    //Console.WriteLine("compr size: {0}, uncompr size: {1}, data offset: 0x{2:X8}", comprSegm, uncomprSegm, dataSegm);
                    zipStream = new InflaterInputStream(buffStream);
                    zipStream.Read(outputBuffer, buffOff, uncomprSegm);
                    zipStream.Flush();
                    buffOff += uncomprSegm;
                    dataSegm += comprSegm;
                }
                buffStream.Close();
                return outputBuffer;
            }
        }
Example #16
0
 public void Read(MemoryStream ms)
 {
     flags = (uint)ms.ReadInt32();
     specularMode = (uint)ms.ReadInt32();
     blendMode = (uint)ms.ReadInt32();
     texture0 = (uint)ms.ReadInt32();
     color0 = (uint)ms.ReadInt32();
     flags0 = (uint)ms.ReadInt32();
     texture1 = (uint)ms.ReadInt32();
     color1 = (uint)ms.ReadInt32();
     flags1 = (uint)ms.ReadInt32();
     color2 = (uint)ms.ReadInt32();
     ms.Read(pad, 0, pad.Length);
 }
Example #17
0
 public uint Read(MemoryStream ms)
 {
     base.Read(ms, "");
     flags = (uint) ms.ReadInt32();
     mcinOff = (uint) ms.ReadInt32();
     mtexOff = (uint) ms.ReadInt32();
     mmdxOff = (uint) ms.ReadInt32();
     mmidOff = (uint) ms.ReadInt32();
     mwmoOff = (uint) ms.ReadInt32();
     mwidOff = (uint) ms.ReadInt32();
     mddfOff = (uint) ms.ReadInt32();
     modfOff = (uint) ms.ReadInt32();
     mfboOff = (uint) ms.ReadInt32();
     mh2oOff = (uint) ms.ReadInt32();
     mftxOff = (uint) ms.ReadInt32();
     ms.Read(pad, 0, pad.Length);
     return size;
 }
Example #18
0
 public uint Read(MemoryStream ms)
 {
     base.Read(ms, "MOHD");
     numMaterials = (uint)ms.ReadInt32();
     numGroups = (uint)ms.ReadInt32();
     numPortals = (uint)ms.ReadInt32();
     numLights = (uint)ms.ReadInt32();
     numModels = (uint)ms.ReadInt32();
     numDoodads = (uint)ms.ReadInt32();
     numSets = (uint)ms.ReadInt32();
     ambientColor = (uint)ms.ReadInt32();
     wmoId = (uint)ms.ReadInt32();
     bboxMin.X = ms.ReadFloat(); bboxMin.Y = ms.ReadFloat(); bboxMin.Z = ms.ReadFloat();
     bboxMax.X = ms.ReadFloat(); bboxMax.Y = ms.ReadFloat(); bboxMax.Z = ms.ReadFloat();
     liquidType = (uint)ms.ReadInt32();
     return size;
 }
Example #19
0
 public void Read(MemoryStream ms)
 {
     flags = (uint)ms.ReadInt32();
     bboxMin.X = ms.ReadFloat(); bboxMin.Y = ms.ReadFloat(); bboxMin.Z = ms.ReadFloat();
     bboxMax.X = ms.ReadFloat(); bboxMax.Y = ms.ReadFloat(); bboxMax.Z = ms.ReadFloat();
     nameOffset = ms.ReadInt32();
 }
Example #20
0
 public void Read(MemoryStream ms)
 {
     byte[] name = new byte[20];
     System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
     ms.Read(name, 0, name.Length);
     setName = enc.GetString(name);
     firstIndex = (uint) ms.ReadInt32();
     numDoodads = (uint)ms.ReadInt32();
     unknown = (uint)ms.ReadInt32();
 }
Example #21
0
                public uint Read(MemoryStream ms, long data_offset = 0)
                {
                    type = (ushort)ms.ReadShort();
                    flags = (ushort)ms.ReadShort();
                    heightLevel1 = ms.ReadFloat();
                    heightLevel2 = ms.ReadFloat();
                    xOffset = (byte)ms.ReadByte();
                    yOffset = (byte)ms.ReadByte();
                    width = (byte)ms.ReadByte();
                    height = (byte)ms.ReadByte();
                    mask2_offset = (uint)ms.ReadInt32();
                    heightMap_offset = (uint)ms.ReadInt32();

                    long save_point = ms.Position;
                    if (mask2_offset != 0 && height > 0)
                    {
                        ms.Position = data_offset + mask2_offset;
                        mask2 = new List<byte>(sizeof(byte) * height);
                        for (int i = 0; i < height; i++)
                            mask2.Add((byte)ms.ReadByte());
                    }

                    if (heightMap_offset != 0 && width * height > 0)
                    {
                        ms.Position = data_offset + heightMap_offset;
                        heightMap = new List<float>(width * height);
                        for (int i = 0; i < width * height; i++)
                            heightMap.Add(ms.ReadFloat());
                    }
                    ms.Position = save_point;

                    return 0;
                }
Example #22
0
        void PCCObjectHelper(MemoryStream tempStream, string filePath)
        {
            tempStream.Seek(0, SeekOrigin.Begin);
            Names = new List<string>();
            Imports = new List<ImportEntry>();
            Exports = new List<ExportEntry>();

            if (GameVersion != 3)
            {
                // Find ME1 and 2 header...Why so difficult ME1 and 2?
                tempStream.Seek(12, SeekOrigin.Begin);
                int tempNameSize = tempStream.ReadInt32();
                tempStream.Seek(64 + tempNameSize, SeekOrigin.Begin);
                int tempGenerator = tempStream.ReadInt32();
                tempStream.Seek(36 + tempGenerator * 12, SeekOrigin.Current);
                int tempPos = (int)tempStream.Position + (GameVersion == 2 ? 0 : 4);
                tempStream.Seek(0, SeekOrigin.Begin);
                header = tempStream.ReadBytes(tempPos);
                tempStream.Seek(0, SeekOrigin.Begin);
            }
            else
                header = tempStream.ReadBytes(headerSize);

            if (magic != ZBlock.magic &&
                    magic.Swap() != ZBlock.magic)
                throw new FormatException(filePath + " is not a pcc file");

            if (GameVersion == 3 && lowVers != 684 && highVers != 194)
                throw new FormatException("unsupported version");

            if (compressed)
            {
                if (GameVersion == 3)
                    ReadCompressedME3(tempStream);
                else
                    ReadCompressedME1And2(tempStream);

                compressed = false;
            }
            else
                listsStream = tempStream;

            //Fill name list
            listsStream.Seek(NameOffset, SeekOrigin.Begin);
            for (int i = 0; i < NameCount; i++)
            {
                int strLength = listsStream.ReadInt32();
                string name = null;

                switch (GameVersion)
                {
                    case 1:
                        name = ReadME1Name(strLength);
                        break;
                    case 2:
                        name = ReadME2Name(strLength);
                        break;
                    case 3:
                        name = ReadME3Name(strLength);
                        break;
                }

                Names.Add(name);               
            }

            byte[] buffer = null;

            // fill import list
            listsStream.Seek(ImportOffset, SeekOrigin.Begin);
            buffer = new byte[ImportEntry.byteSize];
            for (int i = 0; i < ImportCount; i++)
                Imports.Add(new ImportEntry(this, listsStream));

            //fill export list
            listsStream.Seek(ExportOffset, SeekOrigin.Begin);
            for (int i = 0; i < ExportCount; i++)
            {
                uint expInfoOffset = (uint)listsStream.Position;

                // Find export data size
                int count = 0;
                int expInfoSize = 0;
                if (GameVersion == 3)
                {
                    listsStream.Seek(44, SeekOrigin.Current);
                    count = listsStream.ReadInt32();
                    expInfoSize = 68 + (count * 4);
                }
                else
                {
                    listsStream.Seek(40, SeekOrigin.Current);
                    count = listsStream.ReadInt32();
                    listsStream.Seek(4 + count * 12, SeekOrigin.Current);
                    count = listsStream.ReadInt32();
                    listsStream.Seek(4 + count * 4, SeekOrigin.Current);
                    listsStream.Seek(16, SeekOrigin.Current);
                    expInfoSize = (int)(listsStream.Position - expInfoOffset);
                }
                
                // Read export data
                buffer = new byte[expInfoSize];
                listsStream.Seek(expInfoOffset, SeekOrigin.Begin);
                listsStream.Read(buffer, 0, buffer.Length);
                Exports.Add(new ExportEntry(this, buffer, expInfoOffset));
            }
        }
Example #23
0
        private void ParseData( GetDataEventArgs args )
        {
            try
            {
                PacketTypes packet = args.MsgID;
                using (var data = new MemoryStream(args.Msg.readBuffer, args.Index, args.Length))
                {
                    TSPlayer player = TShock.Players[args.Msg.whoAmI];
                    var name = player.Name;
                    if (player.IsLoggedIn)
                    {
                        name = player.UserAccountName;
                    }
                    switch (packet)
                    {
                        case PacketTypes.Tile:
                            {
                                byte type = data.ReadInt8();
                                int x = data.ReadInt32();
                                int y = data.ReadInt32();
                                bool fail = true;
                                Action act;

                if (type == 0 || type == 2 || type == 4)
                                    act = Action.BREAK;
                                else if (type == 1 || type == 3)
                                    act = Action.PLACE;
                                else
                                    act = Action.ERROR;

                                byte tileType = 0;

                                if (act == Action.BREAK)
                                {
                                    tileType = Main.tile[x, y].type;
                                    fail = data.ReadBoolean();
                                }
                                else if (act == Action.PLACE)
                                {
                                    tileType = data.ReadInt8();
                                    fail = false;
                                }
                                if (act != Action.ERROR && !fail)
                                {
                                    TileEvent evt = new TileEvent(x, y, name, player.IP, act, tileType,
                                                                  LogTile.helper.GetTime());
                                    queue.Enqueue(evt);
                                }
                                break;
                            }
                        case PacketTypes.TileKill:
                            {
                                int x = data.ReadInt32();
                                int y = data.ReadInt32();
                                TileEvent evt = new TileEvent(x, y, name, player.IP, Action.BREAK, 0x15,
                                                              LogTile.helper.GetTime());
                                queue.Enqueue(evt);
                                break;
                            }
            //case PacketTypes.ChestOpen:
            //  {
            //    int chestID = data.ReadInt16();
            //    int x = data.ReadInt32();
            //    int y = data.ReadInt32();
            //    int curChest = 0;
            //    if (!chestMap.TryGetValue(player, out curChest)) // chest being opened
            //    {
            //      //Console.WriteLine( string.Format( "Open:{0}: cId:{1}:", player.Name, chestID ) );
            //      if ( chestID > 0 )  // sometimes, this gets called right after a close
            //      {
            //        chestMap.Add(player, chestID);
            //        itemMap.Add(player, Main.chest[chestID].item);
            //      } // if
            //    }
            //    else // chest is being closed
            //    {
            //      //Console.WriteLine( "Closing: " + player.Name );
            //      chestMap.Remove(player);
            //      itemMap.Remove(player);
            //    }

            //    break;
            //  }
            //case PacketTypes.ChestItem:
            //  {
            //    int chestID = data.ReadInt16();
            //    byte itemSlot = data.ReadInt8();
            //    byte stack = data.ReadInt8();
            //    int curChest = 0;
            //    int type = itemMap[player][itemSlot].type;
            //    if (LogTile.enableDebugOutput)									Console.WriteLine(type);
            //    Item[] curItems = Main.chest[chestID].item;
            //    if (LogTile.enableDebugOutput)									Console.WriteLine(curItems[itemSlot].type);
            //    //Console.WriteLine( "Chest item: " + player.Name );
            //    itemMap.Remove(player);
            //    itemMap.Add(player, curItems);
            //    break;
            //  }
            //case PacketTypes.ChestGetContents:
            //  {
            //    int x = data.ReadInt32();
            //    int y = data.ReadInt32();
            //    if (LogTile.enableDebugOutput)
            //      Console.WriteLine("GETChestContents: (" + x + ", " + y + ")");
            //    break;
            //  }
            //case PacketTypes.SignNew:
            //  {
            //    int id = data.ReadInt16();
            //    int x = data.ReadInt32();
            //    int y = data.ReadInt32();
            //    string text = data.ReadString();
            //    break;
            //  }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine( e.Message + " (" + e.StackTrace + ")" );
            }
        }
Example #24
0
        void ReadCompressedME3(MemoryStream tempStream)
        {
            List<Block> blockList = null;

            // seeks the blocks info position
            tempStream.Seek(idxOffsets + 60, SeekOrigin.Begin);
            int generator = tempStream.ReadInt32();
            tempStream.Seek((generator * 12) + 20, SeekOrigin.Current);

            int blockCount = tempStream.ReadInt32();
            blockList = new List<Block>();

            // creating the Block list
            for (int i = 0; i < blockCount; i++)
            {
                Block temp = new Block();
                temp.uncOffset = tempStream.ReadInt32();
                temp.uncSize = tempStream.ReadInt32();
                temp.cprOffset = tempStream.ReadInt32();
                temp.cprSize = tempStream.ReadInt32();
                blockList.Add(temp);
            }

            // correcting the header, in case there's need to be saved
            Buffer.BlockCopy(BitConverter.GetBytes((int)0), 0, header, header.Length - 12, sizeof(int));
            tempStream.Read(header, header.Length - 8, 8);
            headerEnd = (int)tempStream.Position;

            // copying the extraNamesList
            int extraNamesLenght = blockList[0].cprOffset - headerEnd;
            if (extraNamesLenght > 0)
            {
                extraNamesList = new byte[extraNamesLenght];
                tempStream.Read(extraNamesList, 0, extraNamesLenght);
            }

            int dataStart = 0;
            using (MemoryStream he = new MemoryStream(header))
            {
                he.Seek(0, SeekOrigin.Begin);
                he.ReadInt32();
                he.ReadInt32();
                dataStart = he.ReadInt32();
            }

            //Decompress ALL blocks
            listsStream = new MemoryStream();
            for (int i = 0; i < blockCount; i++)
            {
                tempStream.Seek(blockList[i].cprOffset, SeekOrigin.Begin);
                listsStream.Seek(blockList[i].uncOffset, SeekOrigin.Begin);
                listsStream.WriteBytes(ZBlock.Decompress(tempStream, blockList[i].cprSize));
            }
        }
Example #25
0
 public void Read(MemoryStream ms)
 {
     startVertex = (ushort)ms.ReadShort();
     numVertices = (ushort)ms.ReadShort();
     normal.X = ms.ReadFloat(); normal.Y = ms.ReadFloat(); normal.Z = ms.ReadFloat();
     unknown = (uint)ms.ReadInt32();
 }
		//-------------------------------------------------------------------------------------
		private static void UnPackArray(ref object arr, byte[] data, Type elemType, uint owner, DeserContext cox)
		{
			MemoryStream ms = null;
			try
			{
				ms = new MemoryStream(data);
				int[] dims = new int[ms.ReadByte()];
				for(int a = 0; a < dims.Length; a++)
					dims[a] = ms.ReadInt32();

				arr = Array.CreateInstance(elemType, dims);

				//bool fullDeser = true;

				if(IsPrimitive(elemType))
				{
					#region
					uint size = (uint)SizeOfPrimitive(elemType);
					ArrayIndexEnumerator aie = new ArrayIndexEnumerator((Array)arr);
					while(aie.MoveNext())
						((Array)arr).SetValue(FromBytes(elemType, ms.ReadBytes(size)), aie.Current);
					#endregion
				}
				else if(elemType == typeof(String) || elemType == typeof(RefString))
				{
					#region
					byte b;
					ArrayIndexEnumerator aie = new ArrayIndexEnumerator((Array)arr);
					while(aie.MoveNext())
					{
						using(MemoryStream line = new MemoryStream())
							while(true)
							{
								b = (byte)ms.ReadByte();
								if(b == 0xFE)
									((Array)arr).SetValue(null, aie.Current);
								else if(b == 0xFF)
									((Array)arr).SetValue(FromBytes(elemType, line.ToArray()), aie.Current);
								else
									line.WriteByte(b);
								if(b >= 0xFE)
									break;
							}
					}
					#endregion
				}
				else if(elemType == typeof(Type))
				{
					#region
					ArrayIndexEnumerator aie = new ArrayIndexEnumerator((Array)arr);
					while(aie.MoveNext())
					{
						ushort typeid = ms.ReadUInt16();
						if(typeid != 0)
							((Array)arr).SetValue(cox.types[typeid], aie.Current);
					}
					#endregion
				}
				else
				{
					ArrayIndexEnumerator aie = new ArrayIndexEnumerator((Array)arr);
					while(aie.MoveNext())
					{
						ushort typeid = ms.ReadUInt16();
						if(typeid == 0)
							continue;
						Type t = cox.types[typeid];

						if(t.IsArray)
						{
							object val = null;
							uint len = ms.ReadUInt32();
							UnPackArray(ref val, ms.ReadBytes(len), t.GetElementType(), owner, cox);
							((Array)arr).SetValue(val, aie.Current);
						}
						else if(typeid < 25 || t.IsEnum)
							((Array)arr).SetValue(FromBytes(t, ms.ReadBytes((uint)SizeOfPrimitive(t))), aie.Current);
						else if(t == typeof(String) || t == typeof(RefString))
						{
							#region
							using(MemoryStream line = new MemoryStream())
							{
								byte b;
								while(true)
								{
									b = (byte)ms.ReadByte();
									if(b == 0xFE)
										((Array)arr).SetValue(null, aie.Current);
									else if(b == 0xFF)
										((Array)arr).SetValue(FromBytes(t, line.ToArray()), aie.Current);
									else
										line.WriteByte(b);
									if(b >= 0xFE)
										break;
								}
							}
							#endregion
						}
						else if(typeof(ISelfSerialization).IsAssignableFrom(t))
						{
							#region
							object val = FormatterServices.GetUninitializedObject(t);
							TypeSerializationWrap tw = TypeSerializationWrap.GetTypeSerializationWrap(t);
							tw.OnDeserializing(val);

							byte len = (byte)ms.ReadByte();
							if(len > 0)
								((ISelfSerialization)val).Deserialize(ms.ReadBytes(len));
							
							tw.OnDeserialized(val);

							((Array)arr).SetValue(val, aie.Current); 
							#endregion
						}
						else if(t == typeof(Type))
							((Array)arr).SetValue(cox.types[ms.ReadUInt16()], aie.Current);
						else
						{
							uint id = ms.ReadUInt32();

							DeserObj dId;
							if(cox.objs.TryGetValue(id, out dId))
							{
								if(dId.WaitsCount == 0)
									((Array)arr).SetValue(dId.Obj, aie.Current);
								else
								{
									cox.objs[owner].WaitsCount++;
									List<LateDeserInfo> lll;
									if(cox.lateSet.TryGetValue(id, out lll) == false)
										cox.lateSet.Add(id, lll = new List<LateDeserInfo>());
									lll.Add(new LateDeserInfo(owner, ((Array)arr), (int[])aie.Current.Clone()));
								}
							}
							else
							{
								// Противоречит "глубокой" сериализации
								//if(typeof(GlobalObject).IsAssignableFrom(t))
								// throw new Exception("Попытка создать GlobalObject вне GOL!");
								cox.objs.Add(id, new DeserObj(t));
								cox.objs[owner].WaitsCount++;
								List<LateDeserInfo> lll;
								if(cox.lateSet.TryGetValue(id, out lll) == false)
									cox.lateSet.Add(id, lll = new List<LateDeserInfo>());
								lll.Add(new LateDeserInfo(owner, ((Array)arr), (int[])aie.Current.Clone()));
							}
						}
					}
				}
			}
			catch
			{
				throw;
			}
			finally
			{
				if(ms != null)
					ms.Dispose();
			}
		}
Example #27
0
                internal LeaderboardEntry( CMsgClientLBSGetLBEntriesResponse.Entry entry )
                {
                    GlobalRank = entry.global_rank;
                    Score = entry.score;
                    SteamID = new SteamID( entry.steam_id_user );
                    UGCId = new UGCHandle( entry.ugc_id );

                    var details = new List<int>();

                    using ( var stream = new MemoryStream( entry.details ) )
                    {
                        while ( ( stream.Length - stream.Position ) >= sizeof( int ) )
                        {
                            details.Add( stream.ReadInt32() );
                        }
                    }

                    Details = new ReadOnlyCollection<int>( details );
                }
Example #28
0
 public uint Read(MemoryStream ms)
 {
     id = (uint)ms.ReadInt32();
     uid = (uint)ms.ReadInt32();
     pos.X = ms.ReadFloat(); pos.Y = ms.ReadFloat(); pos.Z = ms.ReadFloat();
     rot.X = ms.ReadFloat(); rot.Y = ms.ReadFloat(); rot.Z = ms.ReadFloat();
     bbmin.X = ms.ReadFloat(); bbmin.Y = ms.ReadFloat(); bbmin.Z = ms.ReadFloat();
     bbmax.X = ms.ReadFloat(); bbmax.Y = ms.ReadFloat(); bbmax.Z = ms.ReadFloat();
     flags = (ushort)ms.ReadShort();
     doodadSet = (ushort)ms.ReadShort();
     nameSet = (ushort)ms.ReadShort();
     padding = (ushort)ms.ReadShort();
     if (doodadSet != 0)
     {
         int i = 0;
     }
     return 0;
 }
        public void Test4()
        {
            MemoryStream ms = new MemoryStream();
            BlockAllocatedMemoryStream ms2 = new BlockAllocatedMemoryStream();

            for (int x = 0; x < 10000; x++)
            {
                int value = Random.Int32;
                ms.Write(value);
                ms.Write((byte)value);
                ms2.Write(value);
                ms2.Write((byte)value);
            }

            for (int x = 0; x < 10000; x++)
            {
                long position = Random.Int64Between(0, ms.Length - 5);
                ms.Position = position;
                ms2.Position = position;

                if (ms.ReadInt32() != ms2.ReadInt32())
                    throw new Exception();
                if (ms.ReadNextByte() != ms2.ReadNextByte())
                    throw new Exception();
            }

            for (int x = 0; x < 10000; x++)
            {
                byte[] buffer1 = new byte[100];
                byte[] buffer2 = new byte[100];

                long position = Random.Int64Between(0, (long)(ms.Length * 1.1));
                int readLength = Random.Int32Between(0, 100);
                ms.Position = position;
                ms2.Position = position;

                if (ms.Read(buffer1, 99 - readLength, readLength) != ms2.Read(buffer2, 99 - readLength, readLength))
                {
                    CompareBytes(buffer1, buffer2);
                }

            }

            Compare(ms, ms2);
        }
Example #30
0
 public void Read(MemoryStream ms)
 {
     flags = (uint)ms.ReadInt32();
     pad = (uint)ms.ReadInt32();
 }