Compare commits

..

2 Commits

Author SHA1 Message Date
70317ab4db
more event boilerplate
Some checks are pending
ContinuousIntegration / macos (push) Waiting to run
ContinuousIntegration / windows (push) Waiting to run
ContinuousIntegration / linux (push) Successful in 22s
2024-06-11 10:09:55 +02:00
4295c6cc53
separate out the log parsing, will throw events 2024-06-11 09:45:52 +02:00
7 changed files with 178 additions and 18 deletions

View File

@ -3,10 +3,12 @@
#include <entt/entt.hpp> #include <entt/entt.hpp>
#include <entt/fwd.hpp> #include <entt/fwd.hpp>
#include "factorio_log_parser.hpp"
#include "factorio.hpp" #include "factorio.hpp"
#include <iostream> #include <iostream>
static std::unique_ptr<FactorioLogParser> g_flp = nullptr;
static std::unique_ptr<Factorio> g_f = nullptr; static std::unique_ptr<Factorio> g_f = nullptr;
constexpr const char* plugin_name = "Factorio"; constexpr const char* plugin_name = "Factorio";
@ -34,9 +36,11 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api)
// static store, could be anywhere tho // static store, could be anywhere tho
// construct with fetched dependencies // construct with fetched dependencies
g_f = std::make_unique<Factorio>(*cr, *rmm); g_flp = std::make_unique<FactorioLogParser>();
g_f = std::make_unique<Factorio>(*cr, *rmm, *g_flp);
// register types // register types
PLUG_PROVIDE_INSTANCE(FactorioLogParser, plugin_name, g_flp.get());
PLUG_PROVIDE_INSTANCE(Factorio, plugin_name, g_f.get()); PLUG_PROVIDE_INSTANCE(Factorio, plugin_name, g_f.get());
} catch (const ResolveException& e) { } catch (const ResolveException& e) {
std::cerr << "PLUGIN " << plugin_name << " " << e.what << "\n"; std::cerr << "PLUGIN " << plugin_name << " " << e.what << "\n";
@ -50,6 +54,7 @@ SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) {
std::cout << "PLUGIN " << plugin_name << " STOP()\n"; std::cout << "PLUGIN " << plugin_name << " STOP()\n";
g_f.reset(); g_f.reset();
g_flp.reset();
} }
SOLANA_PLUGIN_EXPORT float solana_plugin_tick(float) { SOLANA_PLUGIN_EXPORT float solana_plugin_tick(float) {

View File

@ -5,6 +5,8 @@ project(solanaceae)
add_library(solanaceae_factorio add_library(solanaceae_factorio
./log_parse.hpp ./log_parse.hpp
./log_parse.cpp ./log_parse.cpp
./factorio_log_parser.hpp
./factorio_log_parser.cpp
./factorio.hpp ./factorio.hpp
./factorio.cpp ./factorio.cpp
) )

View File

@ -1,18 +1,24 @@
#include "./factorio.hpp" #include "./factorio.hpp"
#include "factorio_log_parser.hpp"
#include <solanaceae/message3/components.hpp> #include <solanaceae/message3/components.hpp>
#include <solanaceae/contact/components.hpp> #include <solanaceae/contact/components.hpp>
#include "./log_parse.hpp" Factorio::Factorio(Contact3Registry& cr, RegistryMessageModel& rmm, FactorioLogParser& flp) :
Factorio::Factorio(Contact3Registry& cr, RegistryMessageModel& rmm) :
_cr(cr), _cr(cr),
_rmm(rmm), _rmm(rmm),
_fw("test.txt", [this](const auto& path, const auto event){ this->onFileEvent(path, event);}) _flp(flp)
{ {
_rmm.subscribe(this, RegistryMessageModel_Event::message_construct); _rmm.subscribe(this, RegistryMessageModel_Event::message_construct);
_flp.subscribe(this, FactorioLogParser_Event::join);
_flp.subscribe(this, FactorioLogParser_Event::leave);
_flp.subscribe(this, FactorioLogParser_Event::chat);
_flp.subscribe(this, FactorioLogParser_Event::died);
_flp.subscribe(this, FactorioLogParser_Event::evolution);
_flp.subscribe(this, FactorioLogParser_Event::research_started);
_flp.subscribe(this, FactorioLogParser_Event::research_finished);
_flp.subscribe(this, FactorioLogParser_Event::research_cancelled);
} }
Factorio::~Factorio(void) { Factorio::~Factorio(void) {
@ -22,7 +28,35 @@ bool Factorio::onEvent(const Message::Events::MessageConstruct& e) {
return false; return false;
} }
void Factorio::onFileEvent(const std::string& path, const filewatch::Event change_type) { bool Factorio::onEvent(const FactorioLog::Events::Join& e) {
std::cout << "file even " << filewatch::event_to_string(change_type) << " on '" << path << "'\n"; return false;
}
bool Factorio::onEvent(const FactorioLog::Events::Leave& e) {
return false;
}
bool Factorio::onEvent(const FactorioLog::Events::Chat& e) {
return false;
}
bool Factorio::onEvent(const FactorioLog::Events::Died& e) {
return false;
}
bool Factorio::onEvent(const FactorioLog::Events::Evolution& e) {
return false;
}
bool Factorio::onEvent(const FactorioLog::Events::ResearchStarted& e) {
return false;
}
bool Factorio::onEvent(const FactorioLog::Events::ResearchFinished& e) {
return false;
}
bool Factorio::onEvent(const FactorioLog::Events::ResearchCancelled& e) {
return false;
} }

View File

@ -2,24 +2,28 @@
#include <solanaceae/message3/registry_message_model.hpp> #include <solanaceae/message3/registry_message_model.hpp>
#include <FileWatch.hpp> #include "./factorio_log_parser.hpp"
#include <string> class Factorio : public RegistryMessageModelEventI, public FactorioLogParserEventI {
class Factorio : public RegistryMessageModelEventI {
Contact3Registry& _cr; Contact3Registry& _cr;
RegistryMessageModel& _rmm; RegistryMessageModel& _rmm;
FactorioLogParser& _flp;
filewatch::FileWatch<std::string> _fw;
public: public:
Factorio(Contact3Registry& cr, RegistryMessageModel& rmm); Factorio(Contact3Registry& cr, RegistryMessageModel& rmm, FactorioLogParser& flp);
virtual ~Factorio(void); virtual ~Factorio(void);
protected: // rmm protected: // rmm
bool onEvent(const Message::Events::MessageConstruct& e) override; bool onEvent(const Message::Events::MessageConstruct& e) override;
protected: protected: // flp
void onFileEvent(const std::string& path, const filewatch::Event change_type); bool onEvent(const FactorioLog::Events::Join&) override;
bool onEvent(const FactorioLog::Events::Leave&) override;
bool onEvent(const FactorioLog::Events::Chat&) override;
bool onEvent(const FactorioLog::Events::Died&) override;
bool onEvent(const FactorioLog::Events::Evolution&) override;
bool onEvent(const FactorioLog::Events::ResearchStarted&) override;
bool onEvent(const FactorioLog::Events::ResearchFinished&) override;
bool onEvent(const FactorioLog::Events::ResearchCancelled&) override;
}; };

View File

@ -0,0 +1,27 @@
#include "./factorio_log_parser.hpp"
#include "./log_parse.hpp"
FactorioLogParser::FactorioLogParser(void) :
_fw("test.txt", [this](const auto& path, const auto event){ this->onFileEvent(path, event);})
{
}
FactorioLogParser::~FactorioLogParser(void) {
}
void FactorioLogParser::onFileEvent(const std::string& path, const filewatch::Event change_type) {
std::cout << "file even " << filewatch::event_to_string(change_type) << " on '" << path << "'\n";
// on create, close open log file and reopen and skip to end
// on mod (?), read line, parse
dispatch(
FactorioLogParser_Event::join,
FactorioLog::Events::Join{
"Player"
}
);
}

View File

@ -0,0 +1,87 @@
#pragma once
#include <solanaceae/util/event_provider.hpp>
#include <FileWatch.hpp>
#include <string>
namespace FactorioLog::Events {
// TODO: string views?
struct Join {
std::string player_name;
};
struct Leave {
std::string player_name;
};
struct Chat {
std::string player_name;
std::string message;
};
struct Died {
std::string player_name;
};
struct Evolution {
// ?
std::string evo;
};
struct ResearchStarted {
std::string name;
};
struct ResearchFinished {
std::string name;
};
struct ResearchCancelled {
std::string name;
};
} // FactorioLog::Events
enum class FactorioLogParser_Event : uint32_t {
join,
leave,
chat,
died,
evolution,
research_started,
research_finished,
research_cancelled,
MAX
};
struct FactorioLogParserEventI {
using enumType = FactorioLogParser_Event;
virtual ~FactorioLogParserEventI(void) {}
virtual bool onEvent(const FactorioLog::Events::Join&) { return false; }
virtual bool onEvent(const FactorioLog::Events::Leave&) { return false; }
virtual bool onEvent(const FactorioLog::Events::Chat&) { return false; }
virtual bool onEvent(const FactorioLog::Events::Died&) { return false; }
virtual bool onEvent(const FactorioLog::Events::Evolution&) { return false; }
virtual bool onEvent(const FactorioLog::Events::ResearchStarted&) { return false; }
virtual bool onEvent(const FactorioLog::Events::ResearchFinished&) { return false; }
virtual bool onEvent(const FactorioLog::Events::ResearchCancelled&) { return false; }
};
using FactorioLogParserEventProviderI = EventProviderI<FactorioLogParserEventI>;
class FactorioLogParser : public FactorioLogParserEventProviderI {
filewatch::FileWatch<std::string> _fw;
public:
FactorioLogParser(void);
virtual ~FactorioLogParser(void);
protected:
void onFileEvent(const std::string& path, const filewatch::Event change_type);
};

View File

@ -7,6 +7,7 @@ std::optional<LPLRes> log_parse_line(std::string_view line) {
return std::nullopt; return std::nullopt;
} }
// TODO: make mod name configurable
static const std::regex mod_match{".*Factorio-Event-Logger+.*\\[([A-Z ]+)\\] (.+)$"}; static const std::regex mod_match{".*Factorio-Event-Logger+.*\\[([A-Z ]+)\\] (.+)$"};
std::cmatch matches; std::cmatch matches;