3 * =============================================================================
5 * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
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"
41 #include "sm_srvcmds.h"
42 #include "ForwardSys.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_DOTA
50 #include "convar_sm_dota.h"
51 #elif SOURCE_ENGINE >= SE_ALIENSWARM
52 #include "convar_sm_swarm.h"
53 #elif SOURCE_ENGINE >= SE_LEFT4DEAD
54 #include "convar_sm_l4d.h"
55 #elif SOURCE_ENGINE >= SE_ORANGEBOX
56 #include "convar_sm_ob.h"
58 #include "convar_sm.h"
62 #define MATCHMAKINGDS_SUFFIX ""
63 #define MATCHMAKINGDS_EXT "dll"
64 #elif defined __APPLE__
65 #define MATCHMAKINGDS_SUFFIX ""
66 #define MATCHMAKINGDS_EXT "dylib"
67 #elif defined __linux__
68 #if SOURCE_ENGINE < SE_LEFT4DEAD2
69 #define MATCHMAKINGDS_SUFFIX "_i486"
71 #define MATCHMAKINGDS_SUFFIX ""
73 #define MATCHMAKINGDS_EXT "so"
76 static ILibrary *g_pLogic = NULL;
77 static LogicInitFunction logic_init_fn;
79 IThreader *g_pThreader;
80 ITextParsers *textparsers;
82 ITranslator *translator;
83 IScriptManager *scripts;
85 IExtensionSys *extsys;
86 IHandleSys *handlesys;
88 class VEngineServer_Logic : public IVEngineServer_Logic
91 virtual bool IsMapValid(const char *map)
93 return !!engine->IsMapValid(map);
96 virtual void ServerCommand(const char *cmd)
98 engine->ServerCommand(cmd);
102 static VEngineServer_Logic logic_engine;
105 class VFileSystem_Logic : public IFileSystem_Logic
108 const char *FindFirstEx(const char *pWildCard, const char *pPathID, FileFindHandle_t *pHandle)
110 return filesystem->FindFirstEx(pWildCard, pPathID, pHandle);
112 const char *FindNext(FileFindHandle_t handle)
114 return filesystem->FindNext(handle);
116 void FindClose(FileFindHandle_t handle)
118 filesystem->FindClose(handle);
122 static VFileSystem_Logic logic_filesystem;
125 static ConVar *find_convar(const char *name)
127 return icvar->FindVar(name);
130 static void log_error(const char *fmt, ...)
135 g_Logger.LogErrorEx(fmt, ap);
139 static void log_fatal(const char *fmt, ...)
144 g_Logger.LogFatalEx(fmt, ap);
148 static void log_message(const char *fmt, ...)
152 g_Logger.LogMessageEx(fmt, ap);
156 static void log_to_file(FILE *fp, const char *fmt, ...)
160 g_Logger.LogToOpenFileEx(fp, fmt, ap);
164 static void log_to_game(const char *message)
166 Engine_LogPrintWrapper(message);
169 static bool file_exists(const char *path)
171 return basefilesystem->FileExists(path);
174 static const char *get_cvar_string(ConVar* cvar)
176 return cvar->GetString();
179 static bool get_game_name(char *buffer, size_t maxlength)
181 KeyValues *pGameInfo = new KeyValues("GameInfo");
182 if (g_HL2.KVLoadFromFile(pGameInfo, basefilesystem, "gameinfo.txt"))
185 if ((str = pGameInfo->GetString("game", NULL)) != NULL)
187 strncopy(buffer, str, maxlength);
191 pGameInfo->deleteThis();
195 static const char *get_game_description()
197 return SERVER_CALL(GetGameDescription)();
200 static const char *get_source_engine_name()
202 #if !defined SOURCE_ENGINE
203 # error "Unknown engine type"
205 #if SOURCE_ENGINE == SE_EPISODEONE
207 #elif SOURCE_ENGINE == SE_DARKMESSIAH
208 return "darkmessiah";
209 #elif SOURCE_ENGINE == SE_ORANGEBOX
211 #elif SOURCE_ENGINE == SE_BLOODYGOODTIME
212 return "bloodygoodtime";
213 #elif SOURCE_ENGINE == SE_EYE
215 #elif SOURCE_ENGINE == SE_CSS
217 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
218 return "orangebox_valve";
219 #elif SOURCE_ENGINE == SE_LEFT4DEAD
221 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
223 #elif SOURCE_ENGINE == SE_ALIENSWARM
225 #elif SOURCE_ENGINE == SE_PORTAL2
227 #elif SOURCE_ENGINE == SE_CSGO
229 #elif SOURCE_ENGINE == SE_DOTA
234 static bool symbols_are_hidden()
236 #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)
243 static const char* get_core_config_value(const char* key)
245 return g_CoreConfig.GetCoreConfigValue(key);
248 static bool is_map_loading()
250 return g_SourceMod.IsMapLoading();
253 static bool is_map_running()
255 return g_SourceMod.IsMapRunning();
258 int read_cmd_argc(const CCommand &args)
263 static const char *read_cmd_arg(const CCommand &args, int arg)
265 return args.Arg(arg);
268 static int load_mms_plugin(const char *file, bool *ok, char *error, size_t maxlength)
271 PluginId id = g_pMMPlugins->Load(file, g_PLID, ignore_already, error, maxlength);
275 #ifndef METAMOD_PLAPI_VERSION
281 #ifndef METAMOD_PLAPI_VERSION
282 g_pMMPlugins->Query(id, filep, status, source)
284 g_pMMPlugins->Query(id, NULL, &status, NULL)
286 && status < Pl_Paused))
298 static void unload_mms_plugin(int id)
301 g_pMMPlugins->Unload(id, true, ignore, sizeof(ignore));
304 void do_global_plugin_loads()
306 g_SourceMod.DoGlobalPluginLoads();
309 #if defined METAMOD_PLAPI_VERSION
310 #if SOURCE_ENGINE == SE_LEFT4DEAD
311 #define GAMEFIX "2.l4d"
312 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
313 #define GAMEFIX "2.l4d2"
314 #elif SOURCE_ENGINE == SE_ALIENSWARM
315 #define GAMEFIX "2.swarm"
316 #elif SOURCE_ENGINE == SE_ORANGEBOX
317 #define GAMEFIX "2.ep2"
318 #elif SOURCE_ENGINE == SE_BLOODYGOODTIME
319 #define GAMEFIX "2.bgt"
320 #elif SOURCE_ENGINE == SE_EYE
321 #define GAMEFIX "2.eye"
322 #elif SOURCE_ENGINE == SE_CSS
323 #define GAMEFIX "2.css"
324 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
325 #define GAMEFIX "2.ep2v"
326 #elif SOURCE_ENGINE == SE_DARKMESSIAH
327 #define GAMEFIX "2.darkm"
328 #elif SOURCE_ENGINE == SE_PORTAL2
329 #define GAMEFIX "2.portal2"
330 #elif SOURCE_ENGINE == SE_CSGO
331 #define GAMEFIX "2.csgo"
332 #elif SOURCE_ENGINE == SE_DOTA
333 #define GAMEFIX "2.dota"
335 #define GAMEFIX "2.ep1"
336 #endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
337 #else //METAMOD_PLAPI_VERSION
338 #define GAMEFIX "1.ep1"
339 #endif //METAMOD_PLAPI_VERSION
341 static ServerGlobals serverGlobals;
343 static sm_core_t core_bridge =
348 reinterpret_cast<IVEngineServer*>(&logic_engine),
349 reinterpret_cast<IFileSystem*>(&logic_filesystem),
374 get_game_description,
375 get_source_engine_name,
377 get_core_config_value,
384 do_global_plugin_loads,
385 SM_AreConfigsExecuted,
391 void InitLogicBridge()
393 serverGlobals.universalTime = g_pUniversalTime;
394 serverGlobals.frametime = &gpGlobals->frametime;
395 serverGlobals.interval_per_tick = &gpGlobals->interval_per_tick;
397 core_bridge.engineFactory = (void *)g_SMAPI->GetEngineFactory(false);
398 core_bridge.serverFactory = (void *)g_SMAPI->GetServerFactory(false);
401 char path[PLATFORM_MAX_PATH];
403 g_LibSys.PathFormat(path, sizeof(path), "%s/bin/matchmaking_ds%s.%s", g_SMAPI->GetBaseDir(), MATCHMAKINGDS_SUFFIX, MATCHMAKINGDS_EXT);
405 if ((mmlib = g_LibSys.OpenLibrary(path, NULL, 0)))
407 core_bridge.matchmakingDSFactory = mmlib->GetSymbolAddress("CreateInterface");
408 mmlib->CloseLibrary();
411 logic_init_fn(&core_bridge, &logicore);
413 /* Add SMGlobalClass instances */
414 SMGlobalClass* glob = SMGlobalClass::head;
415 while (glob->m_pGlobalClassNext != NULL)
417 glob = glob->m_pGlobalClassNext;
419 assert(glob->m_pGlobalClassNext == NULL);
420 glob->m_pGlobalClassNext = logicore.head;
422 g_pThreader = logicore.threader;
423 g_pSourcePawn2->SetProfiler(logicore.profiler);
424 translator = logicore.translator;
425 scripts = logicore.scripts;
426 sharesys = logicore.sharesys;
427 extsys = logicore.extsys;
428 g_pCoreIdent = logicore.core_ident;
429 handlesys = logicore.handlesys;
432 bool StartLogicBridge(char *error, size_t maxlength)
434 char file[PLATFORM_MAX_PATH];
436 /* Now it's time to load the logic binary */
437 g_SMAPI->PathFormat(file,
439 "%s/bin/sourcemod.logic." PLATFORM_LIB_EXT,
440 g_SourceMod.GetSourceModPath());
443 g_pLogic = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
447 if (error && maxlength)
449 UTIL_Format(error, maxlength, "failed to load %s: %s", file, myerror);
454 LogicLoadFunction llf = (LogicLoadFunction)g_pLogic->GetSymbolAddress("logic_load");
457 g_pLogic->CloseLibrary();
458 if (error && maxlength)
460 UTIL_Format(error, maxlength, "could not find logic_load function");
465 GetITextParsers getitxt = (GetITextParsers)g_pLogic->GetSymbolAddress("get_textparsers");
466 textparsers = getitxt();
468 logic_init_fn = llf(SM_LOGIC_MAGIC);
473 void ShutdownLogicBridge()
475 g_pLogic->CloseLibrary();