2019-10-25 19:12:42 -04:00
|
|
|
package Common
|
2019-10-24 09:31:59 -04:00
|
|
|
|
2019-11-01 14:12:23 -04:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/essial/OpenDiablo2/PaletteDefs"
|
|
|
|
)
|
2019-10-25 18:40:27 -04:00
|
|
|
|
|
|
|
// PaletteRGB represents a color in a palette
|
2019-10-24 09:31:59 -04:00
|
|
|
type PaletteRGB struct {
|
|
|
|
R, G, B uint8
|
|
|
|
}
|
|
|
|
|
2019-11-01 14:12:23 -04:00
|
|
|
// PaletteType represents a palette
|
|
|
|
type PaletteRec struct {
|
|
|
|
Name PaletteDefs.PaletteType
|
2019-10-24 09:31:59 -04:00
|
|
|
Colors [256]PaletteRGB
|
|
|
|
}
|
|
|
|
|
2019-11-01 14:12:23 -04:00
|
|
|
var Palettes map[PaletteDefs.PaletteType]PaletteRec
|
|
|
|
|
2019-10-24 09:31:59 -04:00
|
|
|
// CreatePalette creates a palette
|
2019-11-01 14:12:23 -04:00
|
|
|
func CreatePalette(name PaletteDefs.PaletteType, data []byte) PaletteRec {
|
|
|
|
result := PaletteRec{Name: name}
|
2019-10-24 09:31:59 -04:00
|
|
|
|
|
|
|
for i := 0; i <= 255; i++ {
|
|
|
|
result.Colors[i] = PaletteRGB{
|
|
|
|
B: data[i*3],
|
|
|
|
G: data[(i*3)+1],
|
|
|
|
R: data[(i*3)+2],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2019-11-01 14:12:23 -04:00
|
|
|
|
|
|
|
func LoadPalettes(mpqFiles map[string]*MpqFileRecord, fileProvider FileProvider) {
|
|
|
|
Palettes = make(map[PaletteDefs.PaletteType]PaletteRec)
|
|
|
|
for file := range mpqFiles {
|
|
|
|
if strings.Index(file, "/data/global/palette/") != 0 || strings.Index(file, ".dat") != len(file)-4 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nameParts := strings.Split(file, `/`)
|
|
|
|
paletteName := PaletteDefs.PaletteType(nameParts[len(nameParts)-2])
|
|
|
|
palette := CreatePalette(paletteName, fileProvider.LoadFile(file))
|
|
|
|
Palettes[paletteName] = palette
|
|
|
|
}
|
|
|
|
log.Printf("Loaded %d palettes", len(Palettes))
|
|
|
|
}
|