Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: This file loads a KeyValues file containing material name mappings.
  4. // When the bsp is compiled, all materials listed in the file will
  5. // be replaced by the second material in the pair.
  6. //
  7. //=============================================================================
  8. #include "vbsp.h"
  9. #include "materialsub.h"
  10. #include "KeyValues.h"
  11. #include "tier1/strtools.h"
  12. bool g_ReplaceMaterials = false;
  13. static KeyValues *kv = 0;
  14. static KeyValues *allMapKeys = 0;
  15. static KeyValues *curMapKeys = 0;
  16. //-----------------------------------------------------------------------------
  17. // Purpose: Loads the KeyValues file for materials replacements
  18. //-----------------------------------------------------------------------------
  19. void LoadMaterialReplacementKeys( const char *gamedir, const char *mapname )
  20. {
  21. // Careful with static variables
  22. if( kv )
  23. {
  24. kv->deleteThis();
  25. kv = 0;
  26. }
  27. if( allMapKeys )
  28. allMapKeys = 0;
  29. if( curMapKeys )
  30. curMapKeys = 0;
  31. Msg( "Loading Replacement Keys\n" );
  32. // Attach the path to the keyValues file
  33. char path[1024];
  34. Q_snprintf( path, sizeof( path ), "%scfg\\materialsub.cfg", gamedir );
  35. // Load the keyvalues file
  36. kv = new KeyValues( "MaterialReplacements" );
  37. Msg( "File path: %s", path );
  38. if( !kv->LoadFromFile( g_pFileSystem, path ) )
  39. {
  40. Msg( "Failed to load KeyValues file!\n" );
  41. g_ReplaceMaterials = false;
  42. kv->deleteThis();
  43. kv = 0;
  44. return;
  45. }
  46. // Load global replace keys
  47. allMapKeys = kv->FindKey( "AllMaps", true );
  48. // Load keys for the current map
  49. curMapKeys = kv->FindKey( mapname );
  50. allMapKeys->ChainKeyValue( curMapKeys );
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Purpose: Deletes all keys
  54. //-----------------------------------------------------------------------------
  55. void DeleteMaterialReplacementKeys( void )
  56. {
  57. if( kv )
  58. {
  59. kv->deleteThis();
  60. kv = 0;
  61. }
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose: Replace the passed-in material name with a replacement name, if one exists
  65. //-----------------------------------------------------------------------------
  66. const char* ReplaceMaterialName( const char *name )
  67. {
  68. // Look for the material name in the global and map KeyValues
  69. // If it's not there, just return the original name
  70. // HACK: This stinks - KeyValues won't take a string with '/' in it.
  71. // If they did, this could be a simple pointer swap.
  72. char newName[1024];
  73. Q_strncpy( newName, name, sizeof( newName ) );
  74. Q_FixSlashes( newName );
  75. return allMapKeys->GetString( newName, name );
  76. }