2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
|
|
|
|
|
|
|
import (
|
2020-07-04 00:49:16 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
|
2020-02-01 18:55:56 -05:00
|
|
|
"log"
|
|
|
|
|
2020-07-03 22:52:50 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-02-26 22:46:47 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dat"
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq"
|
2020-06-28 21:40:52 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-02-01 18:55:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var singleton *assetManager
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
// Initialize creates and assigns all necessary dependencies for the assetManager top-level functions to work correctly
|
2020-07-04 00:49:16 -04:00
|
|
|
func Initialize(renderer d2interface.Renderer,
|
|
|
|
term d2interface.Terminal) error {
|
2020-02-01 18:55:56 -05:00
|
|
|
var (
|
2020-04-11 14:56:47 -04:00
|
|
|
config = d2config.Get()
|
2020-07-04 00:49:16 -04:00
|
|
|
archiveManager = createArchiveManager(config)
|
|
|
|
fileManager = createFileManager(config, archiveManager)
|
2020-04-11 14:56:47 -04:00
|
|
|
paletteManager = createPaletteManager()
|
|
|
|
paletteTransformManager = createPaletteTransformManager()
|
2020-07-03 14:00:56 -04:00
|
|
|
animationManager = createAnimationManager(renderer)
|
2020-04-11 14:56:47 -04:00
|
|
|
fontManager = createFontManager()
|
2020-02-01 18:55:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
singleton = &assetManager{
|
|
|
|
archiveManager,
|
|
|
|
fileManager,
|
|
|
|
paletteManager,
|
2020-02-26 22:52:05 -05:00
|
|
|
paletteTransformManager,
|
2020-02-01 18:55:56 -05:00
|
|
|
animationManager,
|
2020-02-17 22:11:52 -05:00
|
|
|
fontManager,
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
if err := term.BindAction("assetspam", "display verbose asset manager logs", func(verbose bool) {
|
2020-02-01 18:55:56 -05:00
|
|
|
if verbose {
|
2020-06-30 17:04:41 -04:00
|
|
|
term.OutputInfof("asset manager verbose logging enabled")
|
2020-02-01 18:55:56 -05:00
|
|
|
} else {
|
2020-06-30 17:04:41 -04:00
|
|
|
term.OutputInfof("asset manager verbose logging disabled")
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
archiveManager.cache.SetVerbose(verbose)
|
|
|
|
fileManager.cache.SetVerbose(verbose)
|
|
|
|
paletteManager.cache.SetVerbose(verbose)
|
2020-02-26 22:52:05 -05:00
|
|
|
paletteTransformManager.cache.SetVerbose(verbose)
|
2020-02-01 18:55:56 -05:00
|
|
|
animationManager.cache.SetVerbose(verbose)
|
2020-07-01 14:09:18 -04:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := term.BindAction("assetstat", "display asset manager cache statistics", func() {
|
|
|
|
|
2020-07-04 00:49:16 -04:00
|
|
|
var cacheStatistics = func(c d2interface.Cache) float64 {
|
2020-07-01 14:09:18 -04:00
|
|
|
const percent = 100.0
|
|
|
|
return float64(c.GetWeight()) / float64(c.GetBudget()) * percent
|
|
|
|
}
|
|
|
|
|
|
|
|
term.OutputInfof("archive cache: %f", cacheStatistics(archiveManager.cache))
|
|
|
|
term.OutputInfof("file cache: %f", cacheStatistics(fileManager.cache))
|
|
|
|
term.OutputInfof("palette cache: %f", cacheStatistics(paletteManager.cache))
|
|
|
|
term.OutputInfof("palette transform cache: %f", cacheStatistics(paletteTransformManager.cache))
|
|
|
|
term.OutputInfof("animation cache: %f", cacheStatistics(animationManager.cache))
|
|
|
|
term.OutputInfof("font cache: %f", cacheStatistics(fontManager.cache))
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := term.BindAction("assetclear", "clear asset manager cache", func() {
|
2020-02-01 18:55:56 -05:00
|
|
|
archiveManager.cache.Clear()
|
|
|
|
fileManager.cache.Clear()
|
|
|
|
paletteManager.cache.Clear()
|
2020-02-26 22:52:05 -05:00
|
|
|
paletteTransformManager.cache.Clear()
|
2020-02-01 18:55:56 -05:00
|
|
|
animationManager.cache.Clear()
|
2020-02-17 22:11:52 -05:00
|
|
|
fontManager.cache.Clear()
|
2020-07-01 14:09:18 -04:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-01 18:55:56 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
// LoadFileStream streams an MPQ file from a source file path
|
2020-06-27 02:49:27 -04:00
|
|
|
func LoadFileStream(filePath string) (*d2mpq.MpqDataStream, error) {
|
|
|
|
data, err := singleton.fileManager.loadFileStream(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error loading file stream %s (%v)", filePath, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
// LoadFile loads an entire file from a source file path as a []byte
|
2020-02-01 18:55:56 -05:00
|
|
|
func LoadFile(filePath string) ([]byte, error) {
|
|
|
|
data, err := singleton.fileManager.loadFile(filePath)
|
|
|
|
if err != nil {
|
2020-06-22 17:36:45 -04:00
|
|
|
log.Printf("error loading file %s (%v)", filePath, err.Error())
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
// FileExists checks if a file exists on the underlying file system at the given file path.
|
2020-02-01 18:55:56 -05:00
|
|
|
func FileExists(filePath string) (bool, error) {
|
|
|
|
return singleton.fileManager.fileExists(filePath)
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
// LoadAnimation loads an animation by its resource path and its palette path
|
2020-02-01 18:55:56 -05:00
|
|
|
func LoadAnimation(animationPath, palettePath string) (*Animation, error) {
|
|
|
|
return LoadAnimationWithTransparency(animationPath, palettePath, 255)
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
// LoadAnimationWithTransparency loads an animation by its resource path and its palette path with a given transparency value
|
2020-02-01 18:55:56 -05:00
|
|
|
func LoadAnimationWithTransparency(animationPath, palettePath string, transparency int) (*Animation, error) {
|
|
|
|
return singleton.animationManager.loadAnimation(animationPath, palettePath, transparency)
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
// LoadComposite creates a composite object from a ObjectLookupRecord and palettePath describing it
|
2020-07-03 22:52:50 -04:00
|
|
|
func LoadComposite(baseType d2enum.ObjectType, token, palettePath string) (*Composite, error) {
|
|
|
|
return CreateComposite(baseType, token, palettePath), nil
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
2020-02-09 14:12:04 -05:00
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
// LoadFont loads a font the resource files
|
2020-02-17 22:11:52 -05:00
|
|
|
func LoadFont(tablePath, spritePath, palettePath string) (*Font, error) {
|
|
|
|
return singleton.fontManager.loadFont(tablePath, spritePath, palettePath)
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
// LoadPalette loads a palette from a given palette path
|
2020-02-26 22:46:47 -05:00
|
|
|
func LoadPalette(palettePath string) (*d2dat.DATPalette, error) {
|
|
|
|
return singleton.paletteManager.loadPalette(palettePath)
|
|
|
|
}
|