core/systems/ForwardSys.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Sep 14 02:59:26 2008 -0500 (2008-09-14)
changeset 2288 15c77b21589a
parent 2234 ff23c303ae74
child 2289 44045ead61cd
permissions -rw-r--r--
Eliminated head for dead branch: refac-jit
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include <assert.h>
ds@1952
    33
#include <stdarg.h>
ds@1952
    34
#include <string.h>
ds@1952
    35
#include "ForwardSys.h"
ds@1952
    36
#include "PluginSys.h"
ds@1952
    37
#include "ShareSys.h"
ds@1952
    38
#include "DebugReporter.h"
ds@1952
    39
ds@1952
    40
CForwardManager g_Forwards;
ds@1952
    41
ds@1952
    42
/**
ds@1952
    43
 * Genesis turns to its source, reduction occurs stepwise although the essence is all one.
ds@1952
    44
 * End of line.  FTL system check.
ds@1952
    45
 *
ds@1952
    46
 * :TODO: WHAT NEEDS TO BE TESTED IN THIS BEAST
ds@1952
    47
 * VARARG FUNCTIONS:
ds@1952
    48
 * - Pushing no varargs
ds@1952
    49
 */
ds@1952
    50
ds@1952
    51
// :TODO: IMPORTANT!!! The result pointer arg in the execute function maybe invalid if the forward fails
ds@1952
    52
// so later evaluation of this result may cause problems on higher levels of abstraction. DOCUMENT OR FIX ALL FORWARDS!
ds@1952
    53
ds@1952
    54
CForwardManager::~CForwardManager()
ds@1952
    55
{
ds@1952
    56
	CStack<CForward *>::iterator iter;
ds@1952
    57
	for (iter=m_FreeForwards.begin(); iter!=m_FreeForwards.end(); iter++)
ds@1952
    58
	{
ds@1952
    59
		delete (*iter);
ds@1952
    60
	}
ds@1952
    61
	m_FreeForwards.popall();
ds@1952
    62
}
ds@1952
    63
ds@1952
    64
void CForwardManager::OnSourceModAllInitialized()
ds@1952
    65
{
ds@1952
    66
	g_PluginSys.AddPluginsListener(this);
ds@1952
    67
	g_ShareSys.AddInterface(NULL, this);
ds@1952
    68
}
ds@1952
    69
ds@1952
    70
IForward *CForwardManager::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, ...)
ds@1952
    71
{
ds@1952
    72
	CForward *fwd;
ds@1952
    73
	va_list ap;
ds@1952
    74
	va_start(ap, types);
ds@1952
    75
	
ds@1952
    76
	fwd = CForward::CreateForward(name, et, num_params, types, ap);
ds@1952
    77
ds@1952
    78
	va_end(ap);
ds@1952
    79
ds@1952
    80
	if (fwd)
ds@1952
    81
	{
ds@1952
    82
		g_PluginSys.AddFunctionsToForward(name, fwd);
ds@1952
    83
ds@1952
    84
		m_managed.push_back(fwd);
ds@1952
    85
	}
ds@1952
    86
ds@1952
    87
	return fwd;
ds@1952
    88
}
ds@1952
    89
ds@1952
    90
IChangeableForward *CForwardManager::CreateForwardEx(const char *name, ExecType et, int num_params, const ParamType *types, ...)
ds@1952
    91
{
ds@1952
    92
	CForward *fwd;
ds@1952
    93
	va_list ap;
ds@1952
    94
	va_start(ap, types);
ds@1952
    95
ds@1952
    96
	fwd = CForward::CreateForward(name, et, num_params, types, ap);
ds@1952
    97
ds@1952
    98
	va_end(ap);
ds@1952
    99
ds@1952
   100
	if (fwd)
ds@1952
   101
	{
ds@1952
   102
		m_unmanaged.push_back(fwd);
ds@1952
   103
	}
ds@1952
   104
ds@1952
   105
	return fwd;
ds@1952
   106
}
ds@1952
   107
ds@1952
   108
