Skip to content

Commit

Permalink
Feature pl2 asset manager (OpenDiablo2#319)
Browse files Browse the repository at this point in the history
* adding pl2 as a fiel format and an asset manager for pl2 files

* adding pl2 as a file format and an asset manager for pl2 files

* pl2 asset manager and file format

* added call in main.go to load palette transforms
* removed incorrect PaletteTransformType declarations from main.go

* removingn bad resource path for palette transform

* PL2 file format added, added to asset management

PL2 files are beside palette files in the mpq
These files define palette transforms of their respective base palette.

I've bound a command to d2term (in d2asset.go)
	load_pl2 <path>

* Clean up PL2 asset stuff

removed pl2 stuff from d2datadict, didn't belong in there
removed d2term debug binding

* minor cleanup for d2pl2
  • Loading branch information
gravestench authored Feb 27, 2020
1 parent 664b841 commit 019bb92
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 11 deletions.
63 changes: 63 additions & 0 deletions d2common/d2fileformats/d2pl2/pl2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package d2pl2

import (
"encoding/binary"

"github.com/go-restruct/restruct"
)

type PL2File struct {
BasePalette PL2Palette

LightLevelVariations [32]PL2PaletteTransform
InvColorVariations [16]PL2PaletteTransform
SelectedUintShift PL2PaletteTransform
AlphaBlend [3][256]PL2PaletteTransform
AdditiveBlend [256]PL2PaletteTransform
MultiplicativeBlend [256]PL2PaletteTransform
HueVariations [111]PL2PaletteTransform
RedTones PL2PaletteTransform
GreenTones PL2PaletteTransform
BlueTones PL2PaletteTransform
UnknownVariations [14]PL2PaletteTransform
MaxComponentBlend [256]PL2PaletteTransform
DarkendColorShift PL2PaletteTransform

TextColors [13]PL2Color24Bits
TextColorShifts [13]PL2PaletteTransform
}

type PL2Color struct {
R uint8
G uint8
B uint8
_ uint8
}

type PL2Color24Bits struct {
R uint8
G uint8
B uint8
}

type PL2Palette struct {
Colors [256]PL2Color
}

type PL2PaletteTransform struct {
Indices [256]uint8
}

// uses restruct to read the binary dc6 data into structs
func LoadPL2(data []byte) (*PL2File, error) {
result := &PL2File{}

restruct.EnableExprBeta()

err := restruct.Unpack(data, binary.LittleEndian, &result)
if err != nil {
return nil, err
}

return result, nil
}
19 changes: 19 additions & 0 deletions d2common/d2resource/resource_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,23 @@ const (
PaletteStatic = "/data/global/palette/static/pal.dat"
PaletteTrademark = "/data/global/palette/trademark/pal.dat"
PaletteUnits = "/data/global/palette/units/pal.dat"

// --- Palette Transforms ---

PaletteTransformAct1 = "/data/global/palette/act1/Pal.pl2"
PaletteTransformAct2 = "/data/global/palette/act2/Pal.pl2"
PaletteTransformAct3 = "/data/global/palette/act3/Pal.pl2"
PaletteTransformAct4 = "/data/global/palette/act4/Pal.pl2"
PaletteTransformAct5 = "/data/global/palette/act5/Pal.pl2"
PaletteTransformEndGame = "/data/global/palette/endgame/Pal.pl2"
PaletteTransformEndGame2 = "/data/global/palette/endgame2/Pal.pl2"
PaletteTransformFechar = "/data/global/palette/fechar/Pal.pl2"
PaletteTransformLoading = "/data/global/palette/loading/Pal.pl2"
PaletteTransformMenu0 = "/data/global/palette/menu0/Pal.pl2"
PaletteTransformMenu1 = "/data/global/palette/menu1/Pal.pl2"
PaletteTransformMenu2 = "/data/global/palette/menu2/Pal.pl2"
PaletteTransformMenu3 = "/data/global/palette/menu3/Pal.pl2"
PaletteTransformMenu4 = "/data/global/palette/menu4/Pal.pl2"
PaletteTransformSky = "/data/global/palette/sky/Pal.pl2"
PaletteTransformTrademark = "/data/global/palette/trademark/Pal.pl2"
)
11 changes: 6 additions & 5 deletions d2core/d2asset/asset_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ var (
)

type assetManager struct {
archiveManager *archiveManager
fileManager *fileManager
paletteManager *paletteManager
animationManager *animationManager
fontManager *fontManager
archiveManager *archiveManager
fileManager *fileManager
paletteManager *paletteManager
paletteTransformManager *paletteTransformManager
animationManager *animationManager
fontManager *fontManager
}

func loadDC6(dc6Path string) (*d2dc6.DC6File, error) {
Expand Down
23 changes: 17 additions & 6 deletions d2core/d2asset/d2asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2term"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2pl2"
)

var singleton *assetManager
Expand All @@ -17,18 +18,20 @@ func Initialize() error {
verifyNotInit()

var (
config = d2config.Get()
archiveManager = createArchiveManager(config)
fileManager = createFileManager(config, archiveManager)
paletteManager = createPaletteManager()
animationManager = createAnimationManager()
fontManager = createFontManager()
config = d2config.Get()
archiveManager = createArchiveManager(config)
fileManager = createFileManager(config, archiveManager)
paletteManager = createPaletteManager()
paletteTransformManager = createPaletteTransformManager()
animationManager = createAnimationManager()
fontManager = createFontManager()
)

singleton = &assetManager{
archiveManager,
fileManager,
paletteManager,
paletteTransformManager,
animationManager,
fontManager,
}
Expand All @@ -43,13 +46,15 @@ func Initialize() error {
archiveManager.cache.SetVerbose(verbose)
fileManager.cache.SetVerbose(verbose)
paletteManager.cache.SetVerbose(verbose)
paletteTransformManager.cache.SetVerbose(verbose)
animationManager.cache.SetVerbose(verbose)
})

d2term.BindAction("assetstat", "display asset manager cache statistics", func() {
d2term.OutputInfo("archive cache: %f", float64(archiveManager.cache.GetWeight())/float64(archiveManager.cache.GetBudget())*100.0)
d2term.OutputInfo("file cache: %f", float64(fileManager.cache.GetWeight())/float64(fileManager.cache.GetBudget())*100.0)
d2term.OutputInfo("palette cache: %f", float64(paletteManager.cache.GetWeight())/float64(paletteManager.cache.GetBudget())*100.0)
d2term.OutputInfo("palette transform cache: %f", float64(paletteTransformManager.cache.GetWeight())/float64(paletteTransformManager.cache.GetBudget())*100.0)
d2term.OutputInfo("animation cache: %f", float64(animationManager.cache.GetWeight())/float64(animationManager.cache.GetBudget())*100.0)
d2term.OutputInfo("font cache: %f", float64(fontManager.cache.GetWeight())/float64(fontManager.cache.GetBudget())*100.0)
})
Expand All @@ -58,6 +63,7 @@ func Initialize() error {
archiveManager.cache.Clear()
fileManager.cache.Clear()
paletteManager.cache.Clear()
paletteTransformManager.cache.Clear()
animationManager.cache.Clear()
fontManager.cache.Clear()
})
Expand Down Expand Up @@ -95,6 +101,11 @@ func LoadAnimation(animationPath, palettePath string) (*Animation, error) {
return LoadAnimationWithTransparency(animationPath, palettePath, 255)
}

