Skip to content

Commit

Permalink
refresh tokens when expired
Browse files Browse the repository at this point in the history
  • Loading branch information
olebeck committed Jul 27, 2024
1 parent f7fdb1a commit 8888f3b
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 144 deletions.
133 changes: 133 additions & 0 deletions handlers/worlds/worldstate/packs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package worldstate

import (
"encoding/json"
"fmt"
"path"
"slices"
"strings"

"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/sandertv/gophertunnel/minecraft/text"
"github.com/sirupsen/logrus"
)

func addPacksJSON(fs utils.WriterFS, name string, deps []resourcePackDependency) error {
f, err := fs.Create(name)
if err != nil {
return err
}
defer f.Close()
if err := json.NewEncoder(f).Encode(deps); err != nil {
return err
}
return nil
}

var removeSpace = strings.NewReplacer(" ", "")

func formatPackName(packName string) string {
packName = text.Clean(packName)
packName, _ = filenamify.FilenamifyV2(packName)
packName = removeSpace.Replace(packName)
packName = packName[:min(10, len(packName))]
return packName
}

func (w *World) addResourcePacks() error {
packNames := make(map[string][]string)
for _, pack := range w.ResourcePacks {
packName := formatPackName(pack.Name())
packNames[packName] = append(packNames[packName], pack.UUID())
}

for _, pack := range w.ResourcePacks {
log := w.log.WithField("pack", pack.Name())
if pack.Encrypted() && !pack.CanRead() {
log.Warn("Cant add is encrypted")
continue
}
logrus.Infof(locale.Loc("adding_pack", locale.Strmap{"Name": text.Clean(pack.Name())}))

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

packName := formatPackName(pack.Name())
if packIds := packNames[packName]; len(packIds) > 1 {
packName = fmt.Sprintf("%s_%d", packName[:8], slices.Index(packIds, pack.UUID()))
}

err := utils.CopyFS(pack, utils.SubFS(utils.OSWriter{Base: w.Folder}, path.Join("resource_packs", packName)))
if err != nil {
log.Error(err)
continue
}

w.resourcePackDependencies = append(w.resourcePackDependencies, resourcePackDependency{
UUID: pack.Manifest().Header.UUID,
Version: pack.Manifest().Header.Version,
})
}

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

return nil
}

func (w *World) FinalizePacks(addBehaviorPack func(fs utils.WriterFS) (*resource.Header, error)) error {
err := <-w.resourcePacksDone
if err != nil {
return err
}

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

fs := utils.OSWriter{Base: w.Folder}
header, err := addBehaviorPack(fs)
if err != nil {
return err
}

if header != nil {
err = addPacksJSON(fs, "world_behavior_packs.json", []resourcePackDependency{{
UUID: header.UUID,
Version: header.Version,
}})
if err != nil {
return err
}
}

if len(w.resourcePackDependencies) > 0 {
err := addPacksJSON(fs, "world_resource_packs.json", w.resourcePackDependencies)
if err != nil {
return err
}
}

return nil
}
9 changes: 2 additions & 7 deletions handlers/worlds/worldstate/players.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package worldstate
import (
"github.com/bedrock-tool/bedrocktool/handlers/worlds/entity"
"github.com/go-gl/mathgl/mgl32"
"github.com/google/uuid"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
)
Expand All @@ -14,12 +13,8 @@ type player struct {
Pitch, Yaw, HeadYaw float32
}

type worldPlayers struct {
players map[uuid.UUID]*player
}

