Skip to content

Commit

Permalink
event parsing and dispatching
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Jun 11, 2024
1 parent 70317ab commit 6f68944
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 13 deletions.
151 changes: 150 additions & 1 deletion src/factorio_log_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "<server>") {
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 == "<server>") {
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 == "<server>") {
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 == "<server>") {
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
}
);
}
Expand Down
34 changes: 24 additions & 10 deletions src/factorio_log_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,44 @@
#include <FileWatch.hpp>

#include <string>
#include <string_view>

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
Expand Down Expand Up @@ -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);
};

2 changes: 1 addition & 1 deletion src/log_parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

struct LPLRes {
std::string_view event;
std::string_view info;
std::string_view params;
};

std::optional<LPLRes> log_parse_line(std::string_view line);
2 changes: 1 addition & 1 deletion test/test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

{
Expand Down

0 comments on commit 6f68944

Please sign in to comment.