metamod/mplugin.cpp
branchB-work
changeset 130 4bfb3f26c519
parent 127 ba8cc90f015b
child 133 fbe73f167e7d
     1.1 --- a/metamod/mplugin.cpp	Tue Mar 08 10:09:50 2005 +0000
     1.2 +++ b/metamod/mplugin.cpp	Tue Jul 12 22:18:15 2005 +0000
     1.3 @@ -130,6 +130,85 @@
     1.4  	return(mTRUE);
     1.5  }
     1.6  
     1.7 +// Unload a plugin from plugin request
     1.8 +// meta_errno values:
     1.9 +//  - errno's from unload()
    1.10 +mBOOL MPlugin::plugin_unload(plid_t plid, PLUG_LOADTIME now, PL_UNLOAD_REASON reason)
    1.11 +{
    1.12 +	PLUG_ACTION old_action;
    1.13 +	MPlugin *pl_unloader;
    1.14 +
    1.15 +	if ( !(pl_unloader=Plugins->find(plid)) )
    1.16 +	{
    1.17 +		META_WARNING("dll: Not unloading plugin '%s'; plugin that requested unload is not found.", desc);
    1.18 +		RETURN_ERRNO(mFALSE, ME_BADREQ);
    1.19 +	} else if (pl_unloader->index == index) {
    1.20 +		META_WARNING("dll: Not unloading plugin '%s'; Plugin tried to unload itself.", desc);
    1.21 +		RETURN_ERRNO(mFALSE, ME_UNLOAD_SELF);
    1.22 +	} else if (is_unloader) {
    1.23 +		META_WARNING("dll: Not unloading plugin '%s'; Plugin is unloading plugin that tried to unload it.", desc);
    1.24 +		RETURN_ERRNO(mFALSE, ME_UNLOAD_UNLOADER);
    1.25 +	} else {
    1.26 +		unloader_index = pl_unloader->index;
    1.27 +	}
    1.28 +
    1.29 +	//block unloader from being unloaded by other plugin
    1.30 +	pl_unloader->is_unloader = mTRUE;
    1.31 +
    1.32 +	//try unload
    1.33 +	old_action = action;
    1.34 +	action = PA_UNLOAD;
    1.35 +	if (unload(now, reason, PNL_PLG_FORCED))
    1.36 +	{
    1.37 +		META_DEBUG(1,("Unloaded plugin '%s'", desc));
    1.38 +		pl_unloader->is_unloader = mFALSE;
    1.39 +		return mTRUE;
    1.40 +	}
    1.41 +
    1.42 +	pl_unloader->is_unloader = mFALSE;
    1.43 +
    1.44 +	//can't unload plugin now, set delayed
    1.45 +	if (meta_errno==ME_DELAYED)
    1.46 +	{
    1.47 +		action = old_action;
    1.48 +		meta_errno = ME_NOTALLOWED;
    1.49 +		META_DEBUG(2, ("dll: Failed unload plugin '%s'; can't detach now: allowed=%s; now=%s", desc, str_unloadable(), str_loadtime(PT_ANYTIME, SL_SIMPLE)));
    1.50 +	}
    1.51 +
    1.52 +	return mFALSE;
    1.53 +}
    1.54 +
    1.55 +// Parse a filename string from PEXT_LOAD_PLUGIN_BY_* function into a plugin.
    1.56 +// meta_errno values:
    1.57 +mBOOL MPlugin::plugin_parseline(const char *fname, int loader_index)
    1.58 +{
    1.59 +	char *cp;
    1.60 +
    1.61 +	source_plugin_index = loader_index;
    1.62 +
    1.63 +	STRNCPY(filename, fname, sizeof(filename));
    1.64 +	normalize_pathname(filename);
    1.65 +
    1.66 +	//store just name of the actual _file, without path
    1.67 +	cp = strrchr(filename, '/');
    1.68 +	if (cp)
    1.69 +		file = cp+1;
    1.70 +	else
    1.71 +		file = filename;
    1.72 +
    1.73 +	//grab description
    1.74 +	//temporarily use plugin file until plugin can be queried
    1.75 +	snprintf(desc, sizeof(desc), "<%s>", file);
    1.76 +
    1.77 +	//make full pathname
    1.78 +	full_gamedir_path(filename, pathname);
    1.79 +
    1.80 +	source = PS_PLUGIN;
    1.81 +	status = PL_VALID;
    1.82 +
    1.83 +	return mTRUE;
    1.84 +}
    1.85 +
    1.86  // Parse a line from console "load" command into a plugin.
    1.87  // meta_errno values:
    1.88  //  - ME_FORMAT		invalid line format
    1.89 @@ -856,7 +935,7 @@
    1.90  //  - ME_DELAYED	unload request is delayed (till changelevel?)
    1.91  //  - ME_NOTALLOWED	plugin not unloadable after startup
    1.92  //  - errno's from check_input()
    1.93 -mBOOL MPlugin::unload(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
    1.94 +mBOOL MPlugin::unload(PLUG_LOADTIME now, PL_UNLOAD_REASON reason, PL_UNLOAD_REASON real_reason) {
    1.95  	if(!check_input()) {
    1.96  		// details logged, meta_errno set in check_input()
    1.97  		RETURN_ERRNO(mFALSE, ME_ARGUMENT);
    1.98 @@ -939,7 +1018,7 @@
    1.99  		action=PA_LOAD;
   1.100  		clear();
   1.101  	}
   1.102 -	META_LOG("dll: Unloaded plugin '%s' for reason '%s'", desc, str_reason(reason));
   1.103 +	META_LOG("dll: Unloaded plugin '%s' for reason '%s'", desc, str_reason(reason, real_reason));
   1.104  	return(mTRUE);
   1.105  }
   1.106  
   1.107 @@ -979,7 +1058,8 @@
   1.108  	}
   1.109  	// Are we allowed to load this plugin at this time?
   1.110  	// If we cannot load the plugin after unloading it, we keep it.
   1.111 -	if(info->loadable < now) {
   1.112 +	if (info && info->loadable < now)
   1.113 +	{
   1.114  		if(info->loadable > PT_STARTUP) {
   1.115  			META_DEBUG(2, ("dll: Delaying reload plugin '%s'; would not be able to reattach now: allowed=%s; now=%s", desc, str_loadable(), str_loadtime(now, SL_SIMPLE)));
   1.116  			// caller should give message to user
   1.117 @@ -994,7 +1074,7 @@
   1.118  		}
   1.119  	}
   1.120  
   1.121 -	if(!unload(now, reason)) {
   1.122 +	if(status>=PL_RUNNING && !unload(now, reason, PNL_PLG_FORCED)) {
   1.123  		META_ERROR("dll: Failed to unload plugin '%s' for reloading", desc);
   1.124  		// meta_errno should be set already in unload()
   1.125  		return(mFALSE);
   1.126 @@ -1059,7 +1139,7 @@
   1.127  	else if(action==PA_ATTACH)
   1.128  		return(load(now));
   1.129  	else if(action==PA_UNLOAD)
   1.130 -		return(unload(now, reason));
   1.131 +		return(unload(now, reason, reason));
   1.132  	else if(action==PA_RELOAD)
   1.133  		return(reload(now, reason));
   1.134  	else {
   1.135 @@ -1318,8 +1398,15 @@
   1.136  // Return a string describing why a plugin is to be unloaded.
   1.137  // meta_errno values:
   1.138  //  - none
   1.139 -char *MPlugin::str_reason(PL_UNLOAD_REASON preason) {
   1.140 -	switch(preason) {
   1.141 +char *MPlugin::str_reason(PL_UNLOAD_REASON preason, PL_UNLOAD_REASON preal_reason) {
   1.142 +	char buf[128];
   1.143 +	
   1.144 +	if(preason == PNL_PLUGIN)
   1.145 +		preason = PNL_NULL;
   1.146 +	if(preason == PNL_PLG_FORCED)
   1.147 +		preason = PNL_NULL;
   1.148 +
   1.149 +	switch(preal_reason) {
   1.150  		case PNL_NULL:
   1.151  			return("null");
   1.152  		case PNL_INI_DELETED:
   1.153 @@ -1330,8 +1417,14 @@
   1.154  			return("server command");
   1.155  		case PNL_CMD_FORCED:
   1.156  			return("forced by server command");
   1.157 +		case PNL_PLUGIN:
   1.158 +			STRNCPY(buf, str_reason(PNL_NULL, preason), sizeof(buf));
   1.159 +			return(UTIL_VarArgs("%s (request from plugin[%d])", buf, unloader_index));
   1.160 +		case PNL_PLG_FORCED:
   1.161 +			STRNCPY(buf, str_reason(PNL_NULL, preason), sizeof(buf));
   1.162 +			return(UTIL_VarArgs("%s (forced request from plugin[%d])", buf, unloader_index));
   1.163  		default:
   1.164 -			return(UTIL_VarArgs("unknown (%d)", preason));
   1.165 +			return(UTIL_VarArgs("unknown (%d)", preal_reason));
   1.166  	}
   1.167  }
   1.168  
   1.169 @@ -1346,6 +1439,14 @@
   1.170  		case PS_CMD:
   1.171  			if(fmt==SO_SHOW) return("cmd");
   1.172  			else return("console command");
   1.173 +		case PS_PLUGIN:
   1.174 +			if(source_plugin_index <= 0) {
   1.175 +				if(fmt==SO_SHOW) return("plUN");
   1.176 +				else return("unloaded plugin");
   1.177 +			} else {
   1.178 +				if(fmt==SO_SHOW) return(UTIL_VarArgs("pl%d", source_plugin_index));
   1.179 +				else return(UTIL_VarArgs("plugin [%d]", source_plugin_index));
   1.180 +			}
   1.181  		default:
   1.182  			if(fmt==SO_SHOW) return(UTIL_VarArgs("UNK%d", source));
   1.183  			else return(UTIL_VarArgs("unknown (%d)", source));