2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
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-04-11 14:56:47 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2pl2"
|
2020-02-01 21:06:22 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2term"
|
|
|
|
)
|
|
|
|
|
|
|
|
var singleton *assetManager
|
|
|
|
|
|
|
|
func Initialize() error {
|
2020-02-09 14:12:04 -05:00
|
|
|
verifyNotInit()
|
2020-02-01 18:55:56 -05:00
|
|
|
|
|
|
|
var (
|
2020-04-11 14:56:47 -04:00
|
|
|
config = d2config.Get()
|
|
|
|
archiveManager = createArchiveManager(config)
|
|
|
|
fileManager = createFileManager(config, archiveManager)
|
|
|
|
paletteManager = createPaletteManager()
|
|
|
|
paletteTransformManager = createPaletteTransformManager()
|
|
|
|
animationManager = createAnimationManager()
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
d2term.BindAction("assetspam", "display verbose asset manager logs", func(verbose bool) {
|
|
|
|
if verbose {
|
|
|
|
d2term.OutputInfo("asset manager verbose logging enabled")
|
|
|
|
} else {
|
|
|
|
d2term.OutputInfo("asset manager verbose logging disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
d2term.BindAction("assetstat", "display asset manager cache statistics", func() {
|
2020-02-17 22:11:52 -05:00
|
|
|
d2term.OutputInfo("archive cache: %f", float64(archiveManager.cache.GetWeight())/float64(archiveManager.cache.GetBudget())*100.0)
|
|
|
|
d2term.OutputInfo("file cache: %f", float64(fileManager.cache.GetWeight())/float64(fileManager.cache.GetBudget())*100.0)
|
|
|
|
d2term.OutputInfo("palette cache: %f", float64(paletteManager.cache.GetWeight())/float64(paletteManager.cache.GetBudget())*100.0)
|
2020-02-26 22:52:05 -05:00
|
|
|
d2term.OutputInfo("palette transform cache: %f", float64(paletteTransformManager.cache.GetWeight())/float64(paletteTransformManager.cache.GetBudget())*100.0)
|
2020-02-17 22:11:52 -05:00
|
|
|
d2term.OutputInfo("animation cache: %f", float64(animationManager.cache.GetWeight())/float64(animationManager.cache.GetBudget())*100.0)
|
|
|
|
d2term.OutputInfo("font cache: %f", float64(fontManager.cache.GetWeight())/float64(fontManager.cache.GetBudget())*100.0)
|
2020-02-01 18:55:56 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
d2term.BindAction("assetclear", "clear asset manager cache", func() {
|
|
|
|
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-02-01 18:55:56 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Shutdown() {
|
|
|
|
singleton = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadArchive(archivePath string) (*d2mpq.MPQ, error) {
|
2020-02-09 14:12:04 -05:00
|
|
|
verifyWasInit()
|
2020-02-01 18:55:56 -05:00
|
|
|
return singleton.archiveManager.loadArchive(archivePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadFile(filePath string) ([]byte, error) {
|
2020-02-09 14:12:04 -05:00
|
|
|
verifyWasInit()
|
2020-02-01 18:55:56 -05:00
|
|
|
|
|
|
|
data, err := singleton.fileManager.loadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error loading file %s (%v)", filePath, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func FileExists(filePath string) (bool, error) {
|
2020-02-09 14:12:04 -05:00
|
|
|
verifyWasInit()
|
2020-02-01 18:55:56 -05:00
|
|
|
return singleton.fileManager.fileExists(filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadAnimation(animationPath, palettePath string) (*Animation, error) {
|
2020-02-17 22:11:52 -05:00
|
|
|
verifyWasInit()
|
2020-02-01 18:55:56 -05:00
|
|
|
return LoadAnimationWithTransparency(animationPath, palettePath, 255)
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:52:05 -05:00
|
|
|
func LoadPaletteTransform(pl2Path string) (*d2pl2.PL2File, error) {
|
|
|
|
verifyWasInit()
|
|
|
|
return singleton.paletteTransformManager.loadPaletteTransform(pl2Path)
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
func LoadAnimationWithTransparency(animationPath, palettePath string, transparency int) (*Animation, error) {
|
2020-02-09 14:12:04 -05:00
|
|
|
verifyWasInit()
|
2020-02-01 18:55:56 -05:00
|
|
|
return singleton.animationManager.loadAnimation(animationPath, palettePath, transparency)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadComposite(object *d2datadict.ObjectLookupRecord, palettePath string) (*Composite, error) {
|
2020-02-17 22:11:52 -05:00
|
|
|
verifyWasInit()
|
2020-02-01 18:55:56 -05:00
|
|
|
return CreateComposite(object, palettePath), nil
|
|
|
|
}
|
2020-02-09 14:12:04 -05:00
|
|
|
|
2020-02-17 22:11:52 -05:00
|
|
|
func LoadFont(tablePath, spritePath, palettePath string) (*Font, error) {
|
|
|
|
verifyWasInit()
|
|
|
|
return singleton.fontManager.loadFont(tablePath, spritePath, palettePath)
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:46:47 -05:00
|
|
|
func LoadPalette(palettePath string) (*d2dat.DATPalette, error) {
|
|
|
|
verifyWasInit()
|
|
|
|
return singleton.paletteManager.loadPalette(palettePath)
|
|
|
|
}
|
|
|
|
|
2020-02-09 14:12:04 -05:00
|
|
|
func verifyWasInit() {
|
|
|
|
if singleton == nil {
|
|
|
|
panic(ErrNotInit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyNotInit() {
|
|
|
|
if singleton != nil {
|
|
|
|
panic(ErrWasInit)
|
|
|
|
}
|
|
|
|
}
|