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-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)
|
2020-07-05 00:00:00 -04:00
|
|
|
archivedFileManager = 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,
|
2020-07-05 00:00:00 -04:00
|
|
|
archivedFileManager,
|
2020-02-01 18:55:56 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-05 00:00:00 -04:00
|
|
|
archiveManager.GetCache().SetVerbose(verbose)
|
|
|
|
archivedFileManager.GetCache().SetVerbose(verbose)
|
2020-07-05 15:56:48 -04:00
|
|
|
paletteManager.GetCache().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
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
term.OutputInfof("archive cache: %f", cacheStatistics(archiveManager.GetCache()))
|
2020-07-05 00:00:00 -04:00
|
|
|
term.OutputInfof("file cache: %f", cacheStatistics(archivedFileManager.GetCache()))
|
2020-07-05 15:56:48 -04:00
|
|
|
term.OutputInfof("palette cache: %f", cacheStatistics(paletteManager.GetCache()))
|
2020-07-01 14:09:18 -04:00
|
|
|
term.OutputInfof("palette transform cache: %f", cacheStatistics(paletteTransformManager.cache))
|
|
|
|
term.OutputInfof("animation cache: %f", cacheStatistics(animationManager.cache))
|
2020-07-05 15:56:48 -04:00
|
|
|
term.OutputInfof("font cache: %f", cacheStatistics(fontManager.GetCache()))
|
2020-07-01 14:09:18 -04:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := term.BindAction("assetclear", "clear asset manager cache", func() {
|
2020-07-04 22:37:13 -04:00
|
|
|
archiveManager.ClearCache()
|
2020-07-05 00:00:00 -04:00
|
|
|
archivedFileManager.GetCache().Clear()
|
2020-07-05 15:56:48 -04:00
|
|
|
paletteManager.ClearCache()
|
2020-02-26 22:52:05 -05:00
|
|
|
paletteTransformManager.cache.Clear()
|
2020-07-05 15:56:48 -04:00
|
|
|
animationManager.ClearCache()
|
|
|
|
fontManager.ClearCache()
|
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-07-04 22:37:13 -04:00
|
|
|
func LoadFileStream(filePath string) (d2interface.ArchiveDataStream, error) {
|
2020-07-05 00:00:00 -04:00
|
|
|
data, err := singleton.archivedFileManager.LoadFileStream(filePath)
|
2020-06-27 02:49:27 -04:00
|
|
|
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) {
|
2020-07-05 00:00:00 -04:00
|
|
|
data, err := singleton.archivedFileManager.LoadFile(filePath)
|
2020-02-01 18:55:56 -05:00
|
|
|
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) {
|
2020-07-05 00:00:00 -04:00
|
|
|
return singleton.archivedFileManager.FileExists(filePath)
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
// LoadAnimation loads an animation by its resource path and its palette path
|
2020-07-05 13:01:44 -04:00
|
|
|
func LoadAnimation(animationPath, palettePath string) (d2interface.Animation, error) {
|
2020-02-01 18:55:56 -05:00
|
|
|
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-07-05 13:01:44 -04:00
|
|
|
func LoadAnimationWithTransparency(animationPath, palettePath string,
|
|
|
|
transparency int) (d2interface.Animation, error) {
|
|
|
|
return singleton.animationManager.LoadAnimation(animationPath, palettePath, transparency)
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
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-07-05 15:56:48 -04:00
|
|
|
func LoadFont(tablePath, spritePath, palettePath string) (d2interface.Font, error) {
|
|
|
|
return singleton.fontManager.LoadFont(tablePath, spritePath, palettePath)
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
// LoadPalette loads a palette from a given palette path
|
2020-07-05 13:01:44 -04:00
|
|
|
func LoadPalette(palettePath string) (d2interface.Palette, error) {
|
|
|
|
return singleton.paletteManager.LoadPalette(palettePath)
|
2020-02-26 22:46:47 -05:00
|
|
|
}
|