void CForwardManager::OnPluginLoaded(IPlugin *plugin)
ds@1952
   109
{
ds@1952
   110
	/* Attach any globally managed forwards */
ds@1952
   111
	List<CForward *>::iterator iter;
ds@1952
   112
	CForward *fwd;
ds@1952
   113
ds@1952
   114
	for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
ds@1952
   115
	{
ds@1952
   116
		fwd = (*iter);
ds@1952
   117
		IPluginFunction *pFunc = plugin->GetBaseContext()->GetFunctionByName(fwd->GetForwardName());
ds@1952
   118
		if (pFunc)
ds@1952
   119
		{
ds@1952
   120
			fwd->AddFunction(pFunc);
ds@1952
   121
		}
ds@1952
   122
	}
ds@1952
   123
}
ds@1952
   124
ds@1952
   125
void CForwardManager::OnPluginUnloaded(IPlugin *plugin)
ds@1952
   126
{
ds@1952
   127
	List<CForward *>::iterator iter;
ds@1952
   128
	CForward *fwd;
ds@1952
   129
ds@1952
   130
	for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
ds@1952
   131
	{
ds@1952
   132
		fwd = (*iter);
ds@1952
   133
		fwd->RemoveFunctionsOfPlugin(plugin);
ds@1952
   134
	}
ds@1952
   135
ds@1952
   136
	for (iter=m_unmanaged.begin(); iter!=m_unmanaged.end(); iter++)
ds@1952
   137
	{
ds@1952
   138
		fwd = (*iter);
ds@1952
   139
		fwd->RemoveFunctionsOfPlugin(plugin);
ds@1952
   140
	}
ds@1952
   141
}
ds@1952
   142
ds@1952
   143
IForward *CForwardManager::FindForward(const char *name, IChangeableForward **ifchng)
ds@1952
   144
{
ds@1952
   145
	List<CForward *>::iterator iter;
ds@1952
   146
	CForward *fwd;
ds@1952
   147
ds@1952
   148
	for (iter=m_managed.begin(); iter!=m_managed.end(); iter++)
ds@1952
   149
	{
ds@1952
   150
		fwd = (*iter);
ds@1952
   151
		if (strcmp(fwd->GetForwardName(), name) == 0)
ds@1952
   152
		{
ds@1952
   153
			if (ifchng)
ds@1952
   154
			{
ds@1952
   155
				*ifchng = NULL;
ds@1952
   156
			}
ds@1952
   157
			return fwd;
ds@1952
   158
		}
ds@1952
   159
	}
ds@1952
   160
ds@1952
   161
	for (iter=m_unmanaged.begin(); iter!=m_unmanaged.end(); iter++)
ds@1952
   162
	{
ds@1952
   163
		fwd = (*iter);
ds@1952
   164
		if (strcmp(fwd->GetForwardName(), name) == 0)
ds@1952
   165
		{
ds@1952
   166
			if (ifchng)
ds@1952
   167
			{
ds@1952
   168
				*ifchng = fwd;
ds@1952
   169
			}
ds@1952
   170
			return fwd;
ds@1952
   171
		}
ds@1952
   172
	}
ds@1952
   173
ds@1952
   174
	if (ifchng)
ds@1952
   175
	{
ds@1952
   176
		*ifchng = NULL;
ds@1952
   177
	}
ds@1952
   178
ds@1952
   179
	return NULL;
ds@1952
   180
}
ds@1952
   181
ds@1952
   182
void CForwardManager::ReleaseForward(IForward *forward)
ds@1952
   183
{
ds@1952
   184
	ForwardFree(static_cast<CForward *>(forward));
ds@1952
   185
}
ds@1952
   186
ds@1952
   187
void CForwardManager::ForwardFree(CForward *fwd)
ds@1952
   188
{
dvander@2234
   189
    if (fwd == NULL)
dvander@2234
   190
    {
dvander@2234
   191
        return;
dvander@2234
   192
    }
dvander@2234
   193
ds@1952
   194
	m_FreeForwards.push(fwd);
ds@1952
   195
	m_managed.remove(fwd);
ds@1952
   196
}
ds@1952
   197
ds@1952
   198
CForward *CForwardManager::ForwardMake()
ds@1952
   199
{
ds@1952
   200
	CForward *fwd;
ds@1952
   201
	if (m_FreeForwards.empty())
ds@1952
   202
	{
ds@1952
   203
		fwd = new CForward;
ds@1952
   204
	} else {
ds@1952
   205
		fwd = m_FreeForwards.front();
ds@1952
   206
		m_FreeForwards.pop();
ds@1952
   207
	}
ds@1952
   208
	return fwd;
ds@1952
   209
}
ds@1952
   210
