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.

407 lines
11 KiB

  1. //========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdio.h>
  8. // dgoodenough - memory.h doesn't exist on PS3
  9. // PS3_BUILDFIX
  10. #if !defined( _PS3 )
  11. #include <memory.h>
  12. #endif
  13. #if !defined( _GAMECONSOLE ) && !defined( _OSX ) && !defined (LINUX)
  14. #include <windows.h>
  15. #endif
  16. #include "contentcontroldialog.h"
  17. #include "checksum_md5.h"
  18. #include "engineinterface.h"
  19. #include <vgui/IInput.h>
  20. #include <vgui/ISystem.h>
  21. #include <vgui/ISurface.h>
  22. #include "tier1/keyvalues.h"
  23. #include "tier1/convar.h"
  24. #include <vgui_controls/Button.h>
  25. #include <vgui_controls/CheckButton.h>
  26. #include <vgui_controls/Label.h>
  27. #include <vgui_controls/RadioButton.h>
  28. #include <vgui_controls/TextEntry.h>
  29. // dgoodenough - select correct stub header based on console
  30. // PS3_BUILDFIX
  31. #if defined( _PS3 )
  32. #include "ps3/ps3_win32stubs.h"
  33. #endif
  34. #if defined( _X360 )
  35. #include "xbox/xbox_win32stubs.h"
  36. #endif
  37. // memdbgon must be the last include file in a .cpp file!!!
  38. #include <tier0/memdbgon.h>
  39. using namespace vgui;
  40. //-----------------------------------------------------------------------------
  41. // Purpose: Basic help dialog
  42. //-----------------------------------------------------------------------------
  43. CContentControlDialog::CContentControlDialog(vgui::Panel *parent) : vgui::Frame(parent, "ContentControlDialog")
  44. {
  45. SetBounds(0, 0, 372, 160);
  46. SetSizeable( false );
  47. SetTitle( "#GameUI_ContentLock", true );
  48. m_pStatus = new vgui::Label( this, "ContentStatus", "" );
  49. m_pPasswordLabel = new vgui::Label( this, "PasswordPrompt", "#GameUI_PasswordPrompt" );
  50. m_pPassword2Label = new vgui::Label( this, "PasswordReentryPrompt", "#GameUI_PasswordReentryPrompt" );
  51. m_pExplain = new vgui::Label( this, "ContentControlExplain", "" );
  52. m_pPassword = new vgui::TextEntry( this, "Password" );
  53. m_pPassword2 = new vgui::TextEntry( this, "Password2" );
  54. m_pOK = new vgui::Button( this, "Ok", "#GameUI_OK" );
  55. m_pOK->SetCommand( "Ok" );
  56. vgui::Button *cancel = new vgui::Button( this, "Cancel", "#GameUI_Cancel" );
  57. cancel->SetCommand( "Cancel" );
  58. m_szGorePW[ 0 ] = 0;
  59. ResetPassword();
  60. LoadControlSettings("Resource\\ContentControlDialog.res");
  61. // Explain("");
  62. // UpdateContentControlStatus();
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Purpose:
  66. //-----------------------------------------------------------------------------
  67. CContentControlDialog::~CContentControlDialog()
  68. {
  69. }
  70. void CContentControlDialog::Activate()
  71. {
  72. BaseClass::Activate();
  73. m_pPassword->SetText("");
  74. m_pPassword->RequestFocus();
  75. m_pPassword2->SetText("");
  76. Explain("");
  77. UpdateContentControlStatus();
  78. input()->SetAppModalSurface(GetVPanel());
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Purpose:
  82. //-----------------------------------------------------------------------------
  83. void CContentControlDialog::ResetPassword()
  84. {
  85. #if !defined( _OSX ) && !defined (LINUX)
  86. // Set initial value
  87. HKEY key;
  88. if ( ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Valve\\Half-Life\\Settings", 0, KEY_READ, &key))
  89. {
  90. DWORD type;
  91. DWORD bufSize = sizeof(m_szGorePW);
  92. RegQueryValueEx(key, "User Token 2", NULL, &type, (unsigned char *)m_szGorePW, &bufSize );
  93. RegCloseKey( key );
  94. }
  95. else
  96. {
  97. m_szGorePW[ 0 ] = 0;
  98. }
  99. #endif
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Purpose:
  103. //-----------------------------------------------------------------------------
  104. void CContentControlDialog::ApplyPassword()
  105. {
  106. WriteToken( m_szGorePW );
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Purpose:
  110. //-----------------------------------------------------------------------------
  111. void CContentControlDialog::Explain( PRINTF_FORMAT_STRING char const *fmt, ... )
  112. {
  113. if ( !m_pExplain )
  114. return;
  115. va_list argptr;
  116. char text[1024];
  117. va_start (argptr,fmt);
  118. Q_vsnprintf (text, sizeof(text), fmt, argptr);
  119. va_end (argptr);
  120. m_pExplain->SetText( text );
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose:
  124. // Input : *command -
  125. //-----------------------------------------------------------------------------
  126. void CContentControlDialog::OnCommand( const char *command )
  127. {
  128. if ( !stricmp( command, "Ok" ) )
  129. {
  130. bool canclose = false;
  131. char pw1[ 256 ];
  132. char pw2[ 256 ];
  133. m_pPassword->GetText( pw1, 256 );
  134. m_pPassword2->GetText( pw2, 256 );
  135. // Get text and check
  136. // bool enabled = PasswordEnabled(); //( m_szGorePW[0]!=0 ) ? true : false;
  137. // bool pwMatch = stricmp( pw1, pw2 ) == 0 ? true : false;
  138. if (IsPasswordEnabledInDialog())
  139. {
  140. canclose = DisablePassword(pw1);
  141. // canclose = CheckPassword( m_szGorePW, pw1, false );
  142. }
  143. else if (!strcmp(pw1, pw2))
  144. {
  145. canclose = EnablePassword(pw1);
  146. // canclose = CheckPassword( NULL, pw1, true );
  147. }
  148. else
  149. {
  150. Explain( "#GameUI_PasswordsDontMatch" );
  151. }
  152. if ( canclose )
  153. {
  154. OnClose();
  155. }
  156. }
  157. else if ( !stricmp( command, "Cancel" ) )
  158. {
  159. OnClose();
  160. }
  161. else
  162. {
  163. BaseClass::OnCommand( command );
  164. }
  165. }
  166. //-----------------------------------------------------------------------------
  167. // Purpose:
  168. //-----------------------------------------------------------------------------
  169. void CContentControlDialog::OnClose()
  170. {
  171. BaseClass::OnClose();
  172. PostActionSignal(new KeyValues("ContentControlClose"));
  173. // MarkForDeletion();
  174. }
  175. //-----------------------------------------------------------------------------
  176. // Purpose:
  177. //-----------------------------------------------------------------------------
  178. void CContentControlDialog::WriteToken( const char *str )
  179. {
  180. #if !defined( _OSX ) && !defined (LINUX)
  181. // Set initial value
  182. HKEY key;
  183. if ( ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Valve\\Half-Life\\Settings", 0, KEY_WRITE, &key))
  184. {
  185. DWORD type = REG_SZ;
  186. DWORD bufSize = strlen( str ) + 1;
  187. RegSetValueEx(key, "User Token 2", 0, type, (const unsigned char *)str, bufSize );
  188. RegCloseKey( key );
  189. }
  190. Q_strncpy( m_szGorePW, str, sizeof( m_szGorePW ) );
  191. UpdateContentControlStatus();
  192. #endif
  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. }