Skip to content

Commit

Permalink
dont download resourcepacks when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
olebeck committed Jul 11, 2024
1 parent 6eeaa1f commit c55966c
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 25 deletions.
2 changes: 1 addition & 1 deletion handlers/worlds/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (w *worldsHandler) preloadReplay() error {
if err != nil {
log.Error(err)
}
}, func() {}, func(p resource.Pack) {})
}, func() {}, func(p resource.Pack) {}, func(s string) bool { return false })
if err != nil {
return err
}
Expand Down
Binary file modified subcommands/resourcepack-d/resourcepack-d.go
Binary file not shown.
9 changes: 6 additions & 3 deletions ui/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import (
"github.com/google/subcommands"
)

type CLI struct{}
type CLI struct {
ctx context.Context
}

func (c *CLI) Init() bool {
messages.Router.AddHandler("ui", c.HandleMessage)
return true
}

func (c *CLI) Start(ctx context.Context, cancel context.CancelCauseFunc) error {
c.ctx = ctx
isDebug := updater.Version == ""
if !isDebug {
go updater.UpdateCheck(c)
Expand All @@ -32,9 +35,9 @@ func (c *CLI) HandleMessage(msg *messages.Message) *messages.Message {
switch data := msg.Data.(type) {
case messages.RequestLogin:
if data.Wait {
utils.Auth.Login(context.Background(), nil)
utils.Auth.Login(c.ctx, nil)
} else {
go utils.Auth.Login(context.Background(), nil)
go utils.Auth.Login(c.ctx, nil)
}
}
return nil
Expand Down
13 changes: 8 additions & 5 deletions ui/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"github.com/sirupsen/logrus"
)

type TUI struct{}
type TUI struct {
ctx context.Context
}

var _ ui.UI = &TUI{}

Expand All @@ -29,6 +31,7 @@ func (c *TUI) Init() bool {
}

func (c *TUI) Start(ctx context.Context, cancel context.CancelCauseFunc) error {
c.ctx = ctx
isDebug := updater.Version == ""
if !isDebug {
go updater.UpdateCheck(c)
Expand Down Expand Up @@ -71,22 +74,22 @@ func (c *TUI) HandleMessage(msg *messages.Message) *messages.Message {
switch msg := msg.Data.(type) {
case messages.RequestLogin:
if msg.Wait {
utils.Auth.Login(context.Background(), nil)
utils.Auth.Login(c.ctx, nil)
} else {
go utils.Auth.Login(context.Background(), nil)
go utils.Auth.Login(c.ctx, nil)
}
case *messages.ServerInput:
_ = msg
var cancelled bool
server, cancelled := utils.UserInput(context.Background(), locale.Loc("enter_server", nil), utils.ValidateServerInput)
server, cancelled := utils.UserInput(c.ctx, locale.Loc("enter_server", nil), utils.ValidateServerInput)
if cancelled {
return &messages.Message{
Source: "gui",
Data: messages.Error(context.Canceled),
}
}

ret, err := utils.ParseServer(context.Background(), server)
ret, err := utils.ParseServer(c.ctx, server)
if err != nil {
return &messages.Message{
Source: "gui",
Expand Down
3 changes: 3 additions & 0 deletions utils/proxy/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func (p *Context) connectServer(ctx context.Context) (err error) {

server, err := d.DialContext(ctx, "raknet", p.serverAddress, 10*time.Second)
if err != nil {
if p.expectDisconnect {
return nil
}
return err
}
p.Server = server
Expand Down
30 changes: 21 additions & 9 deletions utils/proxy/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import (
)

type Context struct {
Server minecraft.IConn
Client minecraft.IConn
listener *minecraft.Listener
Player Player
ExtraDebug bool
PlayerMoveCB []func()
ListenAddress string
Server minecraft.IConn
Client minecraft.IConn
expectDisconnect bool
listener *minecraft.Listener
Player Player
ExtraDebug bool
PlayerMoveCB []func()
ListenAddress string

withClient bool
addedPacks []resource.Pack
Expand Down Expand Up @@ -222,6 +223,7 @@ func (p *Context) DisconnectServer() {
if p.Server == nil {
return
}
p.expectDisconnect = true
_ = p.Server.Close()
}

Expand Down Expand Up @@ -347,11 +349,21 @@ func (p *Context) doSession(ctx context.Context, cancel context.CancelCauseFunc)
},
})

filterDownloadResourcePacks := func(id string) bool {
ignore := false
for _, handler := range p.handlers {
if handler.FilterResourcePack != nil {
ignore = handler.FilterResourcePack(id)
}
}
return ignore
}

// 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)
server, err := CreateReplayConnector(ctx, filename, p.packetFunc, p.onResourcePacksInfo, p.onFinishedPack, filterDownloadResourcePacks)
if err != nil {
return err
}
Expand All @@ -361,7 +373,7 @@ func (p *Context) doSession(ctx context.Context, cancel context.CancelCauseFunc)
return err
}
} else {
p.rpHandler = newRpHandler(ctx, p.addedPacks)
p.rpHandler = newRpHandler(ctx, p.addedPacks, filterDownloadResourcePacks)
p.rpHandler.OnResourcePacksInfoCB = p.onResourcePacksInfo
p.rpHandler.OnFinishedPack = p.onFinishedPack

Expand Down
7 changes: 4 additions & 3 deletions utils/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ type ingameCommand struct {
type Handler struct {
Name string

ProxyReference func(c *Context)
GameDataModifier func(gameData *minecraft.GameData)
OnAddressAndName func(address, hostname string) error
ProxyReference func(c *Context)
GameDataModifier func(gameData *minecraft.GameData)
OnAddressAndName func(address, hostname string) error
FilterResourcePack func(id string) 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
4 changes: 2 additions & 2 deletions utils/proxy/replay_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ func (r *ReplayConnector) ReadUntilLogin() error {
return nil
}

func CreateReplayConnector(ctx context.Context, filename string, packetFunc PacketFunc, onResourcePackInfo func(), OnFinishedPack func(resource.Pack)) (r *ReplayConnector, err error) {
func CreateReplayConnector(ctx context.Context, filename string, packetFunc PacketFunc, onResourcePackInfo func(), OnFinishedPack func(resource.Pack), filterDownloadResourcePacks func(string) bool) (r *ReplayConnector, err error) {
r = &ReplayConnector{
spawn: make(chan struct{}),
close: make(chan struct{}),
}
r.resourcePackHandler = newRpHandler(ctx, nil)
r.resourcePackHandler = newRpHandler(ctx, nil, filterDownloadResourcePacks)
r.resourcePackHandler.OnResourcePacksInfoCB = onResourcePackInfo
r.resourcePackHandler.OnFinishedPack = OnFinishedPack
r.resourcePackHandler.SetServer(r)
Expand Down
19 changes: 17 additions & 2 deletions utils/proxy/resourcepacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type rpHandler struct {
ctx context.Context
log *logrus.Entry

filterDownloadResourcePacks func(id string) bool

// wait for downloads to be done
dlwg sync.WaitGroup

Expand Down Expand Up @@ -105,7 +107,7 @@ type rpHandler struct {
OnFinishedPack func(resource.Pack)
}

func newRpHandler(ctx context.Context, addedPacks []resource.Pack) *rpHandler {
func newRpHandler(ctx context.Context, addedPacks []resource.Pack, filterDownloadResourcePacks func(string) bool) *rpHandler {
r := &rpHandler{
ctx: ctx,
log: logrus.WithField("part", "ResourcePacks"),
Expand All @@ -118,6 +120,8 @@ func newRpHandler(ctx context.Context, addedPacks []resource.Pack) *rpHandler {
cache: &packCache{},
receivedRemotePackInfo: make(chan struct{}),
receivedRemoteStack: make(chan struct{}),

filterDownloadResourcePacks: filterDownloadResourcePacks,
}
return r
}
Expand Down Expand Up @@ -209,7 +213,6 @@ func (r *rpHandler) OnResourcePacksInfo(pk *packet.ResourcePacksInfo) error {
continue
}

// This UUID_Version is a hack Mojang put in place.
packsToDownload = append(packsToDownload, packID)

m, ok := r.downloadQueue.downloadingPacks[pack.UUID]
Expand Down Expand Up @@ -272,6 +275,18 @@ func (r *rpHandler) OnResourcePacksInfo(pk *packet.ResourcePacksInfo) error {
}
}

packsToDownload = slices.DeleteFunc(packsToDownload, func(id string) bool {
ignore := r.filterDownloadResourcePacks(id)
if ignore {
idsplit := strings.Split(id, "_")
r.ignoredResourcePacks = append(r.ignoredResourcePacks, exemptedResourcePack{
uuid: idsplit[0],
version: idsplit[1],
})
}
return ignore
})

if len(packsToDownload) != 0 {
// start downloading from server whenever a pack info is sent
r.packDownloads = make(chan *packet.ResourcePackDataInfo, len(packsToDownload))
Expand Down

0 comments on commit c55966c

Please sign in to comment.