1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-06 18:27:20 -05:00
OpenDiablo2/d2asset/archive_manager.go
Alex Yatskov 1b03e691b9 High level resource caching; resource cleanup (#264)
* Work on resource loading

* Use new material flag name. (#261)

Update ebiten ref while at it

* Hopefully fix CI (#262)

* Don't try to copy config.json on travis (#263)

I doesn't exist anymore

* Update D2Shared references

* Fix character selection rect

Co-authored-by: Ziemas <ziemas@ziemas.se>
2019-12-21 20:53:18 -05:00

84 lines
1.8 KiB
Go

package d2asset
import (
"fmt"
"path"
"sync"
"github.com/OpenDiablo2/D2Shared/d2data/d2mpq"
"github.com/OpenDiablo2/OpenDiablo2/d2corecommon"
)
type archiveEntry struct {
archivePath string
hashEntryMap d2mpq.HashEntryMap
}
type archiveManager struct {
cache *cache
config *d2corecommon.Configuration
entries []archiveEntry
mutex sync.Mutex
}
func createArchiveManager(config *d2corecommon.Configuration) *archiveManager {
return &archiveManager{cache: createCache(ArchiveBudget), config: config}
}
func (am *archiveManager) loadArchiveForFilePath(filePath string) (*d2mpq.MPQ, error) {
am.mutex.Lock()
defer am.mutex.Unlock()
if err := am.cacheArchiveEntries(); err != nil {
return nil, err
}
for _, archiveEntry := range am.entries {
if archiveEntry.hashEntryMap.Contains(filePath) {
return am.loadArchive(archiveEntry.archivePath)
}
}
return nil, fmt.Errorf("file not found: %s", filePath)
}
func (am *archiveManager) loadArchive(archivePath string) (*d2mpq.MPQ, error) {
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
}
func (am *archiveManager) cacheArchiveEntries() error {
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)
archive, err := am.loadArchive(archivePath)
if err != nil {
return err
}
am.entries = append(
am.entries,
archiveEntry{archivePath, archive.HashEntryMap},
)
}
return nil
}