|
66 | 66 | #include "baseui.h" |
67 | 67 | #include "algo.h" |
68 | 68 | #include "rand.h" |
| 69 | +#include <regex> |
69 | 70 |
|
70 | 71 | enum BranchSubcommand { |
71 | 72 | eOptionBranchElse = 1 |
@@ -821,6 +822,8 @@ bool Game_Interpreter::ExecuteCommand(lcf::rpg::EventCommand const& com) { |
821 | 822 | return CommandManiacSetGameOption(com); |
822 | 823 | case Cmd::Maniac_CallCommand: |
823 | 824 | return CommandManiacCallCommand(com); |
| 825 | + case static_cast<Game_Interpreter::Cmd>(99999): |
| 826 | + return InjectCommand(com); |
824 | 827 | default: |
825 | 828 | return true; |
826 | 829 | } |
@@ -4643,6 +4646,97 @@ bool Game_Interpreter::CommandManiacCallCommand(lcf::rpg::EventCommand const&) { |
4643 | 4646 | return true; |
4644 | 4647 | } |
4645 | 4648 |
|
| 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 | + |
4646 | 4740 | Game_Interpreter& Game_Interpreter::GetForegroundInterpreter() { |
4647 | 4741 | return Game_Battle::IsBattleRunning() |
4648 | 4742 | ? Game_Battle::GetInterpreter() |
|
0 commit comments