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
#include "GS5.h"

#ifdef _MSC_VER
#include <Windows.h>
#endif

#include <cstdarg>
#include <cstdio>

namespace gs {

	//************** gs5_error *******************
#ifdef _MSC_VER
	gs5_error::gs5_error(const char* msg, int code ):std::exception(msg),_code(code){}
#else
	gs5_error::gs5_error(const char* msg, int code ):_msg(msg), _code(code){

	}
#endif

	NORETURN void gs5_error::raise(int code, const char* message, ...){
		va_list list;
		va_start(list,message);
		char buf[1024];
#ifdef _MSC_VER
		vsnprintf_s( buf, sizeof(buf), _TRUNCATE, message, list);	  
#else
		vsnprintf(buf, sizeof(buf)-1, message, list);
#endif
		va_end(list);

		throw gs5_error(buf, code);
	}
	//***************** TGSObject *******************

	TGSObject::TGSObject(gs_handle_t handle): _handle(handle){
		assert(handle != INVALID_GS_HANDLE);
	};
	TGSObject::~TGSObject(){ gsCloseHandle(_handle); }

	//***************** TGSVariable *****************
	//--- Static helpers ---
	//
	const char* TGSVariable::getTypeName(var_type_t varType){
		return gsVariableTypeToString(varType);
	};

	//Permission conversion helpers
	int TGSVariable::AttrFromString(const char* permitStr){
		return gsVariableAttrFromString(permitStr);
	};
	std::string TGSVariable::AttrToString(int permit){
		char Result[32];
		return gsVariableAttrToString(permit, Result, 32);
	};

	//Setter
	void TGSVariable::fromString(const char* v){
		if (!gsSetVariableValueFromString(_handle, v))
			throw gs5_error("String conversion error", GS_ERROR_INVALID_VALUE);
	}
	void TGSVariable::fromInt(int v){
		if (!gsSetVariableValueFromInt(_handle, v))
			throw gs5_error("Int conversion error", GS_ERROR_INVALID_VALUE);
	}
	void TGSVariable::fromInt64(int64_t v){
		if (!gsSetVariableValueFromInt64(_handle, v))
			throw gs5_error("Int64 conversion error", GS_ERROR_INVALID_VALUE);
	}
	void TGSVariable::fromFloat(float v){
		if (!gsSetVariableValueFromFloat(_handle, v))
			throw gs5_error("Float conversion error", GS_ERROR_INVALID_VALUE);
	}
	void TGSVariable::fromDouble(double v){
		if (!gsSetVariableValueFromDouble(_handle, v))
			throw gs5_error("Double conversion error", GS_ERROR_INVALID_VALUE);
	}
	void TGSVariable::fromUTCTime(time_t t){
		if (!gsSetVariableValueFromTime(_handle, t))
			throw gs5_error("Time conversion error", GS_ERROR_INVALID_VALUE);
	}

	//Getter
	const char* TGSVariable::asString(){ 
		return gsGetVariableValueAsString(_handle);
	}
	
	int TGSVariable::asInt(){ 
		int Result;
		if (!gsGetVariableValueAsInt(_handle, Result))
			throw gs5_error("Int conversion error", GS_ERROR_INVALID_VALUE);
		return Result;
	}

	bool TGSVariable::asBool(){ 
		return asInt() != 0;
	}

	void TGSVariable::fromBool(bool v){
		this->fromInt(v?1:0);
	}
	
	int64_t TGSVariable::asInt64(){
		int64_t Result;
		if (!gsGetVariableValueAsInt64(_handle, Result))
			throw gs5_error("Int64 conversion error", GS_ERROR_INVALID_VALUE);
		return Result;
	}

	float TGSVariable::asFloat(){
		float Result;
		if (!gsGetVariableValueAsFloat(_handle, Result))
			throw gs5_error("Float conversion error", GS_ERROR_INVALID_VALUE);
		return Result;
	}

