From e435af679c770a708e65d4047077c285faf5ebee Mon Sep 17 00:00:00 2001 From: Eppin <96589679+Eppin@users.noreply.github.com> Date: Fri, 15 Nov 2024 11:41:08 +0100 Subject: [PATCH] Refresh S/V MO routine memory addresses --- .../EncounterBotOverworldScanner.cs | 15 +++--- SysBot.Pokemon/SV/PokeRoutineExecutor9SV.cs | 52 ++++++++++++++----- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/SysBot.Pokemon/SV/BotEncounter/EncounterBotOverworldScanner.cs b/SysBot.Pokemon/SV/BotEncounter/EncounterBotOverworldScanner.cs index d81ff5b4..3ec22d0f 100644 --- a/SysBot.Pokemon/SV/BotEncounter/EncounterBotOverworldScanner.cs +++ b/SysBot.Pokemon/SV/BotEncounter/EncounterBotOverworldScanner.cs @@ -19,6 +19,7 @@ public class EncounterBotOverworldScanner(PokeBotState cfg, PokeTradeHub hu protected override async Task EncounterLoop(SAV9SV sav, CancellationToken token) { + _saveKeyInitialized = false; _baseBlockKeyPointer = await SwitchConnection.PointerAll(Offsets.BlockKeyPointer, token).ConfigureAwait(false); _previous.Clear(); @@ -161,16 +162,16 @@ private async Task DoMassOutbreakResetting(CancellationToken token) async Task Scan(int? dlc = null) { var middle = dlc == null ? "Main" : $"DLC{dlc}"; - var activeSize = await ReadEncryptedBlockByte(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak{middle}NumActive"], token).ConfigureAwait(false); + var activeSize = await ReadEncryptedBlockByte(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak{middle}NumActive"], !_saveKeyInitialized, token).ConfigureAwait(false); Log($"Scan first {activeSize} outbreaks in {middle} map"); for (var i = 1; i <= activeSize; i++) { - var speciesData = await ReadEncryptedBlockUInt32(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak0{i}{middle}Species"], token).ConfigureAwait(false); + var speciesData = await ReadEncryptedBlockUInt32(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak0{i}{middle}Species"], !_saveKeyInitialized, token).ConfigureAwait(false); var species = (Species)SpeciesConverter.GetNational9((ushort)speciesData); - var form = await ReadEncryptedBlockByte(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak0{i}{middle}Form"], token).ConfigureAwait(false); + var form = await ReadEncryptedBlockByte(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak0{i}{middle}Form"], !_saveKeyInitialized, token).ConfigureAwait(false); var searchConditions = Hub.Config.EncounterSV.MassOutbreakSearchConditions; @@ -210,17 +211,17 @@ private async Task DoKOCounting(CancellationToken token) async Task Scan(int? dlc = null) { var middle = dlc == null ? "Main" : $"DLC{dlc}"; - var activeSize = await ReadEncryptedBlockByte(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak{middle}NumActive"], token).ConfigureAwait(false); + var activeSize = await ReadEncryptedBlockByte(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak{middle}NumActive"], !_saveKeyInitialized, token).ConfigureAwait(false); var displayed = false; for (var i = 1; i <= activeSize; i++) { - var speciesData = await ReadEncryptedBlockUInt32(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak0{i}{middle}Species"], token).ConfigureAwait(false); + var speciesData = await ReadEncryptedBlockUInt32(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak0{i}{middle}Species"], !_saveKeyInitialized, token).ConfigureAwait(false); var species = (Species)SpeciesConverter.GetNational9((ushort)speciesData); - var form = await ReadEncryptedBlockByte(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak0{i}{middle}Form"], token).ConfigureAwait(false); + var form = await ReadEncryptedBlockByte(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak0{i}{middle}Form"], !_saveKeyInitialized, token).ConfigureAwait(false); - var koCount = await ReadEncryptedBlockByte(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak0{i}{middle}NumKOed"], token).ConfigureAwait(false); + var koCount = await ReadEncryptedBlockByte(_baseBlockKeyPointer, _massOutbreakBlocks[$"KOutbreak0{i}{middle}NumKOed"], !_saveKeyInitialized, token).ConfigureAwait(false); if (koCount > 0) { diff --git a/SysBot.Pokemon/SV/PokeRoutineExecutor9SV.cs b/SysBot.Pokemon/SV/PokeRoutineExecutor9SV.cs index a7943cbc..8fbf8af4 100644 --- a/SysBot.Pokemon/SV/PokeRoutineExecutor9SV.cs +++ b/SysBot.Pokemon/SV/PokeRoutineExecutor9SV.cs @@ -420,16 +420,26 @@ public async Task ReadEncryptedBlock(ulong baseBlock, uint blockKey, boo } private readonly Dictionary _cacheBlockArrays = new(); - public async Task ReadEncryptedBlockArray(ulong baseBlock, uint blockKey, int blockSize, CancellationToken token) + public async Task ReadEncryptedBlockArray(ulong baseBlock, uint blockKey, int blockSize, bool init, CancellationToken token) { - if (!_cacheBlockArrays.TryGetValue(blockKey, out var cachedAddress)) + var exists = _cacheBlockArrays.TryGetValue(blockKey, out var cachedAddress); + if (init || !exists) { var address = await SearchSaveKey(baseBlock, blockKey, token).ConfigureAwait(false); address = BitConverter.ToUInt64(await SwitchConnection.ReadBytesAbsoluteAsync(address + 8, 0x8, token).ConfigureAwait(false), 0); cachedAddress = address; - _cacheBlockArrays.Add(blockKey, cachedAddress); - Log($"Initial address for {blockKey:X8} found at {cachedAddress:X8}"); + if (exists) + { + _cacheBlockArrays[blockKey] = cachedAddress; + Log($"Refreshed address for {blockKey:X8} found at {cachedAddress:X8}"); + } + else + { + _cacheBlockArrays.Add(blockKey, cachedAddress); + Log($"Initial address for {blockKey:X8} found at {cachedAddress:X8}"); + } + } var data = await SwitchConnection.ReadBytesAbsoluteAsync(cachedAddress, 6 + blockSize, token).ConfigureAwait(false); @@ -439,16 +449,25 @@ public async Task ReadEncryptedBlockArray(ulong baseBlock, uint blockKey } private readonly Dictionary _cacheBlockUint32s = new(); - public async Task ReadEncryptedBlockUInt32(ulong baseBlock, uint blockKey, CancellationToken token) + public async Task ReadEncryptedBlockUInt32(ulong baseBlock, uint blockKey, bool init, CancellationToken token) { - if (!_cacheBlockUint32s.TryGetValue(blockKey, out var cachedAddress)) + var exists = _cacheBlockUint32s.TryGetValue(blockKey, out var cachedAddress); + if (init || !exists) { var address = await SearchSaveKey(baseBlock, blockKey, token).ConfigureAwait(false); address = BitConverter.ToUInt64(await SwitchConnection.ReadBytesAbsoluteAsync(address + 8, 0x8, token).ConfigureAwait(false), 0); cachedAddress = address; - _cacheBlockUint32s.Add(blockKey, cachedAddress); - Log($"Initial address for {blockKey:X8} found at {cachedAddress:X8}"); + if (exists) + { + _cacheBlockUint32s[blockKey] = cachedAddress; + Log($"Refreshed address for {blockKey:X8} found at {cachedAddress:X8}"); + } + else + { + _cacheBlockUint32s.Add(blockKey, cachedAddress); + Log($"Initial address for {blockKey:X8} found at {cachedAddress:X8}"); + } } var header = await SwitchConnection.ReadBytesAbsoluteAsync(cachedAddress, 5, token).ConfigureAwait(false); @@ -458,16 +477,25 @@ public async Task ReadEncryptedBlockUInt32(ulong baseBlock, uint blockKey, } private readonly Dictionary _cacheBlockBytes = new(); - public async Task ReadEncryptedBlockByte(ulong baseBlock, uint blockKey, CancellationToken token) + public async Task ReadEncryptedBlockByte(ulong baseBlock, uint blockKey, bool init, CancellationToken token) { - if (!_cacheBlockBytes.TryGetValue(blockKey, out var cachedAddress)) + var exists = _cacheBlockBytes.TryGetValue(blockKey, out var cachedAddress); + if (init || !exists) { var address = await SearchSaveKey(baseBlock, blockKey, token).ConfigureAwait(false); address = BitConverter.ToUInt64(await SwitchConnection.ReadBytesAbsoluteAsync(address + 8, 0x8, token).ConfigureAwait(false), 0); cachedAddress = address; - _cacheBlockBytes.Add(blockKey, cachedAddress); - Log($"Initial address for {blockKey:X8} found at {cachedAddress:X8}"); + if (exists) + { + _cacheBlockBytes[blockKey] = cachedAddress; + Log($"Refreshed address for {blockKey:X8} found at {cachedAddress:X8}"); + } + else + { + _cacheBlockBytes.Add(blockKey, cachedAddress); + Log($"Initial address for {blockKey:X8} found at {cachedAddress:X8}"); + } } var header = await SwitchConnection.ReadBytesAbsoluteAsync(cachedAddress, 5, token).ConfigureAwait(false);