1
0
mirror of https://github.com/alliedmodders/metamod-source.git synced 2025-02-21 14: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; return;
} else if (strcmp(command, "refresh") == 0) { } else if (strcmp(command, "refresh") == 0) {
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()); 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; return;
} else if (strcmp(command, "list") == 0) { } else if (strcmp(command, "list") == 0) {
@ -641,7 +642,7 @@ CON_COMMAND(meta, "Metamod:Source Menu")
CONMSG(" list - List plugins\n"); CONMSG(" list - List plugins\n");
CONMSG(" load - Load a plugin\n"); CONMSG(" load - Load a plugin\n");
CONMSG(" pause - Pause a running 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(" retry - Attempt to reload a plugin\n");
CONMSG(" unload - Unload a loaded plugin\n"); CONMSG(" unload - Unload a loaded plugin\n");
CONMSG(" unpause - Unpause a paused plugin\n"); CONMSG(" unpause - Unpause a paused plugin\n");

View File

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

View File

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

View File

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

View File

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

View File

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