Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bedrock-tool/bedrocktool
Browse files Browse the repository at this point in the history
  • Loading branch information
olebeck committed Jul 14, 2024
2 parents efbf09a + 5ab7a2b commit 81a13f2
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion handlers/worlds/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (w *worldsHandler) preloadReplay() error {
if err != nil {
log.Error(err)
}
}, func() {}, func(p resource.Pack) error { return nil }, func(s string) bool { return false })
}, func() {}, func(p resource.Pack) error { return nil }, func(s string) bool { return false }, func() bool { return false })
if err != nil {
return err
}
Expand Down
Binary file modified subcommands/resourcepack-d/resourcepack-d.go
Binary file not shown.
21 changes: 20 additions & 1 deletion utils/proxy/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,22 @@ func (p *Context) doSession(ctx context.Context, cancel context.CancelCauseFunc)
return ignore
}

OnFinishedAll := func() bool {
for _, handler := range p.handlers {
if handler.ResourcePacksFinished != nil {
if handler.ResourcePacksFinished() {
return true
}
}
}
return false
}

// setup Client and Server Connections
wg := sync.WaitGroup{}
if isReplay {
filename := p.serverAddress[5:]
server, err := CreateReplayConnector(ctx, filename, p.packetFunc, p.onResourcePacksInfo, p.onFinishedPack, filterDownloadResourcePacks)
server, err := CreateReplayConnector(ctx, filename, p.packetFunc, p.onResourcePacksInfo, p.onFinishedPack, filterDownloadResourcePacks, OnFinishedAll)
if err != nil {
return err
}
Expand All @@ -370,6 +381,7 @@ func (p *Context) doSession(ctx context.Context, cancel context.CancelCauseFunc)
p.rpHandler = newRpHandler(ctx, p.addedPacks, filterDownloadResourcePacks)
p.rpHandler.OnResourcePacksInfoCB = p.onResourcePacksInfo
p.rpHandler.OnFinishedPack = p.onFinishedPack
p.rpHandler.OnFinishedAll = OnFinishedAll

if p.withClient {
wg.Add(1)
Expand Down Expand Up @@ -429,6 +441,10 @@ func (p *Context) doSession(ctx context.Context, cancel context.CancelCauseFunc)
} else {
p.disconnectReason = "Disconnect"
}

if p.expectDisconnect {
return nil
}
return err
}

Expand Down Expand Up @@ -470,6 +486,9 @@ func (p *Context) doSession(ctx context.Context, cancel context.CancelCauseFunc)
err = context.Cause(ctx)
if err != nil {
p.disconnectReason = err.Error()
if p.expectDisconnect {
return nil
}
return err
}

Expand Down
2 changes: 2 additions & 0 deletions utils/proxy/packet_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ var mutedPackets = []uint32{
packet.IDNetworkStackLatency,
packet.IDInventoryTransaction,
packet.IDPlaySound,
packet.IDPlayerAction,
packet.IDSetTitle,
}

var dirS2C = color.GreenString("S") + "->" + color.CyanString("C")
Expand Down
2 changes: 2 additions & 0 deletions utils/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Handler struct {
FilterResourcePack func(id string) bool
OnFinishedPack func(pack resource.Pack) error

ResourcePacksFinished func() bool

PacketRaw func(header packet.Header, payload []byte, src, dst net.Addr)
PacketCallback func(pk packet.Packet, toServer bool, timeReceived time.Time, preLogin bool) (packet.Packet, error)

Expand Down
3 changes: 2 additions & 1 deletion utils/proxy/replay_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,15 @@ func (r *ReplayConnector) ReadUntilLogin() error {
return nil
}

func CreateReplayConnector(ctx context.Context, filename string, packetFunc PacketFunc, onResourcePackInfo func(), OnFinishedPack func(resource.Pack) error, filterDownloadResourcePacks func(string) bool) (r *ReplayConnector, err error) {
func CreateReplayConnector(ctx context.Context, filename string, packetFunc PacketFunc, onResourcePackInfo func(), OnFinishedPack func(resource.Pack) error, filterDownloadResourcePacks func(string) bool, OnFinishedAll func() bool) (r *ReplayConnector, err error) {
r = &ReplayConnector{
spawn: make(chan struct{}),
close: make(chan struct{}),
}
r.resourcePackHandler = newRpHandler(ctx, nil, filterDownloadResourcePacks)
r.resourcePackHandler.OnResourcePacksInfoCB = onResourcePackInfo
r.resourcePackHandler.OnFinishedPack = OnFinishedPack
r.resourcePackHandler.OnFinishedAll = OnFinishedAll
r.resourcePackHandler.SetServer(r)

logrus.Infof("Reading replay %s", filename)
Expand Down
12 changes: 11 additions & 1 deletion utils/proxy/resourcepacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ type rpHandler struct {
// optional callback that is called as soon as a resource pack is added to the proxies list
OnFinishedPack func(resource.Pack) error

OnFinishedAll func() bool

clientDone chan struct{}
}

Expand Down Expand Up @@ -507,12 +509,20 @@ func (r *rpHandler) OnResourcePackStack(pk *packet.ResourcePackStack) error {

r.remoteStack = pk
close(r.receivedRemoteStack)
r.log.Debug("received remote resourcepack stack, starting game")

if r.clientDone != nil {
r.log.Debug("waiting for client to finish downloading")
<-r.clientDone
}

if r.OnFinishedAll != nil {
if r.OnFinishedAll() {
return nil
}
}

r.log.Debug("starting game")

r.Server.Expect(packet.IDStartGame)
_ = r.Server.WritePacket(&packet.ResourcePackClientResponse{Response: packet.PackResponseCompleted})
return nil
Expand Down

0 comments on commit 81a13f2

Please sign in to comment.