fix(all): restore room placement, inventory counts and saved layouts
This commit is contained in:
1 parent
3b39718d76
commit
e06d68aaaf
10 files changed
+441
-14
No files matched your search
@@ -76,7 +76,7 @@ func (p *playerAssembly) session(ctx command.Context) error {
|
||||
return published.SupporterDetails, err
|
||||
}},
|
||||
p.profileService,
|
||||
social.RoomCommerce{Design: p.design.roomCommerce, Rooms: p.design.rooms, Economy: p.eventEconomy},
|
||||
social.RoomCommerce{Design: p.design.roomCommerce, Rooms: p.design.rooms, Economy: p.eventEconomy, PackCleared: p.worldService.CashPackagePackCleared},
|
||||
commerce.PackInfoHandler{World: p.worldService, Claims: p.clearPackages},
|
||||
p.worldService,
|
||||
p.progressState,
|
||||
|
||||
@@ -4,7 +4,9 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -22,6 +24,15 @@ func TestNativeRoomPurchasesAndExpansionAreAtomicAndDurable(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fixtureCtx := command.Context{State: tx}
|
||||
for _, pack := range []int{1, 2} {
|
||||
for _, quest := range factory.design.world.Story.Packs[pack].MainQuestIDs {
|
||||
if err := actor.assembly.progressState.ClearQuest(fixtureCtx, quest, pack, 0); err != nil {
|
||||
_ = tx.Rollback()
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
_, err = actor.assembly.eventEconomy.Apply(command.Context{State: tx}, "room-commerce-fixture", nil, []gamedata.Reward{{Type: 4, Count: 3000000}, {Type: 44, Count: 300}})
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
@@ -209,3 +220,78 @@ func TestNativeRoomPurchasesAndExpansionAreAtomicAndDurable(t *testing.T) {
|
||||
t.Fatalf("purchase assets changed after restart: %+v want%+v", got, committed)
|
||||
}
|
||||
}
|
||||
|
||||
// Current MyRoomItemTable 101 has packId=1, questLevel=0; LocalText140059
|
||||
// requires Knight of Blood Normal. MyRoomShopScrollItem uses that same gate.
|
||||
func TestNativeRoomShopRequiresCompletedPackBeforeCharging(t *testing.T) {
|
||||
factory, accounts := newIntegrationFactory(t)
|
||||
s := &trapPlayerScenario{factory: factory, account: accounts[0], owner: openNicknamePlayer(t, factory, accounts[0])}
|
||||
t.Cleanup(func() { closeNicknamePlayer(t, s.owner) })
|
||||
s.run(t, true, func(ctx command.Context, p *playerAssembly) {
|
||||
if _, err := p.eventEconomy.Apply(ctx, "locked-room-shop-fixture", nil, []gamedata.Reward{{Type: 4, Count: 3000000}, {Type: 44, Count: 300}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
payment := func(typ, id, n uint64) []byte {
|
||||
return wire.AppendVarint(wire.AppendVarint(wire.AppendVarint(nil, 3, typ), 2, id), 4, n)
|
||||
}
|
||||
buy := wire.AppendVarint(wire.AppendVarint(nil, 2, 1), 3, 1)
|
||||
buy = wire.AppendBytes(buy, 4, payment(4, 0, 500000))
|
||||
var beforeItems any
|
||||
var beforeGold uint64
|
||||
s.run(t, false, func(ctx command.Context, p *playerAssembly) {
|
||||
beforeGold = p.wallet.Snapshot(ctx).Gold
|
||||
beforeItems = p.ownedItems.All(ctx)
|
||||
})
|
||||
if _, err := s.execute("/MyRoomShopBuy", append(appScalar(1, 10), buy...)); err == nil || !strings.Contains(err.Error(), "requires pack 1 difficulty 0") {
|
||||
t.Fatalf("locked purchase: %v", err)
|
||||
}
|
||||
s.reopen(t)
|
||||
s.run(t, false, func(ctx command.Context, p *playerAssembly) {
|
||||
if p.wallet.Snapshot(ctx).Gold != beforeGold || !reflect.DeepEqual(p.ownedItems.All(ctx), beforeItems) {
|
||||
t.Fatal("locked single purchase changed currency or inventory")
|
||||
}
|
||||
})
|
||||
s.run(t, true, func(ctx command.Context, p *playerAssembly) {
|
||||
for _, quest := range factory.design.world.Story.Packs[1].MainQuestIDs {
|
||||
if err := p.progressState.ClearQuest(ctx, quest, 1, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
var lockedID uint64
|
||||
for id, product := range factory.design.roomCommerce.Products {
|
||||
if item := factory.design.rooms.Items[product.Reward.ID]; item.PackID > 1 && (lockedID == 0 || id < lockedID) {
|
||||
lockedID = id
|
||||
}
|
||||
}
|
||||
if lockedID == 0 {
|
||||
t.Fatal("GameData fixture lacks another locked pack furnishing")
|
||||
}
|
||||
product := factory.design.roomCommerce.Products[lockedID]
|
||||
price := product.Prices[0]
|
||||
first := wire.AppendBytes(wire.AppendVarint(wire.AppendVarint(nil, 1, 1), 2, 1), 3, payment(4, 0, 500000))
|
||||
second := wire.AppendBytes(wire.AppendVarint(wire.AppendVarint(nil, 1, lockedID), 2, 1), 3, payment(price.Type, price.ID, price.Count))
|
||||
batch := wire.AppendBytes(wire.AppendBytes(appScalar(1, 11), 2, first), 2, second)
|
||||
if _, err := s.execute("/MyRoomShopMultiBuy", batch); err == nil || !strings.Contains(err.Error(), "requires pack") {
|
||||
t.Fatalf("mixed unlocked/locked purchase: %v", err)
|
||||
}
|
||||
s.reopen(t)
|
||||
s.run(t, false, func(ctx command.Context, p *playerAssembly) {
|
||||
if got := p.wallet.Snapshot(ctx).Gold; got != beforeGold {
|
||||
t.Fatalf("locked batch charged gold: %d want %d", got, beforeGold)
|
||||
}
|
||||
if !reflect.DeepEqual(p.ownedItems.All(ctx), beforeItems) {
|
||||
t.Fatal("locked purchase changed inventory")
|
||||
}
|
||||
})
|
||||
if got := s.protocol(t, "/MyRoomShopInfo", appScalar(1, 12)); len(appRows(got, 1)) != 0 {
|
||||
t.Fatal("locked purchase recorded a purchase count")
|
||||
}
|
||||
s.protocol(t, "/MyRoomShopBuy", append(appScalar(1, 13), buy...))
|
||||
s.run(t, false, func(ctx command.Context, p *playerAssembly) {
|
||||
if got := p.wallet.Snapshot(ctx).Gold; got != beforeGold-500000 {
|
||||
t.Fatalf("completed pack purchase gold=%d want %d", got, beforeGold-500000)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -3,12 +3,13 @@ package gamedata
|
||||
import "fmt"
|
||||
|
||||
type RoomLimits struct{ Objects, Carpets, Characters uint64 }
|
||||
type RoomItem struct{ Kind, ObjectType, Location, MaxCount uint64 }
|
||||
type RoomItem struct{ Kind, ObjectType, Location, MaxCount, PackID, QuestLevel uint64 }
|
||||
type RoomDesign struct {
|
||||
Defaults []uint64
|
||||
Limits map[uint64]RoomLimits
|
||||
Items map[uint64]RoomItem
|
||||
ListCount, MyPresets, UserPresets uint64
|
||||
HiddenWallItemID uint64
|
||||
}
|
||||
|
||||
func LoadRoomDesign(root, version string) (*RoomDesign, error) {
|
||||
@@ -26,6 +27,10 @@ func LoadRoomDesign(root, version string) (*RoomDesign, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d.HiddenWallItemID, err = optionalScalar(raw, 11)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d.UserPresets, err = optionalScalar(raw, 12)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -51,11 +56,11 @@ func LoadRoomDesign(root, version string) (*RoomDesign, error) {
|
||||
return nil, fmt.Errorf("gamedata: no default rooms")
|
||||
}
|
||||
if err := readCashMetadata(db, "MyRoomItemTable", func(raw []byte) error {
|
||||
v, err := cashScalars(raw, 7, 23, 19, 15, 16)
|
||||
v, err := cashScalars(raw, 7, 23, 19, 15, 16, 20, 22)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.Items[v[0]] = RoomItem{Kind: v[1], ObjectType: v[2], Location: v[3], MaxCount: v[4]}
|
||||
d.Items[v[0]] = RoomItem{Kind: v[1], ObjectType: v[2], Location: v[3], MaxCount: v[4], PackID: v[5], QuestLevel: v[6]}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -20,9 +20,10 @@ type RoomEconomy interface {
|
||||
}
|
||||
|
||||
type RoomCommerce struct {
|
||||
Design *gamedata.RoomCommerceDesign
|
||||
Rooms *gamedata.RoomDesign
|
||||
Economy RoomEconomy
|
||||
Design *gamedata.RoomCommerceDesign
|
||||
Rooms *gamedata.RoomDesign
|
||||
Economy RoomEconomy
|
||||
PackCleared func(uint64, uint64) bool
|
||||
}
|
||||
|
||||
type roomCommerceState struct {
|
||||
@@ -162,6 +163,13 @@ func (s RoomCommerce) Handle(ctx command.Context, path string, raw []byte) (int,
|
||||
return fail(fmt.Errorf("%w: room product %d count %d exceeds its limit (known=%t, repeated=%t, bought %d)", ErrCapacity, id, n, known, seen[id], state.Bought[id]))
|
||||
}
|
||||
seen[id] = true
|
||||
item, known := s.Rooms.Items[product.Reward.ID]
|
||||
if !known || product.Reward.Type != 27 {
|
||||
return fail(fmt.Errorf("%w: room product %d has an unknown furnishing %d", ErrInvalid, id, product.Reward.ID))
|
||||
}
|
||||
if item.PackID != 0 && (s.PackCleared == nil || !s.PackCleared(item.PackID, item.QuestLevel)) {
|
||||
return fail(fmt.Errorf("%w: room product %d requires pack %d difficulty %d", ErrIneligible, id, item.PackID, item.QuestLevel))
|
||||
}
|
||||
if err := roomPayment(row, paymentField, product.Prices, n, &costs); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
|
||||
@@ -204,6 +204,12 @@ func (f *Friends) editRooms(state *personalRooms, self FriendProfile, path strin
|
||||
state.Rooms[index], state.Rooms[other] = state.Rooms[other], state.Rooms[index]
|
||||
state.Rooms[index].ID = id
|
||||
state.Rooms[other].ID = target
|
||||
switch state.Primary {
|
||||
case id:
|
||||
state.Primary = target
|
||||
case target:
|
||||
state.Primary = id
|
||||
}
|
||||
if err := f.validatePositions(state.Rooms[index], self, f.rooms.Limits[id]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -3,12 +3,15 @@ package social_test
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"bd2server/internal/server/design/gamedata"
|
||||
"bd2server/internal/server/domain/command"
|
||||
"bd2server/internal/server/domain/inventory"
|
||||
"bd2server/internal/server/domain/profile"
|
||||
"bd2server/internal/server/domain/roster"
|
||||
"bd2server/internal/server/domain/social"
|
||||
"bd2server/internal/server/protocol/wire"
|
||||
)
|
||||
@@ -118,3 +121,191 @@ func TestRoomPopularityResetsMonthlyPerViewerWithoutResettingTotal(t *testing.T)
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// MyRoomSettingUI subtracts use counts from inventory across all rooms.
|
||||
// MyRoomPacket.RecvMyRoomMoveResponse moves the representative ID with its layout.
|
||||
func TestRoomLayoutUsageAndRepresentativeSurviveReloadAndMove(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := &memoryStore{rows: map[string]map[string][]byte{}}
|
||||
design := &gamedata.RoomDesign{Defaults: []uint64{1, 2}, Limits: map[uint64]gamedata.RoomLimits{1: {Objects: 4, Characters: 4}, 2: {Objects: 4, Characters: 4}}, Items: map[uint64]gamedata.RoomItem{201: {Kind: 1}}}
|
||||
open := func() *social.Friends {
|
||||
t.Helper()
|
||||
f, err := social.NewFriends(store, gamedata.FriendRules{Friends: 50, Sent: 50, Received: 50, Recommended: 10, ExpireDays: 7}, gamedata.SupporterRules{}, design, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
f := open()
|
||||
owner := social.FriendProfile{AccountID: "1", OwnerIndex: 1, Version: 1, Snapshot: profile.Snapshot{OwnerIndex: 1}, RoomItems: []inventory.Item{{Type: 27, ID: 201, InvenIndex: 1010, Count: 2, UseCount: 99}, {Type: 28, ID: 301, InvenIndex: 3030, Count: 1}}, RoomCostumes: []roster.Costume{{ID: 101, InvenIndex: 2020}}}
|
||||
if err := f.Publish(ctx, owner); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.Publish(ctx, social.FriendProfile{AccountID: "2", OwnerIndex: 2, Version: 1, Snapshot: profile.Snapshot{OwnerIndex: 2}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seq := uint64(0)
|
||||
call := func(path string, fields []byte) []byte {
|
||||
t.Helper()
|
||||
seq++
|
||||
_, body, err := f.Execute(ctx, command.Identity{AccountID: "1", SessionID: "usage", RequestID: fmt.Sprint(seq)}, 1, path, append(wire.AppendVarint(nil, 1, seq), fields...), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return body
|
||||
}
|
||||
position := func(index, typ uint64) []byte {
|
||||
return wire.AppendVarint(wire.AppendVarint(wire.AppendVarint(nil, 1, index), 2, typ), 3, 1)
|
||||
}
|
||||
room := func(id uint64, name string, hidden bool, positions ...[]byte) []byte {
|
||||
out := wire.AppendString(wire.AppendVarint(nil, 1, id), 2, name)
|
||||
if hidden {
|
||||
out = wire.AppendVarint(out, 4, 1)
|
||||
}
|
||||
for _, p := range positions {
|
||||
out = wire.AppendBytes(out, 3, p)
|
||||
}
|
||||
return out
|
||||
}
|
||||
saved := wire.AppendBytes(nil, 2, room(1, "Representative", false, position(1010, 0), position(2020, 1)))
|
||||
saved = wire.AppendBytes(saved, 2, room(2, "Hidden", true, position(1010, 0), position(3030, 2)))
|
||||
call("/MyRoomSave", saved)
|
||||
f = open()
|
||||
owner.Version++
|
||||
if err := f.Publish(ctx, owner); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rows := func(raw []byte, field int) [][]byte {
|
||||
t.Helper()
|
||||
var result [][]byte
|
||||
if err := wire.Walk(raw, func(v wire.Field) error {
|
||||
if v.Number == field {
|
||||
result = append(result, v.Value)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
value := func(raw []byte, field int) uint64 {
|
||||
t.Helper()
|
||||
v, _, err := wire.Varint(raw, field)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
assertUsage := func(furniture, costume, trophy uint64) {
|
||||
t.Helper()
|
||||
response := call("/MyRoomUserInfo", wire.AppendVarint(nil, 2, 1))
|
||||
if value(response, 2) != 1 {
|
||||
t.Fatal("own room response did not synchronize global inventory usage")
|
||||
}
|
||||
info := rows(response, 1)[0]
|
||||
if got := value(rows(info, 5)[0], 10); got != furniture {
|
||||
t.Fatalf("furnishing usage=%d want %d", got, furniture)
|
||||
}
|
||||
if got := value(rows(info, 6)[0], 7); got != costume {
|
||||
t.Fatalf("costume usage=%d want %d", got, costume)
|
||||
}
|
||||
if got := value(rows(info, 7)[0], 5); got != trophy {
|
||||
t.Fatalf("trophy usage=%d want %d", got, trophy)
|
||||
}
|
||||
if got := value(rows(call("/MyRoomItemInfo", nil), 1)[0], 5); got != trophy {
|
||||
t.Fatalf("trophy inventory usage=%d want %d", got, trophy)
|
||||
}
|
||||
}
|
||||
assertUsage(2, 1, 1)
|
||||
call("/MyRoomMove", wire.AppendVarint(wire.AppendVarint(nil, 2, 1), 3, 2))
|
||||
f = open()
|
||||
info, _, err := f.RoomProfile(ctx, 2, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if value(info, 4) != 2 {
|
||||
t.Fatal("representative ID did not follow the moved room")
|
||||
}
|
||||
visible := rows(info, 8)
|
||||
if len(visible) != 1 || value(visible[0], 1) != 2 || len(rows(visible[0], 3)) != 2 {
|
||||
t.Fatal("visitor lost the moved representative layout")
|
||||
}
|
||||
assertUsage(2, 1, 1)
|
||||
call("/MyRoomSave", wire.AppendBytes(nil, 2, room(1, "Empty", true)))
|
||||
assertUsage(1, 1, 0)
|
||||
call("/MyRoomSave", wire.AppendBytes(nil, 2, room(2, "Empty representative", false)))
|
||||
assertUsage(0, 0, 0)
|
||||
}
|
||||
|
||||
// MyRoomPacket.GetPresetInfoForStore replaces a visited hidden wallpaper with
|
||||
// the viewer's PresetHiddenWallItemId inventory index before sending the preset.
|
||||
func TestVisitedRoomPresetAcceptsOnlyHiddenWallpaperSubstitution(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := &memoryStore{rows: map[string]map[string][]byte{}}
|
||||
design := &gamedata.RoomDesign{Defaults: []uint64{1}, Limits: map[uint64]gamedata.RoomLimits{1: {}}, UserPresets: 10, HiddenWallItemID: 501,
|
||||
Items: map[uint64]gamedata.RoomItem{201: {Location: 1}, 501: {Location: 1}},
|
||||
}
|
||||
f, err := social.NewFriends(store, gamedata.FriendRules{Friends: 50, Sent: 50, Received: 50, Recommended: 10, ExpireDays: 7}, gamedata.SupporterRules{}, design, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, p := range []social.FriendProfile{
|
||||
{AccountID: "1", OwnerIndex: 1, Version: 1, Snapshot: profile.Snapshot{OwnerIndex: 1}, RoomItems: []inventory.Item{{Type: 27, ID: 201, InvenIndex: 1010, Count: 2}, {Type: 27, ID: 501, InvenIndex: 5050, Count: 9999}}},
|
||||
{AccountID: "2", OwnerIndex: 2, Version: 1, Snapshot: profile.Snapshot{OwnerIndex: 2}, RoomItems: []inventory.Item{{Type: 27, ID: 501, InvenIndex: 6060, Count: 9999}}},
|
||||
} {
|
||||
if err := f.Publish(ctx, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
seq := uint64(0)
|
||||
call := func(actor uint64, path string, fields []byte) ([]byte, error) {
|
||||
seq++
|
||||
_, body, err := f.Execute(ctx, command.Identity{AccountID: fmt.Sprint(actor), SessionID: "hidden-preset", RequestID: fmt.Sprint(seq)}, actor, path, append(wire.AppendVarint(nil, 1, seq), fields...), 0)
|
||||
return body, err
|
||||
}
|
||||
position := func(index, board, x uint64) []byte {
|
||||
return wire.AppendVarint(wire.AppendVarint(wire.AppendVarint(wire.AppendVarint(nil, 1, index), 3, board), 4, x), 9, 1)
|
||||
}
|
||||
sourceRoom := wire.AppendBytes(wire.AppendBytes(wire.AppendVarint(nil, 1, 1), 3, position(1010, 2, 0)), 3, position(1010, 3, 0))
|
||||
if _, err := call(1, "/MyRoomSave", wire.AppendBytes(nil, 2, sourceRoom)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
preset := func(itemIndex, x uint64) []byte {
|
||||
item := wire.AppendVarint(wire.AppendVarint(wire.AppendVarint(nil, 1, itemIndex), 2, 501), 4, 2)
|
||||
room := wire.AppendBytes(wire.AppendBytes(wire.AppendVarint(nil, 1, 1), 3, position(itemIndex, 2, x)), 3, position(itemIndex, 3, 0))
|
||||
out := wire.AppendBytes(wire.AppendBytes(wire.AppendVarint(nil, 1, 1), 3, item), 4, room)
|
||||
return wire.AppendBytes(wire.AppendVarint(nil, 2, 1), 3, out)
|
||||
}
|
||||
if _, err := call(2, "/MyRoomPresetSave", preset(6060, 0)); err != nil {
|
||||
t.Fatalf("official hidden wallpaper substitution rejected: %v", err)
|
||||
}
|
||||
before, err := call(2, "/MyRoomPresetInfo", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := call(2, "/MyRoomPresetSave", preset(6060, 1)); err == nil {
|
||||
t.Fatal("hidden wallpaper substitution allowed changed source coordinates")
|
||||
}
|
||||
if _, err := call(2, "/MyRoomPresetSave", preset(7070, 0)); err == nil {
|
||||
t.Fatal("hidden wallpaper substitution accepted unowned inventory index")
|
||||
}
|
||||
after, err := call(2, "/MyRoomPresetInfo", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !slices.Equal(before, after) {
|
||||
t.Fatal("invalid visited preset overwrote the valid stored layout")
|
||||
}
|
||||
found := false
|
||||
if err := wire.Walk(after, func(field wire.Field) error {
|
||||
if field.Number == 2 {
|
||||
found = true
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("visited preset was not stored in the visitor slots")
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package social
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"bd2server/internal/server/protocol/wire"
|
||||
)
|
||||
@@ -106,7 +107,7 @@ func (f *Friends) roomPreset(tx Tx, self FriendProfile, path string, p packet) (
|
||||
if !visible {
|
||||
return nil, fmt.Errorf("%w: rooms of owner %d are not visible to owner %d", ErrPermission, sourceOwner, self.OwnerIndex)
|
||||
}
|
||||
if err := f.validateRoomPreset(source, self.OwnerIndex == sourceOwner, state, row, &preset); err != nil {
|
||||
if err := f.validateRoomPreset(source, self, state, row, &preset); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -140,11 +141,22 @@ func (f *Friends) roomPreset(tx Tx, self FriendProfile, path string, p packet) (
|
||||
return nil, save(tx, "friend.room_presets", key, preset)
|
||||
}
|
||||
|
||||
func (f *Friends) validateRoomPreset(source FriendProfile, own bool, state personalRooms, row packet, result *roomPreset) error {
|
||||
func (f *Friends) validateRoomPreset(source, viewer FriendProfile, state personalRooms, row packet, result *roomPreset) error {
|
||||
own := source.OwnerIndex == viewer.OwnerIndex
|
||||
validationSource := source
|
||||
validationSource.RoomItems = slices.Clone(source.RoomItems)
|
||||
if !own {
|
||||
for _, item := range viewer.RoomItems {
|
||||
if item.Type == 27 && item.ID == f.rooms.HiddenWallItemID {
|
||||
validationSource.RoomItems = append(validationSource.RoomItems, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(row[4]) == 0 || len(row[4]) > len(f.rooms.Limits) || len(row[3]) > 256 {
|
||||
return fmt.Errorf("%w: room preset carries %d rooms and %d items", ErrInvalid, len(row[4]), len(row[3]))
|
||||
}
|
||||
items := map[uint64]uint64{}
|
||||
itemIDs := map[uint64]uint64{}
|
||||
seen := map[uint64]bool{}
|
||||
for _, field := range row[3] {
|
||||
if field.Type != 2 {
|
||||
@@ -168,7 +180,7 @@ func (f *Friends) validateRoomPreset(source FriendProfile, own bool, state perso
|
||||
}
|
||||
seen[index] = true
|
||||
owned := false
|
||||
for _, v := range source.RoomItems {
|
||||
for _, v := range validationSource.RoomItems {
|
||||
if v.Type == 27 && v.InvenIndex == index && v.ID == id && v.Count >= count {
|
||||
owned = true
|
||||
break
|
||||
@@ -178,6 +190,7 @@ func (f *Friends) validateRoomPreset(source FriendProfile, own bool, state perso
|
||||
return fmt.Errorf("%w: owner %d lacks %d of room item %d at index %d", ErrPermission, source.OwnerIndex, count, id, index)
|
||||
}
|
||||
items[index] = count
|
||||
itemIDs[index] = id
|
||||
out := wire.AppendVarint(nil, 1, index)
|
||||
out = wire.AppendVarint(out, 2, id)
|
||||
out = wire.AppendVarint(out, 4, count)
|
||||
@@ -248,7 +261,7 @@ func (f *Friends) validateRoomPreset(source FriendProfile, own bool, state perso
|
||||
if !own {
|
||||
matched := false
|
||||
for _, raw := range original.Positions {
|
||||
if bytes.Equal(raw, position.Value) {
|
||||
if bytes.Equal(raw, position.Value) || !own && itemIDs[index] == f.rooms.HiddenWallItemID && roomPresetHiddenWallMatches(raw, position.Value, source, f.rooms.HiddenWallItemID) {
|
||||
matched = true
|
||||
break
|
||||
}
|
||||
@@ -259,7 +272,7 @@ func (f *Friends) validateRoomPreset(source FriendProfile, own bool, state perso
|
||||
}
|
||||
v.Positions = append(v.Positions, append([]byte(nil), position.Value...))
|
||||
}
|
||||
if err := f.validatePositions(v, source, limits); err != nil {
|
||||
if err := f.validatePositions(v, validationSource, limits); err != nil {
|
||||
return err
|
||||
}
|
||||
result.Rooms = append(result.Rooms, roomWire(v))
|
||||
@@ -300,3 +313,35 @@ func (f *Friends) presetText(p packet, path string) ([]string, error) {
|
||||
}
|
||||
return texts, nil
|
||||
}
|
||||
|
||||
func roomPresetHiddenWallMatches(original, transformed []byte, source FriendProfile, hiddenWallID uint64) bool {
|
||||
originalIndex, _, err := wire.Varint(original, 1)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
hidden, _, err := wire.Varint(original, 9)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
allowed := hidden == 1
|
||||
for _, item := range source.RoomItems {
|
||||
if item.Type == 27 && item.InvenIndex == originalIndex && item.ID == hiddenWallID {
|
||||
allowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !allowed {
|
||||
return false
|
||||
}
|
||||
for field := 2; field <= 9; field++ {
|
||||
a, _, err := wire.Varint(original, field)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
b, _, err := wire.Varint(transformed, field)
|
||||
if err != nil || a != b {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -154,6 +154,10 @@ func (f *Friends) roomUser(tx Tx, viewer, owner FriendProfile, state personalRoo
|
||||
if !visible {
|
||||
return nil, fmt.Errorf("%w: rooms of owner %d are not visible to owner %d", ErrPermission, owner.OwnerIndex, viewer.OwnerIndex)
|
||||
}
|
||||
usage, err := roomUsage(state)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := wire.AppendVarint(nil, 1, owner.OwnerIndex)
|
||||
out = wire.AppendString(out, 2, owner.Snapshot.UserID)
|
||||
out = wire.AppendVarint(out, 3, owner.Snapshot.PortraitCostumeID)
|
||||
@@ -161,15 +165,18 @@ func (f *Friends) roomUser(tx Tx, viewer, owner FriendProfile, state personalRoo
|
||||
for _, item := range owner.RoomItems {
|
||||
switch item.Type {
|
||||
case 27:
|
||||
item.UseCount = usage[[2]uint64{0, item.InvenIndex}]
|
||||
out = wire.AppendBytes(out, 5, inventory.ItemWire(item))
|
||||
case 28:
|
||||
v := wire.AppendVarint(nil, 1, item.InvenIndex)
|
||||
v = wire.AppendVarint(v, 2, item.ID)
|
||||
v = wire.AppendVarint(v, 5, usage[[2]uint64{2, item.InvenIndex}])
|
||||
out = wire.AppendBytes(out, 7, v)
|
||||
}
|
||||
}
|
||||
for _, costume := range owner.RoomCostumes {
|
||||
out = wire.AppendBytes(out, 6, roster.CostumeWire(costume))
|
||||
v := wire.AppendVarint(roster.CostumeWire(costume), 7, usage[[2]uint64{1, costume.InvenIndex}])
|
||||
out = wire.AppendBytes(out, 6, v)
|
||||
}
|
||||
primaryVisible := false
|
||||
for _, room := range state.Rooms {
|
||||
@@ -258,7 +265,11 @@ func (f *Friends) room(tx Tx, self FriendProfile, path string, p packet, now int
|
||||
if err != nil {
|
||||
return fail("Room is unavailable")
|
||||
}
|
||||
return wire.AppendBytes(nil, 1, out), nil
|
||||
response := wire.AppendBytes(nil, 1, out)
|
||||
if target == self.OwnerIndex {
|
||||
response = wire.AppendVarint(response, 2, 1)
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
if target == self.OwnerIndex {
|
||||
return fail("Cannot like your own room")
|
||||
@@ -302,11 +313,16 @@ func (f *Friends) room(tx Tx, self FriendProfile, path string, p packet, now int
|
||||
return f.searchRooms(tx, self, path, p, now)
|
||||
}
|
||||
if path == "/MyRoomItemInfo" {
|
||||
usage, err := roomUsage(state)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []byte
|
||||
for _, item := range self.RoomItems {
|
||||
if item.Type == 28 {
|
||||
v := wire.AppendVarint(nil, 1, item.InvenIndex)
|
||||
v = wire.AppendVarint(v, 2, item.ID)
|
||||
v = wire.AppendVarint(v, 5, usage[[2]uint64{2, item.InvenIndex}])
|
||||
out = wire.AppendBytes(out, 1, v)
|
||||
}
|
||||
}
|
||||
@@ -317,3 +333,21 @@ func (f *Friends) room(tx Tx, self FriendProfile, path string, p packet, now int
|
||||
}
|
||||
return nil, save(tx, "friend.rooms", numberKey(self.OwnerIndex), state)
|
||||
}
|
||||
|
||||
func roomUsage(state personalRooms) (map[[2]uint64]uint64, error) {
|
||||
used := map[[2]uint64]uint64{}
|
||||
for _, room := range state.Rooms {
|
||||
for _, position := range room.Positions {
|
||||
index, _, err := wire.Varint(position, 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
typ, _, err := wire.Varint(position, 2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
used[[2]uint64{typ, index}]++
|
||||
}
|
||||
}
|
||||
return used, nil
|
||||
}
|
||||
@@ -81,6 +81,7 @@ public sealed class Plugin : BaseUnityPlugin
|
||||
TryInstall("notice presentation", () => NoticePresentation.Install(harmony));
|
||||
TryInstall("event request diagnostics", () => EventRequestDiagnostics.Install(harmony, Logger));
|
||||
TryInstall("item stack refresh", () => ItemStackRefresh.Install(harmony, this, Logger));
|
||||
TryInstall("room placement stability", () => RoomPlacementStability.Install(harmony, Logger));
|
||||
Logger.LogInfo("Local identity active: AppManager.IsPlatformLogin => false");
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using BD2.GameNames;
|
||||
using BepInEx.Logging;
|
||||
using HarmonyLib;
|
||||
|
||||
namespace Bd2LocalIdentity;
|
||||
|
||||
internal static class RoomPlacementStability
|
||||
{
|
||||
private static MethodInfo IsHideGetter;
|
||||
|
||||
internal static void Install(Harmony harmony, ManualLogSource logger)
|
||||
{
|
||||
Type stateMachine = Game.FindType("MyRoom/<SetupBoardSkinObjectAsync>d__50")
|
||||
?? throw new MissingMemberException("MyRoom board-skin setup state machine is unavailable");
|
||||
MethodInfo moveNext = stateMachine.GetMethod("MoveNext", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
||||
?? throw new MissingMethodException("MyRoom board-skin setup MoveNext is unavailable");
|
||||
Type boardSkin = Game.FindType("MyRoomObjectBoardSkin")
|
||||
?? throw new MissingMemberException("MyRoomObjectBoardSkin is unavailable");
|
||||
IsHideGetter = boardSkin.GetGameProperty("IsHide", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetMethod
|
||||
?? throw new MissingMethodException("MyRoomObjectBoardSkin.IsHide is unavailable");
|
||||
harmony.Patch(moveNext, transpiler: new HarmonyMethod(typeof(RoomPlacementStability), nameof(NullSafeBoardSkinVisibility)));
|
||||
logger.LogInfo("MyRoom placement tolerates an empty wall before the first board skin");
|
||||
}
|
||||
|
||||
private static bool SafeIsHidden(object boardSkin)
|
||||
{
|
||||
return boardSkin != null && (bool)IsHideGetter.Invoke(boardSkin, null);
|
||||
}
|
||||
|
||||
private static IEnumerable<CodeInstruction> NullSafeBoardSkinVisibility(IEnumerable<CodeInstruction> instructions)
|
||||
{
|
||||
MethodInfo safeGetter = AccessTools.Method(typeof(RoomPlacementStability), nameof(SafeIsHidden));
|
||||
int replaced = 0;
|
||||
foreach (CodeInstruction instruction in instructions)
|
||||
{
|
||||
if (instruction.Calls(IsHideGetter))
|
||||
{
|
||||
instruction.opcode = OpCodes.Call;
|
||||
instruction.operand = safeGetter;
|
||||
replaced++;
|
||||
}
|
||||
yield return instruction;
|
||||
}
|
||||
if (replaced != 1)
|
||||
throw new InvalidOperationException($"MyRoom board-skin visibility shape changed; replaced {replaced} getter(s)");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user