From 870ee9fb5be96b01929f2bb0ea69d259737bc729 Mon Sep 17 00:00:00 2001 From: Lukas Joergensen Date: Sat, 21 Apr 2018 10:24:41 +0200 Subject: [PATCH] Integrate new CInterface into the engine-console --- Engine/source/app/mainLoop.cpp | 6 +++++ Engine/source/cinterface/c_simInterface.cpp | 5 ++-- .../cinterface/c_simobjectInterface.cpp | 2 -- Engine/source/console/codeInterpreter.cpp | 27 ++++++++++++++++--- Engine/source/console/console.cpp | 26 ++++++++++++++++++ Engine/source/module/moduleManager.cpp | 9 +++++++ 6 files changed, 67 insertions(+), 8 deletions(-) diff --git a/Engine/source/app/mainLoop.cpp b/Engine/source/app/mainLoop.cpp index 36998169f..ea2322d81 100644 --- a/Engine/source/app/mainLoop.cpp +++ b/Engine/source/app/mainLoop.cpp @@ -61,6 +61,7 @@ // For the TickMs define... fix this for T2D... #include "T3D/gameBase/processList.h" +#include "cinterface/cinterface.h" #ifdef TORQUE_ENABLE_VFS #include "platform/platformVFS.h" @@ -443,6 +444,11 @@ bool StandardMainLoop::handleCommandLine( S32 argc, const char **argv ) // directly because the resource system restricts // access to the "root" directory. + bool foundExternalMain = false; + CInterface::CallMain(&foundExternalMain); + if (foundExternalMain) + return true; + #ifdef TORQUE_ENABLE_VFS Zip::ZipArchive *vfs = openEmbeddedVFSArchive(); bool useVFS = vfs != NULL; diff --git a/Engine/source/cinterface/c_simInterface.cpp b/Engine/source/cinterface/c_simInterface.cpp index 26130a685..d3b0ea58f 100644 --- a/Engine/source/cinterface/c_simInterface.cpp +++ b/Engine/source/cinterface/c_simInterface.cpp @@ -42,12 +42,13 @@ namespace Sim return Sim::getDataBlockGroup()->findObject(StringTable->insert(pName)); } - DefineNewEngineFunction(WrapObject, SimObjectPtr*, (SimObject* pObject), , "") + // EngineAPI doesn't work with SimObjectPtr + TORQUE_API SimObjectPtr* fnWrapObject (SimObject* pObject) { return new SimObjectPtr(pObject); } - DefineNewEngineFunction(DeleteObjectPtr, void, (SimObjectPtr* pObjectPtr), , "") + TORQUE_API void fnDeleteObjectPtr(SimObjectPtr* pObjectPtr) { delete pObjectPtr; } diff --git a/Engine/source/cinterface/c_simobjectInterface.cpp b/Engine/source/cinterface/c_simobjectInterface.cpp index 757ba243b..4c018c0ab 100644 --- a/Engine/source/cinterface/c_simobjectInterface.cpp +++ b/Engine/source/cinterface/c_simobjectInterface.cpp @@ -20,8 +20,6 @@ // IN THE SOFTWARE. //----------------------------------------------------------------------------- -#include "c_simobjectInterface.h" - #include "console/engineAPI.h" #include "console/simObject.h" diff --git a/Engine/source/console/codeInterpreter.cpp b/Engine/source/console/codeInterpreter.cpp index 46d58476e..294a14b2b 100644 --- a/Engine/source/console/codeInterpreter.cpp +++ b/Engine/source/console/codeInterpreter.cpp @@ -37,6 +37,7 @@ #include "core/strings/stringUnit.h" #include "console/console.h" #include "console/consoleInternal.h" +#include "cinterface/cinterface.h" //#define TORQUE_VALIDATE_STACK @@ -2023,7 +2024,7 @@ OPCodeReturn CodeInterpreter::op_callfunc_resolve(U32 &ip) // Try to look it up. mNSEntry = Namespace::find(fnNamespace)->lookup(fnName); - if (!mNSEntry) + if (!CInterface::GetCInterface().isMethod(fnNamespace, fnName) && !mNSEntry) { ip += 5; Con::warnf(ConsoleLogEntry::General, @@ -2051,6 +2052,7 @@ OPCodeReturn CodeInterpreter::op_callfunc(U32 &ip) U32 *code = mCodeBlock->code; + StringTableEntry fnNamespace = CodeToSTE(mCodeBlock->code, ip + 2); StringTableEntry fnName = CodeToSTE(code, ip); //if this is called from inside a function, append the ip and codeptr @@ -2068,10 +2070,16 @@ OPCodeReturn CodeInterpreter::op_callfunc(U32 &ip) const char *componentReturnValue = ""; Namespace *ns = NULL; + bool cFunctionRes = false; + const char* cRetRes = NULL; + if (callType == FuncCallExprNode::FunctionCall) { if (!mNSEntry) mNSEntry = Namespace::global()->lookup(fnName); + + StringStackWrapper args(mCallArgc, mCallArgv); + cRetRes = CInterface::GetCInterface().CallFunction(fnNamespace, fnName, args.argv, args.argc, &cFunctionRes); } else if (callType == FuncCallExprNode::MethodCall) { @@ -2102,6 +2110,9 @@ OPCodeReturn CodeInterpreter::op_callfunc(U32 &ip) mNSEntry = ns->lookup(fnName); else mNSEntry = NULL; + + StringStackWrapper args(mCallArgc, mCallArgv); + cRetRes = CInterface::GetCInterface().CallMethod(gEvalState.thisObject, fnName, args.argv, args.argc, &cFunctionRes); } else // it's a ParentCall { @@ -2128,7 +2139,7 @@ OPCodeReturn CodeInterpreter::op_callfunc(U32 &ip) nsUsage = mNSEntry->mUsage; routingId = 0; } - if (!mNSEntry || mExec.noCalls) + if (!cFunctionRes && (!mNSEntry || mExec.noCalls)) { if (!mExec.noCalls && !(routingId == MethodOnComponent)) { @@ -2152,11 +2163,19 @@ OPCodeReturn CodeInterpreter::op_callfunc(U32 &ip) // ConsoleFunctionType is for any function defined by script. // Any 'callback' type is an engine function that is exposed to script. - if (mNSEntry->mType == Namespace::Entry::ConsoleFunctionType) + if (mNSEntry->mType == Namespace::Entry::ConsoleFunctionType + || cFunctionRes) { ConsoleValueRef ret; - if (mNSEntry->mFunctionOffset) + if (cFunctionRes) + { + StringStackConsoleWrapper retVal(1, &cRetRes); + ret = retVal.argv[0]; + } + else if (mNSEntry->mFunctionOffset) + { ret = mNSEntry->mCode->exec(mNSEntry->mFunctionOffset, fnName, mNSEntry->mNamespace, mCallArgc, mCallArgv, false, mNSEntry->mPackage); + } STR.popFrame(); // Functions are assumed to return strings, so look ahead to see if we can skip the conversion diff --git a/Engine/source/console/console.cpp b/Engine/source/console/console.cpp index 55880f30f..eab14bbf8 100644 --- a/Engine/source/console/console.cpp +++ b/Engine/source/console/console.cpp @@ -40,6 +40,7 @@ #include #include "platform/threads/mutex.h" #include "core/util/journal/journal.h" +#include "cinterface/cinterface.h" extern StringStack STR; extern ConsoleValueStack CSTK; @@ -1488,6 +1489,18 @@ ConsoleValueRef evaluatef(const char* string, ...) // Internal execute for global function which does not save the stack ConsoleValueRef _internalExecute(S32 argc, ConsoleValueRef argv[]) { + const char** argv_str = static_cast(malloc((argc - 1) * sizeof(char *))); + for (int i = 0; i < argc - 1; i++) + { + argv_str[i] = argv[i + 1]; + } + bool result; + const char* methodRes = CInterface::CallFunction(NULL, argv[0], argv_str, argc - 1, &result); + if (result) + { + return ConsoleValueRef::fromValue(CSTK.pushString(methodRes)); + } + Namespace::Entry *ent; StringTableEntry funcName = StringTable->insert(argv[0]); ent = Namespace::global()->lookup(funcName); @@ -1559,6 +1572,18 @@ ConsoleValueRef _internalExecute(SimObject *object, S32 argc, ConsoleValueRef ar } } + const char** argv_str = static_cast(malloc((argc - 2) * sizeof(char *))); + for (int i = 0; i < argc - 2; i++) + { + argv_str[i] = argv[i + 2]; + } + bool result; + const char* methodRes = CInterface::CallMethod(object, argv[0], argv_str, argc - 2, &result); + if (result) + { + return ConsoleValueRef::fromValue(CSTK.pushString(methodRes)); + } + if(object->getNamespace()) { U32 ident = object->getId(); @@ -1655,6 +1680,7 @@ inline ConsoleValueRef _executef(S32 checkArgc, S32 argc, ConsoleValueRef *argv) //------------------------------------------------------------------------------ bool isFunction(const char *fn) { + if (CInterface::isMethod(NULL, fn)) return true; const char *string = StringTable->lookup(fn); if(!string) return false; diff --git a/Engine/source/module/moduleManager.cpp b/Engine/source/module/moduleManager.cpp index ec45ad918..c5d3b42f6 100644 --- a/Engine/source/module/moduleManager.cpp +++ b/Engine/source/module/moduleManager.cpp @@ -455,6 +455,15 @@ bool ModuleManager::loadModuleGroup( const char* pModuleGroup ) moduleGroup, pLoadReadyModuleDefinition->getModuleId(), pLoadReadyModuleDefinition->getVersionId(), pLoadReadyModuleDefinition->getModuleScriptFilePath() ); } } + else + { + // Is the create method available? + if (pScopeSet->isMethod(pLoadReadyModuleDefinition->getCreateFunction())) + { + // Yes, so call the create method. + Con::executef(pScopeSet, pLoadReadyModuleDefinition->getCreateFunction()); + } + } // Raise notifications. raiseModulePostLoadNotifications( pLoadReadyModuleDefinition );