1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
|
// LuaState.h
// Declares the cLuaState class representing the wrapper over lua_State *, provides associated helper functions
/*
The contained lua_State can be either owned or attached.
Owned lua_State is created by calling Create() and the cLuaState automatically closes the state
Or, lua_State can be attached by calling Attach(), the cLuaState doesn't close such a state
Attaching a state will automatically close an owned state.
Calling a Lua function is done by pushing the function, either by PushFunction() or PushFunctionFromRegistry(),
then pushing the arguments (PushString(), PushNumber(), PushUserData() etc.) and finally
executing CallFunction(). cLuaState automatically keeps track of the number of arguments and the name of the
function (for logging purposes), which makes the call less error-prone.
Reference management is provided by the cLuaState::cRef class. This is used when you need to hold a reference to
any Lua object across several function calls; usually this is used for callbacks. The class is RAII-like, with
automatic resource management.
*/
#pragma once
extern "C"
{
#include "lua/src/lauxlib.h"
}
class cWorld;
class cPlayer;
class cEntity;
class cMonster;
class cItem;
class cItems;
class cClientHandle;
class cPickup;
class cChunkDesc;
class cCraftingGrid;
class cCraftingRecipe;
struct TakeDamageInfo;
class cWindow;
class cPluginLua;
struct HTTPRequest;
class cWebAdmin;
struct HTTPTemplateRequest;
class cTNTEntity;
class cCreeper;
class Vector3i;
class cHopperEntity;
class cBlockEntity;
/// Encapsulates a Lua state and provides some syntactic sugar for common operations
class cLuaState
{
public:
/// Used for storing references to object in the global registry
class cRef
{
public:
/// Creates a reference in the specified LuaState for object at the specified StackPos
cRef(cLuaState & a_LuaState, int a_StackPos);
~cRef();
/// Returns true if the reference is valid
bool IsValid(void) const {return (m_Ref != LUA_REFNIL); }
/// Allows to use this class wherever an int (i. e. ref) is to be used
operator int(void) const { return m_Ref; }
protected:
cLuaState & m_LuaState;
int m_Ref;
} ;
/// A dummy class that's used only to delimit function args from return values for cLuaState::Call()
class cRet
{
} ;
static const cRet Return; // Use this constant to delimit function args from return values for cLuaState::Call()
/** Creates a new instance. The LuaState is not initialized.
a_SubsystemName is used for reporting problems in the console, it is "plugin %s" for plugins,
or "LuaScript" for the cLuaScript template
*/
cLuaState(const AString & a_SubsystemName);
/** Creates a new instance. The a_AttachState is attached.
Subsystem name is set to "<attached>".
*/
explicit cLuaState(lua_State * a_AttachState);
~cLuaState();
/// Allows this object to be used in the same way as a lua_State *, for example in the LuaLib functions
operator lua_State * (void) { return m_LuaState; }
/// Creates the m_LuaState, if not closed already. This state will be automatically closed in the destructor
void Create(void);
/// Closes the m_LuaState, if not closed already
void Close(void);
/// Attaches the specified state. Operations will be carried out on this state, but it will not be closed in the destructor
void Attach(lua_State * a_State);
/// Detaches a previously attached state.
void Detach(void);
/// Returns true if the m_LuaState is valid
bool IsValid(void) const { return (m_LuaState != NULL); }
/** Loads the specified file
Returns false and logs a warning to the console if not successful (but the LuaState is kept open).
m_SubsystemName is displayed in the warning log message.
*/
bool LoadFile(const AString & a_FileName);
/// Returns true if a_FunctionName is a valid Lua function that can be called
bool HasFunction(const char * a_FunctionName);
/** Pushes the function of the specified name onto the stack.
Returns true if successful. Logs a warning on failure (incl. m_SubsystemName)
*/
bool PushFunction(const char * a_FunctionName);
/** Pushes a function that has been saved into the global registry, identified by a_FnRef.
Returns true if successful. Logs a warning on failure
*/
bool PushFunction(int a_FnRef);
/** Pushes a function that is stored in a table ref.
Returns true if successful, false on failure. Doesn't log failure.
*/
bool PushFunctionFromRefTable(cRef & a_TableRef, const char * a_FnName);
/// Pushes a usertype of the specified class type onto the stack
void PushUserType(void * a_Object, const char * a_Type);
// Push a value onto the stack
void Push(const AString & a_String);
void Push(const AStringVector & a_Vector);
void Push(int a_Value);
void Push(double a_Value);
void Push(const char * a_Value);
void Push(bool a_Value);
void Push(cWorld * a_World);
void Push(cPlayer * a_Player);
void Push(const cPlayer * a_Player);
void Push(cEntity * a_Entity);
void Push(cMonster * a_Monster);
void Push(cItem * a_Item);
void Push(cItems * a_Items);
void Push(cClientHandle * a_ClientHandle);
void Push(cPickup * a_Pickup);
void Push(cChunkDesc * a_ChunkDesc);
void Push(const cCraftingGrid * a_Grid);
void Push(const cCraftingRecipe * a_Recipe);
void Push(TakeDamageInfo * a_TDI);
void Push(cWindow * a_Window);
void Push(cPluginLua * a_Plugin);
void Push(const HTTPRequest * a_Request);
void Push(cWebAdmin * a_WebAdmin);
void Push(const HTTPTemplateRequest * a_Request);
void Push(cTNTEntity * a_TNTEntity);
void Push(cCreeper * a_Creeper);
void Push(Vector3i * a_Vector);
void Push(void * a_Ptr);
void Push(cHopperEntity * a_Hopper);
void Push(cBlockEntity * a_BlockEntity);
/// Call any 0-param 0-return Lua function in a single line:
template <typename FnT>
bool Call(FnT a_FnName)
{
if (!PushFunction(a_FnName))
{
return false;
}
return CallFunction(0);
}
/// Call any 1-param 0-return Lua function in a single line:
template<
typename FnT,
typename ArgT1
>
bool Call(FnT a_FnName, ArgT1 a_Arg1)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
return CallFunction(0);
}
/// Call any 2-param 0-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
return CallFunction(0);
}
/// Call any 3-param 0-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
return CallFunction(0);
}
/// Call any 0-param 1-return Lua function in a single line:
template<
typename FnT, typename RetT1
>
bool Call(FnT a_FnName, const cRet & a_Mark, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
return false;
}
if (!CallFunction(1))
{
return false;
}
GetReturn(-1, a_Ret1);
lua_pop(m_LuaState, 1);
return true;
}
/// Call any 1-param 1-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename RetT1
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, const cRet & /*a_Mark*/, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
if (!CallFunction(1))
{
return false;
}
GetReturn(-1, a_Ret1);
lua_pop(m_LuaState, 1);
return true;
}
/// Call any 2-param 1-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename RetT1
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, const cRet & /*a_Mark*/, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
if (!CallFunction(1))
{
return false;
}
GetReturn(-1, a_Ret1);
lua_pop(m_LuaState, 1);
return true;
}
/// Call any 3-param 1-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename RetT1
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, const cRet & /*a_Mark*/, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
if (!CallFunction(1))
{
return false;
}
GetReturn(-1, a_Ret1);
lua_pop(m_LuaState, 1);
return true;
}
/// Call any 4-param 1-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename RetT1
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, const cRet & /*a_Mark*/, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
if (!CallFunction(1))
{
return false;
}
GetReturn(-1, a_Ret1);
lua_pop(m_LuaState, 1);
return true;
}
/// Call any 5-param 1-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename RetT1
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, const cRet & /*a_Mark*/, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
Push(a_Arg5);
if (!CallFunction(1))
{
return false;
}
GetReturn(-1, a_Ret1);
lua_pop(m_LuaState, 1);
return true;
}
/// Call any 6-param 1-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename RetT1
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, const cRet & /*a_Mark*/, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
Push(a_Arg5);
Push(a_Arg6);
if (!CallFunction(1))
{
return false;
}
GetReturn(-1, a_Ret1);
lua_pop(m_LuaState, 1);
return true;
}
/// Call any 7-param 1-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename ArgT7, typename RetT1
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, const cRet & /*a_Mark*/, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
Push(a_Arg5);
Push(a_Arg6);
Push(a_Arg7);
if (!CallFunction(1))
{
return false;
}
GetReturn(-1, a_Ret1);
lua_pop(m_LuaState, 1);
return true;
}
/// Call any 8-param 1-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename ArgT7, typename ArgT8, typename RetT1
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, const cRet & /*a_Mark*/, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
Push(a_Arg5);
Push(a_Arg6);
Push(a_Arg7);
Push(a_Arg8);
if (!CallFunction(1))
{
return false;
}
GetReturn(-1, a_Ret1);
lua_pop(m_LuaState, 1);
return true;
}
/// Call any 9-param 1-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename ArgT7, typename ArgT8, typename ArgT9, typename RetT1
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, ArgT9 a_Arg9, const cRet & /*a_Mark*/, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
Push(a_Arg5);
Push(a_Arg6);
Push(a_Arg7);
Push(a_Arg8);
Push(a_Arg9);
if (!CallFunction(1))
{
return false;
}
GetReturn(-1, a_Ret1);
lua_pop(m_LuaState, 1);
return true;
}
/// Call any 10-param 1-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename ArgT7, typename ArgT8, typename ArgT9, typename ArgT10, typename RetT1
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, ArgT9 a_Arg9, ArgT10 a_Arg10, const cRet & /*a_Mark*/, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
Push(a_Arg5);
Push(a_Arg6);
Push(a_Arg7);
Push(a_Arg8);
Push(a_Arg9);
Push(a_Arg10);
if (!CallFunction(1))
{
return false;
}
GetReturn(-1, a_Ret1);
lua_pop(m_LuaState, 1);
return true;
}
/// Call any 1-param 2-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename RetT1, typename RetT2
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, const cRet & /*a_Mark*/, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
if (!CallFunction(2))
{
return false;
}
GetReturn(-2, a_Ret1);
GetReturn(-1, a_Ret2);
lua_pop(m_LuaState, 2);
return true;
}
/// Call any 2-param 2-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename RetT1, typename RetT2
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, const cRet & /*a_Mark*/, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
if (!CallFunction(2))
{
return false;
}
GetReturn(-2, a_Ret1);
GetReturn(-1, a_Ret2);
lua_pop(m_LuaState, 2);
return true;
}
/// Call any 3-param 2-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3,
typename RetT1, typename RetT2
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, const cRet & /*a_Mark*/, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
if (!CallFunction(2))
{
return false;
}
GetReturn(-2, a_Ret1);
GetReturn(-1, a_Ret2);
lua_pop(m_LuaState, 2);
return true;
}
/// Call any 4-param 2-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4,
typename RetT1, typename RetT2
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, const cRet & /*a_Mark*/, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
if (!CallFunction(2))
{
return false;
}
GetReturn(-2, a_Ret1);
GetReturn(-1, a_Ret2);
lua_pop(m_LuaState, 2);
return true;
}
/// Call any 5-param 2-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename RetT1, typename RetT2
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, const cRet & /*a_Mark*/, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
Push(a_Arg5);
if (!CallFunction(2))
{
return false;
}
GetReturn(-2, a_Ret1);
GetReturn(-1, a_Ret2);
lua_pop(m_LuaState, 2);
return true;
}
/// Call any 6-param 2-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename ArgT6,
typename RetT1, typename RetT2
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, const cRet & /*a_Mark*/, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
Push(a_Arg5);
Push(a_Arg6);
if (!CallFunction(2))
{
return false;
}
GetReturn(-2, a_Ret1);
GetReturn(-1, a_Ret2);
lua_pop(m_LuaState, 2);
return true;
}
/// Call any 7-param 2-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename ArgT6, typename ArgT7,
typename RetT1, typename RetT2
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, const cRet & /*a_Mark*/, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
Push(a_Arg5);
Push(a_Arg6);
Push(a_Arg7);
if (!CallFunction(2))
{
return false;
}
GetReturn(-2, a_Ret1);
GetReturn(-1, a_Ret2);
lua_pop(m_LuaState, 2);
return true;
}
/// Call any 7-param 3-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename ArgT6, typename ArgT7,
typename RetT1, typename RetT2, typename RetT3
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, const cRet & /*a_Mark*/, RetT1 & a_Ret1, RetT2 & a_Ret2, RetT3 & a_Ret3)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
Push(a_Arg5);
Push(a_Arg6);
Push(a_Arg7);
if (!CallFunction(3))
{
return false;
}
GetReturn(-3, a_Ret1);
GetReturn(-2, a_Ret2);
GetReturn(-1, a_Ret3);
lua_pop(m_LuaState, 3);
return true;
}
/// Call any 8-param 3-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename ArgT6, typename ArgT7, typename ArgT8,
typename RetT1, typename RetT2, typename RetT3
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, const cRet & /*a_Mark*/, RetT1 & a_Ret1, RetT2 & a_Ret2, RetT3 & a_Ret3)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
Push(a_Arg5);
Push(a_Arg6);
Push(a_Arg7);
Push(a_Arg8);
if (!CallFunction(3))
{
return false;
}
GetReturn(-3, a_Ret1);
GetReturn(-2, a_Ret2);
GetReturn(-1, a_Ret3);
lua_pop(m_LuaState, 3);
return true;
}
/// Call any 9-param 5-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename ArgT6, typename ArgT7, typename ArgT8, typename ArgT9,
typename RetT1, typename RetT2, typename RetT3, typename RetT4, typename RetT5
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, ArgT9 a_Arg9, const cRet & /*a_Mark*/, RetT1 & a_Ret1, RetT2 & a_Ret2, RetT3 & a_Ret3, RetT4 & a_Ret4, RetT5 & a_Ret5)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
Push(a_Arg4);
Push(a_Arg5);
Push(a_Arg6);
Push(a_Arg7);
Push(a_Arg8);
Push(a_Arg9);
if (!CallFunction(5))
{
return false;
}
GetReturn(-5, a_Ret1);
GetReturn(-4, a_Ret2);
GetReturn(-3, a_Ret3);
GetReturn(-2, a_Ret4);
GetReturn(-1, a_Ret5);
lua_pop(m_LuaState, 5);
return true;
}
/// Retrieve value returned at a_StackPos, if it is a valid bool. If not, a_ReturnedVal is unchanged
void GetReturn(int a_StackPos, bool & a_ReturnedVal);
/// Retrieve value returned at a_StackPos, if it is a valid string. If not, a_ReturnedVal is unchanged
void GetReturn(int a_StackPos, AString & a_ReturnedVal);
/// Retrieve value returned at a_StackPos, if it is a valid number. If not, a_ReturnedVal is unchanged
void GetReturn(int a_StackPos, int & a_ReturnedVal);
/// Retrieve value returned at a_StackPos, if it is a valid number. If not, a_ReturnedVal is unchanged
void GetReturn(int a_StackPos, double & a_ReturnedVal);
/**
Calls the function that has been pushed onto the stack by PushFunction(),
with arguments pushed by PushXXX().
Returns true if successful, logs a warning on failure.
*/
bool CallFunction(int a_NumReturnValues);
/// Returns true if the specified parameters on the stack are of the specified usertable type; also logs warning if not. Used for static functions
bool CheckParamUserTable(int a_StartParam, const char * a_UserTable, int a_EndParam = -1);
/// Returns true if the specified parameters on the stack are of the specified usertype; also logs warning if not. Used for regular functions
bool CheckParamUserType(int a_StartParam, const char * a_UserType, int a_EndParam = -1);
/// Returns true if the specified parameters on the stack are tables; also logs warning if not
bool CheckParamTable(int a_StartParam, int a_EndParam = -1);
/// Returns true if the specified parameters on the stack are numbers; also logs warning if not
bool CheckParamNumber(int a_StartParam, int a_EndParam = -1);
/// Returns true if the specified parameters on the stack are strings; also logs warning if not
bool CheckParamString(int a_StartParam, int a_EndParam = -1);
/// Returns true if the specified parameter on the stack is nil (indicating an end-of-parameters)
bool CheckParamEnd(int a_Param);
/// If the status is nonzero, prints the text on the top of Lua stack and returns true
bool ReportErrors(int status);
/// If the status is nonzero, prints the text on the top of Lua stack and returns true
static bool ReportErrors(lua_State * a_LuaState, int status);
/// Logs all items in the current stack trace to the server console
void LogStackTrace(void);
/// Returns the type of the item on the specified position in the stack
AString GetTypeText(int a_StackPos);
protected:
lua_State * m_LuaState;
/// If true, the state is owned by this object and will be auto-Closed. False => attached state
bool m_IsOwned;
/** The subsystem name is used for reporting errors to the console, it is either "plugin %s" or "LuaScript"
whatever is given to the constructor
*/
AString m_SubsystemName;
/// Name of the currently pushed function (for the Push / Call chain)
AString m_CurrentFunctionName;
/// Number of arguments currently pushed (for the Push / Call chain)
int m_NumCurrentFunctionArgs;
} ;
|