	double TGSVariable::asDouble(){
		double Result;
		if (!gsGetVariableValueAsDouble(_handle, Result))
			throw gs5_error("Double conversion error", GS_ERROR_INVALID_VALUE);
		return Result;
	}
	time_t TGSVariable::asUTCTime(){
		time_t Result;
		if (!gsGetVariableValueAsTime(_handle, Result))
			throw gs5_error("Time conversion error", GS_ERROR_INVALID_VALUE);
		return Result;
	}
		

	//Properties
	const char* TGSVariable::name() { return gsGetVariableName(_handle); }
	var_type_t TGSVariable::typeId(){ return gsGetVariableType(_handle);}
	std::string TGSVariable::attribute(){ return AttrToString(gsGetVariableAttr(_handle)); }

	template <> void TGSVariable::get<int>(int& v){ v = this->asInt();}
	/// gets value as 64-bit integer
	template <> void TGSVariable::get<int64_t>(int64_t& v){ v = this->asInt64();}
	/// gets value as float
	template <> void TGSVariable::get<float>(float& v){ v = this->asFloat();}
	/// gets value as double
	template <> void TGSVariable::get<double>(double& v){ v = this->asDouble();}
	/// gets value as string
	template <> void TGSVariable::get<std::string>(std::string& v){ v = this->asString();}

	/// sets value from 32-bit integer
	template <> void TGSVariable::set<int>(int v){ this->fromInt(v);}
	/// sets value from 64-bit integer
	template <> void TGSVariable::set<int64_t>(int64_t v){ this->fromInt64(v);}
	/// sets value from float
	template <> void TGSVariable::set<float>(float v){ this->fromFloat(v);}
	/// sets value from double
	template <> void TGSVariable::set<double>(double v){ this->fromDouble(v);}
	/// sets value from string
	template <> void TGSVariable::set<std::string>(std::string v){ this->fromString(v.c_str());}
	//************************ TGSAction ***************************

	TGSAction::TGSAction(gs_handle_t handle):TGSObject(handle){
		_totalParams = gsGetActionParamCount(_handle);
	}

	TGSVariable* TGSAction::getParamByIndex(int index){
		if( (index < 0) || (index >= _totalParams)){
			gs5_error::raise(GS_ERROR_INVALID_INDEX, "Index [%d] out of range [0, %d)", index, _totalParams);
		};
		return new TGSVariable( gsGetActionParamByIndex(_handle, index) );
	}
	
	TGSVariable* TGSAction::getParamByName(const char* name){
		gs_handle_t h = gsGetActionParamByName(_handle, name);
		if (h == INVALID_GS_HANDLE) 
			gs5_error::raise(GS_ERROR_INVALID_NAME, "Invalid Param Name [%s]", name);

		return new TGSVariable(h);
	}
	//Properties
	const char* TGSAction::name(){
		return gsGetActionName(_handle);
	}
	action_id_t TGSAction::id(){
		return gsGetActionId(_handle);
	}
	const char* TGSAction::description(){
		return gsGetActionDescription(_handle);
	}
	const char* TGSAction::whatToDo(){
		return gsGetActionString(_handle);
	}

	int TGSAction::paramCount(){
		return _totalParams;
	}

	//**************** TGSLicense **********************

	bool TGSLicense::bindToEntity(TGSEntity* entity){
		if (gsBindLicense(entity->handle(), this->handle())){
			_licensedEntity = entity;
			return true;
		}else
			return false;
	}
		
	TGSVariable * TGSLicense::getParamByIndex(int index){
		if ((index >= 0) && (index < paramCount())){
			return new TGSVariable(gsGetLicenseParamByIndex(_handle, index));
		}
		gs5_error::raise(GS_ERROR_INVALID_INDEX, "Index [%d] out of range [0, %d)", index, paramCount());
	}

