Skip to content

Commit 28ba936

Browse files
authored
Merge pull request #1 from Next21Team/nvault
Add nvault provider
2 parents ba68b56 + f8af6b7 commit 28ba936

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "player_prefs",
3+
"prune_days": 0
4+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#include <amxmodx>
2+
#include <time>
3+
#include <nvault>
4+
#include <json>
5+
#include <player_prefs>
6+
7+
new const CONFIG_FILE[] = "addons/amxmodx/configs/player_prefs_nvault.json";
8+
9+
new g_szVaultName[MAX_NAME_LENGTH];
10+
new g_iPruneDays;
11+
new g_nvHandle = INVALID_HANDLE;
12+
new Array: g_aKeys = Invalid_Array;
13+
14+
public plugin_init()
15+
{
16+
register_plugin("Player Prefs — nVault Provider", "1.0.0", "Psycrow");
17+
}
18+
19+
public plugin_end()
20+
{
21+
if (g_nvHandle != INVALID_HANDLE)
22+
{
23+
nvault_close(g_nvHandle);
24+
g_nvHandle = INVALID_HANDLE;
25+
}
26+
}
27+
28+
public pp_provider_connect()
29+
{
30+
if (!ReadConfig())
31+
{
32+
pp_provider_ready(false);
33+
return;
34+
}
35+
36+
log_amx("[PP nVault] Config loaded. nVault name: %s", g_szVaultName);
37+
38+
g_nvHandle = nvault_open(g_szVaultName);
39+
if (g_nvHandle == INVALID_HANDLE)
40+
{
41+
log_amx("[PP nVault] Error opening nVault: %s", g_szVaultName);
42+
pp_provider_ready(false);
43+
return;
44+
}
45+
46+
if (g_iPruneDays > 0)
47+
nvault_prune(g_nvHandle, 0, get_systime() - (g_iPruneDays * SECONDS_IN_DAY));
48+
49+
g_aKeys = ArrayCreate(MAX_KEY_LENGTH);
50+
51+
pp_provider_ready(true);
52+
}
53+
54+
public pp_provider_load_keys()
55+
{
56+
pp_provider_keys_done();
57+
}
58+
59+
public pp_provider_load_player(const playerIndex, const authId[])
60+
{
61+
new keysNum = ArraySize(g_aKeys);
62+
new nvKey[MAX_KEY_LENGTH + MAX_AUTHID_LENGTH];
63+
new key[MAX_KEY_LENGTH], value[MAX_VALUE_LENGTH];
64+
65+
for (new i; i < keysNum; i++)
66+
{
67+
ArrayGetString(g_aKeys, i, key, charsmax(key));
68+
FormatKey(nvKey, charsmax(nvKey), authId, key);
69+
70+
if (nvault_get(g_nvHandle, nvKey, value, charsmax(value)))
71+
{
72+
nvault_touch(g_nvHandle, nvKey);
73+
pp_provider_pref_loaded(playerIndex, key, value);
74+
}
75+
}
76+
77+
pp_provider_player_done(playerIndex);
78+
}
79+
80+
public pp_provider_save_pref(const playerIndex, const authId[], const key[], const value[])
81+
{
82+
new nvKey[MAX_KEY_LENGTH + MAX_AUTHID_LENGTH];
83+
FormatKey(nvKey, charsmax(nvKey), authId, key);
84+
85+
nvault_set(g_nvHandle, nvKey, value);
86+
}
87+
88+
public pp_provider_register_key(const key[], const defaultValue[])
89+
{
90+
ArrayPushString(g_aKeys, key);
91+
}
92+
93+
FormatKey(output[], len, const authId[], const key[])
94+
{
95+
return formatex(output, len, "%s%s", authId, key);
96+
}
97+
98+
bool: ReadConfig()
99+
{
100+
if (!file_exists(CONFIG_FILE))
101+
{
102+
log_error(AMX_ERR_NATIVE, "[PP nVault] Config file not found: %s", CONFIG_FILE);
103+
return false;
104+
}
105+
106+
new JSON: hConfig = json_parse(CONFIG_FILE, .is_file = true);
107+
108+
if (hConfig == Invalid_JSON || !json_is_object(hConfig))
109+
{
110+
if (hConfig != Invalid_JSON) json_free(hConfig);
111+
log_amx("[PP nVault] JSON error in config file: %s", CONFIG_FILE);
112+
return false;
113+
}
114+
115+
json_object_get_string(hConfig, "name", g_szVaultName, charsmax(g_szVaultName));
116+
g_iPruneDays = json_object_get_number(hConfig, "prune_days");
117+
118+
json_free(hConfig);
119+
120+
if (g_szVaultName[0] == EOS)
121+
{
122+
log_amx("[PP nVault] Configuration is incomplete: name is required");
123+
return false;
124+
}
125+
126+
return true;
127+
}

0 commit comments

Comments
 (0)