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.

402 lines
11 KiB

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