From 6f6894419a42806a5759a7c1bb20e16a2e7ad312 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Tue, 11 Jun 2024 10:37:20 +0200 Subject: [PATCH] event parsing and dispatching --- src/factorio_log_parser.cpp | 151 +++++++++++++++++++++++++++++++++++- src/factorio_log_parser.hpp | 34 +++++--- src/log_parse.hpp | 2 +- test/test1.cpp | 2 +- 4 files changed, 176 insertions(+), 13 deletions(-) diff --git a/src/factorio_log_parser.cpp b/src/factorio_log_parser.cpp index 2b418d3..e3d7d04 100644 --- a/src/factorio_log_parser.cpp +++ b/src/factorio_log_parser.cpp @@ -17,10 +17,159 @@ void FactorioLogParser::onFileEvent(const std::string& path, const filewatch::Ev // on mod (?), read line, parse + std::string line; + + const auto parse_res = log_parse_line(line); + if (parse_res.has_value()) { + dispatchRaw(parse_res.value().event, parse_res.value().params); + } +} + +void FactorioLogParser::dispatchRaw(std::string_view event, std::string_view params) { + if (event == "JOIN") { + throwJoin(params); + } else if (event == "LEAVE") { + throwLeave(params); + } else if (event == "CHAT") { + throwChat(params); + } else if (event == "DIED") { + throwDied(params); + } else if (event == "EVOLUTION") { + throwEvolution(params); + } else if (event == "RESEARCH STARTED") { + throwResearchStarted(params); + } else if (event == "RESEARCH FINISHED") { + throwResearchFinished(params); + } else if (event == "RESEARCH CANCELLED") { + throwResearchCancelled(params); + } else { + std::cerr << "FLP error: unknown event parsed: '" << event << "'\n"; + } +} + +void FactorioLogParser::throwJoin(std::string_view params) { + if (params == "") { + return; + } + dispatch( FactorioLogParser_Event::join, FactorioLog::Events::Join{ - "Player" + params + } + ); +} + +void FactorioLogParser::throwLeave(std::string_view params) { + const auto user = params.substr(0, params.find_first_of(' ')); + if (user.empty() || user.size() == params.size()) { + std::cerr << "FLP error: invalid LEAVE params: '" << params << "'\n"; + return; + } + + if (user == "") { + return; + } + + auto reason = params; + reason.remove_prefix(user.size()+1); + + dispatch( + FactorioLogParser_Event::leave, + FactorioLog::Events::Leave{ + user, + reason + } + ); +} + +void FactorioLogParser::throwChat(std::string_view params) { + const auto user = params.substr(0, params.find_first_of(':')); + + if (user.empty() || user.size() == params.size() || user.size() + 2 > params.size()) { + std::cerr << "FLP error: invalid CHAT params: '" << params << "'\n"; + return; + } + + if (user == "") { + return; + } + + auto message = params; + message.remove_prefix(user.size()+2); // ": " + if (message.empty()) { + std::cerr << "FLP error: empty message? '" << params << "'\n"; + return; + } + + dispatch( + FactorioLogParser_Event::chat, + FactorioLog::Events::Chat{ + user, + message + } + ); +} + +void FactorioLogParser::throwDied(std::string_view params) { + const auto user = params.substr(0, params.find_first_of(' ')); + if (user.empty() || user.size() == params.size()) { + std::cerr << "FLP error: invalid DIED params: '" << params << "'\n"; + return; + } + + if (user == "") { + return; + } + + auto reason = params; + reason.remove_prefix(user.size()+1); + + dispatch( + FactorioLogParser_Event::died, + FactorioLog::Events::Died{ + user, + reason + } + ); +} + +void FactorioLogParser::throwEvolution(std::string_view params) { + if (params.empty()) { + return; // ??? + } + + dispatch( + FactorioLogParser_Event::evolution, + FactorioLog::Events::Evolution{ + params + } + ); +} + +void FactorioLogParser::throwResearchStarted(std::string_view params) { + dispatch( + FactorioLogParser_Event::research_started, + FactorioLog::Events::ResearchStarted{ + params + } + ); +} + +void FactorioLogParser::throwResearchFinished(std::string_view params) { + dispatch( + FactorioLogParser_Event::research_finished, + FactorioLog::Events::ResearchFinished{ + params + } + ); +} + +void FactorioLogParser::throwResearchCancelled(std::string_view params) { + dispatch( + FactorioLogParser_Event::research_cancelled, + FactorioLog::Events::ResearchCancelled{ + params } ); } diff --git a/src/factorio_log_parser.hpp b/src/factorio_log_parser.hpp index ee5d3a5..207c2e0 100644 --- a/src/factorio_log_parser.hpp +++ b/src/factorio_log_parser.hpp @@ -5,42 +5,44 @@ #include #include +#include namespace FactorioLog::Events { - // TODO: string views? struct Join { - std::string player_name; + std::string_view player_name; }; struct Leave { - std::string player_name; + std::string_view player_name; + std::string_view reason; }; struct Chat { - std::string player_name; - std::string message; + std::string_view player_name; + std::string_view message; }; struct Died { - std::string player_name; + std::string_view player_name; + std::string_view reason; }; struct Evolution { // ? - std::string evo; + std::string_view evo; }; struct ResearchStarted { - std::string name; + std::string_view name; }; struct ResearchFinished { - std::string name; + std::string_view name; }; struct ResearchCancelled { - std::string name; + std::string_view name; }; } // FactorioLog::Events @@ -83,5 +85,17 @@ class FactorioLogParser : public FactorioLogParserEventProviderI { protected: void onFileEvent(const std::string& path, const filewatch::Event change_type); + + protected: + void dispatchRaw(std::string_view event, std::string_view params); + + void throwJoin(std::string_view params); + void throwLeave(std::string_view params); + void throwChat(std::string_view params); + void throwDied(std::string_view params); + void throwEvolution(std::string_view params); + void throwResearchStarted(std::string_view params); + void throwResearchFinished(std::string_view params); + void throwResearchCancelled(std::string_view params); }; diff --git a/src/log_parse.hpp b/src/log_parse.hpp index 0b1f237..90b4278 100644 --- a/src/log_parse.hpp +++ b/src/log_parse.hpp @@ -5,7 +5,7 @@ struct LPLRes { std::string_view event; - std::string_view info; + std::string_view params; }; std::optional log_parse_line(std::string_view line); diff --git a/test/test1.cpp b/test/test1.cpp index f5f4770..d171f96 100644 --- a/test/test1.cpp +++ b/test/test1.cpp @@ -8,7 +8,7 @@ int main(void) { const auto parse_res = log_parse_line(" 442.539 Script @__Factorio-Event-Logger__/logger.lua:65: [RESEARCH CANCELLED] worker-robots-speed"); assert(parse_res.has_value()); assert(parse_res.value().event == "RESEARCH CANCELLED"); - assert(parse_res.value().info == "worker-robots-speed"); + assert(parse_res.value().params == "worker-robots-speed"); } {