1
0
mirror of https://github.com/alliedmodders/metamod-source.git synced 2025-02-20 13:54:14 +01:00

The 'meta refresh' command now handles plugins loaded via VDF files.

This commit is contained in:
Scott Ehlert 2008-11-24 07:25:46 -06:00
parent fc81b3b006
commit e329d40384
6 changed files with 159 additions and 83 deletions

View File

@ -203,10 +203,11 @@ CON_COMMAND(meta, "Metamod:Source Menu")
return;
} else if (strcmp(command, "refresh") == 0) {
char full_path[255];
g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
g_SmmAPI.PathFormat(filepath, sizeof(filepath), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
g_SmmAPI.PathFormat(vdfpath, sizeof(vdfpath), "%s/%s", g_ModPath.c_str(), GetMetamodBaseDir());
LoadPluginsFromFile(full_path);
LoadPlugins(filepath, vdfpath);
return;
} else if (strcmp(command, "list") == 0) {
@ -641,7 +642,7 @@ CON_COMMAND(meta, "Metamod:Source Menu")
CONMSG(" list - List plugins\n");
CONMSG(" load - Load a plugin\n");
CONMSG(" pause - Pause a running plugin\n");
CONMSG(" refresh - Reparse plugins file\n");
CONMSG(" refresh - Reparse plugin files\n");
CONMSG(" retry - Attempt to reload a plugin\n");
CONMSG(" unload - Unload a loaded plugin\n");
CONMSG(" unpause - Unpause a paused plugin\n");

View File

@ -118,12 +118,11 @@ void DoInitialPluginLoads()
mmBaseDir = GetMetamodBaseDir();
}
char full_path[260];
char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), pluginFile);
LoadPluginsFromFile(full_path);
g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), mmBaseDir);
LookForVDFs(full_path);
g_SmmAPI.PathFormat(filepath, sizeof(filepath), "%s/%s", g_ModPath.c_str(), pluginFile);
g_SmmAPI.PathFormat(vdfpath, sizeof(vdfpath), "%s/%s", g_ModPath.c_str(), mmBaseDir);
LoadPlugins(filepath, vdfpath);
}
SMM_API void *
@ -344,7 +343,7 @@ void UnloadMetamod(bool shutting_down)
g_SourceHook.CompleteShutdown();
}
void LoadFromVDF(const char *file)
bool LoadFromVDF(const char *file, bool &skipped)
{
PluginId id;
bool already, kvfileLoaded;
@ -367,13 +366,15 @@ void LoadFromVDF(const char *file)
if (!kvfileLoaded)
{
pValues->deleteThis();
return;
skipped = false;
return false;
}
if ((plugin_file = pValues->GetString("file", NULL)) == NULL)
{
pValues->deleteThis();
return;
skipped = false;
return false;
}
if ((alias = pValues->GetString("alias", NULL)) != NULL)
@ -406,18 +407,25 @@ void LoadFromVDF(const char *file)
}
id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error));
skipped = already;
if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
{
LogMessage("[META] Failed to load plugin %s: %s", plugin_file, error);
return false;
}
pValues->deleteThis();
return true;
}
void LookForVDFs(const char *dir)
int LoadVDFPluginsFromDir(const char *dir, int &skipped)
{
bool success, skip;
int total = 0;
char path[MAX_PATH];
skipped = 0;
#if defined _MSC_VER
HANDLE hFind;
WIN32_FIND_DATA fd;
@ -428,7 +436,7 @@ void LookForVDFs(const char *dir)
{
DWORD dw = GetLastError();
if (dw == ERROR_FILE_NOT_FOUND)
return;
return 0;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
@ -438,13 +446,17 @@ void LookForVDFs(const char *dir)
sizeof(error),
NULL);
LogMessage("[META] Could not open folder \"%s\" (%s)", dir, error);
return;
return 0;
}
do
{
g_SmmAPI.PathFormat(path, sizeof(path), "%s\\%s", dir, fd.cFileName);
LoadFromVDF(path);
success = LoadFromVDF(path, skip);
if (skip)
skipped++;
else if (success)
total++;
} while (FindNextFile(hFind, &fd));
FindClose(hFind);
@ -456,7 +468,7 @@ void LookForVDFs(const char *dir)
if ((pDir = opendir(dir)) == NULL)
{
LogMessage("[META] Could not open folder \"%s\" (%s)", dir, strerror(errno));
return;
return 0;
}
while ((pEnt = readdir(pDir)) != NULL)
@ -472,11 +484,17 @@ void LookForVDFs(const char *dir)
continue;
}
g_SmmAPI.PathFormat(path, sizeof(path), "%s/%s", dir, pEnt->d_name);
LoadFromVDF(path);
success = LoadFromVDF(path, skip);
if (skip)
skipped++;
else if (success)
total++;
}
closedir(pDir);
#endif
return total;
}
bool KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *resourceName, const char *pathID)
@ -509,21 +527,22 @@ bool KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *reso
return retOK;
}
int LoadPluginsFromFile(const char *_file)
int LoadPluginsFromFile(const char *filepath, int &skipped)
{
FILE *fp;
int total = 0, skipped=0;
int total = 0;
PluginId id;
bool already;
fp = fopen(_file, "rt");
skipped = 0;
fp = fopen(filepath, "rt");
if (!fp)
{
LogMessage("[META] Could not open plugins file %s\n", _file);
return -1;
return 0;
}
char buffer[255], error[255], full_path[255];
char buffer[255], error[255], full_path[PATH_SIZE];
const char *ptr, *ext, *file;
size_t length;
while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
@ -646,18 +665,29 @@ int LoadPluginsFromFile(const char *_file)
}
fclose(fp);
if (skipped)
{
LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
}
else
{
LogMessage("[META] Loaded %d plugins from file.", total);
}
return total;
}
int LoadPlugins(const char *filepath, const char *vdfpath)
{
int total, skipped, fskipped, vskipped;
const char *s = "";
total = LoadPluginsFromFile(filepath, fskipped);
total += LoadVDFPluginsFromDir(vdfpath, vskipped);
skipped = fskipped + vskipped;
if (total == 0 || total > 1)
s = "s";
if (skipped)
LogMessage("[META] Loaded %d plugin%s (%d already loaded)", total, s, skipped);
else
LogMessage("[META] Loaded %d plugin%s.", total, s);
return total;
}
void LogMessage(const char *msg, ...)
{
va_list ap;
@ -682,13 +712,11 @@ void LevelShutdown_handler(void)
{
if (!bInFirstLevel)
{
char full_path[255];
char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
LoadPluginsFromFile(full_path);
g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), GetMetamodBaseDir());
LookForVDFs(full_path);
g_SmmAPI.PathFormat(filepath, sizeof(filepath), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
g_SmmAPI.PathFormat(vdfpath, sizeof(vdfpath), "%s/%s", g_ModPath.c_str(), GetMetamodBaseDir());
LoadPlugins(filepath, vdfpath);
}
else
{

View File

@ -48,8 +48,8 @@ void *EngineFactory(const char *name, int *code);
void *PhysicsFactory(const char *name, int *code);
void *FileSystemFactory(const char *name, int *code);
/** @brief Loads all plugins found in a file */
int LoadPluginsFromFile(const char *file);
/** @brief Loads all plugins found from the mm_pluginsfile file and from VDFs in mm_basedir */
int LoadPlugins(const char *filepath, const char *vdfdir);
/** @brief Logs a message to the standard log file */
void LogMessage(const char *msg, ...);

View File

@ -235,22 +235,23 @@ CreateInterface(const char *iface, int *ret)
return ptr;
}
int
mm_LoadPluginsFromFile(const char *_file)
static int
LoadPluginsFromFile(const char *filepath, int &skipped)
{
FILE *fp;
int total = 0, skipped=0;
int total = 0;
PluginId id;
bool already;
fp = fopen(_file, "rt");
skipped = 0;
fp = fopen(filepath, "rt");
if (!fp)
{
mm_LogMessage("[META] Could not open plugins file %s\n", _file);
return -1;
return 0;
}
char buffer[255], error[255], full_path[255];
char buffer[255], error[255], full_path[PATH_SIZE];
const char *ptr, *ext, *file;
size_t length;
while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
@ -364,11 +365,6 @@ mm_LoadPluginsFromFile(const char *_file)
}
}
fclose(fp);
if (skipped)
mm_LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
else
mm_LogMessage("[META] Loaded %d plugins from file.", total);
return total;
}
@ -476,13 +472,11 @@ DoInitialPluginLoads()
}
}
char full_path[260];
char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), pluginFile);
mm_LoadPluginsFromFile(full_path);
g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), mmBaseDir);
LookForVDFs(full_path);
g_Metamod.PathFormat(filepath, sizeof(filepath), "%s/%s", mod_path.c_str(), pluginFile);
g_Metamod.PathFormat(vdfpath, sizeof(vdfpath), "%s/%s", mod_path.c_str(), mmBaseDir);
mm_LoadPlugins(filepath, vdfpath);
}
void
@ -588,21 +582,19 @@ Handler_LevelShutdown(void)
if (!in_first_level)
{
char full_path[255];
char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
g_Metamod.PathFormat(full_path,
sizeof(full_path),
g_Metamod.PathFormat(filepath,
sizeof(filepath),
"%s/%s",
mod_path.c_str(),
provider->GetConVarString(mm_pluginsfile));
mm_LoadPluginsFromFile(full_path);
g_Metamod.PathFormat(full_path,
sizeof(full_path),
g_Metamod.PathFormat(vdfpath,
sizeof(vdfpath),
"%s/%s",
mod_path.c_str(),
provider->GetConVarString(mm_basedir));
LookForVDFs(full_path);
mm_LoadPlugins(filepath, vdfpath);
}
else
{
@ -977,6 +969,11 @@ const char *MetamodSource::GetPluginsFile()
return provider->GetConVarString(mm_pluginsfile);
}
const char *MetamodSource::GetVDFDir()
{
return provider->GetConVarString(mm_basedir);
}
IConCommandBaseAccessor *MetamodSource::GetCvarBaseAccessor()
{
return provider->GetConCommandBaseAccessor();
@ -1109,30 +1106,43 @@ void MetamodSource::SetGameDLLInfo(CreateInterfaceFn serverFactory, int version,
is_gamedll_loaded = loaded;
}
static void
ProcessVDF(const char *path)
static bool
ProcessVDF(const char *path, bool &skipped)
{
PluginId id;
bool already;
char alias[24], file[255], error[255];
if (!provider->ProcessVDF(path, file, sizeof(file), alias, sizeof(alias)))
return;
{
skipped = false;
return false;
}
if (alias[0] != '\0')
g_PluginMngr.SetAlias(alias, file);
id = g_PluginMngr.Load(file, Pl_File, already, error, sizeof(error));
skipped = already;
if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
{
mm_LogMessage("[META] Failed to load plugin %s: %s", file, error);
return false;
}
return true;
}
static void
LookForVDFs(const char *dir)
static int
LoadVDFPluginsFromDir(const char *dir, int &skipped)
{
bool success, skip;
int total = 0;
char path[MAX_PATH];
char relpath[MAX_PATH * 2];
skipped = 0;
#if defined _MSC_VER
HANDLE hFind;
WIN32_FIND_DATA fd;
@ -1143,7 +1153,7 @@ LookForVDFs(const char *dir)
{
DWORD dw = GetLastError();
if (dw == ERROR_FILE_NOT_FOUND)
return;
return 0;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
@ -1153,14 +1163,18 @@ LookForVDFs(const char *dir)
sizeof(error),
NULL);
mm_LogMessage("[META] Could not open folder \"%s\" (%s)", dir, error);
return;
return 0;
}
do
{
g_Metamod.PathFormat(path, sizeof(path), "%s\\%s", dir, fd.cFileName);
UTIL_Relatize(relpath, sizeof(relpath), mod_path.c_str(), path);
ProcessVDF(relpath);
success = ProcessVDF(relpath, skip);
if (skip)
skipped++;
else if (success)
total++;
} while (FindNextFile(hFind, &fd));
FindClose(hFind);
@ -1172,7 +1186,7 @@ LookForVDFs(const char *dir)
if ((pDir = opendir(dir)) == NULL)
{
mm_LogMessage("[META] Could not open folder \"%s\" (%s)", dir, strerror(errno));
return;
return 0;
}
while ((pEnt = readdir(pDir)) != NULL)
@ -1189,11 +1203,38 @@ LookForVDFs(const char *dir)
}
g_Metamod.PathFormat(path, sizeof(path), "%s/%s", dir, pEnt->d_name);
UTIL_Relatize(relpath, sizeof(relpath), mod_path.c_str(), path);
ProcessVDF(relpath);
success = ProcessVDF(relpath, skip);
if (skip)
skipped++;
else if (success)
total++;
}
closedir(pDir);
#endif
return total;
}
int
mm_LoadPlugins(const char *filepath, const char *vdfpath)
{
int total, skipped, fskipped, vskipped;
const char *s = "";
total = LoadPluginsFromFile(filepath, fskipped);
total += LoadVDFPluginsFromDir(vdfpath, vskipped);
skipped = fskipped + vskipped;
if (total == 0 || total > 1)
s = "s";
if (skipped)
mm_LogMessage("[META] Loaded %d plugin%s (%d already loaded)", total, s, skipped);
else
mm_LogMessage("[META] Loaded %d plugin%s.", total, s);
return total;
}
bool

