2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
2020-01-31 23:18:11 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq"
|
2020-02-01 21:06:22 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type archiveEntry struct {
|
|
|
|
archivePath string
|
|
|
|
hashEntryMap d2mpq.HashEntryMap
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
type archiveManager struct {
|
2020-01-31 23:18:11 -05:00
|
|
|
cache *d2common.Cache
|
2020-02-20 08:41:41 -05:00
|
|
|
config d2config.Configuration
|
2020-01-31 23:18:11 -05:00
|
|
|
entries []archiveEntry
|
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-02-01 18:55:56 -05:00
|
|
|
archiveBudget = 1024 * 1024 * 512
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
2020-02-20 08:41:41 -05:00
|
|
|
func createArchiveManager(config d2config.Configuration) *archiveManager {
|
2020-02-01 18:55:56 -05:00
|
|
|
return &archiveManager{cache: d2common.CreateCache(archiveBudget), config: config}
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
func (am *archiveManager) loadArchiveForFile(filePath string) (*d2mpq.MPQ, error) {
|
2020-01-31 23:18:11 -05:00
|
|
|
am.mutex.Lock()
|
|
|
|
defer am.mutex.Unlock()
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
if err := am.cacheArchiveEntries(); err != nil {
|
2020-01-31 23:18:11 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, archiveEntry := range am.entries {
|
|
|
|
if archiveEntry.hashEntryMap.Contains(filePath) {
|
2020-02-01 18:55:56 -05:00
|
|
|
return am.loadArchive(archiveEntry.archivePath)
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("file not found")
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
func (am *archiveManager) fileExistsInArchive(filePath string) (bool, error) {
|
2020-01-31 23:18:11 -05:00
|
|
|
am.mutex.Lock()
|
|
|
|
defer am.mutex.Unlock()
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
if err := am.cacheArchiveEntries(); err != nil {
|
2020-01-31 23:18:11 -05:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, archiveEntry := range am.entries {
|
|
|
|
if archiveEntry.hashEntryMap.Contains(filePath) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
func (am *archiveManager) loadArchive(archivePath string) (*d2mpq.MPQ, error) {
|
2020-01-31 23:18:11 -05:00
|
|
|
if archive, found := am.cache.Retrieve(archivePath); found {
|
|
|
|
return archive.(*d2mpq.MPQ), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
archive, err := d2mpq.Load(archivePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := am.cache.Insert(archivePath, archive, int(archive.Data.ArchiveSize)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return archive, nil
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
func (am *archiveManager) cacheArchiveEntries() error {
|
2020-01-31 23:18:11 -05:00
|
|
|
if len(am.entries) == len(am.config.MpqLoadOrder) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
am.entries = nil
|
|
|
|
|
|
|
|
for _, archiveName := range am.config.MpqLoadOrder {
|
|
|
|
archivePath := path.Join(am.config.MpqPath, archiveName)
|
2020-02-01 18:55:56 -05:00
|
|
|
archive, err := am.loadArchive(archivePath)
|
2020-01-31 23:18:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
am.entries = append(
|
|
|
|
am.entries,
|
|
|
|
archiveEntry{archivePath, archive.HashEntryMap},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|