diff --git a/amxmodx/libraries.cpp b/amxmodx/libraries.cpp new file mode 100644 index 00000000..7a650362 --- /dev/null +++ b/amxmodx/libraries.cpp @@ -0,0 +1,243 @@ +#include "libraries.h" +#include "sh_list.h" + +List g_libraries; + +bool AddLibrary(const char *name, LibType type, LibSource src, void *parent) +{ + if (FindLibrary(name, type)) + return false; + + Library *lib = new Library; + + lib->name.assign(name); + lib->type = type; + lib->src = src; + lib->parent = parent; + + g_libraries.push_back(lib); + + return true; +} + +bool DecodeLibCmdString(const char *str, LibDecoder &dec) +{ + if (str[0] != '_') + { + dec.cmd = LibCmd_ReqLib; + dec.buffer = strdup(str); + if (strcmp(str, "dbi") == 0) + dec.cmd = LibCmd_ReqClass; + dec.param1 = dec.buffer; + dec.param2 = NULL; + } else { + str++; + if (*str == 'r') + { + str++; + if (*str == 'c') + dec.cmd = LibCmd_ReqClass; + else if (*str == 'l') + dec.cmd = LibCmd_ReqLib; + else + return false; + str++; + } else if (*str == 'f') { + str++; + dec.cmd = LibCmd_ForceLib; + } else if (*str == 'e') { + str++; + if (*str == 'c') + dec.cmd = LibCmd_ExpectClass; + else if (*str == 'l') + dec.cmd = LibCmd_ExpectLib; + else + return false; + str++; + } else if (*str == 'd') { + str++; + dec.cmd = LibCmd_DefaultLib; + } + if (*str != '_') + return false; + str++; + if (dec.cmd < LibCmd_ExpectLib) + { + dec.buffer = strdup(str); + dec.param1 = dec.buffer; + dec.param2 = NULL; + } else { + dec.buffer = strdup(str); + char *p = strchr(str, '_'); + while (p && (*(p+1) != '_')) + p = strchr(str, '_'); + if (!p || !*(p+1)) + return false; + *p = '\0'; + dec.param1 = dec.buffer; + dec.param2 = p+1; + } + } + + return true; +} + +size_t AddLibrariesFromString(const char *name, LibType type, LibSource src, void *parent) +{ + char buffer[255]; + char *ptr, *p, s; + size_t count = 0; + + snprintf(buffer, sizeof(buffer)-1, "%s", name); + + ptr = buffer; + p = buffer; + while (*p) + { + while (*p && (*p != ',')) + p++; + s = *p; + *p = '\0'; + if (AddLibrary(ptr, type, src, parent)) + count++; + if (!s) + break; + p++; + while (*p && (*p == ',')) + p++; + ptr = p; + } + + return count; +} + +size_t ClearLibraries(LibSource src) +{ + List::iterator iter; + size_t count = 0; + + iter = g_libraries.begin(); + while (iter != g_libraries.end()) + { + if ( (*iter)->src == src ) + { + delete (*iter); + iter = g_libraries.erase(iter); + count++; + } else { + iter++; + } + } + + return count; +} + +size_t RemoveLibraries(void *parent) +{ + List::iterator iter; + Library *lib; + size_t count = 0; + + iter = g_libraries.begin(); + while (iter != g_libraries.end()) + { + lib = (*iter); + if (lib->parent == parent) + { + delete (*iter); + iter = g_libraries.erase(iter); + count++; + } else { + iter++; + } + } + + return count; +} + +bool FindLibrary(const char *name, LibType type) +{ + List::iterator iter; + Library *lib; + + for (iter = g_libraries.begin(); iter != g_libraries.end(); iter++) + { + lib = (*iter); + if (lib->type != type) + continue; + if (strcasecmp(lib->name.c_str(), name) == 0) + { + return true; + } + } + + return false; +} + +LibError RunLibCommand(const LibDecoder *enc) +{ + List::iterator iter,end; + Library *lib; + + iter = g_libraries.begin(); + end = g_libraries.end(); + + if ( (enc->cmd == LibCmd_ReqLib) || (enc->cmd == LibCmd_ReqClass) ) + { + LibType expect; + + if (enc->cmd == LibCmd_ReqLib) + expect = LibType_Library; + else if (enc->cmd == LibCmd_ReqClass) + expect = LibType_Class; + + /** see if it exists */ + for (; iter != end; iter++) + { + lib = (*iter); + if (lib->type != expect) + continue; + if (strcasecmp(lib->name.c_str(), enc->param1) == 0) + return LibErr_None; + } + if (expect == LibType_Library) + return LibErr_NoLibrary; + else if (expect = LibType_Class) + return LibErr_NoClass; + + return LibErr_NoLibrary; + } else if (enc->cmd == LibCmd_ForceLib) { + if (!LoadModule(enc->param1, PT_ANYTIME)) + { + return LibErr_NoLibrary; + } + } else if ( (enc->cmd == LibCmd_DefaultLib) || + ((enc->cmd == LibCmd_ExpectLib) || (enc->cmd == LibCmd_ExpectClass)) ) + { + LibType expect; + + if (enc->cmd == LibCmd_ExpectLib) + expect = LibType_Library; + else if (enc->cmd == LibCmd_ExpectClass) + expect = LibType_Class; + + /** see if it exists */ + for (; iter != end; iter++) + { + lib = (*iter); + if (lib->type != expect) + continue; + if (strcasecmp(lib->name.c_str(), enc->param1) == 0) + return LibErr_None; + } + + if (!LoadModule(enc->param2, PT_ANYTIME)) + { + return LibErr_NoLibrary; + } + + return LibErr_None; + } + + return LibErr_None; +} diff --git a/amxmodx/libraries.h b/amxmodx/libraries.h new file mode 100644 index 00000000..3a696550 --- /dev/null +++ b/amxmodx/libraries.h @@ -0,0 +1,69 @@ +#ifndef _INCLUDE_LIBRARIES_H +#define _INCLUDE_LIBRARIES_H + +#include +#include "amxmodx.h" +#include "CString.h" + +enum LibSource +{ + LibSource_Plugin, + LibSource_Module +}; + +enum LibType +{ + LibType_Library, + LibType_Class +}; + +struct Library +{ + String name; + LibSource src; + LibType type; + void *parent; +}; + +enum LibCmd +{ + LibCmd_ReqLib, + LibCmd_ReqClass, + LibCmd_ForceLib, + LibCmd_ExpectLib, + LibCmd_ExpectClass, + LibCmd_DefaultLib, +}; + +enum LibError +{ + LibErr_None = 0, + LibErr_NoLibrary, + LibErr_NoClass, +}; + +struct LibDecoder +{ + LibDecoder() : buffer(NULL) + { + }; + ~LibDecoder() + { + free(buffer); + } + char *buffer; + char *param1; + char *param2; + LibCmd cmd; +}; + +bool AddLibrary(const char *name, LibType type, LibSource src, void *parent=NULL); +bool DecodeLibCmdString(const char *str, LibDecoder &cmd); +size_t AddLibrariesFromString(const char *name, LibType type, LibSource src, void *parent=NULL); +size_t ClearLibraries(LibSource src); +LibError RunLibCommand(const LibDecoder *enc); +size_t RemoveLibraries(void *parent); +bool FindLibrary(const char *name, LibType type); + + +#endif //_INCLUDE_LIBRARIES_H diff --git a/amxmodx/modules.cpp b/amxmodx/modules.cpp index 3b034902..317b6e53 100755 --- a/amxmodx/modules.cpp +++ b/amxmodx/modules.cpp @@ -831,11 +831,6 @@ bool ConvertModuleName(const char *pathString, String &path) path.append(".so"); #endif - FILE *fp = fopen(path.c_str(), "rb"); - if (!fp) - return false; - fclose(fp); - return true; } @@ -857,10 +852,6 @@ bool LoadModule(const char *shortname, PLUG_LOADTIME now, bool simplify) return false; } else { path.assign(pathString); - FILE *fp = fopen(path.c_str(), "rb"); - if (!fp) - return false; - fclose(fp); } CList::iterator a = g_modules.find(path.c_str());