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 "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"
44 #include "sm_srvcmds.h"
45 #include "ForwardSys.h"
47 #include "logic_bridge.h"
48 #include "PlayerManager.h"
49 #include "AdminCache.h"
50 #include "HalfLife2.h"
51 #include "CoreConfig.h"
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"
63 #define MATCHMAKINGDS_SUFFIX ""
65 #define MATCHMAKINGDS_EXT "so"
68 static ILibrary *g_pLogic = NULL;
69 static LogicInitFunction logic_init_fn;
71 IThreader *g_pThreader;
72 ITextParsers *textparsers;
74 ITranslator *translator;
76 class VEngineServer_Logic : public IVEngineServer_Logic
79 virtual bool IsMapValid(const char *map)
81 return engine->IsMapValid(map);
84 virtual void ServerCommand(const char *cmd)
86 engine->ServerCommand(cmd);
90 static VEngineServer_Logic logic_engine;
92 static void add_natives(sp_nativeinfo_t *natives)
94 g_pCoreNatives->AddNatives(natives);
97 static ConVar *find_convar(const char *name)
99 return icvar->FindVar(name);
102 static void log_error(const char *fmt, ...)
107 g_Logger.LogErrorEx(fmt, ap);
111 static void log_message(const char *fmt, ...)
115 g_Logger.LogMessageEx(fmt, ap);
119 static void log_to_file(FILE *fp, const char *fmt, ...)
123 g_Logger.LogToOpenFileEx(fp, fmt, ap);
127 static void log_to_game(const char *message)
129 Engine_LogPrintWrapper(message);
132 static bool file_exists(const char *path)
134 return basefilesystem->FileExists(path);
137 static const char *get_cvar_string(ConVar* cvar)
139 return cvar->GetString();
142 static bool get_game_name(char *buffer, size_t maxlength)
144 KeyValues *pGameInfo = new KeyValues("GameInfo");
145 if (g_HL2.KVLoadFromFile(pGameInfo, basefilesystem, "gameinfo.txt"))
148 if ((str = pGameInfo->GetString("game", NULL)) != NULL)
150 strncopy(buffer, str, maxlength);
154 pGameInfo->deleteThis();
158 static const char *get_game_description()
160 return SERVER_CALL(GetGameDescription)();
163 static const char *get_source_engine_name()
165 #if !defined SOURCE_ENGINE
166 # error "Unknown engine type"
168 #if SOURCE_ENGINE == SE_EPISODEONE
170 #elif SOURCE_ENGINE == SE_DARKMESSIAH
171 return "darkmessiah";
172 #elif SOURCE_ENGINE == SE_ORANGEBOX
174 #elif SOURCE_ENGINE == SE_BLOODYGOODTIME
175 return "bloodygoodtime";
176 #elif SOURCE_ENGINE == SE_EYE
178 #elif SOURCE_ENGINE == SE_CSS
180 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
181 return "orangebox_valve";
182 #elif SOURCE_ENGINE == SE_LEFT4DEAD
184 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
186 #elif SOURCE_ENGINE == SE_ALIENSWARM
188 #elif SOURCE_ENGINE == SE_PORTAL2
190 #elif SOURCE_ENGINE == SE_CSGO
195 static bool symbols_are_hidden()
197 #if (SOURCE_ENGINE == SE_CSS) || (SOURCE_ENGINE == SE_ORANGEBOXVALVE) || (SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2) || (SOURCE_ENGINE == SE_CSGO)
204 static const char* get_core_config_value(const char* key)
206 return g_CoreConfig.GetCoreConfigValue(key);
209 static ServerGlobals serverGlobals;
211 static sm_core_t core_bridge =
218 reinterpret_cast<IVEngineServer*>(&logic_engine),
245 get_game_description,
246 get_source_engine_name,
248 get_core_config_value,
252 void InitLogicBridge()
254 serverGlobals.universalTime = g_pUniversalTime;
255 serverGlobals.frametime = &gpGlobals->frametime;
256 serverGlobals.interval_per_tick = &gpGlobals->interval_per_tick;
258 core_bridge.core_ident = g_pCoreIdent;
259 core_bridge.engineFactory = (void *)g_SMAPI->GetEngineFactory(false);
260 core_bridge.serverFactory = (void *)g_SMAPI->GetServerFactory(false);
263 char path[PLATFORM_MAX_PATH];
265 g_LibSys.PathFormat(path, sizeof(path), "%s/bin/matchmaking_ds%s.%s", g_SMAPI->GetBaseDir(), MATCHMAKINGDS_SUFFIX, MATCHMAKINGDS_EXT);
267 if ((mmlib = g_LibSys.OpenLibrary(path, NULL, 0)))
269 core_bridge.matchmakingDSFactory = mmlib->GetSymbolAddress("CreateInterface");
270 mmlib->CloseLibrary();
273 logic_init_fn(&core_bridge, &logicore);
275 /* Add SMGlobalClass instances */
276 SMGlobalClass* glob = SMGlobalClass::head;
277 while (glob->m_pGlobalClassNext != NULL)
279 glob = glob->m_pGlobalClassNext;
281 assert(glob->m_pGlobalClassNext == NULL);
282 glob->m_pGlobalClassNext = logicore.head;
284 g_pThreader = logicore.threader;
285 g_pSourcePawn2->SetProfiler(logicore.profiler);
286 translator = logicore.translator;
289 bool StartLogicBridge(char *error, size_t maxlength)
291 char file[PLATFORM_MAX_PATH];
293 /* Now it's time to load the logic binary */
294 g_SMAPI->PathFormat(file,
296 "%s/bin/sourcemod.logic." PLATFORM_LIB_EXT,
297 g_SourceMod.GetSourceModPath());
300 g_pLogic = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
304 if (error && maxlength)
306 UTIL_Format(error, maxlength, "failed to load %s: %s", file, myerror);
311 LogicLoadFunction llf = (LogicLoadFunction)g_pLogic->GetSymbolAddress("logic_load");
314 g_pLogic->CloseLibrary();
315 if (error && maxlength)
317 UTIL_Format(error, maxlength, "could not find logic_load function");
322 GetITextParsers getitxt = (GetITextParsers)g_pLogic->GetSymbolAddress("get_textparsers");
323 textparsers = getitxt();
325 logic_init_fn = llf(SM_LOGIC_MAGIC);
330 void ShutdownLogicBridge()
332 g_pLogic->CloseLibrary();