2020-01-26 00:39:13 -05:00
|
|
|
package d2mpq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
2021-01-08 15:46:11 -05:00
|
|
|
"fmt"
|
2021-03-26 13:52:43 -04:00
|
|
|
"io/fs"
|
2020-01-26 00:39:13 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2020-07-13 09:05:04 -04:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-01-26 00:39:13 -05:00
|
|
|
)
|
|
|
|
|
2020-07-13 09:05:04 -04:00
|
|
|
var _ d2interface.Archive = &MPQ{} // Static check to confirm struct conforms to interface
|
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
// MPQ represents an MPQ archive
|
|
|
|
type MPQ struct {
|
2021-01-08 15:46:11 -05:00
|
|
|
filePath string
|
|
|
|
file *os.File
|
|
|
|
hashes map[uint64]*Hash
|
|
|
|
blocks []*Block
|
|
|
|
header Header
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2020-06-28 22:32:34 -04:00
|
|
|
// PatchInfo represents patch info for the MPQ.
|
2020-01-26 00:39:13 -05:00
|
|
|
type PatchInfo struct {
|
|
|
|
Length uint32 // Length of patch info header, in bytes
|
|
|
|
Flags uint32 // Flags. 0x80000000 = MD5 (?)
|
|
|
|
DataSize uint32 // Uncompressed size of the patch file
|
2021-01-08 15:46:11 -05:00
|
|
|
MD5 [16]byte // MD5 of the entire patch file after decompression
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
// New loads an MPQ file and only reads the header
|
|
|
|
func New(fileName string) (*MPQ, error) {
|
|
|
|
mpq := &MPQ{filePath: fileName}
|
2020-01-26 00:39:13 -05:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if runtime.GOOS == "linux" {
|
2021-01-08 15:46:11 -05:00
|
|
|
mpq.file, err = openIgnoreCase(fileName)
|
2020-01-26 00:39:13 -05:00
|
|
|
} else {
|
2021-01-08 15:46:11 -05:00
|
|
|
mpq.file, err = os.Open(fileName) //nolint:gosec // Will fix later
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
if err := mpq.readHeader(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read reader: %v", err)
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
return mpq, nil
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
// FromFile loads an MPQ file and returns a MPQ structure
|
|
|
|
func FromFile(fileName string) (*MPQ, error) {
|
|
|
|
mpq, err := New(fileName)
|
2020-01-26 00:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
if err := mpq.readHashTable(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read hash table: %v", err)
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
if err := mpq.readBlockTable(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read block table: %v", err)
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
2020-09-23 13:30:54 -04:00
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
return mpq, nil
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
// getFileBlockData gets a block table entry
|
|
|
|
func (mpq *MPQ) getFileBlockData(fileName string) (*Block, error) {
|
|
|
|
fileEntry, ok := mpq.hashes[hashFilename(fileName)]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("file not found")
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
2020-06-22 16:01:14 -04:00
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
if fileEntry.BlockIndex >= uint32(len(mpq.blocks)) {
|
|
|
|
return nil, errors.New("invalid block index")
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
return mpq.blocks[fileEntry.BlockIndex], nil
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the MPQ file
|
2021-01-08 15:46:11 -05:00
|
|
|
func (mpq *MPQ) Close() error {
|
|
|
|
return mpq.file.Close()
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFile reads a file from the MPQ and returns a memory stream
|
2021-01-08 15:46:11 -05:00
|
|
|
func (mpq *MPQ) ReadFile(fileName string) ([]byte, error) {
|
|
|
|
fileBlockData, err := mpq.getFileBlockData(fileName)
|
2020-01-26 00:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
2020-06-27 02:49:27 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
fileBlockData.FileName = strings.ToLower(fileName)
|
2020-06-27 02:49:27 -04:00
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
stream, err := CreateStream(mpq, fileBlockData, fileName)
|
2020-01-26 00:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
2020-06-27 02:49:27 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
buffer := make([]byte, fileBlockData.UncompressedFileSize)
|
2021-01-08 15:46:11 -05:00
|
|
|
if _, err := stream.Read(buffer, 0, fileBlockData.UncompressedFileSize); err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return buffer, nil
|
|
|
|
}
|
|
|
|
|
2020-06-28 22:32:34 -04:00
|
|
|
// ReadFileStream reads the mpq file data and returns a stream
|
2021-01-08 15:46:11 -05:00
|
|
|
func (mpq *MPQ) ReadFileStream(fileName string) (d2interface.DataStream, error) {
|
|
|
|
fileBlockData, err := mpq.getFileBlockData(fileName)
|
2020-06-27 02:49:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-06-27 02:49:27 -04:00
|
|
|
fileBlockData.FileName = strings.ToLower(fileName)
|
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
stream, err := CreateStream(mpq, fileBlockData, fileName)
|
2020-06-27 02:49:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
return &MpqDataStream{stream: stream}, nil
|
2020-06-27 02:49:27 -04:00
|
|
|
}
|
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
// ReadTextFile reads a file and returns it as a string
|
2021-01-08 15:46:11 -05:00
|
|
|
func (mpq *MPQ) ReadTextFile(fileName string) (string, error) {
|
|
|
|
data, err := mpq.ReadFile(fileName)
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
2021-01-08 15:46:11 -05:00
|
|
|
// Listfile returns the list of files in this MPQ
|
|
|
|
func (mpq *MPQ) Listfile() ([]string, error) {
|
|
|
|
data, err := mpq.ReadFile("(listfile)")
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
raw := strings.TrimRight(string(data), "\x00")
|
|
|
|
s := bufio.NewScanner(strings.NewReader(raw))
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
var filePaths []string
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
for s.Scan() {
|
|
|
|
filePath := s.Text()
|
|
|
|
filePaths = append(filePaths, filePath)
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return filePaths, nil
|
|
|
|
}
|
2020-07-04 22:37:13 -04:00
|
|
|
|
2020-08-04 22:52:54 -04:00
|
|
|
// Path returns the MPQ file path
|
2021-01-08 15:46:11 -05:00
|
|
|
func (mpq *MPQ) Path() string {
|
|
|
|
return mpq.filePath
|
2020-07-04 22:37:13 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 22:52:54 -04:00
|
|
|
// Contains returns bool for whether the given filename exists in the mpq
|
2021-01-08 15:46:11 -05:00
|
|
|
func (mpq *MPQ) Contains(filename string) bool {
|
|
|
|
_, ok := mpq.hashes[hashFilename(filename)]
|
|
|
|
return ok
|
2020-07-04 22:37:13 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 22:52:54 -04:00
|
|
|
// Size returns the size of the mpq in bytes
|
2021-01-08 15:46:11 -05:00
|
|
|
func (mpq *MPQ) Size() uint32 {
|
|
|
|
return mpq.header.ArchiveSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func openIgnoreCase(mpqPath string) (*os.File, error) {
|
|
|
|
// First see if file exists with specified case
|
|
|
|
mpqFile, err := os.Open(mpqPath) //nolint:gosec // Will fix later
|
2021-03-26 13:52:43 -04:00
|
|
|
if err != nil {
|
|
|
|
mpqName := filepath.Base(mpqPath)
|
|
|
|
mpqDir := filepath.Dir(mpqPath)
|
2021-01-08 15:46:11 -05:00
|
|
|
|
2021-03-26 13:52:43 -04:00
|
|
|
var files []fs.FileInfo
|
|
|
|
files, err = ioutil.ReadDir(mpqDir)
|
2021-01-08 15:46:11 -05:00
|
|
|
|
2021-03-26 13:52:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-08 15:46:11 -05:00
|
|
|
|
2021-03-26 13:52:43 -04:00
|
|
|
for _, file := range files {
|
|
|
|
if strings.EqualFold(file.Name(), mpqName) {
|
|
|
|
mpqName = file.Name()
|
|
|
|
break
|
|
|
|
}
|
2021-01-08 15:46:11 -05:00
|
|
|
}
|
2021-03-26 13:52:43 -04:00
|
|
|
|
|
|
|
return os.Open(filepath.Join(mpqDir, mpqName)) //nolint:gosec // Will fix later
|
2021-01-08 15:46:11 -05:00
|
|
|
}
|
|
|
|
|
2021-03-26 13:52:43 -04:00
|
|
|
return mpqFile, err
|
2020-07-04 22:37:13 -04:00
|
|
|
}
|