ds@1952
   211
void CForwardManager::OnPluginPauseChange(IPlugin *plugin, bool paused)
ds@1952
   212
{
ds@1952
   213
	/* No longer used */
ds@1952
   214
}
ds@1952
   215
ds@1952
   216
/*************************************
ds@1952
   217
 * ACTUAL FORWARD API IMPLEMENTATION *
ds@1952
   218
 *************************************/
ds@1952
   219
ds@1952
   220
CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, va_list ap)
ds@1952
   221
{
ds@1952
   222
	ParamType _types[SP_MAX_EXEC_PARAMS];
ds@1952
   223
ds@1952
   224
	if (num_params > SP_MAX_EXEC_PARAMS)
ds@1952
   225
	{
ds@1952
   226
		return NULL;
ds@1952
   227
	}
ds@1952
   228
	
ds@1952
   229
	if (types == NULL && num_params)
ds@1952
   230
	{
ds@1952
   231
		for (unsigned int i=0; i<num_params; i++)
ds@1952
   232
		{
ds@1952
   233
			_types[i] = (ParamType)va_arg(ap, int);
ds@1952
   234
			if (_types[i] == Param_VarArgs && (i != num_params - 1))
ds@1952
   235
			{
ds@1952
   236
				return NULL;
ds@1952
   237
			}
ds@1952
   238
		}
ds@1952
   239
		types = _types;
ds@1952
   240
	} else {
ds@1952
   241
		for (unsigned int i=0; i<num_params; i++)
ds@1952
   242
		{
ds@1952
   243
			_types[i] = types[i];
ds@1952
   244
			if (types[i] == Param_VarArgs && (i != num_params - 1))
ds@1952
   245
			{
ds@1952
   246
				return NULL;
ds@1952
   247
			}
ds@1952
   248
		}
ds@1952
   249
	}
ds@1952
   250
ds@1952
   251
	/* First parameter can never be varargs */
ds@1952
   252
	if (num_params && _types[0] == Param_VarArgs)
ds@1952
   253
	{
ds@1952
   254
		return NULL;
ds@1952
   255
	}
ds@1952
   256
ds@1952
   257
	CForward *pForward = g_Forwards.ForwardMake();
ds@1952
   258
	pForward->m_curparam = 0;
ds@1952
   259
	pForward->m_ExecType = et;
ds@1952
   260
	snprintf(pForward->m_name, FORWARDS_NAME_MAX, "%s", name ? name : "");
ds@1952
   261
	
ds@1952
   262
	for (unsigned int i=0; i<num_params; i++)
ds@1952
   263
	{
ds@1952
   264
		pForward->m_types[i] = _types[i];
ds@1952
   265
	}
ds@1952
   266
ds@1952
   267
	if (num_params && _types[num_params-1] == Param_VarArgs)
ds@1952
   268
	{
ds@1952
   269
		pForward->m_varargs = num_params--;
ds@1952
   270
	} else {
ds@1952
   271
		pForward->m_varargs = false;
ds@1952
   272
	}
ds@1952
   273
ds@1952
   274
	pForward->m_numparams = num_params;
ds@1952
   275
	pForward->m_errstate = SP_ERROR_NONE;
ds@1952
   276
ds@1952
   277
	pForward->m_functions.clear();
ds@1952
   278
ds@1952
   279
	return pForward;
ds@1952
   280
}
ds@1952
   281
ds@1952
   282
