core/logic/common_logic.cpp
author David Anderson <dvander@alliedmods.net>
Thu Aug 22 14:05:44 2013 -0700 (2013-08-22)
changeset 4029 cf8bdfcf6503
parent 3872 4c93f74cae51
child 4139 de7d1cd00703
permissions -rw-r--r--
Fix various problems with unloading ClientPrefs and SourceMod (bug 5874, r=ds).
     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 
    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 	SMGlobalClass::head = core->listeners;
   151 
   152 	g_ShareSys.Initialize();
   153 	g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
   154 	
   155 	_logic->core_ident = g_pCoreIdent;
   156 }
   157 
   158 PLATFORM_EXTERN_C ITextParsers *get_textparsers()
   159 {
   160 	return &g_TextParser;
   161 }
   162 
   163 PLATFORM_EXTERN_C LogicInitFunction logic_load(uint32_t magic)
   164 {
   165 	if (magic != SM_LOGIC_MAGIC)
   166 	{
   167 		return NULL;
   168 	}
   169 
   170 	return logic_init;
   171 }
   172 
   173 void CoreNativesToAdd::OnSourceModAllInitialized()
   174 {
   175 	g_CoreNatives.AddNatives(m_NativeList);
   176 }
   177 
   178 /* Overload a few things to prevent libstdc++ linking */
   179 #if defined __linux__ || defined __APPLE__
   180 extern "C" void __cxa_pure_virtual(void)
   181 {
   182 }
   183 
   184 void *operator new(size_t size)
   185 {
   186 	return malloc(size);
   187 }
   188 
   189 void *operator new[](size_t size) 
   190 {
   191 	return malloc(size);
   192 }
   193 
   194 void operator delete(void *ptr) 
   195 {
   196 	free(ptr);
   197 }
   198 
   199 void operator delete[](void * ptr)
   200 {
   201 	free(ptr);
   202 }
   203 #endif
   204