-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffsets.go
More file actions
392 lines (348 loc) · 11.9 KB
/
Copy pathoffsets.go
File metadata and controls
392 lines (348 loc) · 11.9 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package main
import (
"encoding/binary"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
// memBlueCoins holds the set of blue coin IDs (bcid) currently collected
// according to the game's save memory. Rebuilt every scan by the offset tracker
// and read by the /api/memory handler, so it needs a lock.
var (
memBlueCoins = map[string]bool{}
memBlueCoinsMu sync.RWMutex
)
// snapshotBlueCoins returns the collected blue coin IDs as a slice for the API.
func snapshotBlueCoins() []string {
memBlueCoinsMu.RLock()
defer memBlueCoinsMu.RUnlock()
out := make([]string, 0, len(memBlueCoins))
for id := range memBlueCoins {
out = append(out, id)
}
return out
}
// updateMemBlueCoins rebuilds the collected-set from the freshly read bit states
// (keyed by stage name), mapping each set bit to its blue coin ID.
func updateMemBlueCoins(bitsByStage map[string][]bool) {
newSet := make(map[string]bool)
for _, bc := range currentWorld.BlueCoins {
arr, ok := bitsByStage[bc.Stage]
if ok && bc.MemoryIndex >= 0 && bc.MemoryIndex < len(arr) && arr[bc.MemoryIndex] {
newSet[bc.ID] = true
}
}
memBlueCoinsMu.Lock()
memBlueCoins = newSet
memBlueCoinsMu.Unlock()
}
// memShines holds the set of shine IDs currently collected per the save memory.
var (
memShines = map[string]bool{}
memShinesMu sync.RWMutex
)
func snapshotShines() []string {
memShinesMu.RLock()
defer memShinesMu.RUnlock()
out := make([]string, 0, len(memShines))
for id := range memShines {
out = append(out, id)
}
return out
}
// updateMemShines maps each collected (mem_byte, mem_bit) to every shine ID that
// uses it (some shines share a bit across episodes) and stores the full set.
// Virtual "blue coin trade" shines are excluded — the UI's trade counter owns those.
func updateMemShines(collectedMem map[[2]int]bool) {
newSet := make(map[string]bool)
for _, zone := range currentWorld.Zones {
for _, shine := range zone.ShinesAvailable {
if strings.HasPrefix(shine.ID, "blue_coin_trade_") {
continue
}
if collectedMem[[2]int{shine.MemByte, shine.MemBit}] {
newSet[shine.ID] = true
}
}
}
memShinesMu.Lock()
memShines = newSet
memShinesMu.Unlock()
}
// memTradeCount is how many blue-coin trade-in shines are collected per the save.
var (
memTradeCount int
memTradeCountMu sync.RWMutex
)
func snapshotTradeCount() int {
memTradeCountMu.RLock()
defer memTradeCountMu.RUnlock()
return memTradeCount
}
// updateMemTrades counts collected blue_coin_trade_* shines (they have real save
// bits and are earned sequentially, so a count matches the UI's trade counter).
func updateMemTrades(collectedMem map[[2]int]bool) {
count := 0
for _, zone := range currentWorld.Zones {
for _, shine := range zone.ShinesAvailable {
if strings.HasPrefix(shine.ID, "blue_coin_trade_") &&
collectedMem[[2]int{shine.MemByte, shine.MemBit}] {
count++
}
}
}
memTradeCountMu.Lock()
memTradeCount = count
memTradeCountMu.Unlock()
}
// ADDR_SAVE_POINTER points to the start of the save data block
const ADDR_SAVE_POINTER = 0x8040E160
type BlueCoinTracker struct {
LevelName string
Offset int
BitStart int
Count int
}
type ShineTracker struct {
LevelName string
BitOffsets [][]int // Pairs of [byte, bit]
CoinOffset []int // Pair of [byte, bit]
}
// Global state to track changes and prevent log spam
var lastBlueCoinStates = make(map[string][]bool)
var lastShineStates = make(map[string][]bool)
// --- DolphinHookManager Extensions ---
func (d *DolphinHookManager) ResolveSaveBase() uint32 {
data, err := d.Read(ADDR_SAVE_POINTER, 4)
if err != nil || data == nil {
return 0
}
base := binary.BigEndian.Uint32(data)
if base < 0x80000000 || base > 0x81800000 {
return 0
}
return base
}
func (d *DolphinHookManager) ReadBit(base uint32, byteOffset int, bitIdx int) bool {
data, err := d.Read(base+uint32(byteOffset), 1)
if err != nil || len(data) == 0 {
return false
}
return (data[0] & (1 << bitIdx)) != 0
}
// --- Tracker Logic ---
func RunOffsetTracker() {
// Blue Coin Definitions from offsets.json
blueTrackers := []BlueCoinTracker{
{"Bianco Hills", 0x15, 2, 30},
{"Ricco Harbor", 0x1B, 4, 30},
{"Gelato Beach", 0x21, 6, 30},
{"Pinna Park", 0x28, 0, 30},
{"Sirena Beach", 0x2E, 2, 30},
{"Noki Bay", 0x3A, 6, 30},
{"Pianta Village", 0x34, 4, 30},
{"Delfino Plaza", 0x0F, 0, 20},
{"Corona Mountain", 0x43, 4, 10},
}
var shineTrackers = []ShineTracker{
{"Bianco Hills", [][]int{{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7}, {1, 0}, {1, 1}}, []int{0x0C, 4}},
{"Ricco Harbor", [][]int{{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {2, 0}, {2, 1}, {2, 2}, {2, 3}}, []int{0x0C, 5}},
{"Gelato Beach", [][]int{{2, 4}, {2, 5}, {2, 6}, {2, 7}, {3, 0}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}}, []int{0x0C, 6}},
{"Pinna Park", [][]int{{3, 6}, {3, 7}, {4, 0}, {4, 1}, {4, 2}, {4, 3}, {4, 4}, {4, 5}, {4, 6}, {4, 7}}, []int{0x0C, 7}},
{"Sirena Beach", [][]int{{5, 0}, {5, 1}, {5, 2}, {5, 3}, {5, 4}, {5, 5}, {5, 6}, {5, 7}, {6, 0}, {6, 1}}, []int{0x0D, 0}},
{"Noki Bay", [][]int{{6, 2}, {6, 3}, {6, 4}, {6, 5}, {6, 6}, {6, 7}, {7, 0}, {7, 1}, {7, 2}, {7, 3}}, []int{0x0D, 1}},
{"Pianta Village", [][]int{{7, 4}, {7, 5}, {7, 6}, {7, 7}, {8, 0}, {8, 1}, {8, 2}, {8, 3}, {8, 4}, {8, 5}}, []int{0x0D, 2}},
// --- SPECIAL LOCATIONS ---
{"Corona Mountain", [][]int{{14, 7}}, nil},
{"Delfino Plaza", [][]int{
{10, 6}, {11, 0}, {8, 6}, {8, 7}, {9, 0}, {9, 1}, {9, 2}, {9, 3}, {9, 4}, {9, 5}, {9, 6}, {9, 7},
{10, 0}, {10, 1}, {10, 2}, {10, 3}, {10, 4}, {10, 5}, {13, 4}, {13, 5}, {13, 6}, {13, 7},
{14, 0}, {14, 1}, {14, 2}, {14, 3}, {10, 7}, {11, 1}, {11, 2}, {11, 3}, {11, 4}, {11, 5},
{11, 6}, {11, 7}, {12, 0}, {12, 1}, {12, 2}, {12, 3}, {13, 3}, {14, 5}, {14, 6}, {14, 4},
}, nil},
}
debugf("[MAPPING MODE] Tracker active. Collect a Shine or Blue Coin to see memory data.\n")
for {
if !dm.IsHooked {
time.Sleep(1 * time.Second)
continue
}
saveBase := dm.ResolveSaveBase()
if saveBase == 0 {
time.Sleep(1 * time.Second)
continue
}
// 1. Process Blue Coins
bitsByStage := make(map[string][]bool)
for _, bt := range blueTrackers {
currentBits := make([]bool, bt.Count)
for i := 0; i < bt.Count; i++ {
bitPos := bt.BitStart + i
byteOff := bt.Offset + (bitPos / 8)
bitIdx := bitPos % 8
currentBits[i] = dm.ReadBit(saveBase, byteOff, bitIdx)
}
if last, ok := lastBlueCoinStates[bt.LevelName]; ok {
for i, val := range currentBits {
if val && !last[i] {
handleBlueCoinCollection(bt.LevelName, i)
}
}
}
lastBlueCoinStates[bt.LevelName] = currentBits
bitsByStage[bt.LevelName] = currentBits
}
// Expose the full current collected-set to the UI (via /api/memory).
updateMemBlueCoins(bitsByStage)
// 2. Process Shines
collectedMem := make(map[[2]int]bool)
for _, st := range shineTrackers {
currentShines := []bool{}
// Process Main/Secret Bits
for _, p := range st.BitOffsets {
v := dm.ReadBit(saveBase, p[0], p[1])
currentShines = append(currentShines, v)
if v {
collectedMem[[2]int{p[0], p[1]}] = true
}
}
// Safety check for 100-Coin Bit
if st.CoinOffset != nil {
v := dm.ReadBit(saveBase, st.CoinOffset[0], st.CoinOffset[1])
currentShines = append(currentShines, v)
if v {
collectedMem[[2]int{st.CoinOffset[0], st.CoinOffset[1]}] = true
}
}
if last, ok := lastShineStates[st.LevelName]; ok {
for i, val := range currentShines {
if val && !last[i] {
handleShineCollection(st.LevelName, i, st)
}
}
}
lastShineStates[st.LevelName] = currentShines
}
// Expose the full current collected shine-set + trade count to the UI.
updateMemShines(collectedMem)
updateMemTrades(collectedMem)
time.Sleep(500 * time.Millisecond)
}
}
func handleBlueCoinCollection(stageName string, index int) {
for _, bc := range currentWorld.BlueCoins {
if bc.Stage == stageName && bc.MemoryIndex == index {
debugf("[COLLECTED] Blue Coin: %s (%s - Bit Index %d)\n", bc.Title, stageName, index)
return
}
}
// Unmapped index: log it (debug only) so it can be added to the JSON.
debugf("\n=== UNMAPPED BLUE COIN COLLECTED ===\n")
debugf("Stage: %s | memory_index: %d\n", stageName, index)
debugf("-> Set \"memory_index\": %d on the coin you just grabbed in data/blue_coin.json\n", index)
debugf("====================================\n")
if debugEnabled {
logBlueCoinToFile(stageName, index)
}
}
func logBlueCoinToFile(stage string, index int) {
dir := "shines"
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.Mkdir(dir, 0755)
}
f, err := os.OpenFile(filepath.Join(dir, "bc_mapping_log.txt"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
debugf("Error opening blue coin log file: %v\n", err)
return
}
defer f.Close()
entry := fmt.Sprintf("[%s] UNMAPPED %s memory_index: %d\n", time.Now().Format("15:04:05"), stage, index)
if _, err := f.WriteString(entry); err != nil {
debugf("Error writing blue coin log: %v\n", err)
}
}
func handleShineCollection(stageName string, index int, tracker ShineTracker) {
bytePos := 0
bitPos := 0
// Check if the index is within the main BitOffsets (Episodes/Secrets/Plaza list)
if index < len(tracker.BitOffsets) {
bytePos = tracker.BitOffsets[index][0]
bitPos = tracker.BitOffsets[index][1]
} else if tracker.CoinOffset != nil {
// Only fallback to CoinOffset if we are BEYOND the main list
// (This covers the 11th shine in main stages)
bytePos = tracker.CoinOffset[0]
bitPos = tracker.CoinOffset[1]
} else {
// This should not happen if trackers are defined correctly
debugf("[Error] Index %d out of bounds for %s\n", index, stageName)
return
}
// 2. Resolve which shine this bit belongs to (by mem_byte/mem_bit).
zoneName, shineName, ok := lookupShine(bytePos, bitPos)
if ok {
// Known shine: log like blue coins.
debugf("[COLLECTED] Shine: %s - %s (%s - Byte 0x%02X Bit %d)\n",
zoneName, shineName, stageName, bytePos, bitPos)
return
}
// Unknown shine: log (debug only) for mapping.
debugf("\n--- Unknown SHINE COLLECTION DETECTED ---\n")
debugf("Stage: %s | Game Context: %s\n", stageName, dm.CurrentEpisode)
debugf("Memory: Offset 0x%02X | Bit %d\n", bytePos, bitPos)
debugf("----------------------------------\n")
if debugEnabled {
logShineToFile(stageName, "", bytePos, bitPos)
}
}
// lookupShine finds the zone and shine name for a given save mem_byte/mem_bit.
// Returns the first match (some shines share a bit across episodes).
func lookupShine(byteP, bitP int) (zoneName, shineName string, ok bool) {
for _, zone := range currentWorld.Zones {
for _, shine := range zone.ShinesAvailable {
if shine.MemByte == byteP && shine.MemBit == bitP {
return zone.Name, shine.Name, true
}
}
}
return "", "", false
}
func logShineToFile(stage, title string, b, bit int) {
// Ensure the directory exists
dir := "shines"
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.Mkdir(dir, 0755)
}
// We use one file per session to keep it clean, or one global file
filename := filepath.Join(dir, "mapping_log.txt")
// Open file in append mode (create if it doesn't exist)
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
debugf("Error opening log file: %v\n", err)
return
}
defer f.Close()
// Format the entry for easy copy-pasting into JSON
timestamp := time.Now().Format("15:04:05")
entry := fmt.Sprintf("[%s] %s (%s)\n", timestamp, title, stage)
entry += fmt.Sprintf(" \"mem_byte\": %d,\n", b)
entry += fmt.Sprintf(" \"mem_bit\": %d\n", bit)
entry += "------------------------------------------\n"
if _, err := f.WriteString(entry); err != nil {
debugf("Error writing to log: %v\n", err)
}
}
func getShineNameFromIndex(byte, bit int) string {
// go through zones and find one that has mem_byte and mem_bit matching the given values
for _, zone := range currentWorld.Zones {
for _, shine := range zone.ShinesAvailable {
if shine.MemByte == byte && shine.MemBit == bit {
return shine.Name
}
}
}
return ""
}