public bool Extract(DataArchiveFile file, string destinationDirectory)
        {
            if (!_loaded)
            {
                Logger.Error($"Archive is not loaded");
                return(false);
            }

            if (!Directory.Exists(destinationDirectory))
            {
                Logger.Error($"Directory not found: {destinationDirectory}");
                return(false);
            }

            DirectoryInfo destination = new DirectoryInfo(destinationDirectory);

            byte[] fileData = _sacBuffer.GetBytes(file.Offset, file.Size);
            string dir      = Path.Combine(destination.FullName, file.Path);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            string path = Path.Combine(dir, file.Name);

            File.WriteAllBytes(path, fileData);

            Logger.Info($"Extract: {file.Id} {file.Path}{file.Name}");

            return(true);
        }
Example #2
0
        public HdrArchive Read(string sourcePath)
        {
            byte[]         hdrFile          = ReadFile(sourcePath);
            IBuffer        buffer           = BufferProvider.Provide(hdrFile);
            HdrHeader      header           = ReadHeader(buffer);
            List <HdrFile> files            = new List <HdrFile>();
            int            folderIndexStart = header.IndexOffset;
            int            totalFiles       = 0;
            int            currentFile      = 0;

            for (int i = 0; i < header.FolderCount; i++)
            {
                buffer.Position = folderIndexStart + i * IndexBlockSize;
                HdrIndex folderIndex = ReadIndex(buffer);
                buffer.Position = folderIndex.Offset;
                for (int j = 0; j < folderIndex.Length; j++)
                {
                    HdrIndex fileIndex = ReadIndex(buffer);
                    int      offset    = fileIndex.Offset;
                    int      lenght    = fileIndex.Length;
                    string   ext;
                    if (Path.HasExtension(fileIndex.Name))
                    {
                        ext = Path.GetExtension(fileIndex.Name);
                        switch (ext)
                        {
                        case ".pts":
                            // PTS-Files are PNG files with the first 4byte beeing lengt of the data.
                            // We already know the length from the index.
                            // offset += 4;
                            // lenght -= 4;
                            break;
                        }
                    }
                    else
                    {
                        ext = "";
                    }
                    HdrFile file = new HdrFile();
                    file.FileExtension    = ext;
                    file.FileName         = fileIndex.Name;
                    file.HdrDirectoryPath = folderIndex.Name;
                    file.HdrFullPath      = folderIndex.Name + fileIndex.Name;
                    file.Data             = buffer.GetBytes(offset, lenght);
                    file.Offset           = offset;
                    file.Length           = lenght;
                    file.Extension        = ext;
                    files.Add(file);
                    currentFile++;
                }
                totalFiles += folderIndex.Length;
                OnProgressChanged(totalFiles, currentFile);
            }
            return(new HdrArchive(files, header));
        }
