Browse Source

access: add null-safety checks for MyPlayer in accessibility code

Guard against MyPlayer being nullptr in IsPlayerDead(),
CycleSpellHotkeys(), RefreshTownNpcOrder(), SpeakSelectedTownNpc(), and
ListTownNpcsKeyPressed() to prevent crashes during early init or after
disconnect. Also reset AutoWalkTrackerTargetId when MyPlayer is null in
the auto-walk tracker to prevent the walk loop from retrying endlessly.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
pull/8474/head
hidwood 2 months ago
parent
commit
4db589cfc2
  1. 4
      Source/controls/accessibility_keys.cpp
  2. 6
      Source/controls/town_npc_nav.cpp
  3. 2
      Source/controls/tracker.cpp

4
Source/controls/accessibility_keys.cpp

@ -263,6 +263,8 @@ void SpellBookKeyPressed()
void CycleSpellHotkeys(bool next)
{
if (MyPlayer == nullptr)
return;
StaticVector<size_t, NumHotkeys> validHotKeyIndexes;
std::optional<size_t> currentIndex;
for (size_t slot = 0; slot < NumHotkeys; slot++) {
@ -290,6 +292,8 @@ void CycleSpellHotkeys(bool next)
bool IsPlayerDead()
{
if (MyPlayer == nullptr)
return true;
return MyPlayer->_pmode == PM_DEATH || MyPlayerIsDead;
}

6
Source/controls/town_npc_nav.cpp

@ -50,6 +50,8 @@ void RefreshTownNpcOrder(bool selectFirst = false)
TownNpcOrder.clear();
if (leveltype != DTYPE_TOWN)
return;
if (MyPlayer == nullptr)
return;
const Point playerPosition = MyPlayer->position.future;
@ -147,6 +149,8 @@ void SpeakSelectedTownNpc()
SpeakText(_("No NPC selected."), true);
return;
}
if (MyPlayer == nullptr)
return;
const Towner &towner = Towners[SelectedTownNpc];
const Point playerPosition = MyPlayer->position.future;
@ -277,6 +281,8 @@ void ListTownNpcsKeyPressed()
townNpcs.reserve(Towners.size());
cows.reserve(Towners.size());
if (MyPlayer == nullptr)
return;
const Point playerPosition = MyPlayer->position.future;

2
Source/controls/tracker.cpp

@ -1470,6 +1470,7 @@ void AutoWalkToTrackerTargetKeyPressed()
return;
}
if (MyPlayer == nullptr) {
AutoWalkTrackerTargetId = -1;
SpeakText(_("Cannot walk right now."), true);
return;
}
@ -1592,6 +1593,7 @@ void UpdateAutoWalkTracker()
return;
if (MyPlayer == nullptr) {
AutoWalkTrackerTargetId = -1;
SpeakText(_("Cannot walk right now."), true);
return;
}

Loading…
Cancel
Save