core/logic_bridge.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed Nov 24 20:46:17 2010 -0500 (2010-11-24)
changeset 3158 f57d6a76bb31
parent 3143 5a4eb65a5cea
child 3180 61db674548aa
permissions -rw-r--r--
Fixed TF2 GetHoliday Windows signature (bug 4714, r=fyren).
     1 /**
     2  * vim: set ts=4 sw=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2010 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 #include "PlayerManager.h"
    50 #include "AdminCache.h"
    51 #include "HalfLife2.h"
    52 
    53 static ILibrary *g_pLogic = NULL;
    54 static LogicInitFunction logic_init_fn;
    55 
    56 IThreader *g_pThreader;
    57 ITextParsers *textparsers;
    58 sm_logic_t logicore;
    59 ITranslator *translator;
    60 
    61 class VEngineServer_Logic : public IVEngineServer_Logic
    62 {
    63 public:
    64 	virtual bool IsMapValid(const char *map)
    65 	{
    66 		return engine->IsMapValid(map);
    67 	}
    68 
    69 	virtual void ServerCommand(const char *cmd)
    70 	{
    71 		engine->ServerCommand(cmd);
    72 	}
    73 };
    74 
    75 static VEngineServer_Logic logic_engine;
    76 
    77 static void add_natives(sp_nativeinfo_t *natives)
    78 {
    79 	g_pCoreNatives->AddNatives(natives);
    80 }
    81 
    82 static ConVar *find_convar(const char *name)
    83 {
    84 	return icvar->FindVar(name);
    85 }
    86 
    87 static void log_error(const char *fmt, ...)
    88 {
    89 	va_list ap;
    90 
    91 	va_start(ap, fmt);
    92 	g_Logger.LogErrorEx(fmt, ap);
    93 	va_end(ap);
    94 }
    95 
    96 static void generate_error(IPluginContext *ctx, cell_t func_idx, int err, const char *fmt, ...)
    97 {
    98 	va_list ap;
    99 	
   100 	va_start(ap, fmt);
   101 	g_DbgReporter.GenerateErrorVA(ctx, func_idx, err, fmt, ap);
   102 	va_end(ap);
   103 }
   104 
   105 static const char *get_cvar_string(ConVar* cvar)
   106 {
   107 	return cvar->GetString();
   108 }
   109 
   110 static bool get_game_name(char *buffer, size_t maxlength)
   111 {
   112 	KeyValues *pGameInfo = new KeyValues("GameInfo");
   113 	if (g_HL2.KVLoadFromFile(pGameInfo, basefilesystem, "gameinfo.txt"))
   114 	{
   115 		const char *str;
   116 		if ((str = pGameInfo->GetString("game", NULL)) != NULL)
   117 		{
   118 			strncopy(buffer, str, maxlength);
   119 			return true;
   120 		}
   121 	}
   122 	pGameInfo->deleteThis();
   123 	return false;
   124 }
   125 
   126 static const char *get_game_description()
   127 {
   128 	return SERVER_CALL(GetGameDescription)();
   129 }
   130 
   131 static const char *get_source_engine_name()
   132 {
   133 #if !defined SOURCE_ENGINE
   134 # error "Unknown engine type"
   135 #endif
   136 #if SOURCE_ENGINE == SE_EPISODEONE
   137 	return "original";
   138 #elif SOURCE_ENGINE == SE_DARKMESSIAH
   139 	return "darkmessiah";
   140 #elif SOURCE_ENGINE == SE_ORANGEBOX
   141 	return "orangebox";
   142 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
   143 	return "orangebox_valve";
   144 #elif SOURCE_ENGINE == SE_LEFT4DEAD
   145 	return "left4dead";
   146 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
   147 	return "left4dead2";
   148 #elif SOURCE_ENGINE == SE_ALIENSWARM
   149 	return "alienswarm";
   150 #endif
   151 }
   152 
   153 static bool symbols_are_hidden()
   154 {
   155 #if (SOURCE_ENGINE == SE_ORANGEBOXVALVE) || (SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
   156 	return true;
   157 #else
   158 	return false;
   159 #endif
   160 }
   161 
   162 static ServerGlobals serverGlobals;
   163 
   164 static sm_core_t core_bridge =
   165 {
   166 	/* Objects */
   167 	&g_HandleSys,
   168 	NULL,
   169 	&g_SourceMod,
   170 	&g_LibSys,
   171 	reinterpret_cast<IVEngineServer*>(&logic_engine),
   172 	&g_ShareSys,
   173 	&g_RootMenu,
   174 	&g_PluginSys,
   175 	&g_Forwards,
   176 	&g_Timers,
   177 	&g_Players,
   178 	&g_Admins,
   179 	&g_HL2,
   180 	/* Functions */
   181 	add_natives,
   182 	find_convar,
   183 	strncopy,
   184 	UTIL_TrimWhitespace,
   185 	log_error,
   186 	get_cvar_string,
   187 	UTIL_Format,
   188 	generate_error,
   189 	gnprintf,
   190 	atcprintf,
   191 	get_game_name,
   192 	get_game_description,
   193 	get_source_engine_name,
   194 	symbols_are_hidden,
   195 	&serverGlobals
   196 };
   197 
   198 void InitLogicBridge()
   199 {
   200 	serverGlobals.universalTime = g_pUniversalTime;
   201 	serverGlobals.frametime = &gpGlobals->frametime;
   202 	serverGlobals.interval_per_tick = &gpGlobals->interval_per_tick;
   203 
   204 	core_bridge.core_ident = g_pCoreIdent;
   205 	core_bridge.engineFactory = (void *)g_SMAPI->GetEngineFactory(false);
   206 	core_bridge.serverFactory = (void *)g_SMAPI->GetServerFactory(false);
   207 	logic_init_fn(&core_bridge, &logicore);
   208 
   209 	/* Add SMGlobalClass instances */
   210 	SMGlobalClass* glob = SMGlobalClass::head;
   211 	while (glob->m_pGlobalClassNext != NULL)
   212 	{
   213 		glob = glob->m_pGlobalClassNext;
   214 	}
   215 	assert(glob->m_pGlobalClassNext == NULL);
   216 	glob->m_pGlobalClassNext = logicore.head;
   217 
   218 	g_pThreader = logicore.threader;
   219 	g_pSourcePawn2->SetProfiler(logicore.profiler);
   220 	translator = logicore.translator;
   221 }
   222 
   223 bool StartLogicBridge(char *error, size_t maxlength)
   224 {
   225 	char file[PLATFORM_MAX_PATH];
   226 
   227 	/* Now it's time to load the logic binary */
   228 	g_SMAPI->PathFormat(file,
   229 		sizeof(file),
   230 		"%s/bin/sourcemod.logic." PLATFORM_LIB_EXT,
   231 		g_SourceMod.GetSourceModPath());
   232 
   233 	char myerror[255];
   234 	g_pLogic = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
   235 
   236 	if (!g_pLogic)
   237 	{
   238 		if (error && maxlength)
   239 		{
   240 			UTIL_Format(error, maxlength, "failed to load %s: %s", file, myerror);
   241 		}
   242 		return false;
   243 	}
   244 
   245 	LogicLoadFunction llf = (LogicLoadFunction)g_pLogic->GetSymbolAddress("logic_load");
   246 	if (llf == NULL)
   247 	{
   248 		g_pLogic->CloseLibrary();
   249 		if (error && maxlength)
   250 		{
   251 			UTIL_Format(error, maxlength, "could not find logic_load function");
   252 		}
   253 		return false;
   254 	}
   255 
   256 	GetITextParsers getitxt = (GetITextParsers)g_pLogic->GetSymbolAddress("get_textparsers");
   257 	textparsers = getitxt();
   258 
   259 	logic_init_fn = llf(SM_LOGIC_MAGIC);
   260 
   261 	return true;
   262 }
   263 
   264 void ShutdownLogicBridge()
   265 {
   266 	g_pLogic->CloseLibrary();
   267 }
   268