-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathuser_preferences.cpp
More file actions
320 lines (274 loc) · 11.1 KB
/
user_preferences.cpp
File metadata and controls
320 lines (274 loc) · 11.1 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* =============================================================================
* CS2Fixes
* Copyright (C) 2023-2026 Source2ZE
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "user_preferences.h"
#include "commands.h"
#include "common.h"
#include "entwatch.h"
#include "httpmanager.h"
#include "playermanager.h"
#include "strtools.h"
#include <string>
#undef snprintf
#include "vendor/nlohmann/json.hpp"
using json = nlohmann::json;
CUserPreferencesStorage* g_pUserPreferencesStorage = nullptr;
CUserPreferencesSystem* g_pUserPreferencesSystem = nullptr;
CConVar<CUtlString> g_cvarUserPrefsAPI("cs2f_user_prefs_api", FCVAR_PROTECTED, "API for user preferences, currently a REST API", "");
CON_COMMAND_CHAT_FLAGS(pullprefs, "- Pull preferences.", ADMFLAG_ROOT)
{
ZEPlayer* pPlayer = player->GetZEPlayer();
if (!pPlayer) return;
g_pUserPreferencesSystem->PullPreferences(pPlayer->GetPlayerSlot().Get());
}
CON_COMMAND_CHAT_FLAGS(setpref, "- Set a preference key for the player.", ADMFLAG_ROOT)
{
if (args.ArgC() < 3)
Message("Usage: %s <pref> <value>\n", args[0]);
else
{
ZEPlayer* pPlayer = player->GetZEPlayer();
if (!pPlayer) return;
g_pUserPreferencesSystem->SetPreference(pPlayer->GetPlayerSlot().Get(), args[1], args[2]);
}
}
CON_COMMAND_CHAT_FLAGS(getpref, "- Get a preference key for the player.", ADMFLAG_ROOT)
{
if (args.ArgC() < 2)
Message("Usage: %s <pref>\n", args[0]);
else
{
ZEPlayer* pPlayer = player->GetZEPlayer();
if (!pPlayer) return;
const char* sValue = g_pUserPreferencesSystem->GetPreference(pPlayer->GetPlayerSlot().Get(), args[1]);
Message("The setting is set to: %s\n", sValue);
}
}
CON_COMMAND_CHAT_FLAGS(pushprefs, "- Push preferences.", ADMFLAG_ROOT)
{
ZEPlayer* pPlayer = player->GetZEPlayer();
if (!pPlayer) return;
g_pUserPreferencesSystem->PushPreferences(pPlayer->GetPlayerSlot().Get());
}
void CUserPreferencesSystem::ClearPreferences(int iSlot)
{
m_mUserSteamIds[iSlot] = 0;
m_mPreferencesLoaded[iSlot] = false;
m_mPreferencesMaps[iSlot].clear();
}
bool CUserPreferencesSystem::PutPreferences(int iSlot, uint64 iSteamId, UserPrefsMap_t& preferenceData)
{
ZEPlayer* player = g_playerManager->GetPlayer(CPlayerSlot(iSlot));
if (!player || !player->IsAuthenticated()) return false;
if (iSteamId != player->GetSteamId64())
return false;
#ifdef _DEBUG
Message("Putting data for %llu\n", iSteamId);
#endif
m_mUserSteamIds[iSlot] = iSteamId;
m_mPreferencesLoaded[iSlot] = true;
for (auto prefPair : preferenceData)
m_mPreferencesMaps[iSlot][prefPair.first] = prefPair.second;
return true;
}
void CUserPreferencesSystem::OnPutPreferences(int iSlot)
{
int iHideDistance = GetPreferenceInt(iSlot, HIDE_DISTANCE_PREF_KEY_NAME, 0);
int iHideMode = GetPreferenceInt(iSlot, HIDE_MODE_PREF_KEY_NAME, HIDE_MODE_NORMAL);
int iSoundStatus = GetPreferenceInt(iSlot, SOUND_STATUS_PREF_KEY_NAME, 1);
bool bStopSound = (bool)(iSoundStatus & 1);
bool bSilenceSound = (bool)(iSoundStatus & 2);
bool bHideDecals = (bool)GetPreferenceInt(iSlot, DECAL_PREF_KEY_NAME, 1);
bool bNoShake = (bool)GetPreferenceInt(iSlot, NO_SHAKE_PREF_KEY_NAME, 0);
int iButtonWatchMode = GetPreferenceInt(iSlot, BUTTON_WATCH_PREF_KEY_NAME, 0);
bool bZSounds = (bool)GetPreferenceInt(iSlot, ZSOUNDS_PREF_KEY_NAME, 1);
// EntWatch
int iEntwatchMode = GetPreferenceInt(iSlot, EW_PREF_HUD_MODE, 0);
float flEntwatchHudposX = GetPreferenceFloat(iSlot, EW_PREF_HUDPOS_X, EW_HUDPOS_X_DEFAULT);
float flEntwatchHudposY = GetPreferenceFloat(iSlot, EW_PREF_HUDPOS_Y, EW_HUDPOS_Y_DEFAULT);
Color ewHudColor;
V_StringToColor(g_pUserPreferencesSystem->GetPreference(iSlot, EW_PREF_HUDCOLOR, "255 255 255 255"), ewHudColor);
float flEntwatchHudSize = GetPreferenceFloat(iSlot, EW_PREF_HUDSIZE, EW_HUDSIZE_DEFAULT);
// Set the values that we just loaded --- the player is guaranteed available
g_playerManager->SetPlayerStopSound(iSlot, bStopSound);
g_playerManager->SetPlayerSilenceSound(iSlot, bSilenceSound);
g_playerManager->SetPlayerZSounds(iSlot, bZSounds);
g_playerManager->SetPlayerStopDecals(iSlot, bHideDecals);
g_playerManager->SetPlayerNoShake(iSlot, bNoShake);
ZEPlayer* player = g_playerManager->GetPlayer(CPlayerSlot(iSlot));
player->SetHideDistance(iHideDistance);
player->SetHideMode(iHideMode);
for (int i = 0; i < iButtonWatchMode; i++)
player->CycleButtonWatch();
// Set EntWatch
player->SetEntwatchHudMode(iEntwatchMode);
player->SetEntwatchHudPos(flEntwatchHudposX, flEntwatchHudposY);
player->SetEntwatchHudColor(ewHudColor);
player->SetEntwatchHudSize(flEntwatchHudSize);
}
void CUserPreferencesSystem::PullPreferences(int iSlot)
{
if (!g_pUserPreferencesStorage) return;
// Ignore non-authenticated players and get the authenticated SteamID
ZEPlayer* player = g_playerManager->GetPlayer(CPlayerSlot(iSlot));
if (!player || !player->IsAuthenticated()) return;
uint64 iSteamId = player->GetSteamId64();
g_pUserPreferencesStorage->LoadPreferences(
iSteamId,
[iSlot](uint64 iSteamId, UserPrefsMap_t& preferenceData) {
if (g_pUserPreferencesSystem->PutPreferences(iSlot, iSteamId, preferenceData))
g_pUserPreferencesSystem->OnPutPreferences(iSlot);
});
}
const char* CUserPreferencesSystem::GetPreference(int iSlot, const char* sKey, const char* sDefaultValue)
{
uint32 iKeyHash = hash_32_fnv1a_const(sKey);
#ifdef _DEBUG
Message("User at slot %d is reading from preference '%s' with hash %d.\n", iSlot, sKey, iKeyHash);
#endif
if (!m_mPreferencesMaps[iSlot].contains(iKeyHash)) return sDefaultValue;
return m_mPreferencesMaps[iSlot][iKeyHash]->GetValue();
}
int CUserPreferencesSystem::GetPreferenceInt(int iSlot, const char* sKey, int iDefaultValue)
{
const char* pszPreferenceValue = GetPreference(iSlot, sKey, "");
if (*pszPreferenceValue == '\0')
return iDefaultValue;
return V_StringToInt32(pszPreferenceValue, iDefaultValue);
}
float CUserPreferencesSystem::GetPreferenceFloat(int iSlot, const char* sKey, float fDefaultValue)
{
const char* pszPreferenceValue = GetPreference(iSlot, sKey, "");
if (*pszPreferenceValue == '\0')
return fDefaultValue;
return V_StringToFloat32(pszPreferenceValue, fDefaultValue);
}
void CUserPreferencesSystem::SetPreference(int iSlot, const char* sKey, const char* sValue)
{
uint32 iKeyHash = hash_32_fnv1a_const(sKey);
#ifdef _DEBUG
Message("User at slot %d is storing in preference '%s' with hash %d value '%s'.\n", iSlot, sKey, iKeyHash, sValue);
#endif
std::shared_ptr<CPreferenceValue> prefValue;
// Create or populate the content of the preference value
if (!m_mPreferencesMaps[iSlot].contains(iKeyHash))
{
prefValue = std::make_shared<CPreferenceValue>(sKey, sValue);
}
else
{
prefValue = m_mPreferencesMaps[iSlot][iKeyHash];
prefValue->SetKeyValue(sKey, sValue);
}
// Override the key-value pair and insert
m_mPreferencesMaps[iSlot][iKeyHash] = prefValue;
}
void CUserPreferencesSystem::SetPreferenceInt(int iSlot, const char* sKey, int iValue)
{
char sPreferenceString[MAX_PREFERENCE_LENGTH];
V_snprintf(sPreferenceString, sizeof(sPreferenceString), "%d", iValue);
SetPreference(iSlot, sKey, (const char*)sPreferenceString);
}
void CUserPreferencesSystem::SetPreferenceFloat(int iSlot, const char* sKey, float fValue)
{
char sPreferenceString[MAX_PREFERENCE_LENGTH];
V_snprintf(sPreferenceString, sizeof(sPreferenceString), "%f", fValue);
SetPreference(iSlot, sKey, (const char*)sPreferenceString);
}
bool CUserPreferencesSystem::CheckPreferencesLoaded(int iSlot)
{
ZEPlayer* player = g_playerManager->GetPlayer(CPlayerSlot(iSlot));
if (!player || !player->IsAuthenticated()) return false;
return m_mPreferencesLoaded[iSlot];
}
void CUserPreferencesSystem::PushPreferences(int iSlot)
{
if (!g_pUserPreferencesStorage) return;
// Fetch the slot and only 'push' if the player has already loaded
if (!m_mPreferencesLoaded[iSlot]) return;
uint64 iSteamId = m_mUserSteamIds[iSlot];
g_pUserPreferencesStorage->StorePreferences(
iSteamId,
m_mPreferencesMaps[iSlot],
[iSlot](uint64 iSteamId, UserPrefsMap_t& preferenceData) {
if (g_pUserPreferencesSystem->PutPreferences(iSlot, iSteamId, preferenceData))
g_pUserPreferencesSystem->OnPutPreferences(iSlot);
});
}
void CUserPreferencesREST::JsonToPreferencesMap(json data, UserPrefsMap_t& preferencesMap)
{
for (auto it = data.begin(); it != data.end(); ++it)
{
std::string key = it.key();
std::string value = it.value();
// Prepare the key and value pair, and also the key name via hashes
#ifdef _DEBUG
Message("- Storing KV-pair: %s, %s\n", key.c_str(), value.c_str());
#endif
std::shared_ptr<CPreferenceValue> prefValue = std::make_shared<CPreferenceValue>(key.c_str(), value.c_str());
// Store key, value, and key names
uint32 iKeyHash = hash_32_fnv1a_const(prefValue->GetKey());
preferencesMap[iKeyHash] = prefValue;
}
}
void CUserPreferencesREST::LoadPreferences(uint64 iSteamId, StorageCallback_t cb)
{
#ifdef _DEBUG
Message("Loading data for %llu\n", iSteamId);
#endif
if (g_cvarUserPrefsAPI.Get().Length() == 0)
return;
// Submit the request to pull the user data
char sUserPreferencesUrl[256];
V_snprintf(sUserPreferencesUrl, sizeof(sUserPreferencesUrl), "%s%llu", g_cvarUserPrefsAPI.Get().String(), iSteamId);
g_HTTPManager.Get(sUserPreferencesUrl, [iSteamId, cb](HTTPRequestHandle request, json data) {
#ifdef _DEBUG
Message("Executing storage callback during load for %llu\n", iSteamId);
#endif
UserPrefsMap_t preferencesMap;
((CUserPreferencesREST*)g_pUserPreferencesStorage)->JsonToPreferencesMap(data, preferencesMap);
cb(iSteamId, preferencesMap);
preferencesMap.clear();
});
}
void CUserPreferencesREST::StorePreferences(uint64 iSteamId, UserPrefsMap_t& preferences, StorageCallback_t cb)
{
#ifdef _DEBUG
Message("Storing data for %llu\n", iSteamId);
#endif
if (g_cvarUserPrefsAPI.Get().Length() == 0)
return;
// Create the JSON object with all key-value pairs
json sJsonObject = json::object();
for (const auto& [_, prefValue] : preferences)
sJsonObject[prefValue->GetKey()] = prefValue->GetValue();
// Prepare the API URL to send the request to
char sUserPreferencesUrl[256];
V_snprintf(sUserPreferencesUrl, sizeof(sUserPreferencesUrl), "%s%llu", g_cvarUserPrefsAPI.Get().String(), iSteamId);
// Dump the Json object and submit the POST request
std::string sDumpedJson = sJsonObject.dump();
g_HTTPManager.Post(sUserPreferencesUrl, sDumpedJson.c_str(), [iSteamId, cb](HTTPRequestHandle request, json data) {
#ifdef _DEBUG
Message("Executing storage callback during store for %llu\n", iSteamId);
#endif
UserPrefsMap_t preferencesMap;
((CUserPreferencesREST*)g_pUserPreferencesStorage)->JsonToPreferencesMap(data, preferencesMap);
cb(iSteamId, preferencesMap);
preferencesMap.clear();
});
}