From dce68c7094410859e531cadc9e002c264d2077b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=94=E5=BF=B5?= <1@72wo.cn> Date: Thu, 26 Jun 2025 19:40:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor(common):=20=E5=88=A0=E9=99=A4=E6=9C=AA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E5=92=8C=E5=BA=8F=E5=88=97=E5=8C=96=E7=9B=B8=E5=85=B3=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除了多个未使用的数据结构定义,包括 LoginSidInfo、AttackValue、CommendSvrInfo 等 - 删除了序列化工具包 serialize 目录下的所有代码 - 更新了 go.mod 和 go.sum 文件,移除了不再使用的依赖 --- common/core/info/LoginSidInfo.go | 21 - common/core/info/battle/AttackValue.go | 55 --- .../info/battle/CatchMonsterInboundInfo.go | 7 - .../info/battle/CatchMonsterOutboundInfo.go | 9 - .../info/battle/ChallengeBossInboundInfo.go | 7 - .../core/info/battle/ChangePetInboundInfo.go | 7 - common/core/info/battle/ChangePetInfo.go | 19 - .../core/info/battle/ChangePetOutboundInfo.go | 6 - common/core/info/codec.go | 9 - common/core/info/login/CommendSvrInfo.go | 20 - common/core/info/login/CommendSvrInfo_test.go | 47 --- common/core/info/login/LoginUserInfo.go | 213 ---------- common/core/info/login/ServerInfo.go | 35 -- common/core/info/login/SidInfo.go | 24 -- common/core/info/pet/GetPetInfoInboundInfo.go | 7 - common/core/info/pet/PetEffectInfo.go | 21 - common/core/info/pet/PetInfo.go | 73 ---- common/core/info/pet/PetSetExpInboundInfo.go | 9 - common/core/info/pet/PetSetExpOutboundInfo.go | 7 - common/core/info/pet/PetShowInboundInfo.go | 7 - common/core/info/pet/PetShowOutboundInfo.go | 25 -- common/core/info/pet/skill/ChangeSkillInfo.go | 18 - common/core/info/pet/skill/SkillInfo.go | 11 - common/go.mod | 1 + common/go.sum | 2 + common/serialize/Serialize.go | 380 ------------------ common/serialize/Serialize_test.go | 42 -- common/serialize/unSerialize.go | 336 ---------------- logic/controller/login.go | 14 +- logic/service/login/PlayerLoginService.go | 46 ++- logic/service/login/recv.go | 10 - 31 files changed, 52 insertions(+), 1436 deletions(-) delete mode 100644 common/core/info/LoginSidInfo.go delete mode 100644 common/core/info/battle/AttackValue.go delete mode 100644 common/core/info/battle/CatchMonsterInboundInfo.go delete mode 100644 common/core/info/battle/CatchMonsterOutboundInfo.go delete mode 100644 common/core/info/battle/ChallengeBossInboundInfo.go delete mode 100644 common/core/info/battle/ChangePetInboundInfo.go delete mode 100644 common/core/info/battle/ChangePetInfo.go delete mode 100644 common/core/info/battle/ChangePetOutboundInfo.go delete mode 100644 common/core/info/codec.go delete mode 100644 common/core/info/login/CommendSvrInfo.go delete mode 100644 common/core/info/login/CommendSvrInfo_test.go delete mode 100644 common/core/info/login/LoginUserInfo.go delete mode 100644 common/core/info/login/ServerInfo.go delete mode 100644 common/core/info/login/SidInfo.go delete mode 100644 common/core/info/pet/GetPetInfoInboundInfo.go delete mode 100644 common/core/info/pet/PetEffectInfo.go delete mode 100644 common/core/info/pet/PetInfo.go delete mode 100644 common/core/info/pet/PetSetExpInboundInfo.go delete mode 100644 common/core/info/pet/PetSetExpOutboundInfo.go delete mode 100644 common/core/info/pet/PetShowInboundInfo.go delete mode 100644 common/core/info/pet/PetShowOutboundInfo.go delete mode 100644 common/core/info/pet/skill/ChangeSkillInfo.go delete mode 100644 common/core/info/pet/skill/SkillInfo.go delete mode 100644 common/serialize/Serialize.go delete mode 100644 common/serialize/Serialize_test.go delete mode 100644 common/serialize/unSerialize.go delete mode 100644 logic/service/login/recv.go diff --git a/common/core/info/LoginSidInfo.go b/common/core/info/LoginSidInfo.go deleted file mode 100644 index a3e3127c1..000000000 --- a/common/core/info/LoginSidInfo.go +++ /dev/null @@ -1,21 +0,0 @@ -package info - -func (h InfoCodec) In_1001(data []byte) any { //EnumCommandID//todo c2s数据 - - //fmt.Printf(" 处理命令: %s, 数据长度: %d\n", len(data)) - return NewLoginSidInfo() -} -func (h InfoCodec) Out_1001(data []byte) { //EnumCommandID//TODO s2c数据 - - //fmt.Printf(" 处理命令: %s, 数据长度: %d\n", len(data)) -} - -// LoginSidInfo 登录携带的凭证结构体 -type LoginSidInfo struct { - Sid []byte `struc:"[16]byte"` // 登录会话ID,固定长度16字节 -} - -// NewLoginSidInfo 创建一个新的登录凭证实例 -func NewLoginSidInfo() LoginSidInfo { - return LoginSidInfo{} -} diff --git a/common/core/info/battle/AttackValue.go b/common/core/info/battle/AttackValue.go deleted file mode 100644 index d93b7ad7a..000000000 --- a/common/core/info/battle/AttackValue.go +++ /dev/null @@ -1,55 +0,0 @@ -package battle - -import "blazing/common/core/info/pet/skill" - -// AttackValue 战斗攻击值信息 -type AttackValue struct { - // UserId 玩家的米米号 与野怪对战userid = 0 - UserId uint64 - - // SkillId 使用技能的id - SkillId uint64 - - // AttackTime 是否击中 如果为0 则miss 如果为1 则击中 - AttackTime uint64 - - // LostHp 我方造成的伤害 - LostHp uint64 - - // GainHp 我方获得血量 - GainHp uint64 - - // RemainHp 我方剩余血量 - RemainHp uint64 - - // MaxHp 我方最大血量 - MaxHp uint64 - - // State 固定值0 需要后续测试 - State uint64 - - // SkillList 根据精灵的数据插入技能 最多4条 不定长 - SkillList []*skill.SkillInfo - - // IsCritical 是否暴击 - IsCritical uint64 - - // Status 20个字节 各种状态 - // 0:"麻痹" - // 1:"中毒" - // 2:"烧伤" - // 4:"寄生" - // 5:"冻伤" - // 6:"害怕" - // 7:"疲惫" - // 8:"睡眠" - // 9:"石化" - // 10:"混乱" - // 15:"冰封" - // 16:"流血" - Status [20]byte - - // BattleLv 6个单字节byte, 内容为buff等级 攻击 速度 特攻 防御 特防命中等 - // 具体数值为1-6等级 - BattleLv [6]byte -} diff --git a/common/core/info/battle/CatchMonsterInboundInfo.go b/common/core/info/battle/CatchMonsterInboundInfo.go deleted file mode 100644 index f4caa6b13..000000000 --- a/common/core/info/battle/CatchMonsterInboundInfo.go +++ /dev/null @@ -1,7 +0,0 @@ -package battle - -// CatchMonsterInboundInfo 捕捉怪物入站信息 -type CatchMonsterInboundInfo struct { - // 胶囊id - CapsuleID uint64 `json:"capsuleId"` -} diff --git a/common/core/info/battle/CatchMonsterOutboundInfo.go b/common/core/info/battle/CatchMonsterOutboundInfo.go deleted file mode 100644 index 658c0c94e..000000000 --- a/common/core/info/battle/CatchMonsterOutboundInfo.go +++ /dev/null @@ -1,9 +0,0 @@ -package battle - -// CatchMonsterOutboundInfo 捕捉怪物出站信息 -type CatchMonsterOutboundInfo struct { - // 捕捉时间 - CatchTime uint64 `json:"catchTime"` - // 宠物编号 - PetID uint64 `json:"petId"` -} diff --git a/common/core/info/battle/ChallengeBossInboundInfo.go b/common/core/info/battle/ChallengeBossInboundInfo.go deleted file mode 100644 index b4cd819f2..000000000 --- a/common/core/info/battle/ChallengeBossInboundInfo.go +++ /dev/null @@ -1,7 +0,0 @@ -package battle - -// ChallengeBossInboundInfo boss挑战入站信息 -type ChallengeBossInboundInfo struct { - // boss的序号 从0开始 - BossID uint64 `json:"bossId"` -} diff --git a/common/core/info/battle/ChangePetInboundInfo.go b/common/core/info/battle/ChangePetInboundInfo.go deleted file mode 100644 index 1d059e476..000000000 --- a/common/core/info/battle/ChangePetInboundInfo.go +++ /dev/null @@ -1,7 +0,0 @@ -package battle - -// ChangePetInboundInfo 宠物变更入站信息 -type ChangePetInboundInfo struct { - // 捕捉时间 - CatchTime uint64 `json:"catchTime"` -} diff --git a/common/core/info/battle/ChangePetInfo.go b/common/core/info/battle/ChangePetInfo.go deleted file mode 100644 index bb6465014..000000000 --- a/common/core/info/battle/ChangePetInfo.go +++ /dev/null @@ -1,19 +0,0 @@ -package battle - -// ChangePetInfo 宠物变更信息 -type ChangePetInfo struct { - // 米米号 野怪0 - UserID uint64 `json:"userId"` - // 切换上场的精灵编号 - PetID uint64 `json:"petId"` - // 精灵名字 16字节 - PetName string `json:"petName"` - // 切换上场的等级 - Level uint64 `json:"level"` - // 切换上场的生命 - HP uint64 `json:"hp"` - // 切换上场的最大生命 - MaxHP uint64 `json:"maxHp"` - // 切换上场的捕捉时间 - CatchTime uint64 `json:"catchTime"` -} diff --git a/common/core/info/battle/ChangePetOutboundInfo.go b/common/core/info/battle/ChangePetOutboundInfo.go deleted file mode 100644 index c5b137e71..000000000 --- a/common/core/info/battle/ChangePetOutboundInfo.go +++ /dev/null @@ -1,6 +0,0 @@ -package battle - -// ChangePetOutboundInfo 宠物变更出站信息 -type ChangePetOutboundInfo struct { - Info *ChangePetInfo `json:"info"` -} diff --git a/common/core/info/codec.go b/common/core/info/codec.go deleted file mode 100644 index 39c7fbdf2..000000000 --- a/common/core/info/codec.go +++ /dev/null @@ -1,9 +0,0 @@ -package info - -//实现自动InfoCodec -type InfoCodec struct { -} - -func NewInfoCodec() *InfoCodec { - return &InfoCodec{} -} diff --git a/common/core/info/login/CommendSvrInfo.go b/common/core/info/login/CommendSvrInfo.go deleted file mode 100644 index f4c95c186..000000000 --- a/common/core/info/login/CommendSvrInfo.go +++ /dev/null @@ -1,20 +0,0 @@ -package login - -// CommendSvrInfo 初始连接请求信息结构体 -type CommendSvrInfo struct { - MaxOnlineID uint32 // 最大连接数 - IsVip uint32 // 建议为0 - ServerInfoLen uint32 `struc:"sizeof=ServerList"` // 服务器信息长度 ServerInfo - ServerList []ServerInfo // 服务器具体信息 - Reversed uint32 // 保留字段 -} - -func NewCommendSvrInfo() *CommendSvrInfo { - return &CommendSvrInfo{ - MaxOnlineID: 100, - IsVip: 0, - ServerInfoLen: 0, - ServerList: make([]ServerInfo, 0), - Reversed: 0, - } -} diff --git a/common/core/info/login/CommendSvrInfo_test.go b/common/core/info/login/CommendSvrInfo_test.go deleted file mode 100644 index 34dfcce37..000000000 --- a/common/core/info/login/CommendSvrInfo_test.go +++ /dev/null @@ -1,47 +0,0 @@ -package login - -import ( - "bytes" - "encoding/hex" - "fmt" - "testing" - - "github.com/gogf/gf/v2/frame/g" - "github.com/lunixbochs/struc" -) - -func TestNewCommendSvrInfo(t *testing.T) { - - ret := NewCommendSvrInfo() - - lofin := NewServerInfo() - lofin.OnlineID = 1 - lofin.UserCnt = 77 - lofin.IP = "127.0.0.1" - lofin.Port = 27777 - lofin.Friends = 1 - - ret.ServerList = append(ret.ServerList, *lofin) - - // bytt := bytearray.CreateByteArray() - // bytt.WriteUInt32(100) - // bytt.WriteUInt32(0) - - // //ret.ServerList = append(ret.ServerList, *lofin) - var trr bytes.Buffer - // err := struc.Pack(&trr, lofin) - // bytt.WriteUInt32(1) //长度 - // bytt.Write(trr.Bytes()) - // bytt.WriteUInt32(1) - // //err:=Println(trr.Bytes()) - /// 00 00 00 64 00 00 00 00 00 00 00 01 00 00 00 01 00 00 00 4d 31 32 37 2e 30 2e 30 2e 31 00 00 00 00 00 00 00 6c 81 00 00 00 01 00 00 00 00 - - err := struc.Pack(&trr, ret) - fmt.Println(err) - fmt.Println(trr.Bytes()) - g.Dump(ret) - fmt.Println("000000640000000000000001000000010000004d3132372e302e302e31000000000000006c810000000100000000") - fmt.Println("000000640000000000000001000000010000004d3132372e302e302e31000000000000006c810000000100000000") - fmt.Println(hex.EncodeToString(trr.Bytes())) - -} diff --git a/common/core/info/login/LoginUserInfo.go b/common/core/info/login/LoginUserInfo.go deleted file mode 100644 index 2fc152216..000000000 --- a/common/core/info/login/LoginUserInfo.go +++ /dev/null @@ -1,213 +0,0 @@ -package login - -import "blazing/common/core/info/pet" - -// Point 表示坐标结构 -type Point struct { - X uint32 - Y uint32 -} - -// TeamInfo 战队信息 -type TeamInfo struct { - // 此处应包含TeamInfo的具体字段,原Java代码中未给出详细定义 - // 请根据实际需求补充 -} - -// TeamPKInfo 战队PK信息 -type TeamPKInfo struct { - // 此处应包含TeamPKInfo的具体字段,原Java代码中未给出详细定义 - // 请根据实际需求补充 -} - -// PeopleItemInfo 人物物品信息 -type PeopleItemInfo struct { - // 此处应包含PeopleItemInfo的具体字段,原Java代码中未给出详细定义 - // 请根据实际需求补充 -} - -// LoginUserInfo 登录用户信息结构体 -type LoginUserInfo struct { - // 米米号 通过sid拿到 - UserId uint32 - // 注册时间(按秒的时间戳) - RegisterTime uint32 - // 16字节昵称 - Nick [16]byte `array_serialize:"fixed_length,16"` - // 暂时不明建议先给固定值0 - Vip uint16 `ushort:"true"` - // 暂时不明建议先给固定值15 - Viped uint16 `ushort:"true"` - // 暂时不明建议先给固定值0 - DsFlag uint32 - // 机器人人物颜色 00 rgb - Color uint32 - // 暂时不明建议先给固定值0 - Texture uint32 - // 暂时不明建议先给固定值3000 - Energy uint32 `default:"3000"` - // 赛尔豆 - Coins uint32 - // 暂时不明建议先给固定值0 - FightBadge uint32 - // 上线的地图id - MapID uint32 - // 上线的坐标 2个uint - Pos Point `array_serialize:"fixed_length,8"` - // 已经消耗掉的时间(秒为单位) - TimeToday uint32 - // 总电池限制(秒为单位) - TimeLimit uint32 - // 暂时不明感觉是某种活动建议先给固定值0(只能0或1) - IsClothHalfDay byte - // 暂时不明感觉是某种活动建议先给固定值0(只能0或1) - IsRoomHalfDay byte - // 暂时不明感觉是某种活动建议先给固定值0(只能0或1) - IFortressHalfDay byte - // 暂时不明感觉是某种活动建议先给固定值0(只能0或1) - IsHQHalfDay byte - // 暂时不明建议先给固定值0 - LoginCount uint32 - // 邀请活动建议先给固定值0 - Inviter uint32 - // 邀请活动建议先给固定值0 - NewInviteeCount uint32 - // 超no等级建议固定8 - VipLevel uint32 `default:"8"` - // 超no的vip值建议固定80000 - VipValue uint32 `default:"80000"` - // 超no的外形等级建议固定1(暂定) - VipStage uint32 `default:"1"` - // nono是否自动充电 建议固定1 - AutoCharge uint32 `default:"1"` - // 超no的结束时间建议尽可能大 - VipEndTime uint32 `default:"4294967295"` - // 邀请活动建议先给固定值0 - FreshManBonus uint32 - // 超no芯片列表*(80字节) - NonoChipList [80]byte `array_serialize:"fixed_length,80"` - // 50字节,默认值为3 - DailyResArr [50]byte `array_serialize:"fixed_length,50"` - // 教官id - TeacherID uint32 - // 学员id - StudentID uint32 - // 毕业人数 - GraduationCount uint32 - // 默认值为0 - MaxPuniLv uint32 `default:"0"` - // 精灵的最高等级 - PetMaxLevel uint32 - // 所有的精灵的数量 - AllPetNumber uint32 - // 精灵王之战胜场 - MonKingWin uint32 - // 勇者之塔当前到达的层数 - CurrentStage uint32 - // 试炼之塔最大胜利的层数 - MaxStage uint32 - // 试炼之塔当前到达的层数 - CurrentFreshStage uint32 - // 试炼之塔最大胜利的层数 - MaxFreshStage uint32 - // 星际擂台连胜 - MaxArenaWins uint32 - // 未知默认0 - TwoTimes uint32 `default:"0"` - // 未知默认0 - ThreeTimes uint32 `default:"0"` - // 是否自动战斗(未知默认值0) - AutoFight uint32 `default:"0"` - // 自动战斗剩余的场次(未知默认值0) - AutoFightTime uint32 `default:"0"` - // 能量吸收仪剩余次数(未知待定默认值0) - EnergyTime uint32 `default:"0"` - // 学习力吸收仪剩余次数(未知待定默认值0) - LearnTimes uint32 `default:"0"` - // 未知默认0 - MonBattleMedal uint32 `default:"0"` - // 未知默认0 - RecordCount uint32 `default:"0"` - // 未知默认0 - ObtainTm uint32 `default:"0"` - // 当前在孵化的元神珠id - SoulBeadItemID uint32 - // 未知默认0 - ExpireTm uint32 `default:"0"` - // 未知默认0 - FuseTimes uint32 `default:"0"` - // 玩家有没有nono - HasNono uint32 `default:"1"` - // 玩家有没有超能nono - SuperNono uint32 `default:"1"` - // 默认值-1 - NonoState uint32 `default:"4294967295"` - // nono的颜色 - NonoColor uint32 - // nono的名字 必须要补齐到16位 - NonoNick [16]byte `array_serialize:"fixed_length,16"` - // 猜测为战队信息24字节 - TeamInfo TeamInfo `array_serialize:"fixed_length,24"` - // 8字节 - TeamPkInfo TeamPKInfo `array_serialize:"fixed_length,8"` - // 1字节 无内容 - Reserved byte - // 默认值为0 - Badge uint32 `default:"0"` - // 未知(27字节,默认值为3) - Reserved1 [27]byte `array_serialize:"fixed_length,27"` - // 任务状态数组(500字节,3为已经完成,建议默认值为3) - TaskList [500]byte `array_serialize:"fixed_length,500"` - // 精灵背包内的信息由于特性精灵的存在精灵背包不定长 如果有特性占199字节 如果没特性 一个精灵占175字节 - PetList []pet.PetInfo - // 穿戴装备 8字节 - Clothes []PeopleItemInfo -} - -// NewLoginUserInfo 创建新的登录用户信息实例,设置默认值 -func NewLoginUserInfo() *LoginUserInfo { - info := &LoginUserInfo{ - Vip: 0, - Viped: 15, - Energy: 3000, - VipLevel: 8, - VipValue: 80000, - VipStage: 1, - AutoCharge: 1, - VipEndTime: uint32(^uint32(0)), - TwoTimes: 0, - ThreeTimes: 0, - AutoFight: 0, - AutoFightTime: 0, - EnergyTime: 0, - LearnTimes: 0, - MonBattleMedal: 0, - RecordCount: 0, - ObtainTm: 0, - ExpireTm: 0, - FuseTimes: 0, - HasNono: 1, - SuperNono: 1, - NonoState: uint32(^uint32(0)), - Badge: 0, - } - - // 初始化固定长度数组 - for i := range info.NonoChipList { - info.NonoChipList[i] = 0 - } - for i := range info.DailyResArr { - info.DailyResArr[i] = 3 - } - for i := range info.Reserved1 { - info.Reserved1[i] = 3 - } - for i := range info.TaskList { - info.TaskList[i] = 3 - } - - // 初始化nono昵称 - copy(info.NonoNick[:], "nono") - - return info -} diff --git a/common/core/info/login/ServerInfo.go b/common/core/info/login/ServerInfo.go deleted file mode 100644 index c1ac4dba4..000000000 --- a/common/core/info/login/ServerInfo.go +++ /dev/null @@ -1,35 +0,0 @@ -package login - -// ServerInfo 服务器信息结构体 -type ServerInfo struct { - // 连接ID, 即服务器序号 - OnlineID uint32 - // 当前服务器玩家在线数量, 供SWF显示 - UserCnt uint32 - // 服务器IP, 16字节UTF-8, 不足16补齐到16 - IP string `struc:"[16]byte"` // 定长模式:16字节 - // 端口 - Port uint16 - // 好友在线的个数 - Friends uint32 -} - -// NewServerInfo 创建新的服务器信息实例 -func NewServerInfo() *ServerInfo { - return &ServerInfo{} -} - -// // SetIP 设置IP地址并自动填充到16字节 -// func (s *ServerInfo) SetIP(ip string) { -// copy(s.IP[:], ip) -// if len(ip) < 16 { -// for i := len(ip); i < 16; i++ { -// s.IP[i] = 0 // 用0填充剩余字节 -// } -// } -// } - -// // GetIP 获取IP地址(去除填充的0) -// func (s *ServerInfo) GetIP() string { -// return strings.TrimRight(string(s.IP[:]), "\x00") -// } diff --git a/common/core/info/login/SidInfo.go b/common/core/info/login/SidInfo.go deleted file mode 100644 index deedd6f32..000000000 --- a/common/core/info/login/SidInfo.go +++ /dev/null @@ -1,24 +0,0 @@ -package login - -// SidInfo SID信息结构体 -type SidInfo struct { - Sid []byte // SID数据 -} - -// NewSidInfo 创建一个新的SID信息实例 -func NewSidInfo() *SidInfo { - return &SidInfo{ - Sid: make([]byte, 0), - } -} - -// SetSid 设置SID值 -func (s *SidInfo) SetSid(sid []byte) { - s.Sid = make([]byte, len(sid)) - copy(s.Sid, sid) -} - -// GetSid 获取SID值 -func (s *SidInfo) GetSid() []byte { - return s.Sid -} diff --git a/common/core/info/pet/GetPetInfoInboundInfo.go b/common/core/info/pet/GetPetInfoInboundInfo.go deleted file mode 100644 index 73c685668..000000000 --- a/common/core/info/pet/GetPetInfoInboundInfo.go +++ /dev/null @@ -1,7 +0,0 @@ -package pet - -// GetPetInfoInboundInfo 获取宠物信息入站请求 -type GetPetInfoInboundInfo struct { - // 精灵生成时间 - CatchTime uint64 `json:"catchTime"` -} diff --git a/common/core/info/pet/PetEffectInfo.go b/common/core/info/pet/PetEffectInfo.go deleted file mode 100644 index 494e5d25d..000000000 --- a/common/core/info/pet/PetEffectInfo.go +++ /dev/null @@ -1,21 +0,0 @@ -package pet - -// PetEffectInfo 宠物特性信息 -type PetEffectInfo struct { - // ItemId 特性晶片对应的物品id - ItemId uint64 - // Status 默认为1 - Status byte - // LeftCount 未知默认为0 - LeftCount byte - // EffectId 特性id - EffectId uint16 - // Reserve1 保留字段1 - Reserve1 byte - // Reserve2 保留字段2无作用 - Reserve2 byte - // Reserve3 保留字段3 - Reserve3 byte - // Reserve4 保留字段4(13字节) - Reserve4 [13]byte -} diff --git a/common/core/info/pet/PetInfo.go b/common/core/info/pet/PetInfo.go deleted file mode 100644 index f080bc10c..000000000 --- a/common/core/info/pet/PetInfo.go +++ /dev/null @@ -1,73 +0,0 @@ -package pet - -import ( - "blazing/common/core/info/pet/skill" -) - -// PetInfo 宠物基本信息 -type PetInfo struct { - // Id 精灵编号 - Id uint64 - // Name 名字,长度为16字节 - Name [16]byte - // Dv 个体值 - Dv uint64 - // Nature 性格 - Nature uint64 - // Level 等级 - Level uint64 - // Exp 当前等级已获得的经验 - Exp uint64 - // LvExp 当前等级所需的经验 - LvExp uint64 - // NextLvExp 升到下一级的经验 - NextLvExp uint64 - // Hp 当前生命 - Hp uint64 - // MaxHp 最大生命 - MaxHp uint64 - // Attack 攻击 - Attack uint64 - // Defence 防御 - Defence uint64 - // SpecialAttack 特攻 - SpecialAttack uint64 - // SpecialDefence 特防 - SpecialDefence uint64 - // Speed 速度 - Speed uint64 - // EvHp 生命学习力 - EvHp uint64 - // EvAttack 攻击学习力 - EvAttack uint64 - // EvDefence 防御学习力 - EvDefence uint64 - // EvSpecialAttack 特攻学习力 - EvSpecialAttack uint64 - // EvSpecialDefense 特防学习力 - EvSpecialDefense uint64 - // EvSpeed 速度学习力 - EvSpeed uint64 - // SkillSize 技能个数 - SkillSize uint64 - // SkillList 技能信息列表 - SkillList []skill.SkillInfo - // CatchTime 捕捉时间 - CatchTime uint64 - // CatchMap 捕捉地图 - CatchMap uint64 - // CatchRect 未知默认为0 - CatchRect uint64 - // CatchLevel 捕获等级默认为0 - CatchLevel uint64 - // EffectInfo 特性列表 - EffectInfo []PetEffectInfo - // SkinID 皮肤id默认为0 - SkinID uint64 - // Shiny 是否为闪 - Shiny uint64 -} - -func NewPetInfo() *PetInfo { - return &PetInfo{} -} diff --git a/common/core/info/pet/PetSetExpInboundInfo.go b/common/core/info/pet/PetSetExpInboundInfo.go deleted file mode 100644 index 66606e78b..000000000 --- a/common/core/info/pet/PetSetExpInboundInfo.go +++ /dev/null @@ -1,9 +0,0 @@ -package pet - -// PetSetExpInboundInfo 宠物设置经验输入信息 -type PetSetExpInboundInfo struct { - // CatchTime 精灵获取时间 - CatchTime uint64 `json:"catchTime"` - // Exp 分配经验 - Exp uint64 `json:"exp"` -} diff --git a/common/core/info/pet/PetSetExpOutboundInfo.go b/common/core/info/pet/PetSetExpOutboundInfo.go deleted file mode 100644 index 1013eb538..000000000 --- a/common/core/info/pet/PetSetExpOutboundInfo.go +++ /dev/null @@ -1,7 +0,0 @@ -package pet - -// PetSetExpOutboundInfo 宠物设置经验输出信息 -type PetSetExpOutboundInfo struct { - // Exp 剩余累计经验 - Exp uint64 `json:"exp"` -} diff --git a/common/core/info/pet/PetShowInboundInfo.go b/common/core/info/pet/PetShowInboundInfo.go deleted file mode 100644 index e5245d3aa..000000000 --- a/common/core/info/pet/PetShowInboundInfo.go +++ /dev/null @@ -1,7 +0,0 @@ -package pet - -// PetShowInboundInfo 宠物展示输入信息 -type PetShowInboundInfo struct { - CatchTime uint64 `json:"catchTime"` // 捕捉时间 - Flag uint64 `json:"flag"` // 标志位 -} diff --git a/common/core/info/pet/PetShowOutboundInfo.go b/common/core/info/pet/PetShowOutboundInfo.go deleted file mode 100644 index 87b8d0815..000000000 --- a/common/core/info/pet/PetShowOutboundInfo.go +++ /dev/null @@ -1,25 +0,0 @@ -package pet - -// PetShowOutboundInfo 宠物展示输出信息 -type PetShowOutboundInfo struct { - // UserId 米米号 - UserId uint64 `json:"userId"` - // CatchTime 精灵获得的时间 - CatchTime uint64 `json:"catchTime"` - // PetId 精灵编号 - PetId uint64 `json:"petId"` - // Flag 1为显示 0为收回 - Flag uint64 `json:"flag"` - // Dv 个体 - Dv uint64 `json:"dv"` - // Shiny 闪 - Shiny uint64 `json:"shiny"` - // SkinID 皮肤id - SkinID uint64 `json:"skinID"` - // Reserved 填充字段 - Reserved uint64 `json:"reserved"` - // Reserved1 填充字段 - Reserved1 uint64 `json:"reserved1"` - // Reserved2 填充字段 - Reserved2 uint64 `json:"reserved2"` -} diff --git a/common/core/info/pet/skill/ChangeSkillInfo.go b/common/core/info/pet/skill/ChangeSkillInfo.go deleted file mode 100644 index 1d39c476b..000000000 --- a/common/core/info/pet/skill/ChangeSkillInfo.go +++ /dev/null @@ -1,18 +0,0 @@ -package skill - -// ChangeSkillInfo 精灵技能变更信息结构体 -type ChangeSkillInfo struct { - CatchTime uint32 // 精灵生成时间 - Reserved uint32 // 填充字段,默认为1 - Reserved1 uint32 // 填充字段,默认为1 - HasSkill uint32 // 拥有的技能id - ReplaceSkill uint32 // 替换技能的id -} - -// NewChangeSkillInfo 创建一个新的技能变更信息实例 -func NewChangeSkillInfo() *ChangeSkillInfo { - return &ChangeSkillInfo{ - Reserved: 1, - Reserved1: 1, - } -} diff --git a/common/core/info/pet/skill/SkillInfo.go b/common/core/info/pet/skill/SkillInfo.go deleted file mode 100644 index d6dc070ea..000000000 --- a/common/core/info/pet/skill/SkillInfo.go +++ /dev/null @@ -1,11 +0,0 @@ -package skill - -// SkillInfo 技能基础信息 -type SkillInfo struct { - // SkillId 技能ID - SkillId uint32 `json:"skillId"` - // Level 技能等级 - Level uint32 `json:"level"` - // Pp 技能PP值 - Pp uint32 `json:"pp"` -} diff --git a/common/go.mod b/common/go.mod index 8c152fd97..c8c3d3cfe 100644 --- a/common/go.mod +++ b/common/go.mod @@ -26,6 +26,7 @@ require ( github.com/google/uuid v1.6.0 github.com/gorilla/websocket v1.5.0 // indirect github.com/grokify/html-strip-tags-go v0.0.1 // indirect + github.com/lunixbochs/struc v0.0.0-20241101090106-8d528fa2c543 github.com/magiconair/properties v1.8.6 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/common/go.sum b/common/go.sum index 1676a1b3f..d4cf6f350 100644 --- a/common/go.sum +++ b/common/go.sum @@ -40,6 +40,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/lunixbochs/struc v0.0.0-20241101090106-8d528fa2c543 h1:GxMuVb9tJajC1QpbQwYNY1ZAo1EIE8I+UclBjOfjz/M= +github.com/lunixbochs/struc v0.0.0-20241101090106-8d528fa2c543/go.mod h1:vy1vK6wD6j7xX6O6hXe621WabdtNkou2h7uRtTfRMyg= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= diff --git a/common/serialize/Serialize.go b/common/serialize/Serialize.go deleted file mode 100644 index e3e8699ec..000000000 --- a/common/serialize/Serialize.go +++ /dev/null @@ -1,380 +0,0 @@ -package serialize - -import ( - "bytes" - "encoding/binary" - "errors" - "fmt" - "reflect" - "strconv" - "strings" - - "github.com/gogf/gf/v2/util/gconv" - "github.com/tnnmigga/enum" -) - -// DefaultPacketSerializer 默认序列化实现,使用大端序写入数据 -func DefaultPacketSerializer[T any]() PacketSerializer[T] { - return func(data T) ([]byte, error) { - var buf bytes.Buffer - if err := binary.Write(&buf, binary.BigEndian, data); err != nil { - return nil, errors.New("binary.Write failed: " + err.Error()) - } - // 使用大端序写入数据 - // 1. 使用reflect获取结构体类型 - // typ := reflect.TypeOf(data) - // fmt.Println("结构体类型名称:", typ.Name()) - // fmt.Println("字段数量:", typ.NumField()) - - // for i := 0; i < typ.NumField(); i++ { - // field := typ.Field(i) - // fmt.Printf("字段名: %s, 类型: %s", - // field.Name, field.Type) - // fmt.Println("字段值:", reflect.ValueOf(data).Field(i).Interface()) - - // // writedata := reflect.ValueOf(data).Field(i).Interface() - // fmt.Println(field.Type.Kind()) - - // // serializebase[T](field, &buf, writedata) - - // } - - return buf.Bytes(), nil - } -} - -// DefaultPacketDeserializer 默认反序列化实现,使用大端序读取数据 -func DefaultPacketDeserializer[T any]() PacketDeserializer[T] { - return func(data []byte) (T, error) { - var result T - reader := bytes.NewReader(data) - // 使用大端序读取数据 - if err := binary.Read(reader, binary.BigEndian, &result); err != nil { - var zero T - return zero, err - } - return result, nil - } -} - -// NewDefaultPacketHandler 创建默认的数据包处理句柄 -func NewDefaultPacketHandler[T any]() *PacketHandler[T] { - return &PacketHandler[T]{ - Serialize: DefaultPacketSerializer[T](), - Deserialize: DefaultPacketDeserializer[T](), - } -} - -type SerializeMode int - -var lengthtype = enum.New[struct { - LENGTH_FIRST SerializeMode - FIXED_LENGTH SerializeMode -}]() - -// PacketSerializer 定义序列化函数类型,将数据转换为字节切片 -type PacketSerializer[T any] func(data T) ([]byte, error) - -// PacketDeserializer 定义反序列化函数类型,将字节切片转换为数据 -type PacketDeserializer[T any] func(data []byte) (T, error) - -// PacketHandler 封装序列化和反序列化处理函数 -type PacketHandler[T any] struct { - Serialize PacketSerializer[T] // 序列化函数 - Deserialize PacketDeserializer[T] // 反序列化函数 -} - -// 定长序列化器 -type FixedVarSerializer struct { - byteOrder binary.ByteOrder -} - -// 新建序列化器 -func NewFixedVarSerializer(order binary.ByteOrder) *FixedVarSerializer { - if order == nil { - order = binary.BigEndian - } - return &FixedVarSerializer{byteOrder: order} -} - -// 序列化任意结构体 -func (s *FixedVarSerializer) Serialize(obj interface{}) ([]byte, error) { - var buf bytes.Buffer - err := s.serializeValue(reflect.ValueOf(obj), &buf) - if err != nil { - return nil, fmt.Errorf("序列化失败: %w", err) - } - return buf.Bytes(), nil -} - -// 递归序列化值 -func (s *FixedVarSerializer) serializeValue(val reflect.Value, buf *bytes.Buffer) error { - kind := val.Kind() - - switch kind { - case reflect.Struct: - return s.serializeStruct(val, buf) - case reflect.Slice, reflect.Array: - return s.serializeSlice(val, buf) - default: - if err := binary.Write(buf, s.byteOrder, val.Interface()); err != nil { - return nil - } - return nil - } -} - -// 序列化结构体 -func (s *FixedVarSerializer) serializeStruct(val reflect.Value, buf *bytes.Buffer) error { - typ := val.Type() - for i := 0; i < typ.NumField(); i++ { - field := typ.Field(i) - fieldVal := val.Field(i) - - if !field.IsExported() { - continue - } - - // 解析标签(格式:mode,params 如 fixed:16 或 varlen) - tag := field.Tag.Get("serialize") - if tag == "" { - // 未指定标签:按原生类型序列化 - err := s.serializeValue(fieldVal, buf) - if err != nil { - return err - } - continue - } - - // 解析模式和参数 - mode, params, err := parseTag(tag) - if err != nil { - return err - } - - // 根据模式序列化 - switch mode { - case lengthtype.FIXED_LENGTH: - err = s.serializeFixedField(fieldVal, params, buf) - case lengthtype.LENGTH_FIRST: - err = s.serializeVarWithLenField(fieldVal, params, buf) - default: - return fmt.Errorf("无效的序列化模式: %s", tag) - } - if err != nil { - return err - } - } - return nil -} - -// 解析标签(格式:fixed:N 或 varlen 或 varlen:N) -func parseTag(tag string) (SerializeMode, string, error) { - parts := splitTag(tag) - if len(parts) == 0 { - return 0, "", fmt.Errorf("空标签") - } - - modeStr := parts[0] - params := "" - if len(parts) > 1 { - params = parts[1] - } - - switch modeStr { - case "fixed": - if params == "" { - return 0, "", fmt.Errorf("fixed模式需指定长度") - } - return lengthtype.FIXED_LENGTH, params, nil - case "varlen": - // varlen模式:可指定长度字段字节数(如varlen:2表示用2字节存长度) - if params == "" { - params = "4" // 默认用4字节存长度 - } - return lengthtype.LENGTH_FIRST, params, nil - default: - return 0, "", fmt.Errorf("未知模式: %s", modeStr) - } -} - -// 定长模式序列化字段 -func (s *FixedVarSerializer) serializeFixedField(val reflect.Value, lengthStr string, buf *bytes.Buffer) error { - length, err := strconv.Atoi(lengthStr) - if err != nil { - return fmt.Errorf("无效的定长参数: %s", lengthStr) - } - return s.serializeFixedLengthValue(val, length, buf) -} - -// 不定长+长度模式序列化字段 -func (s *FixedVarSerializer) serializeVarWithLenField(val reflect.Value, lenBytesStr string, buf *bytes.Buffer) error { - lenBytes, err := strconv.Atoi(lenBytesStr) - if err != nil { - return fmt.Errorf("无效的长度字节数: %s", lenBytesStr) - } - if lenBytes < 1 || lenBytes > 8 { - return fmt.Errorf("长度字节数需在1-8之间") - } - - // 先序列化内容到临时缓冲区 - var contentBuf bytes.Buffer - err = s.serializeValue(val, &contentBuf) - if err != nil { - return err - } - content := contentBuf.Bytes() - - // 写入长度(用指定字节数) - - lenBuf := make([]byte, lenBytes) - switch lenBytesStr { - case "4": - s.byteOrder.PutUint32(lenBuf, uint32(len(content))) - case "2": - s.byteOrder.PutUint16(lenBuf, uint16(len(content))) - } - //s.byteOrder.PutUint32(lenBuf, uint32(len(content))) - buf.Write(lenBuf[:lenBytes]) - - // 写入内容 - buf.Write(content) - return nil -} - -// 序列化定长值 -func (s *FixedVarSerializer) serializeFixedLengthValue(val reflect.Value, length int, buf *bytes.Buffer) error { - kind := val.Kind() - - if kind == reflect.String { - str := val.String() - if len(str) > length { - str = str[:length] - } - buf.WriteString(str) - buf.Write(make([]byte, length-len(str))) - return nil - } - - var tmpBuf bytes.Buffer - fmt.Println(val) - err := s.serializeValue(val, &tmpBuf) - if err != nil { - return err - } - tmpData := tmpBuf.Bytes() - - if len(tmpData) > length { - buf.Write(tmpData[:length]) - } else { - buf.Write(tmpData) - buf.Write(make([]byte, length-len(tmpData))) - } - return nil -} - -// 序列化slice -func (s *FixedVarSerializer) serializeSlice(val reflect.Value, buf *bytes.Buffer) error { - tempslice := gconv.SliceAny(val) - fmt.Println(val) - //binary.Write(buf, s.byteOrder, val.Bytes()) - for i := 0; i < len(tempslice); i++ { - err := s.serializeValue(val.Index(i), buf) - if err != nil { - return err - } - } - // for i := 0; i < len(tempslice); i++ { - // kind := val.Type().Elem().Field(i) - - // tag := kind.Tag.Get("serialize") - - // // 检查元素是否有标签 - // if tag != "" { - // // 元素有标签:按定长模式处理 - // mode, params, err := parseTag(tag) - // if err != nil { - // return err - // } - - // if mode == lengthtype.FIXED_LENGTH { - // // 定长slice:解析maxItems和itemLen - // parts := splitTag(params) - // if len(parts) != 2 { - // return fmt.Errorf("定长slice标签需格式为 fixed:N,M") - // } - // maxItems, _ := strconv.Atoi(parts[0]) - // itemLen, _ := strconv.Atoi(parts[1]) - // return s.serializeFixedSlice(val, maxItems, itemLen, buf) - // } - // } - - // 无标签或不定长模式:按原生slice处理(先写长度,再写元素) - - // count := val.Len() - // binary.Write(buf, s.byteOrder, int32(len(tempslice))) - // for i := 0; i < count; i++ { - // err := s.serializeValue(val.Index(i), buf) - // if err != nil { - // return err - // } - // } - return nil - //} - return nil -} - -// 序列化定长slice -func (s *FixedVarSerializer) serializeFixedSlice(val reflect.Value, maxItems, itemLen int, buf *bytes.Buffer) error { - count := val.Len() - if count > maxItems { - count = maxItems - } - - buf.WriteByte(byte(count)) - - for i := 0; i < maxItems; i++ { - if i < count { - item := val.Index(i) - tmpBuf := &bytes.Buffer{} - err := s.serializeValue(item, tmpBuf) - if err != nil { - return err - } - tmpData := tmpBuf.Bytes() - - if len(tmpData) > itemLen { - buf.Write(tmpData[:itemLen]) - } else { - buf.Write(tmpData) - buf.Write(make([]byte, itemLen-len(tmpData))) - } - } else { - buf.Write(make([]byte, itemLen)) - } - } - return nil -} - -// 辅助函数:分割标签参数 -func splitTag(tag string) []string { - if tag == "" { - return nil - } - return strings.Split(tag, ":") -} - -// 示例结构体(混合模式) -type Address struct { - City string `serialize:"fixed:20"` // 定长模式:20字节 - Country string `serialize:"varlen"` // 不定长模式:先写4字节长度,再写内容 -} - -type Person struct { - Name string `serialize:"fixed:16"` // 定长姓名 - Age uint8 // 原生类型 - Address Address // 嵌套结构体 - Hobbies []string `serialize:"fixed:5,20"` // 定长slice:5个元素,每个20字节 - Friends []Person `serialize:"varlen"` // 不定长slice:先写长度,再写内容 - Metadata []float64 `serialize:"varlen:2"` // 不定长slice:用2字节存长度 -} diff --git a/common/serialize/Serialize_test.go b/common/serialize/Serialize_test.go deleted file mode 100644 index 90f0b1da6..000000000 --- a/common/serialize/Serialize_test.go +++ /dev/null @@ -1,42 +0,0 @@ -package serialize - -import ( - "blazing/common/core/info/login" - "blazing/common/core/info/pet" - "fmt" - "testing" -) - -func TestDefaultPacketSerializer(t *testing.T) { - serializer := NewFixedVarSerializer(nil) - - tt := login.NewServerInfo() - tt.OnlineID = 99 - tt.IP = "127.0.0.1" - // tt1 := serialize.NewDefaultPacketHandler[info.ServerInfo]() - // tg, _ := tt1.Serialize(*tt) - // 序列化 - data, err := serializer.Serialize(*tt) - if err != nil { - fmt.Println("错误:", err) - return - } - - fmt.Printf("序列化数据(长度: %d字节):\n%x\n", len(data), data) -} -func TestDefaultPacketSerializerPet(t *testing.T) { - serializer := NewFixedVarSerializer(nil) - - tt := pet.NewPetInfo() - - // tt1 := serialize.NewDefaultPacketHandler[info.ServerInfo]() - // tg, _ := tt1.Serialize(*tt) - // 序列化 - data, err := serializer.Serialize(*tt) - if err != nil { - fmt.Println("错误:", err) - return - } - - fmt.Printf("序列化数据(长度: %d字节):\n%x\n", len(data), data) -} diff --git a/common/serialize/unSerialize.go b/common/serialize/unSerialize.go deleted file mode 100644 index e62fe2b62..000000000 --- a/common/serialize/unSerialize.go +++ /dev/null @@ -1,336 +0,0 @@ -package serialize - -import ( - "bytes" - "encoding/binary" - "fmt" - "reflect" - "strconv" - - "github.com/gogf/gf/v2/frame/g" -) - -// FixedVarDeserializer 定长反序列化器 -type FixedVarDeserializer struct { - byteOrder binary.ByteOrder -} - -// NewFixedVarDeserializer 创建新的反序列化器 -func NewFixedVarDeserializer(order binary.ByteOrder) *FixedVarDeserializer { - if order == nil { - order = binary.BigEndian - } - return &FixedVarDeserializer{byteOrder: order} -} - -// Deserialize 反序列化字节数据到结构体 -func (d *FixedVarDeserializer) Deserialize(data []byte, obj reflect.Value) reflect.Value { - reader := bytes.NewReader(data) - g.Dump(obj) - // 获取传入值的类型并创建新实例 - valType := reflect.TypeOf(obj.Interface()) - newVal := reflect.New(valType).Elem() - - // 处理指针类型 - if newVal.Kind() == reflect.Ptr { - newVal = newVal.Elem() - } - - // 遍历所有字段进行反序列化 - for i := 0; i < newVal.NumField(); i++ { - field := newVal.Type().Field(i) - fieldVal := newVal.Field(i) - - tag := field.Tag.Get("serialize") - if tag == "" { - if err := binary.Read(reader, d.byteOrder, fieldVal.Addr().Interface()); err != nil { - return newVal - } - continue - } - - mode, params, err := parseTag(tag) - if err != nil { - return newVal - } - - switch mode { - case lengthtype.FIXED_LENGTH: - if err := d.deserializeFixedField(reader, fieldVal, params); err != nil { - return newVal - } - case lengthtype.LENGTH_FIRST: - if err := d.deserializeVarWithLenField(reader, fieldVal, params); err != nil { - return newVal - } - default: - return newVal - } - } - - return newVal -} - -// 反序列化值 - -// 反序列化结构体 -func (d *FixedVarDeserializer) deserializeStruct(reader *bytes.Reader, val reflect.Value) error { - typ := val.Type() - - for i := 0; i < val.NumField(); i++ { - field := typ.Field(i) - fieldVal := val.Field(i) - - // 跳过未导出字段 - if !field.IsExported() { - continue - } - - // 处理指针类型字段 - if fieldVal.Kind() == reflect.Ptr { - if fieldVal.IsNil() { - fieldVal.Set(reflect.New(fieldVal.Type().Elem())) - } - fieldVal = fieldVal.Elem() - } - - // 解析标签 - tag := field.Tag.Get("serialize") - if tag == "" { - // 无标签:按原生类型处理 - if err := d.deserializeValue(reader, fieldVal); err != nil { - return fmt.Errorf("字段 %s 反序列化失败: %v", field.Name, err) - } - continue - } - - // 解析模式和参数 - mode, params, err := parseTag(tag) - if err != nil { - return fmt.Errorf("字段 %s 标签解析失败: %v", field.Name, err) - } - - // 根据模式反序列化 - switch mode { - case lengthtype.FIXED_LENGTH: - if err := d.deserializeFixedField(reader, fieldVal, params); err != nil { - return fmt.Errorf("字段 %s 定长反序列化失败: %v", field.Name, err) - } - case lengthtype.LENGTH_FIRST: - if err := d.deserializeVarWithLenField(reader, fieldVal, params); err != nil { - return fmt.Errorf("字段 %s 变长反序列化失败: %v", field.Name, err) - } - default: - return fmt.Errorf("字段 %s 不支持的序列化模式: %s", field.Name, mode) - } - } - - return nil -} - -// 反序列化定长字段 -func (d *FixedVarDeserializer) deserializeValue(reader *bytes.Reader, val reflect.Value) error { - kind := val.Kind() - - switch kind { - case reflect.Struct: - return d.deserializeStruct(reader, val) - case reflect.Slice: - return d.deserializeSlice(reader, val) - default: - if err := binary.Read(reader, d.byteOrder, val.Addr().Interface()); err != nil { - return err - } - return nil - } -} - -func (d *FixedVarDeserializer) deserializeFixedField(reader *bytes.Reader, val reflect.Value, lengthStr string) error { - length, err := strconv.Atoi(lengthStr) - if err != nil { - return fmt.Errorf("无效的定长参数: %s", lengthStr) - } - - return d.deserializeFixedLengthValue(reader, val, length) -} - -// 反序列化带长度前缀的可变长度字段 -func (d *FixedVarDeserializer) deserializeVarWithLenField(reader *bytes.Reader, val reflect.Value, lenBytesStr string) error { - lenBytes, err := strconv.Atoi(lenBytesStr) - if err != nil { - return fmt.Errorf("无效的长度字节数: %s", lenBytesStr) - } - - if lenBytes < 1 || lenBytes > 8 { - return fmt.Errorf("长度字节数需在1-8之间") - } - - // 读取长度 - lenData := make([]byte, lenBytes) - if _, err := reader.Read(lenData); err != nil { - return err - } - - var contentLength uint64 - switch lenBytes { - case 1: - contentLength = uint64(lenData[0]) - case 2: - contentLength = uint64(d.byteOrder.Uint16(lenData)) - case 4: - contentLength = uint64(d.byteOrder.Uint32(lenData)) - case 8: - contentLength = d.byteOrder.Uint64(lenData) - default: - return fmt.Errorf("不支持的长度字节数: %d", lenBytes) - } - - // 读取内容 - contentData := make([]byte, contentLength) - if _, err := reader.Read(contentData); err != nil { - return err - } - - // 反序列化内容 - contentReader := bytes.NewReader(contentData) - return d.deserializeValue(contentReader, val) -} - -// 反序列化定长值 -func (d *FixedVarDeserializer) deserializeFixedLengthValue(reader *bytes.Reader, val reflect.Value, length int) error { - kind := val.Kind() - - if kind == reflect.String { - // 读取定长字符串 - data := make([]byte, length) - if _, err := reader.Read(data); err != nil { - return err - } - - // 找到字符串结束位置(去除填充的0) - var strLen int - for strLen = 0; strLen < length && data[strLen] != 0; strLen++ { - } - fmt.Println(val.CanSet()) - val.SetString(string(data[:strLen])) - g.Dump(val) - return nil - } - - // 其他类型:读取固定长度字节,然后反序列化 - data := make([]byte, length) - if _, err := reader.Read(data); err != nil { - return err - } - - // 如果是slice或array,特殊处理 - if kind == reflect.Slice || kind == reflect.Array { - sliceReader := bytes.NewReader(data) - return d.deserializeValue(sliceReader, val) - } - - // 基本类型 - fieldReader := bytes.NewReader(data) - return binary.Read(fieldReader, d.byteOrder, val.Addr().Interface()) -} - -// 反序列化slice -func (d *FixedVarDeserializer) deserializeSlice(reader *bytes.Reader, val reflect.Value) error { - // 获取slice元素类型 - elemType := val.Type().Elem() - - // 检查slice是否有标签 - var tag string - // 检查slice元素是否为结构体 - if elemType.Kind() == reflect.Struct { - // 如果元素是结构体,获取结构体的标签 - structType := elemType - for i := 0; i < structType.NumField(); i++ { - field := structType.Field(i) - if tag = field.Tag.Get("serialize"); tag != "" { - break - } - } - } - - if tag != "" { - // 元素有标签:按定长模式处理 - mode, params, err := parseTag(tag) - if err != nil { - return err - } - - if mode == lengthtype.FIXED_LENGTH { - // 定长slice:解析maxItems和itemLen - parts := splitTag(params) - if len(parts) != 2 { - return fmt.Errorf("定长slice标签需格式为 fixed:N,M") - } - maxItems, _ := strconv.Atoi(parts[0]) - itemLen, _ := strconv.Atoi(parts[1]) - return d.deserializeFixedSlice(reader, val, maxItems, itemLen) - } - } - - // // 无标签或不定长模式:按原生slice处理(先读长度,再读元素) - // var count int - - // // 尝试读取slice长度 - // // 这里假设使用int32作为长度类型,实际应根据序列化逻辑调整 - // var length int32 - // if err := binary.Read(reader, d.byteOrder, &length); err != nil { - // return err - // } - // count = int(length) - - // // 创建足够容量的slice - // if val.Kind() == reflect.Slice { - // val.Set(reflect.MakeSlice(val.Type(), count, count)) - // } - - // // 读取每个元素 - // for i := 0; i < count; i++ { - // item := val.Index(i) - // if err := binary.Read(reader, d.byteOrder, item.Addr().Interface()); err != nil { - // return err - // } - // } - - return nil -} - -// 反序列化定长slice -func (d *FixedVarDeserializer) deserializeFixedSlice(reader *bytes.Reader, val reflect.Value, maxItems, itemLen int) error { - // 读取实际元素数量 - countByte, err := reader.ReadByte() - if err != nil { - return err - } - count := int(countByte) - - if count > maxItems { - count = maxItems - } - - // 创建slice - if val.Kind() == reflect.Slice { - val.Set(reflect.MakeSlice(val.Type(), count, count)) - } - - // 读取每个元素 - for i := 0; i < maxItems; i++ { - if i < count { - item := val.Index(i) - if err := d.deserializeFixedLengthValue(reader, item, itemLen); err != nil { - return err - } - } else { - // 跳过填充字节 - if _, err := reader.Seek(int64(itemLen), 1); err != nil { - return err - } - } - } - - return nil -} diff --git a/logic/controller/login.go b/logic/controller/login.go index a1d2aee5d..f0678b2bf 100644 --- a/logic/controller/login.go +++ b/logic/controller/login.go @@ -1,22 +1,18 @@ package controller import ( - "blazing/common/core/info" "blazing/common/data/entity" "blazing/logic/service/login" "encoding/hex" "fmt" ) -// todo 待实现自动编解码Recv_1001 // 处理命令: 1001 -func (h Controller) Recv_1001(data []byte, player *entity.Player, res chan interface{}) { - pinfo := info.NewLoginSidInfo() - t := parseCmd[info.LoginSidInfo](pinfo, data) - //login.CheakSession(t, player) - // login.OnData_1001(pinfo, player) - fmt.Println(login.CheakSession(t, player)) +func (h Controller) Login(data login.LoginSidInfo, player *entity.Player) interface{} { + + fmt.Println(login.CheakSession(data, player)) t1, _ := hex.DecodeString("0000045D37000003E9000186A600000000000186A6683F89CF6E69656F0000000000000000000000000008000F00000000000000000000000000000000000000000000000000000001000001DB0000018B000000000000A8C000000000000000000000000000000000000000080001388000000001000000017FFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030000000000000000000000000000000000000064000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001FFFFFFFF000000004E4F4E4F0000000000000000000000000000000000000001000000010000000100000001000000010000000100000001000000000003030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030000000100000064000000000000000000000000000000000000001F000000000000006400000000000093F4000093F4000000D5000000F7000000AD00000088000000920000008C0000009C00000000000000000000000000000000000000000000000000000004000027900000001B00004E6200000014000028380000002800004E3E0000002368493DC60000000000000000000000000000000000000000000100000000000000A937000007D1000186A600000000000186A66E69656F00000000000000000000000000000000000000000000000F0000000000000000000001DB0000018B0000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFF0000000000000001000000000000000000000000000000000000000000000000000000000000000000000000") - res <- t1 + + return t1 } diff --git a/logic/service/login/PlayerLoginService.go b/logic/service/login/PlayerLoginService.go index f5c0f7068..4c9cbf0ed 100644 --- a/logic/service/login/PlayerLoginService.go +++ b/logic/service/login/PlayerLoginService.go @@ -1,7 +1,6 @@ package login import ( - "blazing/common/core/info" "blazing/common/data/entity" "blazing/cool" "context" @@ -10,7 +9,50 @@ import ( "strings" ) -func CheakSession(c info.LoginSidInfo, p *entity.Player) bool { +// CommendSvrInfo 初始连接请求信息结构体 +type CommendSvrInfo struct { + MaxOnlineID uint32 // 最大连接数 + IsVip uint32 // 建议为0 + ServerInfoLen uint32 `struc:"sizeof=ServerList"` // 服务器信息长度 ServerInfo + ServerList []ServerInfo // 服务器具体信息 + Reversed uint32 // 保留字段 +} + +func NewCommendSvrInfo() *CommendSvrInfo { + return &CommendSvrInfo{ + MaxOnlineID: 100, + IsVip: 0, + ServerInfoLen: 0, + ServerList: make([]ServerInfo, 0), + Reversed: 0, + } +} + +// ServerInfo 服务器信息结构体 +type ServerInfo struct { + // 连接ID, 即服务器序号 + OnlineID uint32 + // 当前服务器玩家在线数量, 供SWF显示 + UserCnt uint32 + // 服务器IP, 16字节UTF-8, 不足16补齐到16 + IP string `struc:"[16]byte"` // 定长模式:16字节 + // 端口 + Port uint16 + // 好友在线的个数 + Friends uint32 +} + +// NewServerInfo 创建新的服务器信息实例 +func NewServerInfo() *ServerInfo { + return &ServerInfo{} +} + +// LoginSidInfo 登录携带的凭证结构体 +type LoginSidInfo struct { + Sid []byte `struc:"[16]byte"` // 登录会话ID,固定长度16字节 +} + +func CheakSession(c LoginSidInfo, p *entity.Player) bool { // tt, _ := cool.CacheManager.Keys(context.Background()) //g.Dump(tt) t1 := hex.EncodeToString(c.Sid) diff --git a/logic/service/login/recv.go b/logic/service/login/recv.go deleted file mode 100644 index a5edae7a4..000000000 --- a/logic/service/login/recv.go +++ /dev/null @@ -1,10 +0,0 @@ -package login - -import ( - "blazing/common/core/info" - "blazing/common/data/entity" -) - -func OnData_105(c info.LoginSidInfo, p *entity.Player) { - -}