engine/material.c
/*	Loads and parses material.
	Returns false on complete failure.
*/
Loads and parses material.
Returns false on complete failure.
Loads and parses material . Returns false on complete failure .
Material_t *Material_Load(const char *ccPath)
{
	Material_t  *mNewMaterial;
	void        *cData;
	char		cPath[PLATFORM_MAX_PATH],
				cMaterialName[64] = { 0 };
	if (ccPath[0] == ' ')
		Sys_Error("Invalid material path! (%s)\n", ccPath);
	if (!material_initialized)
	{
		Con_Warning("Attempted to load material, before initialization! (%s)\n", ccPath);
		return NULL;
	}
	sprintf(cPath, "%s%s.material", g_state.cMaterialPath, ccPath);
	mNewMaterial = Material_GetByPath(cPath);
	if(mNewMaterial)
		return mNewMaterial;
	cData = COM_LoadHeapFile(cPath);
	if(!cData)
	{
		Con_Warning("Failed to load material! (%s) (%s)\n", cPath, ccPath);
		return NULL;
	}
	Script_StartTokenParsing((char*)cData);
	if(!Script_GetToken(true))
	{
		Con_Warning("Failed to get initial token! (%s) (%i)\n",ccPath,iScriptLine);
		return NULL;
	}
	else if (cToken[0] != '{')
	{
		if (!strcmp(cToken, "material_version"))
		{
			Script_GetToken(false);
		}
		else	
		{
			strncpy(cMaterialName, cToken, sizeof(cMaterialName));
			if (cMaterialName[0] == ' ')
				Sys_Error("Invalid material name!\n");
			mNewMaterial = Material_GetByName(cMaterialName);
			if (mNewMaterial)
			{
				Con_Warning("Attempted to load duplicate material! (%s) (%s) vs (%s) (%s)\n",
					ccPath, cMaterialName,
					mNewMaterial->cPath, mNewMaterial->cName);
				free(cData);
				return mNewMaterial;
			}
		}
		Script_GetToken(true);
		if (cToken[0] != '{')
		{
			Con_Warning("Missing '{'! (%s) (%i)\n", ccPath, iScriptLine);
			goto MATERIAL_LOAD_ERROR;
		}
	}
	mNewMaterial = Material_Allocate();
	if (!mNewMaterial)
	{
		Con_Warning("Failed to allocate material! (%s)\n",ccPath);
		goto MATERIAL_LOAD_ERROR;
	}
	if (cMaterialName[0])
		strncpy(mNewMaterial->cName, cMaterialName, sizeof(mNewMaterial->cName));
	else
	{
		char cIn[PLATFORM_MAX_PATH];
		strncpy(cIn, ccPath, sizeof(cIn));
		ExtractFileBase(cIn, mNewMaterial->cName);
	}
	strncpy(mNewMaterial->cPath, ccPath, sizeof(mNewMaterial->cPath));
	for (;;)
	{
		if(!Script_GetToken(true))
		{
			Con_Warning("End of field without closing brace! (%s) (%i)\n",ccPath,iScriptLine);
			goto MATERIAL_LOAD_ERROR;
		}
		material_currentcontext = MATERIAL_FUNCTION_MATERIAL;
		if (cToken[0] == '}')
		{
			return mNewMaterial;
		}
		else if (cToken[0] == SCRIPT_SYMBOL_FUNCTION)
			Material_CheckFunctions(mNewMaterial);
	}
MATERIAL_LOAD_ERROR:
	free(cData);
	return NULL;
}
Material_t * Material_Load ( const char * ccPath ) { Material_t * mNewMaterial ; void * cData ; char cPath [ PLATFORM_MAX_PATH ] , cMaterialName [ 64 ] = { 0 } ; if ( ccPath [ 0 ] == ' ' ) Sys_Error ( " \n " , ccPath ) ; if ( ! material_initialized ) { Con_Warning ( " \n " , ccPath ) ; return NULL ; } sprintf ( cPath , " " , g_state . cMaterialPath , ccPath ) ; mNewMaterial = Material_GetByPath ( cPath ) ; if ( mNewMaterial ) return mNewMaterial ; cData = COM_LoadHeapFile ( cPath ) ; if ( ! cData ) { Con_Warning ( " \n " , cPath , ccPath ) ; return NULL ; } Script_StartTokenParsing ( ( char * ) cData ) ; if ( ! Script_GetToken ( true ) ) { Con_Warning ( " \n " , ccPath , iScriptLine ) ; return NULL ; } else if ( cToken [ 0 ] != ' ' ) { if ( ! strcmp ( cToken , " " ) ) { Script_GetToken ( false ) ; } else { strncpy ( cMaterialName , cToken , sizeof ( cMaterialName ) ) ; if ( cMaterialName [ 0 ] == ' ' ) Sys_Error ( " \n " ) ; mNewMaterial = Material_GetByName ( cMaterialName ) ; if ( mNewMaterial ) { Con_Warning ( " \n " , ccPath , cMaterialName , mNewMaterial -> cPath , mNewMaterial -> cName ) ; free ( cData ) ; return mNewMaterial ; } } Script_GetToken ( true ) ; if ( cToken [ 0 ] != ' ' ) { Con_Warning ( " \n " , ccPath , iScriptLine ) ; goto MATERIAL_LOAD_ERROR ; } } mNewMaterial = Material_Allocate ( ) ; if ( ! mNewMaterial ) { Con_Warning ( " \n " , ccPath ) ; goto MATERIAL_LOAD_ERROR ; } if ( cMaterialName [ 0 ] ) strncpy ( mNewMaterial -> cName , cMaterialName , sizeof ( mNewMaterial -> cName ) ) ; else { char cIn [ PLATFORM_MAX_PATH ] ; strncpy ( cIn , ccPath , sizeof ( cIn ) ) ; ExtractFileBase ( cIn , mNewMaterial -> cName ) ; } strncpy ( mNewMaterial -> cPath , ccPath , sizeof ( mNewMaterial -> cPath ) ) ; for ( ; ; ) { if ( ! Script_GetToken ( true ) ) { Con_Warning ( " \n " , ccPath , iScriptLine ) ; goto MATERIAL_LOAD_ERROR ; } material_currentcontext = MATERIAL_FUNCTION_MATERIAL ; if ( cToken [ 0 ] == ' ' ) { return mNewMaterial ; } else if ( cToken [ 0 ] == SCRIPT_SYMBOL_FUNCTION ) Material_CheckFunctions ( mNewMaterial ) ; } MATERIAL_LOAD_ERROR : free ( cData ) ; return NULL ; }
Loads and parses material.
Loads and parses material .
// Ensure that the given material names are correct! // Update the given path with the base path plus extension. // Check if it's been cached already... // Probably a name... // Copy over the given name. // Check if it's been cached already... // Assume that the material hasn't been cached yet, so allocate a new copy of one. // Otherwise just use the filename. // End // TODO: Load material data at the END! // Start