core/logic_bridge.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu May 13 04:23:43 2010 -0500 (2010-05-13)
changeset 2994 1ef5e4e002e2
parent 2989 e8b0605226ce
child 2996 45be5b449233
permissions -rw-r--r--
Appended OS names to package archives (bug 4395, 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 "TimerSys.h"
    47 #include "logic_bridge.h"
    48 #include "DebugReporter.h"
    49 
    50 static ILibrary *g_pLogic = NULL;
    51 static LogicInitFunction logic_init_fn;
    52 
    53 IThreader *g_pThreader;
    54 ITextParsers *textparsers;
    55 SM_FN_CRC32 UTIL_CRC32;
    56 
    57 class VEngineServer_Logic : public IVEngineServer_Logic
    58 {
    59 public:
    60 	virtual bool IsMapValid(const char *map)
    61 	{
    62 		return engine->IsMapValid(map);
    63 	}
    64 };
    65 
    66 static VEngineServer_Logic logic_engine;
    67 
    68 static void add_natives(sp_nativeinfo_t *natives)
    69 {
    70 	g_pCoreNatives->AddNatives(natives);
    71 }
    72 
    73 static ConVar *find_convar(const char *name)
    74 {
    75 	return icvar->FindVar(name);
    76 }
    77 
    78 static void log_error(const char *fmt, ...)
    79 {
    80 	va_list ap;
    81 
    82 	va_start(ap, fmt);
    83 	g_Logger.LogErrorEx(fmt, ap);
    84 	va_end(ap);
    85 }
    86 
    87 static void generate_error(IPluginContext *ctx, cell_t func_idx, int err, const char *fmt, ...)
    88 {
    89 	va_list ap;
    90 	
    91 	va_start(ap, fmt);
    92 	g_DbgReporter.GenerateErrorVA(ctx, func_idx, err, fmt, ap);
    93 	va_end(ap);
    94 }
    95 
    96 static const char *get_cvar_string(ConVar* cvar)
    97 {
    98 	return cvar->GetString();
    99 }
   100 
   101 static ServerGlobals serverGlobals;
   102 
   103 static sm_core_t core_bridge =
   104 {
   105 	/* Objects */
   106 	&g_HandleSys,
   107 	NULL,
   108 	&g_SourceMod,
   109 	&g_LibSys,
   110 	reinterpret_cast<IVEngineServer*>(&logic_engine),
   111 	&g_ShareSys,
   112 	&g_RootMenu,
   113 	&g_PluginSys,
   114 	&g_Forwards,
   115 	&g_Timers,
   116 	/* Functions */
   117 	add_natives,
   118 	find_convar,
   119 	strncopy,
   120 	UTIL_TrimWhitespace,
   121 	log_error,
   122 	get_cvar_string,
   123 	UTIL_Format,
   124 	UTIL_ReplaceAll,
   125 	generate_error,
   126 	&serverGlobals
   127 };
   128 
   129 void InitLogicBridge()
   130 {
   131 	sm_logic_t logic;
   132 
   133 	serverGlobals.universalTime = g_pUniversalTime;
   134 	serverGlobals.frametime = &gpGlobals->frametime;
   135 	serverGlobals.interval_per_tick = &gpGlobals->interval_per_tick;
   136 
   137 	core_bridge.core_ident = g_pCoreIdent;
   138 	logic_init_fn(&core_bridge, &logic);
   139 
   140 	/* Add SMGlobalClass instances */
   141 	SMGlobalClass* glob = SMGlobalClass::head;
   142 	while (glob->m_pGlobalClassNext != NULL)
   143 	{
   144 		glob = glob->m_pGlobalClassNext;
   145 	}
   146 	assert(glob->m_pGlobalClassNext == NULL);
   147 	glob->m_pGlobalClassNext = logic.head;
   148 
   149 	g_pThreader = logic.threader;
   150 	g_pSourcePawn2->SetProfiler(logic.profiler);
   151 	UTIL_CRC32 = logic.CRC32;
   152 }
   153 
   154 bool StartLogicBridge(char *error, size_t maxlength)
   155 {
   156 	char file[PLATFORM_MAX_PATH];
   157 
   158 	/* Now it's time to load the logic binary */
   159 	g_SMAPI->PathFormat(file,
   160 		sizeof(file),
   161 		"%s/bin/sourcemod.logic." PLATFORM_LIB_EXT,
   162 		g_SourceMod.GetSourceModPath());
   163 
   164 	char myerror[255];
   165 	g_pLogic = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
   166 
   167 	if (!g_pLogic)
   168 	{
   169 		if (error && maxlength)
   170 		{
   171 			UTIL_Format(error, maxlength, "failed to load %s: %s", file, myerror);
   172 		}
   173 		return false;
   174 	}
   175 
   176 	LogicLoadFunction llf = (LogicLoadFunction)g_pLogic->GetSymbolAddress("logic_load");
   177 	if (llf == NULL)
   178 	{
   179 		g_pLogic->CloseLibrary();
   180 		if (error && maxlength)
   181 		{
   182 			UTIL_Format(error, maxlength, "could not find logic_load function");
   183 		}
   184 		return false;
   185 	}
   186 
   187 	GetITextParsers getitxt = (GetITextParsers)g_pLogic->GetSymbolAddress("get_textparsers");
   188 	textparsers = getitxt();
   189 
   190 	logic_init_fn = llf(SM_LOGIC_MAGIC);
   191 
   192 	return true;
   193 }
   194 
   195 void ShutdownLogicBridge()
   196 {
   197 	g_pLogic->CloseLibrary();
   198 }
   199