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.

406 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdio.h>
  8. #include <memory.h>
  9. #if defined( WIN32 ) && !defined( _X360 )
  10. #include <windows.h>
  11. #endif
  12. #include "ContentControlDialog.h"
  13. #include "checksum_md5.h"
  14. #include "EngineInterface.h"
  15. #include <vgui/IInput.h>
  16. #include <vgui/ISystem.h>
  17. #include <vgui/ISurface.h>
  18. #include "tier1/KeyValues.h"
  19. #include "tier1/convar.h"
  20. #include <vgui_controls/Button.h>
  21. #include <vgui_controls/CheckButton.h>
  22. #include <vgui_controls/Label.h>
  23. #include <vgui_controls/RadioButton.h>
  24. #include <vgui_controls/TextEntry.h>
  25. #include <tier0/vcrmode.h>
  26. #if defined( _X360 )
  27. #include "xbox/xbox_win32stubs.h"
  28. #endif
  29. // memdbgon must be the last include file in a .cpp file!!!
  30. #include <tier0/memdbgon.h>
  31. using namespace vgui;
  32. //-----------------------------------------------------------------------------
  33. // Purpose: Basic help dialog
  34. //-----------------------------------------------------------------------------
  35. CContentControlDialog::CContentControlDialog(vgui::Panel *parent) : vgui::Frame(parent, "ContentControlDialog")
  36. {
  37. SetBounds(0, 0, 372, 160);
  38. SetSizeable( false );
  39. SetTitle( "#GameUI_ContentLock", true );
  40. m_pStatus = new vgui::Label( this, "ContentStatus", "" );
  41. m_pPasswordLabel = new vgui::Label( this, "PasswordPrompt", "#GameUI_PasswordPrompt" );
  42. m_pPassword2Label = new vgui::Label( this, "PasswordReentryPrompt", "#GameUI_PasswordReentryPrompt" );
  43. m_pExplain = new vgui::Label( this, "ContentControlExplain", "" );
  44. m_pPassword = new vgui::TextEntry( this, "Password" );
  45. m_pPassword2 = new vgui::TextEntry( this, "Password2" );
  46. m_pOK = new vgui::Button( this, "Ok", "#GameUI_OK" );
  47. m_pOK->SetCommand( "Ok" );
  48. vgui::Button *cancel = new vgui::Button( this, "Cancel", "#GameUI_Cancel" );
  49. cancel->SetCommand( "Cancel" );
  50. m_szGorePW[ 0 ] = 0;
  51. ResetPassword();
  52. LoadControlSettings("Resource\\ContentControlDialog.res");
  53. // Explain("");
  54. // UpdateContentControlStatus();
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose:
  58. //-----------------------------------------------------------------------------
  59. CContentControlDialog::~CContentControlDialog()
  60. {
  61. }
  62. void CContentControlDialog::Activate()
  63. {
  64. BaseClass::Activate();
  65. m_pPassword->SetText("");
  66. m_pPassword->RequestFocus();
  67. m_pPassword2->SetText("");
  68. Explain("");
  69. UpdateContentControlStatus();
  70. input()->SetAppModalSurface(GetVPanel());
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose:
  74. //-----------------------------------------------------------------------------
  75. void CContentControlDialog::ResetPassword()
  76. {
  77. // Set initial value
  78. #ifdef WIN32
  79. #ifndef _XBOX
  80. HKEY key;
  81. if ( ERROR_SUCCESS == VCRHook_RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Valve\\Half-Life\\Settings", 0, KEY_READ, &key))
  82. {
  83. DWORD type;
  84. DWORD bufSize = sizeof(m_szGorePW);
  85. VCRHook_RegQueryValueEx(key, "User Token 2", NULL, &type, (unsigned char *)m_szGorePW, &bufSize );
  86. VCRHook_RegCloseKey( key );
  87. }
  88. else
  89. #endif
  90. {
  91. m_szGorePW[ 0 ] = 0;
  92. }
  93. #else
  94. vgui::system()->SetRegistryString( "Software\\Valve\\Half-Life\\Settings\\User Token 2", m_szGorePW );
  95. #endif
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Purpose:
  99. //-----------------------------------------------------------------------------
  100. void CContentControlDialog::ApplyPassword()
  101. {
  102. WriteToken( m_szGorePW );
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Purpose:
  106. //-----------------------------------------------------------------------------
  107. void CContentControlDialog::Explain( char const *fmt, ... )
  108. {
  109. if ( !m_pExplain )
  110. return;
  111. va_list argptr;
  112. char text[1024];
  113. va_start (argptr,fmt);
  114. Q_vsnprintf (text, sizeof(text), fmt, argptr);
  115. va_end (argptr);
  116. m_pExplain->SetText( text );
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose:
  120. // Input : *command -
  121. //-----------------------------------------------------------------------------
  122. void CContentControlDialog::OnCommand( const char *command )
  123. {
  124. if ( !stricmp( command, "Ok" ) )
  125. {
  126. bool canclose = false;
  127. char pw1[ 256 ];
  128. char pw2[ 256 ];
  129. m_pPassword->GetText( pw1, 256 );
  130. m_pPassword2->GetText( pw2, 256 );
  131. // Get text and check
  132. // bool enabled = PasswordEnabled(); //( m_szGorePW[0]!=0 ) ? true : false;
  133. // bool pwMatch = stricmp( pw1, pw2 ) == 0 ? true : false;
  134. if (IsPasswordEnabledInDialog())
  135. {
  136. canclose = DisablePassword(pw1);
  137. // canclose = CheckPassword( m_szGorePW, pw1, false );
  138. }
  139. else if (!strcmp(pw1, pw2))
  140. {
  141. canclose = EnablePassword(pw1);
  142. // canclose = CheckPassword( NULL, pw1, true );
  143. }
  144. else
  145. {
  146. Explain( "#GameUI_PasswordsDontMatch" );
  147. }
  148. if ( canclose )
  149. {
  150. OnClose();
  151. }
  152. }
  153. else if ( !stricmp( command, "Cancel" ) )
  154. {
  155. OnClose();
  156. }
  157. else
  158. {
  159. BaseClass::OnCommand( command );
  160. }
  161. }
  162. //-----------------------------------------------------------------------------
  163. // Purpose:
  164. //-----------------------------------------------------------------------------
  165. void CContentControlDialog::OnClose()
  166. {
  167. BaseClass::OnClose();
  168. PostActionSignal(new KeyValues("ContentControlClose"));
  169. // MarkForDeletion();
  170. }
  171. //-----------------------------------------------------------------------------
  172. // Purpose:
  173. //-----------------------------------------------------------------------------
  174. void CContentControlDialog::WriteToken( const char *str )
  175. {
  176. // Set initial value
  177. #ifdef WIN32
  178. #ifndef _XBOX
  179. HKEY key;
  180. if ( ERROR_SUCCESS == VCRHook_RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Valve\\Half-Life\\Settings", 0, KEY_WRITE, &key))
  181. {
  182. DWORD type = REG_SZ;
  183. DWORD bufSize = strlen( str ) + 1;
  184. VCRHook_RegSetValueEx(key, "User Token 2", 0, type, (const unsigned char *)str, bufSize );
  185. VCRHook_RegCloseKey( key );
  186. }
  187. #endif
  188. #else
  189. vgui::system()->SetRegistryString( "Software\\Valve\\Half-Life\\Settings\\User Token 2", m_szGorePW );
  190. #endif
  191. Q_strncpy( m_szGorePW, str, sizeof( m_szGorePW ) );
  192. UpdateContentControlStatus();
  193. }
  194. //-----------------------------------------------------------------------------
  195. // Purpose:
  196. //-----------------------------------------------------------------------------
  197. void CContentControlDialog::HashPassword(const char *newPW, char *hashBuffer, int maxlen )
  198. {
  199. // Compute the md5 hash and save it.
  200. unsigned char md5_hash[16];
  201. MD5Context_t ctx;
  202. MD5Init( &ctx );
  203. MD5Update( &ctx, (unsigned char const *)newPW, strlen( newPW ) );
  204. MD5Final( md5_hash, &ctx );
  205. char hex[ 128 ];
  206. Q_binarytohex( md5_hash, sizeof( md5_hash ), hex, sizeof( hex ) );
  207. // char digestedPW[ 128 ];
  208. Q_strncpy( hashBuffer, hex, maxlen );
  209. }
  210. //-----------------------------------------------------------------------------
  211. // Purpose:
  212. //-----------------------------------------------------------------------------
  213. /*
  214. bool CContentControlDialog::CheckPassword( char const *oldPW, char const *newPW, bool enableContentControl )
  215. {
  216. char digestedPW[ 128 ];
  217. HashPassword(newPW, digestedPW, sizeof( digestedPW ) );
  218. // Compute the md5 hash and save it.
  219. unsigned char md5_hash[16];
  220. MD5Context_t ctx;
  221. MD5Init( &ctx );
  222. MD5Update( &ctx, (unsigned char const *)(LPCSTR)newPW, strlen( newPW ) );
  223. MD5Final( md5_hash, &ctx );
  224. char hex[ 128 ];
  225. Q_binarytohex( md5_hash, sizeof( md5_hash ), hex, sizeof( hex ) );
  226. Q_strncpy( digestedPW, hex, sizeof( digestedPW ) );
  227. */
  228. //-----------------------------------------------------------------------------
  229. // Purpose:
  230. //-----------------------------------------------------------------------------
  231. bool CContentControlDialog::EnablePassword(const char *newPW)
  232. {
  233. if ( !newPW[ 0 ] )
  234. {
  235. Explain( "#GameUI_MustEnterPassword" );
  236. return false;
  237. }
  238. char digestedPW[ 128 ];
  239. HashPassword(newPW, digestedPW, sizeof( digestedPW ) );
  240. // disable violence
  241. /* engine->Cvar_SetValue("violence_hblood", 0.0 );
  242. engine->Cvar_SetValue("violence_hgibs" , 0.0 );
  243. engine->Cvar_SetValue("violence_ablood", 0.0 );
  244. engine->Cvar_SetValue("violence_agibs" , 0.0 );
  245. */
  246. ConVarRef violence_hblood( "violence_hblood" );
  247. violence_hblood.SetValue(false);
  248. ConVarRef violence_hgibs( "violence_hgibs" );
  249. violence_hgibs.SetValue(false);
  250. ConVarRef violence_ablood( "violence_ablood" );
  251. violence_ablood.SetValue(false);
  252. ConVarRef violence_agibs( "violence_agibs" );
  253. violence_agibs.SetValue(false);
  254. // Store digest to registry
  255. // WriteToken( digestedPW );
  256. Q_strncpy(m_szGorePW, digestedPW, sizeof( m_szGorePW ) );
  257. /*
  258. }
  259. else
  260. {
  261. if ( stricmp( oldPW, digestedPW ) )
  262. {
  263. // Warn that password is invalid
  264. Explain( "#GameUI_IncorrectPassword" );
  265. return false;
  266. }
  267. }
  268. }*/
  269. return true;
  270. }
  271. //-----------------------------------------------------------------------------
  272. // Purpose:
  273. //-----------------------------------------------------------------------------
  274. bool CContentControlDialog::DisablePassword(const char *oldPW)
  275. {
  276. if ( !oldPW[ 0 ] )
  277. {
  278. Explain( "#GameUI_MustEnterPassword" );
  279. return false;
  280. }
  281. char digestedPW[ 128 ];
  282. HashPassword(oldPW, digestedPW, sizeof( digestedPW ) );
  283. if( stricmp( m_szGorePW, digestedPW ) )
  284. {
  285. Explain( "#GameUI_IncorrectPassword" );
  286. return false;
  287. }
  288. m_szGorePW[0] = 0;
  289. // set the violence cvars
  290. /* engine->Cvar_SetValue("violence_hblood", 1.0 );
  291. engine->Cvar_SetValue("violence_hgibs" , 1.0 );
  292. engine->Cvar_SetValue("violence_ablood", 1.0 );
  293. engine->Cvar_SetValue("violence_agibs" , 1.0 );
  294. */
  295. ConVarRef violence_hblood( "violence_hblood" );
  296. violence_hblood.SetValue(true);
  297. ConVarRef violence_hgibs( "violence_hgibs" );
  298. violence_hgibs.SetValue(true);
  299. ConVarRef violence_ablood( "violence_ablood" );
  300. violence_ablood.SetValue(true);
  301. ConVarRef violence_agibs( "violence_agibs" );
  302. violence_agibs.SetValue(true);
  303. // // Remove digest value
  304. // WriteToken( "" );
  305. return true;
  306. }
  307. //-----------------------------------------------------------------------------
  308. // Purpose:
  309. //-----------------------------------------------------------------------------
  310. bool CContentControlDialog::IsPasswordEnabledInDialog()
  311. {
  312. return m_szGorePW[0] != 0;
  313. }
  314. //-----------------------------------------------------------------------------
  315. // Purpose:
  316. //-----------------------------------------------------------------------------
  317. void CContentControlDialog::UpdateContentControlStatus( void )
  318. {
  319. bool enabled = IsPasswordEnabledInDialog(); //( m_szGorePW[0]!=0 ) ? true : false;
  320. m_pStatus->SetText( enabled ? "#GameUI_ContentStatusEnabled" : "#GameUI_ContentStatusDisabled" );
  321. if (enabled)
  322. {
  323. m_pPasswordLabel->SetText("#GameUI_PasswordDisablePrompt");
  324. }
  325. else
  326. {
  327. m_pPasswordLabel->SetText("#GameUI_PasswordPrompt");
  328. }
  329. // hide the re-entry
  330. m_pPassword2Label->SetVisible(!enabled);
  331. m_pPassword2->SetVisible(!enabled);
  332. // m_pOK->SetText( enabled ? "#GameUI_Disable" : "#GameUI_Enable" );
  333. }