2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
2020-02-26 08:39:38 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dat"
|
2020-02-01 18:55:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type paletteManager struct {
|
|
|
|
cache *d2common.Cache
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
paletteBudget = 64
|
|
|
|
)
|
|
|
|
|
|
|
|
func createPaletteManager() *paletteManager {
|
|
|
|
return &paletteManager{d2common.CreateCache(paletteBudget)}
|
|
|
|
}
|
|
|
|
|
2020-02-26 08:39:38 -05:00
|
|
|
func (pm *paletteManager) loadPalette(palettePath string) (*d2dat.DATPalette, error) {
|
2020-02-01 18:55:56 -05:00
|
|
|
if palette, found := pm.cache.Retrieve(palettePath); found {
|
2020-02-26 08:39:38 -05:00
|
|
|
return palette.(*d2dat.DATPalette), nil
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
paletteData, err := LoadFile(palettePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-26 08:39:38 -05:00
|
|
|
palette, err := d2dat.LoadDAT(paletteData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pm.cache.Insert(palettePath, palette, 1)
|
|
|
|
return palette, nil
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|