Skip to content

Commit 24cdc27

Browse files
committed
DynRpg: easyrpg_raw rewrite and fix memory issues
1 parent b3c5e4c commit 24cdc27

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

Diff for: src/dynrpg_easyrpg.cpp

+30-16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <map>
2020

2121
#include "dynrpg_easyrpg.h"
22+
#include "string_view.h"
2223
#include "main_data.h"
2324
#include "game_variables.h"
2425
#include "utils.h"
@@ -93,30 +94,43 @@ bool DynRpg::EasyRpgPlugin::EasyRaw(dyn_arg_list args, Game_Interpreter* interpr
9394
return true;
9495
}
9596

96-
auto func = "raw";
97+
auto func = "easyrpg_raw";
9798
bool okay = false;
9899

99-
lcf::rpg::EventCommand outputCommand;
100-
std::vector<int32_t> outputParams = {};
100+
lcf::rpg::EventCommand cmd;
101+
std::vector<int32_t> output_args;
101102

102-
for (std::size_t i = 0; i < args.size(); ++i) {
103-
std::string currValue = DynRpg::ParseVarArg(func, args, i, okay);
104-
Output::Warning("{}", currValue);
103+
if (args.empty()) {
104+
Output::Warning("easyrpg_raw: Command too short");
105+
return true;
106+
}
107+
108+
std::tie(cmd.code) = DynRpg::ParseArgs<int>(func, args, &okay);
109+
110+
if (!okay) {
111+
return true;
112+
}
105113

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

108-
if (i == 0) outputCommand.code = stoi(currValue);
109-
if (i == 1) outputCommand.string = lcf::DBString(currValue);
110-
else outputParams.push_back(stoi(currValue));
118+
if (!okay) {
119+
return true;
120+
}
121+
122+
for (size_t i = 2; i < args.size(); ++i) {
123+
auto [int_arg] = DynRpg::ParseArgs<int>(func, args.subspan(i), &okay);
124+
125+
if (!okay) {
126+
return true;
127+
}
128+
}
111129
}
112130

113-
outputCommand.parameters = lcf::DBArray<int32_t>(outputParams.begin(), outputParams.end());
131+
cmd.parameters = lcf::DBArray<int32_t>(output_args.begin(), output_args.end());
114132

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

121135
return true;
122136
}

Diff for: src/game_interpreter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ bool Game_Interpreter::IsRunning() const {
100100

101101
// Setup.
102102
void Game_Interpreter::Push(
103-
const std::vector<lcf::rpg::EventCommand>& _list,
103+
std::vector<lcf::rpg::EventCommand> _list,
104104
int event_id,
105105
bool started_by_decision_key
106106
) {
@@ -114,7 +114,7 @@ void Game_Interpreter::Push(
114114

115115
lcf::rpg::SaveEventExecFrame frame;
116116
frame.ID = _state.stack.size() + 1;
117-
frame.commands = _list;
117+
frame.commands = std::move(_list);
118118
frame.current_command = 0;
119119
frame.triggered_by_decision_key = started_by_decision_key;
120120
frame.event_id = event_id;

Diff for: src/game_interpreter.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ class Game_Interpreter
6464
void Update(bool reset_loop_count=true);
6565

6666
void Push(
67-
const std::vector<lcf::rpg::EventCommand>& _list,
68-
int _event_id,
69-
bool started_by_decision_key = false
67+
std::vector<lcf::rpg::EventCommand> _list,
68+
int _event_id,
69+
bool started_by_decision_key = false
7070
);
7171
void Push(Game_Event* ev);
7272
void Push(Game_Event* ev, const lcf::rpg::EventPage* page, bool triggered_by_decision_key);
@@ -78,7 +78,6 @@ class Game_Interpreter
7878
bool ExecuteCommand();
7979
virtual bool ExecuteCommand(lcf::rpg::EventCommand const& com);
8080

81-
8281
/**
8382
* Returns a SaveEventExecState needed for the savefile.
8483
*

0 commit comments

Comments
 (0)