From bd7ecb6b332bb19c9a2e720506cd5d376bde9113 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 31 Jul 2020 14:17:38 +0200 Subject: [PATCH] More work on exceptions. --- src/Battling/Flow/TurnHandler.cpp | 2 +- src/Battling/ScriptHandling/ScriptMacros.hpp | 12 ++++++++++-- src/Library/TypeLibrary.hpp | 8 +++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index 51f9477..f0814ef 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -155,7 +155,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo break; } auto& hit = hitIterator[hitIndex]; - uint8_t hitType = hit.GetType(); + uint8_t hitType = attack->GetAttack()->GetAttack()->GetType(); HOOK(ChangeAttackType, targetSource, attack, target.GetRaw(), hitIndex, &hitType); hit.SetType(hitType); auto effectiveness = library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes()); diff --git a/src/Battling/ScriptHandling/ScriptMacros.hpp b/src/Battling/ScriptHandling/ScriptMacros.hpp index fb9edaf..de1f53d 100644 --- a/src/Battling/ScriptHandling/ScriptMacros.hpp +++ b/src/Battling/ScriptHandling/ScriptMacros.hpp @@ -5,7 +5,11 @@ auto next = aggregator.GetNext(); \ if (next == nullptr) \ continue; \ - next->hookName(__VA_ARGS__); \ + try { \ + next->hookName(__VA_ARGS__); \ + } catch (const std::exception& e) { \ + THROW_CREATURE("Exception running script hook '" #hookName "': " << e.what()) \ + } \ } \ } @@ -16,6 +20,10 @@ auto next = aggregator.GetNext(); \ if (next == nullptr) \ continue; \ - next->hookName(__VA_ARGS__); \ + try { \ + next->hookName(__VA_ARGS__); \ + } catch (const std::exception& e) { \ + THROW_CREATURE("Exception running script hook '" #hookName "': " << e.what()) \ + } \ } \ } diff --git a/src/Library/TypeLibrary.hpp b/src/Library/TypeLibrary.hpp index 7bdd488..e946f83 100644 --- a/src/Library/TypeLibrary.hpp +++ b/src/Library/TypeLibrary.hpp @@ -7,6 +7,7 @@ #include #include #include +#include "Exceptions/CreatureException.hpp" namespace CreatureLib::Library { class TypeLibrary { @@ -19,7 +20,12 @@ namespace CreatureLib::Library { inline uint8_t GetTypeId(const ArbUt::BasicStringView& key) const { return _types.Get(key); } inline uint8_t GetTypeId(uint32_t s) const { return _types.Get(s); } [[nodiscard]] inline float GetSingleEffectiveness(uint8_t attacking, uint8_t defensive) const { - return _effectiveness[attacking][defensive]; + try { + return _effectiveness[attacking][defensive]; + } catch (const std::exception& e) { + THROW_CREATURE("Unknown type indices were requested for effectiveness: " + << (uint32_t)attacking << " and " << (uint32_t)defensive); + } } [[nodiscard]] inline float GetEffectiveness(uint8_t attacking, const std::unordered_set& defensive) const {