Fix unneeded runtime errors

This commit is contained in:
Aussiemon 2025-07-03 11:38:15 -06:00 committed by Aussiemon
parent 0e1ed078e9
commit d207c2251f
2 changed files with 60 additions and 36 deletions

View File

@ -4,6 +4,7 @@
#include <sstream> #include <sstream>
#include <components/compiler/locals.hpp> #include <components/compiler/locals.hpp>
#include <components/debug/debuglog.hpp>
#include <components/esm/records.hpp> #include <components/esm/records.hpp>
#include "../mwworld/esmstore.hpp" #include "../mwworld/esmstore.hpp"
@ -300,7 +301,15 @@ namespace MWScript
std::string_view InterpreterContext::getNPCFaction() const std::string_view InterpreterContext::getNPCFaction() const
{ {
const ESM::NPC* npc = getReferenceImp().get<ESM::NPC>()->mBase; const MWWorld::Ptr& ptr = getReferenceImp();
const ESM::RefId& factionId = ptr.getClass().getPrimaryFaction(ptr);
if (factionId.empty())
{
Log(Debug::Warning) << "getNPCFaction(): NPC is not in a faction";
return "%";
}
const ESM::NPC* npc = ptr.get<ESM::NPC>()->mBase;
const ESM::Faction* faction = MWBase::Environment::get().getESMStore()->get<ESM::Faction>().find(npc->mFaction); const ESM::Faction* faction = MWBase::Environment::get().getESMStore()->get<ESM::Faction>().find(npc->mFaction);
return faction->mName; return faction->mName;
} }
@ -310,7 +319,10 @@ namespace MWScript
const MWWorld::Ptr& ptr = getReferenceImp(); const MWWorld::Ptr& ptr = getReferenceImp();
const ESM::RefId& faction = ptr.getClass().getPrimaryFaction(ptr); const ESM::RefId& faction = ptr.getClass().getPrimaryFaction(ptr);
if (faction.empty()) if (faction.empty())
throw std::runtime_error("getNPCRank(): NPC is not in a faction"); {
Log(Debug::Warning) << "getNPCRank(): NPC is not in a faction";
return "%";
}
int rank = ptr.getClass().getPrimaryFactionRank(ptr); int rank = ptr.getClass().getPrimaryFactionRank(ptr);
if (rank < 0 || rank > 9) if (rank < 0 || rank > 9)
@ -349,7 +361,10 @@ namespace MWScript
const ESM::RefId& factionId = getReferenceImp().getClass().getPrimaryFaction(getReferenceImp()); const ESM::RefId& factionId = getReferenceImp().getClass().getPrimaryFaction(getReferenceImp());
if (factionId.empty()) if (factionId.empty())
throw std::runtime_error("getPCRank(): NPC is not in a faction"); {
Log(Debug::Warning) << "getPCRank(): NPC is not in a faction";
return "%";
}
const auto& ranks = player.getClass().getNpcStats(player).getFactionRanks(); const auto& ranks = player.getClass().getNpcStats(player).getFactionRanks();
auto it = ranks.find(factionId); auto it = ranks.find(factionId);
@ -378,7 +393,10 @@ namespace MWScript
const ESM::RefId& factionId = getReferenceImp().getClass().getPrimaryFaction(getReferenceImp()); const ESM::RefId& factionId = getReferenceImp().getClass().getPrimaryFaction(getReferenceImp());
if (factionId.empty()) if (factionId.empty())
throw std::runtime_error("getPCNextRank(): NPC is not in a faction"); {
Log(Debug::Warning) << "getPCNextRank(): NPC is not in a faction";
return "%";
}
const auto& ranks = player.getClass().getNpcStats(player).getFactionRanks(); const auto& ranks = player.getClass().getNpcStats(player).getFactionRanks();
auto it = ranks.find(factionId); auto it = ranks.find(factionId);

View File

