/*	Copyright © 2008, ConnorMcLeod

	Set Player Model 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.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Set Player Model; if not, write to the
	Free Software Foundation, Inc., 59 Temple Place - Suite 330,
	Boston, MA 02111-1307, USA.
*/

#include <amxmodx>
#include <fakemeta>

#define PLUGIN "Set Player Model"
#define AUTHOR "ConnorMcLeod"
#define VERSION "0.0.1"

#define MAX_MODEL_LENGTH	32
#define MAX_PLAYERS	32

new const MODEL[] = "model"

new g_szModel[MAX_PLAYERS+1][MAX_MODEL_LENGTH]

new g_iMaxPlayers

new g_iFwd, g_iMsg
new gmsgClCorpse

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)

	gmsgClCorpse = get_user_msgid("ClCorpse")
	g_iMaxPlayers = get_maxplayers()
}

public client_disconnect(id)
{
	g_szModel[id][0] = 0
	Set_Plugin_State(id, false)
}

public plugin_natives()
{
	register_library("playermodel")
	register_native("fm_set_user_model", "native_fm_set_user_model")
	register_native("fm_reset_user_model", "native_fm_reset_user_model")
}

public native_fm_set_user_model(iPlugin, iParams)
{
	new id = get_param(1)

	if( ! ( 1 <= id <= g_iMaxPlayers ) )
	{
		log_error(AMX_ERR_NATIVE, "Invalid index %d", id)
		return 0
	}

	if( !is_user_connected(id) )
	{
		log_error(AMX_ERR_NATIVE, "Player %d not connected", id)
		return 0
	}

	Set_Plugin_State(id, true)

	new szModel[MAX_MODEL_LENGTH]
	get_string(2, szModel, charsmax(szModel))

	formatex(g_szModel[id], charsmax(szModel), szModel)
	set_user_info(id, MODEL, g_szModel[id])

	return 1
}

public native_fm_reset_user_model(iPlugin, iParams)
{
	new id = get_param(1)

	if( ! ( 1 <= id <= g_iMaxPlayers ) )
	{
		log_error(AMX_ERR_NATIVE, "Invalid index %d", id)
		return 0
	}

	if(!g_szModel[id][0])
	{
		return 0
	}

	g_szModel[id][0] = 0

	if( is_user_connected(id) )
	{
		dllfunc(DLLFunc_ClientUserInfoChanged, id, engfunc(EngFunc_GetInfoKeyBuffer, id))
	}

	Set_Plugin_State(id, false)

	return 1
}

public SetClientKeyValue(id, szInfoBuffer[], szKey[], szValue[])
{
	if(g_szModel[id][0] && equal(szKey, MODEL) && !equal(szValue, g_szModel[id]))
	{
		set_user_info(id, MODEL, g_szModel[id])
		return FMRES_SUPERCEDE
	}
	return FMRES_IGNORED
}

public Message_ClCorpse()
{
	new id = get_msg_arg_int(12)
	if( g_szModel[id][0] )
	{
		set_msg_arg_string(1, g_szModel[id])
	}
}

Set_Plugin_State(id, bool:bActive)
{
	if( bActive )
	{
		if( !g_iFwd )
		{
			g_iFwd = register_forward(FM_SetClientKeyValue, "SetClientKeyValue")
		}
		if( !g_iMsg  )
		{
			g_iMsg = register_message(gmsgClCorpse, "Message_ClCorpse")
		}
	}
	else
	{
		for(new i=1; i<=g_iMaxPlayers; i++)
		{
			if( i != id && g_szModel[i][0] )
			{
				return
			}
		}

		if( g_iFwd )
		{
			unregister_forward(FM_SetClientKeyValue, g_iFwd)
			g_iFwd = 0
		}

	}
}