2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
|
|
|
|
|
|
|
import (
|
2020-09-08 15:58:35 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2cache"
|
2020-02-26 08:39:38 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dat"
|
2020-07-03 21:23:44 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-02-01 18:55:56 -05:00
|
|
|
)
|
|
|
|
|
2020-07-13 09:05:04 -04:00
|
|
|
// Static checks to confirm struct conforms to interface
|
2020-08-04 22:53:36 -04:00
|
|
|
var _ d2interface.PaletteManager = &paletteManager{}
|
2020-07-13 09:05:04 -04:00
|
|
|
var _ d2interface.Cacher = &paletteManager{}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
type paletteManager struct {
|
2020-07-03 21:23:44 -04:00
|
|
|
cache d2interface.Cache
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
paletteBudget = 64
|
|
|
|
)
|
|
|
|
|
2020-08-04 22:53:36 -04:00
|
|
|
func createPaletteManager() d2interface.PaletteManager {
|
2020-09-08 15:58:35 -04:00
|
|
|
return &paletteManager{d2cache.CreateCache(paletteBudget)}
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
2020-07-05 15:56:48 -04:00
|
|
|
// LoadPalette loads a palette from archives managed by the ArchiveManager
|
2020-07-05 13:01:44 -04:00
|
|
|
func (pm *paletteManager) LoadPalette(palettePath string) (d2interface.Palette, error) {
|
2020-02-01 18:55:56 -05:00
|
|
|
if palette, found := pm.cache.Retrieve(palettePath); found {
|
2020-07-05 13:01:44 -04:00
|
|
|
return palette.(d2interface.Palette), nil
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
paletteData, err := LoadFile(palettePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-28 22:32:34 -04:00
|
|
|
palette, err := d2dat.Load(paletteData)
|
2020-02-26 08:39:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:09:18 -04:00
|
|
|
if err := pm.cache.Insert(palettePath, palette, 1); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-26 08:39:38 -05:00
|
|
|
return palette, nil
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
2020-07-05 15:56:48 -04:00
|
|
|
|
|
|
|
// ClearCache clears the palette cache
|
|
|
|
func (pm *paletteManager) ClearCache() {
|
|
|
|
pm.cache.Clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCache returns the palette managers cache
|
|
|
|
func (pm *paletteManager) GetCache() d2interface.Cache {
|
|
|
|
return pm.cache
|
|
|
|
}
|