1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-06 18:27:20 -05:00
OpenDiablo2/d2asset/file_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

57 lines
1.4 KiB
Go

package d2asset
import (
"strings"
"github.com/OpenDiablo2/D2Shared/d2common/d2resource"
"github.com/OpenDiablo2/OpenDiablo2/d2corecommon"
)
type fileManager struct {
cache *cache
archiveManager *archiveManager
config *d2corecommon.Configuration
}
func createFileManager(config *d2corecommon.Configuration, archiveManager *archiveManager) *fileManager {
return &fileManager{createCache(FileBudget), archiveManager, config}
}
func (fm *fileManager) loadFile(filePath string) ([]byte, error) {
filePath = fm.fixupFilePath(filePath)
if value, found := fm.cache.retrieve(filePath); found {
return value.([]byte), nil
}
archive, err := fm.archiveManager.loadArchiveForFilePath(filePath)
if err != nil {
return nil, err
}
data, err := archive.ReadFile(filePath)
if err != nil {
return nil, err
}
if err := fm.cache.insert(filePath, data, len(data)); err != nil {
return nil, err
}
return data, nil
}
func (fm *fileManager) fixupFilePath(filePath string) string {
filePath = strings.ReplaceAll(filePath, "{LANG}", fm.config.Language)
if strings.ToUpper(d2resource.LanguageCode) == "CHI" {
filePath = strings.ReplaceAll(filePath, "{LANG_FONT}", fm.config.Language)
} else {
filePath = strings.ReplaceAll(filePath, "{LANG_FONT}", "latin")
}
filePath = strings.ToLower(filePath)
filePath = strings.ReplaceAll(filePath, `/`, "\\")
filePath = strings.TrimPrefix(filePath, "\\")
return filePath
}