Example #3
0
        public byte[] Write(NecPacket packet, NecClient client)
        {
            // TODO update arrowgene service to write uint*

            byte[]  data     = packet.Data.GetAllBytes();
            IBuffer buffer   = BufferProvider.Provide();
            ulong   dataSize = (ushort)(data.Length + PacketIdSize);

            PacketLengthType packetLengthType;

            if (dataSize < byte.MaxValue)
            {
                packetLengthType = PacketLengthType.Byte;
                buffer.WriteByte((byte)packetLengthType);
                buffer.WriteByte((byte)dataSize);
            }
            else if (dataSize < ushort.MaxValue)
            {
                packetLengthType = PacketLengthType.UInt16;
                buffer.WriteByte((byte)packetLengthType);
                buffer.WriteInt16((ushort)dataSize);
            }
            else if (dataSize < uint.MaxValue)
            {
                packetLengthType = PacketLengthType.UInt32;
                buffer.WriteByte((byte)packetLengthType);
                buffer.WriteInt32((uint)dataSize);
            }
            else
            {
                _logger.Error($"{dataSize} to big");
                return(null);
            }

            buffer.WriteInt16(packet.Id);
            buffer.WriteBytes(data);

            byte headerSize = CalculateHeaderSize(packetLengthType);

            packet.Header = buffer.GetBytes(0, headerSize);

            return(buffer.GetAllBytes());
        }
        public byte[] Write(NecPacket packet)
        {
            byte[]  data   = packet.Data.GetAllBytes();
            IBuffer buffer = BufferProvider.Provide();

            PacketType packetType;

            switch (packet.PacketType)
            {
            case PacketType.HeartBeat:
                packetType = PacketType.HeartBeat;
                buffer.WriteByte((byte)packetType);
                buffer.WriteBytes(data);
                break;

            case PacketType.Unknown1:
                packetType = PacketType.Unknown1;
                buffer.WriteByte((byte)packetType);
                buffer.WriteBytes(data);
                break;

            case PacketType.Disconnect:
                packetType = PacketType.Disconnect;
                buffer.WriteByte((byte)packetType);
                buffer.WriteBytes(data);
                break;

            default:
                ulong dataSize = (ulong)(data.Length + PacketIdSize);
                if (dataSize < byte.MaxValue)
                {
                    packetType = PacketType.Byte;
                    buffer.WriteByte((byte)packetType);
                    buffer.WriteByte((byte)dataSize);
                }
                else if (dataSize < ushort.MaxValue)
                {
                    packetType = PacketType.UInt16;
                    buffer.WriteByte((byte)packetType);
                    buffer.WriteUInt16((ushort)dataSize);
                }
                else if (dataSize < uint.MaxValue)
                {
                    packetType = PacketType.UInt32;
                    buffer.WriteByte((byte)packetType);
                    buffer.WriteUInt32((uint)dataSize);
                }
                else
                {
                    Logger.Error($"{dataSize} to big");
                    return(null);
                }

                buffer.WriteUInt16(packet.Id);
                buffer.WriteBytes(data);
                break;
            }

            byte headerSize = CalculateHeaderSize(packetType);

            packet.Header = buffer.GetBytes(0, headerSize);

            return(buffer.GetAllBytes());
        }
        public List <NecPacket> Read(byte[] data, ServerType serverType)
        {
            List <NecPacket> packets = new List <NecPacket>();

            if (_buffer == null)
            {
                _buffer = BufferProvider.Provide(data);
            }
            else
            {
                _buffer.SetPositionEnd();
                _buffer.WriteBytes(data);
            }

            _buffer.Position = _position;

            bool read = true;

            while (read)
            {
                read = false;

                if (!_readPacketLengthType &&
                    _buffer.Size - _buffer.Position > PacketLengthTypeSize)
                {
                    byte lengthType = _buffer.ReadByte();
                    if (!Enum.IsDefined(typeof(PacketType), lengthType))
                    {
                        Logger.Error($"PacketType: '{lengthType}' not found");
                        Reset();
                        return(packets);
                    }

                    _readPacketLengthType = true;
                    _packetType           = (PacketType)lengthType;
                    _headerSize           = CalculateHeaderSize(_packetType);
                }

                if (_readPacketLengthType &&
                    !_readHeader &&
                    _buffer.Size - _buffer.Position >= _headerSize - PacketLengthTypeSize)
                {
                    // TODO acquire 1st byte differently in case -1 doesnt work
                    _header = _buffer.GetBytes(_buffer.Position - PacketLengthTypeSize, _headerSize);

                    switch (_packetType)
                    {
                    case PacketType.Byte:
                    {
                        _dataSize   = _buffer.ReadByte();
                        _dataSize  -= PacketIdSize;
                        _id         = _buffer.ReadUInt16();
                        _readHeader = true;
                        break;
                    }

                    case PacketType.UInt16:
                    {
                        _dataSize   = _buffer.ReadUInt16();
                        _dataSize  -= PacketIdSize;
                        _id         = _buffer.ReadUInt16();
                        _readHeader = true;
                        break;
                    }

                    case PacketType.UInt32:
                    {
                        _dataSize   = _buffer.ReadUInt32();
                        _dataSize  -= PacketIdSize;
                        _id         = _buffer.ReadUInt16();
                        _readHeader = true;
                        break;
                    }

                    case PacketType.HeartBeat:
                    {
                        _dataSize   = HeartbeatPacketBodySize;
                        _id         = (ushort)CustomPacketId.SendHeartbeat;
                        _readHeader = true;
                        break;
                    }

                    case PacketType.Unknown1:
                    {
                        _dataSize   = Unknown1PacketBodySize;
                        _id         = (ushort)CustomPacketId.SendUnknown1;
                        _readHeader = true;
                        break;
                    }

                    case PacketType.Disconnect:
                    {
                        _dataSize   = DisconnectPacketBodySize;
                        _id         = (ushort)CustomPacketId.SendDisconnect;
                        _readHeader = true;
                        break;
                    }

                    default:
                    {
                        // TODO update arrowgene buffer to read uint24 && int24
                        Logger.Error($"PacketType: '{_packetType}' not supported");
                        Reset();
                        return(packets);
                    }
                    }
                }

                if (_readPacketLengthType &&
                    _readHeader &&
                    _buffer.Size - _buffer.Position >= _dataSize)
                {
                    // TODO update arrowgene service to read uint
                    byte[]    packetData = _buffer.ReadBytes((int)_dataSize);
                    IBuffer   buffer     = BufferProvider.Provide(packetData);
                    NecPacket packet     = new NecPacket(_id, buffer, serverType);
                    packet.Header = _header;
                    packets.Add(packet);
                    _readPacketLengthType = false;
                    _readHeader           = false;
                    read = _buffer.Position != _buffer.Size;
                }
            }

            if (_buffer.Position == _buffer.Size)
            {
                Reset();
            }
            else
            {
                _position = _buffer.Position;
            }

            return(packets);
        }