int CForward::Execute(cell_t *result, IForwardFilter *filter)
ds@1952
   283
{
ds@1952
   284
	if (m_errstate)
ds@1952
   285
	{
ds@1952
   286
		int err = m_errstate;
ds@1952
   287
		Cancel();
ds@1952
   288
		return err;
ds@1952
   289
	}
ds@1952
   290
ds@1952
   291
	FuncIter iter = m_functions.begin();
ds@1952
   292
	IPluginFunction *func;
ds@1952
   293
	cell_t cur_result = 0;
ds@1952
   294
	cell_t high_result = 0;
ds@2181
   295
	cell_t low_result = 0;
ds@1952
   296
	int err;
ds@1952
   297
	unsigned int failed=0, success=0;
ds@1952
   298
	unsigned int num_params = m_curparam;
ds@1952
   299
	FwdParamInfo temp_info[SP_MAX_EXEC_PARAMS];
ds@1952
   300
	FwdParamInfo *param;
ds@1952
   301
	ParamType type;
ds@1952
   302
ds@1952
   303
	/* Save local, reset */
ds@1952
   304
	memcpy(temp_info, m_params, sizeof(m_params));
ds@1952
   305
	m_curparam = 0;
ds@1952
   306
ds@1952
   307
	for (iter=m_functions.begin(); iter!=m_functions.end(); iter++)
ds@1952
   308
	{
ds@1952
   309
		func = (*iter);
ds@1952
   310
ds@1952
   311
		for (unsigned int i=0; i<num_params; i++)
ds@1952
   312
		{
ds@1952
   313
			int err = SP_ERROR_PARAM;
ds@1952
   314
			param = &temp_info[i];
ds@2181
   315
ds@1952
   316
			if (i >= m_numparams || m_types[i] == Param_Any)
ds@1952
   317
			{
ds@1952
   318
				type = param->pushedas;
ds@2181
   319
			}
ds@2181
   320
			else
ds@2181
   321
			{
ds@1952
   322
				type = m_types[i];
ds@1952
   323
			}
ds@2181
   324
ds@1952
   325
			if ((i >= m_numparams) || (type & SP_PARAMFLAG_BYREF))
ds@1952
   326
			{
ds@1952
   327
				/* If we're byref or we're vararg, we always push everything by ref.
ds@1952
   328
				 * Even if they're byval, we must push them byref.
ds@1952
   329
				 */
ds@1952
   330
				if (type == Param_String)
ds@1952
   331
				{
ds@1952
   332
					err = func->PushStringEx((char *)param->byref.orig_addr, param->byref.cells, param->byref.sz_flags, param->byref.flags);
ds@2181
   333
				}
ds@2181
   334
				else if (type == Param_Float || type == Param_Cell)
ds@2181
   335
				{
ds@1952
   336
					err = func->PushCellByRef(&param->val); 
ds@2181
   337
				}
ds@2181
   338
				else
ds@2181
   339
				{
ds@1952
   340
					err = func->PushArray(param->byref.orig_addr, param->byref.cells, param->byref.flags);
ds@1952
   341
					assert(type == Param_Array || type == Param_FloatByRef || type == Param_CellByRef);
ds@1952
   342
				}
ds@2181
   343
			}
ds@2181
   344
			else
ds@2181
   345
			{
ds@1952
   346
				/* If we're not byref or not vararg, our job is a bit easier. */
ds@1952
   347
				assert(type == Param_Cell || type == Param_Float);
ds@1952
   348
				err = func->PushCell(param->val);
ds@1952
   349
			}
ds@2181
   350
ds@1952
   351
			if (err != SP_ERROR_NONE)
ds@1952
   352
			{
ds@2181
   353
				g_DbgReporter.GenerateError(func->GetParentContext(), 
ds@2181
   354
					func->GetFunctionID(), 
ds@2181
   355
					err, 
ds@2181
   356
					"Failed to push parameter while executing forward");
ds@1952
   357
				continue;
ds@1952
   358
			}
ds@1952
   359
		}
ds@1952
   360
		
ds@1952
   361
		/* Call the function and deal with the return value. */
ds@1952
   362
		if ((err=func->Execute(&cur_result)) != SP_ERROR_NONE)
ds@1952
   363
		{
ds@1952
   364
			failed++;
ds@2181
   365
		}
ds@2181
   366
		else
ds@2181
   367
		{
ds@1952
   368
			success++;
ds@1952
   369
			switch (m_ExecType)
ds@1952
   370
			{
ds@1952
   371
			case ET_Event:
ds@1952
   372
				{
ds@1952
   373
					if (cur_result > high_result)
ds@1952
   374
					{
ds@1952
   375
						high_result = cur_result;
ds@1952
   376
					}
ds@1952
   377
					break;
ds@1952
   378
				}
ds@1952
   379
			case ET_Hook:
ds@1952
   380
				{
ds@1952
   381
					if (cur_result > high_result)
ds@1952
   382
					{
ds@1952
   383
						high_result = cur_result;
ds@1952
   384
						if ((ResultType)high_result == Pl_Stop)
ds@1952
   385
						{
ds@1952
   386
							goto done;
ds@1952
   387
						}
ds@1952
   388
					}
ds@1952
   389
					break;
ds@1952
   390
				}
ds@2181
   391
			case ET_LowEvent:
ds@1952
   392
				{
pred@2183
   393
					/* Check if the current result is the lowest so far (or if it's the first result) */
pred@2183
   394
					if (cur_result < low_result || success == 1)
ds@1952
   395
					{
ds@2181
   396
						low_result = cur_result;
ds@1952
   397
					}
ds@1952
   398
					break;
ds@1952
   399
				}
ds@1952
   400
			default:
ds@1952
   401
				{
ds@1952
   402
					break;
ds@1952
   403
				}
ds@1952
   404
			}
ds@1952
   405
		}
ds@1952
   406
	}
ds@1952
   407
ds@1952
   408
done:
ds@2181
   409
ds@1952
   410
	if (success)
ds@1952
   411
	{
ds@2181
   412
		switch (m_ExecType)
ds@1952
   413
		{
ds@2181
   414
		case ET_Ignore:
ds@2181
   415
			{
ds@2181
   416
				cur_result = 0;
ds@2181
   417
				break;
ds@2181
   418
			}
ds@2181
   419
		case ET_Event:
ds@2181
   420
		case ET_Hook:
ds@2181
   421
			{
ds@2181
   422
				cur_result = high_result;
ds@2181
   423
				break;
ds@2181
   424
			}
ds@2181
   425
		case ET_LowEvent:
ds@2181
   426
			{
ds@2181
   427
				cur_result = low_result;
ds@2181
   428
				break;
ds@2181
   429
			}
ds@2181
   430
		default:
ds@2181
   431
			{
ds@2181
   432
				break;
ds@2181
   433
			}
ds@1952
   434
		}
ds@2181
   435
ds@1952
   436
		if (result)
ds@1952
   437
		{
ds@1952
   438
			*result = cur_result;
ds@1952
   439
		}
ds@1952
   440
	}
ds@1952
   441
ds@1952
   442
	return SP_ERROR_NONE;
ds@1952
   443
}
ds@1952
   444
