core/logic_bridge.cpp
author "David Anderson <dvander@alliedmods.net>
Thu May 14 11:24:07 2009 -0400 (2009-05-14)
changeset 2728 96022dad7e53
parent 2726 c96f3fab8211
child 2730 a2ff0a7a1c53
permissions -rw-r--r--
Moved profiler and adt_trie to logic binary.
     1 /**
     2  * vim: set ts=4 sw=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2009 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 
    46 static ILibrary *g_pLogic = NULL;
    47 static LogicInitFunction logic_init_fn;
    48 
    49 IThreader *g_pThreader;
    50 ITextParsers *textparsers;
    51 
    52 class VEngineServer_Logic : public IVEngineServer_Logic
    53 {
    54 public:
    55 	virtual bool IsMapValid(const char *map)
    56 	{
    57 		return engine->IsMapValid(map);
    58 	}
    59 };
    60 
    61 static VEngineServer_Logic logic_engine;
    62 
    63 static void add_natives(sp_nativeinfo_t *natives)
    64 {
    65 	g_pCoreNatives->AddNatives(natives);
    66 }
    67 
    68 static ConVar *find_convar(const char *name)
    69 {
    70 	return icvar->FindVar(name);
    71 }
    72 
    73 static void log_error(const char *fmt, ...)
    74 {
    75 	va_list ap;
    76 
    77 	va_start(ap, fmt);
    78 	g_Logger.LogErrorEx(fmt, ap);
    79 	va_end(ap);
    80 }
    81 
    82 static const char *get_cvar_string(ConVar* cvar)
    83 {
    84 	return cvar->GetString();
    85 }
    86 
    87 static sm_core_t core_bridge =
    88 {
    89 	/* Objects */
    90 	&g_HandleSys,
    91 	NULL,
    92 	&g_SourceMod,
    93 	&g_LibSys,
    94 	reinterpret_cast<IVEngineServer*>(&logic_engine),
    95 	&g_ShareSys,
    96 	&g_RootMenu,
    97 	&g_PluginSys,
    98 	/* Functions */
    99 	add_natives,
   100 	find_convar,
   101 	strncopy,
   102 	UTIL_TrimWhitespace,
   103 	log_error,
   104 	get_cvar_string,
   105 	UTIL_Format
   106 };
   107 
   108 void InitLogicBridge()
   109 {
   110 	sm_logic_t logic;
   111 
   112 	core_bridge.core_ident = g_pCoreIdent;
   113 	logic_init_fn(&core_bridge, &logic);
   114 
   115 	/* Add SMGlobalClass instances */
   116 	SMGlobalClass* glob = SMGlobalClass::head;
   117 	while (glob->m_pGlobalClassNext != NULL)
   118 	{
   119 		glob = glob->m_pGlobalClassNext;
   120 	}
   121 	assert(glob->m_pGlobalClassNext == NULL);
   122 	glob->m_pGlobalClassNext = logic.head;
   123 
   124 	g_pThreader = logic.threader;
   125 	g_pSourcePawn2->SetProfiler(logic.profiler);
   126 }
   127 
   128 bool StartLogicBridge(char *error, size_t maxlength)
   129 {
   130 	char file[PLATFORM_MAX_PATH];
   131 
   132 	/* Now it's time to load the logic binary */
   133 	g_SMAPI->PathFormat(file,
   134 		sizeof(file),
   135 		"%s/bin/sourcemod.logic." PLATFORM_LIB_EXT,
   136 		g_SourceMod.GetSourceModPath());
   137 
   138 	char myerror[255];
   139 	g_pLogic = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
   140 
   141 	if (!g_pLogic)
   142 	{
   143 		if (error && maxlength)
   144 		{
   145 			UTIL_Format(error, maxlength, "failed to load %s: %s", file, myerror);
   146 		}
   147 		return false;
   148 	}
   149 
   150 	LogicLoadFunction llf = (LogicLoadFunction)g_pLogic->GetSymbolAddress("logic_load");
   151 	if (llf == NULL)
   152 	{
   153 		g_pLogic->CloseLibrary();
   154 		if (error && maxlength)
   155 		{
   156 			UTIL_Format(error, maxlength, "could not find logic_load function");
   157 		}
   158 		return false;
   159 	}
   160 
   161 	GetITextParsers getitxt = (GetITextParsers)g_pLogic->GetSymbolAddress("get_textparsers");
   162 	textparsers = getitxt();
   163 
   164 	logic_init_fn = llf(SM_LOGIC_MAGIC);
   165 
   166 	return true;
   167 }
   168 
   169 void ShutdownLogicBridge()
   170 {
   171 	g_pLogic->CloseLibrary();
   172 }
   173