2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
2020-01-31 23:18:11 -05:00
|
|
|
|
|
|
|
import (
|
2020-06-27 02:49:27 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq"
|
2020-01-31 23:18:11 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
2020-06-22 17:36:45 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-02-01 21:06:22 -05: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-02-01 18:55:56 -05:00
|
|
|
type fileManager struct {
|
2020-01-31 23:18:11 -05:00
|
|
|
cache *d2common.Cache
|
2020-02-01 18:55:56 -05:00
|
|
|
archiveManager *archiveManager
|
2020-07-01 14:09:18 -04:00
|
|
|
config *d2config.Configuration
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
func createFileManager(config *d2config.Configuration, archiveManager *archiveManager) *fileManager {
|
2020-02-01 18:55:56 -05:00
|
|
|
return &fileManager{d2common.CreateCache(fileBudget), archiveManager, config}
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-06-27 02:49:27 -04:00
|
|
|
func (fm *fileManager) loadFileStream(filePath string) (*d2mpq.MpqDataStream, error) {
|
|
|
|
filePath = fm.fixupFilePath(filePath)
|
|
|
|
|
|
|
|
archive, err := fm.archiveManager.loadArchiveForFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return archive.ReadFileStream(filePath)
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
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-02-01 18:55:56 -05: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-02-01 18:55:56 -05:00
|
|
|
func (fm *fileManager) fileExists(filePath string) (bool, error) {
|
2020-01-31 23:18:11 -05:00
|
|
|
filePath = fm.fixupFilePath(filePath)
|
2020-02-01 18:55:56 -05:00
|
|
|
return fm.archiveManager.fileExistsInArchive(filePath)
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
filePath = strings.ReplaceAll(filePath, tableToken, fm.config.Language)
|
|
|
|
|
|
|
|
// fixme: not all languages==latin
|
|
|
|
filePath = strings.ReplaceAll(filePath, fontToken, "latin")
|
|
|
|
|
|
|
|
return filePath
|
|
|
|
}
|