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-06-28 21:40:52 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-02-01 18:55:56 -05:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
var singleton *assetManager
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
func Initialize(term d2interface.Terminal) 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
|
|
|
}
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
term.BindAction("assetspam", "display verbose asset manager logs", func(verbose bool) {
|
2020-02-01 18:55:56 -05:00
|
|
|
if verbose {
|
2020-06-28 21:40:52 -04:00
|
|
|
term.OutputInfo("asset manager verbose logging enabled")
|
2020-02-01 18:55:56 -05:00
|
|
|
} else {
|
2020-06-28 21:40:52 -04:00
|
|
|
term.OutputInfo("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-06-28 21:40:52 -04:00
|
|
|
term.BindAction("assetstat", "display asset manager cache statistics", func() {
|
|
|
|
term.OutputInfo("archive cache: %f", float64(archiveManager.cache.GetWeight())/float64(archiveManager.cache.GetBudget())*100.0)
|
|
|
|
term.OutputInfo("file cache: %f", float64(fileManager.cache.GetWeight())/float64(fileManager.cache.GetBudget())*100.0)
|
|
|
|
term.OutputInfo("palette cache: %f", float64(paletteManager.cache.GetWeight())/float64(paletteManager.cache.GetBudget())*100.0)
|
|
|
|
term.OutputInfo("palette transform cache: %f", float64(paletteTransformManager.cache.GetWeight())/float64(paletteTransformManager.cache.GetBudget())*100.0)
|
|
|
|
term.OutputInfo("animation cache: %f", float64(animationManager.cache.GetWeight())/float64(animationManager.cache.GetBudget())*100.0)
|
|
|
|
term.OutputInfo("font cache: %f", float64(fontManager.cache.GetWeight())/float64(fontManager.cache.GetBudget())*100.0)
|
2020-02-01 18:55:56 -05:00
|
|
|
})
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
|
2020-06-27 02:49:27 -04:00
|
|
|
func LoadFileStream(filePath string) (*d2mpq.MpqDataStream, error) {
|
|
|
|
verifyWasInit()
|
|
|
|
|
|
|
|
data, err := singleton.fileManager.loadFileStream(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error loading file stream %s (%v)", filePath, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
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 {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-28 22:32:34 -04:00
|
|
|
func LoadPaletteTransform(pl2Path string) (*d2pl2.PL2, error) {
|
2020-02-26 22:52:05 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|