@ -26,40 +26,40 @@ namespace
std::vector<std::string> globals; std::vector<std::string> globals;
const std::initializer_list<std::tuple<std::string_view, std::string_view>> sActionBindings{ const std::initializer_list<std::tuple<std::string_view, std::string_view>> sActionBindings{
{ "actionslideright", "#{sRight}" }, { "ActionSlideRight", "#{sRight}" },
{ "actionreadymagic", "#{sReady_Magic}" }, { "ActionReadyMagic", "#{sReady_Magic}" },
{ "actionprevweapon", "#{sPrevWeapon}" }, { "ActionPrevWeapon", "#{sPrevWeapon}" },
{ "actionnextweapon", "#{sNextWeapon}" }, { "ActionNextWeapon", "#{sNextWeapon}" },
{ "actiontogglerun", "#{sAuto_Run}" }, { "ActionToggleRun", "#{sAuto_Run}" },
{ "actionslideleft", "#{sLeft}" }, { "ActionSlideLeft", "#{sLeft}" },
{ "actionreadyitem", "#{sReady_Weapon}" }, { "ActionReadyItem", "#{sReady_Weapon}" },
{ "actionprevspell", "#{sPrevSpell}" }, { "ActionPrevSpell", "#{sPrevSpell}" },
{ "actionnextspell", "#{sNextSpell}" }, { "ActionNextSpell", "#{sNextSpell}" },
{ "actionrestmenu", "#{sRestKey}" }, { "ActionRestMenu", "#{sRestKey}" },
{ "actionmenumode", "#{sInventory}" }, { "ActionMenuMode", "#{sInventory}" },
{ "actionactivate", "#{sActivate}" }, { "ActionActivate", "#{sActivate}" },
{ "actionjournal", "#{sJournal}" }, { "ActionJournal", "#{sJournal}" },
{ "actionforward", "#{sForward}" }, { "ActionForward", "#{sForward}" },
{ "actioncrouch", "#{sCrouch_Sneak}" }, { "ActionCrouch", "#{sCrouch_Sneak}" },
{ "actionjump", "#{sJump}" }, { "ActionJump", "#{sJump}" },
{ "actionback", "#{sBack}" }, { "ActionBack", "#{sBack}" },
{ "actionuse", "#{sUse}" }, { "ActionUse", "#{sUse}" },
{ "actionrun", "#{sRun}" }, { "ActionRun", "#{sRun}" },
}; };
using ContextMethod = std::string_view (Interpreter::Context::*)() const; using ContextMethod = std::string_view (Interpreter::Context::*)() const;
const std::initializer_list<std::tuple<std::string_view, std::pair<ContextMethod, ContextMethod>>> sContextMethods{ const std::initializer_list<std::tuple<std::string_view, std::pair<ContextMethod, ContextMethod>>> sContextMethods{
{ "nextpcrank", { &Interpreter::Context::getPCNextRank, nullptr } }, { "NextPCRank", { &Interpreter::Context::getPCNextRank, nullptr } },
{ "pcnextrank", { &Interpreter::Context::getPCNextRank, nullptr } }, { "PCNextRank", { &Interpreter::Context::getPCNextRank, nullptr } },
{ "faction", { &Interpreter::Context::getNPCFaction, nullptr } }, { "Faction", { &Interpreter::Context::getNPCFaction, nullptr } },
{ "pcclass", { &Interpreter::Context::getPCClass, &Interpreter::Context::getPCClass } }, { "PCClass", { &Interpreter::Context::getPCClass, &Interpreter::Context::getPCClass } },
{ "pcname", { &Interpreter::Context::getPCName, &Interpreter::Context::getPCName } }, { "PCName", { &Interpreter::Context::getPCName, &Interpreter::Context::getPCName } },
{ "pcrace", { &Interpreter::Context::getPCRace, &Interpreter::Context::getPCRace } }, { "PCRace", { &Interpreter::Context::getPCRace, &Interpreter::Context::getPCRace } },
{ "pcrank", { &Interpreter::Context::getPCRank, nullptr } }, { "PCRank", { &Interpreter::Context::getPCRank, nullptr } },
{ "class", { &Interpreter::Context::getNPCClass, &Interpreter::Context::getPCClass } }, { "Class", { &Interpreter::Context::getNPCClass, &Interpreter::Context::getPCClass } },
{ "cell", { &Interpreter::Context::getCurrentCellName, &Interpreter::Context::getCurrentCellName } }, { "Cell", { &Interpreter::Context::getCurrentCellName, &Interpreter::Context::getCurrentCellName } },
{ "race", { &Interpreter::Context::getNPCRace, &Interpreter::Context::getPCRace } }, { "Race", { &Interpreter::Context::getNPCRace, &Interpreter::Context::getPCRace } },
{ "rank", { &Interpreter::Context::getNPCRank, nullptr } }, { "Rank", { &Interpreter::Context::getNPCRank, nullptr } },
{ "name", { &Interpreter::Context::getActorName, &Interpreter::Context::getPCName } }, { "Name", { &Interpreter::Context::getActorName, &Interpreter::Context::getPCName } },
}; };
bool longerStr(std::string_view a, std::string_view b) bool longerStr(std::string_view a, std::string_view b)
@ -78,7 +78,7 @@ namespace
return true; return true;
} }
} }
if (check(temp, "pccrimelevel", i, start)) if (check(temp, "PCCrimeLevel", i, start))
{ {
retval << context.getPCBounty(); retval << context.getPCBounty();
return true; return true;
@ -89,7 +89,13 @@ namespace
if (check(temp, name, i, start)) if (check(temp, name, i, start))
{ {
if (method) // Not all variables are available outside of dialogue if (method) // Not all variables are available outside of dialogue
{
retval << (context.*method)(); retval << (context.*method)();
// Re-add the token if replacement failed without an error
if ((context.*method)() == "%")
retval << name;
}
return true; return true;
} }
} }