ds@1952
   445
int CForward::PushCell(cell_t cell)
ds@1952
   446
{
ds@1952
   447
	if (m_curparam < m_numparams)
ds@1952
   448
	{
ds@1952
   449
		if (m_types[m_curparam] == Param_Any)
ds@1952
   450
		{
ds@1952
   451
			m_params[m_curparam].pushedas = Param_Cell;
ds@1952
   452
		} else if (m_types[m_curparam] != Param_Cell) {
ds@1952
   453
			return SetError(SP_ERROR_PARAM);
ds@1952
   454
		}
ds@1952
   455
	} else {
ds@1952
   456
		if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
ds@1952
   457
		{
ds@1952
   458
			return SetError(SP_ERROR_PARAMS_MAX);
ds@1952
   459
		}
ds@1952
   460
		m_params[m_curparam].pushedas = Param_Cell;
ds@1952
   461
	}
ds@1952
   462
ds@1952
   463
	m_params[m_curparam++].val = cell;
ds@1952
   464
ds@1952
   465
	return SP_ERROR_NONE;
ds@1952
   466
}
ds@1952
   467
ds@1952
   468
int CForward::PushFloat(float number)
ds@1952
   469
{
ds@1952
   470
	if (m_curparam < m_numparams)
ds@1952
   471
	{
ds@1952
   472
		if (m_types[m_curparam] == Param_Any)
ds@1952
   473
		{
ds@1952
   474
			m_params[m_curparam].pushedas = Param_Float;
ds@1952
   475
		} else if (m_types[m_curparam] != Param_Float) {
ds@1952
   476
			return SetError(SP_ERROR_PARAM);
ds@1952
   477
		}
ds@1952
   478
	} else {
ds@1952
   479
		if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
ds@1952
   480
		{
ds@1952
   481
			return SetError(SP_ERROR_PARAMS_MAX);
ds@1952
   482
		}
ds@1952
   483
		m_params[m_curparam].pushedas = Param_Float;
ds@1952
   484
	}
ds@1952
   485
ds@1952
   486
	m_params[m_curparam++].val = *(cell_t *)&number;
ds@1952
   487
ds@1952
   488
	return SP_ERROR_NONE;
ds@1952
   489
}
ds@1952
   490
