1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-04 17:27:16 -05:00
OpenDiablo2/d2common/d2loader/mpq/source.go
lord 7f6ae1b785
improve AssetManager implementation (#728)
* improve AssetManager implementation

Notable changes are:
 * removed the individual managers inside of d2asset, only one asset manager
 * AssetManager now has caches for the types of files it loads
 * created a type for TextDictionary (the txt file structs)
 * fixed a file path bug in d2loader Source
 * fixed a asset stream bug in d2loader Asset
 * d2loader.Loader now needs a d2config.Config on creation (for resolving locale files)
 * updated the mpq file in d2asset test data, added test case for "sub-directory"
 * added a Data method to d2asset.Asset. The data is cached on first full read.
 * renamed ArchiveDataStream to DataStream in d2interface
 * moved palette utility func out of d2asset and into d2util
 * bugfix for MacOS mpq loader issue

* minor lint fixes

* removed obsolete interfaces from d2interface

* lint fixes, added data caching to filesystem asset

* adding comment for mpq asset close

* adding comment for mpq asset close
2020-09-14 14:47:11 -04:00

72 lines
1.5 KiB
Go

package mpq
import (
"strings"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/asset"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/asset/types"
)
// static check that Source implements AssetSource
var _ asset.Source = &Source{}
// NewSource creates a new MPQ Source
func NewSource(sourcePath string) (asset.Source, error) {
loaded, err := d2mpq.Load(sourcePath)
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
}
// Type returns the asset type, for MPQ's it always returns the MPQ asset source type
func (v *Source) Type() types.SourceType {
return types.AssetSourceMPQ
}
// Open attempts to open a file within the MPQ archive
func (v *Source) Open(name string) (a asset.Asset, err error) {
name = cleanName(name)
stream, err := v.MPQ.ReadFileStream(name)
if err != nil {
return nil, err
}
a = &Asset{
source: v,
stream: stream,
path: name,
}
return a, nil
}
// Path returns the path of the MPQ on the host filesystem
func (v *Source) Path() string {
return v.MPQ.Path()
}
// String returns the path
func (v *Source) String() string {
return v.Path()
}
func cleanName(name string) string {
name = strings.ReplaceAll(name, "/", "\\")
if string(name[0]) == "\\" {
name = name[1:]
}
return name
}