mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-09-06 11:34:15 -04:00
* move music path enumerations into d2resource * move text dictionary (.tbl) loader into d2fileformats sub-package d2tbl * lint fix, add doc file for d2tbl * moved data_dictionary.go into d2fileformats sub-package d2txt, added doc file * added sub-packages d2geom for geometry-related things, and d2path for path-related things * moved calcstring.go to d2calculation * move bitmuncher, bitstream, stream reader/writer from d2common into sub-package d2datautils * fix lint errors in d2datadict loaders (caused by moving stuf around in d2common) * move size.go into d2geom * move d2common/cache.go into sub-package d2common/d2cache * renamed d2debugutil to d2util, moved utility functions from d2common into d2util
124 lines
2.8 KiB
Go
124 lines
2.8 KiB
Go
package d2asset
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2cache"
|
|
"path"
|
|
"sync"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
|
|
)
|
|
|
|
// Static checks to confirm struct conforms to interface
|
|
var _ d2interface.ArchiveManager = &archiveManager{}
|
|
var _ d2interface.Cacher = &archiveManager{}
|
|
|
|
type archiveManager struct {
|
|
cache d2interface.Cache
|
|
config *d2config.Configuration
|
|
archives []d2interface.Archive
|
|
mutex sync.Mutex
|
|
}
|
|
|
|
const (
|
|
archiveBudget = 1024 * 1024 * 512
|
|
)
|
|
|
|
func createArchiveManager(config *d2config.Configuration) d2interface.ArchiveManager {
|
|
return &archiveManager{cache: d2cache.CreateCache(archiveBudget), config: config}
|
|
}
|
|
|
|
// LoadArchiveForFile loads the archive for the given (in-archive) file path
|
|
func (am *archiveManager) LoadArchiveForFile(filePath string) (d2interface.Archive, error) {
|
|
am.mutex.Lock()
|
|
defer am.mutex.Unlock()
|
|
|
|
if err := am.CacheArchiveEntries(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, archive := range am.archives {
|
|
if archive.Contains(filePath) {
|
|
result, ok := am.LoadArchive(archive.Path())
|
|
if ok == nil {
|
|
return result, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil, errors.New("file not found")
|
|
}
|
|
|
|
// FileExistsInArchive checks if a file exists in an archive
|
|
func (am *archiveManager) FileExistsInArchive(filePath string) (bool, error) {
|
|
am.mutex.Lock()
|
|
defer am.mutex.Unlock()
|
|
|
|
if err := am.CacheArchiveEntries(); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
for _, archiveEntry := range am.archives {
|
|
if archiveEntry.Contains(filePath) {
|
|
return true, nil
|
|
}
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
// LoadArchive loads and caches an archive
|
|
func (am *archiveManager) LoadArchive(archivePath string) (d2interface.Archive, error) {
|
|
if archive, found := am.cache.Retrieve(archivePath); found {
|
|
return archive.(d2interface.Archive), nil
|
|
}
|
|
|
|
archive, err := d2mpq.Load(archivePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := am.cache.Insert(archivePath, archive, int(archive.Size())); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return archive, nil
|
|
}
|
|
|
|
// CacheArchiveEntries updates the archive entries
|
|
func (am *archiveManager) CacheArchiveEntries() error {
|
|
if len(am.archives) == len(am.config.MpqLoadOrder) {
|
|
return nil
|
|
}
|
|
|
|
am.archives = nil
|
|
|
|
for _, archiveName := range am.config.MpqLoadOrder {
|
|
archivePath := path.Join(am.config.MpqPath, archiveName)
|
|
|
|
archive, err := am.LoadArchive(archivePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
am.archives = append(
|
|
am.archives,
|
|
archive,
|
|
)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ClearCache clears the archive manager cache
|
|
func (am *archiveManager) ClearCache() {
|
|
am.cache.Clear()
|
|
}
|
|
|
|
// GetCache returns the archive manager cache
|
|
func (am *archiveManager) GetCache() d2interface.Cache {
|
|
return am.cache
|
|
}
|