corrected commit

This commit is contained in:
David Anderson 2006-05-07 17:11:17 +00:00
parent 603be35426
commit 0a4a209f94
3 changed files with 312 additions and 9 deletions

243
amxmodx/libraries.cpp Normal file
View File

@ -0,0 +1,243 @@
#include "libraries.h"
#include "sh_list.h"
List<Library *> 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<Library *>::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<Library *>::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<Library *>::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<Library *>::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;
}

69
amxmodx/libraries.h Normal file
View File

@ -0,0 +1,69 @@
#ifndef _INCLUDE_LIBRARIES_H
#define _INCLUDE_LIBRARIES_H
#include <string.h>
#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

View File

@ -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<CModule, const char *>::iterator a = g_modules.find(path.c_str());