func LoadPaletteTransform(pl2Path string) (*d2pl2.PL2File, error) {
verifyWasInit()
return singleton.paletteTransformManager.loadPaletteTransform(pl2Path)
}

func LoadAnimationWithTransparency(animationPath, palettePath string, transparency int) (*Animation, error) {
verifyWasInit()
return singleton.animationManager.loadAnimation(animationPath, palettePath, transparency)
Expand Down
37 changes: 37 additions & 0 deletions d2core/d2asset/palette_transform_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package d2asset

import (
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2pl2"
)

type paletteTransformManager struct {
cache *d2common.Cache
}

const (
paletteTransformBudget = 64
)

func createPaletteTransformManager() *paletteTransformManager {
return &paletteTransformManager{d2common.CreateCache(paletteTransformBudget)}
}

func (pm *paletteTransformManager) loadPaletteTransform(path string) (*d2pl2.PL2File, error) {
if pl2, found := pm.cache.Retrieve(path); found {
return pl2.(*d2pl2.PL2File), nil
}

data, err := LoadFile(path);
if err != nil {
return nil, err
}

pl2, err := d2pl2.LoadPL2(data)
if err != nil {
return nil, err
}

pm.cache.Insert(path, pl2, 1)
return pl2, nil
}

0 comments on commit 019bb92

Please sign in to comment.