core/logic/NativeInvoker.h
author Nicholas Hastings <skamonkey@gmail.com>
Thu Jul 04 11:28:32 2013 -0400 (2013-07-04)
changeset 3931 576fa42454dc
parent 3872 4c93f74cae51
child 4106 a39557bde4ac
permissions -rw-r--r--
Fixed build for real.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2009 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_NATIVE_INVOKER_H_
    33 #define _INCLUDE_SOURCEMOD_NATIVE_INVOKER_H_
    34 
    35 #include <sp_vm_api.h>
    36 #include <INativeInvoker.h>
    37 #include "common_logic.h"
    38 
    39 using namespace SourceMod;
    40 using namespace SourcePawn;
    41 
    42 struct ParamInfo
    43 {
    44 	int flags;			/* Copy-back flags */
    45 	bool marked;		/* Whether this is marked as being used */
    46 	cell_t local_addr;	/* Local address to free */
    47 	cell_t *phys_addr;	/* Physical address of our copy */
    48 	cell_t *orig_addr;	/* Original address to copy back to */
    49 	ucell_t size;		/* Size of array in bytes */
    50 	struct
    51 	{
    52 		bool is_sz;		/* is a string */
    53 		int sz_flags;	/* has sz flags */
    54 	} str;
    55 };
    56 
    57 class NativeInvoker : public INativeInvoker
    58 {
    59 public:
    60 	NativeInvoker();
    61 	~NativeInvoker();
    62 public: /* ICallable */
    63 	int PushCell(cell_t cell);
    64 	int PushCellByRef(cell_t *cell, int flags=SM_PARAM_COPYBACK);
    65 	int PushFloat(float number);
    66 	int PushFloatByRef(float *number, int flags=SM_PARAM_COPYBACK);
    67 	int PushArray(cell_t *inarray, unsigned int cells, int flags=0);
    68 	int PushString(const char *string);
    69 	int PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags);
    70 	void Cancel();
    71 public: /* INativeInvoker */
    72 	bool Start(IPluginContext *pContext, const char *name);
    73 	int Invoke(cell_t *result);
    74 private:
    75 	int _PushString(const char *string, int sz_flags, int cp_flags, size_t len);
    76 	int SetError(int err);
    77 private:
    78 	IPluginContext *pContext;
    79 	SPVM_NATIVE_FUNC native;
    80 	cell_t m_params[SP_MAX_EXEC_PARAMS];
    81 	ParamInfo m_info[SP_MAX_EXEC_PARAMS];
    82 	unsigned int m_curparam;
    83 	int m_errorstate;
    84 };
    85 
    86 class NativeInterface : 
    87 	public INativeInterface,
    88 	public SMGlobalClass
    89 {
    90 public: /* SMGlobalClass */
    91 	void OnSourceModAllInitialized();
    92 public: /* SMInterface */
    93 	unsigned int GetInterfaceVersion();
    94 	const char *GetInterfaceName();
    95 public: /* INativeInvoker */
    96 	IPluginRuntime *CreateRuntime(const char *name, size_t bytes);
    97 	INativeInvoker *CreateInvoker();
    98 };
    99 
   100 extern NativeInterface g_NInvoke;
   101 
   102 #endif /* _INCLUDE_SOURCEMOD_NATIVE_INVOKER_H_ */