//========= Copyright © 1996-2001, Valve LLC, All rights reserved. ============
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================

#include "glquake.h"
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "materialsystem/imaterialproxy.h"
#include "materialproxyfactory.h"

IMaterialProxy *CMaterialProxyFactory::CreateProxy( const char *proxyName )
{
	// assumes that the client.dll is already LoadLibraried
	CreateInterfaceFn clientFactory = Sys_GetFactory( "client.dll" );
	if( !clientFactory )
	{
		return NULL;
	}
	// allocate exactly enough memory for the versioned name on the stack.
	char *proxyVersionedName;
	proxyVersionedName = 
		( char * )_alloca( strlen( proxyName ) + 
		strlen( IMATERIAL_PROXY_INTERFACE_VERSION ) + 1 );
	strcpy( proxyVersionedName, proxyName );
	strcat( proxyVersionedName, IMATERIAL_PROXY_INTERFACE_VERSION );
	IMaterialProxy *materialProxy;
	materialProxy = ( IMaterialProxy * )clientFactory( proxyVersionedName, NULL );
	if( !materialProxy )
	{
		Con_DPrintf( "Can't find material proxy \"%s\"\n", proxyName );
		return NULL;
	}
	return materialProxy;
}

void CMaterialProxyFactory::DeleteProxy( IMaterialProxy *pProxy )
{
	// how do you delete something generated by an interface.h factory?
	if( pProxy )
	{
		pProxy->Release();
	}
}
