diff --git a/src/game_switches.cpp b/src/game_switches.cpp index a68bb29918..ecb2b52779 100644 --- a/src/game_switches.cpp +++ b/src/game_switches.cpp @@ -21,6 +21,8 @@ #include "output.h" #include "reader_util.h" +constexpr int kMaxWarnings = 10; + Game_Switches_Class::Game_Switches_Class() {} static std::vector& switches() { @@ -28,6 +30,10 @@ static std::vector& switches() { } bool Game_Switches_Class::Get(int switch_id) const { + if ((switch_id <= 0 || switch_id > Data::switches.size()) && _warnings < kMaxWarnings) { + Output::Debug("Invalid read sw[%d]!", switch_id); + ++_warnings; + } auto& sv = switches(); if (switch_id <= 0 || switch_id > sv.size()) { return false; @@ -36,6 +42,10 @@ bool Game_Switches_Class::Get(int switch_id) const { } void Game_Switches_Class::Set(int switch_id, bool value) { + if ((switch_id <= 0 || switch_id > Data::switches.size()) && _warnings < kMaxWarnings) { + Output::Debug("Invalid write sw[%d] = %d!", switch_id, value); + ++_warnings; + } auto& sv = switches(); if (switch_id <= 0) { return; @@ -71,4 +81,5 @@ int Game_Switches_Class::GetSize() const { void Game_Switches_Class::Reset() { switches().clear(); + _warnings = 0; } diff --git a/src/game_switches.h b/src/game_switches.h index 327a532884..32cbc6d659 100644 --- a/src/game_switches.h +++ b/src/game_switches.h @@ -44,7 +44,7 @@ class Game_Switches_Class { void Reset(); private: - std::vector dummy; + mutable int _warnings = 0; }; diff --git a/src/game_variables.cpp b/src/game_variables.cpp index b87f51b6a0..10bf648f35 100644 --- a/src/game_variables.cpp +++ b/src/game_variables.cpp @@ -22,6 +22,8 @@ #include "player.h" #include "reader_util.h" +constexpr int kMaxWarnings = 10; + Game_Variables_Class::Game_Variables_Class() {} static std::vector& variables() { @@ -29,6 +31,10 @@ static std::vector& variables() { } int Game_Variables_Class::Get(int variable_id) const { + if ((variable_id <= 0 || variable_id > Data::variables.size()) && _warnings < kMaxWarnings) { + Output::Debug("Invalid read var[%d]!", variable_id); + ++_warnings; + } auto& vv = variables(); if (variable_id <= 0 || variable_id > vv.size()) { return 0; @@ -37,6 +43,10 @@ int Game_Variables_Class::Get(int variable_id) const { } void Game_Variables_Class::Set(int variable_id, int value) { + if ((variable_id <= 0 || variable_id > Data::variables.size()) && _warnings < kMaxWarnings) { + Output::Debug("Invalid write var[%d] = %d!", variable_id, value); + ++_warnings; + } auto& vv = variables(); if (variable_id <= 0) { return; @@ -70,4 +80,5 @@ int Game_Variables_Class::GetSize() const { void Game_Variables_Class::Reset() { variables().clear(); + _warnings = 0; } diff --git a/src/game_variables.h b/src/game_variables.h index 85526ea880..296644089f 100644 --- a/src/game_variables.h +++ b/src/game_variables.h @@ -40,6 +40,8 @@ class Game_Variables_Class { int GetSize() const; void Reset(); +private: + mutable int _warnings = 0; }; // Global variable