Skip to content

Commit

Permalink
fix bug causing world to be opened twice
Browse files Browse the repository at this point in the history
  • Loading branch information
olebeck committed Jun 25, 2024
1 parent ad53b36 commit 068972f
Show file tree
Hide file tree
Showing 18 changed files with 260 additions and 160 deletions.
8 changes: 5 additions & 3 deletions cmd/bedrocktool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ func main() {
}

logrus.SetLevel(logrus.DebugLevel)
log := logrus.WithField("part", "main")

if !isDebug {
logrus.Infof(locale.Loc("bedrocktool_version", locale.Strmap{"Version": updater.Version}))
log.Infof(locale.Loc("bedrocktool_version", locale.Strmap{"Version": updater.Version}))
}

ctx, cancel := context.WithCancelCause(context.Background())
Expand Down Expand Up @@ -128,13 +130,13 @@ func main() {
}()

if !ui.Init() {
logrus.Error("Failed to init UI!")
log.Error("Failed to init UI!")
return
}
err := ui.Start(ctx, cancel)
cancel(err)
if err != nil {
logrus.Error(err)
log.Error(err)
}
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/bedrocktool/time_synced.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ import (
var realTimeOffset *ntp.Response

func init() {
log := logrus.WithField("part", "timeSync")
var err error
realTimeOffset, err = ntp.Query("time.windows.com")
if err != nil {
logrus.Fatal(err)
log.Fatal(err)
}
if realTimeOffset.ClockOffset > time.Minute*30 {
logrus.Warnf("Your Clock is off by: %s\n", realTimeOffset.ClockOffset.String())
log.Warnf("Your Clock is off by: %s\n", realTimeOffset.ClockOffset.String())
}

_, err = mpatch.PatchMethod(time.Now, timeNow)
if err != nil {
logrus.Fatal(err)
log.Fatal(err)
}
}

Expand Down
7 changes: 5 additions & 2 deletions handlers/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type packetCapturer struct {
tempBuf *bytes.Buffer
dumpLock sync.Mutex
hostname string
log *logrus.Entry
}

func (p *packetCapturer) AddressAndName(address, hostname string) (err error) {
Expand Down Expand Up @@ -74,7 +75,7 @@ func (p *packetCapturer) OnServerConnect() (disconnect bool, err error) {
if _, ok := written[filename]; ok {
continue
}
logrus.Debugf("Writing %s to capture", pack.Name())
p.log.Debugf("Writing %s to capture", pack.Name())
f, err := z.CreateHeader(&zip.FileHeader{
Name: filename,
Method: zip.Store,
Expand Down Expand Up @@ -116,7 +117,9 @@ func (p *packetCapturer) PacketFunc(header packet.Header, payload []byte, src, d
}

func NewPacketCapturer() *proxy.Handler {
p := &packetCapturer{}
p := &packetCapturer{
log: logrus.WithField("part", "PacketCapture"),
}
return &proxy.Handler{
Name: "Packet Capturer",
ProxyReference: func(pc *proxy.Context) {
Expand Down
10 changes: 6 additions & 4 deletions handlers/skins.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type SkinSaver struct {
ServerName string
fpath string
proxy *proxy.Context
log *logrus.Entry

players map[uuid.UUID]*skinPlayer
}
Expand All @@ -48,16 +49,16 @@ func (s *SkinSaver) AddPlayerSkin(playerID uuid.UUID, playerName string, skin *u
if p.SkinPack == nil {
p.SkinPack = utils.NewSkinPack(playerName, s.fpath)
creating := fmt.Sprintf("Creating Skinpack for %s", playerName)
logrus.Info(creating)
s.log.Info(creating)
}
if p.SkinPack.AddSkin(skin) {
addedStr := fmt.Sprintf("Added a skin %s", playerName)
s.proxy.SendPopup(addedStr)
logrus.Info(addedStr)
s.log.Info(addedStr)
added = true
}
if err := p.SkinPack.Save(path.Join(s.fpath, playerName)); err != nil {
logrus.Error(err)
s.log.Error(err)
}
return added
}
Expand Down Expand Up @@ -157,6 +158,7 @@ func (s *SkinSaver) ProcessPacket(pk packet.Packet) (out []SkinAdd) {
func NewSkinSaver(skinCB func(SkinAdd)) *proxy.Handler {
s := &SkinSaver{
players: make(map[uuid.UUID]*skinPlayer),
log: logrus.WithField("part", "SkinSaver"),
}
return &proxy.Handler{
Name: "Skin Saver",
Expand Down Expand Up @@ -188,7 +190,7 @@ func (s *SkinSaver) stealSkin() {
if !rtTest {
return
}
logrus.Debugf("%d", len(s.players))
s.log.Debugf("%d", len(s.players))

var dist float64 = 4

Expand Down
7 changes: 3 additions & 4 deletions handlers/worlds/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import (
"github.com/sandertv/gophertunnel/minecraft/nbt"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"github.com/sirupsen/logrus"
)

func (w *worldsHandler) processLevelChunk(pk *packet.LevelChunk) {
if len(pk.RawPayload) == 0 {
logrus.Info(locale.Loc("empty_chunk", nil))
w.log.Info(locale.Loc("empty_chunk", nil))
return
}

Expand All @@ -40,7 +39,7 @@ func (w *worldsHandler) processLevelChunk(pk *packet.LevelChunk) {
w.serverState.useOldBiomes, w.serverState.useHashedRids, w.currentWorld.Range(),
)
if err != nil {
logrus.Error(err)
w.log.Error(err)
return
}
var chunkBlockNBT = make(map[cube.Pos]worldstate.DummyBlock)
Expand All @@ -61,7 +60,7 @@ func (w *worldsHandler) processLevelChunk(pk *packet.LevelChunk) {

err = w.currentWorld.StoreChunk(pos, ch, chunkBlockNBT)
if err != nil {
logrus.Error(err)
w.log.Error(err)
}

max := w.currentWorld.Dimension().Range().Height() / 16
Expand Down
8 changes: 5 additions & 3 deletions handlers/worlds/map_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type renderElem struct {
}

type MapUI struct {
log *logrus.Entry
img *image.RGBA // rendered image
renderQueue *lockfree.Queue
renderedChunks map[protocol.ChunkPos]*image.RGBA // prerendered chunks
Expand All @@ -85,6 +86,7 @@ type MapUI struct {

func NewMapUI(w *worldsHandler) *MapUI {
m := &MapUI{
log: logrus.WithField("part", "MapUI"),
img: image.NewRGBA(image.Rect(0, 0, 128, 128)),
zoomLevel: 16,
renderQueue: lockfree.NewQueue(),
Expand Down Expand Up @@ -120,7 +122,7 @@ func (m *MapUI) Start(ctx context.Context) {
UpdateFlags: packet.MapUpdateFlagInitialisation,
})
if err != nil {
logrus.Error(err)
m.log.Error(err)
return
}

Expand Down Expand Up @@ -158,7 +160,7 @@ func (m *MapUI) Start(ctx context.Context) {
Pixels: utils.Img2rgba(m.img),
UpdateFlags: packet.MapUpdateFlagTexture,
}); err != nil {
logrus.Error(err)
m.log.Error(err)
return
}
}
Expand All @@ -172,7 +174,7 @@ func (m *MapUI) Start(ctx context.Context) {
}
err := m.w.proxy.ClientWritePacket(&mapItemPacket)
if err != nil {
logrus.Error(err)
m.log.Error(err)
return
}
}
Expand Down
16 changes: 8 additions & 8 deletions handlers/worlds/packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (w *worldsHandler) packetCB(_pk packet.Packet, toServer bool, timeReceived
w.bp.AddItem(ie)
}
if len(pk.Blocks) > 0 {
logrus.Info(locale.Loc("using_customblocks", nil))
w.log.Info(locale.Loc("using_customblocks", nil))
for _, be := range pk.Blocks {
w.bp.AddBlock(be)
}
Expand All @@ -74,7 +74,7 @@ func (w *worldsHandler) packetCB(_pk packet.Packet, toServer bool, timeReceived
w.currentWorld.Name = pk.WorldName
}

{ // check game version
if len(pk.BaseGameVersion) > 0 && pk.BaseGameVersion != "*" { // check game version
gv := strings.Split(pk.BaseGameVersion, ".")
var err error
if len(gv) > 1 {
Expand All @@ -83,11 +83,11 @@ func (w *worldsHandler) packetCB(_pk packet.Packet, toServer bool, timeReceived
w.serverState.useOldBiomes = ver < 18
}
if err != nil || len(gv) <= 1 {
logrus.Info(locale.Loc("guessing_version", nil))
w.log.Info(locale.Loc("guessing_version", nil))
}

if w.serverState.useOldBiomes {
logrus.Info(locale.Loc("using_under_118", nil))
w.log.Info(locale.Loc("using_under_118", nil))
w.serverState.dimensions[0] = protocol.DimensionDefinition{
Name: "minecraft:overworld",
Range: [2]int32{0, 256},
Expand Down Expand Up @@ -117,7 +117,7 @@ func (w *worldsHandler) packetCB(_pk packet.Packet, toServer bool, timeReceived
var biomes map[string]any
err := nbt.UnmarshalEncoding(pk.SerialisedBiomeDefinitions, &biomes, nbt.NetworkLittleEndian)
if err != nil {
logrus.Error(err)
w.log.WithField("packet", "BiomeDefinitionList").Error(err)
}

for k, v := range biomes {
Expand Down Expand Up @@ -163,7 +163,7 @@ func (w *worldsHandler) playersPackets(_pk packet.Packet) {
var resourcePatch map[string]map[string]string
err := utils.ParseJson(skin.SkinResourcePatch, &resourcePatch)
if err != nil {
logrus.Error(err)
w.log.WithField("data", "SkinResourcePatch").Error(err)
return
}

Expand All @@ -174,7 +174,7 @@ func (w *worldsHandler) playersPackets(_pk packet.Packet) {
if len(skin.SkinGeometry) > 0 {
skinGeometry, _, err := utils.ParseSkinGeometry(skin.SkinGeometry)
if err != nil {
logrus.Warn(err)
w.log.WithField("player", pk.Username).Warn(err)
return
}
if skinGeometry != nil {
Expand Down Expand Up @@ -312,7 +312,7 @@ func (w *worldsHandler) chunkPackets(_pk packet.Packet) {

case *packet.SubChunk:
if err := w.processSubChunk(pk); err != nil {
logrus.Error(err)
w.log.WithField("packet", "SubChunk").Error(err)
}

case *packet.BlockActorData:
Expand Down
Loading

0 comments on commit 068972f

Please sign in to comment.