darope@0: // vi: set ts=4 sw=4 : darope@0: // vim: set tw=75 : darope@0: darope@0: // mlist.cpp - functions for list of plugins (class MPluginList) darope@0: darope@0: /* darope@0: * Copyright (c) 2001-2003 Will Day darope@0: * darope@0: * This file is part of Metamod. darope@0: * darope@0: * Metamod is free software; you can redistribute it and/or modify it darope@0: * under the terms of the GNU General Public License as published by the darope@0: * Free Software Foundation; either version 2 of the License, or (at darope@0: * your option) any later version. darope@0: * darope@0: * Metamod is distributed in the hope that it will be useful, but darope@0: * WITHOUT ANY WARRANTY; without even the implied warranty of darope@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU darope@0: * General Public License for more details. darope@0: * darope@0: * You should have received a copy of the GNU General Public License darope@0: * along with Metamod; if not, write to the Free Software Foundation, darope@0: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA darope@0: * darope@0: * In addition, as a special exception, the author gives permission to darope@0: * link the code of this program with the Half-Life Game Engine ("HL darope@0: * Engine") and Modified Game Libraries ("MODs") developed by Valve, darope@0: * L.L.C ("Valve"). You must obey the GNU General Public License in all darope@0: * respects for all of the code used other than the HL Engine and MODs darope@0: * from Valve. If you modify this file, you may extend this exception darope@0: * to your version of the file, but you are not obligated to do so. If darope@0: * you do not wish to do so, delete this exception statement from your darope@0: * version. darope@0: * darope@0: */ darope@0: darope@0: #include // errno, etc darope@0: darope@0: #include // always darope@0: darope@0: #include "mlist.h" // me darope@0: #include "mplugin.h" // class MPlugin darope@0: #include "plinfo.h" // plid_t, etc darope@0: #include "commands_meta.h" // cmd_meta_pluginlist, etc darope@0: #include "metamod.h" // GameDLL, etc darope@0: #include "types_meta.h" // mBOOL darope@0: #include "log_meta.h" // META_LOG, etc darope@0: #include "osdep.h" // win32 snprintf, normalize_pathname, darope@0: darope@0: // Constructor darope@0: MPluginList::MPluginList(const char *ifile) darope@0: : size(MAX_PLUGINS), endlist(0) darope@0: { darope@0: int i; darope@0: // store filename of ini file darope@0: STRNCPY(inifile, ifile, sizeof(inifile)); darope@0: // initialize array darope@0: memset(plist, 0, sizeof(plist)); darope@0: for(i=0; i< size; i++) { darope@0: plist[i].index=i+1; // 1-based darope@0: } darope@0: endlist=0; darope@0: } darope@0: bailopan@130: // Find a plugin based on the plugin handle. bailopan@130: // meta_errno values: bailopan@130: // - ME_ARGUMENT invalid pindex bailopan@130: // - ME_NOTFOUND couldn't find a matching plugin bailopan@130: MPlugin *MPluginList::find(DLHANDLE handle) bailopan@130: { bailopan@130: int i; bailopan@130: bailopan@130: if(!handle) bailopan@130: RETURN_ERRNO(NULL, ME_ARGUMENT); bailopan@130: for(i=0; i < endlist; i++) { bailopan@130: if(plist[i].status < PL_VALID) bailopan@130: continue; bailopan@130: if(plist[i].handle == handle) bailopan@130: return(&plist[i]); bailopan@130: } bailopan@130: RETURN_ERRNO(NULL, ME_NOTFOUND); bailopan@130: } bailopan@130: darope@0: // Find a plugin based on the plugin index #. darope@0: // meta_errno values: darope@0: // - ME_ARGUMENT invalid pindex darope@0: // - ME_NOTFOUND couldn't find a matching plugin darope@0: MPlugin *MPluginList::find(int pindex) { darope@0: MPlugin *pfound; darope@0: if(pindex <= 0) darope@0: RETURN_ERRNO(NULL, ME_ARGUMENT); darope@0: pfound=&plist[pindex-1]; darope@0: if(pfound->status < PL_VALID) darope@0: RETURN_ERRNO(NULL, ME_NOTFOUND); darope@0: else darope@0: return(pfound); darope@0: } darope@0: darope@0: // Find a plugin with the given plid. darope@0: // meta_errno values: darope@0: // - ME_ARGUMENT null plid_t darope@0: // - ME_NOTFOUND couldn't find a matching plugin darope@0: MPlugin *MPluginList::find(plid_t id) { darope@0: int i; darope@0: darope@0: if(!id) darope@0: RETURN_ERRNO(NULL, ME_ARGUMENT); darope@0: for(i=0; i < endlist; i++) { darope@0: if(plist[i].status < PL_VALID) darope@0: continue; darope@0: if(plist[i].info == id) darope@0: return(&plist[i]); darope@0: } darope@0: RETURN_ERRNO(NULL, ME_NOTFOUND); darope@0: } darope@0: darope@0: // Find a plugin with the given pathname. darope@0: // meta_errno values: darope@0: // - ME_ARGUMENT null path darope@0: // - ME_NOTFOUND couldn't find a matching plugin darope@0: MPlugin *MPluginList::find(const char *findpath) { darope@0: int i; darope@0: darope@0: if(!findpath) darope@0: RETURN_ERRNO(NULL, ME_ARGUMENT); darope@0: META_DEBUG(8, ("Looking for loaded plugin with dlfnamepath: %s", findpath)); darope@0: for(i=0; i < endlist; i++) { darope@0: META_DEBUG(9, ("Looking at: plugin %s loadedpath: %s", plist[i].file, plist[i].pathname)); darope@0: if(plist[i].status < PL_VALID) darope@0: continue; darope@0: if(strmatch(plist[i].pathname, findpath)) { darope@0: META_DEBUG(8, ("Found loaded plugin %s", plist[i].file)); darope@0: return(&plist[i]); darope@0: } darope@0: } darope@0: META_DEBUG(8, ("No loaded plugin found with path: %s", findpath)); darope@0: RETURN_ERRNO(NULL, ME_NOTFOUND); darope@0: } darope@0: darope@0: // Find a plugin that uses the given memory location. darope@0: // meta_errno values: darope@0: // - ME_ARGUMENT null memptr darope@0: // - ME_NOTFOUND couldn't find a matching plugin darope@0: // - errno's from DLFNAME() darope@0: MPlugin *MPluginList::find_memloc(void *memptr) { darope@0: const char *dlfile; darope@0: darope@0: if(!memptr) darope@0: RETURN_ERRNO(NULL, ME_ARGUMENT); darope@0: if(!(dlfile=DLFNAME(memptr))) { darope@0: META_DEBUG(8, ("DLFNAME failed to find memloc %d", memptr)); darope@0: // meta_errno should be already set in DLFNAME darope@0: return(NULL); darope@0: } darope@0: return(find(dlfile)); darope@0: } darope@0: darope@0: // Find a plugin with non-ambiguous prefix string matching desc, file, darope@0: // name, or logtag. darope@0: // meta_errno values: darope@0: // - ME_ARGUMENT null prefix darope@0: // - ME_NOTFOUND couldn't find a matching plugin darope@0: // - ME_NOTUNIQ found multiple matches; no unique match darope@0: MPlugin *MPluginList::find_match(const char *prefix) { darope@0: int i, len; darope@0: MPlugin *iplug, *pfound; darope@0: char buf[NAME_MAX]; darope@0: darope@0: if(!prefix) darope@0: RETURN_ERRNO(NULL, ME_ARGUMENT); darope@0: pfound=NULL; darope@0: len=strlen(prefix); darope@0: snprintf(buf, sizeof(buf), "mm_%s", prefix); darope@0: for(i=0; i < endlist; i++) { darope@0: iplug=&plist[i]; darope@0: if(iplug->status < PL_VALID) darope@0: continue; darope@0: if(iplug->info && strncasecmp(iplug->info->name, prefix, len) == 0) { darope@0: if(pfound) darope@0: RETURN_ERRNO(NULL, ME_NOTUNIQ); darope@0: pfound=iplug; darope@0: continue; darope@0: } darope@0: else if(strncasecmp(iplug->desc, prefix, len) == 0) { darope@0: if(pfound) darope@0: RETURN_ERRNO(NULL, ME_NOTUNIQ); darope@0: pfound=iplug; darope@0: continue; darope@0: } darope@0: else if(strncasecmp(iplug->file, prefix, len) == 0) { darope@0: if(pfound) darope@0: RETURN_ERRNO(NULL, ME_NOTUNIQ); darope@0: pfound=iplug; darope@0: continue; darope@0: } darope@0: else if(strncasecmp(iplug->file, buf, strlen(buf)) == 0) { darope@0: if(pfound) darope@0: RETURN_ERRNO(NULL, ME_NOTUNIQ); darope@0: pfound=iplug; darope@0: continue; darope@0: } darope@0: else if(iplug->info darope@0: && strncasecmp(iplug->info->logtag, prefix, len) == 0) darope@0: { darope@0: if(pfound) darope@0: RETURN_ERRNO(NULL, ME_NOTUNIQ); darope@0: pfound=iplug; darope@0: continue; darope@0: } darope@0: } darope@0: if(pfound) darope@0: return(pfound); darope@0: else darope@0: RETURN_ERRNO(NULL, ME_NOTFOUND); darope@0: } darope@0: darope@91: darope@91: // Find a plugin with same file, logtag, desc or significant darope@91: // prefix of file. Uses the platform_match() method of MPlugin. darope@91: // meta_errno values: darope@91: // - ME_ARGUMENT null prefix darope@91: // - ME_NOTFOUND couldn't find a matching plugin darope@91: MPlugin *MPluginList::find_match(MPlugin *pmatch) { darope@91: int i; darope@91: MPlugin *iplug, *pfound; darope@91: if(!pmatch) darope@91: RETURN_ERRNO(NULL, ME_ARGUMENT); darope@91: pfound=NULL; darope@91: for(i=0; i < endlist; i++) { darope@91: iplug=&plist[i]; darope@91: if(pmatch->platform_match(iplug)) { darope@91: pfound=iplug; darope@91: break; darope@91: } darope@91: } darope@91: darope@91: if(pfound) darope@91: return(pfound); darope@91: else darope@91: RETURN_ERRNO(NULL, ME_NOTFOUND); darope@91: } darope@91: bailopan@130: MPlugin * MPluginList::plugin_addload(plid_t plid, const char *fname, PLUG_LOADTIME now) bailopan@130: { bailopan@130: MPlugin pl_temp; bailopan@130: MPlugin *pl_found, *pl_added, *pl_loader; bailopan@130: bailopan@130: if ( !(pl_loader=find(plid)) ) bailopan@130: { bailopan@130: META_DEBUG(1, ("Couldn't find plugin that gave this loading request!")); bailopan@130: RETURN_ERRNO(NULL, ME_BADREQ); bailopan@130: } bailopan@130: bailopan@130: memset(&pl_temp, 0, sizeof(pl_temp)); bailopan@130: bailopan@130: if (!pl_temp.plugin_parseline(fname, pl_loader->index)) bailopan@130: { bailopan@130: RETURN_ERRNO(NULL, ME_NOTFOUND); bailopan@130: } bailopan@130: bailopan@130: if (pl_temp.resolve() != mTRUE) bailopan@130: { bailopan@130: META_DEBUG(1, ("Couldn't resolve given path into a file: %s", pl_temp.file)); bailopan@130: RETURN_ERRNO(NULL, ME_NOTFOUND); bailopan@130: } bailopan@130: bailopan@130: if ( (pl_found=find(pl_temp.pathname)) ) bailopan@130: { bailopan@130: META_DEBUG(1, ("Plugin '%s' already in current list; file=%s desc='%s'", bailopan@130: pl_temp.file, pl_found->file, pl_found->desc)); bailopan@130: RETURN_ERRNO(NULL, ME_ALREADY); bailopan@130: } bailopan@130: bailopan@130: if (!(pl_added=add(&pl_temp))) bailopan@130: { bailopan@130: META_DEBUG(1, ("Couldn't add plugin '%s' to list; see log", pl_temp.desc)); bailopan@130: return NULL; bailopan@130: } bailopan@130: bailopan@130: pl_added->action = PA_LOAD; bailopan@130: if (!pl_added->load(now)) bailopan@130: { bailopan@130: if (meta_errno==ME_NOTALLOWED || meta_errno==ME_DELAYED) bailopan@130: { bailopan@130: META_DEBUG(1, ("Plugin '%s' couldn't attach; only allowed %s", bailopan@130: pl_added->desc, pl_added->str_loadable(SL_ALLOWED))); bailopan@130: pl_added->clear(); bailopan@130: } else if (pl_added->status == PL_OPENED) { bailopan@130: META_DEBUG(1, ("Opened plugin '%s', but failed to attach; see log", pl_added->desc)); bailopan@130: } else { bailopan@130: META_DEBUG(1, ("Couldn't load plugin '%s'; see log", pl_added->desc)); bailopan@130: } bailopan@130: return NULL; bailopan@130: } bailopan@130: bailopan@130: META_DEBUG(1, ("Loaded plugin '%s' successfully", pl_added->desc)); bailopan@130: meta_errno = ME_NOERROR; bailopan@133: bailopan@130: return pl_added; bailopan@130: } darope@91: darope@0: // Add a plugin to the list. darope@0: // meta_errno values: darope@0: // - ME_MAXREACHED reached max plugins darope@0: MPlugin *MPluginList::add(MPlugin *padd) { darope@0: int i; darope@0: MPlugin *iplug; darope@0: darope@0: // Find either: darope@0: // - a slot in the list that's not being used darope@0: // - the end of the list darope@0: for(i=0; i < endlist && plist[i].status != PL_EMPTY; i++); darope@0: darope@0: // couldn't find a slot to use darope@0: if(i==size) { darope@0: META_ERROR("Couldn't add plugin '%s' to list; reached max plugins (%d)", darope@0: padd->file, i); darope@0: RETURN_ERRNO(NULL, ME_MAXREACHED); darope@0: } darope@0: darope@0: // if we found the end of the list, advance end marker darope@0: if(i==endlist) darope@0: endlist++; darope@0: iplug = &plist[i]; darope@0: darope@0: // copy filename into this free slot darope@0: STRNCPY(iplug->filename, padd->filename, sizeof(iplug->filename)); darope@0: // Copy file offset ptr. darope@0: // Can't just copy ptr, as it points to offset in padd, which will go darope@0: // away; need to point to corresponding offset in iplug. darope@0: iplug->file = iplug->filename + (padd->file - padd->filename); darope@0: // copy description darope@0: STRNCPY(iplug->desc, padd->desc, sizeof(iplug->desc)); darope@0: // copy pathname darope@0: STRNCPY(iplug->pathname, padd->pathname, sizeof(iplug->pathname)); darope@0: // copy source darope@0: iplug->source=padd->source; darope@0: // copy status bailopan@139: iplug->status=padd->status; bailopan@139: //copy other things bailopan@139: iplug->source_plugin_index = padd->source_plugin_index; darope@0: darope@0: return(iplug); darope@0: } darope@0: darope@0: darope@0: // Read plugins.ini at server startup. darope@0: // meta_errno values: darope@0: // - ME_NOFILE ini file missing or empty darope@0: mBOOL MPluginList::ini_startup() { darope@0: FILE *fp; darope@0: char line[MAX_STRBUF_LEN]; darope@0: int n, ln; darope@91: MPlugin *pmatch; darope@0: darope@0: if(!valid_gamedir_file(inifile)) { darope@0: META_ERROR("ini: Metamod plugins file empty or missing: %s", inifile); darope@0: RETURN_ERRNO(mFALSE, ME_NOFILE); darope@0: } darope@0: full_gamedir_path(inifile, inifile); darope@0: darope@0: fp=fopen(inifile, "r"); darope@0: if(!fp) { darope@0: META_ERROR("ini: Unable to open plugins file '%s': %s", inifile, darope@0: strerror(errno)); darope@0: RETURN_ERRNO(mFALSE, ME_NOFILE); darope@0: } darope@0: darope@0: META_LOG("ini: Begin reading plugins list: %s", inifile); darope@0: for(n=0, ln=1; darope@0: !feof(fp) && fgets(line, sizeof(line), fp) && n < size; darope@0: ln++) darope@0: { darope@0: // Remove line terminations. darope@0: char *cp; darope@0: if((cp=strrchr(line, '\r'))) darope@0: *cp='\0'; darope@0: if((cp=strrchr(line, '\n'))) darope@0: *cp='\0'; darope@0: // Parse directly into next entry in array darope@0: if(!plist[n].ini_parseline(line)) { darope@0: if(meta_errno==ME_FORMAT) darope@0: META_ERROR("ini: Skipping malformed line %d of %s", ln, darope@0: inifile); darope@0: continue; darope@0: } darope@0: // Check for a duplicate - an existing entry with this pathname. darope@0: if(find(plist[n].pathname)) { darope@91: // Should we check platform specific level here? darope@98: META_INFO("ini: Skipping duplicate plugin, line %d of %s: %s", darope@0: ln, inifile, plist[n].pathname); darope@0: continue; darope@0: } darope@91: // Check for a matching platform with different platform specifics darope@91: // level. darope@91: if(NULL != (pmatch=find_match(&plist[n]))) { darope@91: if(pmatch->pfspecific >= plist[n].pfspecific) { darope@91: META_DEBUG(1, ("ini: Skipping plugin, line %d of %s: plugin with higher platform specific level already exists. (%d >= %d)", darope@98: ln, inifile, pmatch->pfspecific, plist[n].pfspecific)); darope@91: continue; darope@91: } darope@91: META_DEBUG(1, ("ini: Plugin in line %d overrides existing plugin with lower platform specific level %d, ours %d", darope@98: ln, pmatch->pfspecific, plist[n].pfspecific)); bailopan@128: int _index = pmatch->index; darope@91: memset(pmatch, 0, sizeof(MPlugin)); bailopan@128: pmatch->index = _index; darope@91: } darope@0: plist[n].action=PA_LOAD; darope@0: META_LOG("ini: Read plugin config for: %s", plist[n].desc); darope@0: n++; darope@0: endlist=n; // mark end of list darope@0: } darope@0: META_LOG("ini: Finished reading plugins list: %s; Found %d plugins to load", darope@0: inifile, n); darope@0: darope@0: fclose(fp); darope@0: if(!n) { darope@0: META_ERROR("ini: Warning; no plugins found to load?"); darope@0: } darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Re-read plugins.ini looking for added/deleted/changed plugins. darope@0: // meta_errno values: darope@0: // - ME_NOFILE ini file missing or empty darope@0: mBOOL MPluginList::ini_refresh() { darope@0: FILE *fp; darope@0: char line[MAX_STRBUF_LEN]; darope@0: int n, ln; darope@0: MPlugin pl_temp; darope@0: MPlugin *pl_found, *pl_added; darope@0: darope@0: fp=fopen(inifile, "r"); darope@0: if(!fp) { darope@0: META_ERROR("ini: Unable to open plugins file '%s': %s", inifile, darope@0: strerror(errno)); darope@0: RETURN_ERRNO(mFALSE, ME_NOFILE); darope@0: } darope@0: darope@0: META_LOG("ini: Begin re-reading plugins list: %s", inifile); darope@0: for(n=0, ln=1; darope@0: !feof(fp) && fgets(line, sizeof(line), fp) && n < size; darope@0: ln++) darope@0: { darope@0: // Remove line terminations. darope@0: char *cp; darope@0: if((cp=strrchr(line, '\r'))) darope@0: *cp='\0'; darope@0: if((cp=strrchr(line, '\n'))) darope@0: *cp='\0'; darope@0: // Parse into a temp plugin darope@0: memset(&pl_temp, 0, sizeof(pl_temp)); darope@0: if(!pl_temp.ini_parseline(line)) { darope@0: if(meta_errno==ME_FORMAT) darope@0: META_ERROR("ini: Skipping malformed line %d of %s", darope@0: ln, inifile); darope@0: continue; darope@0: } darope@0: // Try to find plugin with this pathname in the current list of darope@0: // plugins. darope@0: if(!(pl_found=find(pl_temp.pathname))) { darope@93: // Check for a matching platform with higher platform specifics darope@93: // level. darope@93: if(NULL != (pl_found=find_match(&pl_temp))) { darope@93: if(pl_found->pfspecific >= pl_temp.pfspecific) { darope@93: META_DEBUG(1, ("ini: Skipping plugin, line %d of %s: plugin with higher platform specific level already exists. (%d >= %d)", darope@93: ln, inifile, pl_found->pfspecific, pl_temp.pfspecific)); darope@93: continue; darope@93: } darope@98: if(PA_LOAD == pl_found->action) { darope@98: META_DEBUG(1, ("ini: Plugin in line %d overrides loading of plugin with lower platform specific level %d, ours %d", darope@98: ln, pl_found->pfspecific, pl_temp.pfspecific)); bailopan@128: int _index = pl_found->index; darope@98: memset(pl_found, 0, sizeof(MPlugin)); bailopan@128: pl_found->index = _index; darope@98: } darope@98: else { darope@98: META_DEBUG(1, ("ini: Plugin in line %d should override existing plugin with lower platform specific level %d, ours %d. Unable to comply.", darope@98: ln, pl_found->pfspecific, pl_temp.pfspecific)); darope@98: continue; darope@98: } darope@93: } darope@0: // new plugin; add to list darope@0: if((pl_added=add(&pl_temp))) { darope@0: // try to load this plugin at the next opportunity darope@0: pl_added->action=PA_LOAD; darope@0: } darope@0: else darope@0: // error details logged in add() darope@0: continue; darope@0: } darope@0: else { darope@0: // This plugin is already in the current list of plugins. darope@0: // Pathname already matches. Recopy desc, if specified in darope@0: // plugins.ini. darope@0: if(pl_temp.desc[0] != '<') darope@0: STRNCPY(pl_found->desc, pl_temp.desc, sizeof(pl_found->desc)); darope@0: darope@0: // Check the file to see if it looks like it's been modified darope@0: // since we last loaded it. darope@0: if(!pl_found->newer_file()) { darope@0: if(meta_errno==ME_NOFILE) { darope@0: META_ERROR("ini: Skipping plugin, couldn't stat file '%s': %s", darope@0: pl_found->pathname, strerror(errno)); darope@0: continue; darope@0: } darope@0: else { darope@0: // File hasn't been updated. darope@0: // Keep plugin (don't let refresh() unload it). darope@0: pl_found->action=PA_KEEP; darope@0: } darope@0: } darope@0: // Newer file on disk. darope@0: else if(pl_found->status >= PL_OPENED) { darope@0: META_DEBUG(2, ("ini: Plugin '%s' has newer file on disk", pl_found->desc)); darope@0: pl_found->action=PA_RELOAD; darope@0: } darope@0: else darope@0: META_ERROR("ini: Plugin '%s' has newer file, but unexpected status (%s)", darope@0: pl_found->desc, pl_found->str_status()); darope@0: } darope@98: if(NULL != pl_found) { darope@98: META_LOG("ini: Read plugin config for: %s", pl_found->desc); darope@98: } darope@98: else { darope@98: META_LOG("ini: Read plugin config for: %s", pl_temp.desc); darope@98: } darope@0: n++; darope@0: } darope@0: META_LOG("ini: Finished reading plugins list: %s; Found %d plugins", inifile, n); darope@0: darope@0: fclose(fp); darope@0: if(!n) { darope@0: META_ERROR("ini: Warning; no plugins found to load?"); darope@0: } darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Load a plugin from a console command. darope@0: // meta_errno values: darope@0: // - errno's from cmd_parseline() darope@0: // - errno's from resolve() darope@0: // - ME_ALREADY this plugin already loaded darope@0: // - errno's from add() darope@0: // - errno's from load() darope@0: mBOOL MPluginList::cmd_addload(const char *args) { darope@0: MPlugin pl_temp; darope@0: MPlugin *pl_found, *pl_added; darope@0: darope@0: // XXX move back to comands_meta ? darope@0: darope@0: // parse into a temp plugin darope@0: memset(&pl_temp, 0, sizeof(pl_temp)); darope@0: if(pl_temp.cmd_parseline(args) != mTRUE) { darope@0: META_CONS("Couldn't parse 'meta load' arguments: %s", args); darope@0: // meta_errno should be already set in cmd_parseline() darope@0: return(mFALSE); darope@0: } darope@0: darope@0: // resolve given path into a file; accepts various "shortcut" darope@0: // pathnames. darope@0: if(pl_temp.resolve() != mTRUE) { darope@0: // Couldn't find a matching file on disk darope@0: META_CONS("Couldn't resolve given path into a file: %s", darope@0: pl_temp.file); darope@0: // meta_errno should be already set in resolve() darope@0: return(mFALSE); darope@0: } darope@0: darope@0: // Try to find plugin with this pathname in the current list of darope@0: // plugins. darope@0: if((pl_found=find(pl_temp.pathname))) { darope@0: // Already in list darope@0: META_CONS("Plugin '%s' already in current list; file=%s desc='%s'", darope@0: pl_temp.file, pl_found->file, pl_found->desc); darope@0: RETURN_ERRNO(mFALSE, ME_ALREADY); darope@0: } darope@0: // new plugin; add to list darope@0: if(!(pl_added=add(&pl_temp))) { darope@0: META_CONS("Couldn't add plugin '%s' to list; see log", pl_temp.desc); darope@0: // meta_errno should be already set in add() darope@0: return(mFALSE); darope@0: } darope@0: darope@0: // try to load new plugin darope@0: pl_added->action=PA_LOAD; darope@0: if(!pl_added->load(PT_ANYTIME)) { darope@0: // load failed darope@0: if(meta_errno==ME_DELAYED) darope@0: META_CONS("Loaded plugin '%s', but will wait to become active, %s", darope@0: pl_added->desc, pl_added->str_loadable(SL_ALLOWED)); darope@0: else if(meta_errno==ME_NOTALLOWED) { darope@0: META_CONS("Plugin '%s' couldn't attach; only allowed %s", darope@0: pl_added->desc, pl_added->str_loadable(SL_ALLOWED)); darope@0: pl_added->clear(); darope@0: } darope@0: else if(pl_added->status == PL_OPENED) darope@0: META_CONS("Opened plugin '%s', but failed to attach; see log", pl_added->desc); darope@0: else darope@0: META_CONS("Couldn't load plugin '%s'; see log", pl_added->desc); bailopan@139: show(0); darope@0: // meta_errno should be already set in load() darope@0: return(mFALSE); darope@0: } darope@0: META_CONS("Loaded plugin '%s' successfully", pl_added->desc); bailopan@139: show(0); darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Load plugins at startup. darope@0: // meta_errno values: darope@0: // - errno's from ini_startup() darope@0: mBOOL MPluginList::load() { darope@0: int i, n; darope@0: darope@0: if (!ini_startup()) { darope@0: META_ERROR("Problem loading plugins.ini: %s", inifile); darope@0: // meta_errno should be already set in ini_startup() darope@0: return(mFALSE); darope@0: } darope@0: darope@0: META_LOG("dll: Loading plugins..."); darope@0: for(i=0, n=0; i < endlist; i++) { darope@0: if(plist[i].status < PL_VALID) darope@0: continue; darope@0: if(plist[i].load(PT_STARTUP) == mTRUE) darope@0: n++; darope@0: else darope@0: // all plugins should be loadable at startup... darope@0: META_ERROR("dll: Failed to load plugin '%s'", plist[i].file); darope@0: } darope@0: META_LOG("dll: Finished loading %d plugins", n); darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Update list of loaded plugins from ini file, and load any new/changed plugins. darope@0: // meta_errno values: darope@0: // - errno's from ini_refresh() darope@0: mBOOL MPluginList::refresh(PLUG_LOADTIME now) { darope@0: int i, ndone=0, nkept=0, nloaded=0, nunloaded=0, nreloaded=0, ndelayed=0; darope@0: MPlugin *iplug; darope@0: darope@0: if (!ini_refresh()) { darope@0: META_ERROR("dll: Problem reloading plugins.ini: %s", inifile); darope@0: // meta_errno should be already set in ini_refresh() darope@0: return(mFALSE); darope@0: } darope@0: darope@0: META_LOG("dll: Updating plugins..."); darope@0: for(i=0; i < endlist; i++) { darope@0: iplug=&plist[i]; darope@0: if(iplug->status < PL_VALID) darope@0: continue; darope@0: switch(iplug->action) { darope@0: case PA_KEEP: darope@0: META_DEBUG(1, ("Keeping plugin '%s'", iplug->desc)); darope@0: iplug->action=PA_NONE; darope@0: nkept++; darope@0: break; darope@0: case PA_LOAD: darope@0: META_DEBUG(1, ("Loading plugin '%s'", iplug->desc)); darope@0: if(iplug->load(now)) darope@0: nloaded++; darope@0: else if(meta_errno==ME_DELAYED) darope@0: ndelayed++; darope@0: break; darope@0: case PA_RELOAD: darope@0: META_DEBUG(1, ("Reloading plugin '%s'", iplug->desc)); darope@0: if(iplug->reload(now, PNL_FILE_NEWER)) darope@0: nreloaded++; darope@0: else if(meta_errno==ME_DELAYED) darope@0: ndelayed++; darope@0: break; darope@0: case PA_NONE: darope@0: // If previously loaded from ini, but apparently removed from new ini. darope@0: if(iplug->source==PS_INI && iplug->status >= PL_RUNNING) { darope@0: META_DEBUG(1, ("Unloading plugin '%s'", iplug->desc)); darope@0: iplug->action=PA_UNLOAD; bailopan@130: if(iplug->unload(now, PNL_INI_DELETED, PNL_INI_DELETED)) darope@0: nunloaded++; darope@0: else if(meta_errno==ME_DELAYED) darope@0: ndelayed++; darope@0: } darope@0: break; darope@0: case PA_ATTACH: darope@0: // Previously requested attach, but was delayed? darope@0: META_DEBUG(1, ("Retrying attach plugin '%s'", iplug->desc)); darope@0: if(iplug->retry(now, PNL_DELAYED)) darope@0: nloaded++; darope@0: else if(meta_errno==ME_DELAYED) darope@0: ndelayed++; darope@0: break; darope@0: case PA_UNLOAD: darope@0: // Previously requested unload, but was delayed? darope@0: META_DEBUG(1, ("Retrying unload plugin '%s'", iplug->desc)); darope@0: if(iplug->retry(now, PNL_DELAYED)) darope@0: nunloaded++; darope@0: else if(meta_errno==ME_DELAYED) darope@0: ndelayed++; darope@0: break; darope@0: case PA_NULL: darope@0: META_ERROR("dll: Unexpected action for plugin '%s': '%s'", iplug->desc, iplug->str_action()); darope@0: break; darope@0: default: darope@0: META_ERROR("dll: Unrecognized action for plugin '%s': '%s'", iplug->desc, iplug->str_action()); darope@0: break; darope@0: } darope@0: ndone++; darope@0: } darope@0: META_LOG("dll: Finished updating %d plugins; kept %d, loaded %d, unloaded %d, reloaded %d, delayed %d", darope@0: ndone, nkept, nloaded, nunloaded, nreloaded, ndelayed); darope@0: return(mTRUE); darope@0: } darope@0: darope@0: // Re-enable any plugins currently paused. darope@0: // meta_errno values: darope@0: // - none darope@0: void MPluginList::unpause_all(void) { darope@0: int i; darope@0: MPlugin *iplug; darope@0: for(i=0; i < endlist; i++) { darope@0: iplug=&plist[i]; darope@0: if(iplug->status==PL_PAUSED) darope@0: iplug->unpause(); darope@0: } darope@0: } darope@0: darope@0: // Retry any pending actions on plugins, for instance load/unload delayed darope@0: // until changelevel. darope@0: // meta_errno values: darope@0: // - none darope@0: void MPluginList::retry_all(PLUG_LOADTIME now) { darope@0: int i; darope@0: MPlugin *iplug; darope@0: for(i=0; i < endlist; i++) { darope@0: iplug=&plist[i]; darope@0: if(iplug->action != PA_NONE) darope@0: iplug->retry(now, PNL_DELAYED); darope@0: } darope@0: } darope@0: darope@0: // List plugins and information about them in a formatted table. darope@0: // meta_errno values: darope@0: // - none bailopan@139: void MPluginList::show(int source_index) { darope@0: int i, n=0, r=0; darope@0: MPlugin *pl; darope@0: char desc[15+1], file[16+1], vers[7+1]; // plus 1 for term null darope@0: bailopan@139: if (source_index <= 0) bailopan@139: META_CONS("Currently loaded plugins:"); bailopan@139: else bailopan@139: META_CONS("Child plugins:"); bailopan@139: darope@0: META_CONS(" %*s %-*s %-4s %-4s %-*s v%-*s %-3s %-5s %-5s", darope@0: WIDTH_MAX_PLUGINS, "", darope@0: sizeof(desc)-1, "description", darope@0: "stat", "pend", darope@0: sizeof(file)-1, "file", sizeof(vers)-1, "ers", darope@0: "src", "load ", "unlod"); darope@0: for(i=0; i < endlist; i++) { darope@0: pl=&plist[i]; darope@0: if(pl->status < PL_VALID) darope@0: continue; bailopan@139: if ((source_index > 0) && (pl->source_plugin_index != source_index)) bailopan@139: continue; darope@0: STRNCPY(desc, pl->desc, sizeof(desc)); darope@0: STRNCPY(file, pl->file, sizeof(file)); darope@0: if(pl->info && pl->info->version) STRNCPY(vers, pl->info->version, sizeof(vers)); darope@0: else STRNCPY(vers, " -", sizeof(vers)); darope@0: META_CONS(" [%*d] %-*s %-4s %-4s %-*s v%-*s %-3s %-5s %-5s", darope@0: WIDTH_MAX_PLUGINS, pl->index, darope@0: sizeof(desc)-1, desc, darope@0: pl->str_status(ST_SHOW), pl->str_action(SA_SHOW), darope@0: sizeof(file)-1, file, sizeof(vers)-1, vers, darope@0: pl->str_source(SO_SHOW), darope@0: pl->str_loadable(SL_SHOW), pl->str_unloadable(SL_SHOW)); darope@0: if(pl->status == PL_RUNNING) darope@0: r++; darope@0: n++; darope@0: } darope@0: META_CONS("%d plugins, %d running", n, r); darope@0: } darope@0: darope@0: // List plugins and information to Player/client entity. Differs from the darope@0: // "meta list" console command in that: darope@0: // - Shows only "running" plugins, skipping any failed or paused plugins. darope@0: // - Limited info about each plugin, mostly the "public" info (name, author, darope@0: // etc). darope@0: // meta_errno values: darope@0: // - none darope@0: void MPluginList::show_client(edict_t *pEntity) { darope@0: int i, n=0; darope@0: MPlugin *pl; darope@0: META_CLIENT(pEntity, "Currently running plugins:"); darope@0: for(i=0; i < endlist; i++) { darope@0: pl=&plist[i]; darope@0: if(pl->status != PL_RUNNING) darope@0: continue; darope@0: n++; darope@0: META_CLIENT(pEntity, " [%3d] %s, v%s, %s, by %s, see %s", darope@0: n, darope@0: pl->info->name ? pl->info->name : "", darope@0: pl->info->version ? pl->info->version : "", darope@0: pl->info->date ? pl->info->date : "<../../..>", darope@0: pl->info->author ? pl->info->author : "", darope@0: pl->info->url ? pl->info->url : ""); darope@0: } darope@0: META_CLIENT(pEntity, "%d plugins", n); darope@0: } bailopan@139: bailopan@139: mBOOL MPluginList::found_child_plugins(int source_index) bailopan@139: { bailopan@139: int i; bailopan@139: bailopan@139: if (source_index <= 0) bailopan@139: return mFALSE; bailopan@139: bailopan@139: for (i=0; i