Skip to content

Commit

Permalink
Add debug logging when reading/writing to invalid switches and vars
Browse files Browse the repository at this point in the history
mateofio committed Nov 4, 2018
1 parent 58bef92 commit 5ec72a4
Showing 4 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/game_switches.cpp
Original file line number Diff line number Diff line change
@@ -21,13 +21,19 @@
#include "output.h"
#include "reader_util.h"

constexpr int kMaxWarnings = 10;

Game_Switches_Class::Game_Switches_Class() {}

static std::vector<bool>& switches() {
return Main_Data::game_data.system.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;
}
2 changes: 1 addition & 1 deletion src/game_switches.h
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ class Game_Switches_Class {
void Reset();

private:
std::vector<bool> dummy;
mutable int _warnings = 0;
};


11 changes: 11 additions & 0 deletions src/game_variables.cpp
Original file line number Diff line number Diff line change
@@ -22,13 +22,19 @@
#include "player.h"
#include "reader_util.h"

constexpr int kMaxWarnings = 10;

Game_Variables_Class::Game_Variables_Class() {}

static std::vector<int32_t>& variables() {
return Main_Data::game_data.system.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;
}
2 changes: 2 additions & 0 deletions src/game_variables.h
Original file line number Diff line number Diff line change
@@ -40,6 +40,8 @@ class Game_Variables_Class {
int GetSize() const;

void Reset();
private:
mutable int _warnings = 0;
};

// Global variable

0 comments on commit 5ec72a4

Please sign in to comment.