Counter Strike : Global Offensive Source Code
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.

102 lines
3.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Swap a compiled caption file.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "utlbuffer.h"
  8. #include "byteswap.h"
  9. #include "filesystem.h"
  10. #include "tier2/fileutils.h"
  11. #include "captioncompiler.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. BEGIN_BYTESWAP_DATADESC( CompiledCaptionHeader_t )
  15. DEFINE_FIELD( magic, FIELD_INTEGER ),
  16. DEFINE_FIELD( version, FIELD_INTEGER ),
  17. DEFINE_FIELD( numblocks, FIELD_INTEGER ),
  18. DEFINE_FIELD( blocksize, FIELD_INTEGER ),
  19. DEFINE_FIELD( directorysize, FIELD_INTEGER ),
  20. DEFINE_FIELD( dataoffset, FIELD_INTEGER ),
  21. END_BYTESWAP_DATADESC()
  22. BEGIN_BYTESWAP_DATADESC( CaptionLookup_t )
  23. DEFINE_FIELD( hash, FIELD_INTEGER ),
  24. DEFINE_FIELD( blockNum, FIELD_INTEGER ),
  25. DEFINE_FIELD( offset, FIELD_SHORT ),
  26. DEFINE_FIELD( length, FIELD_SHORT ),
  27. END_BYTESWAP_DATADESC()
  28. //-----------------------------------------------------------------------------
  29. // Swap a compiled closecaption file
  30. //-----------------------------------------------------------------------------
  31. bool SwapClosecaptionFile( void *pData )
  32. {
  33. CByteswap swap;
  34. swap.ActivateByteSwapping( true );
  35. CompiledCaptionHeader_t *pHdr = (CompiledCaptionHeader_t*)pData;
  36. if ( !IsPC() )
  37. {
  38. // pre-swap file header
  39. swap.SwapFieldsToTargetEndian( pHdr );
  40. }
  41. if ( pHdr->magic != COMPILED_CAPTION_FILEID || pHdr->version != COMPILED_CAPTION_VERSION )
  42. {
  43. // bad data
  44. return false;
  45. }
  46. // lookup headers
  47. pData = (byte*)pData + sizeof(CompiledCaptionHeader_t);
  48. swap.SwapFieldsToTargetEndian( (CaptionLookup_t*)pData, pHdr->directorysize );
  49. // unicode data
  50. pData = (byte*)pHdr + pHdr->dataoffset;
  51. swap.SwapBufferToTargetEndian( (wchar_t*)pData, (wchar_t*)pData, pHdr->numblocks * pHdr->blocksize / sizeof(wchar_t) );
  52. if ( IsPC() )
  53. {
  54. // post-swap file header
  55. swap.SwapFieldsToTargetEndian( pHdr );
  56. }
  57. return true;
  58. }
  59. #if defined( CLIENT_DLL ) || defined( GAME_DLL )
  60. //-----------------------------------------------------------------------------
  61. // Callback for UpdateOrCreate - generates .360 file
  62. //-----------------------------------------------------------------------------
  63. static bool CaptionCreateCallback( const char *pSourceName, const char *pTargetName, const char *pPathID, void *pExtraData )
  64. {
  65. // Generate the file
  66. CUtlBuffer buf;
  67. bool bOk = g_pFullFileSystem->ReadFile( pSourceName, pPathID, buf );
  68. if ( bOk )
  69. {
  70. bOk = SwapClosecaptionFile( buf.Base() );
  71. if ( bOk )
  72. {
  73. bOk = g_pFullFileSystem->WriteFile( pTargetName, pPathID, buf );
  74. }
  75. else
  76. {
  77. Warning( "Failed to create %s\n", pTargetName );
  78. }
  79. }
  80. return bOk;
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Calls utility function UpdateOrCreate
  84. //-----------------------------------------------------------------------------
  85. int UpdateOrCreateCaptionFile( const char *pSourceName, char *pTargetName, int maxLen, bool bForce )
  86. {
  87. return ::UpdateOrCreate( pSourceName, pTargetName, maxLen, "GAME", CaptionCreateCallback, bForce );
  88. }
  89. #endif