2019-12-21 20:53:18 -05:00
|
|
|
package d2asset
|
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
type animationManager struct {
|
|
|
|
cache *cache
|
|
|
|
}
|
|
|
|
|
|
|
|
func createAnimationManager() *animationManager {
|
|
|
|
return &animationManager{cache: createCache(AnimationBudget)}
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
func (sm *animationManager) loadAnimation(animationPath, palettePath string, transparency int) (*Animation, error) {
|
|
|
|
cachePath := fmt.Sprintf("%s;%s;%d", animationPath, palettePath, transparency)
|
2019-12-21 20:53:18 -05:00
|
|
|
if animation, found := sm.cache.retrieve(cachePath); found {
|
|
|
|
return animation.(*Animation).clone(), nil
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
var animation *Animation
|
|
|
|
switch strings.ToLower(filepath.Ext(animationPath)) {
|
|
|
|
case ".dc6":
|
|
|
|
dc6, err := loadDC6(animationPath, palettePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
animation, err = createAnimationFromDC6(dc6)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case ".dcc":
|
|
|
|
dcc, err := loadDCC(animationPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
palette, err := loadPalette(palettePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
animation, err = createAnimationFromDCC(dcc, palette, transparency)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unknown animation format")
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
if err := sm.cache.insert(cachePath, animation.clone(), 1); err != nil {
|
2019-12-21 20:53:18 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
return animation, nil
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|