ds@1952
   491
int CForward::PushCellByRef(cell_t *cell, int flags)
ds@1952
   492
{
ds@1952
   493
	if (m_curparam < m_numparams)
ds@1952
   494
	{
ds@1952
   495
		if (m_types[m_curparam] == Param_Any)
ds@1952
   496
		{
ds@1952
   497
			m_params[m_curparam].pushedas = Param_CellByRef;
ds@1952
   498
		} else if (m_types[m_curparam] != Param_CellByRef) {
ds@1952
   499
			return SetError(SP_ERROR_PARAM);
ds@1952
   500
		}
ds@1952
   501
	} else {
ds@1952
   502
		if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
ds@1952
   503
		{
ds@1952
   504
			return SetError(SP_ERROR_PARAMS_MAX);
ds@1952
   505
		}
ds@1952
   506
		m_params[m_curparam].pushedas = Param_CellByRef;
ds@1952
   507
	}
ds@1952
   508
ds@1952
   509
	_Int_PushArray(cell, 1, flags);
ds@1952
   510
	m_curparam++;
ds@1952
   511
ds@1952
   512
	return SP_ERROR_NONE;
ds@1952
   513
}
ds@1952
   514
ds@1952
   515
int CForward::PushFloatByRef(float *num, int flags)
ds@1952
   516
{
ds@1952
   517
	if (m_curparam < m_numparams)
ds@1952
   518
	{
ds@1952
   519
		if (m_types[m_curparam] == Param_Any)
ds@1952
   520
		{
ds@1952
   521
			m_params[m_curparam].pushedas = Param_FloatByRef;
ds@1952
   522
		} else if (m_types[m_curparam] != Param_FloatByRef) {
ds@1952
   523
			return SetError(SP_ERROR_PARAM);
ds@1952
   524
		}
ds@1952
   525
	} else {
ds@1952
   526
		if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
ds@1952
   527
		{
ds@1952
   528
			return SetError(SP_ERROR_PARAMS_MAX);
ds@1952
   529
		}
ds@1952
   530
		m_params[m_curparam].pushedas = Param_FloatByRef;
ds@1952
   531
	}
ds@1952
   532
ds@1952
   533
	_Int_PushArray((cell_t *)num, 1, flags);
ds@1952
   534
	m_curparam++;
ds@1952
   535
ds@1952
   536
	return SP_ERROR_NONE;
ds@1952
   537
}
ds@1952
   538
ds@1952
   539
void CForward::_Int_PushArray(cell_t *inarray, unsigned int cells, int flags)
ds@1952
   540
{
ds@1952
   541
	m_params[m_curparam].byref.cells = cells;
ds@1952
   542
	m_params[m_curparam].byref.flags = flags;
ds@1952
   543
	m_params[m_curparam].byref.orig_addr = inarray;
ds@1952
   544
}
ds@1952
   545
ds@1952
   546
int CForward::PushArray(cell_t *inarray, unsigned int cells, int flags)
ds@1952
   547
{
ds@1952
   548
	/* We don't allow this here */
ds@1952
   549
	if (!inarray)
ds@1952
   550
	{
ds@1952
   551
		return SetError(SP_ERROR_PARAM);
ds@1952
   552
	}
ds@1952
   553
ds@1952
   554
	if (m_curparam < m_numparams)
ds@1952
   555
	{
ds@1952
   556
		if (m_types[m_curparam] == Param_Any)
ds@1952
   557
		{
ds@1952
   558
			m_params[m_curparam].pushedas = Param_Array;
ds@1952
   559
		} else if (m_types[m_curparam] != Param_Array) {
ds@1952
   560
			return SetError(SP_ERROR_PARAM);
ds@1952
   561
		}
ds@1952
   562
	} else {
ds@1952
   563
		if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
ds@1952
   564
		{
ds@1952
   565
			return SetError(SP_ERROR_PARAMS_MAX);
ds@1952
   566
		}
ds@1952
   567
		m_params[m_curparam].pushedas = Param_Array;
ds@1952
   568
	}
ds@1952
   569
ds@1952
   570
	_Int_PushArray(inarray, cells, flags);
ds@1952
   571
ds@1952
   572
	m_curparam++;
ds@1952
   573
ds@1952
   574
	return SP_ERROR_NONE;
ds@1952
   575
}
ds@1952
   576
