summaryrefslogtreecommitdiff
path: root/engine/materialproxyfactory.cpp
blob: e4936195a72e795dc728e4bba4e340fc755f9bb1 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "materialsystem/imaterialproxy.h"
#include "materialproxyfactory.h"
#include "toolframework/itoolframework.h"
#include "toolframework/itoolsystem.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
extern CSysModule		*g_ClientDLLModule;

IMaterialProxy *CMaterialProxyFactory::CreateProxy( const char *proxyName )
{
#if !defined(SWDS)
	IMaterialProxy *materialProxy = LookupProxy( proxyName, Sys_GetFactory( g_ClientDLLModule ) );

	// If the client didn't have it and we're in tool mode, ask the tools...
	if ( toolframework->InToolMode() && !materialProxy )
	{
		materialProxy = toolframework->LookupProxy( proxyName );
	}

	if( !materialProxy )
	{
		ConDMsg( "Can't find material proxy \"%s\"\n", proxyName );
		return NULL;
	}
	return materialProxy;
#else
	return NULL;
#endif
}

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


//-----------------------------------------------------------------------------
// Look up proxy
//-----------------------------------------------------------------------------
IMaterialProxy *CMaterialProxyFactory::LookupProxy( const char *proxyName, CreateInterfaceFn factory )
{
	if( !factory )
		return NULL;

	// allocate exactly enough memory for the versioned name on the stack.
	char *proxyVersionedName;
	int buflen = Q_strlen( proxyName ) + Q_strlen( IMATERIAL_PROXY_INTERFACE_VERSION ) + 1;

	proxyVersionedName = ( char * )_alloca( buflen );
	Q_strncpy( proxyVersionedName, proxyName, buflen );
	Q_strncat( proxyVersionedName, IMATERIAL_PROXY_INTERFACE_VERSION, buflen, COPY_ALL_CHARACTERS );
	return ( IMaterialProxy * )factory( proxyVersionedName, NULL );
}