Skip to content

Commit 99cb093

Browse files
authored
Merge pull request #3288 from Ghabry/dynrpg-refactor
Refactor Dynrpg namespace into a class. Whitelist @easyrpg_ commands
2 parents 6331bb8 + 1ccac18 commit 99cb093

17 files changed

+204
-162
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ add_library(${PROJECT_NAME} OBJECT
102102
src/drawable_list.h
103103
src/drawable_mgr.cpp
104104
src/drawable_mgr.h
105-
src/dynrpg.cpp
106-
src/dynrpg.h
107105
src/dynrpg_easyrpg.cpp
108106
src/dynrpg_easyrpg.h
109107
src/dynrpg_textplugin.cpp
@@ -163,6 +161,8 @@ add_library(${PROJECT_NAME} OBJECT
163161
src/game_config.h
164162
src/game_config_game.cpp
165163
src/game_config_game.h
164+
src/game_dynrpg.cpp
165+
src/game_dynrpg.h
166166
src/game_enemy.cpp
167167
src/game_enemy.h
168168
src/game_enemyparty.cpp

Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ libeasyrpg_player_a_SOURCES = \
8282
src/drawable_list.h \
8383
src/drawable_mgr.cpp \
8484
src/drawable_mgr.h \
85-
src/dynrpg.cpp \
86-
src/dynrpg.h \
8785
src/dynrpg_easyrpg.cpp \
8886
src/dynrpg_easyrpg.h \
8987
src/dynrpg_textplugin.h \
@@ -143,6 +141,8 @@ libeasyrpg_player_a_SOURCES = \
143141
src/game_config.h \
144142
src/game_config_game.cpp \
145143
src/game_config_game.h \
144+
src/game_dynrpg.cpp \
145+
src/game_dynrpg.h \
146146
src/game_enemy.cpp \
147147
src/game_enemy.h \
148148
src/game_enemyparty.cpp \

src/dynrpg_easyrpg.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,23 @@ static bool EasyOput(dyn_arg_list args) {
4848
return true;
4949
}
5050

51-
static bool EasyCall(dyn_arg_list args) {
52-
auto token = std::get<0>(DynRpg::ParseArgs<std::string>("call", args));
51+
bool DynRpg::EasyRpgPlugin::EasyCall(dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) {
52+
auto func_name = std::get<0>(DynRpg::ParseArgs<std::string>("call", args));
5353

54-
if (token.empty()) {
54+
if (func_name.empty()) {
5555
// empty function name
5656
Output::Warning("call: Empty RPGSS function name");
5757

5858
return true;
5959
}
6060

61-
if (!DynRpg::HasFunction(token)) {
62-
// Not a supported function
63-
Output::Warning("Unsupported RPGSS function: {}", token);
64-
return true;
61+
for (auto& plugin: instance.plugins) {
62+
if (plugin->Invoke(func_name, args.subspan(1), do_yield, interpreter)) {
63+
return true;
64+
}
6565
}
6666

67-
return DynRpg::Invoke(token, args.subspan(1));
67+
return false;
6868
}
6969

7070
static bool EasyAdd(dyn_arg_list args) {
@@ -88,10 +88,15 @@ static bool EasyAdd(dyn_arg_list args) {
8888
return true;
8989
}
9090

91-
void DynRpg::EasyRpgPlugin::RegisterFunctions() {
92-
DynRpg::RegisterFunction("call", EasyCall);
93-
DynRpg::RegisterFunction("easyrpg_output", EasyOput);
94-
DynRpg::RegisterFunction("easyrpg_add", EasyAdd);
91+
bool DynRpg::EasyRpgPlugin::Invoke(StringView func, dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) {
92+
if (func == "call") {
93+
return EasyCall(args, do_yield, interpreter);
94+
} else if (func == "easyrpg_output") {
95+
return EasyOput(args);
96+
} else if (func == "easyrpg_add") {
97+
return EasyAdd(args);
98+
}
99+
return false;
95100
}
96101

97102
void DynRpg::EasyRpgPlugin::Load(const std::vector<uint8_t>& buffer) {

src/dynrpg_easyrpg.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#ifndef EP_DYNRPG_EASYRPG_H
1919
#define EP_DYNRPG_EASYRPG_H
2020

21-
#include "dynrpg.h"
21+
#include "game_dynrpg.h"
22+
#include "game_battle.h"
23+
#include "game_map.h"
2224

2325
namespace DynRpg {
2426
/**
@@ -27,11 +29,14 @@ namespace DynRpg {
2729
*/
2830
class EasyRpgPlugin : public DynRpgPlugin {
2931
public:
30-
EasyRpgPlugin() : DynRpgPlugin("EasyRpgPlugin") {}
32+
EasyRpgPlugin(Game_DynRpg& instance) : DynRpgPlugin("EasyRpgPlugin", instance) {}
3133

32-
void RegisterFunctions() override;
34+
bool Invoke(StringView func, dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) override;
3335
void Load(const std::vector<uint8_t>& buffer) override;
3436
std::vector<uint8_t> Save() override;
37+
38+
private:
39+
bool EasyCall(dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter);
3540
};
3641
}
3742

src/dynrpg_textplugin.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ class DynRpgText : public Drawable {
138138
std::vector<uint8_t> Save(const std::string& id) {
139139
std::stringstream ss;
140140
ss << x << "," << y << ",";
141-
for (size_t i = 0; i < texts.size(); ++i) {
141+
for (int i = 0; i < static_cast<int>(texts.size()); ++i) {
142142
std::string t = texts[i];
143143
// Replace , with a sentinel 0x01 to not mess up the tokenizer
144144
std::replace(t.begin(), t.end(), ',', '\1');
145145
ss << t;
146-
if (i < texts.size() - 1) {
146+
if (i < static_cast<int>(texts.size()) - 1) {
147147
ss << "\n";
148148
}
149149

@@ -399,14 +399,23 @@ static bool RemoveAll(dyn_arg_list) {
399399
return true;
400400
}
401401

402-
void DynRpg::TextPlugin::RegisterFunctions() {
403-
DynRpg::RegisterFunction("write_text", WriteText);
404-
DynRpg::RegisterFunction("append_line", AppendLine);
405-
DynRpg::RegisterFunction("append_text", AppendText);
406-
DynRpg::RegisterFunction("change_text", ChangeText);
407-
DynRpg::RegisterFunction("change_position", ChangePosition);
408-
DynRpg::RegisterFunction("remove_text", RemoveText);
409-
DynRpg::RegisterFunction("remove_all", RemoveAll);
402+
bool DynRpg::TextPlugin::Invoke(StringView func, dyn_arg_list args, bool&, Game_Interpreter*) {
403+
if (func == "write_text") {
404+
return WriteText(args);
405+
} else if (func == "append_line") {
406+
return AppendLine(args);
407+
} else if (func == "append_text") {
408+
return AppendText(args);
409+
} else if (func == "change_text") {
410+
return ChangeText(args);
411+
} else if (func == "change_position") {
412+
return ChangePosition(args);
413+
} else if (func == "remove_text") {
414+
return RemoveText(args);
415+
} else if (func == "remove_all") {
416+
return RemoveAll(args);
417+
}
418+
return false;
410419
}
411420

412421
void DynRpg::TextPlugin::Update() {

src/dynrpg_textplugin.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
#ifndef EP_DYNRPG_TEXTPLUGIN_H
1919
#define EP_DYNRPG_TEXTPLUGIN_H
2020

21-
#include "dynrpg.h"
21+
#include "game_dynrpg.h"
2222

2323
namespace DynRpg {
2424
class TextPlugin : public DynRpgPlugin {
2525
public:
26-
TextPlugin() : DynRpgPlugin("DynTextPlugin") {}
27-
~TextPlugin();
26+
TextPlugin(Game_DynRpg& instance) : DynRpgPlugin("DynTextPlugin", instance) {}
27+
~TextPlugin() override;
2828

29-
void RegisterFunctions() override;
29+
bool Invoke(StringView func, dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) override;
3030
void Update() override;
3131
void Load(const std::vector<uint8_t>&) override;
3232
std::vector<uint8_t> Save() override;

0 commit comments

Comments
 (0)