diff --git a/doric-Qt/doric/context.cpp b/doric-Qt/doric/context.cpp index ca91e455..d735e003 100644 --- a/doric-Qt/doric/context.cpp +++ b/doric-Qt/doric/context.cpp @@ -60,3 +60,14 @@ InterfaceDriver* Context::getDriver() } return driver; } + +QObject* Context::obtainPlugin(QString name) +{ + if (mPluginMap.keys().contains(name)) { + return mPluginMap.value(name); + } else { + QObject *plugin = getDriver()->getRegistry()->pluginInfoMap.createObject(name); + mPluginMap.insert(name, plugin); + return plugin; + } +} diff --git a/doric-Qt/doric/context.h b/doric-Qt/doric/context.h index 2f4d46e6..6c5198d2 100644 --- a/doric-Qt/doric/context.h +++ b/doric-Qt/doric/context.h @@ -10,6 +10,7 @@ class Context { private: QString mContextId; + QMap mPluginMap; RootNode *mRootNode; QString source; QString script; @@ -29,6 +30,8 @@ public: void callEntity(QString methodName, QVariantList args); InterfaceDriver* getDriver(); + + QObject* obtainPlugin(QString name); }; #endif // CONTEXT_H diff --git a/doric-Qt/doric/context_manager.cpp b/doric-Qt/doric/context_manager.cpp index c74e6362..02d30f49 100644 --- a/doric-Qt/doric/context_manager.cpp +++ b/doric-Qt/doric/context_manager.cpp @@ -8,3 +8,8 @@ Context *ContextManager::createContext(QString script, QString source, QString e context->getDriver()->createContext(QString::number(contextId), script, source); return context; } + +Context *ContextManager::getContext(QString contextId) +{ + return contextMap->take(contextId); +} diff --git a/doric-Qt/doric/context_manager.h b/doric-Qt/doric/context_manager.h index e5b8e25c..447693cd 100644 --- a/doric-Qt/doric/context_manager.h +++ b/doric-Qt/doric/context_manager.h @@ -11,12 +11,10 @@ private: static ContextManager *local_instance; ContextManager() { - qDebug() << "ContextManager create"; } ~ContextManager() { - qDebug() << "ContextManager destroy"; } QAtomicInt *counter = new QAtomicInt(); @@ -30,6 +28,8 @@ public: } Context *createContext(QString script, QString source, QString extra); + + Context *getContext(QString contextId); }; #endif // CONTEXTMANAGER_H diff --git a/doric-Qt/doric/doric.pro b/doric-Qt/doric/doric.pro index 2afbc2b9..0c8eee98 100644 --- a/doric-Qt/doric/doric.pro +++ b/doric-Qt/doric/doric.pro @@ -29,6 +29,8 @@ SOURCES += \ main.cpp \ native_driver.cpp \ panel.cpp \ + plugin/shader_plugin.cpp \ + registry.cpp \ shader/root_node.cpp \ utils/constant.cpp @@ -64,8 +66,11 @@ HEADERS += \ interface_driver.h \ native_driver.h \ panel.h \ + plugin/shader_plugin.h \ + registry.h \ shader/root_node.h \ template/singleton.h \ utils/constant.h \ utils/count_down_latch.h \ + utils/object_factory.h \ utils/utils.h diff --git a/doric-Qt/doric/doric.pro.user b/doric-Qt/doric/doric.pro.user index 86fd3c26..01231fa4 100644 --- a/doric-Qt/doric/doric.pro.user +++ b/doric-Qt/doric/doric.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/doric-Qt/doric/engine/bridge_extension.cpp b/doric-Qt/doric/engine/bridge_extension.cpp index da334428..b8fa133c 100644 --- a/doric-Qt/doric/engine/bridge_extension.cpp +++ b/doric-Qt/doric/engine/bridge_extension.cpp @@ -1,5 +1,8 @@ #include +#include + #include "bridge_extension.h" +#include "../context_manager.h" BridgeExtension::BridgeExtension(QObject *parent) : QObject(parent) { @@ -8,6 +11,17 @@ BridgeExtension::BridgeExtension(QObject *parent) : QObject(parent) void BridgeExtension::callNative(QString contextId, QString module, QString methodName, QString callbackId, QJSValue jsValue) { + Context *context = ContextManager::getInstance()->getContext(contextId); + bool classRegistered = context->getDriver()->getRegistry()->acquirePluginInfo(module); + if (classRegistered) { + QObject *plugin = context->obtainPlugin(module); + QMetaObject::invokeMethod( + plugin, + methodName.toStdString().c_str(), + Qt::DirectConnection, QGenericReturnArgument(), + Q_ARG(QJSValue, jsValue), Q_ARG(QString, callbackId)); + qDebug() << plugin; + } qDebug() << "contextId: " + contextId; qDebug() << "module: " + module; qDebug() << "methodName: " + methodName; diff --git a/doric-Qt/doric/engine/js_engine.cpp b/doric-Qt/doric/engine/js_engine.cpp index 76ef3d86..56bf957e 100644 --- a/doric-Qt/doric/engine/js_engine.cpp +++ b/doric-Qt/doric/engine/js_engine.cpp @@ -79,6 +79,11 @@ JSEngine::JSEngine(QObject *parent) : QObject(parent) }); } +void JSEngine::prepareContext(QString contextId, QString script, QString source) +{ + mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source); +} + QJSValue JSEngine::invokeDoricMethod(QString method, QVariantList arguments) { return mJSE->invokeObject(Constant::GLOBAL_DORIC, method, arguments); @@ -90,11 +95,6 @@ void JSEngine::loadBuiltinJS(QString assetName) QString result = mJSE->loadJS(script, "Assets://" + assetName); } -void JSEngine::prepareContext(QString contextId, QString script, QString source) -{ - mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source); -} - QString JSEngine::packageContextScript(QString contextId, QString content) { return QString(Constant::TEMPLATE_CONTEXT_CREATE).replace("%s1", content).replace("%s2", contextId).replace("%s3", contextId); @@ -105,6 +105,11 @@ QString JSEngine::packageModuleScript(QString moduleName, QString content) return QString(Constant::TEMPLATE_MODULE).replace("%s1", moduleName).replace("%s2", content); } +Registry *JSEngine::getRegistry() +{ + return this->mRegistry; +} + JSEngine::~JSEngine() { diff --git a/doric-Qt/doric/engine/js_engine.h b/doric-Qt/doric/engine/js_engine.h index 61e974aa..fdc62b1c 100644 --- a/doric-Qt/doric/engine/js_engine.h +++ b/doric-Qt/doric/engine/js_engine.h @@ -5,12 +5,14 @@ #include #include "interface_jse.h" +#include "../registry.h" class JSEngine : public QObject { Q_OBJECT private: InterfaceJSE *mJSE; + Registry *mRegistry = new Registry(); void loadBuiltinJS(QString assetName); QString packageContextScript(QString contextId, QString content); @@ -22,8 +24,9 @@ public: ~JSEngine(); - QJSValue invokeDoricMethod(QString method, QVariantList arguments); void prepareContext(QString contextId, QString script, QString source); + QJSValue invokeDoricMethod(QString method, QVariantList arguments); + Registry *getRegistry(); }; #endif // JSENGINE_H diff --git a/doric-Qt/doric/interface_driver.h b/doric-Qt/doric/interface_driver.h index 892780d4..9e2ba10a 100644 --- a/doric-Qt/doric/interface_driver.h +++ b/doric-Qt/doric/interface_driver.h @@ -4,6 +4,8 @@ #include #include +#include "registry.h" + class InterfaceDriver { public: @@ -14,6 +16,8 @@ public: virtual void createContext(QString contextId, QString script, QString source) = 0; virtual void destroyContext(QString contextId) = 0; + + virtual Registry* getRegistry() = 0; }; #endif // INTERFACEDRIVER_H diff --git a/doric-Qt/doric/native_driver.cpp b/doric-Qt/doric/native_driver.cpp index 40ed3af1..eb1a3407 100644 --- a/doric-Qt/doric/native_driver.cpp +++ b/doric-Qt/doric/native_driver.cpp @@ -29,3 +29,8 @@ void NativeDriver::destroyContext(QString contextId) { } + +Registry *NativeDriver::getRegistry() +{ + return this->jsEngine.getRegistry(); +} diff --git a/doric-Qt/doric/native_driver.h b/doric-Qt/doric/native_driver.h index d194d785..aed5588f 100644 --- a/doric-Qt/doric/native_driver.h +++ b/doric-Qt/doric/native_driver.h @@ -12,12 +12,10 @@ private: static NativeDriver *local_instance; NativeDriver() { - qDebug() << "constructor"; } ~NativeDriver() { - qDebug() << "destructor"; } JSEngine jsEngine; @@ -36,5 +34,7 @@ public: void createContext(QString contextId, QString script, QString source) override; void destroyContext(QString contextId) override; + + Registry * getRegistry() override; }; #endif // NATIVEDRIVER_H diff --git a/doric-Qt/doric/plugin/shader_plugin.cpp b/doric-Qt/doric/plugin/shader_plugin.cpp new file mode 100644 index 00000000..c9a0c3b1 --- /dev/null +++ b/doric-Qt/doric/plugin/shader_plugin.cpp @@ -0,0 +1,13 @@ +#include "shader_plugin.h" + +#include + +ShaderPlugin::ShaderPlugin(QObject* parent) : QObject (parent) +{ + +} + +void ShaderPlugin::render(QJSValue jsValue, QString callbackId) +{ + qDebug() << ""; +} diff --git a/doric-Qt/doric/plugin/shader_plugin.h b/doric-Qt/doric/plugin/shader_plugin.h new file mode 100644 index 00000000..467e1c36 --- /dev/null +++ b/doric-Qt/doric/plugin/shader_plugin.h @@ -0,0 +1,16 @@ +#ifndef SHADERPLUGIN_H +#define SHADERPLUGIN_H + +#include +#include + +class ShaderPlugin : public QObject +{ + Q_OBJECT +public: + ShaderPlugin(QObject* parent); + + Q_INVOKABLE void render(QJSValue jsValue, QString callbackId); +}; + +#endif // SHADERPLUGIN_H diff --git a/doric-Qt/doric/registry.cpp b/doric-Qt/doric/registry.cpp new file mode 100644 index 00000000..a5022ecf --- /dev/null +++ b/doric-Qt/doric/registry.cpp @@ -0,0 +1,13 @@ +#include "registry.h" + +#include "plugin/shader_plugin.h" + +Registry::Registry() +{ + registerNativePlugin("shader"); +} + +bool Registry::acquirePluginInfo(QString name) +{ + return pluginInfoMap.acquireClass(name); +} diff --git a/doric-Qt/doric/registry.h b/doric-Qt/doric/registry.h new file mode 100644 index 00000000..20441e4d --- /dev/null +++ b/doric-Qt/doric/registry.h @@ -0,0 +1,24 @@ +#ifndef REGISTRY_H +#define REGISTRY_H + +#include + +#include "utils/object_factory.h" + +class Registry +{ +public: + ObjectFactory pluginInfoMap; + + Registry(); + + template + void registerNativePlugin(QString name) + { + pluginInfoMap.registerClass(name); + } + + bool acquirePluginInfo(QString name); +}; + +#endif // REGISTRY_H diff --git a/doric-Qt/doric/utils/object_factory.h b/doric-Qt/doric/utils/object_factory.h new file mode 100644 index 00000000..bc3e622f --- /dev/null +++ b/doric-Qt/doric/utils/object_factory.h @@ -0,0 +1,46 @@ +#include +#include +#include + +#ifndef OBJECTFACTORY_H +#define OBJECTFACTORY_H + +class ObjectFactory +{ +public: + template + static void registerClass(QString name) + { + constructors().insert(name, &constructorHelper); + } + + static bool acquireClass(QString name) + { + return constructors().keys().contains(name); + } + + static QObject* createObject(const QString& name, QObject* parent = NULL) + { + Constructor constructor = constructors().value(name); + if ( constructor == NULL ) + return NULL; + return (*constructor)(parent); + } + +private: + typedef QObject* (*Constructor)(QObject* parent); + + template + static QObject* constructorHelper(QObject* parent) + { + return new T(parent); + } + + static QHash& constructors() + { + static QHash instance; + return instance; + } +}; + +#endif // OBJECTFACTORY_H