2020-09-08 15:45:26 -04:00
|
|
|
package mpq
|
|
|
|
|
|
|
|
import (
|
2021-01-10 02:44:42 -05:00
|
|
|
"io"
|
2020-09-14 14:47:11 -04:00
|
|
|
"strings"
|
|
|
|
|
2020-09-08 15:45:26 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/asset"
|
|
|
|
)
|
|
|
|
|
|
|
|
// static check that Source implements AssetSource
|
|
|
|
var _ asset.Source = &Source{}
|
|
|
|
|
|
|
|
// NewSource creates a new MPQ Source
|
|
|
|
func NewSource(sourcePath string) (asset.Source, error) {
|
2021-01-08 15:46:11 -05:00
|
|
|
loaded, err := d2mpq.FromFile(sourcePath)
|
2020-09-08 15:45:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Source{loaded}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Source is an implementation of an asset source for MPQ archives
|
|
|
|
type Source struct {
|
|
|
|
MPQ d2interface.Archive
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open attempts to open a file within the MPQ archive
|
2021-01-10 02:44:42 -05:00
|
|
|
func (v *Source) Open(name string) (a io.ReadSeeker, err error) {
|
2020-09-14 14:47:11 -04:00
|
|
|
name = cleanName(name)
|
2021-01-10 02:44:42 -05:00
|
|
|
return v.MPQ.ReadFileStream(name)
|
|
|
|
}
|
2020-09-08 15:45:26 -04:00
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
// Exists returns true if the file exists
|
|
|
|
func (v *Source) Exists(subPath string) bool {
|
|
|
|
subPath = cleanName(subPath)
|
|
|
|
return v.MPQ.Contains(subPath)
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
|
|
|
|
2020-09-14 14:47:11 -04:00
|
|
|
// Path returns the path of the MPQ on the host filesystem
|
2020-09-09 14:35:52 -04:00
|
|
|
func (v *Source) Path() string {
|
2020-09-08 15:45:26 -04:00
|
|
|
return v.MPQ.Path()
|
|
|
|
}
|
2020-09-09 14:35:52 -04:00
|
|
|
|
|
|
|
// String returns the path
|
|
|
|
func (v *Source) String() string {
|
|
|
|
return v.Path()
|
|
|
|
}
|
2020-09-14 14:47:11 -04:00
|
|
|
|
|
|
|
func cleanName(name string) string {
|
|
|
|
name = strings.ReplaceAll(name, "/", "\\")
|
|
|
|
|
|
|
|
if string(name[0]) == "\\" {
|
|
|
|
name = name[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|