|
| 1 | + |
| 2 | +#include "scriptManagement.h" |
| 3 | +#include <bit> |
| 4 | +#include <cstdint> |
| 5 | +#include <cstring> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +#include "../misc/log.h" |
| 9 | +extern std::unique_ptr<LogNWNX> logger; |
| 10 | + |
| 11 | +constexpr uint32_t NWN_DEFAULT_EXECUTESCRIPT_ENH_PARAMS_LEN = 32; |
| 12 | + |
| 13 | +struct NWN2Param { |
| 14 | + uint32_t _; |
| 15 | + uint32_t value; |
| 16 | + uint32_t ptr; |
| 17 | + uint32_t size; |
| 18 | + uint32_t type; |
| 19 | +}; |
| 20 | +static_assert(sizeof(NWN2Param) == 5 * 4); |
| 21 | + |
| 22 | +static std::vector<NWN2Param> scriptparams; |
| 23 | + |
| 24 | +// 1.23 |
| 25 | +// g_pVirtualMachine |
| 26 | +constexpr uint32_t NWN2_OFFSET_CVIRTUALMACHINE = 0x00864424; |
| 27 | +// CVirtualMachine::ExecuteScript |
| 28 | +constexpr uint32_t NWN2_OFFSET_EXECUTESCRIPT = 0x0072B380; |
| 29 | +constexpr uint32_t NWN2_OFFSET_EXECUTESCRIPT_ENH = 0x0072B050; |
| 30 | + |
| 31 | +constexpr uint32_t NWN2_OFFSET_InitParam = 0x0055EA40; |
| 32 | +constexpr uint32_t NWN2_OFFSET_CleanParam = 0x006b5cd0; |
| 33 | + |
| 34 | +struct NWN2ParamsList { |
| 35 | + struct NWN2Param* list; |
| 36 | + size_t size; |
| 37 | +}; |
| 38 | +static_assert(sizeof(NWN2ParamsList) == 8); |
| 39 | + |
| 40 | +static struct NWN2ParamsList* nwn2_scriptparams = (struct NWN2ParamsList*)(0x0086F15C); |
| 41 | +// static size_t scriptparams_count = 0; |
| 42 | + |
| 43 | +struct CVirtualMachine { }; |
| 44 | +static CVirtualMachine** nwn2_vm |
| 45 | + = std::bit_cast<struct CVirtualMachine**>(NWN2_OFFSET_CVIRTUALMACHINE); |
| 46 | + |
| 47 | +// Function hooks |
| 48 | +using CVirtualMachine_ExecuteScript_t = BOOL(__thiscall*)(CVirtualMachine* thisVM, |
| 49 | + const NWN::CExoString& scriptName, |
| 50 | + NWN::OBJECTID objectId, |
| 51 | + uint32_t unknown1, |
| 52 | + uint32_t unknown2); |
| 53 | +static CVirtualMachine_ExecuteScript_t CVirtualMachine_ExecuteScript |
| 54 | + = std::bit_cast<CVirtualMachine_ExecuteScript_t>(NWN2_OFFSET_EXECUTESCRIPT); |
| 55 | + |
| 56 | +// |
| 57 | +using CVirtualMachine_ExecuteScriptEnhanced_t |
| 58 | + = int32_t(__thiscall*)(CVirtualMachine* thisVM, |
| 59 | + const NWN::CExoString& scriptName, |
| 60 | + // const NWN::CExoString& scriptName, |
| 61 | + NWN::OBJECTID objectID, |
| 62 | + void* ParamList, |
| 63 | + uint32_t unknow1, |
| 64 | + uint32_t unknow2); |
| 65 | +static CVirtualMachine_ExecuteScriptEnhanced_t CVirtualMachine_ExecuteScriptEnhanced |
| 66 | + = std::bit_cast<CVirtualMachine_ExecuteScriptEnhanced_t>(NWN2_OFFSET_EXECUTESCRIPT_ENH); |
| 67 | + |
| 68 | +// |
| 69 | +using CVirtualMachine_InitParam_t = void(__thiscall*)(void* paramLst, uint32_t iNb); |
| 70 | +static CVirtualMachine_InitParam_t CVirtualMachine_InitParam |
| 71 | + = std::bit_cast<CVirtualMachine_InitParam_t>(NWN2_OFFSET_InitParam); |
| 72 | + |
| 73 | +// |
| 74 | +using CVirtualMachine_CleanParam_t = void(__thiscall*)(void* paramLst); |
| 75 | +static CVirtualMachine_CleanParam_t CVirtualMachine_CleanParam |
| 76 | + = std::bit_cast<CVirtualMachine_CleanParam_t>(NWN2_OFFSET_CleanParam); |
| 77 | + |
| 78 | +namespace NWScript { |
| 79 | + |
| 80 | +void ExecuteScript(const char* sScript, NWN::OBJECTID oTarget) |
| 81 | +{ |
| 82 | + logger->Trace("ExecuteScript %s, %lu", sScript, oTarget); |
| 83 | + CVirtualMachine_ExecuteScript( |
| 84 | + *nwn2_vm, |
| 85 | + NWN::CExoString {.m_sString = (char*)sScript, // un-const cast, safe as param is read only |
| 86 | + .m_nBufferLength = strlen(sScript)}, |
| 87 | + oTarget, 1, 1); |
| 88 | +} |
| 89 | + |
| 90 | +int32_t ExecuteScriptEnhanced(const char* sScriptName, NWN::OBJECTID oTarget, bool bClearParams) |
| 91 | +{ |
| 92 | + logger->Trace("ExecuteScriptEnhanced %s, %lu", sScriptName, oTarget); |
| 93 | + |
| 94 | + const NWN::CExoString script |
| 95 | + = {.m_sString = (char*)sScriptName, .m_nBufferLength = strlen(sScriptName)}; |
| 96 | + |
| 97 | + NWN2ParamsList save = *nwn2_scriptparams; |
| 98 | + |
| 99 | + nwn2_scriptparams->list = scriptparams.data(); |
| 100 | + nwn2_scriptparams->size = scriptparams.size(); |
| 101 | + |
| 102 | + // call the script |
| 103 | + int retValue |
| 104 | + = CVirtualMachine_ExecuteScriptEnhanced(*nwn2_vm, script, oTarget, nwn2_scriptparams, 1, 1); |
| 105 | + |
| 106 | + // Is the script ok? |
| 107 | + if (retValue != 0) |
| 108 | + retValue = ((uint32_t*)*nwn2_vm)[1]; |
| 109 | + else |
| 110 | + retValue = -1; |
| 111 | + |
| 112 | + *nwn2_scriptparams = save; |
| 113 | + |
| 114 | + return retValue; |
| 115 | +} |
| 116 | +void AddScriptParameterInt(int32_t nParam) |
| 117 | +{ |
| 118 | + logger->Trace("AddScriptParameterInt %d", nParam); |
| 119 | + |
| 120 | + scriptparams.push_back(NWN2Param { |
| 121 | + ._ = 0, |
| 122 | + .value = std::bit_cast<uint32_t>(nParam), |
| 123 | + .ptr = 0, |
| 124 | + .size = 0, |
| 125 | + .type = 0, |
| 126 | + }); |
| 127 | +} |
| 128 | +void AddScriptParameterString(const char* sParam) |
| 129 | +{ |
| 130 | + logger->Trace("AddScriptParameterString %s", sParam); |
| 131 | + |
| 132 | + scriptparams.push_back(NWN2Param { |
| 133 | + ._ = 0, |
| 134 | + .value = 0, |
| 135 | + .ptr = std::bit_cast<uint32_t>(sParam), |
| 136 | + .size = strlen(sParam) + 1, |
| 137 | + .type = 2, |
| 138 | + }); |
| 139 | +} |
| 140 | +void AddScriptParameterFloat(float fParam) |
| 141 | +{ |
| 142 | + logger->Trace("AddScriptParameterFloat %f", fParam); |
| 143 | + |
| 144 | + scriptparams.push_back(NWN2Param { |
| 145 | + ._ = 0, |
| 146 | + .value = std::bit_cast<uint32_t>(fParam), |
| 147 | + .ptr = 0, |
| 148 | + .size = 0, |
| 149 | + .type = 1, |
| 150 | + }); |
| 151 | +} |
| 152 | +void AddScriptParameterObject(NWN::OBJECTID oParam) |
| 153 | +{ |
| 154 | + logger->Trace("AddScriptParameterObject %lu", oParam); |
| 155 | + |
| 156 | + scriptparams.push_back(NWN2Param { |
| 157 | + ._ = 0, |
| 158 | + .value = oParam, |
| 159 | + .ptr = 0, |
| 160 | + .size = 0, |
| 161 | + .type = 4, |
| 162 | + }); |
| 163 | +} |
| 164 | + |
| 165 | +void ClearScriptParams() |
| 166 | +{ |
| 167 | + logger->Trace("ClearScriptParams"); |
| 168 | + scriptparams.clear(); |
| 169 | +} |
| 170 | +} // namespace NWScript |
0 commit comments