DNF90 服务端与启动器完整源码(含 GM 工具功能增强)

服务端(Go):
- GM 塞背包/带属性装备发放:在线角色推送 op14 增量物品行,背包实时刷新,无需回角色选择界面
- GM 改金币/改等级:去掉在线拒绝,落库后推送 op14 钱包/op37 等级状态,在线实时刷新
- 等级端点统一切换到新版 GMSetCharacterLevel(自带在线刷新)
- 无限/锁死疲劳模式 + 燃烧疲劳:每房间消耗 2 点、打怪与通关经验双倍
- 燃烧疲劳开关时给所有在线角色推送系统提示(op12 mode 0)
- 深渊史诗掉率:GM 全局倍率 + 城镇增益药剂窗口倍率,真实接入掉落结算

启动器(Python/JS):
- 标题栏自绘、窗口 1204x717、禁止拖拽改大小、仅最小化/关闭
- GM 工具:疲劳与燃烧、深渊史诗掉率、拍卖行上架(单一定价对齐)
- 拍卖行上架前端对齐服务端单一价格语义,修复 400 报错
This commit is contained in:
hanfuit committed 2026-11-04 14:31:55 +08:00
commit 645b64fe21
4388 files changed
+1405457

No files matched your search

@@ -0,0 +1,131 @@
package itemquality
import (
"crypto/rand"
"encoding/binary"
"fmt"
"strconv"
"strings"
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
)
const (
// TopSeed is the current-client top-quality sentinel used by the gold
// kaleido box.
TopSeed uint32 = 999999998
// RandomSeedCount excludes the dedicated top-quality sentinel.
RandomSeedCount uint32 = TopSeed - 1
)
func NewRandomSeed() (uint32, error) {
const acceptLimit = ^uint32(0) - (^uint32(0) % RandomSeedCount)
var raw [4]byte
for {
if _, err := rand.Read(raw[:]); err != nil {
return 0, fmt.Errorf("generate equipment quality seed: %w", err)
}
value := binary.LittleEndian.Uint32(raw[:])
if value < acceptLimit {
return value%RandomSeedCount + 1, nil
}
}
}
func Valid(seed uint32) bool {
return seed > 0 && seed <= TopSeed
}
// currentEquipmentRawEntrySize is the full current 0x77 wire row length. Only
// that row carries the quality seed at raw+0x06; a shorter legacy constructor
// row (u8 slot, u32 item id at +1, u32 create scalar at +5, u8 packed reinforce
// at +9, u16 durability at +10) must never be written at [6:10], or the seed's
// high byte overwrites the packed reinforce byte at +9 and randomly rewrites
// the item's upgrade level.
const currentEquipmentRawEntrySize = 0x77
// Apply persists a quality seed in both the relational item metadata and a
// present raw item projection. It never writes the independent stack-count or
// instance-value aliases. The raw projection is written only when the row is a
// complete current 0x77 row whose [6:10] is the seed slot; legacy rows keep
// their create scalar and packed reinforce byte intact and expose the seed
// through Extra["quality_seed"] alone (the projection layer reads it from
// Extra before ever consulting a legacy row's bytes).
func Apply(stack *dnfrepo.ItemStack, seed uint32) error {
if stack == nil || !Valid(seed) {
return fmt.Errorf("invalid equipment quality seed: %d", seed)
}
if stack.Extra == nil {
stack.Extra = make(map[string]string, 4)
}
stack.Extra["quality_seed"] = strconv.FormatUint(uint64(seed), 10)
if len(stack.RawEntry) == currentEquipmentRawEntrySize {
binary.LittleEndian.PutUint32(stack.RawEntry[6:10], seed)
}
return nil
}
// equipmentItemKind is the durable item_kind that marks a row as equipment.
// dnfbridge writes it as its dungeonDropItemEquipment kind.
const equipmentItemKind = "equipment"
// equipmentSeedAliases are the Extra keys a durable equipment row can carry
// its quality seed under. quality_seed is the dedicated current key;
// amount_or_count and count_or_instance are the legacy aliases older rows use.
var equipmentSeedAliases = []string{"quality_seed", "amount_or_count", "count_or_instance"}
// StackSeed returns the quality seed (品级) one durable row carries, or 0 when
// the row is not an equipment instance that owns a seed. It is the single
// source of truth for the raw+0x06 word of a class0 item-list row:
//
// - the dedicated quality_seed Extra key wins outright, so rows imported
// before item_kind normalization keep their grade;
// - every other alias is equipment metadata only when the durable kind says
// equipment, because amount_or_count is shared with stackables;
// - a complete current 0x77 row that echoes its own item id is read at
// raw+0x06, where the current client keeps the grade seed.
//
// Both the login/select-character projection and every op13/op14 re-encoder
// must resolve that word through this function. Encoding the stack count there
// instead (an equipment row always carries Count 1) makes the client repaint
// the item with the grade of seed 1 until the next full character list load.
func StackSeed(stack dnfrepo.ItemStack) uint32 {
if seed := seedFromExtra(stack.Extra, "quality_seed"); Valid(seed) {
return seed
}
if !strings.EqualFold(strings.TrimSpace(stack.Extra["item_kind"]), equipmentItemKind) {
return 0
}
if seed := seedFromExtra(stack.Extra, equipmentSeedAliases...); Valid(seed) {
return seed
}
if stack.ItemID > 0 && len(stack.RawEntry) == currentEquipmentRawEntrySize &&
binary.LittleEndian.Uint32(stack.RawEntry[2:6]) == uint32(stack.ItemID) {
if seed := binary.LittleEndian.Uint32(stack.RawEntry[6:10]); Valid(seed) {
return seed
}
}
return 0
}
// seedFromExtra reads the first present, parseable Extra alias as the unsigned
// word the wire row carries. A present-but-zero alias still ends the scan, so
// the resolution matches the item-list projection's sceneInventoryExtraUint32
// exactly instead of silently falling through to a later alias.
func seedFromExtra(extra map[string]string, keys ...string) uint32 {
for _, key := range keys {
raw := strings.TrimSpace(extra[key])
if raw == "" {
continue
}
value, err := strconv.ParseUint(raw, 0, 64)
if err != nil {
continue
}
if value > uint64(^uint32(0)) {
value = uint64(^uint32(0))
}
return uint32(value)
}
return 0
}
@@ -0,0 +1,166 @@
package itemquality
import (
"encoding/binary"
"testing"
dnfrepo "longheng.io/server/internal/modules/dnf/repository"
)
func TestApplyWritesOnlyDedicatedQualityFields(t *testing.T) {
raw := make([]byte, currentEquipmentRawEntrySize)
binary.LittleEndian.PutUint32(raw[2:6], 1001)
raw[0x0A] = 0x12 // packed reinforce must survive a seed write
stack := dnfrepo.ItemStack{
ItemID: 1001,
Count: 1,
RawEntry: raw,
Extra: map[string]string{"count_or_instance": "77"},
}
if err := Apply(&stack, 123456); err != nil {
t.Fatalf("Apply: %v", err)
}
if stack.Extra["quality_seed"] != "123456" ||
stack.Extra["count_or_instance"] != "77" ||
binary.LittleEndian.Uint32(stack.RawEntry[6:10]) != 123456 ||
stack.RawEntry[0x0A] != 0x12 {
t.Fatalf("stack = %+v raw=%x", stack, stack.RawEntry)
}
}
// TestApplyDoesNotTouchLegacyRowPackedReinforce reproduces the reported bug:
// an ordinary grade box randomly changed the upgrade level of equipment stored
// in the legacy constructor layout. A 43-byte legacy row keeps its create
// scalar at +5:9 and its packed reinforce byte at +9. The old Apply wrote the
// whole seed dword at [6:10] for any row with len>=10, so the random seed's
// high byte (seed>>24, 0..59) landed on +9 and silently rewrote the upgrade
// level. Apply must leave legacy rows untouched below +10.
func TestApplyDoesNotTouchLegacyRowPackedReinforce(t *testing.T) {
// buildLegacyStarterRow layout: u8 slot, u32 item id at +1, u32 create
// scalar at +5, u8 packed reinforce at +9, u16 durability at +10.
raw := make([]byte, 43)
raw[0] = 15
binary.LittleEndian.PutUint32(raw[1:5], 12400)
binary.LittleEndian.PutUint32(raw[5:9], 1)
raw[9] = 0x2A // +10 reinforce, high 3 bits are the flag bits
binary.LittleEndian.PutUint16(raw[10:12], 27)
stack := dnfrepo.ItemStack{
ItemID: 12400,
Count: 1,
RawEntry: raw,
Extra: map[string]string{"durability": "27", "max_durability": "27"},
}
seed := uint32(0x12ABCDEF) // seed>>24 == 0x12 would rewrite the level
if err := Apply(&stack, seed); err != nil {
t.Fatalf("Apply: %v", err)
}
if stack.Extra["quality_seed"] != "313249263" { // 0x12ABCDEF
t.Fatalf("quality_seed = %q, want decimal 0x12ABCDEF", stack.Extra["quality_seed"])
}
if stack.RawEntry[0x09] != 0x2A {
t.Fatalf("legacy packed reinforce byte at +9 rewritten to 0x%02X, want 0x2A", stack.RawEntry[0x09])
}
if binary.LittleEndian.Uint32(stack.RawEntry[5:9]) != 1 {
t.Fatalf("legacy create scalar at +5 rewritten: %x", stack.RawEntry[5:9])
}
if binary.LittleEndian.Uint16(stack.RawEntry[10:12]) != 27 {
t.Fatalf("legacy durability at +10 rewritten: %d", binary.LittleEndian.Uint16(stack.RawEntry[10:12]))
}
}
func TestNewRandomSeedExcludesTopSentinel(t *testing.T) {
for range 32 {
seed, err := NewRandomSeed()
if err != nil {
t.Fatalf("NewRandomSeed: %v", err)
}
if seed == 0 || seed > RandomSeedCount {
t.Fatalf("seed = %d", seed)
}
}
}
// equipmentRow builds a complete current 0x77 row: item id at +2, seed at +6.
func equipmentRow(itemID uint32, seed uint32) []byte {
raw := make([]byte, currentEquipmentRawEntrySize)
binary.LittleEndian.PutUint32(raw[2:6], itemID)
binary.LittleEndian.PutUint32(raw[6:10], seed)
return raw
}
// TestStackSeedResolvesTheDurableGradeWord pins the resolution order both the
// login projection and the inventory op13/op14 re-encoders rely on. Reading the
// stack count here instead is what made an enchant repaint the whole bag with
// the lowest grade until the next select-character list rebuild.
func TestStackSeedResolvesTheDurableGradeWord(t *testing.T) {
const (
itemID = uint32(100390003)
seed = uint32(998506240)
)
tests := []struct {
name string
stack dnfrepo.ItemStack
want uint32
}{
{
name: "dedicated quality_seed wins without any item kind",
stack: dnfrepo.ItemStack{ItemID: int64(itemID), Count: 1, Extra: map[string]string{"quality_seed": "998506240"}},
want: seed,
},
{
name: "quality_seed wins over a stale legacy alias",
stack: dnfrepo.ItemStack{ItemID: int64(itemID), Count: 1, Extra: map[string]string{
"item_kind": "equipment", "quality_seed": "998506240", "amount_or_count": "1",
}},
want: seed,
},
{
name: "equipment amount alias keeps imported rows",
stack: dnfrepo.ItemStack{ItemID: int64(itemID), Count: 1, Extra: map[string]string{"item_kind": "equipment", "amount_or_count": "345678901"}},
want: 345678901,
},
{
name: "equipment row without an alias falls back to raw+0x06",
stack: dnfrepo.ItemStack{ItemID: int64(itemID), Count: 1, RawEntry: equipmentRow(itemID, seed), Extra: map[string]string{"item_kind": "equipment"}},
want: seed,
},
{
name: "a stackable count is never read as a grade",
stack: dnfrepo.ItemStack{ItemID: int64(itemID), Count: 7, RawEntry: equipmentRow(itemID, seed), Extra: map[string]string{"item_kind": "stackable", "amount_or_count": "7"}},
want: 0,
},
{
name: "a stackable with no kind stays unknown",
stack: dnfrepo.ItemStack{ItemID: int64(itemID), Count: 7, RawEntry: equipmentRow(itemID, seed)},
want: 0,
},
{
name: "a row that does not echo its own item id is not trusted",
stack: dnfrepo.ItemStack{ItemID: int64(itemID), Count: 1, RawEntry: equipmentRow(itemID+1, seed), Extra: map[string]string{"item_kind": "equipment"}},
want: 0,
},
{
name: "a legacy short row keeps its create scalar",
stack: dnfrepo.ItemStack{ItemID: int64(itemID), Count: 1, RawEntry: equipmentRow(itemID, seed)[:43], Extra: map[string]string{"item_kind": "equipment"}},
want: 0,
},
{
name: "the top sentinel is a legal grade",
stack: dnfrepo.ItemStack{ItemID: int64(itemID), Count: 1, Extra: map[string]string{"item_kind": "equipment", "quality_seed": "999999998"}},
want: TopSeed,
},
{
name: "an out of range alias is refused",
stack: dnfrepo.ItemStack{ItemID: int64(itemID), Count: 1, Extra: map[string]string{"item_kind": "equipment", "quality_seed": "999999999"}},
want: 0,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := StackSeed(test.stack); got != test.want {
t.Fatalf("StackSeed = %d, want %d", got, test.want)
}
})
}
}