core/logic_bridge.cpp
author David Anderson <dvander@alliedmods.net>
Mon Sep 07 21:29:18 2009 -0700 (2009-09-07)
changeset 2798 6dd90eaa0593
parent 2730 a2ff0a7a1c53
child 2884 be328dae7d3a
permissions -rw-r--r--
Fixed invalid array access in PluginSys (bug 3933, r=pred).
     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 
    47 static ILibrary *g_pLogic = NULL;
    48 static LogicInitFunction logic_init_fn;
    49 
    50 IThreader *g_pThreader;
    51 ITextParsers *textparsers;
    52 
    53 class VEngineServer_Logic : public IVEngineServer_Logic
    54 {
    55 public:
    56 	virtual bool IsMapValid(const char *map)
    57 	{
    58 		return engine->IsMapValid(map);
    59 	}
    60 };
    61 
    62 static VEngineServer_Logic logic_engine;
    63 
    64 static void add_natives(sp_nativeinfo_t *natives)
    65 {
    66 	g_pCoreNatives->AddNatives(natives);
    67 }
    68 
    69 static ConVar *find_convar(const char *name)
    70 {
    71 	return icvar->FindVar(name);
    72 }
    73 
    74 static void log_error(const char *fmt, ...)
    75 {
    76 	va_list ap;
    77 
    78 	va_start(ap, fmt);
    79 	g_Logger.LogErrorEx(fmt, ap);
    80 	va_end(ap);
    81 }
    82 
    83 static const char *get_cvar_string(ConVar* cvar)
    84 {
    85 	return cvar->GetString();
    86 }
    87 
    88 static sm_core_t core_bridge =
    89 {
    90 	/* Objects */
    91 	&g_HandleSys,
    92 	NULL,
    93 	&g_SourceMod,
    94 	&g_LibSys,
    95 	reinterpret_cast<IVEngineServer*>(&logic_engine),
    96 	&g_ShareSys,
    97 	&g_RootMenu,
    98 	&g_PluginSys,
    99 	&g_Forwards,
   100 	/* Functions */
   101 	add_natives,
   102 	find_convar,
   103 	strncopy,
   104 	UTIL_TrimWhitespace,
   105 	log_error,
   106 	get_cvar_string,
   107 	UTIL_Format
   108 };
   109 
   110 void InitLogicBridge()
   111 {
   112 	sm_logic_t logic;
   113 
   114 	core_bridge.core_ident = g_pCoreIdent;
   115 	logic_init_fn(&core_bridge, &logic);
   116 
   117 	/* Add SMGlobalClass instances */
   118 	SMGlobalClass* glob = SMGlobalClass::head;
   119 	while (glob->m_pGlobalClassNext != NULL)
   120 	{
   121 		glob = glob->m_pGlobalClassNext;
   122 	}
   123 	assert(glob->m_pGlobalClassNext == NULL);
   124 	glob->m_pGlobalClassNext = logic.head;
   125 
   126 	g_pThreader = logic.threader;
   127 	g_pSourcePawn2->SetProfiler(logic.profiler);
   128 }
   129 
   130 bool StartLogicBridge(char *error, size_t maxlength)
   131 {
   132 	char file[PLATFORM_MAX_PATH];
   133 
   134 	/* Now it's time to load the logic binary */
   135 	g_SMAPI->PathFormat(file,
   136 		sizeof(file),
   137 		"%s/bin/sourcemod.logic." PLATFORM_LIB_EXT,
   138 		g_SourceMod.GetSourceModPath());
   139 
   140 	char myerror[255];
   141 	g_pLogic = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
   142 
   143 	if (!g_pLogic)
   144 	{
   145 		if (error && maxlength)
   146 		{
   147 			UTIL_Format(error, maxlength, "failed to load %s: %s", file, myerror);
   148 		}
   149 		return false;
   150 	}
   151 
   152 	LogicLoadFunction llf = (LogicLoadFunction)g_pLogic->GetSymbolAddress("logic_load");
   153 	if (llf == NULL)
   154 	{
   155 		g_pLogic->CloseLibrary();
   156 		if (error && maxlength)
   157 		{
   158 			UTIL_Format(error, maxlength, "could not find logic_load function");
   159 		}
   160 		return false;
   161 	}
   162 
   163 	GetITextParsers getitxt = (GetITextParsers)g_pLogic->GetSymbolAddress("get_textparsers");
   164 	textparsers = getitxt();
   165 
   166 	logic_init_fn = llf(SM_LOGIC_MAGIC);
   167 
   168 	return true;
   169 }
   170 
   171 void ShutdownLogicBridge()
   172 {
   173 	g_pLogic->CloseLibrary();
   174 }
   175