2014-08-04 10:38:42 +00:00
|
|
|
// vim: set ts=4 sw=4 tw=99 noet:
|
|
|
|
//
|
|
|
|
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
|
|
|
// Copyright (C) The AMX Mod X Development Team.
|
|
|
|
//
|
|
|
|
// This software is licensed under the GNU General Public License, version 3 or higher.
|
|
|
|
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
|
|
|
// https://alliedmods.net/amxmodx-license
|
|
|
|
|
|
|
|
//
|
|
|
|
// NVault Module
|
|
|
|
//
|
|
|
|
|
2005-07-23 16:57:21 +00:00
|
|
|
#include <stdlib.h>
|
2005-04-03 03:26:35 +00:00
|
|
|
#include "amxxapi.h"
|
2005-08-01 19:56:54 +00:00
|
|
|
#include "NVault.h"
|
2014-08-10 14:57:53 +00:00
|
|
|
|
2005-08-01 19:56:54 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#define MKDIR(p) mkdir(p)
|
|
|
|
#else
|
|
|
|
#define MKDIR(p) mkdir(p, 0755)
|
|
|
|
#endif
|
2005-04-03 03:26:35 +00:00
|
|
|
|
2013-02-13 07:14:37 +00:00
|
|
|
#if defined(__linux__) || defined(__APPLE__)
|
2005-07-23 16:57:21 +00:00
|
|
|
#include <unistd.h>
|
2006-06-04 07:49:07 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2005-07-23 16:57:21 +00:00
|
|
|
#else
|
|
|
|
#include <direct.h>
|
|
|
|
#endif
|
|
|
|
|
2014-08-10 14:57:53 +00:00
|
|
|
ke::Vector<NVault *> g_Vaults;
|
2017-04-14 17:36:59 +00:00
|
|
|
ke::Deque<int> g_OldVaults;
|
2005-08-01 19:56:54 +00:00
|
|
|
|
|
|
|
VaultMngr g_VaultMngr;
|
2005-04-03 03:26:35 +00:00
|
|
|
|
2008-03-04 19:06:39 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
extern "C" void __cxa_pure_virtual(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-04-03 03:26:35 +00:00
|
|
|
static cell nvault_open(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
int len, id=-1;
|
|
|
|
char *name = MF_GetAmxString(amx, params[1], 0, &len);
|
2005-08-01 19:56:54 +00:00
|
|
|
char path[255], file[255];
|
2017-03-11 18:26:25 +00:00
|
|
|
MF_BuildPathnameR(path, sizeof(path), "%s/vault", MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"));
|
2005-08-01 20:54:41 +00:00
|
|
|
sprintf(file, "%s/%s.vault", path, name);
|
2014-08-10 14:57:53 +00:00
|
|
|
for (size_t i=0; i<g_Vaults.length(); i++)
|
2005-04-03 03:26:35 +00:00
|
|
|
{
|
2005-09-18 02:59:36 +00:00
|
|
|
if (!g_Vaults[i])
|
|
|
|
continue;
|
2017-03-11 18:26:25 +00:00
|
|
|
if (strcmp(g_Vaults.at(i)->GetFilename(), file) == 0)
|
2005-04-03 03:26:35 +00:00
|
|
|
return i;
|
|
|
|
}
|
2006-08-18 22:46:07 +00:00
|
|
|
NVault *v = (NVault *)g_VaultMngr.OpenVault(file);
|
|
|
|
if (v == NULL || !v->Open())
|
2005-08-01 20:54:41 +00:00
|
|
|
{
|
|
|
|
delete v;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-08-01 19:56:54 +00:00
|
|
|
if (!g_OldVaults.empty())
|
|
|
|
{
|
2017-04-14 17:36:59 +00:00
|
|
|
id = g_OldVaults.popFrontCopy();
|
2005-08-01 19:56:54 +00:00
|
|
|
}
|
2005-04-03 03:26:35 +00:00
|
|
|
if (id != -1)
|
|
|
|
{
|
2005-08-01 19:56:54 +00:00
|
|
|
g_Vaults[id] = v;
|
2005-04-03 03:26:35 +00:00
|
|
|
} else {
|
2014-08-10 14:57:53 +00:00
|
|
|
g_Vaults.append(v);
|
|
|
|
id = (int)g_Vaults.length()-1;
|
2005-04-03 03:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2006-08-20 21:23:38 +00:00
|
|
|
static cell nvault_touch(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
unsigned int id = params[1];
|
2014-08-10 14:57:53 +00:00
|
|
|
if (id >= g_Vaults.length() || !g_Vaults.at(id))
|
2006-08-20 21:23:38 +00:00
|
|
|
{
|
|
|
|
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid vault id: %d\n", id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
NVault *pVault = g_Vaults.at(id);
|
|
|
|
int len;
|
|
|
|
char *key = MF_GetAmxString(amx, params[2], 0, &len);
|
|
|
|
|
|
|
|
if (params[3] == -1)
|
|
|
|
{
|
|
|
|
pVault->Touch(key, time(NULL));
|
|
|
|
} else {
|
|
|
|
pVault->Touch(key, static_cast<time_t>(params[3]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-04-03 03:26:35 +00:00
|
|
|
static cell nvault_get(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
unsigned int id = params[1];
|
2014-08-10 14:57:53 +00:00
|
|
|
if (id >= g_Vaults.length() || !g_Vaults.at(id))
|
2005-07-23 16:57:21 +00:00
|
|
|
{
|
|
|
|
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid vault id: %d\n", id);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-01 19:56:54 +00:00
|
|
|
NVault *pVault = g_Vaults.at(id);
|
2005-07-23 16:57:21 +00:00
|
|
|
unsigned int numParams = (*params)/sizeof(cell);
|
|
|
|
int len;
|
|
|
|
char *key = MF_GetAmxString(amx, params[2], 0, &len);
|
2005-08-01 19:56:54 +00:00
|
|
|
const char *val = pVault->GetValue(key);
|
2005-07-23 16:57:21 +00:00
|
|
|
switch (numParams)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
return atoi(val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
cell *fAddr = MF_GetAmxAddr(amx, params[3]);
|
|
|
|
*fAddr = amx_ftoc((REAL)atof(val));
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
len = *(MF_GetAmxAddr(amx, params[4]));
|
|
|
|
return MF_SetAmxString(amx, params[3], val, len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-01 19:56:54 +00:00
|
|
|
static cell nvault_lookup(AMX *amx, cell *params)
|
2005-07-23 16:57:21 +00:00
|
|
|
{
|
|
|
|
unsigned int id = params[1];
|
2014-08-10 14:57:53 +00:00
|
|
|
if (id >= g_Vaults.length() || !g_Vaults.at(id))
|
2005-07-23 16:57:21 +00:00
|
|
|
{
|
|
|
|
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid vault id: %d\n", id);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-01 19:56:54 +00:00
|
|
|
NVault *pVault = g_Vaults.at(id);
|
2005-07-23 16:57:21 +00:00
|
|
|
int len;
|
2005-08-01 19:56:54 +00:00
|
|
|
time_t stamp;
|
2005-08-01 20:54:41 +00:00
|
|
|
char *key = MF_GetAmxString(amx, params[2], 0, &len);
|
|
|
|
char *buffer = new char[params[4]+1];
|
|
|
|
if (!pVault->GetValue(key, stamp, buffer, params[4]))
|
2005-07-23 16:57:21 +00:00
|
|
|
{
|
2005-08-01 19:56:54 +00:00
|
|
|
delete [] buffer;
|
|
|
|
return 0;
|
2005-07-23 16:57:21 +00:00
|
|
|
}
|
2005-08-01 20:54:41 +00:00
|
|
|
MF_SetAmxString(amx, params[3], buffer, params[4]);
|
|
|
|
cell *addr = MF_GetAmxAddr(amx, params[5]);
|
2005-08-01 19:56:54 +00:00
|
|
|
addr[0] = (cell)stamp;
|
|
|
|
delete [] buffer;
|
|
|
|
return 1;
|
2005-07-23 16:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell nvault_set(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
unsigned int id = params[1];
|
2014-08-10 14:57:53 +00:00
|
|
|
if (id >= g_Vaults.length() || !g_Vaults.at(id))
|
2005-04-03 03:26:35 +00:00
|
|
|
{
|
|
|
|
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid vault id: %d\n", id);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-01 19:56:54 +00:00
|
|
|
NVault *pVault = g_Vaults.at(id);
|
2005-07-23 16:57:21 +00:00
|
|
|
int len;
|
2015-01-29 15:52:47 +00:00
|
|
|
const char *key = MF_GetAmxString(amx, params[2], 0, &len);
|
|
|
|
const char *val = MF_GetAmxString(amx, params[3], 1, &len);
|
2005-07-23 16:57:21 +00:00
|
|
|
|
2005-08-01 19:56:54 +00:00
|
|
|
pVault->SetValue(key, val);
|
2005-07-23 16:57:21 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell nvault_pset(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
unsigned int id = params[1];
|
2014-08-10 14:57:53 +00:00
|
|
|
if (id >= g_Vaults.length() || !g_Vaults.at(id))
|
2005-07-23 16:57:21 +00:00
|
|
|
{
|
|
|
|
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid vault id: %d\n", id);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-01 19:56:54 +00:00
|
|
|
NVault *pVault = g_Vaults.at(id);
|
2005-07-23 16:57:21 +00:00
|
|
|
int len;
|
2015-01-29 15:52:47 +00:00
|
|
|
const char *key = MF_GetAmxString(amx, params[2], 0, &len);
|
|
|
|
const char *val = MF_GetAmxString(amx, params[3], 1, &len);
|
2005-07-23 16:57:21 +00:00
|
|
|
|
2005-08-01 19:56:54 +00:00
|
|
|
pVault->SetValue(key, val, 0);
|
2005-04-03 03:26:35 +00:00
|
|
|
|
2005-07-23 16:57:21 +00:00
|
|
|
return 1;
|
2005-04-03 03:26:35 +00:00
|
|
|
}
|
|
|
|
|
2005-08-01 19:56:54 +00:00
|
|
|
static cell nvault_close(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
unsigned int id = params[1];
|
2014-08-10 14:57:53 +00:00
|
|
|
if (id >= g_Vaults.length() || !g_Vaults.at(id))
|
2005-08-01 19:56:54 +00:00
|
|
|
{
|
|
|
|
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid vault id: %d\n", id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
NVault *pVault = g_Vaults.at(id);
|
|
|
|
pVault->Close();
|
|
|
|
delete pVault;
|
|
|
|
g_Vaults[id] = NULL;
|
2017-04-14 17:36:59 +00:00
|
|
|
g_OldVaults.append(id);
|
2005-08-01 19:56:54 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-08-01 20:54:41 +00:00
|
|
|
static cell AMX_NATIVE_CALL nvault_prune(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
unsigned int id = params[1];
|
2014-08-10 14:57:53 +00:00
|
|
|
if (id >= g_Vaults.length() || !g_Vaults.at(id))
|
2005-08-01 20:54:41 +00:00
|
|
|
{
|
|
|
|
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid vault id: %d\n", id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
NVault *pVault = g_Vaults.at(id);
|
|
|
|
time_t start = (time_t )params[2];
|
|
|
|
time_t end = (time_t )params[3];
|
|
|
|
|
|
|
|
return pVault->Prune(start, end);
|
|
|
|
}
|
|
|
|
|
2006-04-26 05:21:29 +00:00
|
|
|
static cell AMX_NATIVE_CALL nvault_remove(AMX *amx, cell *params)
|
|
|
|
{
|
|
|
|
unsigned int id = params[1];
|
2014-08-10 14:57:53 +00:00
|
|
|
if (id >= g_Vaults.length() || !g_Vaults.at(id))
|
2006-04-26 05:21:29 +00:00
|
|
|
{
|
|
|
|
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid vault id: %d\n", id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
NVault *pVault = g_Vaults.at(id);
|
|
|
|
int len;
|
|
|
|
const char *key = MF_GetAmxString(amx, params[2], 0, &len);
|
|
|
|
pVault->Remove(key);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-08-01 19:56:54 +00:00
|
|
|
IVaultMngr *GetVaultMngr()
|
|
|
|
{
|
|
|
|
return static_cast<IVaultMngr *>(&g_VaultMngr);
|
|
|
|
}
|
|
|
|
|
2005-04-03 03:26:35 +00:00
|
|
|
void OnAmxxAttach()
|
|
|
|
{
|
|
|
|
//create the dir if it doesn't exist
|
2006-05-10 11:53:22 +00:00
|
|
|
MKDIR(MF_BuildPathname("%s/vault", MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data")));
|
2005-08-01 19:56:54 +00:00
|
|
|
MF_AddNatives(nVault_natives);
|
2005-08-02 11:51:31 +00:00
|
|
|
MF_RegisterFunction((void *)GetVaultMngr, "GetVaultMngr");
|
2005-04-03 03:26:35 +00:00
|
|
|
}
|
2005-08-01 19:56:54 +00:00
|
|
|
|
2006-05-10 11:53:22 +00:00
|
|
|
void OnPluginsUnloaded()
|
2005-08-01 20:54:41 +00:00
|
|
|
{
|
2014-08-10 14:57:53 +00:00
|
|
|
for (size_t i=0; i<g_Vaults.length(); i++)
|
2005-08-01 20:54:41 +00:00
|
|
|
{
|
|
|
|
if (g_Vaults[i])
|
|
|
|
delete g_Vaults[i];
|
|
|
|
}
|
|
|
|
g_Vaults.clear();
|
|
|
|
while (!g_OldVaults.empty())
|
2017-04-14 17:36:59 +00:00
|
|
|
g_OldVaults.popFront();
|
2005-08-01 20:54:41 +00:00
|
|
|
}
|
|
|
|
|
2005-08-01 19:56:54 +00:00
|
|
|
AMX_NATIVE_INFO nVault_natives[] = {
|
|
|
|
{"nvault_open", nvault_open},
|
|
|
|
{"nvault_get", nvault_get},
|
|
|
|
{"nvault_lookup", nvault_lookup},
|
|
|
|
{"nvault_set", nvault_set},
|
|
|
|
{"nvault_pset", nvault_pset},
|
|
|
|
{"nvault_close", nvault_close},
|
2005-08-01 20:54:41 +00:00
|
|
|
{"nvault_prune", nvault_prune},
|
2006-04-26 05:21:29 +00:00
|
|
|
{"nvault_remove", nvault_remove},
|
2006-08-20 21:23:38 +00:00
|
|
|
{"nvault_touch", nvault_touch},
|
2005-08-01 19:56:54 +00:00
|
|
|
{NULL, NULL},
|
2005-08-01 20:54:41 +00:00
|
|
|
};
|