4 // mlist.cpp - functions for list of plugins (class MPluginList)
9 * This file is part of Metamod.
11 * Metamod is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * Metamod is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Metamod; if not, write to the Free Software Foundation,
23 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * In addition, as a special exception, the author gives permission to
26 * link the code of this program with the Half-Life Game Engine ("HL
27 * Engine") and Modified Game Libraries ("MODs") developed by Valve,
28 * L.L.C ("Valve"). You must obey the GNU General Public License in all
29 * respects for all of the code used other than the HL Engine and MODs
30 * from Valve. If you modify this file, you may extend this exception
31 * to your version of the file, but you are not obligated to do so. If
32 * you do not wish to do so, delete this exception statement from your
37 #include <errno.h> // errno, etc
39 #include <extdll.h> // always
41 #include "mlist.h" // me
42 #include "mplugin.h" // class MPlugin
43 #include "plinfo.h" // plid_t, etc
44 #include "commands_meta.h" // cmd_meta_pluginlist, etc
45 #include "metamod.h" // GameDLL, etc
46 #include "types_meta.h" // mBOOL
47 #include "log_meta.h" // META_LOG, etc
48 #include "osdep.h" // win32 snprintf, normalize_pathname,
51 MPluginList::MPluginList(const char *ifile)
52 : size(MAX_PLUGINS), endlist(0)
55 // store filename of ini file
56 STRNCPY(inifile, ifile, sizeof(inifile));
58 memset(plist, 0, sizeof(plist));
59 for(i=0; i< size; i++) {
60 plist[i].index=i+1; // 1-based
65 // Find a plugin based on the plugin index #.
67 // - ME_ARGUMENT invalid pindex
68 // - ME_NOTFOUND couldn't find a matching plugin
69 MPlugin *MPluginList::find(int pindex) {
72 RETURN_ERRNO(NULL, ME_ARGUMENT);
73 pfound=&plist[pindex-1];
74 if(pfound->status < PL_VALID)
75 RETURN_ERRNO(NULL, ME_NOTFOUND);
80 // Find a plugin with the given plid.
82 // - ME_ARGUMENT null plid_t
83 // - ME_NOTFOUND couldn't find a matching plugin
84 MPlugin *MPluginList::find(plid_t id) {
88 RETURN_ERRNO(NULL, ME_ARGUMENT);
89 for(i=0; i < endlist; i++) {
90 if(plist[i].status < PL_VALID)
92 if(plist[i].info == id)
95 RETURN_ERRNO(NULL, ME_NOTFOUND);
98 // Find a plugin with the given pathname.
100 // - ME_ARGUMENT null path
101 // - ME_NOTFOUND couldn't find a matching plugin
102 MPlugin *MPluginList::find(const char *findpath) {
106 RETURN_ERRNO(NULL, ME_ARGUMENT);
107 META_DEBUG(8, ("Looking for loaded plugin with dlfnamepath: %s", findpath));
108 for(i=0; i < endlist; i++) {
109 META_DEBUG(9, ("Looking at: plugin %s loadedpath: %s", plist[i].file, plist[i].pathname));
110 if(plist[i].status < PL_VALID)
112 if(strmatch(plist[i].pathname, findpath)) {
113 META_DEBUG(8, ("Found loaded plugin %s", plist[i].file));
117 META_DEBUG(8, ("No loaded plugin found with path: %s", findpath));
118 RETURN_ERRNO(NULL, ME_NOTFOUND);
121 // Find a plugin that uses the given memory location.
122 // meta_errno values:
123 // - ME_ARGUMENT null memptr
124 // - ME_NOTFOUND couldn't find a matching plugin
125 // - errno's from DLFNAME()
126 MPlugin *MPluginList::find_memloc(void *memptr) {
130 RETURN_ERRNO(NULL, ME_ARGUMENT);
131 if(!(dlfile=DLFNAME(memptr))) {
132 META_DEBUG(8, ("DLFNAME failed to find memloc %d", memptr));
133 // meta_errno should be already set in DLFNAME
136 return(find(dlfile));
139 // Find a plugin with non-ambiguous prefix string matching desc, file,
141 // meta_errno values:
142 // - ME_ARGUMENT null prefix
143 // - ME_NOTFOUND couldn't find a matching plugin
144 // - ME_NOTUNIQ found multiple matches; no unique match
145 MPlugin *MPluginList::find_match(const char *prefix) {
147 MPlugin *iplug, *pfound;
151 RETURN_ERRNO(NULL, ME_ARGUMENT);
154 snprintf(buf, sizeof(buf), "mm_%s", prefix);
155 for(i=0; i < endlist; i++) {
157 if(iplug->status < PL_VALID)
159 if(iplug->info && strncasecmp(iplug->info->name, prefix, len) == 0) {
161 RETURN_ERRNO(NULL, ME_NOTUNIQ);
165 else if(strncasecmp(iplug->desc, prefix, len) == 0) {
167 RETURN_ERRNO(NULL, ME_NOTUNIQ);
171 else if(strncasecmp(iplug->file, prefix, len) == 0) {
173 RETURN_ERRNO(NULL, ME_NOTUNIQ);
177 else if(strncasecmp(iplug->file, buf, strlen(buf)) == 0) {
179 RETURN_ERRNO(NULL, ME_NOTUNIQ);
184 && strncasecmp(iplug->info->logtag, prefix, len) == 0)
187 RETURN_ERRNO(NULL, ME_NOTUNIQ);
195 RETURN_ERRNO(NULL, ME_NOTFOUND);
199 // Find a plugin with same file, logtag, desc or significant
200 // prefix of file. Uses the platform_match() method of MPlugin.
201 // meta_errno values:
202 // - ME_ARGUMENT null prefix
203 // - ME_NOTFOUND couldn't find a matching plugin
204 MPlugin *MPluginList::find_match(MPlugin *pmatch) {
206 MPlugin *iplug, *pfound;
208 RETURN_ERRNO(NULL, ME_ARGUMENT);
210 for(i=0; i < endlist; i++) {
212 if(pmatch->platform_match(iplug)) {
221 RETURN_ERRNO(NULL, ME_NOTFOUND);
225 // Add a plugin to the list.
226 // meta_errno values:
227 // - ME_MAXREACHED reached max plugins
228 MPlugin *MPluginList::add(MPlugin *padd) {
233 // - a slot in the list that's not being used
234 // - the end of the list
235 for(i=0; i < endlist && plist[i].status != PL_EMPTY; i++);
237 // couldn't find a slot to use
239 META_ERROR("Couldn't add plugin '%s' to list; reached max plugins (%d)",
241 RETURN_ERRNO(NULL, ME_MAXREACHED);
244 // if we found the end of the list, advance end marker
249 // copy filename into this free slot
250 STRNCPY(iplug->filename, padd->filename, sizeof(iplug->filename));
251 // Copy file offset ptr.
252 // Can't just copy ptr, as it points to offset in padd, which will go
253 // away; need to point to corresponding offset in iplug.
254 iplug->file = iplug->filename + (padd->file - padd->filename);
256 STRNCPY(iplug->desc, padd->desc, sizeof(iplug->desc));
258 STRNCPY(iplug->pathname, padd->pathname, sizeof(iplug->pathname));
260 iplug->source=padd->source;
262 iplug->status=padd->status;
268 // Read plugins.ini at server startup.
269 // meta_errno values:
270 // - ME_NOFILE ini file missing or empty
271 mBOOL MPluginList::ini_startup() {
273 char line[MAX_STRBUF_LEN];
277 if(!valid_gamedir_file(inifile)) {
278 META_ERROR("ini: Metamod plugins file empty or missing: %s", inifile);
279 RETURN_ERRNO(mFALSE, ME_NOFILE);
281 full_gamedir_path(inifile, inifile);
283 fp=fopen(inifile, "r");
285 META_ERROR("ini: Unable to open plugins file '%s': %s", inifile,
287 RETURN_ERRNO(mFALSE, ME_NOFILE);
290 META_LOG("ini: Begin reading plugins list: %s", inifile);
292 !feof(fp) && fgets(line, sizeof(line), fp) && n < size;
295 // Remove line terminations.
297 if((cp=strrchr(line, '\r')))
299 if((cp=strrchr(line, '\n')))
301 // Parse directly into next entry in array
302 if(!plist[n].ini_parseline(line)) {
303 if(meta_errno==ME_FORMAT)
304 META_ERROR("ini: Skipping malformed line %d of %s", ln,
308 // Check for a duplicate - an existing entry with this pathname.
309 if(find(plist[n].pathname)) {
310 // Should we check platform specific level here?
311 META_INFO("ini: Skipping duplicate plugin, line %d of %s: %s",
312 ln, inifile, plist[n].pathname);
315 // Check for a matching platform with different platform specifics
317 if(NULL != (pmatch=find_match(&plist[n]))) {
318 if(pmatch->pfspecific >= plist[n].pfspecific) {
319 META_DEBUG(1, ("ini: Skipping plugin, line %d of %s: plugin with higher platform specific level already exists. (%d >= %d)",
320 ln, inifile, pmatch->pfspecific, plist[n].pfspecific));
323 META_DEBUG(1, ("ini: Plugin in line %d overrides existing plugin with lower platform specific level %d, ours %d",
324 ln, pmatch->pfspecific, plist[n].pfspecific));
325 memset(pmatch, 0, sizeof(MPlugin));
327 plist[n].action=PA_LOAD;
328 META_LOG("ini: Read plugin config for: %s", plist[n].desc);
330 endlist=n; // mark end of list
332 META_LOG("ini: Finished reading plugins list: %s; Found %d plugins to load",
337 META_ERROR("ini: Warning; no plugins found to load?");
342 // Re-read plugins.ini looking for added/deleted/changed plugins.
343 // meta_errno values:
344 // - ME_NOFILE ini file missing or empty
345 mBOOL MPluginList::ini_refresh() {
347 char line[MAX_STRBUF_LEN];
350 MPlugin *pl_found, *pl_added;
352 fp=fopen(inifile, "r");
354 META_ERROR("ini: Unable to open plugins file '%s': %s", inifile,
356 RETURN_ERRNO(mFALSE, ME_NOFILE);
359 META_LOG("ini: Begin re-reading plugins list: %s", inifile);
361 !feof(fp) && fgets(line, sizeof(line), fp) && n < size;
364 // Remove line terminations.
366 if((cp=strrchr(line, '\r')))
368 if((cp=strrchr(line, '\n')))
370 // Parse into a temp plugin
371 memset(&pl_temp, 0, sizeof(pl_temp));
372 if(!pl_temp.ini_parseline(line)) {
373 if(meta_errno==ME_FORMAT)
374 META_ERROR("ini: Skipping malformed line %d of %s",
378 // Try to find plugin with this pathname in the current list of
380 if(!(pl_found=find(pl_temp.pathname))) {
381 // Check for a matching platform with higher platform specifics
383 if(NULL != (pl_found=find_match(&pl_temp))) {
384 if(pl_found->pfspecific >= pl_temp.pfspecific) {
385 META_DEBUG(1, ("ini: Skipping plugin, line %d of %s: plugin with higher platform specific level already exists. (%d >= %d)",
386 ln, inifile, pl_found->pfspecific, pl_temp.pfspecific));
389 if(PA_LOAD == pl_found->action) {
390 META_DEBUG(1, ("ini: Plugin in line %d overrides loading of plugin with lower platform specific level %d, ours %d",
391 ln, pl_found->pfspecific, pl_temp.pfspecific));
392 memset(pl_found, 0, sizeof(MPlugin));
395 META_DEBUG(1, ("ini: Plugin in line %d should override existing plugin with lower platform specific level %d, ours %d. Unable to comply.",
396 ln, pl_found->pfspecific, pl_temp.pfspecific));
400 // new plugin; add to list
401 if((pl_added=add(&pl_temp))) {
402 // try to load this plugin at the next opportunity
403 pl_added->action=PA_LOAD;
406 // error details logged in add()
410 // This plugin is already in the current list of plugins.
411 // Pathname already matches. Recopy desc, if specified in
413 if(pl_temp.desc[0] != '<')
414 STRNCPY(pl_found->desc, pl_temp.desc, sizeof(pl_found->desc));
416 // Check the file to see if it looks like it's been modified
417 // since we last loaded it.
418 if(!pl_found->newer_file()) {
419 if(meta_errno==ME_NOFILE) {
420 META_ERROR("ini: Skipping plugin, couldn't stat file '%s': %s",
421 pl_found->pathname, strerror(errno));
425 // File hasn't been updated.
426 // Keep plugin (don't let refresh() unload it).
427 pl_found->action=PA_KEEP;
430 // Newer file on disk.
431 else if(pl_found->status >= PL_OPENED) {
432 META_DEBUG(2, ("ini: Plugin '%s' has newer file on disk", pl_found->desc));
433 pl_found->action=PA_RELOAD;
436 META_ERROR("ini: Plugin '%s' has newer file, but unexpected status (%s)",
437 pl_found->desc, pl_found->str_status());
439 if(NULL != pl_found) {
440 META_LOG("ini: Read plugin config for: %s", pl_found->desc);
443 META_LOG("ini: Read plugin config for: %s", pl_temp.desc);
447 META_LOG("ini: Finished reading plugins list: %s; Found %d plugins", inifile, n);
451 META_ERROR("ini: Warning; no plugins found to load?");
456 // Load a plugin from a console command.
457 // meta_errno values:
458 // - errno's from cmd_parseline()
459 // - errno's from resolve()
460 // - ME_ALREADY this plugin already loaded
461 // - errno's from add()
462 // - errno's from load()
463 mBOOL MPluginList::cmd_addload(const char *args) {
465 MPlugin *pl_found, *pl_added;
467 // XXX move back to comands_meta ?
469 // parse into a temp plugin
470 memset(&pl_temp, 0, sizeof(pl_temp));
471 if(pl_temp.cmd_parseline(args) != mTRUE) {
472 META_CONS("Couldn't parse 'meta load' arguments: %s", args);
473 // meta_errno should be already set in cmd_parseline()
477 // resolve given path into a file; accepts various "shortcut"
479 if(pl_temp.resolve() != mTRUE) {
480 // Couldn't find a matching file on disk
481 META_CONS("Couldn't resolve given path into a file: %s",
483 // meta_errno should be already set in resolve()
487 // Try to find plugin with this pathname in the current list of
489 if((pl_found=find(pl_temp.pathname))) {
491 META_CONS("Plugin '%s' already in current list; file=%s desc='%s'",
492 pl_temp.file, pl_found->file, pl_found->desc);
493 RETURN_ERRNO(mFALSE, ME_ALREADY);
495 // new plugin; add to list
496 if(!(pl_added=add(&pl_temp))) {
497 META_CONS("Couldn't add plugin '%s' to list; see log", pl_temp.desc);
498 // meta_errno should be already set in add()
502 // try to load new plugin
503 pl_added->action=PA_LOAD;
504 if(!pl_added->load(PT_ANYTIME)) {
506 if(meta_errno==ME_DELAYED)
507 META_CONS("Loaded plugin '%s', but will wait to become active, %s",
508 pl_added->desc, pl_added->str_loadable(SL_ALLOWED));
509 else if(meta_errno==ME_NOTALLOWED) {
510 META_CONS("Plugin '%s' couldn't attach; only allowed %s",
511 pl_added->desc, pl_added->str_loadable(SL_ALLOWED));
514 else if(pl_added->status == PL_OPENED)
515 META_CONS("Opened plugin '%s', but failed to attach; see log", pl_added->desc);
517 META_CONS("Couldn't load plugin '%s'; see log", pl_added->desc);
519 // meta_errno should be already set in load()
522 META_CONS("Loaded plugin '%s' successfully", pl_added->desc);
527 // Load plugins at startup.
528 // meta_errno values:
529 // - errno's from ini_startup()
530 mBOOL MPluginList::load() {
533 if (!ini_startup()) {
534 META_ERROR("Problem loading plugins.ini: %s", inifile);
535 // meta_errno should be already set in ini_startup()
539 META_LOG("dll: Loading plugins...");
540 for(i=0, n=0; i < endlist; i++) {
541 if(plist[i].status < PL_VALID)
543 if(plist[i].load(PT_STARTUP) == mTRUE)
546 // all plugins should be loadable at startup...
547 META_ERROR("dll: Failed to load plugin '%s'", plist[i].file);
549 META_LOG("dll: Finished loading %d plugins", n);
553 // Update list of loaded plugins from ini file, and load any new/changed plugins.
554 // meta_errno values:
555 // - errno's from ini_refresh()
556 mBOOL MPluginList::refresh(PLUG_LOADTIME now) {
557 int i, ndone=0, nkept=0, nloaded=0, nunloaded=0, nreloaded=0, ndelayed=0;
560 if (!ini_refresh()) {
561 META_ERROR("dll: Problem reloading plugins.ini: %s", inifile);
562 // meta_errno should be already set in ini_refresh()
566 META_LOG("dll: Updating plugins...");
567 for(i=0; i < endlist; i++) {
569 if(iplug->status < PL_VALID)
571 switch(iplug->action) {
573 META_DEBUG(1, ("Keeping plugin '%s'", iplug->desc));
574 iplug->action=PA_NONE;
578 META_DEBUG(1, ("Loading plugin '%s'", iplug->desc));
581 else if(meta_errno==ME_DELAYED)
585 META_DEBUG(1, ("Reloading plugin '%s'", iplug->desc));
586 if(iplug->reload(now, PNL_FILE_NEWER))
588 else if(meta_errno==ME_DELAYED)
592 // If previously loaded from ini, but apparently removed from new ini.
593 if(iplug->source==PS_INI && iplug->status >= PL_RUNNING) {
594 META_DEBUG(1, ("Unloading plugin '%s'", iplug->desc));
595 iplug->action=PA_UNLOAD;
596 if(iplug->unload(now, PNL_INI_DELETED))
598 else if(meta_errno==ME_DELAYED)
603 // Previously requested attach, but was delayed?
604 META_DEBUG(1, ("Retrying attach plugin '%s'", iplug->desc));
605 if(iplug->retry(now, PNL_DELAYED))
607 else if(meta_errno==ME_DELAYED)
611 // Previously requested unload, but was delayed?
612 META_DEBUG(1, ("Retrying unload plugin '%s'", iplug->desc));
613 if(iplug->retry(now, PNL_DELAYED))
615 else if(meta_errno==ME_DELAYED)
619 META_ERROR("dll: Unexpected action for plugin '%s': '%s'", iplug->desc, iplug->str_action());
622 META_ERROR("dll: Unrecognized action for plugin '%s': '%s'", iplug->desc, iplug->str_action());
627 META_LOG("dll: Finished updating %d plugins; kept %d, loaded %d, unloaded %d, reloaded %d, delayed %d",
628 ndone, nkept, nloaded, nunloaded, nreloaded, ndelayed);
632 // Re-enable any plugins currently paused.
633 // meta_errno values:
635 void MPluginList::unpause_all(void) {
638 for(i=0; i < endlist; i++) {
640 if(iplug->status==PL_PAUSED)
645 // Retry any pending actions on plugins, for instance load/unload delayed
646 // until changelevel.
647 // meta_errno values:
649 void MPluginList::retry_all(PLUG_LOADTIME now) {
652 for(i=0; i < endlist; i++) {
654 if(iplug->action != PA_NONE)
655 iplug->retry(now, PNL_DELAYED);
659 // List plugins and information about them in a formatted table.
660 // meta_errno values:
662 void MPluginList::show() {
665 char desc[15+1], file[16+1], vers[7+1]; // plus 1 for term null
667 META_CONS("Currently loaded plugins:");
668 META_CONS(" %*s %-*s %-4s %-4s %-*s v%-*s %-3s %-5s %-5s",
669 WIDTH_MAX_PLUGINS, "",
670 sizeof(desc)-1, "description",
672 sizeof(file)-1, "file", sizeof(vers)-1, "ers",
673 "src", "load ", "unlod");
674 for(i=0; i < endlist; i++) {
676 if(pl->status < PL_VALID)
678 STRNCPY(desc, pl->desc, sizeof(desc));
679 STRNCPY(file, pl->file, sizeof(file));
680 if(pl->info && pl->info->version) STRNCPY(vers, pl->info->version, sizeof(vers));
681 else STRNCPY(vers, " -", sizeof(vers));
682 META_CONS(" [%*d] %-*s %-4s %-4s %-*s v%-*s %-3s %-5s %-5s",
683 WIDTH_MAX_PLUGINS, pl->index,
684 sizeof(desc)-1, desc,
685 pl->str_status(ST_SHOW), pl->str_action(SA_SHOW),
686 sizeof(file)-1, file, sizeof(vers)-1, vers,
687 pl->str_source(SO_SHOW),
688 pl->str_loadable(SL_SHOW), pl->str_unloadable(SL_SHOW));
689 if(pl->status == PL_RUNNING)
693 META_CONS("%d plugins, %d running", n, r);
696 // List plugins and information to Player/client entity. Differs from the
697 // "meta list" console command in that:
698 // - Shows only "running" plugins, skipping any failed or paused plugins.
699 // - Limited info about each plugin, mostly the "public" info (name, author,
701 // meta_errno values:
703 void MPluginList::show_client(edict_t *pEntity) {
706 META_CLIENT(pEntity, "Currently running plugins:");
707 for(i=0; i < endlist; i++) {
709 if(pl->status != PL_RUNNING)
712 META_CLIENT(pEntity, " [%3d] %s, v%s, %s, by %s, see %s",
714 pl->info->name ? pl->info->name : "<unknown>",
715 pl->info->version ? pl->info->version : "<?>",
716 pl->info->date ? pl->info->date : "<../../..>",
717 pl->info->author ? pl->info->author : "<unknown>",
718 pl->info->url ? pl->info->url : "<unknown>");
720 META_CLIENT(pEntity, "%d plugins", n);