2020-09-08 15:45:26 -04:00
|
|
|
package d2loader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-10 02:44:42 -05:00
|
|
|
"io"
|
2020-09-08 15:45:26 -04:00
|
|
|
"path/filepath"
|
2020-09-14 14:47:11 -04:00
|
|
|
"strings"
|
2020-09-08 15:45:26 -04:00
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/mpq"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/filesystem"
|
|
|
|
|
2020-09-08 17:39:34 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2cache"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-09-08 15:45:26 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/asset"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/asset/types"
|
2020-09-14 14:47:11 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-09-09 08:22:13 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2020-09-08 15:45:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-10 02:44:42 -05:00
|
|
|
defaultCacheBudget = 1024 * 1024 * 512
|
|
|
|
errFmtFileNotFound = "file not found: %s"
|
2020-09-14 14:47:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-11-16 06:47:11 -05:00
|
|
|
logPrefix = "File Loader"
|
2020-09-14 14:47:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
fontToken = d2resource.LanguageFontToken
|
|
|
|
tableToken = d2resource.LanguageTableToken
|
2020-09-08 15:45:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewLoader creates a new loader
|
2020-11-03 07:54:15 -05:00
|
|
|
func NewLoader(l d2util.LogLevel) (*Loader, error) {
|
2021-01-10 02:44:42 -05:00
|
|
|
loader := &Loader{
|
|
|
|
LoaderProviders: make(map[types.SourceType]func(path string) (asset.Source, error), 2),
|
|
|
|
}
|
|
|
|
|
|
|
|
loader.LoaderProviders[types.AssetSourceMPQ] = mpq.NewSource
|
|
|
|
loader.LoaderProviders[types.AssetSourceFileSystem] = filesystem.OnAddSource
|
2020-09-14 14:47:11 -04:00
|
|
|
|
2020-09-08 17:39:34 -04:00
|
|
|
loader.Cache = d2cache.CreateCache(defaultCacheBudget)
|
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset
* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file
* fixed log level tests
2020-11-02 21:23:07 -05:00
|
|
|
loader.Logger = d2util.NewLogger()
|
2020-09-08 15:45:26 -04:00
|
|
|
|
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset
* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file
* fixed log level tests
2020-11-02 21:23:07 -05:00
|
|
|
loader.Logger.SetPrefix(logPrefix)
|
|
|
|
loader.Logger.SetLevel(l)
|
2020-09-14 14:47:11 -04:00
|
|
|
|
2020-11-03 07:54:15 -05:00
|
|
|
return loader, nil
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:52 -04:00
|
|
|
// Loader represents the manager that handles loading and caching assets with the asset Sources
|
2020-09-08 15:45:26 -04:00
|
|
|
// that have been added
|
|
|
|
type Loader struct {
|
2020-11-16 06:47:11 -05:00
|
|
|
language *string
|
2020-11-16 15:39:48 -05:00
|
|
|
charset *string
|
2020-09-08 15:45:26 -04:00
|
|
|
d2interface.Cache
|
2020-09-09 08:22:13 -04:00
|
|
|
*d2util.Logger
|
2021-01-10 02:44:42 -05:00
|
|
|
LoaderProviders map[types.SourceType]func(path string) (asset.Source, error)
|
|
|
|
Sources []asset.Source
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
|
|
|
|
2020-11-16 06:47:11 -05:00
|
|
|
// SetLanguage sets the language for loader
|
|
|
|
func (l *Loader) SetLanguage(language *string) {
|
|
|
|
l.language = language
|
|
|
|
}
|
|
|
|
|
2020-11-16 15:39:48 -05:00
|
|
|
// SetCharset sets the charset for loader
|
|
|
|
func (l *Loader) SetCharset(charset *string) {
|
|
|
|
l.charset = charset
|
|
|
|
}
|
|
|
|
|
2020-09-08 15:45:26 -04:00
|
|
|
// Load attempts to load an asset with the given sub-path. The sub-path is relative to the root
|
|
|
|
// of each asset source root (regardless of the type of asset source)
|
2021-01-10 02:44:42 -05:00
|
|
|
func (l *Loader) Load(subPath string) (io.ReadSeeker, error) {
|
2020-11-16 06:47:11 -05:00
|
|
|
subPath = filepath.Clean(subPath)
|
2020-09-14 14:47:11 -04:00
|
|
|
|
2020-11-16 06:47:11 -05:00
|
|
|
if l.language != nil {
|
2020-11-16 15:39:48 -05:00
|
|
|
charset := l.charset
|
|
|
|
language := l.language
|
|
|
|
|
|
|
|
subPath = strings.ReplaceAll(subPath, fontToken, *charset)
|
|
|
|
subPath = strings.ReplaceAll(subPath, tableToken, *language)
|
2020-09-14 14:47:11 -04:00
|
|
|
}
|
|
|
|
|
2020-09-08 15:45:26 -04:00
|
|
|
// if it isn't in the cache, we check if each source can open the file
|
2020-09-09 14:35:52 -04:00
|
|
|
for idx := range l.Sources {
|
|
|
|
source := l.Sources[idx]
|
2020-09-08 15:45:26 -04:00
|
|
|
|
|
|
|
// if the source can open the file, then we cache it and return it
|
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset
* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file
* fixed log level tests
2020-11-02 21:23:07 -05:00
|
|
|
loadedAsset, err := source.Open(subPath)
|
|
|
|
if err != nil {
|
|
|
|
l.Debug(fmt.Sprintf("Checked `%s`, file not found", source.Path()))
|
|
|
|
continue
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset
* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file
* fixed log level tests
2020-11-02 21:23:07 -05:00
|
|
|
|
2020-11-03 07:54:15 -05:00
|
|
|
srcBase, _ := filepath.Abs(source.Path())
|
2021-01-10 02:44:42 -05:00
|
|
|
l.Info(fmt.Sprintf("Loaded %s -> %s", srcBase, subPath))
|
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset
* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file
* fixed log level tests
2020-11-02 21:23:07 -05:00
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
return loadedAsset, nil
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
|
|
|
|
2020-09-14 14:47:11 -04:00
|
|
|
return nil, fmt.Errorf(errFmtFileNotFound, subPath)
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddSource adds an asset source with the given path. The path will either resolve to a directory
|
|
|
|
// or a file on the host filesystem. In the case that it is a file, the file extension is used
|
|
|
|
// to determine the type of asset source. In the case that the path points to a directory, a
|
|
|
|
// FileSystemSource will be added.
|
2021-01-10 02:44:42 -05:00
|
|
|
func (l *Loader) AddSource(path string, sourceType types.SourceType) error {
|
2020-09-09 14:35:52 -04:00
|
|
|
if l.Sources == nil {
|
|
|
|
l.Sources = make([]asset.Source, 0)
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanPath := filepath.Clean(path)
|
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
source, err := l.LoaderProviders[sourceType](cleanPath)
|
|
|
|
|
2020-09-08 15:45:26 -04:00
|
|
|
if err != nil {
|
2021-01-10 02:44:42 -05:00
|
|
|
return err
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
l.Infof("Adding source: '%s'", cleanPath)
|
|
|
|
l.Sources = append(l.Sources, source)
|
2020-09-08 15:45:26 -04:00
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-08 15:45:26 -04:00
|
|
|
|
2021-01-11 04:31:57 -05:00
|
|
|
// Exists checks if the given path exists in at least one source
|
2021-01-10 02:44:42 -05:00
|
|
|
func (l *Loader) Exists(subPath string) bool {
|
|
|
|
subPath = filepath.Clean(subPath)
|
2020-09-08 15:45:26 -04:00
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
if l.language != nil {
|
|
|
|
charset := l.charset
|
|
|
|
language := l.language
|
|
|
|
|
|
|
|
subPath = strings.ReplaceAll(subPath, fontToken, *charset)
|
|
|
|
subPath = strings.ReplaceAll(subPath, tableToken, *language)
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
// if it isn't in the cache, we check if each source can open the file
|
|
|
|
for idx := range l.Sources {
|
|
|
|
source := l.Sources[idx]
|
2020-09-09 14:35:52 -04:00
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
// if the source can open the file, then we cache it and return it
|
|
|
|
if source.Exists(subPath) {
|
|
|
|
return true
|
2020-09-09 14:35:52 -04:00
|
|
|
}
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
2020-09-09 14:35:52 -04:00
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
return false
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|