2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-07-08 09:16:16 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
|
2020-02-01 18:55:56 -05:00
|
|
|
)
|
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
var singleton *assetManager //nolint:gochecknoglobals // Currently global by design
|
2020-02-01 18:55:56 -05:00
|
|
|
|
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-07-08 09:16:16 -04:00
|
|
|
archiveManager = createArchiveManager(d2config.Config)
|
|
|
|
archivedFileManager = createFileManager(d2config.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-13 20:30:30 -04:00
|
|
|
if term != nil {
|
|
|
|
return singleton.BindTerminalCommands(term)
|
2020-07-01 14:09:18 -04:00
|
|
|
}
|
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-07-08 17:46:45 -04:00
|
|
|
return LoadAnimationWithEffect(animationPath, palettePath, d2enum.DrawEffectNone)
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
2020-07-08 17:46:45 -04:00
|
|
|
// LoadAnimationWithEffect loads an animation by its resource path and its palette path with a given transparency value
|
|
|
|
func LoadAnimationWithEffect(animationPath, palettePath string,
|
|
|
|
drawEffect d2enum.DrawEffect) (d2interface.Animation, error) {
|
|
|
|
return singleton.animationManager.LoadAnimation(animationPath, palettePath, drawEffect)
|
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
|
|
|
}
|