/*	Renegade Scripts.dll
	Example Plugin Code
	Copyright 2007 Whitedragon(MDB), Jonathan Wilson

	This file is part of the Renegade scripts.dll
	The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
	the terms of the GNU General Public License as published by the Free
	Software Foundation; either version 2, or (at your option) any later
	version. See the file COPYING for more details.
	In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
	Only the source code to the module(s) containing the licenced code has to be released.
*/

/*  This is designed to serve as both an example on how to make a plugin and to give users the basic framework of a plugin.
    The plugin is simple: it creates an object creation hook and attaches the script "Plugin_Example_Script" to all objects.
	The script prints out a message whenever an object is created or destroyed.
	There are also examples of the new format for bhs.dll hooks.
*/

#include "scripts.h"
#include <stdarg.h>
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif


#include "engine.h"
#include "gmmain.h"
#include "plugin.h"


extern "C" 
{
	#include "LuaLib\lua.h"
	#include "LuaLib\lualib.h"
	#include "LuaLib\lauxlib.h"
}
#include <Winsock2.h>
#include "Misc.h"
#include "LuaManager.h"
#include "Hooks.h"


void Plugin_Load() 
{
	Load_Hooks();
	LuaManager::Load();
	LuaScriptManager::Load();
	LuaSocketManager::Load();

	LuaManager::ShowBanner();
	char basepath[64];
	GetCurrentDirectory(64, basepath);
	char pluginsdir[256];
	sprintf(pluginsdir, "%s\\LuaPlugins\\*", basepath);

	char *plugins[256];
	memset((void *)plugins, 0, 256*4);
	GetFiles(pluginsdir, plugins, 256);

	for(int i = 0; i < 256; i++)
	{
		char *x = plugins[i];
		if(!x)
		{
			continue;
		}
		char *path = new char[strlen(x)+strlen(basepath)+14];
		sprintf(path, "%s\\LuaPlugins\\%s", basepath, x);
		LuaManager::LoadLua(path);
		delete []path;
		delete []x;
	}

	AddCharacterPurchaseMonHook(LuaManager::Purchase_Hook, "OnCharacterPurchase");
	AddVehiclePurchaseMonHook(LuaManager::Purchase_Hook, "OnVehiclePurchase");
	AddPowerupPurchaseMonHook(LuaManager::Purchase_Hook, "OnPowerupPurchase");
	
	AddVersionHook(LuaManager::Call_Version_Hook);
	
	ObjectCreateHookStruct *_och = new ObjectCreateHookStruct;
	_och->hook = LuaManager::Call_Object_Hook;
	_och->data = 0;
	AddObjectCreateHook(_och);

	AddSerialHook(LuaManager::Call_Serial_Hook);
	AddLoadingEHook(LuaManager::Call_LoadingE_Hook);
	AddDamageHook(LuaManager::Call_Damage_Hook);
	AddChatEHook(LuaManager::Call_Chat_Hook);
	AddPingHook(LuaManager::Call_Ping_Hook);
	AddSuicideHook(LuaManager::Call_Suicide_Hook);
	AddRadioHook(LuaManager::Call_Radio_Hook);
	AddThinkHook(LuaManager::Call_Think_Hook);
}

void Plugin_Unload() 
{
	LuaSocketManager::Cleanup();
	LuaManager::Cleanup();
	LuaScriptManager::Cleanup();
	DestroyEngineMath();
}


extern "C" {

DLLEXPORT void SSGM_Chat_Hook(int ID, int Type, const wchar_t *Msg) 
{

}

DLLEXPORT void SSGM_Host_Hook(int ID, int Type, const char *Msg) 
{
	LuaManager::Call_Host_Hook(ID, Type, Msg);
}

DLLEXPORT void SSGM_Player_Join_Hook(int ID, const char *Nick) 
{
	LuaManager::Call_Player_Join_Hook(ID, Nick);
}

DLLEXPORT void SSGM_Player_Leave_Hook(int ID) 
{
	LuaManager::Call_Player_Leave_Hook(ID);
}

DLLEXPORT void SSGM_Level_Loaded_Hook() 
{
	LuaManager::Call_Level_Loaded_Hook();
}

DLLEXPORT void SSGM_GameOver_Hook() 
{
	LuaManager::Call_GameOver_Hook();
}

DLLEXPORT void SSGM_Console_Output_Hook(const char *Output) 
{
	LuaManager::Call_Console_Output_Hook(Output);
}

DLLEXPORT bool SSGM_DDE_Hook(const char *DDE) 
{
	LuaManager::Call_DDE_Hook(DDE);
	return true;
}

}