client: Trigger replicate.sub_sync_node_added callbacks only after and immediately when replicate:create_node has been fully processed

This commit is contained in:
Perttu Ahola 2014-10-22 09:40:56 +03:00
parent 45a33aa8c7
commit 16097914bd
10 changed files with 77 additions and 36 deletions

View File

@ -163,6 +163,7 @@ if(BUILD_CLIENT)
src/lua_bindings/voxel_volume.cpp
src/lua_bindings/spatial_update_queue.cpp
src/lua_bindings/misc_urho3d.cpp
src/lua_bindings/replicate.cpp
)
add_executable(${CLIENT_EXE_NAME} ${CLIENT_SRCS})

9
client/dummy.lua Normal file
View File

@ -0,0 +1,9 @@
-- Buildat: client/api.lua
-- http://www.apache.org/licenses/LICENSE-2.0
-- Copyright 2014 Perttu Ahola <celeron55@gmail.com>
local log = buildat.Logger("dummy")
function __buildat_replicate_on_node_created(node_id)
end
-- vim: set noet ts=4 sw=4:

View File

@ -83,6 +83,7 @@ dofile(__buildat_get_path("share").."/client/extensions.lua")
dofile(__buildat_get_path("share").."/client/sandbox.lua")
dofile(__buildat_get_path("share").."/client/replication.lua")
dofile(__buildat_get_path("share").."/client/modules.lua")
dofile(__buildat_get_path("share").."/client/dummy.lua")
local test = require("buildat/extension/test")
test.f()

View File

@ -12,8 +12,8 @@ M.safe.main_scene = getmetatable(magic.Scene).wrap(__buildat_replicated_scene)
local sync_node_added_subs = {}
-- Callback will be called for each node added to the scene, once they have a
-- name. Callback is called immediately for all existing nodes.
-- Callback will be called for each node added to the scene.
-- Callback is called immediately for all existing nodes.
function M.safe.sub_sync_node_added(opts, cb)
-- Add to subscriber table
table.insert(sync_node_added_subs, cb)
@ -31,32 +31,17 @@ function M.safe.sub_sync_node_added(opts, cb)
handle_node(scene)
end
local nodes_waiting_for_name = {}
-- NOTE: Using this safe version of magic.SubscribeToEvent() does not have a
-- performance impact over the unsafe one. Tested 2014-10-15.
magic.SubscribeToEvent("NodeAdded", function(event_type, event_data)
local node = event_data:GetPtr("Node", "Node")
--log:info("NodeAdded: "..node:GetName())
if node:GetName() == "" then
nodes_waiting_for_name[node:GetID()] = true
else
for _, v in ipairs(sync_node_added_subs) do
v(node)
end
-- Override dummy default
function __buildat_replicate_on_node_created(node_id)
log:debug("__buildat_replicate_on_node_created(): id="..node_id)
local node = M.safe.main_scene:GetNode(node_id)
if not node then
return
end
end)
magic.SubscribeToEvent("NodeNameChanged", function(event_type, event_data)
local node = event_data:GetPtr("Node", "Node")
--log:info("NodeNameChanged: "..node:GetName())
if nodes_waiting_for_name[node:GetID()] then
nodes_waiting_for_name[node:GetID()] = nil
for _, v in ipairs(sync_node_added_subs) do
v(node)
end
for _, v in ipairs(sync_node_added_subs) do
v(node)
end
end)
end
return M
-- vim: set noet ts=4 sw=4:

View File

@ -300,6 +300,11 @@ struct CApp: public App, public magic::Application
return m_thread_pool.get();
}
lua_State* get_lua()
{
return L;
}
// Non-public methods
void Start()
@ -362,20 +367,13 @@ struct CApp: public App, public magic::Application
m_camera_node->SetPosition(magic::Vector3(7.0, 7.0, 7.0));
m_camera_node->LookAt(magic::Vector3(0, 1, 0));
// TODO: Pass the scene to the Lua environment in a proper way (see
// stuff in lua_bindings)
magic::Renderer *renderer = GetSubsystem<magic::Renderer>();
magic::SharedPtr<magic::Viewport> viewport(new magic::Viewport(
context_, m_scene, m_camera_node->GetComponent<magic::Camera>()));
renderer->SetViewport(0, viewport);
// Won't work; accessing the resulting value in Lua segfaults.
/*magic::WeakPtr<magic::LuaFunction> f =
m_script->GetFunction("__buildat_set_sync_scene");
if(!f)
throw Exception("__buildat_set_sync_scene not found");
f->BeginCall();
f->PushUserType(m_scene.Get(), "Scene");
f->EndCall();*/
// Run initial client Lua scripts
ss_ init_lua_path = g_client_config.share_path+"/client/init.lua";
int error = luaL_dofile(L, init_lua_path.c_str());

View File

@ -19,6 +19,10 @@ namespace interface {
struct ThreadPool;
}
}
extern "C" {
struct lua_State;
typedef struct lua_State lua_State;
}
namespace app
{
@ -67,6 +71,7 @@ namespace app
const ss_ &file_hash, const ss_ &cached_path) = 0;
virtual Urho3D::Scene* get_scene() = 0;
virtual interface::thread_pool::ThreadPool* get_thread_pool() = 0;
virtual lua_State* get_lua() = 0;
};
App* createApp(Urho3D::Context *context, const Options &options = Options());

View File

@ -8,6 +8,7 @@
#include "interface/packet_stream.h"
#include "interface/sha1.h"
#include "interface/fs.h"
#include "lua_bindings/replicate.h"
#include <c55/string_util.h>
#include <cereal/archives/portable_binary.hpp>
#include <cereal/types/string.hpp>
@ -401,6 +402,9 @@ void CState::setup_packet_handlers()
c->ReadDeltaUpdate(msg);
c->ApplyAttributes();
}
lua_State *L = m_app->get_lua();
lua_bindings::replicate::on_node_created(L, node_id);
};
m_packet_handlers["replicate:create_component"] =

View File

@ -3,7 +3,8 @@
#pragma once
#include "core/types.h"
extern "C" {
#include <lua.h>
struct lua_State;
typedef struct lua_State lua_State;
}
namespace lua_bindings

View File

@ -0,0 +1,20 @@
// http://www.apache.org/licenses/LICENSE-2.0
// Copyright 2014 Perttu Ahola <celeron55@gmail.com>
#include "lua_bindings/replicate.h"
#include "core/log.h"
#include <luabind/luabind.hpp>
#define MODULE "lua_bindings"
namespace lua_bindings {
namespace replicate {
void on_node_created(lua_State *L, uint node_id)
{
luabind::call_function<void>(L, "__buildat_replicate_on_node_created",
(uint32_t)node_id);
}
} // namespace replicate
} // namespace lua_bindingss
// vim: set noet ts=4 sw=4:

View File

@ -0,0 +1,17 @@
// http://www.apache.org/licenses/LICENSE-2.0
// Copyright 2014 Perttu Ahola <celeron55@gmail.com>
#pragma once
#include "core/types.h"
extern "C" {
struct lua_State;
typedef struct lua_State lua_State;
}
namespace lua_bindings
{
namespace replicate
{
void on_node_created(lua_State *L, uint node_id);
}
}
// vim: set noet ts=4 sw=4: