History

Add NETWORKLESS_TEST_SCRIPT type to NetworkManager.

This is a slightly hacky way to make ScriptManager work without any networking, and minimize the number of dependencies needed to run a test.

Dale Glass committed 3 years ago

Commit 02a0e331 parent 12f239b7 files · +170 −58Browse files

Showing the changes to libraries/script-engine/src/ScriptManager.cpp (1 of 7 files). Show all

Unified · Split

libraries/script-engine/src/ScriptManager.cpp+47−15
@@ -269,6 +269,9 @@ ScriptManager::ScriptManager(Context context, const QString& scriptContents, con · 268 unmodified lines
269269 case Context::AGENT_SCRIPT:
270270 _type = Type::AGENT;
271271 break;
272+ case Context::NETWORKLESS_TEST_SCRIPT:
273+ _type = Type::NETWORKLESS_TEST;
274+ break;
272275 }
273276
274277 _scriptingInterface = std::make_shared<ScriptManagerScriptingInterface>(this);
@@ -321,6 +324,8 @@ QString ScriptManager::getContext() const { · 46 unmodified lines
321324 return "entity_server";
322325 case AGENT_SCRIPT:
323326 return "agent";
327+ case NETWORKLESS_TEST_SCRIPT:
328+ return "networkless_test";
324329 default:
325330 return "unknown";
326331 }
@@ -664,15 +669,25 @@ void ScriptManager::init() { · 337 unmodified lines
664669 }
665670
666671 _isInitialized = true;
667− runStaticInitializers(this);
672+
673+ if (_context != NETWORKLESS_TEST_SCRIPT) {
674+ // This initializes a bunch of systems that want network access. We
675+ // want to avoid it in test script mode.
676+ runStaticInitializers(this);
677+ }
668678
669679 auto scriptEngine = _engine.get();
670680
671− ScriptValue xmlHttpRequestConstructorValue = scriptEngine->newFunction(XMLHttpRequestClass::constructor);
672− scriptEngine->globalObject().setProperty("XMLHttpRequest", xmlHttpRequestConstructorValue);
681+ if (_context != NETWORKLESS_TEST_SCRIPT) {
682+ // For test scripts we want to minimize the amount of functionality available, for the least
683+ // amount of dependencies and faster test system startup.
684+
685+ ScriptValue xmlHttpRequestConstructorValue = scriptEngine->newFunction(XMLHttpRequestClass::constructor);
686+ scriptEngine->globalObject().setProperty("XMLHttpRequest", xmlHttpRequestConstructorValue);
673687
674− ScriptValue webSocketConstructorValue = scriptEngine->newFunction(WebSocketClass::constructor);
675− scriptEngine->globalObject().setProperty("WebSocket", webSocketConstructorValue);
688+ ScriptValue webSocketConstructorValue = scriptEngine->newFunction(WebSocketClass::constructor);
689+ scriptEngine->globalObject().setProperty("WebSocket", webSocketConstructorValue);
690+ }
676691
677692 /*@jsdoc
678693 * Prints a message to the program log and emits {@link Script.printedMessage}.
@@ -703,7 +718,12 @@ void ScriptManager::init() { · 24 unmodified lines
703718 scriptEngine->registerGlobalObject("Vec3", &_vec3Library);
704719 scriptEngine->registerGlobalObject("Mat4", &_mat4Library);
705720 scriptEngine->registerGlobalObject("Uuid", &_uuidLibrary);
706− scriptEngine->registerGlobalObject("Messages", DependencyManager::get<MessagesClient>().data());
721+
722+ if (_context != NETWORKLESS_TEST_SCRIPT) {
723+ // This requires networking, we want to avoid the need for it in test scripts
724+ scriptEngine->registerGlobalObject("Messages", DependencyManager::get<MessagesClient>().data());
725+ }
726+
707727 scriptEngine->registerGlobalObject("File", new FileScriptingInterface(this));
708728 scriptEngine->registerGlobalObject("console", &_consoleScriptingInterface);
709729 scriptEngine->registerFunction("console", "info", ConsoleScriptingInterface::info, scriptEngine->currentContext()->argumentCount());
@@ -717,25 +737,28 @@ void ScriptManager::init() { · 7 unmodified lines
717737 scriptEngine->registerFunction("console", "groupCollapsed", ConsoleScriptingInterface::groupCollapsed, 1);
718738 scriptEngine->registerFunction("console", "groupEnd", ConsoleScriptingInterface::groupEnd, 0);
719739
720− // Scriptable cache access
721− auto resourcePrototype = createScriptableResourcePrototype(shared_from_this());
722− scriptEngine->globalObject().setProperty("Resource", resourcePrototype);
723− scriptEngine->setDefaultPrototype(qMetaTypeId<ScriptableResource*>(), resourcePrototype);
724−
725740 // constants
726741 scriptEngine->globalObject().setProperty("TREE_SCALE", scriptEngine->newValue(TREE_SCALE));
727742
728− scriptEngine->registerGlobalObject("Assets", _assetScriptingInterface);
729− scriptEngine->registerGlobalObject("Resources", DependencyManager::get<ResourceScriptingInterface>().data());
743+ if (_context != NETWORKLESS_TEST_SCRIPT) {
744+ // Scriptable cache access
745+ auto resourcePrototype = createScriptableResourcePrototype(shared_from_this());
746+ scriptEngine->globalObject().setProperty("Resource", resourcePrototype);
747+ scriptEngine->setDefaultPrototype(qMetaTypeId<ScriptableResource*>(), resourcePrototype);
748+
749+ scriptEngine->registerGlobalObject("Assets", _assetScriptingInterface);
750+ scriptEngine->registerGlobalObject("Resources", DependencyManager::get<ResourceScriptingInterface>().data());
730751
731− scriptEngine->registerGlobalObject("DebugDraw", &DebugDraw::getInstance());
752+ scriptEngine->registerGlobalObject("DebugDraw", &DebugDraw::getInstance());
732753
733− scriptEngine->registerGlobalObject("UserActivityLogger", DependencyManager::get<UserActivityLoggerScriptingInterface>().data());
754+ scriptEngine->registerGlobalObject("UserActivityLogger", DependencyManager::get<UserActivityLoggerScriptingInterface>().data());
755+ }
734756
735757 #if DEV_BUILD || PR_BUILD
736758 scriptEngine->registerGlobalObject("StackTest", new StackTestScriptingInterface(this));
737759 #endif
738760
761+ qCDebug(scriptengine) << "Engine initialized";
739762 }
740763
741764 // registers a global object by name
@@ -820,6 +843,10 @@ void ScriptManager::addEventHandler(const EntityItemID& entityID, const QString& · 78 unmodified lines
820843 }
821844
822845 bool ScriptManager::isStopped() const {
846+ if (_context == NETWORKLESS_TEST_SCRIPT) {
847+ return false;
848+ }
849+
823850 QSharedPointer<ScriptEngines> scriptEngines(_scriptEngines);
824851 return !scriptEngines || scriptEngines->isStopped();
825852 }
@@ -841,6 +868,7 @@ void ScriptManager::run() { · 15 unmodified lines
841868 PROFILE_SET_THREAD_NAME("Script: " + name);
842869
843870 if (isStopped()) {
871+ qCCritical(scriptengine) << "ScriptManager is stopped or ScriptEngines is not available, refusing to run script";
844872 return; // bail early - avoid setting state in init(), as evaluate() will bail too
845873 }
846874
@@ -873,8 +901,12 @@ void ScriptManager::run() { · 26 unmodified lines
873901
874902 std::chrono::microseconds totalUpdates(0);
875903
904+ qCDebug(scriptengine) << "Waiting for finish";
905+
876906 // TODO: Integrate this with signals/slots instead of reimplementing throttling for ScriptManager
877907 while (!_isFinished) {
908+ qCDebug(scriptengine) << "In script event loop";
909+
878910 auto beforeSleep = clock::now();
879911
880912 // Throttle to SCRIPT_FPS