2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
2020-01-31 23:18:11 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-09-08 15:58:35 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2cache"
|
2020-01-31 23:18:11 -05:00
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq"
|
2020-07-08 09:16:16 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
2020-07-13 09:05:04 -04:00
|
|
|
// Static checks to confirm struct conforms to interface
|
|
|
|
var _ d2interface.ArchiveManager = &archiveManager{}
|
|
|
|
var _ d2interface.Cacher = &archiveManager{}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
type archiveManager struct {
|
2020-07-04 22:37:13 -04:00
|
|
|
cache d2interface.Cache
|
2020-07-08 09:16:16 -04:00
|
|
|
config *d2config.Configuration
|
2020-07-04 22:37:13 -04:00
|
|
|
archives []d2interface.Archive
|
|
|
|
mutex sync.Mutex
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-02-01 18:55:56 -05:00
|
|
|
archiveBudget = 1024 * 1024 * 512
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
func createArchiveManager(config *d2config.Configuration) d2interface.ArchiveManager {
|
2020-09-08 15:58:35 -04:00
|
|
|
return &archiveManager{cache: d2cache.CreateCache(archiveBudget), config: config}
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
// LoadArchiveForFile loads the archive for the given (in-archive) file path
|
|
|
|
func (am *archiveManager) LoadArchiveForFile(filePath string) (d2interface.Archive, error) {
|
2020-01-31 23:18:11 -05:00
|
|
|
am.mutex.Lock()
|
|
|
|
defer am.mutex.Unlock()
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
if err := am.CacheArchiveEntries(); err != nil {
|
2020-01-31 23:18:11 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
for _, archive := range am.archives {
|
|
|
|
if archive.Contains(filePath) {
|
|
|
|
result, ok := am.LoadArchive(archive.Path())
|
2020-06-22 19:33:12 -04:00
|
|
|
if ok == nil {
|
|
|
|
return result, nil
|
|
|
|
}
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("file not found")
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
// FileExistsInArchive checks if a file exists in an archive
|
|
|
|
func (am *archiveManager) FileExistsInArchive(filePath string) (bool, error) {
|
2020-01-31 23:18:11 -05:00
|
|
|
am.mutex.Lock()
|
|
|
|
defer am.mutex.Unlock()
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
if err := am.CacheArchiveEntries(); err != nil {
|
2020-01-31 23:18:11 -05:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
for _, archiveEntry := range am.archives {
|
|
|
|
if archiveEntry.Contains(filePath) {
|
2020-01-31 23:18:11 -05:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
// LoadArchive loads and caches an archive
|
|
|
|
func (am *archiveManager) LoadArchive(archivePath string) (d2interface.Archive, error) {
|
2020-01-31 23:18:11 -05:00
|
|
|
if archive, found := am.cache.Retrieve(archivePath); found {
|
2020-07-04 22:37:13 -04:00
|
|
|
return archive.(d2interface.Archive), nil
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
archive, err := d2mpq.Load(archivePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
if err := am.cache.Insert(archivePath, archive, int(archive.Size())); err != nil {
|
2020-01-31 23:18:11 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return archive, nil
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
// CacheArchiveEntries updates the archive entries
|
|
|
|
func (am *archiveManager) CacheArchiveEntries() error {
|
2020-07-08 09:16:16 -04:00
|
|
|
if len(am.archives) == len(am.config.MpqLoadOrder) {
|
2020-01-31 23:18:11 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
am.archives = nil
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
for _, archiveName := range am.config.MpqLoadOrder {
|
|
|
|
archivePath := path.Join(am.config.MpqPath, archiveName)
|
2020-07-01 14:09:18 -04:00
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
archive, err := am.LoadArchive(archivePath)
|
2020-01-31 23:18:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
am.archives = append(
|
|
|
|
am.archives,
|
|
|
|
archive,
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-04 22:37:13 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|