	TGSVariable * TGSLicense::getParamByName(const char* name){
		gs_handle_t h = gsGetLicenseParamByName(_handle, name);
		if (h != INVALID_GS_HANDLE) return new TGSVariable(h);

		gs5_error::raise(GS_ERROR_INVALID_NAME, "Invalid Param Name [%s]", name);
	}
	
	std::string TGSLicense::getParamStr(const char* name){
		std::auto_ptr<TGSVariable> var(params(name));
		return var->asString();
	}
	void TGSLicense::setParamStr(const char* name, const char* v){
		std::auto_ptr<TGSVariable> var(params(name));
		var->fromString(v);
	}

	int TGSLicense::getParamInt(const char* name){
		std::auto_ptr<TGSVariable> var(params(name));
		return var->asInt();
	}

	void TGSLicense::setParamInt(const char* name, int v){
		std::auto_ptr<TGSVariable> var(params(name));
		var->fromInt(v);
	}
	
	int64_t TGSLicense::getParamInt64(const char* name){
		std::auto_ptr<TGSVariable> var(params(name));
		return var->asInt64();
	}
	void TGSLicense::setParamInt64(const char* name, int64_t v){
		std::auto_ptr<TGSVariable> var(params(name));
		var->fromInt64(v);
	}
	
	bool TGSLicense::getParamBool(const char* name){
		std::auto_ptr<TGSVariable> var(params(name));
		return var->asBool();
	}
	void TGSLicense::setParamBool(const char* name, bool v){
		std::auto_ptr<TGSVariable> var(params(name));
		var->fromBool(v);
	}

	float TGSLicense::getParamFloat(const char* name){
		std::auto_ptr<TGSVariable> var(params(name));
		return var->asFloat();
	}
	void TGSLicense::setParamFloat(const char* name, float v){
		std::auto_ptr<TGSVariable> var(params(name));
		var->fromFloat(v);
	}
	
	double TGSLicense::getParamDouble(const char* name){
		std::auto_ptr<TGSVariable> var(params(name));
		return var->asDouble();
	}
	void TGSLicense::setParamDouble(const char* name, double v){
		std::auto_ptr<TGSVariable> var(params(name));
		var->fromDouble(v);
	}
	
	time_t TGSLicense::getParamUTCTime(const char* name){
		std::auto_ptr<TGSVariable> var(params(name));
		return var->asUTCTime();
	}
	void TGSLicense::setParamUTCTime(const char* name, time_t v){
		std::auto_ptr<TGSVariable> var(params(name));
		var->fromUTCTime(v);
	}


	//Properties
	const char* TGSLicense::id(){
		return gsGetLicenseId(_handle);
	}

	const char* TGSLicense::name(){
		return gsGetLicenseName(_handle);
	}

	const char* TGSLicense::description(){
		return gsGetLicenseDescription(_handle);
	}

	TLicenseStatus TGSLicense::status(){
		return gsGetLicenseStatus(_handle);
	}

	bool TGSLicense::isValid(){
		return gsIsLicenseValid(_handle);
	}

  void TGSLicense::lock(){
    gsLockLicense(_handle);
  }

	TGSEntity * TGSLicense::licensedEntity() {
		return _licensedEntity;
	}

	std::string TGSLicense::getUnlockRequestCode(){
		std::auto_ptr<TGSRequest> req(TGSCore::getInstance()->createRequest());
		req->addAction(ACT_UNLOCK, this);
		return req->code();
	}

	int TGSLicense::paramCount(){
		return gsGetLicenseParamCount(_handle);
	}

	TGSVariable * TGSLicense::params(int index){
		return getParamByIndex(index);
	}

	TGSVariable * TGSLicense::params(const char* name){
		return getParamByName(name);
	}

	int TGSLicense::actionCount(){
		return gsGetActionInfoCount(_handle);
	}

	action_id_t TGSLicense::actionIds(int index){
		action_id_t Result;
		gsGetActionInfoByIndex(_handle, index, &Result);
		return Result;
	}

