format a bit

This commit is contained in:
olebeck 2023-04-02 00:22:50 +02:00
parent 29417b600c
commit 88589933ea
23 changed files with 137 additions and 229 deletions

3
go.mod
View File

@ -6,7 +6,7 @@ go 1.20
replace github.com/sandertv/gophertunnel => github.com/olebeck/gophertunnel v1.28.1-1
//replace github.com/df-mc/dragonfly => ./dragonfly
replace github.com/df-mc/dragonfly => github.com/olebeck/dragonfly v0.9.4-4
replace github.com/df-mc/dragonfly => github.com/olebeck/dragonfly v0.9.4-5
//replace gioui.org => ./gio
replace gioui.org => github.com/olebeck/gio v0.0.0-20230321105529-d424f1a59af9
@ -61,6 +61,7 @@ require (
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/muhammadmuzzammil1998/jsonc v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/repeale/fp-go v0.11.1 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect

11
go.sum
View File

@ -10,6 +10,8 @@ git.wow.st/gmp/jni v0.0.0-20210610011705-34026c7e22d0 h1:bGG/g4ypjrCJoSvFrP5hafr
git.wow.st/gmp/jni v0.0.0-20210610011705-34026c7e22d0/go.mod h1:+axXBRUTIDlCeE73IKeD/os7LoEnTKdkp8/gQOFjqyo=
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/TeaEntityLab/fpGo/v2 v2.3.2 h1:qlGjFzXukp9IgVQl32cz4PosbiFEnwXa2EpDbwddHS8=
github.com/TeaEntityLab/fpGo/v2 v2.3.2/go.mod h1:b06fRNLSnNiUwfSskBC3f3cocA4mAC2iySdtaP/T9uA=
github.com/benoitkugler/pstokenizer v1.0.0/go.mod h1:l1G2Voirz0q/jj0TQfabNxVsa8HZXh/VMxFSRALWTiE=
github.com/benoitkugler/textlayout v0.3.0 h1:2ehWXEkgb6RUokTjXh1LzdGwG4dRP6X3dqhYYDYhUVk=
github.com/benoitkugler/textlayout v0.3.0/go.mod h1:o+1hFV+JSHBC9qNLIuwVoLedERU7sBPgEFcuSgfvi/w=
@ -83,10 +85,8 @@ github.com/muhammadmuzzammil1998/jsonc v1.0.0 h1:8o5gBQn4ZA3NBA9DlTujCj2a4w0tqWr
github.com/muhammadmuzzammil1998/jsonc v1.0.0/go.mod h1:saF2fIVw4banK0H4+/EuqfFLpRnoy5S+ECwTOCcRcSU=
github.com/nicksnyder/go-i18n/v2 v2.2.1 h1:aOzRCdwsJuoExfZhoiXHy4bjruwCMdt5otbYojM/PaA=
github.com/nicksnyder/go-i18n/v2 v2.2.1/go.mod h1:fF2++lPHlo+/kPaj3nB0uxtPwzlPm+BlgwGX7MkeGj0=
github.com/olebeck/dragonfly v0.9.4-3 h1:zG6f7zBCM9HWx6eQ/O5VKUBxU6bjln+nDlfCF0vNZ/A=
github.com/olebeck/dragonfly v0.9.4-3/go.mod h1:k8OQvjmfj+JbrwQf1qHfMETlFHOp0WJLjILN+QVqh+c=
github.com/olebeck/dragonfly v0.9.4-4 h1:qe2pEasLpGdbOvz6DYE56AlxMCyidj60/i0ZbfdD+no=
github.com/olebeck/dragonfly v0.9.4-4/go.mod h1:k8OQvjmfj+JbrwQf1qHfMETlFHOp0WJLjILN+QVqh+c=
github.com/olebeck/dragonfly v0.9.4-5 h1:ixVX0Fe3wX8FryJiKC5gFVS4KDrKtd1dXJoBkzWhd1c=
github.com/olebeck/dragonfly v0.9.4-5/go.mod h1:k8OQvjmfj+JbrwQf1qHfMETlFHOp0WJLjILN+QVqh+c=
github.com/olebeck/gio v0.0.0-20230321105529-d424f1a59af9 h1:TqDsMHwjW5ZYfh+RE8ussT62m0qXqo+QjzSXb7BCVA4=
github.com/olebeck/gio v0.0.0-20230321105529-d424f1a59af9/go.mod h1:+W1Kpf96YcfissZocFqIp6O42FDTuphkObbEybp+Ffc=
github.com/olebeck/gophertunnel v1.28.1-1 h1:bw2jeMz94YHF5qQYhq1Yq/6fALkklGu7k26YbPI4DSs=
@ -100,6 +100,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/repeale/fp-go v0.11.1 h1:Q/e+gNyyHaxKAyfdbBqvip3DxhVWH453R+kthvSr9Mk=
github.com/repeale/fp-go v0.11.1/go.mod h1:4KrwQJB1VRY+06CA+jTc4baZetr6o2PeuqnKr5ybQUc=
github.com/sanbornm/go-selfupdate v0.0.0-20210106163404-c9b625feac49 h1:LuxslTBxJrrNeKfqoywIERWWhH43TgiVAiPEVlhgNBA=
github.com/sanbornm/go-selfupdate v0.0.0-20210106163404-c9b625feac49/go.mod h1:fY313ZGG810aWruFYcyq3coFpHDrWJVoMfSRI81y1r4=
github.com/sandertv/go-raknet v1.12.0 h1:olUzZlIJyX/pgj/mrsLCZYjKLNDsYiWdvQ4NIm3z0DA=
@ -198,6 +200,7 @@ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/inconshreveable/go-update.v0 v0.0.0-20150814200126-d8b0b1d421aa h1:drvf2JoUL1fz3ttkGNkw+rf3kZa2//7XkYGpSO4NHNA=

View File

@ -54,11 +54,6 @@ func (c *BlindProxyCMD) Execute(ctx context.Context, ui utils.UI) error {
listener.ID(), "Creative", 1, listener.Addr().(*net.UDPAddr).Port, listener.Addr().(*net.UDPAddr).Port,
)))
go func() {
<-ctx.Done()
listener.Close()
}()
clientConn, err := listener.Accept()
if err != nil {
return err

View File

@ -5,6 +5,7 @@ import (
"context"
"encoding/binary"
"flag"
"fmt"
"io"
"net"
"os"
@ -15,7 +16,6 @@ import (
"github.com/bedrock-tool/bedrocktool/utils"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"github.com/sirupsen/logrus"
)
func init() {
@ -32,13 +32,7 @@ func dumpPacket(f io.WriteCloser, toServer bool, payload []byte) {
binary.Write(f, binary.LittleEndian, packetSize)
binary.Write(f, binary.LittleEndian, toServer)
binary.Write(f, binary.LittleEndian, time.Now().UnixMilli())
n, err := f.Write(payload)
if err != nil {
logrus.Error(err)
}
if n < int(packetSize) {
f.Write(make([]byte, int(packetSize)-n))
}
f.Write(payload)
f.Write([]byte{0xBB, 0xBB, 0xBB, 0xBB})
}
@ -59,7 +53,7 @@ func (c *CaptureCMD) Execute(ctx context.Context, ui utils.UI) error {
}
os.Mkdir("captures", 0o775)
fio, err := os.Create("captures/" + hostname + "-" + time.Now().Format("2006-01-02_15-04-05") + ".pcap2")
fio, err := os.Create(fmt.Sprintf("captures/%s-%s.pcap2", hostname, time.Now().Format("2006-01-02_15-04-05")))
if err != nil {
return err
}
@ -68,7 +62,7 @@ func (c *CaptureCMD) Execute(ctx context.Context, ui utils.UI) error {
proxy, err := utils.NewProxy()
if err != nil {
logrus.Fatal(err)
return err
}
proxy.PacketFunc = func(header packet.Header, payload []byte, src, dst net.Addr) {
IsfromClient := src.String() == proxy.Client.LocalAddr().String()
@ -79,10 +73,5 @@ func (c *CaptureCMD) Execute(ctx context.Context, ui utils.UI) error {
dumpPacket(fio, IsfromClient, buf.Bytes())
}
err = proxy.Run(ctx, address)
time.Sleep(2 * time.Second)
if err != nil {
return err
}
return nil
return proxy.Run(ctx, address)
}

View File

@ -59,8 +59,7 @@ func (c *ChatLogCMD) Execute(ctx context.Context, ui utils.UI) error {
return pk, nil
}
err = proxy.Run(ctx, address)
return err
return proxy.Run(ctx, address)
}
func init() {

View File

@ -149,13 +149,13 @@ func (c *SkinCMD) Execute(ctx context.Context, ui utils.UI) error {
proxy, _ := utils.NewProxy()
proxy.WithClient = !c.NoProxy
proxy.OnClientConnect = func(hasClient bool) {
ui.Message(messages.SetUIState, messages.UIStateConnecting)
ui.Message(messages.SetUIState(messages.UIStateConnecting))
}
proxy.ConnectCB = func(err error) bool {
if err != nil {
return false
}
ui.Message(messages.SetUIState, messages.UIStateMain)
ui.Message(messages.SetUIState(messages.UIStateMain))
logrus.Info(locale.Loc("ctrl_c_to_exit", nil))
return true
}
@ -168,7 +168,7 @@ func (c *SkinCMD) Execute(ctx context.Context, ui utils.UI) error {
proxy.PacketCB = func(pk packet.Packet, toServer bool, _ time.Time) (packet.Packet, error) {
if !toServer {
for _, s := range s.ProcessPacket(pk) {
ui.Message(messages.NewSkin, messages.NewSkinPayload{
ui.Message(messages.NewSkin{
PlayerName: s.PlayerName,
Skin: s.Skin,
})
@ -178,9 +178,9 @@ func (c *SkinCMD) Execute(ctx context.Context, ui utils.UI) error {
}
if proxy.WithClient {
ui.Message(messages.SetUIState, messages.UIStateConnect)
ui.Message(messages.SetUIState(messages.UIStateConnect))
} else {
ui.Message(messages.SetUIState, messages.UIStateConnecting)
ui.Message(messages.SetUIState(messages.UIStateConnecting))
}
err = proxy.Run(ctx, address)
return err

View File

@ -4,6 +4,7 @@ import (
"github.com/bedrock-tool/bedrocktool/locale"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/chunk"
"github.com/repeale/fp-go"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"github.com/sirupsen/logrus"
@ -76,13 +77,9 @@ func (w *worldsServer) processLevelChunk(pk *packet.LevelChunk) {
})
default:
// legacy
empty := true
for _, sub := range ch.Sub() {
if !sub.Empty() {
empty = false
break
}
}
empty := fp.Every(func(sub *chunk.SubChunk) bool {
return sub.Empty()
})(ch.Sub())
if !empty {
w.mapUI.SetChunk(pk.Position, ch, true)
}

View File

@ -29,37 +29,43 @@ func blockColorAt(c *chunk.Chunk, x uint8, y int16, z uint8) (blockColor color.R
blockColor = color.RGBA{255, 0, 255, 255}
b, found := world.BlockByRuntimeID(rid)
if found {
if _, isWater := b.(block.Water); isWater {
// get the first non water block at the position
heightBlock := c.HeightMap().At(x, z)
depth := y - heightBlock
if depth > 0 {
blockColor = blockColorAt(c, x, heightBlock, z)
}
// blend that blocks color with water depending on depth
waterColor := (&block.Water{}).Color()
waterColor.A = uint8(utils.Clamp(int(150+depth*7), 255))
blockColor = utils.BlendColors(blockColor, waterColor)
blockColor.R -= uint8(depth * 2)
blockColor.G -= uint8(depth * 2)
blockColor.B -= uint8(depth * 2)
return blockColor
} else {
col := b.Color()
if col.A != 255 {
col = utils.BlendColors(blockColorAt(c, x, y-1, z), col)
}
return col
}
} else {
/*
name, nbt := b.EncodeBlock()
fmt.Printf("unknown color %d %s %s %s\n", rid, reflect.TypeOf(b), name, nbt)
*/
if !found {
return blockColor
}
if _, isWater := b.(block.Water); isWater {
waterColor := block.Water{}.Color()
// get the first non water block at the position
heightBlock := c.HeightMap().At(x, z)
depth := y - heightBlock
if depth > 0 {
blockColor = blockColorAt(c, x, heightBlock, z)
}
// blend that blocks color with water depending on depth
waterColor.A = uint8(utils.Clamp(int(150+depth*7), 255))
blockColor = utils.BlendColors(blockColor, waterColor)
blockColor.R -= uint8(depth * 2)
blockColor.G -= uint8(depth * 2)
blockColor.B -= uint8(depth * 2)
return blockColor
} else {
col := b.Color()
if col.A != 255 {
col = utils.BlendColors(blockColorAt(c, x, y-1, z), col)
}
/*
a := color.RGBA{255, 0, 255, 255}
if col == a {
name, nbt := b.EncodeBlock()
fmt.Printf("unknown color %d %s %s %s\n", rid, reflect.TypeOf(b), name, nbt)
}
*/
return col
}
}
func chunkGetColorAt(c *chunk.Chunk, x uint8, y int16, z uint8) color.RGBA {

View File

@ -95,7 +95,7 @@ func NewMapUI(w *worldsServer) *MapUI {
}
func (m *MapUI) Start() {
r := m.w.gui.Message("can_show_images", nil)
r := m.w.gui.Message(messages.CanShowImages{})
if r.Ok {
m.showOnGui = true
}
@ -182,7 +182,7 @@ func (m *MapUI) SchedRedraw() {
// Redraw draws chunk images to the map image
func (m *MapUI) Redraw() {
m.l.Lock()
updatedChunks := []protocol.ChunkPos{}
updatedChunks := make([]protocol.ChunkPos, 0, m.renderQueue.Length())
for {
r, ok := m.renderQueue.Dequeue().(*RenderElem)
if !ok {
@ -225,7 +225,7 @@ func (m *MapUI) Redraw() {
}
if m.showOnGui {
min, max := m.GetBounds()
m.w.gui.Message(messages.UpdateMap, messages.UpdateMapPayload{
m.w.gui.Message(messages.UpdateMap{
ChunkCount: len(m.renderedChunks),
Rotation: m.w.serverState.PlayerPos.Yaw,
UpdatedTiles: updatedChunks,

View File

@ -25,6 +25,7 @@ import (
"github.com/df-mc/dragonfly/server/world/mcdb"
"github.com/df-mc/goleveldb/leveldb/opt"
"github.com/go-gl/mathgl/mgl32"
"github.com/repeale/fp-go"
"github.com/sandertv/gophertunnel/minecraft"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
@ -104,8 +105,8 @@ func NewWorldsServer(ctx context.Context, proxy *utils.ProxyContext, ServerName
w.mapUI = NewMapUI(w)
w.Reset(w.CurrentName())
w.gui.Message(messages.Init, messages.InitPayload{
Handler: w.uiMessage,
w.gui.Message(messages.Init{
Handler: nil,
})
return w
@ -173,7 +174,7 @@ func (c *WorldCMD) Execute(ctx context.Context, ui utils.UI) error {
}
proxy.ConnectCB = w.OnConnect
proxy.OnClientConnect = func(hasClient bool) {
w.gui.Message(messages.SetUIState, messages.UIStateConnecting)
w.gui.Message(messages.SetUIState(messages.UIStateConnecting))
}
proxy.PacketCB = func(pk packet.Packet, toServer bool, _ time.Time) (packet.Packet, error) {
forward := true
@ -200,32 +201,16 @@ func (c *WorldCMD) Execute(ctx context.Context, ui utils.UI) error {
return pk, nil
}
w.gui.Message(messages.SetUIState, messages.UIStateConnect)
w.gui.Message(messages.SetUIState(messages.UIStateConnect))
err = w.proxy.Run(ctx, serverAddress)
if err != nil {
return err
}
w.SaveAndReset()
ui.Message(messages.SetUIState, messages.UIStateFinished)
ui.Message(messages.SetUIState(messages.UIStateFinished))
return nil
}
func (w *worldsServer) uiMessage(name string, data interface{}) messages.MessageResponse {
r := messages.MessageResponse{
Ok: false,
Data: nil,
}
switch name {
case messages.SetVoidGen:
set_void_gen := data.(messages.SetVoidGenPayload)
r.Ok = w.setVoidGen(set_void_gen.Value, true)
case messages.SetWorldName:
set_world_name := data.(messages.SetWorldNamePayload)
r.Ok = w.setWorldName(set_world_name.WorldName, true)
}
return r
}
func (w *worldsServer) SetPlayerPos(Position mgl32.Vec3, Pitch, Yaw, HeadYaw float32) {
last := w.serverState.PlayerPos
current := TPlayerPos{
@ -243,16 +228,14 @@ func (w *worldsServer) SetPlayerPos(Position mgl32.Vec3, Pitch, Yaw, HeadYaw flo
func (w *worldsServer) setVoidGen(val bool, fromUI bool) bool {
w.settings.voidGen = val
var s string
var s = locale.Loc("void_generator_false", nil)
if w.settings.voidGen {
s = locale.Loc("void_generator_true", nil)
} else {
s = locale.Loc("void_generator_false", nil)
}
w.proxy.SendMessage(s)
if !fromUI {
w.gui.Message(messages.SetVoidGen, messages.SetVoidGenPayload{
w.gui.Message(messages.SetVoidGen{
Value: w.settings.voidGen,
})
}
@ -265,7 +248,7 @@ func (w *worldsServer) setWorldName(val string, fromUI bool) bool {
w.proxy.SendMessage(locale.Loc("worldname_set", locale.Strmap{"Name": w.worldState.Name}))
if !fromUI {
w.gui.Message(messages.SetWorldName, messages.SetWorldNamePayload{
w.gui.Message(messages.SetWorldName{
WorldName: w.worldState.Name,
})
}
@ -301,25 +284,22 @@ func (w *worldsServer) SaveAndReset() {
for cp := range w.worldState.chunks {
keys = append(keys, cp)
}
for _, cp := range keys {
has_any := false
for _, sc := range w.worldState.chunks[cp].Sub() {
has_any = !sc.Empty()
if has_any {
break
}
}
if !has_any {
delete(w.worldState.chunks, cp)
}
for _, cp := range fp.Filter(func(cp protocol.ChunkPos) bool {
return fp.Some(func(sc *chunk.SubChunk) bool {
return !sc.Empty()
})(w.worldState.chunks[cp].Sub())
})(keys) {
delete(w.worldState.chunks, cp)
}
if len(w.worldState.chunks) == 0 {
w.Reset(w.CurrentName())
return
}
logrus.Infof(locale.Loc("saving_world", locale.Strmap{"Name": w.worldState.Name, "Count": len(w.worldState.chunks)}))
w.gui.Message(messages.SavingWorld, messages.SavingWorldPayload{
w.gui.Message(messages.SavingWorld{
Name: w.worldState.Name,
Chunks: len(w.worldState.chunks),
})
@ -558,11 +538,11 @@ func (w *worldsServer) SaveAndReset() {
logrus.Info(locale.Loc("saved", locale.Strmap{"Name": filename}))
//os.RemoveAll(folder)
w.Reset(w.CurrentName())
w.gui.Message(messages.SetUIState, messages.UIStateMain)
w.gui.Message(messages.SetUIState(messages.UIStateMain))
}
func (w *worldsServer) OnConnect(err error) bool {
w.gui.Message(messages.SetUIState, messages.UIStateMain)
w.gui.Message(messages.SetUIState(messages.UIStateMain))
if err != nil {
return false

View File

@ -114,8 +114,8 @@ func (g *GUI) run(w *app.Window) error {
}
}
func (g *GUI) Message(name string, data interface{}) messages.MessageResponse {
r := g.router.Handler(name, data)
func (g *GUI) Message(data interface{}) messages.MessageResponse {
r := g.router.Handler(data)
if r.Ok || r.Data != nil {
return r
}
@ -125,8 +125,8 @@ func (g *GUI) Message(name string, data interface{}) messages.MessageResponse {
Data: nil,
}
switch name {
case "can_show_images":
switch data.(type) {
case messages.CanShowImages:
r.Ok = true
}

View File

@ -13,7 +13,7 @@ import (
"github.com/bedrock-tool/bedrocktool/ui/messages"
)
type HandlerFunc = func(name string, data interface{}) messages.MessageResponse
type HandlerFunc = func(data interface{}) messages.MessageResponse
type Page interface {
Actions() []component.AppBarAction
@ -133,10 +133,10 @@ func (r *Router) Layout(gtx layout.Context, th *material.Theme) layout.Dimension
return layout.Dimensions{Size: gtx.Constraints.Max}
}
func (r *Router) Handler(name string, data interface{}) messages.MessageResponse {
func (r *Router) Handler(data interface{}) messages.MessageResponse {
page, ok := r.pages[r.current]
if ok {
return page.Handler()(name, data)
return page.Handler()(data)
}
return messages.MessageResponse{}
}

View File

@ -176,7 +176,7 @@ func (p *Page) Layout(gtx C, th *material.Theme) D {
}
func (p *Page) Handler() pages.HandlerFunc {
return func(name string, data interface{}) messages.MessageResponse {
return func(data interface{}) messages.MessageResponse {
return messages.MessageResponse{
Ok: false,
Data: nil,

View File

@ -24,7 +24,7 @@ type Page struct {
State messages.UIState
SkinsList widget.List
l sync.Mutex
Skins []messages.NewSkinPayload
Skins []messages.NewSkin
}
func New(router *pages.Router) *Page {
@ -96,28 +96,20 @@ func (p *Page) Layout(gtx C, th *material.Theme) D {
return layout.Flex{}.Layout(gtx)
}
func (p *Page) handler(name string, data interface{}) messages.MessageResponse {
func (p *Page) handler(data interface{}) messages.MessageResponse {
r := messages.MessageResponse{
Ok: false,
Data: nil,
}
switch name {
switch m := data.(type) {
case messages.SetUIState:
state := data.(messages.UIState)
p.State = state
p.State = m
p.Router.Invalidate()
r.Ok = true
case messages.Init:
init := data.(messages.InitPayload)
_ = init
r.Ok = true
case messages.NewSkin:
p.l.Lock()
new_skin := data.(messages.NewSkinPayload)
p.Skins = append(p.Skins, new_skin)
p.Skins = append(p.Skins, m)
p.l.Unlock()
p.Router.Invalidate()
r.Ok = true

View File

@ -90,7 +90,7 @@ func drawTile(img *image.RGBA, min, pos protocol.ChunkPos, tile *image.RGBA) {
), tile, image.Point{}, draw.Src)
}
func (m *Map) Update(u *messages.UpdateMapPayload) {
func (m *Map) Update(u *messages.UpdateMap) {
if m.MapImage == nil {
m.scaleFactor = 1
}

View File

@ -30,7 +30,7 @@ type Page struct {
worldName string
worldsList widget.List
worlds []messages.SavingWorldPayload
worlds []messages.SavingWorld
l sync.Mutex
}
@ -115,47 +115,33 @@ func (p *Page) Layout(gtx C, th *material.Theme) D {
return layout.Dimensions{}
}
func (u *Page) handler(name string, data interface{}) messages.MessageResponse {
func (u *Page) handler(data interface{}) messages.MessageResponse {
r := messages.MessageResponse{
Ok: false,
Data: nil,
}
switch name {
switch m := data.(type) {
case messages.SetUIState:
state := data.(messages.UIState)
u.State = state
u.State = m
u.Router.Invalidate()
r.Ok = true
case messages.Init:
init := data.(messages.InitPayload)
_ = init
r.Ok = true
case messages.UpdateMap:
update_map := data.(messages.UpdateMapPayload)
u.chunkCount = update_map.ChunkCount
u.worldMap.Update(&update_map)
u.chunkCount = m.ChunkCount
u.worldMap.Update(&m)
u.Router.Invalidate()
r.Ok = true
case messages.SetVoidGen:
set_void_gen := data.(messages.SetVoidGenPayload)
u.voidGen = set_void_gen.Value
u.voidGen = m.Value
u.Router.Invalidate()
r.Ok = true
case messages.SetWorldName:
set_world_name := data.(messages.SetWorldNamePayload)
u.worldName = set_world_name.WorldName
u.worldName = m.WorldName
u.Router.Invalidate()
r.Ok = true
case messages.SavingWorld:
u.l.Lock()
saving_world := data.(messages.SavingWorldPayload)
u.worlds = append(u.worlds, saving_world)
u.worlds = append(u.worlds, m)
u.l.Unlock()
u.Router.Invalidate()
r.Ok = true

View File

@ -8,4 +8,4 @@ import (
type C = layout.Context
type D = layout.Dimensions
type HandlerFunc = func(name string, data interface{}) messages.MessageResponse
type HandlerFunc = func(data interface{}) messages.MessageResponse

View File

@ -11,7 +11,7 @@ type MessageResponse struct {
Data interface{}
}
type UIState = int
type UIState int
const (
UIStateConnect = iota
@ -24,39 +24,29 @@ type HandlerFunc = func(name string, data interface{}) MessageResponse
//
const SetUIState = "set_ui_state"
type SetUIStatePayload = UIState
type SetUIState = UIState
//
const SetVoidGen = "set_void_gen"
type SetVoidGenPayload struct {
type SetVoidGen struct {
Value bool
}
//
const SetWorldName = "set_world_name"
type SetWorldNamePayload struct {
type SetWorldName struct {
WorldName string
}
//
var Init = "init"
type InitPayload struct {
type Init struct {
Handler HandlerFunc
}
//
var UpdateMap = "update_map"
type UpdateMapPayload struct {
type UpdateMap struct {
ChunkCount int
Rotation float32
UpdatedTiles []protocol.ChunkPos
@ -67,16 +57,14 @@ type UpdateMapPayload struct {
//
var NewSkin = "new_skin"
type NewSkinPayload struct {
type NewSkin struct {
PlayerName string
Skin *protocol.Skin
}
var SavingWorld = "saving_world"
type SavingWorldPayload struct {
type SavingWorld struct {
Name string
Chunks int
}
type CanShowImages struct{}

View File

@ -8,6 +8,7 @@ import (
"strings"
"github.com/bedrock-tool/bedrocktool/utils"
"github.com/repeale/fp-go"
"github.com/sandertv/gophertunnel/minecraft/resource"
"github.com/sirupsen/logrus"
)
@ -73,22 +74,16 @@ func (bp *BehaviourPack) CheckAddLink(pack utils.Pack) {
hasEntitiesFolder := false
if bp.HasEntities() {
for _, f := range z.File {
if f.Name == "entity" && f.FileInfo().IsDir() {
hasEntitiesFolder = true
break
}
}
hasEntitiesFolder = fp.Some(func(f *zip.File) bool {
return f.Name == "entity" && f.FileInfo().IsDir()
})(z.File)
}
hasItemsFolder := false
if bp.HasItems() {
for _, f := range z.File {
if f.Name == "items" && f.FileInfo().IsDir() {
hasItemsFolder = true
break
}
}
hasItemsFolder = fp.Some(func(f *zip.File) bool {
return f.Name == "items" && f.FileInfo().IsDir()
})(z.File)
}
// has no assets needed
@ -139,7 +134,7 @@ func (bp *BehaviourPack) Save(fpath string) error {
return e.Encode(thing)
}
if len(bp.blocks) > 0 { // blocks
if bp.HasBlocks() { // blocks
blocks_dir := path.Join(fpath, "blocks")
os.Mkdir(blocks_dir, 0o755)
for _, be := range bp.blocks {
@ -149,7 +144,7 @@ func (bp *BehaviourPack) Save(fpath string) error {
}
}
}
if len(bp.items) > 0 { // items
if bp.HasItems() { // items
items_dir := path.Join(fpath, "items")
os.Mkdir(items_dir, 0o755)
for _, ib := range bp.items {
@ -159,7 +154,7 @@ func (bp *BehaviourPack) Save(fpath string) error {
}
}
}
if len(bp.entities) > 0 { // items
if bp.HasEntities() { // entities
items_dir := path.Join(fpath, "entities")
os.Mkdir(items_dir, 0o755)
for _, eb := range bp.entities {

View File

@ -17,7 +17,7 @@ import (
type UI interface {
Init() bool
Start(context.Context, context.CancelFunc) error
Message(name string, data interface{}) messages.MessageResponse
Message(data interface{}) messages.MessageResponse
ServerInput(context.Context, string) (string, string, error)
}
@ -25,7 +25,7 @@ type BaseUI struct {
UI
}
func (u *BaseUI) Message(name string, data interface{}) messages.MessageResponse {
func (u *BaseUI) Message(data interface{}) messages.MessageResponse {
return messages.MessageResponse{
Ok: false,
Data: nil,

View File

@ -1,8 +1,12 @@
package utils
import "net"
import (
"net"
var PrivateIPNetworks = []net.IPNet{
"github.com/repeale/fp-go"
)
var privateIPNetworks = []net.IPNet{
{
IP: net.ParseIP("10.0.0.0"),
Mask: net.CIDRMask(8, 32),
@ -19,12 +23,9 @@ var PrivateIPNetworks = []net.IPNet{
// IPPrivate checks if ip is private
func IPPrivate(ip net.IP) bool {
for _, ipNet := range PrivateIPNetworks {
if ipNet.Contains(ip) {
return true
}
}
return false
return fp.Some(func(ipNet net.IPNet) bool {
return ipNet.Contains(ip)
})(privateIPNetworks)
}
// GetLocalIP returns the non loopback local IP of the host

View File

@ -120,30 +120,6 @@ func spawnConn(ctx context.Context, clientConn *minecraft.Conn, serverConn *mine
return nil
}
// get longest line length
func maxLen(lines []string) int {
o := 0
for _, line := range lines {
if o < len(line) {
o = len(line)
}
}
return o
}
// MarginLines makes text centered
func MarginLines(lines []string) string {
ret := ""
max := maxLen(lines)
for _, line := range lines {
if len(line) != max {
ret += strings.Repeat(" ", max/2-len(line)/4)
}
ret += line + "\n"
}
return ret
}
// SplitExt splits path to filename and extension
func SplitExt(filename string) (name, ext string) {
name, ext = path.Base(filename), path.Ext(filename)