fix packs with same names causing issues

This commit is contained in:
olebeck 2023-04-07 17:40:29 +02:00
parent 2c87715966
commit c20e017f0d
2 changed files with 43 additions and 30 deletions

View File

@ -17,6 +17,7 @@ import (
"github.com/bedrock-tool/bedrocktool/ui/messages" "github.com/bedrock-tool/bedrocktool/ui/messages"
"github.com/bedrock-tool/bedrocktool/utils" "github.com/bedrock-tool/bedrocktool/utils"
"github.com/bedrock-tool/bedrocktool/utils/behaviourpack" "github.com/bedrock-tool/bedrocktool/utils/behaviourpack"
"github.com/flytam/filenamify"
"github.com/df-mc/dragonfly/server/block/cube" "github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/world" "github.com/df-mc/dragonfly/server/world"
@ -424,6 +425,26 @@ func (w *worldsHandler) SaveAndReset() {
w.serverState.worldCounter += 1 w.serverState.worldCounter += 1
if w.settings.SaveImage {
f, _ := os.Create(folder + ".png")
png.Encode(f, w.mapUI.ToImage())
f.Close()
}
w.AddPacks(folder)
// zip it
filename := folder + ".mcworld"
if err := utils.ZipFolder(filename, folder); err != nil {
logrus.Error(err)
}
logrus.Info(locale.Loc("saved", locale.Strmap{"Name": filename}))
//os.RemoveAll(folder)
w.Reset(w.CurrentName())
w.gui.Message(messages.SetUIState(messages.UIStateMain))
}
func (w *worldsHandler) AddPacks(folder string) {
type dep struct { type dep struct {
PackID string `json:"pack_id"` PackID string `json:"pack_id"`
Version [3]int `json:"version"` Version [3]int `json:"version"`
@ -470,22 +491,31 @@ func (w *worldsHandler) SaveAndReset() {
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
} else { } else {
packNames := make(map[string]int)
for _, pack := range packs {
packNames[pack.Name()] += 1
}
var rdeps []dep var rdeps []dep
for k, p := range packs { for _, pack := range packs {
if p.Encrypted() && !p.CanDecrypt() { if pack.Encrypted() && !pack.CanDecrypt() {
logrus.Warnf("Cant add %s, it is encrypted", p.Name()) logrus.Warnf("Cant add %s, it is encrypted", pack.Name())
continue continue
} }
logrus.Infof(locale.Loc("adding_pack", locale.Strmap{"Name": k})) logrus.Infof(locale.Loc("adding_pack", locale.Strmap{"Name": pack.Name()}))
name := p.Name()
name = strings.ReplaceAll(name, ":", "_") packName := pack.Name()
packFolder := path.Join(folder, "resource_packs", name) if packNames[packName] > 1 {
packName += "_" + pack.UUID()
}
packName, _ = filenamify.FilenamifyV2(packName)
packFolder := path.Join(folder, "resource_packs", packName)
os.MkdirAll(packFolder, 0o755) os.MkdirAll(packFolder, 0o755)
utils.UnpackZip(p, int64(p.Len()), packFolder) utils.UnpackZip(pack, int64(pack.Len()), packFolder)
rdeps = append(rdeps, dep{ rdeps = append(rdeps, dep{
PackID: p.Manifest().Header.UUID, PackID: pack.Manifest().Header.UUID,
Version: p.Manifest().Header.Version, Version: pack.Manifest().Header.Version,
}) })
} }
if len(rdeps) > 0 { if len(rdeps) > 0 {
@ -493,22 +523,6 @@ func (w *worldsHandler) SaveAndReset() {
} }
} }
} }
if w.settings.SaveImage {
f, _ := os.Create(folder + ".png")
png.Encode(f, w.mapUI.ToImage())
f.Close()
}
// zip it
filename := folder + ".mcworld"
if err := utils.ZipFolder(filename, folder); err != nil {
logrus.Error(err)
}
logrus.Info(locale.Loc("saved", locale.Strmap{"Name": filename}))
//os.RemoveAll(folder)
w.Reset(w.CurrentName())
w.gui.Message(messages.SetUIState(messages.UIStateMain))
} }
func (w *worldsHandler) OnConnect(err error) bool { func (w *worldsHandler) OnConnect(err error) bool {

View File

@ -60,8 +60,7 @@ var PackFromBase = func(pack *resource.Pack) Pack {
return b return b
} }
func GetPacks(server *minecraft.Conn) (packs map[string]Pack, err error) { func GetPacks(server *minecraft.Conn) (packs []Pack, err error) {
packs = make(map[string]Pack)
for _, pack := range server.ResourcePacks() { for _, pack := range server.ResourcePacks() {
pack := PackFromBase(pack) pack := PackFromBase(pack)
if pack.Encrypted() && pack.CanDecrypt() { if pack.Encrypted() && pack.CanDecrypt() {
@ -73,9 +72,9 @@ func GetPacks(server *minecraft.Conn) (packs map[string]Pack, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
packs[pack.Name()] = &Packb{pack2} packs = append(packs, &Packb{pack2})
} else { } else {
packs[pack.Name()] = pack packs = append(packs, pack)
} }
} }
return return