	const char* TGSLicense::actionNames(int index){
		action_id_t dummy;
		return gsGetActionInfoByIndex(_handle, index, &dummy);
	}

	// *************** TGSRequest ***************************
	//Global action targeting all entities
	TGSAction* TGSRequest::addAction(action_id_t actId){
		return this->addAction(actId, (const char*)NULL);
	}
	//Action targeting all licenses of an entity
	TGSAction* TGSRequest::addAction(action_id_t actId, TGSEntity * entity){
		return this->addAction(actId, entity->id());
	}

	TGSAction* TGSRequest::addAction(action_id_t actId, TGSLicense * lic){
		gs_handle_t h = gsAddRequestAction(_handle, actId, lic->handle());
		if (h != INVALID_GS_HANDLE) return new TGSAction(h);

		gs5_error::raise(GS_ERROR_INVALID_ACTION, "Invalid action (actId = %d) for target license (%s)", actId, lic->name());
	}

	TGSAction* TGSRequest::addAction(action_id_t actId, const char* entityId){
		gs_handle_t h = gsAddRequestActionEx(_handle, actId, entityId, NULL);
		if (h != INVALID_GS_HANDLE) return new TGSAction(h);

		gs5_error::raise(GS_ERROR_INVALID_ACTION, "Invalid action (actId = %d)", actId);
	}

	const char* TGSRequest::code(){
		return gsGetRequestCode(_handle);
	}
	//******************* TGSEntity ***************************

	bool TGSEntity::beginAccess(){
		return gsBeginAccessEntity(_handle);
	}
	bool TGSEntity::endAccess(){
		return gsEndAccessEntity(_handle);
	}

	std::string TGSEntity::getUnlockRequestCode(){
		std::auto_ptr<TGSRequest> req(TGSCore::getInstance()->createRequest());
		req->addAction(ACT_UNLOCK, this);
		return req->code();
	}

  bool TGSEntity::hasLicense(){
    return gsHasLicense(_handle);
  }

  TGSLicense* TGSEntity::getLicense(){
		gs_handle_t h = gsOpenLicense(_handle);
		if (h == INVALID_GS_HANDLE) 
			gs5_error::raise(GS_ERROR_INVALID_LICENSE, "No License Bundled to entity[%s]", name());
			
		return new TGSLicense(h, this);
  }
	//Properties
	unsigned int TGSEntity::attribute(){
		return gsGetEntityAttributes(_handle);
	}
	const char * TGSEntity::id(){
		return gsGetEntityId(_handle);
	}
	const char* TGSEntity::name(){
		return gsGetEntityName(_handle);
	}
	const char* TGSEntity::description(){
		return gsGetEntityDescription(_handle);
	}

  void TGSEntity::lock(){
    if (hasLicense()){
      std::auto_ptr<TGSLicense> lic(getLicense());
      lic->lock();
    }
  }

	//************** TGSCore *************************

	void WINAPI TGSCore::s_monitorCallback(int eventId, TEventHandle hEvent, void* usrData){
		((TGSCore*)usrData)->onEvent(eventId, hEvent);
	}

	void TGSCore::setAppEventHandler(TGSAppEventHandler handler, void * usrData){
		_appEventHandler = handler;
		_appEventUsrData = usrData;
	}
	void TGSCore::setLicenseEventHandler(TGSLicenseEventHandler handler, void * usrData){
		_licEventHandler = handler;
		_licEventUsrData = usrData;
	}
	void TGSCore::setEntityEventHandler(TGSEntityEventHandler handler, void * usrData){
		_entityEventHandler = handler;
		_entityEventUsrData = usrData;
	}

	void TGSCore::setUserEventHandler(TGSUserEventHandler handler, void * usrData){
		_userEventHandler = handler;
		_userEventUsrData = usrData;
	}

