core/logic/common_logic.cpp
author David Anderson <dvander@alliedmods.net>
Fri Mar 29 11:37:29 2013 -0700 (2013-03-29)
changeset 3872 4c93f74cae51
parent 3800 21dfb9897341
child 4029 cf8bdfcf6503
permissions -rw-r--r--
Move scared guts of core into logic (bug 5680, r=fyren).
     1 /**
     2  * vim: set ts=4 sw=4 :
     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 
    52 sm_core_t smcore;
    53 IHandleSys *handlesys = &g_HandleSys;
    54 IdentityToken_t *g_pCoreIdent;
    55 SMGlobalClass *SMGlobalClass::head = NULL;
    56 ISourceMod *g_pSM;
    57 ILibrarySys *libsys;
    58 ITextParsers *textparser = &g_TextParser;
    59 IVEngineServer *engine;
    60 IShareSys *sharesys = &g_ShareSys;
    61 IRootConsole *rootmenu;
    62 IPluginManager *pluginsys = g_PluginSys.GetOldAPI();
    63 IForwardManager *forwardsys;
    64 ITimerSystem *timersys;
    65 ServerGlobals serverGlobals;
    66 IPlayerManager *playerhelpers;
    67 IAdminSystem *adminsys;
    68 IGameHelpers *gamehelpers;
    69 ISourcePawnEngine *g_pSourcePawn;
    70 ISourcePawnEngine2 *g_pSourcePawn2;
    71 CNativeOwner g_CoreNatives;
    72 IScriptManager *scripts = &g_PluginSys;
    73 
    74 static void AddCorePhraseFile(const char *filename)
    75 {
    76 	g_pCorePhrases->AddPhraseFile(filename);
    77 }
    78 
    79 static IGameConfig *GetCoreGameConfig()
    80 {
    81 	return g_pGameConf;
    82 }
    83 
    84 // Defined in smn_filesystem.cpp.
    85 extern bool OnLogPrint(const char *msg);
    86 
    87 static void GenerateError(IPluginContext *ctx, cell_t idx, int err, const char *msg, ...)
    88 {
    89 	va_list ap;
    90 	va_start(ap, msg);
    91 	g_DbgReporter.GenerateErrorVA(ctx, idx, err, msg, ap);
    92 	va_end(ap);
    93 }
    94 
    95 static void AddNatives(sp_nativeinfo_t *natives)
    96 {
    97 	g_CoreNatives.AddNatives(natives);
    98 }
    99 
   100 static void DumpHandles(void (*dumpfn)(const char *fmt, ...))
   101 {
   102 	g_HandleSys.Dump(dumpfn);
   103 }
   104 
   105 static sm_logic_t logic =
   106 {
   107 	NULL,
   108 	g_pThreader,
   109 	sm_profiler,
   110 	&g_Translator,
   111 	stristr,
   112 	CoreTranslate,
   113 	AddCorePhraseFile,
   114 	UTIL_ReplaceAll,
   115 	UTIL_ReplaceEx,
   116 	UTIL_DecodeHexString,
   117 	GetCoreGameConfig,
   118 	OnLogPrint,
   119 	&g_DbgReporter,
   120 	GenerateError,
   121 	AddNatives,
   122 	DumpHandles,
   123 	&g_PluginSys,
   124 	&g_ShareSys,
   125 	&g_Extensions,
   126 	&g_HandleSys,
   127 	NULL,
   128 	-1.0f
   129 };
   130 
   131 static void logic_init(const sm_core_t* core, sm_logic_t* _logic)
   132 {
   133 	logic.head = SMGlobalClass::head;
   134 
   135 	memcpy(&smcore, core, sizeof(sm_core_t));
   136 	memcpy(_logic, &logic, sizeof(sm_logic_t));
   137 	memcpy(&serverGlobals, core->serverGlobals, sizeof(ServerGlobals));
   138 
   139 	libsys = core->libsys;
   140 	engine = core->engine;
   141 	g_pSM = core->sm;
   142 	rootmenu = core->rootmenu;
   143 	forwardsys = core->forwardsys;
   144 	timersys = core->timersys;
   145 	playerhelpers = core->playerhelpers;
   146 	adminsys = core->adminsys;
   147 	gamehelpers = core->gamehelpers;
   148 	g_pSourcePawn = *core->spe1;
   149 	g_pSourcePawn2 = *core->spe2;
   150 
   151 	g_ShareSys.Initialize();
   152 	g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
   153 	
   154 	_logic->core_ident = g_pCoreIdent;
   155 }
   156 
   157 PLATFORM_EXTERN_C ITextParsers *get_textparsers()
   158 {
   159 	return &g_TextParser;
   160 }
   161 
   162 PLATFORM_EXTERN_C LogicInitFunction logic_load(uint32_t magic)
   163 {
   164 	if (magic != SM_LOGIC_MAGIC)
   165 	{
   166 		return NULL;
   167 	}
   168 
   169 	return logic_init;
   170 }
   171 
   172 void CoreNativesToAdd::OnSourceModAllInitialized()
   173 {
   174 	g_CoreNatives.AddNatives(m_NativeList);
   175 }
   176 
   177 /* Overload a few things to prevent libstdc++ linking */
   178 #if defined __linux__ || defined __APPLE__
   179 extern "C" void __cxa_pure_virtual(void)
   180 {
   181 }
   182 
   183 void *operator new(size_t size)
   184 {
   185 	return malloc(size);
   186 }
   187 
   188 void *operator new[](size_t size) 
   189 {
   190 	return malloc(size);
   191 }
   192 
   193 void operator delete(void *ptr) 
   194 {
   195 	free(ptr);
   196 }
   197 
   198 void operator delete[](void * ptr)
   199 {
   200 	free(ptr);
   201 }
   202 #endif
   203