Browse Source

Load hotkeys from the requested save slot

Add a slot-aware LoadHotkeys overload and use it in player load paths so hero data and hotkeys are always read from the same save number.

This removes the saveNum/gSaveNumber mismatch risk in pfile_read_player_from_save while keeping the existing wrapper for current-player call sites.
pull/8507/head
morfidon 5 days ago
parent
commit
1033fc5b2d
  1. 15
      Source/loadsave.cpp
  2. 1
      Source/loadsave.h
  3. 2
      Source/pfile.cpp

15
Source/loadsave.cpp

@ -2331,13 +2331,20 @@ size_t HotkeysSize(size_t nHotkeys = NumHotkeys)
void LoadHotkeys()
{
LoadHelper file(OpenSaveArchive(gSaveNumber), "hotkeys");
if (MyPlayer == nullptr)
return;
LoadHotkeys(gSaveNumber, *MyPlayer);
}
void LoadHotkeys(uint32_t saveNum, Player &myPlayer)
{
LoadHelper file(OpenSaveArchive(saveNum), "hotkeys");
if (!file.IsValid()) {
SanitizePlayerSpellSelections(*MyPlayer);
SanitizePlayerSpellSelections(myPlayer);
return;
}
Player &myPlayer = *MyPlayer;
size_t nHotkeys = 4; // Defaults to old save format number
// Refill the spell arrays with no selection
@ -2524,7 +2531,7 @@ tl::expected<void, std::string> LoadGame(bool firstflag)
LoadPlayer(file, myPlayer);
ValidatePlayer();
CalcPlrInv(myPlayer, false);
LoadHotkeys();
LoadHotkeys(gSaveNumber, myPlayer);
myPlayer.queuedSpell.spellId = myPlayer._pRSpell;
myPlayer.queuedSpell.spellType = myPlayer._pRSplType;

1
Source/loadsave.h

@ -25,6 +25,7 @@ _item_indexes RemapItemIdxFromSpawn(_item_indexes i);
_item_indexes RemapItemIdxToSpawn(_item_indexes i);
bool IsHeaderValid(uint32_t magicNumber);
void LoadHotkeys();
void LoadHotkeys(uint32_t saveNum, Player &myPlayer);
void LoadHeroItems(Player &player);
/**
* @brief Remove invalid inventory items from the inventory grid

2
Source/pfile.cpp

@ -780,7 +780,7 @@ void pfile_read_player_from_save(uint32_t saveNum, Player &player)
RemoveAllInvalidItems(player);
CalcPlrInv(player, false);
if (&player == MyPlayer) {
LoadHotkeys();
LoadHotkeys(saveNum, player);
player.queuedSpell.spellId = player._pRSpell;
player.queuedSpell.spellType = player._pRSplType;
} else {

Loading…
Cancel
Save