/* * * * * * * * * * * * * * * * 
 * 	 ______     __  __     ______     __  __     ______     ______     __  __    
 *	/\  __ \   /\ \/\ \   /\  == \   /\ \_\ \   /\  __ \   /\  ___\   /\ \/ /    
 *	\ \ \/\ \  \ \ \_\ \  \ \  __<   \ \  __ \  \ \  __ \  \ \ \____  \ \  _"-.  
 *	 \ \_____\  \ \_____\  \ \_____\  \ \_\ \_\  \ \_\ \_\  \ \_____\  \ \_\ \_\ 
 *	  \/_____/   \/_____/   \/_____/   \/_/\/_/   \/_/\/_/   \/_____/   \/_/\/_/ 
 *
 *  - Version : 3L
 *  - By Oubliette
 * * * * * * * * * * * * * * * */

#include "oubhack.h"
#include "oubhack_client_hook.h"

ICHook::ICHook(){

	ILuaInterface* cLua = oubhack->GetLuaInterface( LUA_STATE_CLIENT );

	cLua->CreateTable();
	this->m_pClientHooks = cLua->IGetObject();

}

void ICHook::Close(){

	this->m_pClientHooks->UnReference();

}

void ICHook::SetHook( const char* ccName, ILuaObject* objFunc ){

	this->m_pClientHooks->SetMember( ccName, objFunc );

}

ILuaObject* ICHook::GetHook( const char* ccName ){

	return this->m_pClientHooks->GetMember( ccName );

}

bool ICHook::CallRunString( const char* ccName, const char* ccPath, const char* ccString ){

	if ( !this->GetHook( "RunString" )->isFunction() ) return true;

	ILuaObject* obj = this->GetHook( "RunString" );

	obj->Push();

	oubhack->GetLuaInterface( LUA_STATE_CLIENT )->PushString( ccName );
	oubhack->GetLuaInterface( LUA_STATE_CLIENT )->PushString( ccPath );
	oubhack->GetLuaInterface( LUA_STATE_CLIENT )->PushString( ccString );

	oubhack->GetLuaInterface( LUA_STATE_CLIENT )->Call( 3, 1 );

	bool bRes = oubhack->GetLuaInterface( LUA_STATE_CLIENT )->GetBool();
	oubhack->GetLuaInterface( LUA_STATE_CLIENT )->Pop();

	obj->UnReference();

	return bRes;

}

bool ICHook::CallClose( ){

	if ( !this->GetHook( "Shutdown" )->isFunction() ) return false;

	ILuaObject* obj = this->GetHook( "Shutdown" );

	obj->Push();

	oubhack->GetLuaInterface( LUA_STATE_CLIENT )->Call( 0, 1 );

	bool bRes = oubhack->GetLuaInterface( LUA_STATE_CLIENT )->GetBool();
	oubhack->GetLuaInterface( LUA_STATE_CLIENT )->Pop();

	obj->UnReference();

	return bRes;

}