	void TGSCore::onEvent(int eventId, TEventHandle hEvent){
		TEventType evtType = gsGetEventType(hEvent);
		switch(evtType){
		case EVENT_TYPE_APP:
			{
				if(_appEventHandler) _appEventHandler(eventId, _appEventUsrData);
				break;
			}
		case EVENT_TYPE_LICENSE:
			{
				if(_licEventHandler) _licEventHandler(eventId, _licEventUsrData);
				break;
			}
		case EVENT_TYPE_ENTITY:
			{
				std::auto_ptr<TGSEntity> entity(new TGSEntity(gsGetEventSource(hEvent)));
				if(_entityEventHandler) _entityEventHandler(eventId, entity.get(), _entityEventUsrData);
				break;
			}

		case EVENT_TYPE_USER:
			{
				unsigned int evtDataSize;
				void * evtData = gsGetUserEventData(hEvent, &evtDataSize);
				if(_userEventHandler) _userEventHandler(eventId, evtData, evtDataSize, _userEventUsrData);
				break;
			}
		}
	}

	TGSCore::TGSCore(): _appEventHandler(NULL), _appEventUsrData(NULL),
		_licEventHandler(NULL), _licEventUsrData(NULL), _entityEventHandler(NULL), _entityEventUsrData(NULL),
    _userEventHandler(NULL), _userEventUsrData(NULL)
    {
		gsCreateMonitorEx(s_monitorCallback, this, "$SDK");
	}
	
	TGSCore::~TGSCore(){
		cleanUp();
	}

  static TGSCore* s_core = NULL;
	TGSCore* TGSCore::getInstance(){
		if(s_core == NULL){
			s_core = new TGSCore();
		}
		return s_core;
	}

  void TGSCore::finish(){
    if (s_core){
      TGSCore * p = s_core;
      s_core = NULL;

      if (p){
        delete p;
      }
    }
  }

	int TGSCore::cleanUp(){
		return gsCleanUp();
	}

	bool TGSCore::init(const char* productId, const char* productLic, const char* licPassword){
		return ( 0 == gsInit(productId, productLic, licPassword, NULL));
	}
  
  bool TGSCore::init(const char* productId, const unsigned char* pLicData, int licSize, const char* licPassword){
    return (0 == gsInit(productId, pLicData, licSize, licPassword, NULL));
  }

	//Convert event id to human readable string, for debug purpose
	const char* TGSCore::getEventName(int eventId){
		struct TEventIdName {
			int id;
			const char* name;
		};
		static TEventIdName s_id_names[] = {
			{ EVENT_APP_BEGIN, "EVENT_APP_BEGIN"},
			{ EVENT_APP_RUN, "EVENT_APP_RUN"},
			{ EVENT_APP_END, "EVENT_APP_END"},
			{ EVENT_APP_CLOCK_ROLLBACK, "EVENT_APP_CLOCK_ROLLBACK"},
			{ EVENT_APP_INTEGRITY_CORRUPT, "EVENT_APP_INTEGRITY_CORRUPT"},

			{ EVENT_PASS_BEGIN_RING1, "EVENT_PASS_BEGIN_RING1"},
			{ EVENT_PASS_BEGIN_RING2, "EVENT_PASS_BEGIN_RING2"},
			{ EVENT_PASS_END_RING1, "EVENT_PASS_END_RING1"},
			{ EVENT_PASS_END_RING2, "EVENT_PASS_END_RING2"},
			{ EVENT_PASS_CHANGE, "EVENT_PASS_CHANGE"},

			{ EVENT_LICENSE_NEWINSTALL, "EVENT_LICENSE_NEWINSTALL"},
			{ EVENT_LICENSE_READY, "EVENT_LICENSE_READY"},
			{ EVENT_LICENSE_FAIL, "EVENT_LICENSE_FAIL"},
			{ EVENT_LICENSE_LOADING, "EVENT_LICENSE_LOADING"},

			{ EVENT_ENTITY_TRY_ACCESS, "EVENT_ENTITY_TRY_ACCESS"},
			{ EVENT_ENTITY_ACCESS_STARTED, "EVENT_ENTITY_ACCESS_STARTED"},
			{ EVENT_ENTITY_ACCESS_ENDING, "EVENT_ENTITY_ACCESS_ENDING"},
			{ EVENT_ENTITY_ACCESS_ENDED, "EVENT_ENTITY_ACCESS_ENDED"},
			{ EVENT_ENTITY_ACCESS_INVALID, "EVENT_ENTITY_ACCESS_INVALID"},
			{ EVENT_ENTITY_ACCESS_HEARTBEAT, "EVENT_ENTITY_ACCESS_HEARTBEAT"},
			{ EVENT_ENTITY_ACTION_APPLIED, "EVENT_ENTITY_ACTION_APPLIED"}
		};

		for(unsigned int i = 0; i < sizeof(s_id_names)/sizeof(s_id_names[0]); i++){
			if(eventId == s_id_names[i].id) return s_id_names[i].name;
		}
		return "Unknown Event";
	}


