refactor storage format and bpe handling; add redirecting bpe support and utility function for next largest number

This commit is contained in:
adman
2026-05-06 16:47:50 +02:00
parent be6d2fb461
commit c93d907d46
3 changed files with 26 additions and 21 deletions
+1
View File
@@ -42,6 +42,7 @@ func MakeBiomeFormat(version string) StorageFormat {
func MakeBlockFormat(version string) StorageFormat { func MakeBlockFormat(version string) StorageFormat {
directBpe := uint8(math.Ceil(math.Log2(float64(len(data.BlocksForVersion(version)))))) directBpe := uint8(math.Ceil(math.Log2(float64(len(data.BlocksForVersion(version))))))
return StorageFormat{ return StorageFormat{
RedirectingBpes: []uint8{1, 2, 3},
AvailableBpes: []uint8{0, 4, 5, 6, 7, 8, directBpe}, AvailableBpes: []uint8{0, 4, 5, 6, 7, 8, directBpe},
BiggestDirect: true, BiggestDirect: true,
Len: BlocksPerChunkSection, Len: BlocksPerChunkSection,
+10 -15
View File
@@ -6,9 +6,12 @@ import (
"io" "io"
"math" "math"
"slices" "slices"
"github.com/admin-else/strom/util"
) )
type StorageFormat struct { type StorageFormat struct {
RedirectingBpes []uint8 // block bpe 1 2 3 redirect to 4
AvailableBpes []uint8 AvailableBpes []uint8
BiggestDirect bool BiggestDirect bool
Len int32 Len int32
@@ -33,24 +36,16 @@ func (r StorageFormat) Import(data []uint64, bpe uint8, palette []int32) (s *Sto
s.resize(bpe) s.resize(bpe)
if !slices.Contains(r.AvailableBpes, bpe) { if !slices.Contains(r.AvailableBpes, bpe) {
var newBpe uint8 if !slices.Contains(r.RedirectingBpes, bpe) {
for _, newBpe = range r.AvailableBpes { err = NoAvailableBpeErr
if bpe <= newBpe { return
break
} }
} var found bool
var tmps *Storage bpe, found = util.GetNextLargestNumberSlice(bpe, r.AvailableBpes)
tmps, err = StorageFormat{ if !found {
AvailableBpes: []uint8{bpe}, err = NoAvailableBpeErr
BiggestDirect: false,
Len: r.Len,
}.ImportDataDirect(data)
if err != nil {
return return
} }
tmps.resize(newBpe)
data = tmps.data
bpe = newBpe
} }
if bpe == 0 && len(palette) != 1 { if bpe == 0 && len(palette) != 1 {
err = Bpe0PalletMustBeLenOneErr err = Bpe0PalletMustBeLenOneErr
+9
View File
@@ -85,3 +85,12 @@ func SliceRange[T Numeric](s, e T) (ret []T) {
func IsLastElement[T comparable](s []T, e T) bool { func IsLastElement[T comparable](s []T, e T) bool {
return len(s) > 0 && s[len(s)-1] == e return len(s) > 0 && s[len(s)-1] == e
} }
func GetNextLargestNumberSlice[T Numeric](a T, s []T) (ret T, found bool) {
for _, v := range s {
if v > a {
return v, true
}
}
return
}