1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-27 09:35:29 +00:00

Replaced bufio.NewReader with File.Read (#375)

This commit is contained in:
Intyre 2020-06-22 00:43:30 +02:00 committed by GitHub
parent ab9571b8e8
commit ec17fcf3e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,6 @@
package d2mpq package d2mpq
import ( import (
"bufio"
"bytes" "bytes"
"compress/zlib" "compress/zlib"
"encoding/binary" "encoding/binary"
@ -58,9 +57,8 @@ func (v *Stream) loadBlockOffsets() error {
blockPositionCount := ((v.BlockTableEntry.UncompressedFileSize + v.BlockSize - 1) / v.BlockSize) + 1 blockPositionCount := ((v.BlockTableEntry.UncompressedFileSize + v.BlockSize - 1) / v.BlockSize) + 1
v.BlockPositions = make([]uint32, blockPositionCount) v.BlockPositions = make([]uint32, blockPositionCount)
v.MPQData.File.Seek(int64(v.BlockTableEntry.FilePosition), 0) v.MPQData.File.Seek(int64(v.BlockTableEntry.FilePosition), 0)
reader := bufio.NewReader(v.MPQData.File)
mpqBytes := make([]byte, blockPositionCount*4) mpqBytes := make([]byte, blockPositionCount*4)
reader.Read(mpqBytes) v.MPQData.File.Read(mpqBytes)
for i := range v.BlockPositions { for i := range v.BlockPositions {
idx := i * 4 idx := i * 4
v.BlockPositions[i] = binary.LittleEndian.Uint32(mpqBytes[idx : idx+4]) v.BlockPositions[i] = binary.LittleEndian.Uint32(mpqBytes[idx : idx+4])
@ -135,9 +133,7 @@ func (v *Stream) bufferData() {
func (v *Stream) loadSingleUnit() { func (v *Stream) loadSingleUnit() {
fileData := make([]byte, v.BlockSize) fileData := make([]byte, v.BlockSize)
v.MPQData.File.Seek(int64(v.MPQData.Data.HeaderSize), 0) v.MPQData.File.Seek(int64(v.MPQData.Data.HeaderSize), 0)
//binary.Read(v.MPQData.File, binary.LittleEndian, &fileData) v.MPQData.File.Read(fileData)
reader := bufio.NewReader(v.MPQData.File)
reader.Read(fileData)
if v.BlockSize == v.BlockTableEntry.UncompressedFileSize { if v.BlockSize == v.BlockTableEntry.UncompressedFileSize {
v.CurrentData = fileData v.CurrentData = fileData
return return
@ -160,9 +156,7 @@ func (v *Stream) loadBlock(blockIndex, expectedLength uint32) []byte {
offset += v.BlockTableEntry.FilePosition offset += v.BlockTableEntry.FilePosition
data := make([]byte, toRead) data := make([]byte, toRead)
v.MPQData.File.Seek(int64(offset), 0) v.MPQData.File.Seek(int64(offset), 0)
//binary.Read(v.MPQData.File, binary.LittleEndian, &data) v.MPQData.File.Read(data)
reader := bufio.NewReader(v.MPQData.File)
reader.Read(data)
if v.BlockTableEntry.HasFlag(FileEncrypted) && v.BlockTableEntry.UncompressedFileSize > 3 { if v.BlockTableEntry.HasFlag(FileEncrypted) && v.BlockTableEntry.UncompressedFileSize > 3 {
if v.EncryptionSeed == 0 { if v.EncryptionSeed == 0 {
panic("Unable to determine encryption key") panic("Unable to determine encryption key")