ds@1952
   577
void CForward::_Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags)
ds@1952
   578
{
ds@1952
   579
	m_params[m_curparam].byref.cells = cells;	/* Notice this contains the char count not cell count */
ds@1952
   580
	m_params[m_curparam].byref.flags = cp_flags;
ds@1952
   581
	m_params[m_curparam].byref.orig_addr = inarray;
ds@1952
   582
	m_params[m_curparam].byref.sz_flags = sz_flags;
ds@1952
   583
}
ds@1952
   584
ds@1952
   585
int CForward::PushString(const char *string)
ds@1952
   586
{
ds@1952
   587
	if (m_curparam < m_numparams)
ds@1952
   588
	{
ds@1952
   589
		if (m_types[m_curparam] == Param_Any)
ds@1952
   590
		{
ds@1952
   591
			m_params[m_curparam].pushedas = Param_String;
ds@1952
   592
		} else if (m_types[m_curparam] != Param_String) {
ds@1952
   593
			return SetError(SP_ERROR_PARAM);
ds@1952
   594
		}
ds@1952
   595
	} else {
ds@1952
   596
		if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
ds@1952
   597
		{
ds@1952
   598
			return SetError(SP_ERROR_PARAMS_MAX);
ds@1952
   599
		}
ds@1952
   600
		m_params[m_curparam].pushedas = Param_String;
ds@1952
   601
	}
ds@1952
   602
ds@1952
   603
	_Int_PushString((cell_t *)string, strlen(string)+1, SM_PARAM_STRING_COPY, 0);
ds@1952
   604
	m_curparam++;
ds@1952
   605
ds@1952
   606
	return SP_ERROR_NONE;
ds@1952
   607
}
ds@1952
   608
ds@1952
   609
int CForward::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
ds@1952
   610
{
ds@1952
   611
	if (m_curparam < m_numparams)
ds@1952
   612
	{
ds@1952
   613
		if (m_types[m_curparam] == Param_Any)
ds@1952
   614
		{
ds@1952
   615
			m_params[m_curparam].pushedas = Param_String;
ds@1952
   616
		} else if (m_types[m_curparam] != Param_String) {
ds@1952
   617
			return SetError(SP_ERROR_PARAM);
ds@1952
   618
		}
ds@1952
   619
	} else {
ds@1952
   620
		if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
ds@1952
   621
		{
ds@1952
   622
			return SetError(SP_ERROR_PARAMS_MAX);
ds@1952
   623
		}
ds@1952
   624
		m_params[m_curparam].pushedas = Param_String;
ds@1952
   625
	}
ds@1952
   626
ds@1952
   627
	_Int_PushString((cell_t *)buffer, length, sz_flags, cp_flags);
ds@1952
   628
	m_curparam++;
ds@1952
   629
ds@1952
   630
	return SP_ERROR_NONE;
ds@1952
   631
}
ds@1952
   632
ds@1952
   633
void CForward::Cancel()
ds@1952
   634
{
ds@1952
   635
	if (!m_curparam)
ds@1952
   636
	{
ds@1952
   637
		return;
ds@1952
   638
	}
ds@1952
   639
ds@1952
   640
	m_curparam = 0;
ds@1952
   641
	m_errstate = SP_ERROR_NONE;
ds@1952
   642
}
ds@1952
   643
ds@1952
   644
bool CForward::AddFunction(IPluginContext *pContext, funcid_t index)
ds@1952
   645
{
ds@1952
   646
	IPluginFunction *pFunc = pContext->GetFunctionById(index);
ds@1952
   647
ds@1952
   648
	if (!pFunc)
ds@1952
   649
	{
ds@1952
   650
		return false;
ds@1952
   651
	}
ds@1952
   652
ds@1952
   653
	return AddFunction(pFunc);
ds@1952
   654
}
ds@1952
   655
