From 04c30b2fe8d6a72d71915066e48aea1572cdd94d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Gim=C3=A9nez?= Date: Mon, 13 Mar 2023 21:28:12 +0900 Subject: [PATCH] Add layers on stats and small refactor --- pkg/data/data.go | 2 +- pkg/data/items.go | 2 +- pkg/data/npc.go | 2 +- pkg/data/stat/stats.go | 12 ++++++++--- pkg/memory/game_reader.go | 45 ++++++++++++++++++++++++++------------- pkg/memory/item.go | 25 +++++++--------------- pkg/memory/monsters.go | 6 +++--- pkg/memory/player.go | 8 +++---- 8 files changed, 57 insertions(+), 45 deletions(-) diff --git a/pkg/data/data.go b/pkg/data/data.go index 2edf922..ea87b2a 100644 --- a/pkg/data/data.go +++ b/pkg/data/data.go @@ -118,7 +118,7 @@ type PlayerUnit struct { Name string Area area.Area Position Position - Stats map[stat.Stat]int + Stats map[stat.ID]int Skills map[skill.Skill]int States state.States } diff --git a/pkg/data/items.go b/pkg/data/items.go index 29137b7..fc1fc42 100644 --- a/pkg/data/items.go +++ b/pkg/data/items.go @@ -25,7 +25,7 @@ type Item struct { Position Position Ethereal bool IsHovered bool - Stats map[stat.Stat]int + Stats map[stat.ID]stat.Data Identified bool IsVendor bool } diff --git a/pkg/data/npc.go b/pkg/data/npc.go index e4c2d44..a158d4d 100644 --- a/pkg/data/npc.go +++ b/pkg/data/npc.go @@ -18,7 +18,7 @@ type Monster struct { Name npc.ID IsHovered bool Position Position - Stats map[stat.Stat]int + Stats map[stat.ID]int Type MonsterType } diff --git a/pkg/data/stat/stats.go b/pkg/data/stat/stats.go index 8737f03..6b6aa37 100644 --- a/pkg/data/stat/stats.go +++ b/pkg/data/stat/stats.go @@ -1,9 +1,15 @@ package stat -type Stat int16 +type ID int16 + +type Data struct { + ID ID + Value int + Layer int +} const ( - Strength Stat = iota + Strength ID = iota Energy Dexterity Vitality @@ -364,7 +370,7 @@ const ( PassiveMagicPierce ) -func (s Stat) String() string { +func (s ID) String() string { switch s { case Strength: return "strength" diff --git a/pkg/memory/game_reader.go b/pkg/memory/game_reader.go index c5ad1df..0ca920a 100644 --- a/pkg/memory/game_reader.go +++ b/pkg/memory/game_reader.go @@ -86,23 +86,38 @@ func (gd *GameReader) hoveredData() (hoveredUnitID uint, hoveredType uint, isHov return 0, 0, false } -func getStatData(statEnum, statValue uint) (stat.Stat, int) { - value := int(statValue) - switch stat.Stat(statEnum) { - case stat.Life, - stat.MaxLife, - stat.Mana, - stat.MaxMana, - stat.Stamina, - stat.LifePerLevel, - stat.ManaPerLevel: - value = int(statValue >> 8) - case stat.ColdLength, - stat.PoisonLength: - value = int(statValue / 25) +func (gd *GameReader) getStatsData(statCount uint, statPtr uintptr) []stat.Data { + var stats = make([]stat.Data, 0) + statBuffer := gd.Process.ReadBytesFromMemory(statPtr, statCount*10) + for i := 0; i < int(statCount); i++ { + offset := uint(i * 8) + statLayer := ReadUIntFromBuffer(statBuffer, offset, Uint16) + statEnum := ReadUIntFromBuffer(statBuffer, offset+0x2, Uint16) + statValue := ReadUIntFromBuffer(statBuffer, offset+0x4, Uint32) + + value := int(statValue) + switch stat.ID(statEnum) { + case stat.Life, + stat.MaxLife, + stat.Mana, + stat.MaxMana, + stat.Stamina, + stat.LifePerLevel, + stat.ManaPerLevel: + value = int(statValue >> 8) + case stat.ColdLength, + stat.PoisonLength: + value = int(statValue / 25) + } + + stats = append(stats, stat.Data{ + ID: stat.ID(statEnum), + Value: value, + Layer: int(statLayer), + }) } - return stat.Stat(statEnum), value + return stats } func setProperties(item *data.Item, flags uint32) { diff --git a/pkg/memory/item.go b/pkg/memory/item.go index 6f4d874..769a94d 100644 --- a/pkg/memory/item.go +++ b/pkg/memory/item.go @@ -102,28 +102,19 @@ func (gd *GameReader) Items(playerPosition data.Position) data.Items { return items } -func (gd *GameReader) getItemStats(statCount uint, statPtr uintptr, statExCount uint, statExPtr uintptr) map[stat.Stat]int { - stats := map[stat.Stat]int{} - +func (gd *GameReader) getItemStats(statCount uint, statPtr uintptr, statExCount uint, statExPtr uintptr) map[stat.ID]stat.Data { + stats := make(map[stat.ID]stat.Data, 0) if statCount < 20 && statCount > 0 { - statBuffer := gd.Process.ReadBytesFromMemory(statPtr, statCount*10) - for i := 0; i < int(statCount); i++ { - offset := uint(i * 8) - statEnum := ReadUIntFromBuffer(statBuffer, offset+0x2, Uint16) - statValue := ReadUIntFromBuffer(statBuffer, offset+0x4, Uint32) - st, value := getStatData(statEnum, statValue) - stats[st] = value + stats1 := gd.getStatsData(statCount, statPtr) + for _, v := range stats1 { + stats[v.ID] = v } } if statExCount < 20 && statExCount > 0 { - statBuffer := gd.Process.ReadBytesFromMemory(statExPtr, statExCount*10) - for i := 0; i < int(statExCount); i++ { - offset := uint(i * 8) - statEnum := ReadUIntFromBuffer(statBuffer, offset+0x2, Uint16) - statValue := ReadUIntFromBuffer(statBuffer, offset+0x4, Uint32) - st, value := getStatData(statEnum, statValue) - stats[st] = value + stats2 := gd.getStatsData(statExCount, statExPtr) + for _, v := range stats2 { + stats[v.ID] = v } } diff --git a/pkg/memory/monsters.go b/pkg/memory/monsters.go index c5f4e8b..c2e8e83 100644 --- a/pkg/memory/monsters.go +++ b/pkg/memory/monsters.go @@ -97,8 +97,8 @@ func getMonsterType(typeFlag byte) data.MonsterType { return data.MonsterTypeNone } -func (gd *GameReader) getMonsterStats(statCount uint, statPtr uintptr) map[stat.Stat]int { - stats := map[stat.Stat]int{} +func (gd *GameReader) getMonsterStats(statCount uint, statPtr uintptr) map[stat.ID]int { + stats := map[stat.ID]int{} if statCount > 0 { statBuffer := gd.Process.ReadBytesFromMemory(statPtr+0x2, statCount*8) @@ -106,7 +106,7 @@ func (gd *GameReader) getMonsterStats(statCount uint, statPtr uintptr) map[stat. offset := uint(i * 8) statEnum := ReadUIntFromBuffer(statBuffer, offset, Uint16) statValue := ReadUIntFromBuffer(statBuffer, offset+0x2, Uint32) - stats[stat.Stat(statEnum)] = int(statValue) + stats[stat.ID(statEnum)] = int(statValue) } } diff --git a/pkg/memory/player.go b/pkg/memory/player.go index b3ba142..ffa0695 100644 --- a/pkg/memory/player.go +++ b/pkg/memory/player.go @@ -93,20 +93,20 @@ func (gd *GameReader) GetPlayerUnit(playerUnit uintptr) data.PlayerUnit { statPtr := gd.Process.ReadUInt(statsListExPtr+0x30, Uint64) statCount := gd.Process.ReadUInt(statsListExPtr+0x38, Uint64) - stats := map[stat.Stat]int{} + stats := map[stat.ID]int{} for j := 0; j < int(statCount); j++ { statOffset := uintptr(statPtr) + 0x2 + uintptr(j*8) statNumber := gd.Process.ReadUInt(statOffset, Uint16) statValue := gd.Process.ReadUInt(statOffset+0x02, Uint32) - switch stat.Stat(statNumber) { + switch stat.ID(statNumber) { case stat.Life, stat.MaxLife, stat.Mana, stat.MaxMana: - stats[stat.Stat(statNumber)] = int(uint32(statValue) >> 8) + stats[stat.ID(statNumber)] = int(uint32(statValue) >> 8) default: - stats[stat.Stat(statNumber)] = int(statValue) + stats[stat.ID(statNumber)] = int(statValue) } }