2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
animationBudget = 64
|
|
|
|
)
|
|
|
|
|
|
|
|
type animationManager struct {
|
|
|
|
cache *d2common.Cache
|
|
|
|
}
|
|
|
|
|
|
|
|
func createAnimationManager() *animationManager {
|
|
|
|
return &animationManager{d2common.CreateCache(animationBudget)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *animationManager) loadAnimation(animationPath, palettePath string, transparency int) (*Animation, error) {
|
|
|
|
cachePath := fmt.Sprintf("%s;%s;%d", animationPath, palettePath, transparency)
|
|
|
|
if animation, found := am.cache.Retrieve(cachePath); found {
|
|
|
|
return animation.(*Animation).Clone(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var animation *Animation
|
2020-07-01 14:09:18 -04:00
|
|
|
|
2020-02-22 20:44:30 -05:00
|
|
|
ext := strings.ToLower(filepath.Ext(animationPath))
|
|
|
|
switch ext {
|
2020-02-01 18:55:56 -05:00
|
|
|
case ".dc6":
|
2020-02-26 08:39:38 -05:00
|
|
|
dc6, err := loadDC6(animationPath)
|
2020-02-01 18:55:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:46:47 -05:00
|
|
|
palette, err := LoadPalette(palettePath)
|
2020-02-26 08:39:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-28 12:10:25 -04:00
|
|
|
animation, err = CreateAnimationFromDC6(dc6, palette)
|
2020-02-01 18:55:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case ".dcc":
|
|
|
|
dcc, err := loadDCC(animationPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:46:47 -05:00
|
|
|
palette, err := LoadPalette(palettePath)
|
2020-02-01 18:55:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-28 12:10:25 -04:00
|
|
|
animation, err = CreateAnimationFromDCC(dcc, palette, transparency)
|
2020-02-01 18:55:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
2020-02-22 20:44:30 -05:00
|
|
|
return nil, fmt.Errorf("unknown animation format: %s", ext)
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := am.cache.Insert(cachePath, animation.Clone(), 1); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return animation, nil
|
|
|
|
}
|