Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh S/V MO routine memory addresses #121

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions SysBot.Pokemon/SV/BotEncounter/EncounterBotOverworldScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class EncounterBotOverworldScanner(PokeBotState cfg, PokeTradeHub<PK9> hu

protected override async Task EncounterLoop(SAV9SV sav, CancellationToken token)
{
_saveKeyInitialized = false;
_baseBlockKeyPointer = await SwitchConnection.PointerAll(Offsets.BlockKeyPointer, token).ConfigureAwait(false);
_previous.Clear();

Expand Down Expand Up @@ -161,16 +162,16 @@ private async Task<bool> DoMassOutbreakResetting(CancellationToken token)
async Task<bool> 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;

Expand Down Expand Up @@ -210,17 +211,17 @@ private async Task<bool> DoKOCounting(CancellationToken token)
async Task<bool> 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)
{
Expand Down
52 changes: 40 additions & 12 deletions SysBot.Pokemon/SV/PokeRoutineExecutor9SV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,16 +420,26 @@ public async Task<byte[]> ReadEncryptedBlock(ulong baseBlock, uint blockKey, boo
}

private readonly Dictionary<uint, ulong> _cacheBlockArrays = new();
public async Task<byte[]> ReadEncryptedBlockArray(ulong baseBlock, uint blockKey, int blockSize, CancellationToken token)
public async Task<byte[]> 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);
Expand All @@ -439,16 +449,25 @@ public async Task<byte[]> ReadEncryptedBlockArray(ulong baseBlock, uint blockKey
}

private readonly Dictionary<uint, ulong> _cacheBlockUint32s = new();
public async Task<uint> ReadEncryptedBlockUInt32(ulong baseBlock, uint blockKey, CancellationToken token)
public async Task<uint> 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);
Expand All @@ -458,16 +477,25 @@ public async Task<uint> ReadEncryptedBlockUInt32(ulong baseBlock, uint blockKey,
}

private readonly Dictionary<uint, ulong> _cacheBlockBytes = new();
public async Task<byte> ReadEncryptedBlockByte(ulong baseBlock, uint blockKey, CancellationToken token)
public async Task<byte> 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);
Expand Down
Loading