core/logic_bridge.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Tue Mar 19 11:18:44 2013 -0400 (2013-03-19)
changeset 3853 3c8f57923db5
parent 3800 21dfb9897341
child 3872 4c93f74cae51
permissions -rw-r--r--
Added basic support for Dota 2 (bug 5656, r=asherkin).
     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 "PlayerManager.h"
    49 #include "AdminCache.h"
    50 #include "HalfLife2.h"
    51 #include "CoreConfig.h"
    52 
    53 #if defined _WIN32
    54 	#define MATCHMAKINGDS_SUFFIX	""
    55 	#define MATCHMAKINGDS_EXT	"dll"
    56 #elif defined __APPLE__
    57 	#define MATCHMAKINGDS_SUFFIX	""
    58 	#define MATCHMAKINGDS_EXT	"dylib"
    59 #elif defined __linux__
    60 #if SOURCE_ENGINE < SE_LEFT4DEAD2
    61 	#define MATCHMAKINGDS_SUFFIX	"_i486"
    62 #else
    63 	#define MATCHMAKINGDS_SUFFIX	""
    64 #endif
    65 	#define MATCHMAKINGDS_EXT	"so"
    66 #endif
    67 
    68 static ILibrary *g_pLogic = NULL;
    69 static LogicInitFunction logic_init_fn;
    70 
    71 IThreader *g_pThreader;
    72 ITextParsers *textparsers;
    73 sm_logic_t logicore;
    74 ITranslator *translator;
    75 
    76 class VEngineServer_Logic : public IVEngineServer_Logic
    77 {
    78 public:
    79 	virtual bool IsMapValid(const char *map)
    80 	{
    81 		return engine->IsMapValid(map);
    82 	}
    83 
    84 	virtual void ServerCommand(const char *cmd)
    85 	{
    86 		engine->ServerCommand(cmd);
    87 	}
    88 };
    89 
    90 static VEngineServer_Logic logic_engine;
    91 
    92 static void add_natives(sp_nativeinfo_t *natives)
    93 {
    94 	g_pCoreNatives->AddNatives(natives);
    95 }
    96 
    97 static ConVar *find_convar(const char *name)
    98 {
    99 	return icvar->FindVar(name);
   100 }
   101 
   102 static void log_error(const char *fmt, ...)
   103 {
   104 	va_list ap;
   105 
   106 	va_start(ap, fmt);
   107 	g_Logger.LogErrorEx(fmt, ap);
   108 	va_end(ap);
   109 }
   110 
   111 static void log_message(const char *fmt, ...)
   112 {
   113 	va_list ap;
   114 	va_start(ap, fmt);
   115 	g_Logger.LogMessageEx(fmt, ap);
   116 	va_end(ap);
   117 }
   118 
   119 static void log_to_file(FILE *fp, const char *fmt, ...)
   120 {
   121 	va_list ap;
   122 	va_start(ap, fmt);
   123 	g_Logger.LogToOpenFileEx(fp, fmt, ap);
   124 	va_end(ap);
   125 }
   126 
   127 static void log_to_game(const char *message)
   128 {
   129 	Engine_LogPrintWrapper(message);
   130 }
   131 
   132 static bool file_exists(const char *path)
   133 {
   134 	return basefilesystem->FileExists(path);
   135 }
   136 
   137 static const char *get_cvar_string(ConVar* cvar)
   138 {
   139 	return cvar->GetString();
   140 }
   141 
   142 static bool get_game_name(char *buffer, size_t maxlength)
   143 {
   144 	KeyValues *pGameInfo = new KeyValues("GameInfo");
   145 	if (g_HL2.KVLoadFromFile(pGameInfo, basefilesystem, "gameinfo.txt"))
   146 	{
   147 		const char *str;
   148 		if ((str = pGameInfo->GetString("game", NULL)) != NULL)
   149 		{
   150 			strncopy(buffer, str, maxlength);
   151 			return true;
   152 		}
   153 	}
   154 	pGameInfo->deleteThis();
   155 	return false;
   156 }
   157 
   158 static const char *get_game_description()
   159 {
   160 	return SERVER_CALL(GetGameDescription)();
   161 }
   162 
   163 static const char *get_source_engine_name()
   164 {
   165 #if !defined SOURCE_ENGINE
   166 # error "Unknown engine type"
   167 #endif
   168 #if SOURCE_ENGINE == SE_EPISODEONE
   169 	return "original";
   170 #elif SOURCE_ENGINE == SE_DARKMESSIAH
   171 	return "darkmessiah";
   172 #elif SOURCE_ENGINE == SE_ORANGEBOX
   173 	return "orangebox";
   174 #elif SOURCE_ENGINE == SE_BLOODYGOODTIME
   175 	return "bloodygoodtime";
   176 #elif SOURCE_ENGINE == SE_EYE
   177 	return "eye";
   178 #elif SOURCE_ENGINE == SE_CSS
   179 	return "css";
   180 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
   181 	return "orangebox_valve";
   182 #elif SOURCE_ENGINE == SE_LEFT4DEAD
   183 	return "left4dead";
   184 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
   185 	return "left4dead2";
   186 #elif SOURCE_ENGINE == SE_ALIENSWARM
   187 	return "alienswarm";
   188 #elif SOURCE_ENGINE == SE_PORTAL2
   189 	return "portal2";
   190 #elif SOURCE_ENGINE == SE_CSGO
   191 	return "csgo";
   192 #elif SOURCE_ENGINE == SE_DOTA
   193 	return "dota";
   194 #endif
   195 }
   196 
   197 static bool symbols_are_hidden()
   198 {
   199 #if (SOURCE_ENGINE == SE_CSS) || (SOURCE_ENGINE == SE_ORANGEBOXVALVE) || (SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2) || (SOURCE_ENGINE == SE_CSGO) || (SOURCE_ENGINE == SE_DOTA)
   200 	return true;
   201 #else
   202 	return false;
   203 #endif
   204 }
   205 
   206 static const char* get_core_config_value(const char* key)
   207 {
   208 	return g_CoreConfig.GetCoreConfigValue(key);
   209 }	
   210 
   211 static ServerGlobals serverGlobals;
   212 
   213 static sm_core_t core_bridge =
   214 {
   215 	/* Objects */
   216 	&g_HandleSys,
   217 	NULL,
   218 	&g_SourceMod,
   219 	&g_LibSys,
   220 	reinterpret_cast<IVEngineServer*>(&logic_engine),
   221 	&g_ShareSys,
   222 	&g_RootMenu,
   223 	&g_PluginSys,
   224 	&g_Forwards,
   225 	&g_Timers,
   226 	&g_Players,
   227 	&g_Admins,
   228 	&g_HL2,
   229 	&g_pSourcePawn,
   230 	&g_pSourcePawn2,
   231 	/* Functions */
   232 	add_natives,
   233 	find_convar,
   234 	strncopy,
   235 	UTIL_TrimWhitespace,
   236 	log_error,
   237 	log_message,
   238 	log_to_file,
   239 	log_to_game,
   240 	file_exists,
   241 	get_cvar_string,
   242 	UTIL_Format,
   243 	UTIL_FormatArgs,
   244 	gnprintf,
   245 	atcprintf,
   246 	get_game_name,
   247 	get_game_description,
   248 	get_source_engine_name,
   249 	symbols_are_hidden,
   250 	get_core_config_value,
   251 	&serverGlobals
   252 };
   253 
   254 void InitLogicBridge()
   255 {
   256 	serverGlobals.universalTime = g_pUniversalTime;
   257 	serverGlobals.frametime = &gpGlobals->frametime;
   258 	serverGlobals.interval_per_tick = &gpGlobals->interval_per_tick;
   259 
   260 	core_bridge.core_ident = g_pCoreIdent;
   261 	core_bridge.engineFactory = (void *)g_SMAPI->GetEngineFactory(false);
   262 	core_bridge.serverFactory = (void *)g_SMAPI->GetServerFactory(false);
   263 
   264 	ILibrary *mmlib;
   265 	char path[PLATFORM_MAX_PATH];
   266 
   267 	g_LibSys.PathFormat(path, sizeof(path), "%s/bin/matchmaking_ds%s.%s", g_SMAPI->GetBaseDir(), MATCHMAKINGDS_SUFFIX, MATCHMAKINGDS_EXT);
   268 
   269 	if ((mmlib = g_LibSys.OpenLibrary(path, NULL, 0)))
   270 	{
   271 		core_bridge.matchmakingDSFactory = mmlib->GetSymbolAddress("CreateInterface");
   272 		mmlib->CloseLibrary();
   273 	}
   274 	
   275 	logic_init_fn(&core_bridge, &logicore);
   276 
   277 	/* Add SMGlobalClass instances */
   278 	SMGlobalClass* glob = SMGlobalClass::head;
   279 	while (glob->m_pGlobalClassNext != NULL)
   280 	{
   281 		glob = glob->m_pGlobalClassNext;
   282 	}
   283 	assert(glob->m_pGlobalClassNext == NULL);
   284 	glob->m_pGlobalClassNext = logicore.head;
   285 
   286 	g_pThreader = logicore.threader;
   287 	g_pSourcePawn2->SetProfiler(logicore.profiler);
   288 	translator = logicore.translator;
   289 }
   290 
   291 bool StartLogicBridge(char *error, size_t maxlength)
   292 {
   293 	char file[PLATFORM_MAX_PATH];
   294 
   295 	/* Now it's time to load the logic binary */
   296 	g_SMAPI->PathFormat(file,
   297 		sizeof(file),
   298 		"%s/bin/sourcemod.logic." PLATFORM_LIB_EXT,
   299 		g_SourceMod.GetSourceModPath());
   300 
   301 	char myerror[255];
   302 	g_pLogic = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
   303 
   304 	if (!g_pLogic)
   305 	{
   306 		if (error && maxlength)
   307 		{
   308 			UTIL_Format(error, maxlength, "failed to load %s: %s", file, myerror);
   309 		}
   310 		return false;
   311 	}
   312 
   313 	LogicLoadFunction llf = (LogicLoadFunction)g_pLogic->GetSymbolAddress("logic_load");
   314 	if (llf == NULL)
   315 	{
   316 		g_pLogic->CloseLibrary();
   317 		if (error && maxlength)
   318 		{
   319 			UTIL_Format(error, maxlength, "could not find logic_load function");
   320 		}
   321 		return false;
   322 	}
   323 
   324 	GetITextParsers getitxt = (GetITextParsers)g_pLogic->GetSymbolAddress("get_textparsers");
   325 	textparsers = getitxt();
   326 
   327 	logic_init_fn = llf(SM_LOGIC_MAGIC);
   328 
   329 	return true;
   330 }
   331 
   332 void ShutdownLogicBridge()
   333 {
   334 	g_pLogic->CloseLibrary();
   335 }
   336