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.

257 lines
5.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include "tier1/strtools.h"
  11. #include "ifaceposerworkspace.h"
  12. //-----------------------------------------------------------------------------
  13. // Purpose:
  14. //-----------------------------------------------------------------------------
  15. class CWorkspaceFiles : public IWorkspaceFiles
  16. {
  17. public:
  18. CWorkspaceFiles( void );
  19. ~CWorkspaceFiles( void );
  20. virtual void Init( char const *pchShortName );
  21. // Restore
  22. int GetNumStoredFiles( int type );
  23. const char *GetStoredFile( int type, int number );
  24. // Save
  25. void StartStoringFiles( int type );
  26. void FinishStoringFiles( int type );
  27. void StoreFile( int type, const char *filename );
  28. private:
  29. static const char *NameForType( int type );
  30. static int TypeForName( const char *name );
  31. LONG CreateWorkspaceKey( char const *pchGameName, PHKEY phKey );
  32. bool ReadInt( const char *szSubKey, int *value );
  33. bool WriteInt( const char *szSubKey, int value );
  34. bool ReadString( const char *szSubKey, char *value, int bufferlen );
  35. bool WriteString( const char *szSubKey, const char *value );
  36. HKEY m_hKeyMain;
  37. int m_nStoredFiles[ NUM_FILE_TYPES ];
  38. };
  39. static CWorkspaceFiles g_WorkspaceFiles;
  40. IWorkspaceFiles *workspacefiles = ( IWorkspaceFiles * )&g_WorkspaceFiles;
  41. CWorkspaceFiles::CWorkspaceFiles( void ) :
  42. m_hKeyMain( (HKEY)0 )
  43. {
  44. memset( m_nStoredFiles, 0, sizeof( m_nStoredFiles ) );
  45. }
  46. CWorkspaceFiles::~CWorkspaceFiles( void )
  47. {
  48. if ( (HKEY)0 != m_hKeyMain )
  49. {
  50. RegCloseKey( m_hKeyMain );
  51. }
  52. }
  53. void CWorkspaceFiles::Init( char const *pchShortName )
  54. {
  55. CreateWorkspaceKey( pchShortName, &m_hKeyMain );
  56. }
  57. const char *CWorkspaceFiles::NameForType( int type )
  58. {
  59. switch ( type )
  60. {
  61. case EXPRESSION:
  62. return "expressionfiles";
  63. case CHOREODATA:
  64. return "choreodatafiles";
  65. case MODELDATA:
  66. return "modelfiles";
  67. default:
  68. break;
  69. }
  70. return "unknown";
  71. }
  72. int CWorkspaceFiles::TypeForName( const char *name )
  73. {
  74. if ( !Q_stricmp( name, "expressionfiles" ) )
  75. {
  76. return EXPRESSION;
  77. }
  78. else if ( !Q_stricmp( name, "choreodatafiles" ) )
  79. {
  80. return CHOREODATA;
  81. }
  82. else if ( !Q_stricmp( name, "modelfiles" ) )
  83. {
  84. return MODELDATA;
  85. }
  86. return -1;
  87. }
  88. int CWorkspaceFiles::GetNumStoredFiles( int type )
  89. {
  90. char szKeyName[ 256 ];
  91. Q_snprintf( szKeyName, sizeof( szKeyName ), "%s\\total", NameForType( type ) );
  92. int num = 0;
  93. ReadInt( szKeyName, &num );
  94. return num;
  95. }
  96. const char *CWorkspaceFiles::GetStoredFile( int type, int number )
  97. {
  98. char szKeyName[ 256 ];
  99. sprintf( szKeyName, "%s\\%04i", NameForType( type ), number );
  100. static char filename[ 256 ];
  101. filename[ 0 ] = 0;
  102. ReadString( szKeyName, filename, 256 );
  103. return filename;
  104. }
  105. void CWorkspaceFiles::StartStoringFiles( int type )
  106. {
  107. m_nStoredFiles[ type ] = 0;
  108. }
  109. void CWorkspaceFiles::FinishStoringFiles( int type )
  110. {
  111. char szKeyName[ 256 ];
  112. sprintf( szKeyName, "%s\\total", NameForType( type ) );
  113. WriteInt( szKeyName, m_nStoredFiles[ type ] );
  114. }
  115. void CWorkspaceFiles::StoreFile( int type, const char *filename )
  116. {
  117. char szKeyName[ 256 ];
  118. sprintf( szKeyName, "%s\\%04i", NameForType( type ), m_nStoredFiles[ type ]++ );
  119. WriteString( szKeyName, filename );
  120. }
  121. LONG CWorkspaceFiles::CreateWorkspaceKey( char const *pchGameName, PHKEY phKey )
  122. {
  123. DWORD disp;
  124. char sz[ 512 ];
  125. Q_snprintf( sz, sizeof( sz ), "Software\\Valve\\faceposer\\workspace\\%s", pchGameName );
  126. return RegCreateKeyEx(
  127. HKEY_CURRENT_USER, // handle of open key
  128. sz, // address of name of subkey to open
  129. 0, // DWORD ulOptions, // reserved
  130. NULL, // Type of value
  131. REG_OPTION_NON_VOLATILE, // Store permanently in reg.
  132. KEY_ALL_ACCESS, // REGSAM samDesired, // security access mask
  133. NULL,
  134. phKey, // Key we are creating
  135. &disp ); // Type of creation
  136. }
  137. bool CWorkspaceFiles::ReadInt( const char *szSubKey, int *value )
  138. {
  139. LONG lResult; // Registry function result code
  140. DWORD dwType; // Type of key
  141. DWORD dwSize; // Size of element data
  142. dwSize = sizeof( DWORD );
  143. lResult = RegQueryValueEx(
  144. m_hKeyMain, // handle to key
  145. szSubKey, // value name
  146. 0, // reserved
  147. &dwType, // type buffer
  148. (LPBYTE)value, // data buffer
  149. &dwSize ); // size of data buffer
  150. if (lResult != ERROR_SUCCESS) // Failure
  151. return false;
  152. if (dwType != REG_DWORD)
  153. return false;
  154. return true;
  155. }
  156. bool CWorkspaceFiles::WriteInt( const char *szSubKey, int value )
  157. {
  158. LONG lResult; // Registry function result code
  159. DWORD dwSize; // Size of element data
  160. dwSize = sizeof( DWORD );
  161. lResult = RegSetValueEx(
  162. m_hKeyMain, // handle to key
  163. szSubKey, // value name
  164. 0, // reserved
  165. REG_DWORD, // type buffer
  166. (LPBYTE)&value, // data buffer
  167. dwSize ); // size of data buffer
  168. if (lResult != ERROR_SUCCESS) // Failure
  169. return false;
  170. return true;
  171. }
  172. bool CWorkspaceFiles::ReadString( const char *szSubKey, char *value, int buffersize )
  173. {
  174. LONG lResult; // Registry function result code
  175. DWORD dwType; // Type of key
  176. DWORD dwSize; // Size of element data
  177. dwSize = buffersize;
  178. lResult = RegQueryValueEx(
  179. m_hKeyMain, // handle to key
  180. szSubKey, // value name
  181. 0, // reserved
  182. &dwType, // type buffer
  183. (LPBYTE)value, // data buffer
  184. &dwSize ); // size of data buffer
  185. if (lResult != ERROR_SUCCESS) // Failure
  186. return false;
  187. if (dwType != REG_SZ)
  188. return false;
  189. return true;
  190. }
  191. bool CWorkspaceFiles::WriteString( const char *szSubKey, const char *value )
  192. {
  193. LONG lResult; // Registry function result code
  194. DWORD dwSize; // Size of element data
  195. dwSize = strlen( value ) + 1;
  196. lResult = RegSetValueEx(
  197. m_hKeyMain, // handle to key
  198. szSubKey, // value name
  199. 0, // reserved
  200. REG_SZ, // type buffer
  201. (LPBYTE)value, // data buffer
  202. dwSize ); // size of data buffer
  203. if (lResult != ERROR_SUCCESS) // Failure
  204. return false;
  205. return true;
  206. }