-
Notifications
You must be signed in to change notification settings - Fork 1
/
ivscript_hack.h
1422 lines (1216 loc) · 65.6 KB
/
ivscript_hack.h
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
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========== Copyright ?2008, Valve Corporation, All rights reserved. ========
//
// Purpose: VScript
//
// Overview
// --------
// VScript is an abstract binding layer that allows code to expose itself to
// multiple scripting languages in a uniform format. Code can expose
// functions, classes, and data to the scripting languages, and can also
// call functions that reside in scripts.
//
// Initializing
// ------------
//
// To create a script virtual machine (VM), grab the global instance of
// IScriptManager, call CreateVM, then call Init on the returned VM. Right
// now you can have multiple VMs, but only VMs for a specific language.
//
// Exposing functions and classes
// ------------------------------
//
// To expose a C++ function to the scripting system, you just need to fill out a
// description block. Using templates, the system will automatically deduce
// all of the binding requirements (parameters and return values). Functions
// are limited as to what the types of the parameters can be. See ScriptVariant_t.
//
// extern IScriptVM *pScriptVM;
// bool Foo( int );
// void Bar();
// float FooBar( int, const char * );
// float OverlyTechnicalName( bool );
//
// void RegisterFuncs()
// {
// ScriptRegisterFunction( pScriptVM, Foo );
// ScriptRegisterFunction( pScriptVM, Bar );
// ScriptRegisterFunction( pScriptVM, FooBar );
// ScriptRegisterFunctionNamed( pScriptVM, OverlyTechnicalName, "SimpleName" );
// }
//
// class CMyClass
// {
// public:
// bool Foo( int );
// void Bar();
// float FooBar( int, const char * );
// float OverlyTechnicalName( bool );
// };
//
// BEGIN_SCRIPTDESC_ROOT( CMyClass )
// DEFINE_SCRIPTFUNC( Foo )
// DEFINE_SCRIPTFUNC( Bar )
// DEFINE_SCRIPTFUNC( FooBar )
// DEFINE_SCRIPTFUNC_NAMED( OverlyTechnicalName, "SimpleMemberName" )
// END_SCRIPTDESC();
//
// class CMyDerivedClass : public CMyClass
// {
// public:
// float DerivedFunc() const;
// };
//
// BEGIN_SCRIPTDESC( CMyDerivedClass, CMyClass )
// DEFINE_SCRIPTFUNC( DerivedFunc )
// END_SCRIPTDESC();
//
// CMyDerivedClass derivedInstance;
//
// void AnotherFunction()
// {
// // Manual class exposure
// pScriptVM->RegisterClass( GetScriptDescForClass( CMyClass ) );
//
// // Auto registration by instance
// pScriptVM->RegisterInstance( &derivedInstance, "theInstance" );
// }
//
// Classes with "DEFINE_SCRIPT_CONSTRUCTOR()" in their description can be instanced within scripts
//
// Scopes
// ------
// Scripts can either be run at the global scope, or in a user defined scope. In the latter case,
// all "globals" within the script are actually in the scope. This can be used to bind private
// data spaces with C++ objects.
//
// Calling a function on a script
// ------------------------------
// Generally, use the "Call" functions. This example is the equivalent of DoIt("Har", 6.0, 99).
//
// hFunction = pScriptVM->LookupFunction( "DoIt", hScope );
// pScriptVM->Call( hFunction, hScope, true, NULL, "Har", 6.0, 99 );
//
//=============================================================================
#ifndef IVSCRIPT_H
#define IVSCRIPT_H
#include "platform.h"
#include "datamap.h"
#include "appframework/IAppSystem.h"
#include "tier1/functors.h"
#include "tier0/memdbgon.h"
#if defined( _WIN32 )
#pragma once
#endif
#ifdef VSCRIPT_DLL_EXPORT
#define VSCRIPT_INTERFACE DLL_EXPORT
#define VSCRIPT_OVERLOAD DLL_GLOBAL_EXPORT
#define VSCRIPT_CLASS DLL_CLASS_EXPORT
#else
#define VSCRIPT_INTERFACE DLL_IMPORT
#define VSCRIPT_OVERLOAD DLL_GLOBAL_IMPORT
#define VSCRIPT_CLASS DLL_CLASS_IMPORT
#endif
class CUtlBuffer;
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
#define VSCRIPT_INTERFACE_VERSION "VScriptManager009"
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class IScriptVM;
enum ScriptLanguage_t
{
SL_NONE,
SL_GAMEMONKEY,
SL_SQUIRREL,
SL_LUA,
SL_PYTHON,
SL_DEFAULT = SL_SQUIRREL
};
class IScriptManager : public IAppSystem
{
public:
virtual IScriptVM* CreateVM(ScriptLanguage_t language = SL_DEFAULT) = 0;
virtual void DestroyVM(IScriptVM*) = 0;
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
DECLARE_POINTER_HANDLE(HSCRIPT);
#define INVALID_HSCRIPT ((HSCRIPT)-1)
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
enum ExtendedFieldType
{
FIELD_TYPEUNKNOWN = FIELD_TYPECOUNT,
FIELD_CSTRING,
FIELD_HSCRIPT,
FIELD_VARIANT,
};
typedef int ScriptDataType_t;
struct ScriptVariant_t;
template <typename T> struct ScriptDeducer { /*enum { FIELD_TYPE = FIELD_TYPEUNKNOWN };*/ };
#define DECLARE_DEDUCE_FIELDTYPE( fieldType, type ) template<> struct ScriptDeducer<type> { enum { FIELD_TYPE = fieldType }; };
DECLARE_DEDUCE_FIELDTYPE(FIELD_VOID, void);
DECLARE_DEDUCE_FIELDTYPE(FIELD_FLOAT, float);
DECLARE_DEDUCE_FIELDTYPE(FIELD_CSTRING, const char*);
DECLARE_DEDUCE_FIELDTYPE(FIELD_CSTRING, char*);
DECLARE_DEDUCE_FIELDTYPE(FIELD_VECTOR, Vector);
DECLARE_DEDUCE_FIELDTYPE(FIELD_VECTOR, const Vector&);
DECLARE_DEDUCE_FIELDTYPE(FIELD_INTEGER, int);
DECLARE_DEDUCE_FIELDTYPE(FIELD_BOOLEAN, bool);
DECLARE_DEDUCE_FIELDTYPE(FIELD_CHARACTER, char);
DECLARE_DEDUCE_FIELDTYPE(FIELD_HSCRIPT, HSCRIPT);
DECLARE_DEDUCE_FIELDTYPE(FIELD_VARIANT, ScriptVariant_t);
#define ScriptDeduceType( T ) ScriptDeducer<T>::FIELD_TYPE
template <typename T>
inline const char* ScriptFieldTypeName()
{
T::using_unknown_script_type();
}
#define DECLARE_NAMED_FIELDTYPE( fieldType, strName ) template <> inline const char * ScriptFieldTypeName<fieldType>() { return strName; }
DECLARE_NAMED_FIELDTYPE(void, "void");
DECLARE_NAMED_FIELDTYPE(float, "float");
DECLARE_NAMED_FIELDTYPE(const char*, "cstring");
DECLARE_NAMED_FIELDTYPE(char*, "cstring");
DECLARE_NAMED_FIELDTYPE(Vector, "vector");
DECLARE_NAMED_FIELDTYPE(const Vector&, "vector");
DECLARE_NAMED_FIELDTYPE(int, "integer");
DECLARE_NAMED_FIELDTYPE(bool, "boolean");
DECLARE_NAMED_FIELDTYPE(char, "character");
DECLARE_NAMED_FIELDTYPE(HSCRIPT, "hscript");
DECLARE_NAMED_FIELDTYPE(ScriptVariant_t, "variant");
inline const char* ScriptFieldTypeName(int16 eType)
{
switch (eType)
{
case FIELD_VOID: return "void";
case FIELD_FLOAT: return "float";
case FIELD_CSTRING: return "cstring";
case FIELD_VECTOR: return "vector";
case FIELD_INTEGER: return "integer";
case FIELD_BOOLEAN: return "boolean";
case FIELD_CHARACTER: return "character";
case FIELD_HSCRIPT: return "hscript";
case FIELD_VARIANT: return "variant";
default: return "unknown_script_type";
}
}
//---------------------------------------------------------
struct ScriptFuncDescriptor_t
{
ScriptFuncDescriptor_t()
{
m_pszFunction = NULL;
m_ReturnType = FIELD_TYPEUNKNOWN;
m_pszDescription = NULL;
}
const char* m_pszScriptName;
const char* m_pszFunction;
const char* m_pszDescription;
ScriptDataType_t m_ReturnType;
CUtlVector<ScriptDataType_t> m_Parameters;
};
//---------------------------------------------------------
// Prefix a script description with this in order to not show the function or class in help
#define SCRIPT_HIDE "@"
// Prefix a script description of a class to indicate it is a singleton and the single instance should be in the help
#define SCRIPT_SINGLETON "!"
// Prefix a script description with this to indicate it should be represented using an alternate name
#define SCRIPT_ALIAS( alias, description ) "#" alias ":" description
//---------------------------------------------------------
enum ScriptFuncBindingFlags_t
{
SF_MEMBER_FUNC = 0x01,
};
#ifdef _PS3
typedef void* ScriptFunctionBindingStorageType_t; // Function descriptor is actually 64-bit
#else
typedef void* ScriptFunctionBindingStorageType_t;
#endif
typedef bool (*ScriptBindingFunc_t)(ScriptFunctionBindingStorageType_t pFunction, void* pContext, ScriptVariant_t* pArguments, int nArguments, ScriptVariant_t* pReturn);
struct ScriptFunctionBinding_t
{
ScriptFuncDescriptor_t m_desc;
ScriptBindingFunc_t m_pfnBinding;
ScriptFunctionBindingStorageType_t m_pFunction;
unsigned m_flags;
};
//---------------------------------------------------------
class IScriptInstanceHelper
{
public:
virtual void* GetProxied(void* p) { return p; }
virtual bool ToString(void* p, char* pBuf, int bufSize) { return false; }
virtual void* BindOnRead(HSCRIPT hInstance, void* pOld, const char* pszId) { return NULL; }
};
//---------------------------------------------------------
struct ScriptClassDesc_t
{
ScriptClassDesc_t() : m_pszScriptName(0), m_pszClassname(0), m_pszDescription(0), m_pBaseDesc(0), m_pfnConstruct(0), m_pfnDestruct(0), pHelper(NULL) {}
const char* m_pszScriptName;
const char* m_pszClassname;
const char* m_pszDescription;
ScriptClassDesc_t* m_pBaseDesc;
CUtlVector<ScriptFunctionBinding_t> m_FunctionBindings;
void* (*m_pfnConstruct)();
void (*m_pfnDestruct)(void*);
IScriptInstanceHelper* pHelper; // optional helper
};
//---------------------------------------------------------
// A simple variant type. Intentionally not full featured (no implicit conversion, no memory management)
//---------------------------------------------------------
enum SVFlags_t
{
SV_FREE = 0x01,
};
#if defined _MSC_VER
# pragma warning(push)
# pragma warning(disable:4800)
#endif
struct ScriptVariant_t
{
ScriptVariant_t() : m_flags(0), m_type(FIELD_VOID) { m_pVector = 0; }
ScriptVariant_t(int val) : m_flags(0), m_type(FIELD_INTEGER) { m_int = val; }
ScriptVariant_t(float val) : m_flags(0), m_type(FIELD_FLOAT) { m_float = val; }
ScriptVariant_t(double val) : m_flags(0), m_type(FIELD_FLOAT) { m_float = (float)val; }
ScriptVariant_t(char val) : m_flags(0), m_type(FIELD_CHARACTER) { m_char = val; }
ScriptVariant_t(bool val) : m_flags(0), m_type(FIELD_BOOLEAN) { m_bool = val; }
ScriptVariant_t(HSCRIPT val) : m_flags(0), m_type(FIELD_HSCRIPT) { m_hScript = val; }
ScriptVariant_t(const Vector& val, bool bCopy = false) : m_flags(0), m_type(FIELD_VECTOR) { if (!bCopy) { m_pVector = &val; } else { m_pVector = new Vector(val); m_flags |= SV_FREE; } }
ScriptVariant_t(const Vector* val, bool bCopy = false) : m_flags(0), m_type(FIELD_VECTOR) { if (!bCopy) { m_pVector = val; } else { m_pVector = new Vector(*val); m_flags |= SV_FREE; } }
ScriptVariant_t(const char* val, bool bCopy = false) : m_flags(0), m_type(FIELD_CSTRING) { if (!bCopy) { m_pszString = val; } else { m_pszString = strdup(val); m_flags |= SV_FREE; } }
bool IsNull() const { return (m_type == FIELD_VOID); }
operator int() const { Assert(m_type == FIELD_INTEGER); return m_int; }
operator float() const { Assert(m_type == FIELD_FLOAT); return m_float; }
operator const char* () const { Assert(m_type == FIELD_CSTRING); return (m_pszString) ? m_pszString : ""; }
operator const Vector& () const { Assert(m_type == FIELD_VECTOR); static Vector vecNull(0, 0, 0); return (m_pVector) ? *m_pVector : vecNull; }
operator char() const { Assert(m_type == FIELD_CHARACTER); return m_char; }
operator bool() const { Assert(m_type == FIELD_BOOLEAN); return m_bool; }
operator HSCRIPT() const { Assert(m_type == FIELD_HSCRIPT); return m_hScript; }
void operator=(int i) { m_type = FIELD_INTEGER; m_int = i; }
void operator=(float f) { m_type = FIELD_FLOAT; m_float = f; }
void operator=(double f) { m_type = FIELD_FLOAT; m_float = (float)f; }
void operator=(const Vector& vec) { m_type = FIELD_VECTOR; m_pVector = &vec; }
void operator=(const Vector* vec) { m_type = FIELD_VECTOR; m_pVector = vec; }
void operator=(const char* psz) { m_type = FIELD_CSTRING; m_pszString = psz; }
void operator=(char c) { m_type = FIELD_CHARACTER; m_char = c; }
void operator=(bool b) { m_type = FIELD_BOOLEAN; m_bool = b; }
void operator=(HSCRIPT h) { m_type = FIELD_HSCRIPT; m_hScript = h; }
void Free() { if ((m_flags & SV_FREE) && (m_type == FIELD_HSCRIPT || m_type == FIELD_VECTOR || m_type == FIELD_CSTRING)) delete m_pszString; } // Generally only needed for return results
template <typename T>
T Get()
{
T value;
AssignTo(&value);
return value;
}
template <typename T>
bool AssignTo(T* pDest)
{
ScriptDataType_t destType = ScriptDeduceType(T);
if (destType == FIELD_TYPEUNKNOWN)
{
DevWarning("Unable to convert script variant to unknown type\n");
}
if (destType == m_type)
{
*pDest = *this;
return true;
}
if (m_type != FIELD_VECTOR && m_type != FIELD_CSTRING && destType != FIELD_VECTOR && destType != FIELD_CSTRING)
{
switch (m_type)
{
case FIELD_VOID: *pDest = 0; break;
case FIELD_INTEGER: *pDest = m_int; return true;
case FIELD_FLOAT: *pDest = m_float; return true;
case FIELD_CHARACTER: *pDest = m_char; return true;
case FIELD_BOOLEAN: *pDest = m_bool; return true;
case FIELD_HSCRIPT: *pDest = m_hScript; return true;
}
}
else
{
DevWarning("No free conversion of %s script variant to %s right now\n",
ScriptFieldTypeName(m_type), ScriptFieldTypeName<T>());
if (destType != FIELD_VECTOR)
{
*pDest = 0;
}
}
return false;
}
bool AssignTo(float* pDest)
{
switch (m_type)
{
case FIELD_VOID: *pDest = 0; return false;
case FIELD_INTEGER: *pDest = m_int; return true;
case FIELD_FLOAT: *pDest = m_float; return true;
case FIELD_BOOLEAN: *pDest = m_bool; return true;
default:
DevWarning("No conversion from %s to float now\n", ScriptFieldTypeName(m_type));
return false;
}
}
bool AssignTo(int* pDest)
{
switch (m_type)
{
case FIELD_VOID: *pDest = 0; return false;
case FIELD_INTEGER: *pDest = m_int; return true;
case FIELD_FLOAT: *pDest = m_float; return true;
case FIELD_BOOLEAN: *pDest = m_bool; return true;
default:
DevWarning("No conversion from %s to int now\n", ScriptFieldTypeName(m_type));
return false;
}
}
bool AssignTo(bool* pDest)
{
switch (m_type)
{
case FIELD_VOID: *pDest = 0; return false;
case FIELD_INTEGER: *pDest = m_int; return true;
case FIELD_FLOAT: *pDest = m_float; return true;
case FIELD_BOOLEAN: *pDest = m_bool; return true;
default:
DevWarning("No conversion from %s to bool now\n", ScriptFieldTypeName(m_type));
return false;
}
}
bool AssignTo(char** pDest)
{
DevWarning("No free conversion of string or vector script variant right now\n");
// If want to support this, probably need to malloc string and require free on other side [3/24/2008 tom]
*pDest = "";
return false;
}
bool AssignTo(ScriptVariant_t* pDest)
{
pDest->m_type = m_type;
if (m_type == FIELD_VECTOR)
{
pDest->m_pVector = new Vector;
((Vector*)(pDest->m_pVector))->Init(m_pVector->x, m_pVector->y, m_pVector->z);
pDest->m_flags |= SV_FREE;
}
else if (m_type == FIELD_CSTRING)
{
pDest->m_pszString = strdup(m_pszString);
pDest->m_flags |= SV_FREE;
}
else
{
pDest->m_int = m_int;
}
return false;
}
union
{
int m_int;
float m_float;
const char* m_pszString;
const Vector* m_pVector;
char m_char;
bool m_bool;
HSCRIPT m_hScript;
};
int16 m_type;
int16 m_flags;
private:
};
#define SCRIPT_VARIANT_NULL ScriptVariant_t()
#if defined _MSC_VER
# pragma warning(pop)
#endif
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
#include "vscript_templates_hack.h"
// Lower level macro primitives
#define ScriptInitFunctionBinding( pScriptFunction, func ) ScriptInitFunctionBindingNamed( pScriptFunction, func, #func )
#define ScriptInitFunctionBindingNamed( pScriptFunction, func, scriptName ) do { ScriptInitFuncDescriptorNamed( (&(pScriptFunction)->m_desc), func, scriptName ); (pScriptFunction)->m_pfnBinding = ScriptCreateBinding( &func ); (pScriptFunction)->m_pFunction = (void *)&func; } while (0)
#define ScriptInitMemberFunctionBinding( pScriptFunction, class, func ) ScriptInitMemberFunctionBinding_( pScriptFunction, class, func, #func )
#define ScriptInitMemberFunctionBindingNamed( pScriptFunction, class, func, scriptName ) ScriptInitMemberFunctionBinding_( pScriptFunction, class, func, scriptName )
#define ScriptInitMemberFunctionBinding_( pScriptFunction, class, func, scriptName ) do { ScriptInitMemberFuncDescriptor_( (&(pScriptFunction)->m_desc), class, func, scriptName ); (pScriptFunction)->m_pfnBinding = ScriptCreateBinding( ((class *)0), &class::func ); (pScriptFunction)->m_pFunction = ScriptConvertFuncPtrToVoid( &class::func ); (pScriptFunction)->m_flags = SF_MEMBER_FUNC; } while (0)
#define ScriptInitClassDesc( pClassDesc, class, pBaseClassDesc ) ScriptInitClassDescNamed( pClassDesc, class, pBaseClassDesc, #class )
#define ScriptInitClassDescNamed( pClassDesc, class, pBaseClassDesc, scriptName ) ScriptInitClassDescNamed_( pClassDesc, class, pBaseClassDesc, scriptName )
#define ScriptInitClassDescNoBase( pClassDesc, class ) ScriptInitClassDescNoBaseNamed( pClassDesc, class, #class )
#define ScriptInitClassDescNoBaseNamed( pClassDesc, class, scriptName ) ScriptInitClassDescNamed_( pClassDesc, class, NULL, scriptName )
#define ScriptInitClassDescNamed_( pClassDesc, class, pBaseClassDesc, scriptName ) do { (pClassDesc)->m_pszScriptName = scriptName; (pClassDesc)->m_pszClassname = #class; (pClassDesc)->m_pBaseDesc = pBaseClassDesc; } while ( 0 )
#define ScriptAddFunctionToClassDesc( pClassDesc, class, func, description ) ScriptAddFunctionToClassDescNamed( pClassDesc, class, func, #func, description )
#define ScriptAddFunctionToClassDescNamed( pClassDesc, class, func, scriptName, description ) do { ScriptFunctionBinding_t *pBinding = &((pClassDesc)->m_FunctionBindings[(pClassDesc)->m_FunctionBindings.AddToTail()]); pBinding->m_desc.m_pszDescription = description; ScriptInitMemberFunctionBindingNamed( pBinding, class, func, scriptName ); } while (0)
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
#define ScriptRegisterFunction( pVM, func, description ) ScriptRegisterFunctionNamed( pVM, func, #func, description )
#define ScriptRegisterFunctionNamed( pVM, func, scriptName, description ) do { static ScriptFunctionBinding_t binding; binding.m_desc.m_pszDescription = description; binding.m_desc.m_Parameters.RemoveAll(); ScriptInitFunctionBindingNamed( &binding, func, scriptName ); pVM->RegisterFunction( &binding ); } while (0)
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
#define ALLOW_SCRIPT_ACCESS() template <typename T> friend ScriptClassDesc_t *GetScriptDesc(T *);
#define BEGIN_SCRIPTDESC( className, baseClass, description ) BEGIN_SCRIPTDESC_NAMED( className, baseClass, #className, description )
#define BEGIN_SCRIPTDESC_ROOT( className, description ) BEGIN_SCRIPTDESC_ROOT_NAMED( className, #className, description )
#ifdef MSVC
#define DEFINE_SCRIPTDESC_FUNCTION( className, baseClass ) \
ScriptClassDesc_t * GetScriptDesc( className * )
#else
#define DEFINE_SCRIPTDESC_FUNCTION( className, baseClass ) \
template <> ScriptClassDesc_t * GetScriptDesc<baseClass>( baseClass *); \
template <> ScriptClassDesc_t * GetScriptDesc<className>( className *)
#endif
#define BEGIN_SCRIPTDESC_NAMED( className, baseClass, scriptName, description ) \
ScriptClassDesc_t g_##className##_ScriptDesc; \
DEFINE_SCRIPTDESC_FUNCTION( className, baseClass ) \
{ \
static bool bInitialized; \
if ( bInitialized ) \
{ \
return &g_##className##_ScriptDesc; \
} \
\
bInitialized = true; \
\
typedef className _className; \
ScriptClassDesc_t *pDesc = &g_##className##_ScriptDesc; \
pDesc->m_pszDescription = description; \
ScriptInitClassDescNamed( pDesc, className, GetScriptDescForClass( baseClass ), scriptName ); \
ScriptClassDesc_t *pInstanceHelperBase = pDesc->m_pBaseDesc; \
while ( pInstanceHelperBase ) \
{ \
if ( pInstanceHelperBase->pHelper ) \
{ \
pDesc->pHelper = pInstanceHelperBase->pHelper; \
break; \
} \
pInstanceHelperBase = pInstanceHelperBase->m_pBaseDesc; \
}
#define BEGIN_SCRIPTDESC_ROOT_NAMED( className, scriptName, description ) \
BEGIN_SCRIPTDESC_NAMED( className, ScriptNoBase_t, scriptName, description )
#define END_SCRIPTDESC() \
return pDesc; \
}
#define DEFINE_SCRIPTFUNC( func, description ) DEFINE_SCRIPTFUNC_NAMED( func, #func, description )
#define DEFINE_SCRIPTFUNC_NAMED( func, scriptName, description ) ScriptAddFunctionToClassDescNamed( pDesc, _className, func, scriptName, description );
#define DEFINE_SCRIPT_CONSTRUCTOR() ScriptAddConstructorToClassDesc( pDesc, _className );
#define DEFINE_SCRIPT_INSTANCE_HELPER( p ) pDesc->pHelper = (p);
template <typename T> ScriptClassDesc_t* GetScriptDesc(T*);
struct ScriptNoBase_t;
template <> inline ScriptClassDesc_t* GetScriptDesc<ScriptNoBase_t>(ScriptNoBase_t*) { return NULL; }
#define GetScriptDescForClass( className ) GetScriptDesc( ( className *)NULL )
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
template <typename T>
class CScriptConstructor
{
public:
static void* Construct() { return new T; }
static void Destruct(void* p) { delete (T*)p; }
};
#define ScriptAddConstructorToClassDesc( pClassDesc, class ) do { (pClassDesc)->m_pfnConstruct = &CScriptConstructor<class>::Construct; (pClassDesc)->m_pfnDestruct = &CScriptConstructor<class>::Destruct; } while (0)
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
enum ScriptErrorLevel_t
{
SCRIPT_LEVEL_WARNING = 0,
SCRIPT_LEVEL_ERROR,
};
typedef void (*ScriptOutputFunc_t)(const char* pszText);
typedef bool (*ScriptErrorFunc_t)(ScriptErrorLevel_t eLevel, const char* pszText);
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
#ifdef RegisterClass
#undef RegisterClass
#endif
enum ScriptStatus_t
{
SCRIPT_ERROR = -1,
SCRIPT_DONE,
SCRIPT_RUNNING,
};
class IScriptVM
{
public:
virtual bool Init() = 0;
virtual void Shutdown() = 0;
virtual bool ConnectDebugger() = 0;
virtual void DisconnectDebugger() = 0;
virtual ScriptLanguage_t GetLanguage() = 0;
virtual const char* GetLanguageName() = 0;
virtual void AddSearchPath(const char* pszSearchPath) = 0;
//--------------------------------------------------------
virtual bool Frame(float simTime) = 0;
//--------------------------------------------------------
// Simple script usage
//--------------------------------------------------------
virtual ScriptStatus_t Run(const char* pszScript, bool bWait = true) = 0;
inline ScriptStatus_t Run(const unsigned char* pszScript, bool bWait = true) { return Run((char*)pszScript, bWait); }
//--------------------------------------------------------
// Compilation
//--------------------------------------------------------
virtual HSCRIPT CompileScript(const char* pszScript, const char* pszId = NULL) = 0;
inline HSCRIPT CompileScript(const unsigned char* pszScript, const char* pszId = NULL) { return CompileScript((char*)pszScript, pszId); }
virtual void ReleaseScript(HSCRIPT) = 0;
//--------------------------------------------------------
// Execution of compiled
//--------------------------------------------------------
virtual ScriptStatus_t Run(HSCRIPT hScript, HSCRIPT hScope = NULL, bool bWait = true) = 0;
virtual ScriptStatus_t Run(HSCRIPT hScript, bool bWait) = 0;
//--------------------------------------------------------
// Scope
//--------------------------------------------------------
virtual HSCRIPT CreateScope(const char* pszScope, HSCRIPT hParent = NULL) = 0;
virtual void ReleaseScope(HSCRIPT hScript) = 0;
//--------------------------------------------------------
// Script functions
//--------------------------------------------------------
virtual HSCRIPT LookupFunction(const char* pszFunction, HSCRIPT hScope = NULL) = 0;
virtual void ReleaseFunction(HSCRIPT hScript) = 0;
//--------------------------------------------------------
// Script functions (raw, use Call())
//--------------------------------------------------------
virtual ScriptStatus_t ExecuteFunction(HSCRIPT hFunction, ScriptVariant_t* pArgs, int nArgs, ScriptVariant_t* pReturn, HSCRIPT hScope, bool bWait) = 0;
//--------------------------------------------------------
// External functions
//--------------------------------------------------------
virtual void RegisterFunction(ScriptFunctionBinding_t* pScriptFunction) = 0;
//--------------------------------------------------------
// External classes
//--------------------------------------------------------
virtual bool RegisterClass(ScriptClassDesc_t* pClassDesc) = 0;
//--------------------------------------------------------
// External instances. Note class will be auto-registered.
//--------------------------------------------------------
virtual HSCRIPT RegisterInstance(ScriptClassDesc_t* pDesc, void* pInstance) = 0;
virtual void SetInstanceUniqeId(HSCRIPT hInstance, const char* pszId) = 0;
template <typename T> HSCRIPT RegisterInstance(T* pInstance) { return RegisterInstance(GetScriptDesc(pInstance), pInstance); }
template <typename T> HSCRIPT RegisterInstance(T* pInstance, const char* pszInstance, HSCRIPT hScope = NULL) { HSCRIPT hInstance = RegisterInstance(GetScriptDesc(pInstance), pInstance); SetValue(hScope, pszInstance, hInstance); return hInstance; }
virtual void RemoveInstance(HSCRIPT) = 0;
void RemoveInstance(HSCRIPT hInstance, const char* pszInstance, HSCRIPT hScope = NULL) { ClearValue(hScope, pszInstance); RemoveInstance(hInstance); }
void RemoveInstance(const char* pszInstance, HSCRIPT hScope = NULL) { ScriptVariant_t val; if (GetValue(hScope, pszInstance, &val)) { if (val.m_type == FIELD_HSCRIPT) { RemoveInstance(val, pszInstance, hScope); } ReleaseValue(val); } }
virtual void* GetInstanceValue(HSCRIPT hInstance, ScriptClassDesc_t* pExpectedType = NULL) = 0;
//----------------------------------------------------------------------------
virtual bool GenerateUniqueKey(const char* pszRoot, char* pBuf, int nBufSize) = 0;
//----------------------------------------------------------------------------
virtual bool ValueExists(HSCRIPT hScope, const char* pszKey) = 0;
bool ValueExists(const char* pszKey) { return ValueExists(NULL, pszKey); }
virtual bool SetValue(HSCRIPT hScope, const char* pszKey, const char* pszValue) = 0;
virtual bool SetValue(HSCRIPT hScope, const char* pszKey, const ScriptVariant_t& value) = 0;
bool SetValue(const char* pszKey, const ScriptVariant_t& value) { return SetValue(NULL, pszKey, value); }
virtual void CreateTable(ScriptVariant_t& Table) = 0;
virtual int GetNumTableEntries(HSCRIPT hScope) = 0;
virtual int GetKeyValue(HSCRIPT hScope, int nIterator, ScriptVariant_t* pKey, ScriptVariant_t* pValue) = 0;
virtual bool GetValue(HSCRIPT hScope, const char* pszKey, ScriptVariant_t* pValue) = 0;
bool GetValue(const char* pszKey, ScriptVariant_t* pValue) { return GetValue(NULL, pszKey, pValue); }
virtual void ReleaseValue(ScriptVariant_t& value) = 0;
virtual bool ClearValue(HSCRIPT hScope, const char* pszKey) = 0;
bool ClearValue(const char* pszKey) { return ClearValue(NULL, pszKey); }
//----------------------------------------------------------------------------
virtual void WriteState(CUtlBuffer* pBuffer) = 0;
virtual void ReadState(CUtlBuffer* pBuffer) = 0;
virtual void RemoveOrphanInstances() = 0;
virtual void DumpState() = 0;
virtual void SetOutputCallback(ScriptOutputFunc_t pFunc) = 0;
virtual void SetErrorCallback(ScriptErrorFunc_t pFunc) = 0;
//----------------------------------------------------------------------------
virtual bool RaiseException(const char* pszExceptionText) = 0;
//----------------------------------------------------------------------------
// Call API
//
// Note for string and vector return types, the caller must delete the pointed to memory
//----------------------------------------------------------------------------
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope = NULL, bool bWait = true, ScriptVariant_t* pReturn = NULL)
{
return ExecuteFunction(hFunction, NULL, 0, pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1)
{
ScriptVariant_t args[1]; args[0] = arg1;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2)
{
ScriptVariant_t args[2]; args[0] = arg1; args[1] = arg2;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2, typename ARG_TYPE_3>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2, ARG_TYPE_3 arg3)
{
ScriptVariant_t args[3]; args[0] = arg1; args[1] = arg2; args[2] = arg3;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2, typename ARG_TYPE_3, typename ARG_TYPE_4>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2, ARG_TYPE_3 arg3, ARG_TYPE_4 arg4)
{
ScriptVariant_t args[4]; args[0] = arg1; args[1] = arg2; args[2] = arg3; args[3] = arg4;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2, typename ARG_TYPE_3, typename ARG_TYPE_4, typename ARG_TYPE_5>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2, ARG_TYPE_3 arg3, ARG_TYPE_4 arg4, ARG_TYPE_5 arg5)
{
ScriptVariant_t args[5]; args[0] = arg1; args[1] = arg2; args[2] = arg3; args[3] = arg4; args[4] = arg5;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2, typename ARG_TYPE_3, typename ARG_TYPE_4, typename ARG_TYPE_5, typename ARG_TYPE_6>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2, ARG_TYPE_3 arg3, ARG_TYPE_4 arg4, ARG_TYPE_5 arg5, ARG_TYPE_6 arg6)
{
ScriptVariant_t args[6]; args[0] = arg1; args[1] = arg2; args[2] = arg3; args[3] = arg4; args[4] = arg5; args[5] = arg6;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2, typename ARG_TYPE_3, typename ARG_TYPE_4, typename ARG_TYPE_5, typename ARG_TYPE_6, typename ARG_TYPE_7>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2, ARG_TYPE_3 arg3, ARG_TYPE_4 arg4, ARG_TYPE_5 arg5, ARG_TYPE_6 arg6, ARG_TYPE_7 arg7)
{
ScriptVariant_t args[7]; args[0] = arg1; args[1] = arg2; args[2] = arg3; args[3] = arg4; args[4] = arg5; args[5] = arg6; args[6] = arg7;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2, typename ARG_TYPE_3, typename ARG_TYPE_4, typename ARG_TYPE_5, typename ARG_TYPE_6, typename ARG_TYPE_7, typename ARG_TYPE_8>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2, ARG_TYPE_3 arg3, ARG_TYPE_4 arg4, ARG_TYPE_5 arg5, ARG_TYPE_6 arg6, ARG_TYPE_7 arg7, ARG_TYPE_8 arg8)
{
ScriptVariant_t args[8]; args[0] = arg1; args[1] = arg2; args[2] = arg3; args[3] = arg4; args[4] = arg5; args[5] = arg6; args[6] = arg7; args[7] = arg8;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2, typename ARG_TYPE_3, typename ARG_TYPE_4, typename ARG_TYPE_5, typename ARG_TYPE_6, typename ARG_TYPE_7, typename ARG_TYPE_8, typename ARG_TYPE_9>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2, ARG_TYPE_3 arg3, ARG_TYPE_4 arg4, ARG_TYPE_5 arg5, ARG_TYPE_6 arg6, ARG_TYPE_7 arg7, ARG_TYPE_8 arg8, ARG_TYPE_9 arg9)
{
ScriptVariant_t args[9]; args[0] = arg1; args[1] = arg2; args[2] = arg3; args[3] = arg4; args[4] = arg5; args[5] = arg6; args[6] = arg7; args[7] = arg8; args[8] = arg9;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2, typename ARG_TYPE_3, typename ARG_TYPE_4, typename ARG_TYPE_5, typename ARG_TYPE_6, typename ARG_TYPE_7, typename ARG_TYPE_8, typename ARG_TYPE_9, typename ARG_TYPE_10>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2, ARG_TYPE_3 arg3, ARG_TYPE_4 arg4, ARG_TYPE_5 arg5, ARG_TYPE_6 arg6, ARG_TYPE_7 arg7, ARG_TYPE_8 arg8, ARG_TYPE_9 arg9, ARG_TYPE_10 arg10)
{
ScriptVariant_t args[10]; args[0] = arg1; args[1] = arg2; args[2] = arg3; args[3] = arg4; args[4] = arg5; args[5] = arg6; args[6] = arg7; args[7] = arg8; args[8] = arg9; args[9] = arg10;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2, typename ARG_TYPE_3, typename ARG_TYPE_4, typename ARG_TYPE_5, typename ARG_TYPE_6, typename ARG_TYPE_7, typename ARG_TYPE_8, typename ARG_TYPE_9, typename ARG_TYPE_10, typename ARG_TYPE_11>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2, ARG_TYPE_3 arg3, ARG_TYPE_4 arg4, ARG_TYPE_5 arg5, ARG_TYPE_6 arg6, ARG_TYPE_7 arg7, ARG_TYPE_8 arg8, ARG_TYPE_9 arg9, ARG_TYPE_10 arg10, ARG_TYPE_11 arg11)
{
ScriptVariant_t args[11]; args[0] = arg1; args[1] = arg2; args[2] = arg3; args[3] = arg4; args[4] = arg5; args[5] = arg6; args[6] = arg7; args[7] = arg8; args[8] = arg9; args[9] = arg10; args[10] = arg11;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2, typename ARG_TYPE_3, typename ARG_TYPE_4, typename ARG_TYPE_5, typename ARG_TYPE_6, typename ARG_TYPE_7, typename ARG_TYPE_8, typename ARG_TYPE_9, typename ARG_TYPE_10, typename ARG_TYPE_11, typename ARG_TYPE_12>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2, ARG_TYPE_3 arg3, ARG_TYPE_4 arg4, ARG_TYPE_5 arg5, ARG_TYPE_6 arg6, ARG_TYPE_7 arg7, ARG_TYPE_8 arg8, ARG_TYPE_9 arg9, ARG_TYPE_10 arg10, ARG_TYPE_11 arg11, ARG_TYPE_12 arg12)
{
ScriptVariant_t args[12]; args[0] = arg1; args[1] = arg2; args[2] = arg3; args[3] = arg4; args[4] = arg5; args[5] = arg6; args[6] = arg7; args[7] = arg8; args[8] = arg9; args[9] = arg10; args[10] = arg11; args[11] = arg12;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2, typename ARG_TYPE_3, typename ARG_TYPE_4, typename ARG_TYPE_5, typename ARG_TYPE_6, typename ARG_TYPE_7, typename ARG_TYPE_8, typename ARG_TYPE_9, typename ARG_TYPE_10, typename ARG_TYPE_11, typename ARG_TYPE_12, typename ARG_TYPE_13>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2, ARG_TYPE_3 arg3, ARG_TYPE_4 arg4, ARG_TYPE_5 arg5, ARG_TYPE_6 arg6, ARG_TYPE_7 arg7, ARG_TYPE_8 arg8, ARG_TYPE_9 arg9, ARG_TYPE_10 arg10, ARG_TYPE_11 arg11, ARG_TYPE_12 arg12, ARG_TYPE_13 arg13)
{
ScriptVariant_t args[13]; args[0] = arg1; args[1] = arg2; args[2] = arg3; args[3] = arg4; args[4] = arg5; args[5] = arg6; args[6] = arg7; args[7] = arg8; args[8] = arg9; args[9] = arg10; args[10] = arg11; args[11] = arg12; args[12] = arg13;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
template <typename ARG_TYPE_1, typename ARG_TYPE_2, typename ARG_TYPE_3, typename ARG_TYPE_4, typename ARG_TYPE_5, typename ARG_TYPE_6, typename ARG_TYPE_7, typename ARG_TYPE_8, typename ARG_TYPE_9, typename ARG_TYPE_10, typename ARG_TYPE_11, typename ARG_TYPE_12, typename ARG_TYPE_13, typename ARG_TYPE_14>
ScriptStatus_t Call(HSCRIPT hFunction, HSCRIPT hScope, bool bWait, ScriptVariant_t* pReturn, ARG_TYPE_1 arg1, ARG_TYPE_2 arg2, ARG_TYPE_3 arg3, ARG_TYPE_4 arg4, ARG_TYPE_5 arg5, ARG_TYPE_6 arg6, ARG_TYPE_7 arg7, ARG_TYPE_8 arg8, ARG_TYPE_9 arg9, ARG_TYPE_10 arg10, ARG_TYPE_11 arg11, ARG_TYPE_12 arg12, ARG_TYPE_13 arg13, ARG_TYPE_14 arg14)
{
ScriptVariant_t args[14]; args[0] = arg1; args[1] = arg2; args[2] = arg3; args[3] = arg4; args[4] = arg5; args[5] = arg6; args[6] = arg7; args[7] = arg8; args[8] = arg9; args[9] = arg10; args[10] = arg11; args[11] = arg12; args[12] = arg13; args[13] = arg14;
return ExecuteFunction(hFunction, args, ARRAYSIZE(args), pReturn, hScope, bWait);
}
};
//-----------------------------------------------------------------------------
// Script scope helper class
//-----------------------------------------------------------------------------
class CDefScriptScopeBase
{
public:
static IScriptVM* GetVM()
{
extern IScriptVM* g_pScriptVM;
return g_pScriptVM;
}
};
template <class BASE_CLASS = CDefScriptScopeBase>
class CScriptScopeT : public CDefScriptScopeBase
{
public:
CScriptScopeT() :
m_hScope(INVALID_HSCRIPT),
m_flags(0)
{
}
~CScriptScopeT()
{
Term();
}
bool IsInitialized()
{
return m_hScope != INVALID_HSCRIPT;
}
bool Init(const char* pszName)
{
m_hScope = GetVM()->CreateScope(pszName);
return (m_hScope != NULL);
}
bool Init(HSCRIPT hScope, bool bExternal = true)
{
if (bExternal)
{
m_flags |= EXTERNAL;
}
m_hScope = hScope;
return (m_hScope != NULL);
}
bool InitGlobal()
{
Assert(0); // todo [3/24/2008 tom]
m_hScope = GetVM()->CreateScope("");
return (m_hScope != NULL);
}
void Term()
{
if (m_hScope != INVALID_HSCRIPT)
{
IScriptVM* pVM = GetVM();
if (pVM)
{
for (int i = 0; i < m_FuncHandles.Count(); i++)
{
pVM->ReleaseFunction(*m_FuncHandles[i]);
}
}
m_FuncHandles.Purge();
if (m_hScope && pVM && !(m_flags & EXTERNAL))
{
pVM->ReleaseScope(m_hScope);
}
m_hScope = INVALID_HSCRIPT;
}
m_flags = 0;
}
void InvalidateCachedValues()
{
IScriptVM* pVM = GetVM();
for (int i = 0; i < m_FuncHandles.Count(); i++)
{
if (*m_FuncHandles[i])
pVM->ReleaseFunction(*m_FuncHandles[i]);
*m_FuncHandles[i] = INVALID_HSCRIPT;
}
m_FuncHandles.RemoveAll();
}
operator HSCRIPT()
{
return (m_hScope != INVALID_HSCRIPT) ? m_hScope : NULL;
}
bool ValueExists(const char* pszKey) { return GetVM()->ValueExists(m_hScope, pszKey); }
bool SetValue(const char* pszKey, const ScriptVariant_t& value) { return GetVM()->SetValue(m_hScope, pszKey, value); }
bool GetValue(const char* pszKey, ScriptVariant_t* pValue) { return GetVM()->GetValue(m_hScope, pszKey, pValue); }
void ReleaseValue(ScriptVariant_t& value) { GetVM()->ReleaseValue(value); }
bool ClearValue(const char* pszKey) { return GetVM()->ClearValue(m_hScope, pszKey); }
ScriptStatus_t Run(HSCRIPT hScript)
{
InvalidateCachedValues();
return GetVM()->Run(hScript, m_hScope);
}
ScriptStatus_t Run(const char* pszScriptText, const char* pszScriptName = NULL)
{
InvalidateCachedValues();
HSCRIPT hScript = GetVM()->CompileScript(pszScriptText, pszScriptName);
if (hScript)
{
ScriptStatus_t result = GetVM()->Run(hScript, m_hScope);
GetVM()->ReleaseScript(hScript);
return result;
}
return SCRIPT_ERROR;
}
ScriptStatus_t Run(const unsigned char* pszScriptText, const char* pszScriptName = NULL)
{
return Run((const char*)pszScriptText, pszScriptName);
}
HSCRIPT LookupFunction(const char* pszFunction)
{
return GetVM()->LookupFunction(pszFunction, m_hScope);
}
void ReleaseFunction(HSCRIPT hScript)
{
GetVM()->ReleaseFunction(hScript);
}
bool FunctionExists(const char* pszFunction)
{
HSCRIPT hFunction = GetVM()->LookupFunction(pszFunction, m_hScope);
GetVM()->ReleaseFunction(hFunction);
return (hFunction != NULL);