core/systems/PluginSys.cpp
changeset 173 662fc7f7144b
child 174 5611e4b3fa42
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/systems/PluginSys.cpp	Fri Nov 10 18:08:13 2006 +0000
     1.3 @@ -0,0 +1,170 @@
     1.4 +#include <stdio.h>
     1.5 +#include "PluginSys.h"
     1.6 +
     1.7 +using namespace SourcePawn;
     1.8 +
     1.9 +CPlugin *CPlugin::CreatePlugin(const char *file, 
    1.10 +								bool debug_default, 
    1.11 +								PluginLifetime life, 
    1.12 +								char *error, 
    1.13 +								size_t maxlen)
    1.14 +{
    1.15 +	static unsigned int MySerial = 0;
    1.16 +	FILE *fp = fopen(file, "rb");
    1.17 +
    1.18 +	if (!fp)
    1.19 +	{
    1.20 +		snprintf(error, maxlen, "Could not open file");
    1.21 +		return NULL;
    1.22 +	}
    1.23 +
    1.24 +	int err;
    1.25 +	sp_plugin_t *pl = g_pSourcePawn->LoadFromFilePointer(fp, &err);
    1.26 +	if (pl == NULL)
    1.27 +	{
    1.28 +		snprintf(error, maxlen, "Could not load plugin, error %d", err);
    1.29 +		return NULL;
    1.30 +	}
    1.31 +
    1.32 +	fclose(fp);
    1.33 +
    1.34 +	ICompilation *co = g_pVM->StartCompilation(pl);
    1.35 +	
    1.36 +	if (debug_default)
    1.37 +	{
    1.38 +		if (!g_pVM->SetCompilationOption(co, "debug", "1"))
    1.39 +		{
    1.40 +			g_pVM->AbortCompilation(co);
    1.41 +			snprintf(error, maxlen, "Could not set plugin to debug mode");
    1.42 +			return NULL;
    1.43 +		}
    1.44 +	}
    1.45 +
    1.46 +	sp_context_t *ctx = g_pVM->CompileToContext(co, &err);
    1.47 +	if (ctx == NULL)
    1.48 +	{
    1.49 +		snprintf(error, maxlen, "Plugin failed to load, JIT error: %d", err);
    1.50 +		return NULL;
    1.51 +	}
    1.52 +
    1.53 +	IPluginContext *base = g_pSourcePawn->CreateBaseContext(ctx);
    1.54 +	CPlugin *pPlugin = new CPlugin;
    1.55 +
    1.56 +	snprintf(pPlugin->m_filename, PLATFORM_MAX_PATH, "%s", file);
    1.57 +	pPlugin->m_debugging = debug_default;
    1.58 +	pPlugin->m_ctx_current.base = base;
    1.59 +	pPlugin->m_ctx_current.ctx = ctx;
    1.60 +	pPlugin->m_lifetime = life;
    1.61 +	pPlugin->m_lock = false;
    1.62 +	pPlugin->m_serial = ++MySerial;
    1.63 +	pPlugin->m_status = Plugin_Loaded;
    1.64 +	pPlugin->m_plugin = pl;
    1.65 +
    1.66 +	pPlugin->UpdateInfo();
    1.67 +
    1.68 +	return pPlugin;
    1.69 +}
    1.70 +
    1.71 +void CPlugin::UpdateInfo()
    1.72 +{
    1.73 +	/* Now grab the info */
    1.74 +	uint32_t idx;
    1.75 +	IPluginContext *base = GetBaseContext();
    1.76 +	int err = base->FindPubvarByName("myinfo", &idx);
    1.77 +
    1.78 +	if (err == SP_ERROR_NONE)
    1.79 +	{
    1.80 +		struct sm_plugininfo_c_t
    1.81 +		{
    1.82 +			cell_t name;
    1.83 +			cell_t description;
    1.84 +			cell_t author;
    1.85 +			cell_t version;
    1.86 +			cell_t url;
    1.87 +		};
    1.88 +		sm_plugininfo_c_t *cinfo;
    1.89 +		cell_t local_addr;
    1.90 +
    1.91 +		base->GetPubvarAddrs(idx, &local_addr, (cell_t **)&cinfo);
    1.92 +		base->LocalToString(cinfo->name, (char **)&m_info.name);
    1.93 +		base->LocalToString(cinfo->description, (char **)&m_info.description);
    1.94 +		base->LocalToString(cinfo->author, (char **)&m_info.author);
    1.95 +		base->LocalToString(cinfo->url, (char **)&m_info.url);
    1.96 +		base->LocalToString(cinfo->version, (char **)&m_info.version);
    1.97 +	}
    1.98 +
    1.99 +	m_info.author = m_info.author ? m_info.author : "";
   1.100 +	m_info.description = m_info.description ? m_info.description : "";
   1.101 +	m_info.name = m_info.name ? m_info.name : "";
   1.102 +	m_info.url = m_info.url ? m_info.url : "";
   1.103 +	m_info.version = m_info.version ? m_info.version : "";
   1.104 +}
   1.105 +
   1.106 +const sp_plugin_t *CPlugin::GetPluginStructure() const
   1.107 +{
   1.108 +	return m_plugin;
   1.109 +}
   1.110 +
   1.111 +IPluginContext *CPlugin::GetBaseContext() const
   1.112 +{
   1.113 +	return m_ctx_current.base;
   1.114 +}
   1.115 +
   1.116 +sp_context_t *CPlugin::GetContext() const
   1.117 +{
   1.118 +	return m_ctx_current.ctx;
   1.119 +}
   1.120 +
   1.121 +const char *CPlugin::GetFilename() const
   1.122 +{
   1.123 +	return m_filename;
   1.124 +}
   1.125 +
   1.126 +PluginLifetime CPlugin::GetLifetime() const
   1.127 +{
   1.128 +	return m_lifetime;
   1.129 +}
   1.130 +
   1.131 +bool CPlugin::GetLockForUpdates() const
   1.132 +{
   1.133 +	return m_lock;
   1.134 +}
   1.135 +
   1.136 +const sm_plugininfo_t *CPlugin::GetPublicInfo() const
   1.137 +{
   1.138 +	return &m_info;
   1.139 +}
   1.140 +
   1.141 +unsigned int CPlugin::GetSerial() const
   1.142 +{
   1.143 +	return m_serial;
   1.144 +}
   1.145 +
   1.146 +PluginStatus CPlugin::GetStatus() const
   1.147 +{
   1.148 +	return m_status;
   1.149 +}
   1.150 +
   1.151 +bool CPlugin::IsDebugging() const
   1.152 +{
   1.153 +	return m_debugging;
   1.154 +}
   1.155 +
   1.156 +void CPlugin::SetLockForUpdates(bool lock_status)
   1.157 +{
   1.158 +	m_lock = lock_status;
   1.159 +}
   1.160 +
   1.161 +bool CPlugin::SetPauseState(bool paused)
   1.162 +{
   1.163 +	if (paused && GetStatus() != Plugin_Paused)
   1.164 +	{
   1.165 +		return false;
   1.166 +	} else if (!paused && GetStatus() != Plugin_Running) {
   1.167 +		return false;
   1.168 +	}
   1.169 +	
   1.170 +	/* :TODO: execute some forwards or some crap */
   1.171 +
   1.172 +	return true;
   1.173 +}