From 4687ecd99bf528bb9f056927810c52da3d36f7a0 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 1 Aug 2005 19:56:54 +0000 Subject: [PATCH] Committed hopefully final version --- dlls/nvault/Binary.cpp | 1 + dlls/nvault/Binary.h | 1 + dlls/nvault/CQueue.h | 126 ++++++++++++++++++++++++ dlls/nvault/NVault.cpp | 20 ++++ dlls/nvault/NVault.h | 4 +- dlls/nvault/amxxapi.cpp | 137 ++++++++++++++++----------- dlls/nvault/amxxapi.h | 7 +- dlls/nvault/{sdk => }/amxxmodule.cpp | 4 +- dlls/nvault/{sdk => }/amxxmodule.h | 7 +- dlls/nvault/compat.h | 2 - dlls/nvault/{sdk => }/moduleconfig.h | 0 dlls/nvault/nvault.vcproj | 10 +- 12 files changed, 255 insertions(+), 64 deletions(-) create mode 100755 dlls/nvault/CQueue.h rename dlls/nvault/{sdk => }/amxxmodule.cpp (99%) rename dlls/nvault/{sdk => }/amxxmodule.h (99%) rename dlls/nvault/{sdk => }/moduleconfig.h (100%) diff --git a/dlls/nvault/Binary.cpp b/dlls/nvault/Binary.cpp index 9d029dca..a86caeab 100755 --- a/dlls/nvault/Binary.cpp +++ b/dlls/nvault/Binary.cpp @@ -1,4 +1,5 @@ #include "Binary.h" +#include "amxxmodule.h" BinaryWriter::BinaryWriter(FILE *fp) { diff --git a/dlls/nvault/Binary.h b/dlls/nvault/Binary.h index 2e12b603..9d6bbf5f 100755 --- a/dlls/nvault/Binary.h +++ b/dlls/nvault/Binary.h @@ -3,6 +3,7 @@ #include #include "compat.h" +#include "amxxmodule.h" class BinaryReader { diff --git a/dlls/nvault/CQueue.h b/dlls/nvault/CQueue.h new file mode 100755 index 00000000..44285185 --- /dev/null +++ b/dlls/nvault/CQueue.h @@ -0,0 +1,126 @@ +/* AMX Mod X +* +* by the AMX Mod X Development Team +* originally developed by OLO +* +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation; either version 2 of the License, or (at +* your option) any later version. +* +* 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, write to the Free Software Foundation, +* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* In addition, as a special exception, the author gives permission to +* link the code of this program with the Half-Life Game Engine ("HL +* Engine") and Modified Game Libraries ("MODs") developed by Valve, +* L.L.C ("Valve"). You must obey the GNU General Public License in all +* respects for all of the code used other than the HL Engine and MODs +* from Valve. If you modify this file, you may extend this exception +* to your version of the file, but you are not obligated to do so. If +* you do not wish to do so, delete this exception statement from your +* version. +*/ + +//by David "BAILOPAN" Anderson +#ifndef _INCLUDE_CQUEUE_H +#define _INCLUDE_CQUEUE_H + +template +class CQueue +{ +public: + class CQueueItem + { + public: + CQueueItem(const T &i, CQueueItem *n) + { + item = i; + next = n; + } + CQueueItem *GetNext() + { + return next; + } + T & GetItem() + { + return item; + } + void SetNext(CQueueItem *n) + { + next = n; + } + private: + T item; + CQueueItem *next; + }; +public: + CQueue() + { + mSize = 0; + mFirst = NULL; + mLast = NULL; + } + + bool empty() + { + return ((mSize==0)?true:false); + } + + void push(const T &v) + { + CQueueItem *p = new CQueueItem(v, NULL); + if (empty()) + { + mFirst = p; + } else { + mLast->SetNext(p); + } + mLast = p; + mSize++; + } + + void pop() + { + if (mFirst == mLast) + { + delete mFirst; + mFirst = NULL; + mLast = NULL; + } else { + CQueueItem *p = mFirst->GetNext(); + delete mFirst; + mFirst = p; + } + mSize--; + } + + T & front() + { + return mFirst->GetItem(); + } + + T & back() + { + return mLast->GetItem(); + } + + unsigned int size() + { + return mSize; + } +private: + CQueueItem *mFirst; + CQueueItem *mLast; + unsigned int mSize; +}; + +#endif //_INCLUDE_CQUEUE_H + diff --git a/dlls/nvault/NVault.cpp b/dlls/nvault/NVault.cpp index 0cd15a03..f726eda9 100755 --- a/dlls/nvault/NVault.cpp +++ b/dlls/nvault/NVault.cpp @@ -1,5 +1,6 @@ #include "NVault.h" #include "Binary.h" +#include "amxxmodule.h" template int HashFunction(const K & k) @@ -154,6 +155,17 @@ bool NVault::_SaveToFile() return true; } +const char *NVault::GetValue(const char *key) +{ + String sKey(key); + if (!m_Hash.Exists(sKey)) + { + return ""; + } else { + return m_Hash.Retrieve(sKey).c_str(); + } +} + bool NVault::Open() { _ReadFromFile(); @@ -189,6 +201,7 @@ bool NVault::Close() _SaveToFile(); m_Journal->End(); m_Journal->Erase(); + m_Open = false; return true; } @@ -260,3 +273,10 @@ bool NVault::GetValue(const char *key, time_t &stamp, char buffer[], size_t len) return true; } + +IVault *VaultMngr::OpenVault(const char *file) +{ + NVault *pVault = new NVault(file); + + return static_cast(pVault); +} diff --git a/dlls/nvault/NVault.h b/dlls/nvault/NVault.h index 7bfa2be2..af9e4777 100755 --- a/dlls/nvault/NVault.h +++ b/dlls/nvault/NVault.h @@ -36,6 +36,7 @@ public: ~NVault(); public: bool GetValue(const char *key, time_t &stamp, char buffer[], size_t len); + const char *GetValue(const char *key); void SetValue(const char *key, const char *val); void SetValue(const char *key, const char *val, time_t stamp); size_t Prune(time_t start, time_t end); @@ -44,6 +45,7 @@ public: bool Open(); bool Close(); size_t Items(); + const char *GetFilename() { return m_File.c_str(); } private: VaultError _ReadFromFile(); bool _SaveToFile(); @@ -54,7 +56,7 @@ private: bool m_Open; }; -class VaultMngr +class VaultMngr : public IVaultMngr { public: //when you delete it, it will be closed+saved automatically diff --git a/dlls/nvault/amxxapi.cpp b/dlls/nvault/amxxapi.cpp index 0dbca6be..808215c3 100755 --- a/dlls/nvault/amxxapi.cpp +++ b/dlls/nvault/amxxapi.cpp @@ -1,5 +1,13 @@ #include #include "amxxapi.h" +#include "NVault.h" +#include "CQueue.h" + +#ifdef WIN32 +#define MKDIR(p) mkdir(p) +#else +#define MKDIR(p) mkdir(p, 0755) +#endif #ifdef __linux__ #include @@ -7,29 +15,37 @@ #include #endif -CVector Vaults; +CVector g_Vaults; +CQueue g_OldVaults; + +VaultMngr g_VaultMngr; static cell nvault_open(AMX *amx, cell *params) { int len, id=-1; char *name = MF_GetAmxString(amx, params[1], 0, &len); - char *file = MF_BuildPathname("%s/nvault/%s", LOCALINFO("amxx_datadir"), name); - for (size_t i=0; iGetFilename(), file) == 0) { - id = i; - } else if (strcmp(Vaults.at(i)->GetFileName(), file) == 0) { return i; } } - Vault *v = new Vault(file); + NVault *v = new NVault(file); + if (!g_OldVaults.empty()) + { + id = g_OldVaults.front(); + g_OldVaults.pop(); + } if (id != -1) { - Vaults[id] = v; + g_Vaults[id] = v; } else { - Vaults.push_back(v); - id = (int)Vaults.size()-1; + g_Vaults.push_back(v); + id = (int)g_Vaults.size()-1; } return id; @@ -38,15 +54,16 @@ static cell nvault_open(AMX *amx, cell *params) static cell nvault_get(AMX *amx, cell *params) { unsigned int id = params[1]; - if (id > Vaults.size() || !Vaults.at(id)) + if (id >= g_Vaults.size() || !g_Vaults.at(id)) { MF_LogError(amx, AMX_ERR_NATIVE, "Invalid vault id: %d\n", id); return 0; } + NVault *pVault = g_Vaults.at(id); unsigned int numParams = (*params)/sizeof(cell); int len; char *key = MF_GetAmxString(amx, params[2], 0, &len); - const char *val = Vaults.at(id)->Find(key)->val.c_str(); + const char *val = pVault->GetValue(key); switch (numParams) { case 2: @@ -72,61 +89,45 @@ static cell nvault_get(AMX *amx, cell *params) return 0; } -static cell nvault_timeget(AMX *amx, cell *params) +static cell nvault_lookup(AMX *amx, cell *params) { unsigned int id = params[1]; - if (id > Vaults.size() || !Vaults.at(id)) + if (id >= g_Vaults.size() || !g_Vaults.at(id)) { MF_LogError(amx, AMX_ERR_NATIVE, "Invalid vault id: %d\n", id); return 0; } - unsigned int numParams = (*params)/sizeof(cell); + NVault *pVault = g_Vaults.at(id); int len; - HashTable::htNode *node; - char *key = MF_GetAmxString(amx, params[2], 0, &len); - node = Vaults.at(id)->Find(key); - const char *val = node->val.c_str(); - cell *t_addr = MF_GetAmxAddr(amx, params[3]); - *t_addr = (cell)(node->stamp); - switch (numParams) + time_t stamp; + char *key = MF_GetAmxString(amx, params[1], 0, &len); + char *buffer = new char[params[3]+1]; + if (!pVault->GetValue(key, stamp, buffer, params[3])) { - case 3: - { - return atoi(val); - break; - } - case 4: - { - cell *fAddr = MF_GetAmxAddr(amx, params[4]); - *fAddr = amx_ftoc((REAL)atof(val)); - return 1; - break; - } - case 5: - { - len = *(MF_GetAmxAddr(amx, params[5])); - return MF_SetAmxString(amx, params[4], val, len); - break; - } + delete [] buffer; + return 0; } - - return 0; + MF_SetAmxString(amx, params[2], buffer, params[3]); + cell *addr = MF_GetAmxAddr(amx, params[4]); + addr[0] = (cell)stamp; + delete [] buffer; + return 1; } static cell nvault_set(AMX *amx, cell *params) { unsigned int id = params[1]; - if (id > Vaults.size() || !Vaults.at(id)) + if (id >= g_Vaults.size() || !g_Vaults.at(id)) { 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); char *val = MF_FormatAmxString(amx, params, 3, &len); - Vaults.at(id)->Store(key, val); + pVault->SetValue(key, val); return 1; } @@ -134,27 +135,57 @@ static cell nvault_set(AMX *amx, cell *params) static cell nvault_pset(AMX *amx, cell *params) { unsigned int id = params[1]; - if (id > Vaults.size() || !Vaults.at(id)) + if (id >= g_Vaults.size() || !g_Vaults.at(id)) { 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); char *val = MF_FormatAmxString(amx, params, 3, &len); - Vaults.at(id)->Store(key, val, false); + pVault->SetValue(key, val, 0); return 1; } +static cell nvault_close(AMX *amx, cell *params) +{ + unsigned int id = params[1]; + if (id >= g_Vaults.size() || !g_Vaults.at(id)) + { + 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; + g_OldVaults.push(id); + + return 1; +} + +IVaultMngr *GetVaultMngr() +{ + return static_cast(&g_VaultMngr); +} + void OnAmxxAttach() { //create the dir if it doesn't exist -#ifdef __linux__ - mkdir(MF_BuildPathname("%s/nvault", LOCALINFO("amxx_datadir")), 0700); -#else - mkdir(MF_BuildPathname("%s/nvault", LOCALINFO("amxx_datadir"))); -#endif + MKDIR(MF_BuildPathname("%s/vault", LOCALINFO("amxx_datadir"))); + MF_AddNatives(nVault_natives); + MF_RegisterFunction(GetVaultMngr, "GetVaultMngr"); } + +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}, + {NULL, NULL}, +}; \ No newline at end of file diff --git a/dlls/nvault/amxxapi.h b/dlls/nvault/amxxapi.h index 876391d5..16afc639 100755 --- a/dlls/nvault/amxxapi.h +++ b/dlls/nvault/amxxapi.h @@ -2,10 +2,9 @@ #define _INCLUDE_AMXXAPI_H #include "CVector.h" -#include "hash.h" -#include "nvault.h" -#include "journal.h" -#include "sdk/amxxmodule.h" +#include "CQueue.h" +#include "CString.h" +#include "amxxmodule.h" AMX_NATIVE_INFO nVault_natives[]; diff --git a/dlls/nvault/sdk/amxxmodule.cpp b/dlls/nvault/amxxmodule.cpp similarity index 99% rename from dlls/nvault/sdk/amxxmodule.cpp rename to dlls/nvault/amxxmodule.cpp index aa649e2b..c38a396b 100755 --- a/dlls/nvault/sdk/amxxmodule.cpp +++ b/dlls/nvault/amxxmodule.cpp @@ -2502,6 +2502,7 @@ PFN_GET_PLAYER_EDICT g_fn_GetPlayerEdict; PFN_FORMAT g_fn_Format; PFN_REGISTERFUNCTION g_fn_RegisterFunction; PFN_REQ_FNPTR g_fn_RequestFunction; +PFN_AMX_PUSH g_fn_AmxPush; // *** Exports *** C_DLLEXPORT int AMXX_Query(int *interfaceVersion, amxx_module_info_s *moduleInfo) @@ -2610,6 +2611,7 @@ C_DLLEXPORT int AMXX_Attach(PFN_REQ_FNPTR reqFnptrFunc) REQFUNC("GetPlayerHealth", g_fn_GetPlayerHealth, PFN_GET_PLAYER_HEALTH); REQFUNC("GetPlayerFlags", g_fn_GetPlayerFlags, PFN_GETPLAYERFLAGS); REQFUNC("GetPlayerEdict", g_fn_GetPlayerEdict, PFN_GET_PLAYER_EDICT); + REQFUNC("amx_Push", g_fn_AmxPush, PFN_AMX_PUSH); // Memory REQFUNC_OPT("Allocator", g_fn_Allocator, PFN_ALLOCATOR); @@ -2717,7 +2719,7 @@ void ValidateMacros_DontCallThis_Smiley() MF_IsPlayerHLTV(0); MF_GetPlayerArmor(0); MF_GetPlayerHealth(0); - MF_AmxExec(0, 0, 0, 0); + MF_AmxExec(0, 0, 0); MF_AmxExecv(0, 0, 0, 0, 0); MF_AmxFindPublic(0, 0, 0); MF_AmxAllot(0, 0, 0, 0); diff --git a/dlls/nvault/sdk/amxxmodule.h b/dlls/nvault/amxxmodule.h similarity index 99% rename from dlls/nvault/sdk/amxxmodule.h rename to dlls/nvault/amxxmodule.h index bbaec2f5..baf6d259 100755 --- a/dlls/nvault/sdk/amxxmodule.h +++ b/dlls/nvault/amxxmodule.h @@ -1987,7 +1987,7 @@ typedef void * (*PFN_REALLOCATOR) (const char* /*filename*/, const unsigned const unsigned int /*type*/, const size_t /*size*/, void* /*addr*/ ); typedef void (*PFN_DEALLOCATOR) (const char* /*filename*/, const unsigned int /*line*/, const char* /*func*/, const unsigned int /*type*/, const void* /*addr*/ ); -typedef int (*PFN_AMX_EXEC) (AMX* /*amx*/, cell* /*return val*/, int /*index*/, int /*numparams*/, ... /*params*/); +typedef int (*PFN_AMX_EXEC) (AMX* /*amx*/, cell* /*return val*/, int /*index*/); typedef int (*PFN_AMX_EXECV) (AMX* /*amx*/, cell* /*return val*/, int /*index*/, int /*numparams*/, cell[] /*params*/); typedef int (*PFN_AMX_ALLOT) (AMX* /*amx*/, int /*length*/, cell* /*amx_addr*/, cell** /*phys_addr*/); typedef int (*PFN_AMX_FINDPUBLIC) (AMX* /*amx*/, char* /*func name*/, int* /*index*/); @@ -2002,6 +2002,7 @@ typedef void (*PFN_UNREGISTER_SPFORWARD) (int /*id*/); typedef void (*PFN_MERGEDEFINITION_FILE) (const char * /*filename*/); typedef const char * (*PFN_FORMAT) (const char * /*fmt*/, ... /*params*/); typedef void (*PFN_REGISTERFUNCTION) (void * /*pfn*/, const char * /*desc*/); +typedef int (*PFN_AMX_PUSH) (AMX * /*amx*/, cell /*value*/); extern PFN_ADD_NATIVES g_fn_AddNatives; extern PFN_BUILD_PATHNAME g_fn_BuildPathname; @@ -2065,6 +2066,7 @@ extern PFN_FORMAT g_fn_Format; extern PFN_GET_PLAYER_TEAM g_fn_GetPlayerTeam; extern PFN_REGISTERFUNCTION g_fn_RegisterFunction; extern PFN_REQ_FNPTR g_fn_RequestFunction; +extern PFN_AMX_PUSH g_fn_AmxPush; #ifdef MAY_NEVER_BE_DEFINED // Function prototypes for intellisense and similar systems @@ -2123,6 +2125,8 @@ edict_t* MF_GetPlayerEdict (int id) { } const char * MF_Format (const char *fmt, ...) { } void MF_RegisterFunction (void *pfn, const char *description) { } void * MF_RequestFunction (const char *description) { } +int MF_AmxPush (AMX *amx, cell *params) { } +int MF_AmxExec (AMX *amx, cell *retval, int idx) { } #endif // MAY_NEVER_BE_DEFINED #define MF_AddNatives g_fn_AddNatives @@ -2187,6 +2191,7 @@ void MF_LogError(AMX *amx, int err, const char *fmt, ...); #define MF_Format g_fn_Format #define MF_RegisterFunction g_fn_RegisterFunction #define MF_RequestFunction g_fn_RequestFunction; +#define MF_AmxPush g_fn_AmxPush /*** Memory ***/ void *operator new(size_t reportedSize); diff --git a/dlls/nvault/compat.h b/dlls/nvault/compat.h index 190a78ab..70f6de80 100755 --- a/dlls/nvault/compat.h +++ b/dlls/nvault/compat.h @@ -2,8 +2,6 @@ #define _INCLUDE_COMPAT_H #ifdef WIN32 -typedef __int32 int32_t; -typedef unsigned __int32 uint32_t; typedef __int16 int16_t; typedef unsigned __int16 uint16_t; typedef __int8 int8_t; diff --git a/dlls/nvault/sdk/moduleconfig.h b/dlls/nvault/moduleconfig.h similarity index 100% rename from dlls/nvault/sdk/moduleconfig.h rename to dlls/nvault/moduleconfig.h diff --git a/dlls/nvault/nvault.vcproj b/dlls/nvault/nvault.vcproj index 6f6a00b4..267fe071 100755 --- a/dlls/nvault/nvault.vcproj +++ b/dlls/nvault/nvault.vcproj @@ -152,6 +152,9 @@ + + @@ -174,10 +177,10 @@ Name="SDK" Filter=""> + RelativePath=".\amxxmodule.cpp"> + RelativePath=".\amxxmodule.h"> @@ -185,6 +188,9 @@ + +