2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
2020-01-31 23:18:11 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
2020-07-08 09:16:16 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-06-22 17:36:45 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-07-08 09:16:16 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-02-01 18:55:56 -05:00
|
|
|
fileBudget = 1024 * 1024 * 32
|
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.ArchivedFileManager = &fileManager{}
|
|
|
|
var _ d2interface.Cacher = &fileManager{}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
type fileManager struct {
|
2020-07-03 21:23:44 -04:00
|
|
|
cache d2interface.Cache
|
2020-07-04 22:37:13 -04:00
|
|
|
archiveManager d2interface.ArchiveManager
|
2020-07-08 09:16:16 -04:00
|
|
|
config *d2config.Configuration
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
func createFileManager(config *d2config.Configuration,
|
2020-07-05 00:00:00 -04:00
|
|
|
archiveManager d2interface.ArchiveManager) d2interface.ArchivedFileManager {
|
2020-07-04 00:49:16 -04:00
|
|
|
return &fileManager{
|
|
|
|
d2common.CreateCache(fileBudget),
|
|
|
|
archiveManager,
|
|
|
|
config,
|
|
|
|
}
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-05 00:00:00 -04:00
|
|
|
// LoadFileStream loads a file as a stream automatically from an archive
|
|
|
|
func (fm *fileManager) LoadFileStream(filePath string) (d2interface.ArchiveDataStream, error) {
|
2020-06-27 02:49:27 -04:00
|
|
|
filePath = fm.fixupFilePath(filePath)
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
archive, err := fm.archiveManager.LoadArchiveForFile(filePath)
|
2020-06-27 02:49:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return archive.ReadFileStream(filePath)
|
|
|
|
}
|
|
|
|
|
2020-07-05 00:00:00 -04:00
|
|
|
// LoadFile loads a file automatically from a managed archive
|
|
|
|
func (fm *fileManager) LoadFile(filePath string) ([]byte, error) {
|
2020-01-31 23:18:11 -05:00
|
|
|
filePath = fm.fixupFilePath(filePath)
|
|
|
|
if value, found := fm.cache.Retrieve(filePath); found {
|
|
|
|
return value.([]byte), nil
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
archive, err := fm.archiveManager.LoadArchiveForFile(filePath)
|
2020-01-31 23:18:11 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-05 00:00:00 -04:00
|
|
|
// FileExists checks if a file exists in an archive
|
|
|
|
func (fm *fileManager) FileExists(filePath string) (bool, error) {
|
2020-01-31 23:18:11 -05:00
|
|
|
filePath = fm.fixupFilePath(filePath)
|
2020-07-04 22:37:13 -04:00
|
|
|
return fm.archiveManager.FileExistsInArchive(filePath)
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-05 00:00:00 -04:00
|
|
|
func (fm *fileManager) ClearCache() {
|
|
|
|
fm.cache.Clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fm *fileManager) GetCache() d2interface.Cache {
|
|
|
|
return fm.cache
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
func (fm *fileManager) fixupFilePath(filePath string) string {
|
2020-06-22 17:36:45 -04:00
|
|
|
filePath = fm.removeLocaleTokens(filePath)
|
2020-01-31 23:18:11 -05:00
|
|
|
filePath = strings.ToLower(filePath)
|
|
|
|
filePath = strings.ReplaceAll(filePath, `/`, "\\")
|
|
|
|
filePath = strings.TrimPrefix(filePath, "\\")
|
|
|
|
|
|
|
|
return filePath
|
|
|
|
}
|
2020-06-22 17:36:45 -04:00
|
|
|
|
|
|
|
func (fm *fileManager) removeLocaleTokens(filePath string) string {
|
|
|
|
tableToken := d2resource.LanguageTableToken
|
|
|
|
fontToken := d2resource.LanguageFontToken
|
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
filePath = strings.ReplaceAll(filePath, tableToken, fm.config.Language)
|
2020-06-22 17:36:45 -04:00
|
|
|
|
|
|
|
// fixme: not all languages==latin
|
|
|
|
filePath = strings.ReplaceAll(filePath, fontToken, "latin")
|
|
|
|
|
|
|
|
return filePath
|
|
|
|
}
|