From e6b4c19b8002b552101a7cbcfcb6eae76666de70 Mon Sep 17 00:00:00 2001 From: Imrglop <69129770+Imrglop@users.noreply.github.com> Date: Sat, 30 Nov 2024 12:52:37 -0500 Subject: [PATCH] client.registerScreen --- .../script/objects/ClientScriptingObject.cpp | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/client/script/objects/ClientScriptingObject.cpp b/src/client/script/objects/ClientScriptingObject.cpp index 18da735..c5866af 100644 --- a/src/client/script/objects/ClientScriptingObject.cpp +++ b/src/client/script/objects/ClientScriptingObject.cpp @@ -12,6 +12,7 @@ #include "client/feature/command/script/JsCommand.h" #include #include +#include JsValueRef ClientScriptingObject::registerEventCallback(JsValueRef callee, bool isConstructor, JsValueRef* arguments, unsigned short argCount, void* callbackState) { JsValueRef undefined; @@ -117,6 +118,7 @@ void ClientScriptingObject::initialize(JsContextRef ctx, JsValueRef parentObj) { Chakra::DefineFunc(moduleManager, mmgrForEachModule, XW("forEachModule")); Chakra::DefineFunc(commandManager, cmgrRegisterCommandCallback, XW("registerCommand")); Chakra::DefineFunc(commandManager, cmgrGetPrefixCallback, XW("getPrefix")); + Chakra::DefineFunc(commandManager, smgrRegisterScreenCallback, XW("registerScreen")); Chakra::SetPropertyString(object, L"version", util::StrToWStr(std::string(Latite::version.data(), Latite::version.size()))); } @@ -292,7 +294,7 @@ JsValueRef ClientScriptingObject::cmgrRegisterCommandCallback(JsValueRef callee, return undefined; } - JsCommand* cmd; + JsCommand* cmd = nullptr; JS::JsGetExternalData(arguments[1], reinterpret_cast(&cmd)); @@ -304,7 +306,7 @@ JsValueRef ClientScriptingObject::cmgrRegisterCommandCallback(JsValueRef callee, if (Latite::getCommandManager().registerScriptCommand(cmd)) { JsScript::getThis()->addResource(cmd, [](void* obj) { if (!Latite::getCommandManager().deregisterScriptCommand(reinterpret_cast(obj))) { - Logger::Warn("Script is already deregistered"); + Logger::Warn("Script command is already deregistered"); } // hoepfully this doesnt double delete... @@ -326,7 +328,7 @@ JsValueRef ClientScriptingObject::cmgrDeregisterCommandCallback(JsValueRef calle return undefined; } - JsCommand* cmd; + JsCommand* cmd = nullptr; // TODO: check if its a script command @@ -343,5 +345,32 @@ JsValueRef ClientScriptingObject::cmgrGetPrefixCallback(JsValueRef callee, bool } JsValueRef ClientScriptingObject::smgrRegisterScreenCallback(JsValueRef callee, bool isConstructor, JsValueRef* arguments, unsigned short argCount, void* callbackState) { - return JsValueRef(); + JsValueRef undefined; + JS::JsGetUndefinedValue(&undefined); + if (!Chakra::VerifyArgCount(argCount, 2)) return undefined; + + auto sz = Chakra::VerifyParameters({ {arguments[1], JsObject} }); + if (!sz.success) { + // handle + Chakra::ThrowError(sz.str); + return undefined; + } + + JsScreen* scn = nullptr; + + JS::JsGetExternalData(arguments[1], reinterpret_cast(&scn)); + + if (!scn) { + Chakra::ThrowError(L"Object is not a command"); + return undefined; + } + + if (Latite::getScreenManager().registerScriptScreen(scn)) { + JsScript::getThis()->addResource(scn, [](void* obj) { + if (!Latite::getScreenManager().deregisterScriptScreen(reinterpret_cast(obj))) { + Logger::Warn("Screen is already deregistered"); + } + }); + } + return undefined; }