Skip to content

Commit

Permalink
show world saving progress in ui
Browse files Browse the repository at this point in the history
  • Loading branch information
olebeck committed Jul 11, 2024
1 parent 2c348b4 commit 6eeaa1f
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 82 deletions.
12 changes: 11 additions & 1 deletion handlers/worlds/packs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"slices"

"github.com/bedrock-tool/bedrocktool/locale"
"github.com/bedrock-tool/bedrocktool/ui/messages"
"github.com/bedrock-tool/bedrocktool/utils"
"github.com/flytam/filenamify"
"github.com/sandertv/gophertunnel/minecraft/resource"
"github.com/sirupsen/logrus"
)

func (w *worldsHandler) AddPacks(fs utils.WriterFS) error {
func (w *worldsHandler) AddPacks(worldName string, fs utils.WriterFS) error {
type dep struct {
PackID string `json:"pack_id"`
Version [3]int `json:"version"`
Expand Down Expand Up @@ -79,6 +80,15 @@ func (w *worldsHandler) AddPacks(fs utils.WriterFS) error {
}
logrus.Infof(locale.Loc("adding_pack", locale.Strmap{"Name": pack.Name()}))

messages.Router.Handle(&messages.Message{
Source: "subcommand",
Target: "ui",
Data: messages.ProcessingWorldUpdate{
Name: worldName,
State: "Adding Resourcepack " + pack.Name(),
},
})

packName := pack.Name()
if packIds := packNames[packName]; len(packIds) > 1 {
packName = fmt.Sprintf("%s_%d", packName, slices.Index(packIds, pack.UUID()))
Expand Down
36 changes: 27 additions & 9 deletions handlers/worlds/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,21 +416,25 @@ func (w *worldsHandler) saveWorldState(worldState *worldstate.World) error {
messages.Router.Handle(&messages.Message{
Source: "subcommand",
Target: "ui",
Data: messages.SavingWorld{
World: &messages.SavedWorld{
Name: worldState.Name,
Path: filename,
Chunks: len(worldState.StoredChunks),
Entities: len(worldState.StoredChunks),
},
Data: messages.ProcessingWorldUpdate{
Name: worldState.Name,
State: "Saving",
},
})

err := worldState.Finish(w.playerData(), w.settings.ExcludedMobs, w.settings.Players, spawnPos, w.proxy.Server.GameData(), w.bp)
if err != nil {
return err
}

messages.Router.Handle(&messages.Message{
Source: "subcommand",
Target: "ui",
Data: messages.ProcessingWorldUpdate{
Name: worldState.Name,
State: "Writing mcworld file",
},
})

f, err := os.Create(filename)
if err != nil {
return err
Expand All @@ -445,7 +449,7 @@ func (w *worldsHandler) saveWorldState(worldState *worldstate.World) error {
ofs := &utils.OSWriter{Base: worldState.Folder}
mfs := &utils.MultiWriterFS{FSs: []utils.WriterFS{zfs, ofs}}

err = w.AddPacks(mfs)
err = w.AddPacks(worldState.Name, mfs)
if err != nil {
return err
}
Expand All @@ -455,6 +459,20 @@ func (w *worldsHandler) saveWorldState(worldState *worldstate.World) error {
}

w.log.Info(locale.Loc("saved", locale.Strmap{"Name": filename}))

messages.Router.Handle(&messages.Message{
Source: "subcommand",
Target: "ui",
Data: messages.FinishedSavingWorld{
World: &messages.SavedWorld{
Name: worldState.Name,
Path: filename,
Chunks: len(worldState.StoredChunks),
Entities: worldState.EntityCount(),
},
},
})

return nil
}

Expand Down
19 changes: 19 additions & 0 deletions handlers/worlds/worldstate/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/bedrock-tool/bedrocktool/locale"
"github.com/bedrock-tool/bedrocktool/ui/messages"
"github.com/bedrock-tool/bedrocktool/utils"
"github.com/bedrock-tool/bedrocktool/utils/behaviourpack"
"github.com/df-mc/dragonfly/server/block/cube"
Expand Down Expand Up @@ -468,12 +469,30 @@ func (w *World) Finish(playerData map[string]any, excludedMobs []string, withPla
}
}

messages.Router.Handle(&messages.Message{
Source: "subcommand",
Target: "ui",
Data: messages.ProcessingWorldUpdate{
Name: w.Name,
State: "Storing Chunks",
},
})

w.applyBlockUpdates()
err := w.storeMemToProvider()
if err != nil {
return err
}

messages.Router.Handle(&messages.Message{
Source: "subcommand",
Target: "ui",
Data: messages.ProcessingWorldUpdate{
Name: w.Name,
State: "Storing Entities",
},
})

chunkEntities := make(map[world.ChunkPos][]world.Entity)
for _, es := range w.memState.entities {
var ignore bool
Expand Down
18 changes: 13 additions & 5 deletions ui/gui/mctext/mctext.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mctext

import (
"image/color"
"math/rand/v2"
"regexp"

"gioui.org/font"
Expand All @@ -13,7 +14,7 @@ import (

var splitter = regexp.MustCompile("((?:§.)?(?:[^§]+)?)")

func Label(th *material.Theme, size unit.Sp, txt string) func(gtx layout.Context) layout.Dimensions {
func Label(th *material.Theme, size unit.Sp, txt string, invalidate func(), frame int) func(gtx layout.Context) layout.Dimensions {
split := splitter.FindAllString(txt, -1)
var Styles []styledtext.SpanStyle

Expand Down Expand Up @@ -97,9 +98,7 @@ func Label(th *material.Theme, size unit.Sp, txt string) func(gtx layout.Context
_ = obfuscated
}

partT := string(partR)

if len(partT) == 0 {
if len(partR) == 0 {
continue
}

Expand All @@ -112,6 +111,15 @@ func Label(th *material.Theme, size unit.Sp, txt string) func(gtx layout.Context
fontWeight = font.Bold
}

if obfuscated {
obfuscatedDict := []rune{'a', 'b', 'c', 'd'}
r := rand.New(rand.NewPCG(0, uint64(frame/3)))
for i := 0; i < len(partR); i++ {
partR[i] = obfuscatedDict[r.IntN(len(obfuscatedDict))]
}
invalidate()
}

Styles = append(Styles, styledtext.SpanStyle{
Font: font.Font{
Typeface: th.Face,
Expand All @@ -120,7 +128,7 @@ func Label(th *material.Theme, size unit.Sp, txt string) func(gtx layout.Context
},
Size: size,
Color: activeColor,
Content: partT,
Content: string(partR),
})
}

Expand Down
14 changes: 10 additions & 4 deletions ui/gui/pages/packs/packs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type Page struct {

State messages.UIState
back widget.Clickable

frame int
invalidate func()
}

type packEntry struct {
Expand All @@ -59,13 +62,14 @@ type packEntry struct {
Err error
}

func New() pages.Page {
func New(invalidate func()) pages.Page {
return &Page{
packsList: widget.List{
List: layout.List{
Axis: layout.Vertical,
},
},
invalidate: invalidate,
}
}

Expand Down Expand Up @@ -114,7 +118,7 @@ func MulAlpha(c color.NRGBA, alpha uint8) color.NRGBA {
return c
}

func drawPackEntry(gtx C, th *material.Theme, pack *packEntry) D {
func (p *Page) drawPackEntry(gtx C, th *material.Theme, pack *packEntry) D {
var size = ""
var colorSize = th.Palette.Fg
if pack.IsFinished {
Expand Down Expand Up @@ -145,7 +149,7 @@ func drawPackEntry(gtx C, th *material.Theme, pack *packEntry) D {
}),
layout.Flexed(1, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(mctext.Label(th, th.TextSize, pack.Name)),
layout.Rigid(mctext.Label(th, th.TextSize, pack.Name, p.invalidate, p.frame)),
layout.Rigid(material.Label(th, th.TextSize, pack.Version).Layout),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
var c color.NRGBA
Expand Down Expand Up @@ -203,6 +207,8 @@ func drawPackEntry(gtx C, th *material.Theme, pack *packEntry) D {
}

func (p *Page) Layout(gtx C, th *material.Theme) D {
p.frame++

for _, pack := range p.Packs {
if pack.button.Clicked(gtx) {
if pack.IsFinished {
Expand Down Expand Up @@ -249,7 +255,7 @@ func (p *Page) Layout(gtx C, th *material.Theme) D {

return material.List(th, &p.packsList).Layout(gtx, len(p.Packs), func(gtx C, index int) D {
pack := p.Packs[index]
return drawPackEntry(gtx, th, pack)
return p.drawPackEntry(gtx, th, pack)
})
}),
)
Expand Down
8 changes: 4 additions & 4 deletions ui/gui/pages/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Router struct {
Invalidate func()
LogWidget func(layout.Context, *material.Theme) layout.Dimensions

pages map[string]func() Page
pages map[string]func(invalidate func()) Page
currentPage Page

ModalNavDrawer *component.ModalNavDrawer
Expand All @@ -62,7 +62,7 @@ func NewRouter(uii ui.UI) *Router {

r := &Router{
ui: uii,
pages: make(map[string]func() Page),
pages: make(map[string]func(invalidate func()) Page),
ModalLayer: modal,
ModalNavDrawer: component.ModalNavFrom(&nav, modal),
AppBar: component.NewAppBar(modal),
Expand All @@ -75,7 +75,7 @@ func NewRouter(uii ui.UI) *Router {
return r
}

func (r *Router) Register(p func() Page, id string) {
func (r *Router) Register(p func(invalidate func()) Page, id string) {
r.pages[id] = p
}

Expand All @@ -85,7 +85,7 @@ func (r *Router) SwitchTo(tag string) {
logrus.Errorf("unknown page %s", tag)
return
}
page := createPage()
page := createPage(r.Invalidate)

r.currentPage = page
r.AppBar.Title = page.NavItem().Name
Expand Down
2 changes: 1 addition & 1 deletion ui/gui/pages/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Page struct {
startButton widget.Clickable
}

func New() pages.Page {
func New(invalidate func()) pages.Page {
p := &Page{
settings: make(map[string]*settingsPage),
}
Expand Down
2 changes: 1 addition & 1 deletion ui/gui/pages/skins/skins.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Page struct {
back widget.Clickable
}

func New() pages.Page {
func New(invalidate func()) pages.Page {
return &Page{
SkinsList: widget.List{
List: layout.List{
Expand Down
Loading

0 comments on commit 6eeaa1f

Please sign in to comment.