Skip to content

Commit 0802758

Browse files
committed
Barebones Scripting Language
TPC commands for testing purposes: @raw 99999, "@10110(""test"",[1 ,2, 3,4 ,5,6],0) //show dialog" @raw 99999, "@10310 ("""",[0 ,0, 100],0) //add 100 money" @raw 99999, "@10110(""@10110(\""test\"",[1 ,2, 3,4 ,5,6],0)"",[1 ,2, 3,4 ,5,6],0) //show it's own syntax as dialog" Work in progress. I need a lot of help with this one. It injects commands from 99999's com.string. I can't parse proper syntax, since it isn't mapped anywhere. But it can be useful alongsize stringvars, to load commands from txt files.
1 parent ad6e823 commit 0802758

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

src/game_interpreter.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include "baseui.h"
6767
#include "algo.h"
6868
#include "rand.h"
69+
#include <regex>
6970

7071
enum BranchSubcommand {
7172
eOptionBranchElse = 1
@@ -821,6 +822,8 @@ bool Game_Interpreter::ExecuteCommand(lcf::rpg::EventCommand const& com) {
821822
return CommandManiacSetGameOption(com);
822823
case Cmd::Maniac_CallCommand:
823824
return CommandManiacCallCommand(com);
825+
case static_cast<Game_Interpreter::Cmd>(99999):
826+
return InjectCommand(com);
824827
default:
825828
return true;
826829
}
@@ -4643,6 +4646,97 @@ bool Game_Interpreter::CommandManiacCallCommand(lcf::rpg::EventCommand const&) {
46434646
return true;
46444647
}
46454648

4649+
static std::string comToString(lcf::rpg::EventCommand const& com) {
4650+
std::ostringstream oss;
4651+
oss << "@" << com.code << "(\"" << com.string << "\", [";
4652+
for (size_t i = 0; i < com.parameters.size(); ++i) {
4653+
oss << com.parameters[i];
4654+
if (i != com.parameters.size() - 1) {
4655+
oss << ",";
4656+
}
4657+
}
4658+
oss << "], " << com.indent << ")";
4659+
return oss.str();
4660+
}
4661+
4662+
static lcf::rpg::EventCommand stringToCom(const std::string& input) {
4663+
lcf::rpg::EventCommand com;
4664+
4665+
std::string temp = input;
4666+
4667+
// Extracting code
4668+
std::regex codeRegex("@(\\d+)\\s*\\(");
4669+
std::smatch codeMatch;
4670+
if (std::regex_search(temp, codeMatch, codeRegex)) {
4671+
com.code = std::stoi(codeMatch[1].str());
4672+
}
4673+
4674+
// Extracting string
4675+
std::regex stringRegex("\"((?:\\\\\"|[^\"])*)\"");
4676+
std::smatch stringMatch;
4677+
if (std::regex_search(temp, stringMatch, stringRegex)) {
4678+
std::string stringStr = stringMatch[1].str();
4679+
4680+
// Replace escaped quotes with regular quotes
4681+
std::string::size_type pos = stringStr.find("\\\"");
4682+
while (pos != std::string::npos) {
4683+
stringStr.replace(pos, 2, "\"");
4684+
pos = stringStr.find("\\\"", pos + 1);
4685+
}
4686+
4687+
com.string = lcf::DBString(stringStr.c_str());
4688+
}
4689+
4690+
// Extracting parameters
4691+
std::regex paramRegex("\\[(.*?)\\]");
4692+
std::smatch paramMatch;
4693+
if (std::regex_search(temp, paramMatch, paramRegex)) {
4694+
std::string paramStr = paramMatch[1].str();
4695+
std::istringstream iss(paramStr);
4696+
std::string param;
4697+
std::vector<int32_t> params;
4698+
while (std::getline(iss, param, ',')) {
4699+
params.push_back(static_cast<int32_t>(std::stoi(param)));
4700+
}
4701+
com.parameters = lcf::DBArray<int32_t>(params.begin(), params.end());
4702+
}
4703+
4704+
// Extracting indent
4705+
std::regex indentRegex("\\](\\d+)\\)");
4706+
std::smatch indentMatch;
4707+
if (std::regex_search(temp, indentMatch, indentRegex)) {
4708+
com.indent = std::stoi(indentMatch[1].str());
4709+
}
4710+
else {
4711+
com.indent = 0; // Default value for indent when missing
4712+
}
4713+
4714+
return com;
4715+
}
4716+
4717+
bool Game_Interpreter::InjectCommand(lcf::rpg::EventCommand const& com) {
4718+
std::string entry = ToString(com.string);
4719+
4720+
auto* frame = GetFramePtr();
4721+
4722+
// Split the entry string into lines
4723+
std::istringstream iss(entry);
4724+
std::string line;
4725+
while (std::getline(iss, line, '\n')) {
4726+
// Remove carriage return ('\r') if present
4727+
if (!line.empty() && line.back() == '\r') {
4728+
line.pop_back();
4729+
}
4730+
4731+
// Perform tasks on each line
4732+
lcf::rpg::EventCommand result = stringToCom(line);
4733+
frame->commands.push_back(result);
4734+
Output::Debug("Injecting: {}", comToString(result));
4735+
}
4736+
4737+
return true;
4738+
}
4739+
46464740
Game_Interpreter& Game_Interpreter::GetForegroundInterpreter() {
46474741
return Game_Battle::IsBattleRunning()
46484742
? Game_Battle::GetInterpreter()

src/game_interpreter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ class Game_Interpreter
284284
bool CommandManiacChangePictureId(lcf::rpg::EventCommand const& com);
285285
bool CommandManiacSetGameOption(lcf::rpg::EventCommand const& com);
286286
bool CommandManiacCallCommand(lcf::rpg::EventCommand const& com);
287+
bool InjectCommand(lcf::rpg::EventCommand const& com);
287288

288289
int DecodeInt(lcf::DBArray<int32_t>::const_iterator& it);
289290
const std::string DecodeString(lcf::DBArray<int32_t>::const_iterator& it);

0 commit comments

Comments
 (0)