diff --git a/builtin/entitysync/entitysync.cpp b/builtin/entitysync/entitysync.cpp index e514415..cfe81a6 100644 --- a/builtin/entitysync/entitysync.cpp +++ b/builtin/entitysync/entitysync.cpp @@ -7,6 +7,7 @@ #include "interface/event.h" #include "interface/tcpsocket.h" #include "interface/packet_stream.h" +#include "interface/magic_event.h" #include #include #include @@ -24,33 +25,9 @@ namespace magic = Urho3D; namespace entitysync { -struct MagicEventHandler: public magic::Object -{ - OBJECT(MagicEventHandler); - - const char *MODULE = "entitysync"; - - MagicEventHandler(magic::Context *context): - magic::Object(context) - { - SubscribeToEvent(magic::E_UPDATE, HANDLER(MagicEventHandler, on_update)); - SubscribeToEvent(magic::E_NODEADDED, HANDLER(MagicEventHandler, on_node_added)); - } - - void on_update(magic::StringHash event_type, magic::VariantMap &event_data) - { - } - - void on_node_added(magic::StringHash event_type, magic::VariantMap &event_data) - { - log_w(MODULE, "Node added"); - } -}; - struct Module: public interface::Module, public entitysync::Interface { interface::Server *m_server; - magic::SharedPtr m_magic_event_handler; Module(interface::Server *server): interface::Module("entitysync"), @@ -71,12 +48,13 @@ struct Module: public interface::Module, public entitysync::Interface m_server->sub_event(this, Event::t("core:unload")); m_server->sub_event(this, Event::t("core:continue")); + /*m_server->sub_magic_event(this, magic::E_NODEADDED, + Event::t("entitysync:node_added"));*/ + m_server->access_scene([&](magic::Scene *scene) { magic::Context *context = scene->GetContext(); - m_magic_event_handler = new MagicEventHandler(context); - /*magic::ResourceCache* cache = m_context->GetSubsystem(); @@ -90,9 +68,10 @@ struct Module: public interface::Module, public entitysync::Interface void event(const Event::Type &type, const Event::Private *p) { - EVENT_VOIDN("core:start", on_start) - EVENT_VOIDN("core:unload", on_unload) - EVENT_VOIDN("core:continue", on_continue) + EVENT_VOIDN("core:start", on_start) + EVENT_VOIDN("core:unload", on_unload) + EVENT_VOIDN("core:continue", on_continue) + EVENT_TYPEN("entitysync:node_added", on_node_added, interface::MagicEvent) } void on_start() @@ -107,14 +86,7 @@ struct Module: public interface::Module, public entitysync::Interface { } - void on_update(magic::StringHash event_type, magic::VariantMap &event_data) - { - /*m_server->access_scene([&](magic::Scene *scene) - { - });*/ - } - - void on_node_added(magic::StringHash event_type, magic::VariantMap &event_data) + void on_node_added(const interface::MagicEvent &event) { log_w(MODULE, "Node added"); /*m_server->access_scene([&](magic::Scene *scene) diff --git a/src/interface/server.h b/src/interface/server.h index 0a1b083..1462b8d 100644 --- a/src/interface/server.h +++ b/src/interface/server.h @@ -8,6 +8,7 @@ namespace Urho3D { class Scene; + class StringHash; }; namespace interface @@ -64,12 +65,15 @@ namespace interface virtual void sub_event(struct Module *module, const Event::Type &type) = 0; virtual void emit_event(Event event) = 0; - template - void emit_event(const ss_ &name, PrivateT *p){ - emit_event(std::move(Event(name, up_(p)))); + template + void emit_event(const TypeT &type, PrivateT *p){ + emit_event(std::move(Event(type, up_(p)))); } virtual void access_scene(std::function cb) = 0; + virtual void sub_magic_event(struct interface::Module *module, + const magic::StringHash &event_type, + const Event::Type &buildat_event_type) = 0; virtual void add_socket_event(int fd, const Event::Type &event_type) = 0; virtual void remove_socket_event(int fd) = 0; diff --git a/src/server/state.cpp b/src/server/state.cpp index 1e0055e..2c568bb 100644 --- a/src/server/state.cpp +++ b/src/server/state.cpp @@ -10,6 +10,7 @@ #include "interface/event.h" #include "interface/file_watch.h" #include "interface/fs.h" +#include "interface/magic_event.h" //#include "interface/thread.h" #include "interface/mutex.h" #include @@ -72,6 +73,34 @@ static sv_ list_includes(const ss_ &path, const sv_ &include_dirs) return result; } +/*struct MagicEventHandler: public magic::Object +{ + OBJECT(MagicEventHandler); + + interface::Server *m_server; + interface::Event::Type m_buildat_event_type; + + MagicEventHandler(magic::Context *context, + interface::Server *server, + const magic::StringHash &eventType, + const interface::Event::Type &buildat_event_type): + magic::Object(context), + m_server(server), + m_buildat_event_type(buildat_event_type) + { + SubscribeToEvent(eventType, HANDLER(MagicEventHandler, on_event)); + } + + void on_event(magic::StringHash event_type, magic::VariantMap &event_data) + { + auto *evreg = interface::getGlobalEventRegistry(); + log_w(MODULE, "MagicEventHandler::on_event(): %s (%zu)", + cs(evreg->name(m_buildat_event_type)), m_buildat_event_type); + m_server->emit_event(m_buildat_event_type, new interface::MagicEvent( + event_type, event_data)); + } +};*/ + struct CState: public State, public interface::Server { struct ModuleContainer { @@ -526,6 +555,18 @@ struct CState: public State, public interface::Server m_event_queue.push_back(std::move(event)); } + void access_scene(std::function cb) + { + interface::MutexScope ms(m_magic_mutex); + cb(m_magic_scene); + } + + void sub_magic_event(struct interface::Module *module, + const magic::StringHash &eventType, + const Event::Type &buildat_event_type) + { + } + void handle_events() { // Note: Locking m_modules_mutex here is not needed because no modules @@ -652,12 +693,6 @@ struct CState: public State, public interface::Server } while(0); } - void access_scene(std::function cb) - { - interface::MutexScope ms(m_magic_mutex); - cb(m_magic_scene); - } - void tmp_store_data(const ss_ &name, const ss_ &data) { interface::MutexScope ms(m_tmp_data_mutex); diff --git a/src/server/state.h b/src/server/state.h index dfa9027..4f7a955 100644 --- a/src/server/state.h +++ b/src/server/state.h @@ -45,7 +45,6 @@ namespace server virtual void handle_events() = 0; virtual sv_ get_sockets() = 0; virtual void emit_socket_event(int fd) = 0; - virtual void access_scene(std::function cb) = 0; };