Example #6
0
        public List <NecPacket> Read(byte[] data, NecClient client)
        {
            List <NecPacket> packets = new List <NecPacket>();

            if (_buffer == null)
            {
                _buffer = BufferProvider.Provide(data);
            }
            else
            {
                _buffer.SetPositionEnd();
                _buffer.WriteBytes(data);
            }

            _buffer.Position = _position;

            bool read = true;

            while (read)
            {
                read = false;

                if (!_readPacketLengthType &&
                    _buffer.Size - _buffer.Position > PacketLengthTypeSize)
                {
                    byte lengthType = _buffer.ReadByte();
                    if (!Enum.IsDefined(typeof(PacketLengthType), lengthType))
                    {
                        _logger.Error($"PacketLengthType: '{lengthType}' not found");
                        byte[] dataDump = _buffer.GetBytes(_buffer.Position - 1, _buffer.Size);
                        _logger.LogErrorPacket(client, dataDump, null);
                        Reset();
                        return(packets);
                    }

                    _readPacketLengthType = true;
                    _packetLengthType     = (PacketLengthType)lengthType;
                    _headerSize           = CalculateHeaderSize(_packetLengthType);
                }

                if (_readPacketLengthType &&
                    !_readHeader &&
                    _buffer.Size - _buffer.Position >= _headerSize - PacketLengthTypeSize)
                {
                    // TODO aquire 1st byte differently incase -1 doesnt work
                    _header = _buffer.GetBytes(_buffer.Position - PacketLengthTypeSize, _headerSize);

                    switch (_packetLengthType)
                    {
                    case PacketLengthType.Byte:
                    {
                        _dataSize = _buffer.ReadByte();
                        break;
                    }

                    case PacketLengthType.UInt16:
                    {
                        _dataSize = _buffer.ReadUInt16();
                        break;
                    }

                    case PacketLengthType.UInt32:
                    {
                        _dataSize = _buffer.ReadUInt32();
                        break;
                    }

                    default:
                    {
                        // TODO update arrowgene service to read uint24 && int24
                        _logger.Error($"PacketLengthType: '{_packetLengthType}' not supported");
                        Reset();
                        return(packets);
                    }
                    }

                    _dataSize  -= PacketIdSize;
                    _id         = _buffer.ReadUInt16();
                    _readHeader = true;
                }

                if (_readPacketLengthType &&
                    _readHeader &&
                    _buffer.Size - _buffer.Position >= _dataSize)
                {
                    // TODO update arrowgene service to read uint
                    byte[]    packetData = _buffer.ReadBytes((int)_dataSize);
                    IBuffer   buffer     = BufferProvider.Provide(packetData);
                    NecPacket packet     = new NecPacket(_id, buffer);
                    packet.Header = _header;
                    packets.Add(packet);
                    _readPacketLengthType = false;
                    _readHeader           = false;
                    read = _buffer.Position != _buffer.Size;
                }
            }

            if (_buffer.Position == _buffer.Size)
            {
                Reset();
            }
            else
            {
                _position = _buffer.Position;
            }

            return(packets);
        }
 public virtual void WriteBuffer(IBuffer value, int offset, int length)
 {
     WriteBytes(value.GetBytes(offset, length));
 }