This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Main.cpp
370 lines (276 loc) · 9.05 KB
/
Main.cpp
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
#include <iostream>
#include <angelscript.h>
#include "Angelscript/CASManager.h"
#include "Angelscript/CASHook.h"
#include "Angelscript/CASModule.h"
#include "Angelscript/IASInitializer.h"
#include "Angelscript/IASModuleBuilder.h"
#include "Angelscript/add_on/scriptbuilder.h"
#include "Angelscript/add_on/scriptstdstring.h"
#include "Angelscript/add_on/scriptarray.h"
#include "Angelscript/add_on/scriptdictionary.h"
#include "Angelscript/add_on/scriptany.h"
#include "Angelscript/ScriptAPI/CASScheduler.h"
#include "Angelscript/ScriptAPI/Reflection/ASReflection.h"
#include "Angelscript/util/ASExtendAdapter.h"
#include "Angelscript/util/ASUtil.h"
#include "Angelscript/util/CASExtendAdapter.h"
#include "Angelscript/util/CASRefPtr.h"
#include "Angelscript/util/CASObjPtr.h"
#include "Angelscript/wrapper/ASCallable.h"
#include "Angelscript/wrapper/CASContext.h"
#include "CBaseEntity.h"
#include "CScriptBaseEntity.h"
#include "ASCBaseEntity.h"
namespace ModuleAccessMask
{
/**
* Access masks for modules.
*/
enum ModuleAccessMask
{
/**
* No access.
*/
NONE = 0,
/**
* Shared API.
*/
SHARED = 1 << 0,
/**
* Map script specific.
*/
MAPSCRIPT = SHARED | 1 << 1,
/**
* Plugin script specific.
*/
PLUGIN = SHARED | 1 << 2,
/**
* All scripts.
*/
ALL = SHARED | MAPSCRIPT | PLUGIN
};
}
void Print( const std::string& szString )
{
std::cout << szString;
}
/*
* A hook to test out the hook system.
* Stops as soon as it's handled.
* Can be hooked by calling g_Hooks.HookFunction( Hooks::Main, @MainHook( ... ) );
*/
CASHook hook( "Main", "const string& in", "", ModuleAccessMask::ALL, HookStopMode::ON_HANDLED );
class CASTestInitializer : public IASInitializer
{
public:
bool RegisterCoreAPI( CASManager& manager ) override
{
RegisterStdString( manager.GetEngine() );
RegisterScriptArray( manager.GetEngine(), true );
RegisterScriptDictionary( manager.GetEngine() );
RegisterScriptAny( manager.GetEngine() );
RegisterScriptScheduler( manager.GetEngine() );
RegisterScriptReflection( *manager.GetEngine() );
manager.GetEngine()->RegisterTypedef( "size_t", "uint32" );
return true;
}
bool AddHooks( CASManager& manager, CASHookManager& hookManager ) override
{
//Add a hook. Scripts will be able to hook these, when it's invoked by C++ code all hooked functions are called.
hookManager.AddHook( &hook );
return true;
}
bool RegisterAPI( CASManager& manager ) override
{
auto pEngine = manager.GetEngine();
//Printing function.
pEngine->RegisterGlobalFunction( "void Print(const string& in szString)", asFUNCTION( Print ), asCALL_CDECL );
//Register the interface that all custom entities use. Allows you to take them as handles to functions.
pEngine->RegisterInterface( "IScriptEntity" );
//Register the entity class.
RegisterScriptCBaseEntity( *pEngine );
//Register the entity base class. Used to call base class implementations.
RegisterScriptBaseEntity( *pEngine );
return true;
}
};
/**
* Builder for the test script.
*/
class CASTestModuleBuilder : public IASModuleBuilder
{
public:
CASTestModuleBuilder( const std::string& szDecl )
: m_szDecl( szDecl )
{
}
bool AddScripts( CScriptBuilder& builder ) override
{
//By using a handle this can be changed, but since there are no other instances, it can only be made null.
//TODO: figure out a better way.
auto result = builder.AddSectionFromMemory(
"__Globals",
"CScheduler@ Scheduler;" );
if( result < 0 )
return false;
if( builder.AddSectionFromMemory(
"__CScriptBaseEntity",
m_szDecl.c_str() ) < 0 )
return false;
return builder.AddSectionFromFile( "scripts/test.as" ) >= 0;
}
bool PostBuild( CScriptBuilder& builder, const bool bSuccess, CASModule* pModule ) override
{
if( !bSuccess )
return false;
auto& scriptModule = *pModule->GetModule();
//Set the scheduler instance.
if( !as::SetGlobalByName( scriptModule, "Scheduler", pModule->GetScheduler() ) )
return false;
return true;
}
private:
std::string m_szDecl;
};
int main( int iArgc, char* pszArgV[] )
{
std::cout << "Hello World!" << std::endl;
CASManager manager;
CASTestInitializer initializer;
if( manager.Initialize( initializer ) )
{
auto pEngine = manager.GetEngine();
//Create the declaration used for script entity base classes.
const auto szDecl = as::CreateExtendBaseclassDeclaration( "CScriptBaseEntity", "IScriptEntity", "CBaseEntity", "BaseEntity" );
std::cout << szDecl << std::endl;
//Create some module types.
//Map scripts are per-map scripts that always have their hooks executed before any other module.
manager.GetModuleManager().AddDescriptor( "MapScript", ModuleAccessMask::MAPSCRIPT, as::ModulePriority::HIGHEST );
//Plugins are persistent scripts that can keep running after map changes.
manager.GetModuleManager().AddDescriptor( "Plugin", ModuleAccessMask::PLUGIN );
//Make a map script.
CASTestModuleBuilder builder( szDecl );
auto pModule = manager.GetModuleManager().BuildModule( "MapScript", "MapModule", builder );
if( pModule )
{
//Call the main function.
if( auto pFunction = pModule->GetModule()->GetFunctionByName( "main" ) )
{
//Add main as a hook.
hook.AddFunction( pFunction );
std::string szString = "Hello World!\n";
//Note: main takes a const string& in, so pass the address here. References are handled as pointers.
as::Call( pFunction, &szString );
//Call the hook.
hook.Call( CallFlag::NONE, &szString );
}
//Test the object pointer.
if( auto pFunction = pModule->GetModule()->GetFunctionByName( "GetLifetime" ) )
{
CASOwningContext ctx( *manager.GetEngine() );
CASFunction func( *pFunction, ctx );
if( func.Call( CallFlag::NONE ) )
{
CASObjPtr ptr;
void* pThis;
if( func.GetReturnValue( &pThis ) )
{
const int iTypeId = pFunction->GetReturnTypeId();
ptr.Set( pThis, manager.GetEngine()->GetTypeInfoById( iTypeId ) );
}
if( ptr )
{
std::cout << "Object stored" << std::endl << "Type: " << ptr.GetTypeInfo()->GetName() << std::endl;
}
else
{
std::cout << "Object not stored" << std::endl;
}
}
}
//Call a function using the different function call helpers.
if( auto pFunction = pModule->GetModule()->GetFunctionByName( "NoArgs" ) )
{
{
//Test the smart pointer.
CASRefPtr<asIScriptFunction> func;
CASRefPtr<asIScriptFunction> func2( pFunction );
func = func2;
func = std::move( func2 );
CASRefPtr<asIScriptFunction> func3( func );
CASRefPtr<asIScriptFunction> func4( std::move( func ) );
func.Set( pFunction );
auto pPtr = func.Get();
}
//Regular varargs.
as::Call( pFunction );
//Argument list.
as::CallArgs( pFunction, CASArguments() );
auto pFunc = [ = ]( CallFlags_t flags, ... )
{
va_list list;
va_start( list, flags );
as::VCall( flags, pFunction, list );
va_end( list );
};
//va_list version.
pFunc( CallFlag::NONE );
}
//Test the scheduler.
pModule->GetScheduler()->Think( 10 );
//Get the parameter types. Angelscript's type info support isn't complete yet, so not all types have an asITypeInfo instance yet.
/*
if( auto pFunc2 = pModule->GetModule()->GetFunctionByName( "Function" ) )
{
for( asUINT uiIndex = 0; uiIndex < pFunc2->GetParamCount(); ++uiIndex )
{
int iTypeId;
const char* pszName;
pFunc2->GetParam( uiIndex, &iTypeId, nullptr, &pszName );
std::cout << "Parameter " << uiIndex << ": " << pszName << std::endl;
if( auto pType = manager.GetEngine()->GetTypeInfoById( iTypeId ) )
{
std::cout << pType->GetNamespace() << "::" << pType->GetName() << std::endl;
asDWORD uiFlags = pType->GetFlags();
if( uiFlags & asOBJ_VALUE )
std::cout << "Value" << std::endl;
if( uiFlags & asOBJ_REF )
std::cout << "Ref" << std::endl;
if( uiFlags & asOBJ_ENUM )
std::cout << "Enum" << std::endl;
if( uiFlags & asOBJ_FUNCDEF )
std::cout << "Funcdef" << std::endl;
if( uiFlags & asOBJ_POD )
std::cout << "POD" << std::endl;
if( uiFlags & asOBJ_TYPEDEF )
std::cout << "Typedef" << std::endl;
}
else //Only primitive types don't have type info right now.
std::cout << "No type info" << std::endl;
}
}
*/
//Try to create a C++ class that is extended in a script.
bool bCreatedExtend = false;
//This data will need to be stored somewhere, bound to the baseclass.
if( auto pEntity = as::CreateExtensionClassInstance<CScriptBaseEntity>( *pEngine, *pModule->GetModule(), "CEntity", "CBaseEntity", "BaseEntity" ) )
{
bCreatedExtend = true;
CBaseEntity* pBaseEnt = pEntity;
pBaseEnt->Spawn();
const int result = pBaseEnt->ScheduleOfType( "foo" );
int x = 10;
delete pEntity;
}
std::cout << "Created extend class: " << ( bCreatedExtend ? "yes" : "no" ) << std::endl;
//Remove the module.
manager.GetModuleManager().RemoveModule( pModule );
}
}
//Shut down the Angelscript engine, frees all resources.
manager.Shutdown();
//Wait for input.
getchar();
return 0;
}