@@ -185,84 +185,92 @@ public void Restart(
185
185
186
186
public void CallSaveStateEvent ( string name )
187
187
{
188
- try
188
+ using ( GuiAPI . ThisIsTheLuaAutoUnlockHack ( ) )
189
189
{
190
- foreach ( var lf in RegisteredFunctions . Where ( static l => l . Event == NamedLuaFunction . EVENT_TYPE_SAVESTATE ) . ToList ( ) )
190
+ try
191
191
{
192
- lf . Call ( name ) ;
192
+ foreach ( var lf in RegisteredFunctions . Where ( static l => l . Event == NamedLuaFunction . EVENT_TYPE_SAVESTATE ) . ToList ( ) )
193
+ {
194
+ lf . Call ( name ) ;
195
+ }
196
+ }
197
+ catch ( Exception e )
198
+ {
199
+ LogToLuaConsole ( $ "error running function attached by lua function event.onsavestate\n Error message: { e . Message } ") ;
193
200
}
194
- GuiAPI . ThisIsTheLuaAutounlockHack ( ) ;
195
- }
196
- catch ( Exception e )
197
- {
198
- GuiAPI . ThisIsTheLuaAutounlockHack ( ) ;
199
- LogToLuaConsole ( $ "error running function attached by lua function event.onsavestate\n Error message: { e . Message } ") ;
200
201
}
201
202
}
202
203
203
204
public void CallLoadStateEvent ( string name )
204
205
{
205
- try
206
+ using ( GuiAPI . ThisIsTheLuaAutoUnlockHack ( ) )
206
207
{
207
- foreach ( var lf in RegisteredFunctions . Where ( static l => l . Event == NamedLuaFunction . EVENT_TYPE_LOADSTATE ) . ToList ( ) )
208
+ try
208
209
{
209
- lf . Call ( name ) ;
210
+ foreach ( var lf in RegisteredFunctions . Where ( static l => l . Event == NamedLuaFunction . EVENT_TYPE_LOADSTATE ) . ToList ( ) )
211
+ {
212
+ lf . Call ( name ) ;
213
+ }
214
+ }
215
+ catch ( Exception e )
216
+ {
217
+ LogToLuaConsole ( $ "error running function attached by lua function event.onloadstate\n Error message: { e . Message } ") ;
210
218
}
211
- GuiAPI . ThisIsTheLuaAutounlockHack ( ) ;
212
- }
213
- catch ( Exception e )
214
- {
215
- GuiAPI . ThisIsTheLuaAutounlockHack ( ) ;
216
- LogToLuaConsole ( $ "error running function attached by lua function event.onloadstate\n Error message: { e . Message } ") ;
217
219
}
218
220
}
219
221
220
222
public void CallFrameBeforeEvent ( )
221
223
{
222
224
if ( IsUpdateSupressed ) return ;
223
- try
225
+
226
+ using ( GuiAPI . ThisIsTheLuaAutoUnlockHack ( ) )
224
227
{
225
- foreach ( var lf in RegisteredFunctions . Where ( static l => l . Event == NamedLuaFunction . EVENT_TYPE_PREFRAME ) . ToList ( ) )
228
+ try
229
+ {
230
+ foreach ( var lf in RegisteredFunctions . Where ( static l => l . Event == NamedLuaFunction . EVENT_TYPE_PREFRAME ) . ToList ( ) )
231
+ {
232
+ lf . Call ( ) ;
233
+ }
234
+ }
235
+ catch ( Exception e )
226
236
{
227
- lf . Call ( ) ;
237
+ LogToLuaConsole ( $ "error running function attached by lua function event.onframestart \n Error message: { e . Message } " ) ;
228
238
}
229
- GuiAPI . ThisIsTheLuaAutounlockHack ( ) ;
230
- }
231
- catch ( Exception e )
232
- {
233
- GuiAPI . ThisIsTheLuaAutounlockHack ( ) ;
234
- LogToLuaConsole ( $ "error running function attached by lua function event.onframestart\n Error message: { e . Message } ") ;
235
239
}
236
240
}
237
241
238
242
public void CallFrameAfterEvent ( )
239
243
{
240
244
if ( IsUpdateSupressed ) return ;
241
- try
245
+
246
+ using ( GuiAPI . ThisIsTheLuaAutoUnlockHack ( ) )
242
247
{
243
- foreach ( var lf in RegisteredFunctions . Where ( static l => l . Event == NamedLuaFunction . EVENT_TYPE_POSTFRAME ) . ToList ( ) )
248
+ try
244
249
{
245
- lf . Call ( ) ;
250
+ foreach ( var lf in RegisteredFunctions . Where ( static l => l . Event == NamedLuaFunction . EVENT_TYPE_POSTFRAME ) . ToList ( ) )
251
+ {
252
+ lf . Call ( ) ;
253
+ }
254
+ }
255
+ catch ( Exception e )
256
+ {
257
+ LogToLuaConsole ( $ "error running function attached by lua function event.onframeend\n Error message: { e . Message } ") ;
246
258
}
247
- GuiAPI . ThisIsTheLuaAutounlockHack ( ) ;
248
- }
249
- catch ( Exception e )
250
- {
251
- GuiAPI . ThisIsTheLuaAutounlockHack ( ) ;
252
- LogToLuaConsole ( $ "error running function attached by lua function event.onframeend\n Error message: { e . Message } ") ;
253
259
}
254
260
}
255
261
256
262
public void CallExitEvent ( LuaFile lf )
257
263
{
258
- foreach ( var exitCallback in RegisteredFunctions
259
- . Where ( l => l . Event == NamedLuaFunction . EVENT_TYPE_ENGINESTOP
260
- && ( l . LuaFile . Path == lf . Path || ReferenceEquals ( l . LuaFile . Thread , lf . Thread ) ) )
261
- . ToList ( ) )
264
+ using ( GuiAPI . ThisIsTheLuaAutoUnlockHack ( ) )
262
265
{
263
- exitCallback . Call ( ) ;
266
+ foreach ( var exitCallback in RegisteredFunctions
267
+ . Where ( l => l . Event == NamedLuaFunction . EVENT_TYPE_ENGINESTOP
268
+ && ( l . LuaFile . Path == lf . Path || ReferenceEquals ( l . LuaFile . Thread , lf . Thread ) ) )
269
+ . ToList ( ) )
270
+ {
271
+ exitCallback . Call ( ) ;
272
+ }
264
273
}
265
- GuiAPI . ThisIsTheLuaAutounlockHack ( ) ;
266
274
}
267
275
268
276
public void Close ( )
@@ -319,31 +327,31 @@ public void ExecuteString(string command)
319
327
320
328
public ( bool WaitForFrame , bool Terminated ) ResumeScript ( LuaFile lf )
321
329
{
322
- _currThread = lf . Thread ;
323
-
324
- try
330
+ using ( GuiAPI . ThisIsTheLuaAutoUnlockHack ( ) )
325
331
{
326
- LuaLibraryBase . SetCurrentThread ( lf ) ;
332
+ _currThread = lf . Thread ;
327
333
328
- var execResult = _currThread . Resume ( ) ;
329
- GuiAPI . ThisIsTheLuaAutounlockHack ( ) ;
334
+ try
335
+ {
336
+ LuaLibraryBase . SetCurrentThread ( lf ) ;
330
337
331
- _currThread = null ;
332
- var result = execResult == KeraLua . LuaStatus . OK
333
- ? ( WaitForFrame : false , Terminated : true ) // terminated
334
- : ( WaitForFrame : FrameAdvanceRequested , Terminated : false ) ; // yielded
338
+ var execResult = _currThread . Resume ( ) ;
335
339
336
- FrameAdvanceRequested = false ;
337
- return result ;
338
- }
339
- catch ( Exception )
340
- {
341
- GuiAPI . ThisIsTheLuaAutounlockHack ( ) ;
342
- throw ;
343
- }
344
- finally
345
- {
346
- LuaLibraryBase . ClearCurrentThread ( ) ;
340
+ _currThread = null ;
341
+ var result = execResult switch
342
+ {
343
+ KeraLua . LuaStatus . OK => ( WaitForFrame : false , Terminated : true ) ,
344
+ KeraLua . LuaStatus . Yield => ( WaitForFrame : FrameAdvanceRequested , Terminated : false ) ,
345
+ _ => throw new InvalidOperationException ( $ "{ nameof ( _currThread . Resume ) } () returned { execResult } ?")
346
+ } ;
347
+
348
+ FrameAdvanceRequested = false ;
349
+ return result ;
350
+ }
351
+ finally
352
+ {
353
+ LuaLibraryBase . ClearCurrentThread ( ) ;
354
+ }
347
355
}
348
356
}
349
357
0 commit comments