forked from perilouswithadollarsign/cstrike15_src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvconfig.cpp
152 lines (128 loc) · 4.61 KB
/
vconfig.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose: Utilities for setting vproject settings
//
//===========================================================================//
#ifdef _WIN32
#if !defined( _X360 )
#include <windows.h>
#endif
#include <direct.h>
#include <io.h> // _chmod
#include <process.h>
#endif
#if defined( _X360 )
#include "xbox/xbox_win32stubs.h"
#endif
#include "vconfig.h"
// NOTE: This has to be the last file included!
#include "tier0/memdbgon.h"
#ifdef _WIN32
//-----------------------------------------------------------------------------
// Purpose: Returns the string value of a registry key
// Input : *pName - name of the subKey to read
// *pReturn - string buffer to receive read string
// size - size of specified buffer
//-----------------------------------------------------------------------------
bool GetVConfigRegistrySetting( const char *pName, char *pReturn, int size )
{
// Open the key
HKEY hregkey;
// Changed to HKEY_CURRENT_USER from HKEY_LOCAL_MACHINE
if ( RegOpenKeyEx( HKEY_CURRENT_USER, VPROJECT_REG_KEY, 0, KEY_QUERY_VALUE, &hregkey ) != ERROR_SUCCESS )
return false;
// Get the value
DWORD dwSize = size;
if ( RegQueryValueEx( hregkey, pName, NULL, NULL,(LPBYTE) pReturn, &dwSize ) != ERROR_SUCCESS )
return false;
// Close the key
RegCloseKey( hregkey );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Sends a global system message to alert programs to a changed environment variable
//-----------------------------------------------------------------------------
void NotifyVConfigRegistrySettingChanged( void )
{
DWORD_PTR dwReturnValue = 0;
// Propagate changes so that environment variables takes immediate effect!
SendMessageTimeout( HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) "Environment", SMTO_ABORTIFHUNG, 5000, &dwReturnValue );
}
//-----------------------------------------------------------------------------
// Purpose: Set the registry entry to a string value, under the given subKey
// Input : *pName - name of the subKey to set
// *pValue - string value
//-----------------------------------------------------------------------------
void SetVConfigRegistrySetting( const char *pName, const char *pValue, bool bNotify )
{
HKEY hregkey;
// Changed to HKEY_CURRENT_USER from HKEY_LOCAL_MACHINE
// Open the key
if ( RegCreateKeyEx(
HKEY_CURRENT_USER, // base key
VPROJECT_REG_KEY, // subkey
0, // reserved
0, // lpClass
0, // options
(REGSAM)KEY_ALL_ACCESS, // access desired
NULL, // security attributes
&hregkey, // result
NULL // tells if it created the key or not (which we don't care)
) != ERROR_SUCCESS )
{
return;
}
// Set the value to the string passed in
int nType = strchr( pValue, '%' ) ? REG_EXPAND_SZ : REG_SZ;
RegSetValueEx( hregkey, pName, 0, nType, (const unsigned char *)pValue, (int) strlen(pValue) );
// Notify other programs
if ( bNotify )
{
NotifyVConfigRegistrySettingChanged();
}
// Close the key
RegCloseKey( hregkey );
}
//-----------------------------------------------------------------------------
// Purpose: Removes the obsolete user keyvalue
// Input : *pName - name of the subKey to set
// *pValue - string value
//-----------------------------------------------------------------------------
bool RemoveObsoleteVConfigRegistrySetting( const char *pValueName, char *pOldValue, int size )
{
// Open the key
HKEY hregkey;
if ( RegOpenKeyEx( HKEY_CURRENT_USER, "Environment", 0, (REGSAM)KEY_ALL_ACCESS, &hregkey ) != ERROR_SUCCESS )
return false;
// Return the old state if they've requested it
if ( pOldValue != NULL )
{
DWORD dwSize = size;
// Get the value
if ( RegQueryValueEx( hregkey, pValueName, NULL, NULL,(LPBYTE) pOldValue, &dwSize ) != ERROR_SUCCESS )
return false;
}
// Remove the value
if ( RegDeleteValue( hregkey, pValueName ) != ERROR_SUCCESS )
return false;
// Close the key
RegCloseKey( hregkey );
// Notify other programs
NotifyVConfigRegistrySettingChanged();
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Take a user-defined environment variable and swap it out for the internally used one
//-----------------------------------------------------------------------------
bool ConvertObsoleteVConfigRegistrySetting( const char *pValueName )
{
char szValue[MAX_PATH];
if ( RemoveObsoleteVConfigRegistrySetting( pValueName, szValue, sizeof( szValue ) ) )
{
// Set it up the correct way
SetVConfigRegistrySetting( pValueName, szValue );
return true;
}
return false;
}
#endif