2014-09-19 18:42:07 +03:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Copyright 2014 Perttu Ahola <celeron55@gmail.com>
|
2014-09-18 21:54:29 +03:00
|
|
|
#include "core/log.h"
|
|
|
|
#include "interface/module.h"
|
|
|
|
#include "interface/server.h"
|
|
|
|
#include "interface/event.h"
|
|
|
|
#include "interface/fs.h"
|
2014-09-19 15:54:23 +03:00
|
|
|
#include "client_file/api.h"
|
|
|
|
#include "network/api.h"
|
2014-09-18 21:54:29 +03:00
|
|
|
#include <cereal/archives/binary.hpp>
|
|
|
|
#include <cereal/types/string.hpp>
|
|
|
|
#include <fstream>
|
|
|
|
#include <streambuf>
|
|
|
|
|
|
|
|
using interface::Event;
|
|
|
|
|
|
|
|
namespace client_lua {
|
|
|
|
|
|
|
|
struct Module: public interface::Module
|
|
|
|
{
|
|
|
|
interface::Server *m_server;
|
|
|
|
|
|
|
|
Module(interface::Server *server):
|
|
|
|
m_server(server),
|
|
|
|
interface::Module("client_lua")
|
|
|
|
{
|
|
|
|
log_v(MODULE, "client_lua construct");
|
|
|
|
}
|
|
|
|
|
|
|
|
~Module()
|
|
|
|
{
|
|
|
|
log_v(MODULE, "client_lua destruct");
|
|
|
|
}
|
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
|
|
|
log_v(MODULE, "client_lua init");
|
|
|
|
m_server->sub_event(this, Event::t("core:start"));
|
2014-09-18 23:47:05 +03:00
|
|
|
m_server->sub_event(this, Event::t("core:module_loaded"));
|
|
|
|
m_server->sub_event(this, Event::t("core:module_unloaded"));
|
2014-09-18 21:54:29 +03:00
|
|
|
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)
|
2014-09-18 23:47:05 +03:00
|
|
|
EVENT_TYPEN("core:module_loaded", on_module_loaded,
|
|
|
|
interface::ModuleLoadedEvent)
|
|
|
|
EVENT_TYPEN("core:module_unloaded", on_module_unloaded,
|
|
|
|
interface::ModuleUnloadedEvent)
|
2014-09-18 21:54:29 +03:00
|
|
|
EVENT_TYPEN("network:new_client", on_new_client, network::NewClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_start()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-18 23:47:05 +03:00
|
|
|
void on_module_loaded(const interface::ModuleLoadedEvent &event)
|
|
|
|
{
|
2014-09-27 11:50:16 +03:00
|
|
|
log_t(MODULE, "on_module_loaded(): %s", cs(event.name));
|
2014-09-18 23:47:05 +03:00
|
|
|
ss_ module_name = event.name;
|
|
|
|
ss_ module_path = m_server->get_module_path(module_name);
|
|
|
|
ss_ client_lua_path = module_path+"/client_lua";
|
|
|
|
auto list = interface::getGlobalFilesystem()
|
|
|
|
->list_directory(client_lua_path);
|
2014-09-27 11:50:16 +03:00
|
|
|
if(list.empty())
|
|
|
|
return;
|
2014-09-18 23:47:05 +03:00
|
|
|
|
|
|
|
sv_<ss_> log_list;
|
|
|
|
for(const interface::Filesystem::Node &n : list){
|
|
|
|
if(n.is_directory)
|
|
|
|
continue;
|
|
|
|
log_list.push_back(n.name);
|
|
|
|
}
|
|
|
|
log_i(MODULE, "client_lua: %s: %s", cs(module_name), cs(dump(log_list)));
|
|
|
|
|
|
|
|
for(const interface::Filesystem::Node &n : list){
|
|
|
|
if(n.is_directory)
|
|
|
|
continue;
|
2014-09-19 16:46:17 +03:00
|
|
|
const ss_ &file_path = client_lua_path+"/"+n.name;
|
2014-09-19 02:43:16 +03:00
|
|
|
const ss_ &public_file_name = module_name+"/"+n.name;
|
2014-09-18 23:47:05 +03:00
|
|
|
client_file::access(m_server, [&](client_file::Interface * i){
|
2014-09-19 16:46:17 +03:00
|
|
|
i->add_file_path(public_file_name, file_path);
|
2014-09-18 23:47:05 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_module_unloaded(const interface::ModuleUnloadedEvent &event)
|
|
|
|
{
|
|
|
|
log_v(MODULE, "on_module_unloaded(): %s", cs(event.name));
|
2014-09-19 22:45:24 +03:00
|
|
|
// TODO: Tell client_file to remove files
|
2014-09-18 23:47:05 +03:00
|
|
|
}
|
|
|
|
|
2014-09-18 21:54:29 +03:00
|
|
|
void on_new_client(const network::NewClient &new_client)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
EXPORT void* createModule_client_lua(interface::Server *server){
|
|
|
|
return (void*)(new Module(server));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-24 14:53:57 +03:00
|
|
|
// vim: set noet ts=4 sw=4:
|