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 "DebugReporter.h"
49 #include "PlayerManager.h"
50 #include "AdminCache.h"
51 #include "HalfLife2.h"
52 #include "CoreConfig.h"
55 #define MATCHMAKINGDS_SUFFIX ""
56 #define MATCHMAKINGDS_EXT "dll"
57 #elif defined __APPLE__
58 #define MATCHMAKINGDS_SUFFIX ""
59 #define MATCHMAKINGDS_EXT "dylib"
60 #elif defined __linux__
61 #if SOURCE_ENGINE < SE_LEFT4DEAD2
62 #define MATCHMAKINGDS_SUFFIX "_i486"
64 #define MATCHMAKINGDS_SUFFIX ""
66 #define MATCHMAKINGDS_EXT "so"
69 static ILibrary *g_pLogic = NULL;
70 static LogicInitFunction logic_init_fn;
72 IThreader *g_pThreader;
73 ITextParsers *textparsers;
75 ITranslator *translator;
77 class VEngineServer_Logic : public IVEngineServer_Logic
80 virtual bool IsMapValid(const char *map)
82 return engine->IsMapValid(map);
85 virtual void ServerCommand(const char *cmd)
87 engine->ServerCommand(cmd);
91 static VEngineServer_Logic logic_engine;
93 static void add_natives(sp_nativeinfo_t *natives)
95 g_pCoreNatives->AddNatives(natives);
98 static ConVar *find_convar(const char *name)
100 return icvar->FindVar(name);
103 static void log_error(const char *fmt, ...)
108 g_Logger.LogErrorEx(fmt, ap);
112 static void generate_error(IPluginContext *ctx, cell_t func_idx, int err, const char *fmt, ...)
117 g_DbgReporter.GenerateErrorVA(ctx, func_idx, err, fmt, ap);
121 static const char *get_cvar_string(ConVar* cvar)
123 return cvar->GetString();
126 static bool get_game_name(char *buffer, size_t maxlength)
128 KeyValues *pGameInfo = new KeyValues("GameInfo");
129 if (g_HL2.KVLoadFromFile(pGameInfo, basefilesystem, "gameinfo.txt"))
132 if ((str = pGameInfo->GetString("game", NULL)) != NULL)
134 strncopy(buffer, str, maxlength);
138 pGameInfo->deleteThis();
142 static const char *get_game_description()
144 return SERVER_CALL(GetGameDescription)();
147 static const char *get_source_engine_name()
149 #if !defined SOURCE_ENGINE
150 # error "Unknown engine type"
152 #if SOURCE_ENGINE == SE_EPISODEONE
154 #elif SOURCE_ENGINE == SE_DARKMESSIAH
155 return "darkmessiah";
156 #elif SOURCE_ENGINE == SE_ORANGEBOX
158 #elif SOURCE_ENGINE == SE_BLOODYGOODTIME
159 return "bloodygoodtime";
160 #elif SOURCE_ENGINE == SE_EYE
162 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
163 return "orangebox_valve";
164 #elif SOURCE_ENGINE == SE_LEFT4DEAD
166 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
168 #elif SOURCE_ENGINE == SE_ALIENSWARM
173 static bool symbols_are_hidden()
175 #if (SOURCE_ENGINE == SE_ORANGEBOXVALVE) || (SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
182 static const char* get_core_config_value(const char* key)
184 return g_CoreConfig.GetCoreConfigValue(key);
187 static ServerGlobals serverGlobals;
189 static sm_core_t core_bridge =
196 reinterpret_cast<IVEngineServer*>(&logic_engine),
217 get_game_description,
218 get_source_engine_name,
220 get_core_config_value,
224 void InitLogicBridge()
226 serverGlobals.universalTime = g_pUniversalTime;
227 serverGlobals.frametime = &gpGlobals->frametime;
228 serverGlobals.interval_per_tick = &gpGlobals->interval_per_tick;
230 core_bridge.core_ident = g_pCoreIdent;
231 core_bridge.engineFactory = (void *)g_SMAPI->GetEngineFactory(false);
232 core_bridge.serverFactory = (void *)g_SMAPI->GetServerFactory(false);
235 char path[PLATFORM_MAX_PATH];
237 g_LibSys.PathFormat(path, sizeof(path), "%s/bin/matchmaking_ds%s.%s", g_SMAPI->GetBaseDir(), MATCHMAKINGDS_SUFFIX, MATCHMAKINGDS_EXT);
239 if ((mmlib = g_LibSys.OpenLibrary(path, NULL, 0)))
241 core_bridge.matchmakingDSFactory = mmlib->GetSymbolAddress("CreateInterface");
242 mmlib->CloseLibrary();
245 logic_init_fn(&core_bridge, &logicore);
247 /* Add SMGlobalClass instances */
248 SMGlobalClass* glob = SMGlobalClass::head;
249 while (glob->m_pGlobalClassNext != NULL)
251 glob = glob->m_pGlobalClassNext;
253 assert(glob->m_pGlobalClassNext == NULL);
254 glob->m_pGlobalClassNext = logicore.head;
256 g_pThreader = logicore.threader;
257 g_pSourcePawn2->SetProfiler(logicore.profiler);
258 translator = logicore.translator;
261 bool StartLogicBridge(char *error, size_t maxlength)
263 char file[PLATFORM_MAX_PATH];
265 /* Now it's time to load the logic binary */
266 g_SMAPI->PathFormat(file,
268 "%s/bin/sourcemod.logic." PLATFORM_LIB_EXT,
269 g_SourceMod.GetSourceModPath());
272 g_pLogic = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
276 if (error && maxlength)
278 UTIL_Format(error, maxlength, "failed to load %s: %s", file, myerror);
283 LogicLoadFunction llf = (LogicLoadFunction)g_pLogic->GetSymbolAddress("logic_load");
286 g_pLogic->CloseLibrary();
287 if (error && maxlength)
289 UTIL_Format(error, maxlength, "could not find logic_load function");
294 GetITextParsers getitxt = (GetITextParsers)g_pLogic->GetSymbolAddress("get_textparsers");
295 textparsers = getitxt();
297 logic_init_fn = llf(SM_LOGIC_MAGIC);
302 void ShutdownLogicBridge()
304 g_pLogic->CloseLibrary();