func (w *World) AddPlayer(pk *packet.AddPlayer) {
w.players.players[pk.UUID] = &player{
w.players[pk.UUID] = &player{
add: pk,
Position: pk.Position,
Pitch: pk.Pitch,
Expand All @@ -29,7 +24,7 @@ func (w *World) AddPlayer(pk *packet.AddPlayer) {
}

func (w *World) playersToEntities() {
for _, p := range w.players.players {
for _, p := range w.players {
metadata := protocol.NewEntityMetadata()
metadata.SetFlag(protocol.EntityDataKeyFlags, protocol.EntityDataFlagAlwaysShowName)
metadata.SetFlag(protocol.EntityDataKeyFlags, protocol.EntityDataFlagShowName)
Expand Down
127 changes: 2 additions & 125 deletions handlers/worlds/worldstate/world.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package worldstate

import (
"encoding/json"
"errors"
"fmt"
"os"
"path"
"slices"
"strings"
"sync"
"time"
Expand All @@ -21,14 +19,12 @@ import (
"github.com/df-mc/dragonfly/server/world/mcdb"
"github.com/df-mc/goleveldb/leveldb"
"github.com/df-mc/goleveldb/leveldb/opt"
"github.com/flytam/filenamify"
"github.com/google/uuid"
"github.com/sandertv/gophertunnel/minecraft"
"github.com/sandertv/gophertunnel/minecraft/nbt"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"github.com/sandertv/gophertunnel/minecraft/resource"
"github.com/sandertv/gophertunnel/minecraft/text"
"github.com/sirupsen/logrus"
"golang.org/x/exp/maps"
)
Expand Down Expand Up @@ -78,7 +74,7 @@ type World struct {
finish chan struct{}
err error

players worldPlayers
players map[uuid.UUID]*player

VoidGen bool
timeSync time.Time
Expand Down Expand Up @@ -130,10 +126,7 @@ func New(dimensionDefinitions map[int]protocol.DimensionDefinition, onChunkUpdat

uniqueIDsToRuntimeIDs: make(map[int64]uint64),
},
players: worldPlayers{
players: make(map[uuid.UUID]*player),
},

players: make(map[uuid.UUID]*player),
blockUpdates: make(map[world.ChunkPos][]blockUpdate),
onChunkUpdate: onChunkUpdate,
IgnoredChunks: make(map[world.ChunkPos]bool),
Expand Down Expand Up @@ -202,122 +195,6 @@ func (w *World) storeMemToProvider() error {
return nil
}

func addPacksJSON(fs utils.WriterFS, name string, deps []resourcePackDependency) error {
f, err := fs.Create(name)
if err != nil {
return err
}
defer f.Close()
if err := json.NewEncoder(f).Encode(deps); err != nil {
return err
}
return nil
}

var removeSpace = strings.NewReplacer(" ", "")

func formatPackName(packName string) string {
packName = text.Clean(packName)
packName, _ = filenamify.FilenamifyV2(packName)
packName = removeSpace.Replace(packName)
packName = packName[:min(10, len(packName))]
return packName
}

func (w *World) addResourcePacks() error {
packNames := make(map[string][]string)
for _, pack := range w.ResourcePacks {
packName := formatPackName(pack.Name())
packNames[packName] = append(packNames[packName], pack.UUID())
}

for _, pack := range w.ResourcePacks {
log := w.log.WithField("pack", pack.Name())
if pack.Encrypted() && !pack.CanRead() {
log.Warn("Cant add is encrypted")
continue
}
logrus.Infof(locale.Loc("adding_pack", locale.Strmap{"Name": text.Clean(pack.Name())}))

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

packName := formatPackName(pack.Name())
if packIds := packNames[packName]; len(packIds) > 1 {
packName = fmt.Sprintf("%s_%d", packName[:8], slices.Index(packIds, pack.UUID()))
}

err := utils.CopyFS(pack, utils.SubFS(utils.OSWriter{Base: w.Folder}, path.Join("resource_packs", packName)))
if err != nil {
log.Error(err)
continue
}

w.resourcePackDependencies = append(w.resourcePackDependencies, resourcePackDependency{
UUID: pack.Manifest().Header.UUID,
Version: pack.Manifest().Header.Version,
})
}

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

return nil
}

func (w *World) FinalizePacks(addBehaviorPack func(fs utils.WriterFS) (*resource.Header, error)) error {
err := <-w.resourcePacksDone
if err != nil {
return err
}

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

fs := utils.OSWriter{Base: w.Folder}
header, err := addBehaviorPack(fs)
if err != nil {
return err
}

if header != nil {
err = addPacksJSON(fs, "world_behavior_packs.json", []resourcePackDependency{{
UUID: header.UUID,
Version: header.Version,
}})
if err != nil {
return err
}
}

if len(w.resourcePackDependencies) > 0 {
err := addPacksJSON(fs, "world_resource_packs.json", w.resourcePackDependencies)
if err != nil {
return err
}
}

return nil
}

func (w *World) Dimension() world.Dimension {
return w.dimension
}
Expand Down
8 changes: 2 additions & 6 deletions utils/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,10 @@ func (a *authsrv) Playfab(ctx context.Context) (*playfab.Client, error) {
if err != nil {
return nil, err
}
a.playfab = playfab.NewClient(discovery)
a.playfab = playfab.NewClient(discovery, a)
}
if !a.playfab.LoggedIn() {
liveToken, err := a.Token()
if err != nil {
return nil, err
}
err = a.playfab.Login(ctx, liveToken)
err := a.playfab.Login(ctx)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 8888f3b

Please sign in to comment.