Skip to content

Commit

Permalink
Merge pull request #3288 from Ghabry/dynrpg-refactor
Browse files Browse the repository at this point in the history
Refactor Dynrpg namespace into a class. Whitelist @easyrpg_ commands
  • Loading branch information
Ghabry authored Nov 10, 2024
2 parents 6331bb8 + 1ccac18 commit 99cb093
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 162 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ add_library(${PROJECT_NAME} OBJECT
src/drawable_list.h
src/drawable_mgr.cpp
src/drawable_mgr.h
src/dynrpg.cpp
src/dynrpg.h
src/dynrpg_easyrpg.cpp
src/dynrpg_easyrpg.h
src/dynrpg_textplugin.cpp
Expand Down Expand Up @@ -163,6 +161,8 @@ add_library(${PROJECT_NAME} OBJECT
src/game_config.h
src/game_config_game.cpp
src/game_config_game.h
src/game_dynrpg.cpp
src/game_dynrpg.h
src/game_enemy.cpp
src/game_enemy.h
src/game_enemyparty.cpp
Expand Down
4 changes: 2 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ libeasyrpg_player_a_SOURCES = \
src/drawable_list.h \
src/drawable_mgr.cpp \
src/drawable_mgr.h \
src/dynrpg.cpp \
src/dynrpg.h \
src/dynrpg_easyrpg.cpp \
src/dynrpg_easyrpg.h \
src/dynrpg_textplugin.h \
Expand Down Expand Up @@ -143,6 +141,8 @@ libeasyrpg_player_a_SOURCES = \
src/game_config.h \
src/game_config_game.cpp \
src/game_config_game.h \
src/game_dynrpg.cpp \
src/game_dynrpg.h \
src/game_enemy.cpp \
src/game_enemy.h \
src/game_enemyparty.cpp \
Expand Down
29 changes: 17 additions & 12 deletions src/dynrpg_easyrpg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ static bool EasyOput(dyn_arg_list args) {
return true;
}

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

if (token.empty()) {
if (func_name.empty()) {
// empty function name
Output::Warning("call: Empty RPGSS function name");

return true;
}

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

return DynRpg::Invoke(token, args.subspan(1));
return false;
}

static bool EasyAdd(dyn_arg_list args) {
Expand All @@ -88,10 +88,15 @@ static bool EasyAdd(dyn_arg_list args) {
return true;
}

void DynRpg::EasyRpgPlugin::RegisterFunctions() {
DynRpg::RegisterFunction("call", EasyCall);
DynRpg::RegisterFunction("easyrpg_output", EasyOput);
DynRpg::RegisterFunction("easyrpg_add", EasyAdd);
bool DynRpg::EasyRpgPlugin::Invoke(StringView func, dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) {
if (func == "call") {
return EasyCall(args, do_yield, interpreter);
} else if (func == "easyrpg_output") {
return EasyOput(args);
} else if (func == "easyrpg_add") {
return EasyAdd(args);
}
return false;
}

void DynRpg::EasyRpgPlugin::Load(const std::vector<uint8_t>& buffer) {
Expand Down
11 changes: 8 additions & 3 deletions src/dynrpg_easyrpg.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#ifndef EP_DYNRPG_EASYRPG_H
#define EP_DYNRPG_EASYRPG_H

#include "dynrpg.h"
#include "game_dynrpg.h"
#include "game_battle.h"
#include "game_map.h"

namespace DynRpg {
/**
Expand All @@ -27,11 +29,14 @@ namespace DynRpg {
*/
class EasyRpgPlugin : public DynRpgPlugin {
public:
EasyRpgPlugin() : DynRpgPlugin("EasyRpgPlugin") {}
EasyRpgPlugin(Game_DynRpg& instance) : DynRpgPlugin("EasyRpgPlugin", instance) {}

void RegisterFunctions() override;
bool Invoke(StringView func, dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) override;
void Load(const std::vector<uint8_t>& buffer) override;
std::vector<uint8_t> Save() override;

private:
bool EasyCall(dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter);
};
}

Expand Down
29 changes: 19 additions & 10 deletions src/dynrpg_textplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ class DynRpgText : public Drawable {
std::vector<uint8_t> Save(const std::string& id) {
std::stringstream ss;
ss << x << "," << y << ",";
for (size_t i = 0; i < texts.size(); ++i) {
for (int i = 0; i < static_cast<int>(texts.size()); ++i) {
std::string t = texts[i];
// Replace , with a sentinel 0x01 to not mess up the tokenizer
std::replace(t.begin(), t.end(), ',', '\1');
ss << t;
if (i < texts.size() - 1) {
if (i < static_cast<int>(texts.size()) - 1) {
ss << "\n";
}

Expand Down Expand Up @@ -399,14 +399,23 @@ static bool RemoveAll(dyn_arg_list) {
return true;
}

void DynRpg::TextPlugin::RegisterFunctions() {
DynRpg::RegisterFunction("write_text", WriteText);
DynRpg::RegisterFunction("append_line", AppendLine);
DynRpg::RegisterFunction("append_text", AppendText);
DynRpg::RegisterFunction("change_text", ChangeText);
DynRpg::RegisterFunction("change_position", ChangePosition);
DynRpg::RegisterFunction("remove_text", RemoveText);
DynRpg::RegisterFunction("remove_all", RemoveAll);
bool DynRpg::TextPlugin::Invoke(StringView func, dyn_arg_list args, bool&, Game_Interpreter*) {
if (func == "write_text") {
return WriteText(args);
} else if (func == "append_line") {
return AppendLine(args);
} else if (func == "append_text") {
return AppendText(args);
} else if (func == "change_text") {
return ChangeText(args);
} else if (func == "change_position") {
return ChangePosition(args);
} else if (func == "remove_text") {
return RemoveText(args);
} else if (func == "remove_all") {
return RemoveAll(args);
}
return false;
}

void DynRpg::TextPlugin::Update() {
Expand Down
8 changes: 4 additions & 4 deletions src/dynrpg_textplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
#ifndef EP_DYNRPG_TEXTPLUGIN_H
#define EP_DYNRPG_TEXTPLUGIN_H

#include "dynrpg.h"
#include "game_dynrpg.h"

namespace DynRpg {
class TextPlugin : public DynRpgPlugin {
public:
TextPlugin() : DynRpgPlugin("DynTextPlugin") {}
~TextPlugin();
TextPlugin(Game_DynRpg& instance) : DynRpgPlugin("DynTextPlugin", instance) {}
~TextPlugin() override;

void RegisterFunctions() override;
bool Invoke(StringView func, dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) override;
void Update() override;
void Load(const std::vector<uint8_t>&) override;
std::vector<uint8_t> Save() override;
Expand Down
Loading

0 comments on commit 99cb093

Please sign in to comment.