Skip to content

Commit

Permalink
refactor: Print offending script in case of bad commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mtijanic committed Mar 23, 2018
1 parent 38595f0 commit 2ad5e26
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
26 changes: 20 additions & 6 deletions Core/NWNXCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "Services/Tasks/Tasks.hpp"
#include "Services/Messaging/Messaging.hpp"
#include "Services/PerObjectStorage/PerObjectStorage.hpp"
#include "Utils.hpp"

#include <cstring>

Expand All @@ -31,6 +32,22 @@ bool CompareStringPrefix(const API::CExoString* str, const char *prefix)
return str && str->m_sString && str->m_nBufferLength >= len && std::strncmp(prefix, str->m_sString, len) == 0;
}

void NotifyLegacyCall(const char* str)
{
LOG_NOTICE("Legacy NWNX call detected: \"%s\" from %s.nss - ignored", str, Utils::GetCurrentScript().c_str());
const char *cmd = str + sizeof(NWNX_LEGACY_PREFIX) - 1;
if (!std::strncmp(cmd, "PUSH_ARGUMENT", std::strlen("PUSH_ARGUMENT")) ||
!std::strncmp(cmd, "CALL_FUNCTION", std::strlen("CALL_FUNCTION")) ||
!std::strncmp(cmd, "GET_RETURN_VALUE", std::strlen("GET_RETURN_VALUE")))
{
LOG_NOTICE(" Please recompile all scripts that include \"nwnx.nss\"");
}
else
{
LOG_NOTICE(" This is a leftover from 1.69 nwnx2 scripts.");
}
}

}

namespace Core {
Expand Down Expand Up @@ -273,8 +290,7 @@ void NWNXCore::SetStringHandler(API::CNWSScriptVarTable* thisPtr, API::CExoStrin
}
else if (CompareStringPrefix(index, NWNX_LEGACY_PREFIX))
{
LOG_NOTICE("Legacy NWNX call detected: \"%s\" - ignored", index);
LOG_NOTICE(" This is likely due to legacy 1.69 code in the module, or you didn't recompile scripts including \"nwnx.nss\"");
NotifyLegacyCall(index->CStr());
}

g_setStringHook->CallOriginal<void>(thisPtr, index, value);
Expand All @@ -289,8 +305,7 @@ API::Types::ObjectID NWNXCore::GetObjectHandler(API::CNWSScriptVarTable* thisPtr
}
else if (CompareStringPrefix(index, NWNX_LEGACY_PREFIX))
{
LOG_NOTICE("Legacy NWNX call detected: \"%s\" - ignored", index);
LOG_NOTICE(" This is likely due to legacy 1.69 code in the module, or you didn't recompile scripts including \"nwnx.nss\"");
NotifyLegacyCall(index->CStr());
}

return g_getObjectHook->CallOriginal<API::Types::ObjectID>(thisPtr, index);
Expand All @@ -306,8 +321,7 @@ API::CExoString NWNXCore::GetStringHandler(API::CNWSScriptVarTable* thisPtr, API
}
else if (CompareStringPrefix(index, NWNX_LEGACY_PREFIX))
{
LOG_NOTICE("Legacy NWNX call detected: \"%s\" - ignored", index);
LOG_NOTICE(" This is likely due to legacy 1.69 code in the module, or you didn't recompile scripts including \"nwnx.nss\"");
NotifyLegacyCall(index->CStr());
}

return g_getStringHook->CallOriginal<API::CExoString>(thisPtr, index);
Expand Down
14 changes: 14 additions & 0 deletions NWNXLib/Utils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include "Utils.hpp"
#include "API/Globals.hpp"
#include "API/CAppManager.hpp"
#include "API/CServerExoApp.hpp"
#include "API/CVirtualMachine.hpp"
#include <sstream>

namespace NWNXLib {
Expand All @@ -12,6 +16,16 @@ std::string ObjectIDToString(const API::Types::ObjectID id)
return ss.str();
}

std::string GetCurrentScript()
{
auto *pVM = API::Globals::VirtualMachine();
return std::string(pVM->m_pVirtualMachineScript[pVM->m_nRecursionLevel].m_sScriptName.CStr());
}
void ExecuteScript(const std::string& script, API::Types::ObjectID oidOwner)
{
API::CExoString exoStr = script.c_str();
API::Globals::VirtualMachine()->RunScript(&exoStr, oidOwner, 1);
}

}
}
3 changes: 3 additions & 0 deletions NWNXLib/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace Utils {

std::string ObjectIDToString(const API::Types::ObjectID id);

std::string GetCurrentScript();
void ExecuteScript(const std::string& script, API::Types::ObjectID oidOwner);

}

}

0 comments on commit 2ad5e26

Please sign in to comment.