View File

@ -96,6 +96,7 @@ public:
bool IsLoadedAsGameDLL();
const char *GetGameBinaryPath();
const char *GetPluginsFile();
const char *GetVDFDir();
void UnregisterConCommandBase(PluginId id, ConCommandBase *pCommand);
void NotifyVSPListening(IServerPluginCallbacks *callbacks, int version);
void SetGameDLLInfo(CreateInterfaceFn serverFactory, int version, bool loaded);
@ -108,7 +109,7 @@ void
mm_LogMessage(const char *msg, ...);
int
mm_LoadPluginsFromFile(const char *_file);
mm_LoadPlugins(const char *filepath, const char *vdfpath);
void
mm_InitializeForLoad();

View File

@ -153,14 +153,19 @@ bool Command_Meta(IMetamodSourceCommandInfo *info)
}
else if (strcmp(command, "refresh") == 0)
{
char full_path[255];
g_Metamod.PathFormat(full_path,
sizeof(full_path),
char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
g_Metamod.PathFormat(filepath,
sizeof(filepath),
"%s/%s",
g_Metamod.GetBaseDir(),
g_Metamod.GetPluginsFile());
g_Metamod.PathFormat(vdfpath,
sizeof(vdfpath),
"%s/%s",
g_Metamod.GetBaseDir(),
g_Metamod.GetVDFDir());
mm_LoadPluginsFromFile(full_path);
mm_LoadPlugins(filepath, vdfpath);
return true;
}
@ -663,7 +668,7 @@ bool Command_Meta(IMetamodSourceCommandInfo *info)
CONMSG(" list - List plugins\n");
CONMSG(" load - Load a plugin\n");
CONMSG(" pause - Pause a running plugin\n");
CONMSG(" refresh - Reparse plugins file\n");
CONMSG(" refresh - Reparse plugin files\n");
CONMSG(" retry - Attempt to reload a plugin\n");
CONMSG(" unload - Unload a loaded plugin\n");
CONMSG(" unpause - Unpause a paused plugin\n");