Skip to content

Commit

Permalink
DynRpg: easyrpg_raw rewrite and fix memory issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Oct 30, 2023
1 parent b3c5e4c commit 24cdc27
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
46 changes: 30 additions & 16 deletions src/dynrpg_easyrpg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <map>

#include "dynrpg_easyrpg.h"
#include "string_view.h"
#include "main_data.h"
#include "game_variables.h"
#include "utils.h"
Expand Down Expand Up @@ -93,30 +94,43 @@ bool DynRpg::EasyRpgPlugin::EasyRaw(dyn_arg_list args, Game_Interpreter* interpr
return true;
}

auto func = "raw";
auto func = "easyrpg_raw";
bool okay = false;

lcf::rpg::EventCommand outputCommand;
std::vector<int32_t> outputParams = {};
lcf::rpg::EventCommand cmd;
std::vector<int32_t> output_args;

for (std::size_t i = 0; i < args.size(); ++i) {
std::string currValue = DynRpg::ParseVarArg(func, args, i, okay);
Output::Warning("{}", currValue);
if (args.empty()) {
Output::Warning("easyrpg_raw: Command too short");
return true;
}

std::tie(cmd.code) = DynRpg::ParseArgs<int>(func, args, &okay);

if (!okay) {
return true;
}

if (!okay) return true;
if (args.size() >= 2) {
auto [string_arg] = DynRpg::ParseArgs<std::string>(func, args.subspan(1), &okay);
cmd.string = lcf::DBString(string_arg);

if (i == 0) outputCommand.code = stoi(currValue);
if (i == 1) outputCommand.string = lcf::DBString(currValue);
else outputParams.push_back(stoi(currValue));
if (!okay) {
return true;
}

for (size_t i = 2; i < args.size(); ++i) {
auto [int_arg] = DynRpg::ParseArgs<int>(func, args.subspan(i), &okay);

if (!okay) {
return true;
}
}
}

outputCommand.parameters = lcf::DBArray<int32_t>(outputParams.begin(), outputParams.end());
cmd.parameters = lcf::DBArray<int32_t>(output_args.begin(), output_args.end());

//FIXME: this will crash when you two interpreters run a raw command in parallel.
// The lack to access the current interpreter frame is a lack in the dynrpg API design.
// Have to fix this. The current frame should be easy to access
std::vector<lcf::rpg::EventCommand> cmdList = { outputCommand };
interpreter->Push(cmdList, 0, false);
interpreter->Push({ cmd }, 0, false);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ bool Game_Interpreter::IsRunning() const {

// Setup.
void Game_Interpreter::Push(
const std::vector<lcf::rpg::EventCommand>& _list,
std::vector<lcf::rpg::EventCommand> _list,
int event_id,
bool started_by_decision_key
) {
Expand All @@ -114,7 +114,7 @@ void Game_Interpreter::Push(

lcf::rpg::SaveEventExecFrame frame;
frame.ID = _state.stack.size() + 1;
frame.commands = _list;
frame.commands = std::move(_list);
frame.current_command = 0;
frame.triggered_by_decision_key = started_by_decision_key;
frame.event_id = event_id;
Expand Down
7 changes: 3 additions & 4 deletions src/game_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ class Game_Interpreter
void Update(bool reset_loop_count=true);

void Push(
const std::vector<lcf::rpg::EventCommand>& _list,
int _event_id,
bool started_by_decision_key = false
std::vector<lcf::rpg::EventCommand> _list,
int _event_id,
bool started_by_decision_key = false
);
void Push(Game_Event* ev);
void Push(Game_Event* ev, const lcf::rpg::EventPage* page, bool triggered_by_decision_key);
Expand All @@ -78,7 +78,6 @@ class Game_Interpreter
bool ExecuteCommand();
virtual bool ExecuteCommand(lcf::rpg::EventCommand const& com);


/**
* Returns a SaveEventExecState needed for the savefile.
*
Expand Down

0 comments on commit 24cdc27

Please sign in to comment.