From 1ccac188bfbfba5a3dcb9d182aa05ca64034b8b4 Mon Sep 17 00:00:00 2001
From: Ghabry <gabriel+github@mastergk.de>
Date: Mon, 4 Nov 2024 00:18:31 +0100
Subject: [PATCH] DynRPG: Fix unit tests

---
 src/dynrpg_easyrpg.cpp  | 2 +-
 src/dynrpg_easyrpg.h    | 2 +-
 src/dynrpg_textplugin.h | 2 +-
 src/game_dynrpg.cpp     | 4 ++--
 src/game_dynrpg.h       | 5 ++++-
 tests/dynrpg.cpp        | 2 ++
 tests/test_mock_actor.h | 6 +++---
 7 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/src/dynrpg_easyrpg.cpp b/src/dynrpg_easyrpg.cpp
index e13821a721..1767be3507 100644
--- a/src/dynrpg_easyrpg.cpp
+++ b/src/dynrpg_easyrpg.cpp
@@ -58,7 +58,7 @@ bool DynRpg::EasyRpgPlugin::EasyCall(dyn_arg_list args, bool& do_yield, Game_Int
 		return true;
 	}
 
-	for (auto& plugin: Main_Data::game_dynrpg->plugins) {
+	for (auto& plugin: instance.plugins) {
 		if (plugin->Invoke(func_name, args.subspan(1), do_yield, interpreter)) {
 			return true;
 		}
diff --git a/src/dynrpg_easyrpg.h b/src/dynrpg_easyrpg.h
index b83b4defb3..6e2d9fe05b 100644
--- a/src/dynrpg_easyrpg.h
+++ b/src/dynrpg_easyrpg.h
@@ -29,7 +29,7 @@ namespace DynRpg {
 	 */
 	class EasyRpgPlugin : public DynRpgPlugin {
 	public:
-		EasyRpgPlugin() : DynRpgPlugin("EasyRpgPlugin") {}
+		EasyRpgPlugin(Game_DynRpg& instance) : DynRpgPlugin("EasyRpgPlugin", instance) {}
 
 		bool Invoke(StringView func, dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) override;
 		void Load(const std::vector<uint8_t>& buffer) override;
diff --git a/src/dynrpg_textplugin.h b/src/dynrpg_textplugin.h
index 2c21b30ad9..4a2f165c02 100644
--- a/src/dynrpg_textplugin.h
+++ b/src/dynrpg_textplugin.h
@@ -23,7 +23,7 @@
 namespace DynRpg {
 	class TextPlugin : public DynRpgPlugin {
 	public:
-		TextPlugin() : DynRpgPlugin("DynTextPlugin") {}
+		TextPlugin(Game_DynRpg& instance) : DynRpgPlugin("DynTextPlugin", instance) {}
 		~TextPlugin() override;
 
 		bool Invoke(StringView func, dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) override;
diff --git a/src/game_dynrpg.cpp b/src/game_dynrpg.cpp
index 3ef34ab7a0..6610808401 100644
--- a/src/game_dynrpg.cpp
+++ b/src/game_dynrpg.cpp
@@ -180,11 +180,11 @@ void Game_DynRpg::InitPlugins() {
 	}
 
 	if (Player::IsPatchDynRpg() || Player::HasEasyRpgExtensions()) {
-		plugins.emplace_back(new DynRpg::EasyRpgPlugin());
+		plugins.emplace_back(new DynRpg::EasyRpgPlugin(*this));
 	}
 
 	if (Player::IsPatchDynRpg()) {
-		plugins.emplace_back(new DynRpg::TextPlugin());
+		plugins.emplace_back(new DynRpg::TextPlugin(*this));
 	}
 
 	plugins_loaded = true;
diff --git a/src/game_dynrpg.h b/src/game_dynrpg.h
index e1ed92cc8d..591cf58e9c 100644
--- a/src/game_dynrpg.h
+++ b/src/game_dynrpg.h
@@ -154,7 +154,7 @@ class Game_DynRpg {
 /** Base class for implementing a DynRpg Plugins */
 class DynRpgPlugin {
 public:
-	explicit DynRpgPlugin(std::string identifier) : identifier(std::move(identifier)) {}
+	explicit DynRpgPlugin(std::string identifier, Game_DynRpg& instance) : instance(instance), identifier(std::move(identifier)) {}
 	DynRpgPlugin() = delete;
 	virtual ~DynRpgPlugin() = default;
 
@@ -164,6 +164,9 @@ class DynRpgPlugin {
 	virtual void Load(const std::vector<uint8_t>&) {}
 	virtual std::vector<uint8_t> Save() { return {}; }
 
+protected:
+	Game_DynRpg& instance;
+
 private:
 	std::string identifier;
 };
diff --git a/tests/dynrpg.cpp b/tests/dynrpg.cpp
index 6e70c2be29..0927d40683 100644
--- a/tests/dynrpg.cpp
+++ b/tests/dynrpg.cpp
@@ -151,6 +151,8 @@ TEST_CASE("easyrpg dynrpg invoke") {
 	Game_DynRpg dyn;
 	const MockActor m;
 
+	Player::game_config.patch_dynrpg.Set(true);
+
 	std::vector<int32_t> vars = {-1};
 	Main_Data::game_variables->SetData(vars);
 	Main_Data::game_variables->SetWarning(0);
diff --git a/tests/test_mock_actor.h b/tests/test_mock_actor.h
index cd5b393711..963a2149ed 100644
--- a/tests/test_mock_actor.h
+++ b/tests/test_mock_actor.h
@@ -78,7 +78,7 @@ class MockActor {
 public:
 	MockActor(int eng = Player::EngineRpg2k3 | Player::EngineEnglish)
 	{
-		_engine = Player::game_config.engine;
+		_config = Player::game_config;
 		Player::game_config.engine = eng;
 
 		InitEmptyDB();
@@ -100,10 +100,10 @@ class MockActor {
 		Main_Data::Cleanup();
 
 		lcf::Data::data = {};
-		Player::game_config.engine = _engine;
+		Player::game_config = _config;
 	}
 private:
-	int _engine = {};
+	Game_ConfigGame _config = {};
 };
 
 struct MockBattle : public MockActor {