core/logic/common_logic.cpp
author Peace-Maker <januk@freenet.de>
Sat May 10 05:42:33 2014 -0700 (2014-05-10)
changeset 4354 7011587e0021
parent 4139 de7d1cd00703
permissions -rw-r--r--
Remove LogMessageEx definition from logging.inc (bug 5897, r=kyles).
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet:
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 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 <new>
    32 #include <stdlib.h>
    33 #include <sm_platform.h>
    34 #include <string.h>
    35 #include <stdarg.h>
    36 #include "common_logic.h"
    37 #include "TextParsers.h"
    38 #include "Profiler.h"
    39 #include "sm_crc32.h"
    40 #include "MemoryUtils.h"
    41 #include "stringutil.h"
    42 #include "ThreadSupport.h"
    43 #include "Translator.h"
    44 #include "GameConfigs.h"
    45 #include "DebugReporter.h"
    46 #include "PluginSys.h"
    47 #include "ShareSys.h"
    48 #include "NativeOwner.h"
    49 #include "HandleSys.h"
    50 #include "ExtensionSys.h"
    51 #include "ForwardSys.h"
    52 
    53 sm_core_t smcore;
    54 IHandleSys *handlesys = &g_HandleSys;
    55 IdentityToken_t *g_pCoreIdent;
    56 SMGlobalClass *SMGlobalClass::head = NULL;
    57 ISourceMod *g_pSM;
    58 ILibrarySys *libsys;
    59 ITextParsers *textparser = &g_TextParser;
    60 IVEngineServer *engine;
    61 IShareSys *sharesys = &g_ShareSys;
    62 IRootConsole *rootmenu;
    63 IPluginManager *pluginsys = g_PluginSys.GetOldAPI();
    64 IForwardManager *forwardsys = &g_Forwards;
    65 ITimerSystem *timersys;
    66 ServerGlobals serverGlobals;
    67 IPlayerManager *playerhelpers;
    68 IAdminSystem *adminsys;
    69 IGameHelpers *gamehelpers;
    70 ISourcePawnEngine *g_pSourcePawn;
    71 ISourcePawnEngine2 *g_pSourcePawn2;
    72 CNativeOwner g_CoreNatives;
    73 IScriptManager *scripts = &g_PluginSys;
    74 
    75 static void AddCorePhraseFile(const char *filename)
    76 {
    77 	g_pCorePhrases->AddPhraseFile(filename);
    78 }
    79 
    80 static IGameConfig *GetCoreGameConfig()
    81 {
    82 	return g_pGameConf;
    83 }
    84 
    85 // Defined in smn_filesystem.cpp.
    86 extern bool OnLogPrint(const char *msg);
    87 
    88 static void GenerateError(IPluginContext *ctx, cell_t idx, int err, const char *msg, ...)
    89 {
    90 	va_list ap;
    91 	va_start(ap, msg);
    92 	g_DbgReporter.GenerateErrorVA(ctx, idx, err, msg, ap);
    93 	va_end(ap);
    94 }
    95 
    96 static void AddNatives(sp_nativeinfo_t *natives)
    97 {
    98 	g_CoreNatives.AddNatives(natives);
    99 }
   100 
   101 static void DumpHandles(void (*dumpfn)(const char *fmt, ...))
   102 {
   103 	g_HandleSys.Dump(dumpfn);
   104 }
   105 
   106 static sm_logic_t logic =
   107 {
   108 	NULL,
   109 	g_pThreader,
   110 	sm_profiler,
   111 	&g_Translator,
   112 	stristr,
   113 	CoreTranslate,
   114 	AddCorePhraseFile,
   115 	UTIL_ReplaceAll,
   116 	UTIL_ReplaceEx,
   117 	UTIL_DecodeHexString,
   118 	GetCoreGameConfig,
   119 	OnLogPrint,
   120 	&g_DbgReporter,
   121 	GenerateError,
   122 	AddNatives,
   123 	DumpHandles,
   124 	&g_PluginSys,
   125 	&g_ShareSys,
   126 	&g_Extensions,
   127 	&g_HandleSys,
   128 	&g_Forwards,
   129 	NULL,
   130 	-1.0f
   131 };
   132 
   133 static void logic_init(const sm_core_t* core, sm_logic_t* _logic)
   134 {
   135 	logic.head = SMGlobalClass::head;
   136 
   137 	memcpy(&smcore, core, sizeof(sm_core_t));
   138 	memcpy(_logic, &logic, sizeof(sm_logic_t));
   139 	memcpy(&serverGlobals, core->serverGlobals, sizeof(ServerGlobals));
   140 
   141 	libsys = core->libsys;
   142 	engine = core->engine;
   143 	g_pSM = core->sm;
   144 	rootmenu = core->rootmenu;
   145 	timersys = core->timersys;
   146 	playerhelpers = core->playerhelpers;
   147 	adminsys = core->adminsys;
   148 	gamehelpers = core->gamehelpers;
   149 	g_pSourcePawn = *core->spe1;
   150 	g_pSourcePawn2 = *core->spe2;
   151 	SMGlobalClass::head = core->listeners;
   152 
   153 	g_ShareSys.Initialize();
   154 	g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
   155 	
   156 	_logic->core_ident = g_pCoreIdent;
   157 }
   158 
   159 PLATFORM_EXTERN_C ITextParsers *get_textparsers()
   160 {
   161 	return &g_TextParser;
   162 }
   163 
   164 PLATFORM_EXTERN_C LogicInitFunction logic_load(uint32_t magic)
   165 {
   166 	if (magic != SM_LOGIC_MAGIC)
   167 	{
   168 		return NULL;
   169 	}
   170 
   171 	return logic_init;
   172 }
   173 
   174 void CoreNativesToAdd::OnSourceModAllInitialized()
   175 {
   176 	g_CoreNatives.AddNatives(m_NativeList);
   177 }
   178 
   179 /* Overload a few things to prevent libstdc++ linking */
   180 #if defined __linux__ || defined __APPLE__
   181 extern "C" void __cxa_pure_virtual(void)
   182 {
   183 }
   184 
   185 void *operator new(size_t size)
   186 {
   187 	return malloc(size);
   188 }
   189 
   190 void *operator new[](size_t size) 
   191 {
   192 	return malloc(size);
   193 }
   194 
   195 void operator delete(void *ptr) 
   196 {
   197 	free(ptr);
   198 }
   199 
   200 void operator delete[](void * ptr)
   201 {
   202 	free(ptr);
   203 }
   204 #endif
   205