	void TGSCore::flush(){ gsFlush(); }

	TGSEntity * TGSCore::getEntityByIndex(int index) const {
		int N = getTotalEntities();
		if ((index >= 0) && (index < N)) return new TGSEntity(gsOpenEntityByIndex(index));

		gs5_error::raise(GS_ERROR_INVALID_INDEX, "Index [%d] out of range [0, %d)", index, N);
	}

	TGSEntity * TGSCore::getEntityById(entity_id_t entityId ) const {
		gs_handle_t h = gsOpenEntityById(entityId);
		if(h != INVALID_GS_HANDLE) return new TGSEntity(h);

		gs5_error::raise(GS_ERROR_INVALID_ENTITY, "Invalid EntityId (%s)", entityId);
	}

	//Variables
	TGSVariable * TGSCore::addVariable(const char* varName, TVarType varType,
		unsigned int permission, const char* initValStr){
		return new TGSVariable(gsAddVariable(varName, varType, permission, initValStr));
	}

	bool TGSCore::removeVariable(const char* varName){
		return gsRemoveVariable(varName);
	}

	TGSVariable * TGSCore::getVariableByIndex(int index) const {
		gs_handle_t h = gsGetVariableByIndex(index);
		if (h != INVALID_GS_HANDLE) return new TGSVariable(h);

		gs5_error::raise(GS_ERROR_INVALID_INDEX, "Invalid Variable Index [%s]", index);
	}

	TGSVariable * TGSCore::getVariableByName(const char* name) const {
		gs_handle_t h = gsGetVariable(name);
		if (h != INVALID_GS_HANDLE) return new TGSVariable(h);

		gs5_error::raise(GS_ERROR_INVALID_NAME, "Invalid Variable Name [%s]", name);
	}

	//Request
	TGSRequest * TGSCore::createRequest(){
		return new TGSRequest(gsCreateRequest());
	}

  bool TGSCore::applyLicenseCode(const char* code, const char* sn, const char* snRef){
    return gsApplyLicenseCodeEx(code, sn, snRef);
  }

	//---------- Time Engine Service ------------
	void TGSCore::turnOnInternalTimer(){ gsTurnOnInternalTimer(); }
	void TGSCore::turnOffInternalTimer(){ gsTurnOffInternalTimer(); }
	bool TGSCore::isInternalTimerActive(){ return gsIsInternalTimerActive(); }
	void TGSCore::tickFromExternalTimer(){ gsTickFromExternalTimer(); }
	void TGSCore::pauseTimeEngine(){ gsPauseTimeEngine(); }
	void TGSCore::resumeTimeEngine(){ gsResumeTimeEngine(); }
	bool TGSCore::isTimeEngineActive(){ return gsIsTimeEngineActive(); }