ds@1952
   656
bool CForward::RemoveFunction(IPluginContext *pContext, funcid_t index)
ds@1952
   657
{
ds@1952
   658
	IPluginFunction *pFunc = pContext->GetFunctionById(index);
ds@1952
   659
ds@1952
   660
	if (!pFunc)
ds@1952
   661
	{
ds@1952
   662
		return false;
ds@1952
   663
	}
ds@1952
   664
ds@1952
   665
	return RemoveFunction(pFunc);
ds@1952
   666
}
ds@1952
   667
ds@1952
   668
bool CForward::RemoveFunction(IPluginFunction *func)
ds@1952
   669
{
ds@1952
   670
	bool found = false;
ds@1952
   671
	FuncIter iter;
ds@1952
   672
	List<IPluginFunction *> *lst;
ds@1952
   673
ds@1952
   674
	if (func->IsRunnable())
ds@1952
   675
	{
ds@1952
   676
		lst = &m_functions;
ds@1952
   677
	} else {
ds@1952
   678
		lst = &m_paused;
ds@1952
   679
	}
ds@1952
   680
ds@1952
   681
	for (iter=m_functions.begin(); iter!=m_functions.end(); iter++)
ds@1952
   682
	{
ds@1952
   683
		if ((*iter) == func)
ds@1952
   684
		{
ds@1952
   685
			found = true;
ds@1952
   686
			lst->erase(iter);
ds@1952
   687
			break;
ds@1952
   688
		}
ds@1952
   689
	}
ds@1952
   690
ds@1952
   691
	/* Cancel a call, if any */
ds@1952
   692
	if (found || m_curparam)
ds@1952
   693
	{
ds@1952
   694
		func->Cancel();
ds@1952
   695
	}
ds@1952
   696
ds@1952
   697
	return found;
ds@1952
   698
}
ds@1952
   699
ds@1952
   700
unsigned int CForward::RemoveFunctionsOfPlugin(IPlugin *plugin)
ds@1952
   701
{
ds@1952
   702
	FuncIter iter;
ds@1952
   703
	IPluginFunction *func;
ds@1952
   704
	unsigned int removed = 0;
ds@1952
   705
	IPluginContext *pContext = plugin->GetBaseContext();
ds@1952
   706
	for (iter=m_functions.begin(); iter!=m_functions.end();)
ds@1952
   707
	{
ds@1952
   708
		func = (*iter);
ds@1952
   709
		if (func->GetParentContext() == pContext)
ds@1952
   710
		{
ds@1952
   711
			iter = m_functions.erase(iter);
ds@1952
   712
			removed++;
ds@1952
   713
		} else {
ds@1952
   714
			iter++;
ds@1952
   715
		}
ds@1952
   716
	}
ds@1952
   717
ds@1952
   718
	return removed;
ds@1952
   719
}
ds@1952
   720
ds@1952
   721
bool CForward::AddFunction(IPluginFunction *func)
ds@1952
   722
{
ds@1952
   723
	if (m_curparam)
ds@1952
   724
	{
ds@1952
   725
		return false;
ds@1952
   726
	}
ds@1952
   727
ds@1952
   728
	if (func->IsRunnable())
ds@1952
   729
	{
ds@1952
   730
		m_functions.push_back(func);
ds@1952
   731
	} else {
ds@1952
   732
		m_paused.push_back(func);
ds@1952
   733
	}
ds@1952
   734
ds@1952
   735
	return true;
ds@1952
   736
}
ds@1952
   737
ds@1952
   738
const char *CForward::GetForwardName()
ds@1952
   739
{
ds@1952
   740
	return m_name;
ds@1952
   741
}
ds@1952
   742
ds@1952
   743
unsigned int CForward::GetFunctionCount()
ds@1952
   744
{
ds@1952
   745
	return m_functions.size();
ds@1952
   746
}
ds@1952
   747
ds@1952
   748
ExecType CForward::GetExecType()
ds@1952
   749
{
ds@1952
   750
	return m_ExecType;
ds@1952
   751
}