-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarpscan.go
More file actions
252 lines (232 loc) · 5.77 KB
/
Copy pathwarpscan.go
File metadata and controls
252 lines (232 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)
// --- Interactive exact-value memory search (opt-in via -search) ---
//
// A minimal Cheat-Engine-style scanner for finding small state values (e.g. the
// selected episode and the world/painting id on the episode-select screen).
// Works on single bytes.
//
// Commands:
// n <v> new scan: addresses whose byte == <v>
// n new snapshot (unknown value) — then use inc/dec/diff/same
// <v> next scan: keep addresses whose byte == <v>
// inc/dec keep addresses whose value increased/decreased since last scan
// diff/same keep addresses whose value changed/stayed
// le/ge <n> keep addresses whose current value is <= / >= <n>
// l list current candidates (up to 40)
// r <hex> read the byte at a GC address (e.g. r 80A01C9B)
// d <hex> <len> hex-dump <len> bytes from a GC address
// q quit
const (
searchRAMStart = 0x80000000
searchBlock = 0x400000
searchBlocks = 6 // 24 MB
)
func readAllRAM() []byte {
buf := make([]byte, searchBlock*searchBlocks)
for i := 0; i < searchBlocks; i++ {
d, err := dm.Read(uint32(searchRAMStart)+uint32(i*searchBlock), searchBlock)
if err == nil && d != nil {
copy(buf[i*searchBlock:], d)
}
}
return buf
}
func parseHexAddr(s string) (uint32, bool) {
a, err := strconv.ParseUint(strings.TrimPrefix(s, "0x"), 16, 32)
if err != nil {
return 0, false
}
return uint32(a), true
}
// init dispatches the interactive search tool when -search is passed, so main.go
// carries no reference to it (this file stays uncommitted/local).
func init() {
for _, a := range os.Args[1:] {
if a == "-search" || a == "search" {
RunSearch()
os.Exit(0)
}
}
}
func RunSearch() {
globalCfg = LoadConfig()
fmt.Println("[SEARCH] waiting for Dolphin...")
for !dm.IsHooked {
if !dm.Hook() {
time.Sleep(1 * time.Second)
continue
}
}
fmt.Println("[SEARCH] hooked. n <v> | n | <v> | inc | dec | diff | same | le/ge <n> | l | r <hex> | d <hex> <len> | q")
var cands []uint32
var prev []byte
isAll := false // true = candidate set is "everything" (right after `n` snapshot)
haveScan := false
report := func() {
if isAll {
fmt.Println("[SEARCH] snapshot taken (unknown value). Change it in-game, then: inc/dec/diff/same")
return
}
fmt.Printf("[SEARCH] %d candidate(s)\n", len(cands))
if len(cands) <= 40 {
for _, a := range cands {
off := int(a - searchRAMStart)
v := byte(0)
if off >= 0 && off < len(prev) {
v = prev[off]
}
fmt.Printf(" 0x%08X = %d\n", a, v)
}
}
}
// applyFilter narrows candidates by keep(oldVal, newVal).
applyFilter := func(cur []byte, keep func(old, nw byte) bool) {
var next []uint32
if isAll {
for i := 0; i < len(cur) && i < len(prev); i++ {
if keep(prev[i], cur[i]) {
next = append(next, uint32(searchRAMStart+i))
}
}
} else {
for _, a := range cands {
off := int(a - searchRAMStart)
if off >= 0 && off < len(cur) && keep(prev[off], cur[off]) {
next = append(next, a)
}
}
}
cands = next
isAll = false
prev = cur
}
sc := bufio.NewScanner(os.Stdin)
fmt.Print("> ")
for sc.Scan() {
fields := strings.Fields(strings.TrimSpace(sc.Text()))
if len(fields) == 0 {
fmt.Print("> ")
continue
}
cmd := fields[0]
switch {
case cmd == "q":
return
case cmd == "l":
report()
case cmd == "r" && len(fields) == 2:
addr, ok := parseHexAddr(fields[1])
if !ok {
fmt.Println("bad address")
break
}
d, err := dm.Read(addr, 1)
if err != nil || d == nil {
fmt.Println("read failed")
break
}
fmt.Printf(" 0x%08X = %d\n", addr, d[0])
case cmd == "d" && len(fields) == 3:
addr, ok := parseHexAddr(fields[1])
n, err := strconv.Atoi(fields[2])
if !ok || err != nil || n <= 0 || n > 512 {
fmt.Println("usage: d <hexaddr> <len 1-512>")
break
}
d, err := dm.Read(addr, n)
if err != nil || d == nil {
fmt.Println("read failed")
break
}
for off := 0; off < len(d); off += 16 {
end := off + 16
if end > len(d) {
end = len(d)
}
fmt.Printf(" 0x%08X % X\n", addr+uint32(off), d[off:end])
}
case cmd == "n" && len(fields) == 2:
v, err := strconv.Atoi(fields[1])
if err != nil || v < 0 || v > 255 {
fmt.Println("value must be 0-255")
break
}
cur := readAllRAM()
cands = nil
for i := 0; i < len(cur); i++ {
if cur[i] == byte(v) {
cands = append(cands, uint32(searchRAMStart+i))
}
}
isAll = false
prev = cur
haveScan = true
report()
case cmd == "n":
prev = readAllRAM()
cands = nil
isAll = true
haveScan = true
report()
case cmd == "inc" || cmd == "dec" || cmd == "diff" || cmd == "same":
if !haveScan {
fmt.Println("do a `n` scan first")
break
}
cur := readAllRAM()
applyFilter(cur, func(old, nw byte) bool {
switch cmd {
case "inc":
return nw > old
case "dec":
return nw < old
case "diff":
return nw != old
default:
return nw == old
}
})
report()
case (cmd == "le" || cmd == "ge") && len(fields) == 2:
if !haveScan {
fmt.Println("do a `n` scan first")
break
}
n, err := strconv.Atoi(fields[1])
if err != nil {
fmt.Println("bad number")
break
}
cur := readAllRAM()
applyFilter(cur, func(_, nw byte) bool {
if cmd == "le" {
return int(nw) <= n
}
return int(nw) >= n
})
report()
default: // bare number -> exact filter
v, err := strconv.Atoi(cmd)
if err != nil || v < 0 || v > 255 {
fmt.Println("unknown command")
break
}
if !haveScan {
fmt.Println("do a `n` scan first")
break
}
cur := readAllRAM()
applyFilter(cur, func(_, nw byte) bool { return nw == byte(v) })
report()
}
fmt.Print("> ")
}
}