diff --git a/builtin/client_data/client_data.cpp b/builtin/client_data/client_data.cpp new file mode 100644 index 0000000..486f515 --- /dev/null +++ b/builtin/client_data/client_data.cpp @@ -0,0 +1,104 @@ +#include "core/log.h" +#include "interface/module.h" +#include "interface/server.h" +#include "interface/event.h" +#include "interface/fs.h" +#include "client_file/include/api.h" +#include "network/include/api.h" +#include +#include +#include +#include + +using interface::Event; + +namespace client_data { + +struct Module: public interface::Module +{ + interface::Server *m_server; + + Module(interface::Server *server): + m_server(server), + interface::Module("client_data") + { + log_v(MODULE, "client_data construct"); + } + + ~Module() + { + log_v(MODULE, "client_data destruct"); + } + + void init() + { + log_v(MODULE, "client_data init"); + m_server->sub_event(this, Event::t("core:start")); + m_server->sub_event(this, Event::t("core:module_loaded")); + m_server->sub_event(this, Event::t("core:module_unloaded")); + m_server->sub_event(this, Event::t("network:new_client")); + } + + void event(const Event::Type &type, const Event::Private *p) + { + EVENT_VOIDN("core:start", on_start) + EVENT_TYPEN("core:module_loaded", on_module_loaded, + interface::ModuleLoadedEvent) + EVENT_TYPEN("core:module_unloaded", on_module_unloaded, + interface::ModuleUnloadedEvent) + EVENT_TYPEN("network:new_client", on_new_client, network::NewClient) + } + + void on_start() + { + } + + void on_module_loaded(const interface::ModuleLoadedEvent &event) + { + log_v(MODULE, "on_module_loaded(): %s", cs(event.name)); + ss_ module_name = event.name; + ss_ module_path = m_server->get_module_path(module_name); + ss_ client_data_path = module_path+"/client_data"; + auto list = interface::getGlobalFilesystem() + ->list_directory(client_data_path); + + sv_ log_list; + for(const interface::Filesystem::Node &n : list){ + if(n.is_directory) + continue; + log_list.push_back(n.name); + } + log_i(MODULE, "client_data: %s: %s", cs(module_name), cs(dump(log_list))); + + for(const interface::Filesystem::Node &n : list){ + if(n.is_directory) + continue; + const ss_ &file_name = n.name; + std::ifstream f(client_data_path+"/"+file_name); + std::string file_content((std::istreambuf_iterator(f)), + std::istreambuf_iterator()); + + const ss_ &public_file_name = module_name+"/"+n.name; + client_file::access(m_server, [&](client_file::Interface * i){ + i->add_file_content(public_file_name, file_content); + }); + } + } + + void on_module_unloaded(const interface::ModuleUnloadedEvent &event) + { + log_v(MODULE, "on_module_unloaded(): %s", cs(event.name)); + // TODO + } + + void on_new_client(const network::NewClient &new_client) + { + } +}; + +extern "C" { + EXPORT void* createModule_client_data(interface::Server *server){ + return (void*)(new Module(server)); + } +} +} diff --git a/builtin/client_lua/client_lua.cpp b/builtin/client_lua/client_lua.cpp index 4f07786..a39b134 100644 --- a/builtin/client_lua/client_lua.cpp +++ b/builtin/client_lua/client_lua.cpp @@ -4,7 +4,6 @@ #include "interface/event.h" #include "interface/fs.h" #include "client_file/include/api.h" -#include "client_lua/include/api.h" #include "network/include/api.h" #include #include diff --git a/builtin/client_lua/include/api.h b/builtin/client_lua/include/api.h deleted file mode 100644 index f63e71b..0000000 --- a/builtin/client_lua/include/api.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once -#include "interface/event.h" - -namespace client_lua -{ -} diff --git a/test/testmodules/__loader/__loader.cpp b/test/testmodules/__loader/__loader.cpp index fbb1008..da6d34d 100644 --- a/test/testmodules/__loader/__loader.cpp +++ b/test/testmodules/__loader/__loader.cpp @@ -44,6 +44,7 @@ struct Module: public interface::Module m_server->load_module("network", builtin+"/network"); m_server->load_module("client_file", builtin+"/client_file"); m_server->load_module("client_lua", builtin+"/client_lua"); + m_server->load_module("client_data", builtin+"/client_data"); sv_ load_list = { "test1",