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.

138 lines
2.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "p4helpers.h"
  9. #include "tier2/tier2.h"
  10. #include "p4lib/ip4.h"
  11. #ifdef PLATFORM_WINDOWS_PC
  12. #include <Windows.h>
  13. #endif // PLATFORM_WINDOWS_PC
  14. //////////////////////////////////////////////////////////////////////////
  15. //
  16. // CP4File implementation
  17. //
  18. //////////////////////////////////////////////////////////////////////////
  19. CP4File::CP4File( char const *szFilename )
  20. {
  21. #ifdef PLATFORM_WINDOWS_PC
  22. // On windows, get the pathname of the file on disk first before using that as a perforce path
  23. // this avoids invalid Adds(). Have to go through GetShortPathName and then GetLongPathName from
  24. // the short path name
  25. TCHAR szShortPathName[ MAX_PATH ] = TEXT( "" );
  26. const DWORD shortRetVal = GetShortPathName( szFilename, szShortPathName, ARRAYSIZE( szShortPathName ) );
  27. if ( shortRetVal > 0 && shortRetVal <= ARRAYSIZE( szShortPathName ) )
  28. {
  29. TCHAR szLongPathName[ MAX_PATH ] = TEXT( "" );
  30. const DWORD longRetVal = GetLongPathName( szShortPathName, szLongPathName, ARRAYSIZE( szLongPathName ) );
  31. if ( longRetVal > 0 && longRetVal <= ARRAYSIZE( szLongPathName ) )
  32. {
  33. m_sFilename = szLongPathName;
  34. return;
  35. }
  36. }
  37. #endif // PLATFORM_WINDOWS_PC
  38. m_sFilename = szFilename;
  39. }
  40. CP4File::~CP4File()
  41. {
  42. }
  43. bool CP4File::Edit( void )
  44. {
  45. if ( !p4 )
  46. return true;
  47. return p4->OpenFileForEdit( m_sFilename.String() );
  48. }
  49. bool CP4File::Add( void )
  50. {
  51. if ( !p4 )
  52. return true;
  53. return p4->OpenFileForAdd( m_sFilename.String() );
  54. }
  55. bool CP4File::Revert( void )
  56. {
  57. if ( !p4 )
  58. return true;
  59. return p4->RevertFile( m_sFilename.String() );
  60. }
  61. // Is the file in perforce?
  62. bool CP4File::IsFileInPerforce()
  63. {
  64. if ( !p4 )
  65. return false;
  66. return p4->IsFileInPerforce( m_sFilename.String() );
  67. }
  68. bool CP4File::SetFileType(const CUtlString& desiredFileType)
  69. {
  70. if ( !p4 )
  71. return false;
  72. return p4->SetFileType( m_sFilename.String(), desiredFileType.String() );
  73. }
  74. //////////////////////////////////////////////////////////////////////////
  75. //
  76. // CP4Factory implementation
  77. //
  78. //////////////////////////////////////////////////////////////////////////
  79. CP4Factory::CP4Factory()
  80. {
  81. }
  82. CP4Factory::~CP4Factory()
  83. {
  84. }
  85. bool CP4Factory::SetDummyMode( bool bDummyMode )
  86. {
  87. bool bOld = m_bDummyMode;
  88. m_bDummyMode = bDummyMode;
  89. return bOld;
  90. }
  91. void CP4Factory::SetOpenFileChangeList( const char *szChangeListName )
  92. {
  93. if ( !m_bDummyMode && p4 )
  94. p4->SetOpenFileChangeList( szChangeListName );
  95. }
  96. CP4File *CP4Factory::AccessFile( char const *szFilename ) const
  97. {
  98. if ( !m_bDummyMode )
  99. return new CP4File( szFilename );
  100. else
  101. return new CP4File_Dummy( szFilename );
  102. }
  103. // Default p4 factory
  104. static CP4Factory s_static_p4_factory;
  105. CP4Factory *g_p4factory = &s_static_p4_factory; // NULL before the factory constructs