core/logic_bridge.cpp
author David Anderson <dvander@alliedmods.net>
Fri Mar 29 11:37:29 2013 -0700 (2013-03-29)
changeset 3872 4c93f74cae51
parent 3853 3c8f57923db5
child 3874 5de15a26a168
permissions -rw-r--r--
Move scared guts of core into logic (bug 5680, 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 "sm_autonatives.h"
    37 #include "logic/intercom.h"
    38 #include "LibrarySys.h"
    39 #include "sm_stringutil.h"
    40 #include "Logger.h"
    41 #include "sm_srvcmds.h"
    42 #include "ForwardSys.h"
    43 #include "TimerSys.h"
    44 #include "logic_bridge.h"
    45 #include "PlayerManager.h"
    46 #include "AdminCache.h"
    47 #include "HalfLife2.h"
    48 #include "CoreConfig.h"
    49 #if SOURCE_ENGINE >= SE_ALIENSWARM
    50 #include "convar_sm_swarm.h"
    51 #elif SOURCE_ENGINE >= SE_LEFT4DEAD
    52 #include "convar_sm_l4d.h"
    53 #elif SOURCE_ENGINE >= SE_ORANGEBOX
    54 #include "convar_sm_ob.h"
    55 #else
    56 #include "convar_sm.h"
    57 #endif
    58 
    59 #if defined _WIN32
    60 	#define MATCHMAKINGDS_SUFFIX	""
    61 	#define MATCHMAKINGDS_EXT	"dll"
    62 #elif defined __APPLE__
    63 	#define MATCHMAKINGDS_SUFFIX	""
    64 	#define MATCHMAKINGDS_EXT	"dylib"
    65 #elif defined __linux__
    66 #if SOURCE_ENGINE < SE_LEFT4DEAD2
    67 	#define MATCHMAKINGDS_SUFFIX	"_i486"
    68 #else
    69 	#define MATCHMAKINGDS_SUFFIX	""
    70 #endif
    71 	#define MATCHMAKINGDS_EXT	"so"
    72 #endif
    73 
    74 static ILibrary *g_pLogic = NULL;
    75 static LogicInitFunction logic_init_fn;
    76 
    77 IThreader *g_pThreader;
    78 ITextParsers *textparsers;
    79 sm_logic_t logicore;
    80 ITranslator *translator;
    81 IScriptManager *scripts;
    82 IShareSys *sharesys;
    83 IExtensionSys *extsys;
    84 IHandleSys *handlesys;
    85 
    86 class VEngineServer_Logic : public IVEngineServer_Logic
    87 {
    88 public:
    89 	virtual bool IsMapValid(const char *map)
    90 	{
    91 		return !!engine->IsMapValid(map);
    92 	}
    93 
    94 	virtual void ServerCommand(const char *cmd)
    95 	{
    96 		engine->ServerCommand(cmd);
    97 	}
    98 };
    99 
   100 static VEngineServer_Logic logic_engine;
   101 
   102 static ConVar *find_convar(const char *name)
   103 {
   104 	return icvar->FindVar(name);
   105 }
   106 
   107 static void log_error(const char *fmt, ...)
   108 {
   109 	va_list ap;
   110 
   111 	va_start(ap, fmt);
   112 	g_Logger.LogErrorEx(fmt, ap);
   113 	va_end(ap);
   114 }
   115 
   116 static void log_fatal(const char *fmt, ...)
   117 {
   118 	va_list ap;
   119 
   120 	va_start(ap, fmt);
   121 	g_Logger.LogFatalEx(fmt, ap);
   122 	va_end(ap);
   123 }
   124 
   125 static void log_message(const char *fmt, ...)
   126 {
   127 	va_list ap;
   128 	va_start(ap, fmt);
   129 	g_Logger.LogMessageEx(fmt, ap);
   130 	va_end(ap);
   131 }
   132 
   133 static void log_to_file(FILE *fp, const char *fmt, ...)
   134 {
   135 	va_list ap;
   136 	va_start(ap, fmt);
   137 	g_Logger.LogToOpenFileEx(fp, fmt, ap);
   138 	va_end(ap);
   139 }
   140 
   141 static void log_to_game(const char *message)
   142 {
   143 	Engine_LogPrintWrapper(message);
   144 }
   145 
   146 static bool file_exists(const char *path)
   147 {
   148 	return basefilesystem->FileExists(path);
   149 }
   150 
   151 static const char *get_cvar_string(ConVar* cvar)
   152 {
   153 	return cvar->GetString();
   154 }
   155 
   156 static bool get_game_name(char *buffer, size_t maxlength)
   157 {
   158 	KeyValues *pGameInfo = new KeyValues("GameInfo");
   159 	if (g_HL2.KVLoadFromFile(pGameInfo, basefilesystem, "gameinfo.txt"))
   160 	{
   161 		const char *str;
   162 		if ((str = pGameInfo->GetString("game", NULL)) != NULL)
   163 		{
   164 			strncopy(buffer, str, maxlength);
   165 			return true;
   166 		}
   167 	}
   168 	pGameInfo->deleteThis();
   169 	return false;
   170 }
   171 
   172 static const char *get_game_description()
   173 {
   174 	return SERVER_CALL(GetGameDescription)();
   175 }
   176 
   177 static const char *get_source_engine_name()
   178 {
   179 #if !defined SOURCE_ENGINE
   180 # error "Unknown engine type"
   181 #endif
   182 #if SOURCE_ENGINE == SE_EPISODEONE
   183 	return "original";
   184 #elif SOURCE_ENGINE == SE_DARKMESSIAH
   185 	return "darkmessiah";
   186 #elif SOURCE_ENGINE == SE_ORANGEBOX
   187 	return "orangebox";
   188 #elif SOURCE_ENGINE == SE_BLOODYGOODTIME
   189 	return "bloodygoodtime";
   190 #elif SOURCE_ENGINE == SE_EYE
   191 	return "eye";
   192 #elif SOURCE_ENGINE == SE_CSS
   193 	return "css";
   194 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
   195 	return "orangebox_valve";
   196 #elif SOURCE_ENGINE == SE_LEFT4DEAD
   197 	return "left4dead";
   198 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
   199 	return "left4dead2";
   200 #elif SOURCE_ENGINE == SE_ALIENSWARM
   201 	return "alienswarm";
   202 #elif SOURCE_ENGINE == SE_PORTAL2
   203 	return "portal2";
   204 #elif SOURCE_ENGINE == SE_CSGO
   205 	return "csgo";
   206 #elif SOURCE_ENGINE == SE_DOTA
   207 	return "dota";
   208 #endif
   209 }
   210 
   211 static bool symbols_are_hidden()
   212 {
   213 #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)
   214 	return true;
   215 #else
   216 	return false;
   217 #endif
   218 }
   219 
   220 static const char* get_core_config_value(const char* key)
   221 {
   222 	return g_CoreConfig.GetCoreConfigValue(key);
   223 }
   224 
   225 static bool is_map_loading()
   226 {
   227 	return g_SourceMod.IsMapLoading();
   228 }
   229 
   230 static bool is_map_running()
   231 {
   232 	return g_SourceMod.IsMapRunning();
   233 }
   234 
   235 int read_cmd_argc(const CCommand &args)
   236 {
   237 	return args.ArgC();
   238 }
   239 
   240 static const char *read_cmd_arg(const CCommand &args, int arg)
   241 {
   242 	return args.Arg(arg);
   243 }
   244 
   245 static int load_mms_plugin(const char *file, bool *ok, char *error, size_t maxlength)
   246 {
   247 	bool ignore_already;
   248 	PluginId id = g_pMMPlugins->Load(file, g_PLID, ignore_already, error, maxlength);
   249 
   250 	Pl_Status status;
   251 
   252 #ifndef METAMOD_PLAPI_VERSION
   253 	const char *file;
   254 	PluginId source;
   255 #endif
   256 
   257 	if (!id || (
   258 #ifndef METAMOD_PLAPI_VERSION
   259 		g_pMMPlugins->Query(id, file, status, source)
   260 #else
   261 		g_pMMPlugins->Query(id, NULL, &status, NULL)
   262 #endif
   263 		&& status < Pl_Paused))
   264 	{
   265 		*ok = false;
   266 	}
   267 	else
   268 	{
   269 		*ok = true;
   270 	}
   271 
   272 	return id;
   273 }
   274 
   275 static void unload_mms_plugin(int id)
   276 {
   277 	char ignore[255];
   278 	g_pMMPlugins->Unload(id, true, ignore, sizeof(ignore));
   279 }
   280 
   281 void do_global_plugin_loads()
   282 {
   283 	g_SourceMod.DoGlobalPluginLoads();
   284 }
   285 
   286 static ServerGlobals serverGlobals;
   287 
   288 static sm_core_t core_bridge =
   289 {
   290 	/* Objects */
   291 	&g_SourceMod,
   292 	&g_LibSys,
   293 	reinterpret_cast<IVEngineServer*>(&logic_engine),
   294 	&g_RootMenu,
   295 	&g_Forwards,
   296 	&g_Timers,
   297 	&g_Players,
   298 	&g_Admins,
   299 	&g_HL2,
   300 	&g_pSourcePawn,
   301 	&g_pSourcePawn2,
   302 	/* Functions */
   303 	find_convar,
   304 	strncopy,
   305 	UTIL_TrimWhitespace,
   306 	log_error,
   307 	log_fatal,
   308 	log_message,
   309 	log_to_file,
   310 	log_to_game,
   311 	file_exists,
   312 	get_cvar_string,
   313 	UTIL_Format,
   314 	UTIL_FormatArgs,
   315 	gnprintf,
   316 	atcprintf,
   317 	get_game_name,
   318 	get_game_description,
   319 	get_source_engine_name,
   320 	symbols_are_hidden,
   321 	get_core_config_value,
   322 	is_map_loading,
   323 	is_map_running,
   324 	read_cmd_argc,
   325 	read_cmd_arg,
   326 	load_mms_plugin,
   327 	unload_mms_plugin,
   328 	do_global_plugin_loads,
   329 	SM_AreConfigsExecuted,
   330 	SM_ExecuteForPlugin,
   331 	&serverGlobals
   332 };
   333 
   334 void InitLogicBridge()
   335 {
   336 	serverGlobals.universalTime = g_pUniversalTime;
   337 	serverGlobals.frametime = &gpGlobals->frametime;
   338 	serverGlobals.interval_per_tick = &gpGlobals->interval_per_tick;
   339 
   340 	core_bridge.engineFactory = (void *)g_SMAPI->GetEngineFactory(false);
   341 	core_bridge.serverFactory = (void *)g_SMAPI->GetServerFactory(false);
   342 
   343 	ILibrary *mmlib;
   344 	char path[PLATFORM_MAX_PATH];
   345 
   346 	g_LibSys.PathFormat(path, sizeof(path), "%s/bin/matchmaking_ds%s.%s", g_SMAPI->GetBaseDir(), MATCHMAKINGDS_SUFFIX, MATCHMAKINGDS_EXT);
   347 
   348 	if ((mmlib = g_LibSys.OpenLibrary(path, NULL, 0)))
   349 	{
   350 		core_bridge.matchmakingDSFactory = mmlib->GetSymbolAddress("CreateInterface");
   351 		mmlib->CloseLibrary();
   352 	}
   353 	
   354 	logic_init_fn(&core_bridge, &logicore);
   355 
   356 	/* Add SMGlobalClass instances */
   357 	SMGlobalClass* glob = SMGlobalClass::head;
   358 	while (glob->m_pGlobalClassNext != NULL)
   359 	{
   360 		glob = glob->m_pGlobalClassNext;
   361 	}
   362 	assert(glob->m_pGlobalClassNext == NULL);
   363 	glob->m_pGlobalClassNext = logicore.head;
   364 
   365 	g_pThreader = logicore.threader;
   366 	g_pSourcePawn2->SetProfiler(logicore.profiler);
   367 	translator = logicore.translator;
   368 	scripts = logicore.scripts;
   369 	sharesys = logicore.sharesys;
   370 	extsys = logicore.extsys;
   371 	g_pCoreIdent = logicore.core_ident;
   372 	handlesys = logicore.handlesys;
   373 }
   374 
   375 bool StartLogicBridge(char *error, size_t maxlength)
   376 {
   377 	char file[PLATFORM_MAX_PATH];
   378 
   379 	/* Now it's time to load the logic binary */
   380 	g_SMAPI->PathFormat(file,
   381 		sizeof(file),
   382 		"%s/bin/sourcemod.logic." PLATFORM_LIB_EXT,
   383 		g_SourceMod.GetSourceModPath());
   384 
   385 	char myerror[255];
   386 	g_pLogic = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
   387 
   388 	if (!g_pLogic)
   389 	{
   390 		if (error && maxlength)
   391 		{
   392 			UTIL_Format(error, maxlength, "failed to load %s: %s", file, myerror);
   393 		}
   394 		return false;
   395 	}
   396 
   397 	LogicLoadFunction llf = (LogicLoadFunction)g_pLogic->GetSymbolAddress("logic_load");
   398 	if (llf == NULL)
   399 	{
   400 		g_pLogic->CloseLibrary();
   401 		if (error && maxlength)
   402 		{
   403 			UTIL_Format(error, maxlength, "could not find logic_load function");
   404 		}
   405 		return false;
   406 	}
   407 
   408 	GetITextParsers getitxt = (GetITextParsers)g_pLogic->GetSymbolAddress("get_textparsers");
   409 	textparsers = getitxt();
   410 
   411 	logic_init_fn = llf(SM_LOGIC_MAGIC);
   412 
   413 	return true;
   414 }
   415 
   416 void ShutdownLogicBridge()
   417 {
   418 	g_pLogic->CloseLibrary();
   419 }
   420