builtin/client_lua
This commit is contained in:
parent
3db0f36cbe
commit
ab83f7e5ca
12
share/builtin/client_lua/boot.lua
Normal file
12
share/builtin/client_lua/boot.lua
Normal file
@ -0,0 +1,12 @@
|
||||
-- Buildat: client_lua/boot.lua
|
||||
local log = buildat:Logger("client_lua")
|
||||
log:info("boot.lua loaded")
|
||||
|
||||
-- Temporary test
|
||||
require "Polycode/Core"
|
||||
scene = Scene(Scene.SCENE_2D)
|
||||
scene:getActiveCamera():setOrthoSize(640, 480)
|
||||
label = SceneLabel("Hello from remote module!", 32)
|
||||
label:setPosition(0, -100, 0)
|
||||
scene:addChild(label)
|
||||
|
107
share/builtin/client_lua/client_lua.cpp
Normal file
107
share/builtin/client_lua/client_lua.cpp
Normal file
@ -0,0 +1,107 @@
|
||||
#include "core/log.h"
|
||||
#include "interface/module.h"
|
||||
#include "interface/server.h"
|
||||
#include "interface/event.h"
|
||||
#include "interface/fs.h"
|
||||
#include "client_lua/include/api.h"
|
||||
#include "network/include/api.h"
|
||||
#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"));
|
||||
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("network:new_client", on_new_client, network::NewClient)
|
||||
}
|
||||
|
||||
void on_start()
|
||||
{
|
||||
}
|
||||
|
||||
void on_new_client(const network::NewClient &new_client)
|
||||
{
|
||||
log_i(MODULE, "client_lua::on_new_client: id=%zu", new_client.info.id);
|
||||
|
||||
ss_ module_path = m_server->get_module_path(MODULE);
|
||||
|
||||
std::ifstream f(module_path+"/boot.lua");
|
||||
std::string script_content((std::istreambuf_iterator<char>(f)),
|
||||
std::istreambuf_iterator<char>());
|
||||
|
||||
network::access(m_server, [&](network::Interface * inetwork){
|
||||
inetwork->send(new_client.info.id, "core:run_script", script_content);
|
||||
});
|
||||
|
||||
sv_<ss_> module_names = m_server->get_loaded_modules();
|
||||
|
||||
for(const ss_ &module_name : module_names){
|
||||
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);
|
||||
|
||||
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;
|
||||
std::ifstream f(client_lua_path+"/"+n.name);
|
||||
std::string file_content((std::istreambuf_iterator<char>(f)),
|
||||
std::istreambuf_iterator<char>());
|
||||
|
||||
std::ostringstream os(std::ios::binary);
|
||||
{
|
||||
cereal::BinaryOutputArchive ar(os);
|
||||
ar(n.name);
|
||||
ar(file_content);
|
||||
}
|
||||
network::access(m_server, [&](network::Interface * inetwork){
|
||||
inetwork->send(new_client.info.id, "core:cache_file", os.str());
|
||||
});
|
||||
}
|
||||
m_server->emit_event(ss_()+"client_lua:files_sent:"+module_name,
|
||||
new FilesSent(new_client.info.id));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
EXPORT void* createModule_client_lua(interface::Server *server){
|
||||
return (void*)(new Module(server));
|
||||
}
|
||||
}
|
||||
}
|
15
share/builtin/client_lua/include/api.h
Normal file
15
share/builtin/client_lua/include/api.h
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#include "interface/event.h"
|
||||
#include "network/include/api.h"
|
||||
|
||||
namespace client_lua
|
||||
{
|
||||
struct FilesSent: public interface::Event::Private
|
||||
{
|
||||
network::PeerInfo::Id recipient;
|
||||
|
||||
FilesSent(const network::PeerInfo::Id &recipient): recipient(recipient){}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
#pragma once
|
||||
#include "interface/event.h"
|
||||
#include <functional>
|
||||
|
||||
|
27
share/client/init.lua
Normal file
27
share/client/init.lua
Normal file
@ -0,0 +1,27 @@
|
||||
-- Buildat: client/init.lua
|
||||
buildat = {}
|
||||
function buildat:Logger(module)
|
||||
local logger = {}
|
||||
function logger:info(text)
|
||||
print(os.date("%b %d %H:%M:%S "..module..": "..text))
|
||||
end
|
||||
return logger
|
||||
end
|
||||
|
||||
local log = buildat:Logger("__client")
|
||||
|
||||
log:info("init.lua loaded")
|
||||
|
||||
print("")
|
||||
require "Polycode/Core"
|
||||
scene = Scene(Scene.SCENE_2D)
|
||||
scene:getActiveCamera():setOrthoSize(640, 480)
|
||||
label = SceneLabel("Hello from Lua!", 32)
|
||||
label:setPosition(-50, -50, 0)
|
||||
scene:addChild(label)
|
||||
|
||||
function buildat:sub_packet(name, cb)
|
||||
end
|
||||
function buildat:unsub_packet(cb)
|
||||
end
|
||||
|
@ -1,7 +0,0 @@
|
||||
print("foo")
|
||||
require "Polycode/Core"
|
||||
scene = Scene(Scene.SCENE_2D)
|
||||
scene:getActiveCamera():setOrthoSize(640, 480)
|
||||
label = SceneLabel("Hello from Lua!", 32)
|
||||
label:setPosition(-50, -50, 0)
|
||||
scene:addChild(label)
|
@ -267,7 +267,8 @@ struct CApp: public Polycode::EventHandler, public App
|
||||
//luaopen_Physics3D(L);
|
||||
//luaopen_UI(L);
|
||||
|
||||
int error = luaL_dofile(L, (g_client_config.share_path+"/init.lua").c_str());
|
||||
ss_ init_lua_path = g_client_config.share_path+"/client/init.lua";
|
||||
int error = luaL_dofile(L, init_lua_path.c_str());
|
||||
if(error){
|
||||
log_w(MODULE, "luaL_dofile: An error occurred: %s\n",
|
||||
lua_tostring(L, -1));
|
||||
|
@ -42,8 +42,12 @@ struct Module: public interface::Module
|
||||
{
|
||||
ss_ builtin = m_server->get_builtin_modules_path();
|
||||
m_server->load_module("network", builtin+"/network");
|
||||
m_server->load_module("client_lua", builtin+"/client_lua");
|
||||
|
||||
sv_<ss_> load_list = {"test1", "test2"};
|
||||
sv_<ss_> load_list = {
|
||||
"test1",
|
||||
"test2",
|
||||
};
|
||||
for(const ss_ &name : load_list){
|
||||
m_server->load_module(name, m_server->get_modules_path()+"/"+name);
|
||||
}
|
||||
@ -59,7 +63,9 @@ struct Module: public interface::Module
|
||||
|
||||
void on_module_modified(const interface::ModuleModifiedEvent &event)
|
||||
{
|
||||
log_v(MODULE, "__loader::on_module_modified()");
|
||||
log_v(MODULE, "__loader::on_module_modified(): %s", cs(event.name));
|
||||
if(event.name == "__loader")
|
||||
return;
|
||||
m_server->reload_module(event.name, event.path);
|
||||
}
|
||||
};
|
||||
|
4
test/testmodules/test1/client_lua/init.lua
Normal file
4
test/testmodules/test1/client_lua/init.lua
Normal file
@ -0,0 +1,4 @@
|
||||
-- Buildat: test1/boot.lua
|
||||
local log = buildat:Logger("test1")
|
||||
log:info("boot.lua loaded")
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "interface/server.h"
|
||||
#include "interface/event.h"
|
||||
#include "test1/include/api.h"
|
||||
#include "client_lua/include/api.h"
|
||||
#include "network/include/api.h"
|
||||
#include "core/log.h"
|
||||
|
||||
@ -34,6 +35,7 @@ struct Module: public interface::Module
|
||||
m_server->sub_event(this, Event::t("core:start"));
|
||||
m_server->sub_event(this, m_EventType_test1_thing);
|
||||
m_server->sub_event(this, Event::t("network:new_client"));
|
||||
m_server->sub_event(this, Event::t("client_lua:files_sent:test1"));
|
||||
m_server->sub_event(this, Event::t("network:packet_received"));
|
||||
}
|
||||
|
||||
@ -42,6 +44,8 @@ struct Module: public interface::Module
|
||||
EVENT_VOIDN("core:start", on_start)
|
||||
EVENT_TYPE(m_EventType_test1_thing, on_thing, Thing)
|
||||
EVENT_TYPEN("network:new_client", on_new_client, network::NewClient)
|
||||
EVENT_TYPEN("client_lua:files_sent:test1", on_lua_files_sent,
|
||||
client_lua::FilesSent)
|
||||
EVENT_TYPEN("network:packet_received", on_packet_received, network::Packet)
|
||||
}
|
||||
|
||||
@ -60,7 +64,7 @@ struct Module: public interface::Module
|
||||
|
||||
network::access(m_server, [&](network::Interface * inetwork){
|
||||
inetwork->send(new_client.info.id, "test1:dummy", "dummy data");
|
||||
inetwork->send(new_client.info.id, "core:run_script",
|
||||
/*inetwork->send(new_client.info.id, "core:run_script",
|
||||
"print(\"Remote script is running\")\n"
|
||||
"require \"Polycode/Core\"\n"
|
||||
"scene = Scene(Scene.SCENE_2D)\n"
|
||||
@ -68,7 +72,15 @@ struct Module: public interface::Module
|
||||
"label = SceneLabel(\"Hello from remote module!\", 32)\n"
|
||||
"label:setPosition(0, -100, 0)\n"
|
||||
"scene:addChild(label)\n"
|
||||
);
|
||||
);*/
|
||||
});
|
||||
}
|
||||
|
||||
void on_lua_files_sent(const client_lua::FilesSent &event)
|
||||
{
|
||||
network::access(m_server, [&](network::Interface * inetwork){
|
||||
inetwork->send(event.recipient, "core:run_script",
|
||||
"print(\"TODO: Run init.lua\")");
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user