	//-------- HTML Render -----------
	bool TGSCore::renderHTML(const char* url, const char* title, int width, int height){
		return gsRenderHTML(url, title, width, height);
	}
	bool TGSCore::renderHTML(const char* url, const char* title, int width, int height,
		bool resizable, bool exitAppWhenUIClosed, bool cleanUpAfterRendering){
		return gsRenderHTMLEx(url, title, width, height, resizable, exitAppWhenUIClosed, cleanUpAfterRendering);
	}

	const char* TGSCore::lastErrorMessage(){ return gsGetLastErrorMessage(); }
	int TGSCore::lastErrorCode(){ return gsGetLastErrorCode(); }

	const char* TGSCore::SDKVersion(){ return gsGetVersion(); }

	const char* TGSCore::productName(){ return gsGetProductName(); }
	const char* TGSCore::productId() { return gsGetProductId(); }
	int TGSCore::buildId(){ return gsGetBuildId(); }

	bool TGSCore::runInVM(){ return gsRunInsideVM(0xFFFFFFFF); }

	int TGSCore::getTotalEntities() const { return gsGetEntityCount(); }
	
  void TGSCore::lockAllEntities(){
    for (int i = 0; i < getTotalEntities(); i++){
      std::auto_ptr<TGSEntity> entity(getEntityByIndex(i));
      entity->lock();
    }
  }

  bool TGSCore::isAllEntitiesLocked() const {
    for (int i = 0; i < getTotalEntities(); i++){
      std::auto_ptr<TGSEntity> entity(getEntityByIndex(i));
      if (!entity->isLocked()) return false;
    }
    return true;
  }

	//Debug Helpers (v5.0.14.0+)
	bool TGSCore::isDebugVersion(){
		return gsIsDebugVersion();
	}

	void TGSCore::trace(const char* msg){
		gsTrace(msg);
	}


  std::string TGSCore::getFixRequestCode(){
	  std::auto_ptr<TGSRequest> req(this->createRequest());
	  req->addAction(ACT_FIX);
	  return req->code();
  }
  std::string TGSCore::getUnlockRequestCode(){
	  std::auto_ptr<TGSRequest> req(this->createRequest());
	  req->addAction(ACT_UNLOCK);
	  return req->code();
  }
  std::string TGSCore::getCleanRequestCode(){
	  std::auto_ptr<TGSRequest> req(this->createRequest());
	  req->addAction(ACT_CLEAN);
	  return req->code();
  }
  std::string TGSCore::getDummyRequestCode(){
	  std::auto_ptr<TGSRequest> req(this->createRequest());
	  req->addAction(ACT_DUMMY);
	  return req->code();
  }


/// Auto destroy TCore shared instance when not used.
struct TCoreDestroy{
  ~TCoreDestroy(){
    TGSCore::finish();
  }
};

static TCoreDestroy s_destroyCoreWhenNotUsed;


#if defined(DEBUG)||defined(_DEBUG)
	void DebugMsg(const char* msg){
#ifdef _WINDOWS
		OutputDebugStringA(msg);
#endif
		TGSCore::getInstance()->trace(msg);
	}
	void DebugMsgFmt(const char* format, ...){
		va_list list;
		va_start(list,format);
		char buf[1024];
#ifdef _MSC_VER
		vsnprintf_s( buf, sizeof(buf), _TRUNCATE, format, list);	  
#else
		vsnprintf(buf, sizeof(buf)-1, buf, list);
#endif
		va_end(list);

		DebugMsg(buf);
	}

 //Auto Debug >> << pairs
	TAutoDbg::~TAutoDbg(){
		DebugMsg((_msg + " <<").c_str());
	}

	TAutoDbg::TAutoDbg(const char* format,...){
		va_list list;
		va_start(list,format);
		char buf[1024];
	#ifdef _MSC_VER
		vsnprintf_s( buf, sizeof(buf), _TRUNCATE, format,list);
	#else
		vsnprintf( buf, sizeof(buf)-1, format,list);
	#endif
		_msg = buf;

		va_end(list);

		DebugMsg((_msg + " >>").c_str());
	}

#endif
};