6
6
#include < string>
7
7
8
8
#include " ../misc/log.h"
9
+ #include " nwn2heap.h"
9
10
extern std::unique_ptr<LogNWNX> logger;
10
11
11
12
constexpr uint32_t NWN_DEFAULT_EXECUTESCRIPT_ENH_PARAMS_LEN = 32 ;
@@ -79,25 +80,110 @@ static CVirtualMachine_ExecuteScriptEnhanced_t CVirtualMachine_ExecuteScriptEnha
79
80
80
81
namespace NWScript {
81
82
82
- void ExecuteScript (const char * sScript , NWN::OBJECTID oTarget, bool * outExecuted)
83
+ static int OriginalCNWSMsgAddr = 0 ;
84
+
85
+ int * GetPtrToCNWSMessage ()
86
+ {
87
+ int ptr = *(int *)OFFS_g_pAppManager;
88
+ ptr = *(int *)(ptr + 4 );
89
+ ptr = *(int *)(ptr + 4 );
90
+ return (int *)(ptr + 0x10020 );
91
+ }
92
+
93
+ void ApplyScriptCNWSMessage ()
94
+ {
95
+ int * ptrToCNWSMessage = GetPtrToCNWSMessage ();
96
+
97
+ static unsigned char * scriptCNWSMsg;
98
+ static bool isInit = false ;
99
+
100
+ if (!isInit) {
101
+ OriginalCNWSMsgAddr = *ptrToCNWSMessage;
102
+ NWN2_HeapMgr* pHeapMgr = NWN2_HeapMgr::Instance ();
103
+ NWN2_Heap* pHeap = pHeapMgr->GetDefaultHeap ();
104
+ scriptCNWSMsg = (unsigned char *)pHeap->Allocate (0x58 );
105
+ unsigned char * Msg1 = (unsigned char *)pHeap->Allocate (0x80 );
106
+ unsigned char * Msg2 = (unsigned char *)pHeap->Allocate (0x80 );
107
+
108
+ scriptCNWSMsg[0 ] = 0xC0 ;
109
+ scriptCNWSMsg[1 ] = 0x42 ;
110
+ scriptCNWSMsg[2 ] = 0x80 ;
111
+ scriptCNWSMsg[3 ] = 0x00 ;
112
+
113
+ ((uint32_t *)scriptCNWSMsg)[0x14 ] = 0xFFFFFFFF ;
114
+ ((uint32_t *)scriptCNWSMsg)[0x15 ] = 0x7F000000 ;
115
+
116
+ ((uint32_t *)scriptCNWSMsg)[1 ] = (int )Msg1;
117
+ ((uint32_t *)scriptCNWSMsg)[2 ] = 0x80 ;
118
+ ((uint32_t *)scriptCNWSMsg)[3 ] = 0x0 ;
119
+
120
+ ((uint32_t *)scriptCNWSMsg)[4 ] = (int )Msg2;
121
+ ((uint32_t *)scriptCNWSMsg)[5 ] = 0x80 ;
122
+ ((uint32_t *)scriptCNWSMsg)[6 ] = 0 ;
123
+ ((uint32_t *)scriptCNWSMsg)[7 ] = 0 ;
124
+
125
+ scriptCNWSMsg[0x20 ] = 0 ;
126
+
127
+ ((uint32_t *)scriptCNWSMsg)[9 ] = 0x0 ;
128
+ ((uint32_t *)scriptCNWSMsg)[0xa ] = 0x0 ;
129
+ ((uint32_t *)scriptCNWSMsg)[0xb ] = 0x0 ;
130
+ ((uint32_t *)scriptCNWSMsg)[0xc ] = 0x0 ;
131
+
132
+ ((uint32_t *)scriptCNWSMsg)[0xd ] = 0x0 ;
133
+ ((uint32_t *)scriptCNWSMsg)[0xe ] = 0x0 ;
134
+ ((uint32_t *)scriptCNWSMsg)[0xf ] = 0x0 ;
135
+ ((uint32_t *)scriptCNWSMsg)[0x10 ] = 0x0 ;
136
+ scriptCNWSMsg[0x44 ] = 0x0 ;
137
+ scriptCNWSMsg[0x45 ] = 0x0 ;
138
+ isInit = true ;
139
+ }
140
+
141
+ if (scriptCNWSMsg != NULL )
142
+ *ptrToCNWSMessage = (int )(scriptCNWSMsg);
143
+ }
144
+
145
+ void RestoreOriginalCNWSMessage ()
146
+ {
147
+ // Be sure to not restore before saving the Original CNWSMsg
148
+ if (OriginalCNWSMsgAddr != 0 ) {
149
+ int * ptrToCNWSMessage = GetPtrToCNWSMessage ();
150
+ *ptrToCNWSMessage = OriginalCNWSMsgAddr;
151
+ }
152
+ }
153
+
154
+ void ExecuteScript (const char * sScript ,
155
+ NWN::OBJECTID oTarget,
156
+ bool * outExecuted,
157
+ bool bReplaceCNWSMsg)
83
158
{
84
159
logger->Trace (" ExecuteScript %s, %lu" , sScript , oTarget);
160
+
161
+ if (bReplaceCNWSMsg)
162
+ ApplyScriptCNWSMessage ();
163
+
85
164
auto executed = CVirtualMachine_ExecuteScript (
86
165
*nwn2_vm,
87
166
NWN::CExoString {.m_sString = (char *)sScript , // un-const cast, safe as param is read only
88
167
.m_nBufferLength = strlen (sScript )},
89
168
oTarget, 1 , 1 );
90
169
if (outExecuted != nullptr )
91
170
*outExecuted = executed;
171
+
172
+ if (bReplaceCNWSMsg)
173
+ RestoreOriginalCNWSMessage ();
92
174
}
93
175
94
176
int32_t ExecuteScriptEnhanced (const char * sScriptName ,
95
177
NWN::OBJECTID oTarget,
96
178
bool bClearParams,
97
- bool * outExecuted)
179
+ bool * outExecuted,
180
+ bool bReplaceCNWSMsg)
98
181
{
99
182
logger->Trace (" ExecuteScriptEnhanced %s, %lu" , sScriptName , oTarget);
100
183
184
+ if (bReplaceCNWSMsg)
185
+ ApplyScriptCNWSMessage ();
186
+
101
187
const NWN::CExoString script
102
188
= {.m_sString = (char *)sScriptName , .m_nBufferLength = strlen (sScriptName )};
103
189
@@ -121,6 +207,9 @@ int32_t ExecuteScriptEnhanced(const char* sScriptName,
121
207
122
208
*nwn2_scriptparams = save;
123
209
210
+ if (bReplaceCNWSMsg)
211
+ RestoreOriginalCNWSMessage ();
212
+
124
213
return retValue;
125
214
}
126
215
void AddScriptParameterInt (int32_t nParam)
0 commit comments