core/logic_bridge.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Feb 08 19:57:20 2010 -0800 (2010-02-08)
changeset 2914 bf5f9aa53abe
parent 2906 33cc9854bd97
child 2989 e8b0605226ce
permissions -rw-r--r--
Fixed FindEntityByClassname crash in Synergy (bug 4235, r=dvander).
     1 /**
     2  * vim: set ts=4 sw=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 #include <stdio.h>
    32 #include <assert.h>
    33 #include "sourcemod.h"
    34 #include "sourcemm_api.h"
    35 #include "sm_globals.h"
    36 #include "NativeOwner.h"
    37 #include "sm_autonatives.h"
    38 #include "logic/intercom.h"
    39 #include "LibrarySys.h"
    40 #include "HandleSys.h"
    41 #include "sm_stringutil.h"
    42 #include "Logger.h"
    43 #include "ShareSys.h"
    44 #include "sm_srvcmds.h"
    45 #include "ForwardSys.h"
    46 #include "logic_bridge.h"
    47 
    48 static ILibrary *g_pLogic = NULL;
    49 static LogicInitFunction logic_init_fn;
    50 
    51 IThreader *g_pThreader;
    52 ITextParsers *textparsers;
    53 SM_FN_CRC32 UTIL_CRC32;
    54 
    55 class VEngineServer_Logic : public IVEngineServer_Logic
    56 {
    57 public:
    58 	virtual bool IsMapValid(const char *map)
    59 	{
    60 		return engine->IsMapValid(map);
    61 	}
    62 };
    63 
    64 static VEngineServer_Logic logic_engine;
    65 
    66 static void add_natives(sp_nativeinfo_t *natives)
    67 {
    68 	g_pCoreNatives->AddNatives(natives);
    69 }
    70 
    71 static ConVar *find_convar(const char *name)
    72 {
    73 	return icvar->FindVar(name);
    74 }
    75 
    76 static void log_error(const char *fmt, ...)
    77 {
    78 	va_list ap;
    79 
    80 	va_start(ap, fmt);
    81 	g_Logger.LogErrorEx(fmt, ap);
    82 	va_end(ap);
    83 }
    84 
    85 static const char *get_cvar_string(ConVar* cvar)
    86 {
    87 	return cvar->GetString();
    88 }
    89 
    90 static sm_core_t core_bridge =
    91 {
    92 	/* Objects */
    93 	&g_HandleSys,
    94 	NULL,
    95 	&g_SourceMod,
    96 	&g_LibSys,
    97 	reinterpret_cast<IVEngineServer*>(&logic_engine),
    98 	&g_ShareSys,
    99 	&g_RootMenu,
   100 	&g_PluginSys,
   101 	&g_Forwards,
   102 	/* Functions */
   103 	add_natives,
   104 	find_convar,
   105 	strncopy,
   106 	UTIL_TrimWhitespace,
   107 	log_error,
   108 	get_cvar_string,
   109 	UTIL_Format,
   110 	UTIL_ReplaceAll
   111 };
   112 
   113 void InitLogicBridge()
   114 {
   115 	sm_logic_t logic;
   116 
   117 	core_bridge.core_ident = g_pCoreIdent;
   118 	logic_init_fn(&core_bridge, &logic);
   119 
   120 	/* Add SMGlobalClass instances */
   121 	SMGlobalClass* glob = SMGlobalClass::head;
   122 	while (glob->m_pGlobalClassNext != NULL)
   123 	{
   124 		glob = glob->m_pGlobalClassNext;
   125 	}
   126 	assert(glob->m_pGlobalClassNext == NULL);
   127 	glob->m_pGlobalClassNext = logic.head;
   128 
   129 	g_pThreader = logic.threader;
   130 	g_pSourcePawn2->SetProfiler(logic.profiler);
   131 	UTIL_CRC32 = logic.CRC32;
   132 }
   133 
   134 bool StartLogicBridge(char *error, size_t maxlength)
   135 {
   136 	char file[PLATFORM_MAX_PATH];
   137 
   138 	/* Now it's time to load the logic binary */
   139 	g_SMAPI->PathFormat(file,
   140 		sizeof(file),
   141 		"%s/bin/sourcemod.logic." PLATFORM_LIB_EXT,
   142 		g_SourceMod.GetSourceModPath());
   143 
   144 	char myerror[255];
   145 	g_pLogic = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
   146 
   147 	if (!g_pLogic)
   148 	{
   149 		if (error && maxlength)
   150 		{
   151 			UTIL_Format(error, maxlength, "failed to load %s: %s", file, myerror);
   152 		}
   153 		return false;
   154 	}
   155 
   156 	LogicLoadFunction llf = (LogicLoadFunction)g_pLogic->GetSymbolAddress("logic_load");
   157 	if (llf == NULL)
   158 	{
   159 		g_pLogic->CloseLibrary();
   160 		if (error && maxlength)
   161 		{
   162 			UTIL_Format(error, maxlength, "could not find logic_load function");
   163 		}
   164 		return false;
   165 	}
   166 
   167 	GetITextParsers getitxt = (GetITextParsers)g_pLogic->GetSymbolAddress("get_textparsers");
   168 	textparsers = getitxt();
   169 
   170 	logic_init_fn = llf(SM_LOGIC_MAGIC);
   171 
   172 	return true;
   173 }
   174 
   175 void ShutdownLogicBridge()
   176 {
   177 	g_pLogic->CloseLibrary();
   178 }
   179