server: Keep track of resource paths and use a BuildatResourceRouter to allow loading resources from modules on the server

This commit is contained in:
Perttu Ahola 2014-10-02 20:45:04 +03:00
parent 9f7f9bd804
commit 4d91f34d4e
4 changed files with 72 additions and 2 deletions

View File

@ -259,6 +259,10 @@ struct Module: public interface::Module, public client_file::Interface
log_v(MODULE, "File added: %s: %s (%s)", cs(name),
cs(interface::sha1::hex(hash)), cs(path));
m_files[name] = sp_<FileInfo>(new FileInfo(name, content, hash, path));
// Tell path to server so that it can be used in network-synced Scene
m_server->add_file_path(name, path);
ss_ dir_path = interface::Filesystem::strip_file_name(path);
m_watch->add(dir_path, [this, name, path](const ss_ & path_){
if(path_ != path){

View File

@ -80,6 +80,9 @@ namespace interface
virtual void tmp_store_data(const ss_ &name, const ss_ &data) = 0;
virtual ss_ tmp_restore_data(const ss_ &name) = 0;
// Add resource file path (to make a mirror of the client)
virtual void add_file_path(const ss_ &name, const ss_ &path) = 0;
};
}
// vim: set noet ts=4 sw=4:

View File

@ -25,6 +25,8 @@
#include <Component.h>
#include <ReplicationState.h>
#include <PhysicsWorld.h>
#include <ResourceCache.h>
#include <Octree.h>
#pragma GCC diagnostic pop
#include <iostream>
#include <algorithm>
@ -32,6 +34,7 @@
#define MODULE "__state"
using interface::Event;
namespace magic = Urho3D;
extern server::Config g_server_config;
extern bool g_sigint_received;
@ -77,6 +80,28 @@ static sv_<ss_> list_includes(const ss_ &path, const sv_<ss_> &include_dirs)
return result;
}
struct BuildatResourceRouter : public magic::ResourceRouter
{
server::State *m_server;
public:
BuildatResourceRouter(magic::Context *context, server::State *server):
magic::ResourceRouter(context),
m_server(server)
{}
magic::String Route(const magic::String &name)
{
ss_ path = m_server->get_file_path(name.CString());
if(path == ""){
log_v(MODULE, "Resource route access: %s (assuming local file)",
name.CString());
return name;
}
log_v(MODULE, "Resource route access: %s -> %s",
name.CString(), cs(path));
return path.c_str();
}
};
struct MagicEventHandler: public magic::Object
{
OBJECT(MagicEventHandler);
@ -157,6 +182,7 @@ struct CState: public State, public interface::Server
magic::SharedPtr<magic::Context> m_magic_context;
magic::SharedPtr<magic::Engine> m_magic_engine;
magic::SharedPtr<BuildatResourceRouter> m_router;
magic::SharedPtr<magic::Scene> m_magic_scene;
sm_<Event::Type, magic::SharedPtr<MagicEventHandler>> m_magic_event_handlers;
// NOTE: m_magic_mutex must be locked when constructing or destructing
@ -185,6 +211,9 @@ struct CState: public State, public interface::Server
sm_<ss_, ss_> m_tmp_data;
interface::Mutex m_tmp_data_mutex;
sm_<ss_, ss_> m_file_paths;
interface::Mutex m_file_paths_mutex;
CState():
m_compiler(rccpp::createCompiler(g_server_config.compiler_command))
{
@ -241,7 +270,17 @@ struct CState: public State, public interface::Server
if(!m_magic_engine->Initialize(params))
throw Exception("Urho3D engine initialization failed");
m_magic_scene = new magic::Scene(m_magic_context);
m_magic_scene->CreateComponent<magic::PhysicsWorld>();
m_magic_scene->CreateComponent<magic::PhysicsWorld>(magic::LOCAL);
// Useless but gets rid of warnings like
// "ERROR: No Octree component in scene, drawable will not render"
m_magic_scene->CreateComponent<magic::Octree>(magic::LOCAL);
magic::ResourceCache *magic_cache =
m_magic_context->GetSubsystem<magic::ResourceCache>();
//magic_cache->SetAutoReloadResources(true);
m_router = new BuildatResourceRouter(m_magic_context, this);
magic_cache->SetResourceRouter(
magic::SharedPtr<magic::ResourceRouter>(m_router));
}
~CState()
{
@ -747,6 +786,24 @@ struct CState: public State, public interface::Server
m_tmp_data.erase(name);
return data;
}
// Add resource file path (to make a mirror of the client)
void add_file_path(const ss_ &name, const ss_ &path)
{
log_d(MODULE, "add_file_path(): %s -> %s", cs(name), cs(path));
interface::MutexScope ms(m_file_paths_mutex);
m_file_paths[name] = path;
}
// Returns "" if not found
ss_ get_file_path(const ss_ &name)
{
interface::MutexScope ms(m_file_paths_mutex);
auto it = m_file_paths.find(name);
if(it == m_file_paths.end())
return "";
return it->second;
}
};
State* createState()

View File

@ -42,10 +42,16 @@ namespace server
virtual void sub_event(struct interface::Module *module,
const interface::Event::Type &type) = 0;
virtual void emit_event(interface::Event event) = 0;
virtual void access_scene(std::function<void(magic::Scene*)> cb) = 0;
virtual void handle_events() = 0;
virtual sv_<int> get_sockets() = 0;
virtual void emit_socket_event(int fd) = 0;
virtual void access_scene(std::function<void(magic::Scene*)> cb) = 0;
// Add resource file path (to make a mirror of the client)
virtual void add_file_path(const ss_ &name, const ss_ &path) = 0;
// Returns "" if not found
virtual ss_ get_file_path(const ss_ &name) = 0;
};
State* createState();