metamod/dllapi.cpp
author darope
Sat Apr 08 19:29:39 2006 +0000 (2006-04-08)
branchB-work
changeset 217 43ad553832f6
parent 209 812e3285c758
child 223 fa27b9403af3
permissions -rw-r--r--
Fixed erroneous handling of the interface to the engine, gamedll and
plugins in the cases that Metamod was compiled against a different version
of the HL SDK.
For one, Metamod now uses zeroed dummy space at the end of function pointer
structures so that units copying into Metamod do not overwrite anything
valuable and that units copying from Metamod get zeroes and not arbitrary
values when the sizes of the structures don't match. We achieve this within
Metamod and without having to change the SDK.
Secondly, Metamod now tries to determine what engine interface it deals
with. It then adjusts the function pointers it got from the engine
accordingly and also will not overwrite engine memory when copying its own
function pointers into engine memory space.
The way this was done before was inaccurate and clumsy and resulted in
errors when using Metamod with older engine versions. Since the method of
using the IS_VALID_PTR() for checking for valid function pointers doesn't
yield correct results, a new method was implemented. We now try to
determine the valid range of memory addresses for engine functions more
accurately in order to check if a function pointer is a valid pointer for
a function living in the engine.
darope@0
     1
// vi: set ts=4 sw=4 :
darope@0
     2
// vim: set tw=75 :
darope@0
     3
darope@0
     4
// dllapi.cpp - implementation of Half-Life DLL routines
darope@0
     5
darope@0
     6
/*
darope@0
     7
 * Copyright (c) 2001-2003 Will Day <[email protected]>
darope@0
     8
 *
darope@0
     9
 *    This file is part of Metamod.
darope@0
    10
 *
darope@0
    11
 *    Metamod is free software; you can redistribute it and/or modify it
darope@0
    12
 *    under the terms of the GNU General Public License as published by the
darope@0
    13
 *    Free Software Foundation; either version 2 of the License, or (at
darope@0
    14
 *    your option) any later version.
darope@0
    15
 *
darope@0
    16
 *    Metamod is distributed in the hope that it will be useful, but
darope@0
    17
 *    WITHOUT ANY WARRANTY; without even the implied warranty of
darope@0
    18
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
darope@0
    19
 *    General Public License for more details.
darope@0
    20
 *
darope@0
    21
 *    You should have received a copy of the GNU General Public License
darope@0
    22
 *    along with Metamod; if not, write to the Free Software Foundation,
darope@0
    23
 *    Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
darope@0
    24
 *
darope@0
    25
 *    In addition, as a special exception, the author gives permission to
darope@0
    26
 *    link the code of this program with the Half-Life Game Engine ("HL
darope@0
    27
 *    Engine") and Modified Game Libraries ("MODs") developed by Valve,
darope@0
    28
 *    L.L.C ("Valve").  You must obey the GNU General Public License in all
darope@0
    29
 *    respects for all of the code used other than the HL Engine and MODs
darope@0
    30
 *    from Valve.  If you modify this file, you may extend this exception
darope@0
    31
 *    to your version of the file, but you are not obligated to do so.  If
darope@0
    32
 *    you do not wish to do so, delete this exception statement from your
darope@0
    33
 *    version.
darope@0
    34
 *
darope@0
    35
 */
darope@0
    36
darope@0
    37
#include <extdll.h>			// always
darope@0
    38
darope@0
    39
#include "dllapi.h"			// me
darope@0
    40
#include "metamod.h"		// SETUP_API_CALLS, etc
darope@0
    41
#include "api_info.h"		// dllapi_info, etc
darope@0
    42
#include "commands_meta.h"	// client_meta, etc
darope@0
    43
#include "log_meta.h"		// META_ERROR, etc
darope@0
    44
darope@0
    45
darope@0
    46
// Original DLL routines, functions returning "void".
darope@0
    47
#define META_DLLAPI_HANDLE_void(FN_TYPE, pfnName, pfn_args) \
darope@0
    48
	SETUP_API_CALLS_void(FN_TYPE, pfnName, dllapi_info); \
darope@0
    49
	CALL_PLUGIN_API_void(P_PRE, pfnName, pfn_args, dllapi_table); \
darope@0
    50
	CALL_GAME_API_void(pfnName, pfn_args, dllapi_table); \
darope@0
    51
	CALL_PLUGIN_API_void(P_POST, pfnName, pfn_args, dllapi_post_table);
darope@0
    52
darope@0
    53
// Original DLL routines, functions returning an actual value.
darope@0
    54
#define META_DLLAPI_HANDLE(ret_t, ret_init, FN_TYPE, pfnName, pfn_args) \
darope@0
    55
	SETUP_API_CALLS(ret_t, ret_init, FN_TYPE, pfnName, dllapi_info); \
darope@0
    56
	CALL_PLUGIN_API(P_PRE, ret_init, pfnName, pfn_args, MRES_SUPERCEDE, dllapi_table); \
darope@0
    57
	CALL_GAME_API(pfnName, pfn_args, dllapi_table); \
darope@0
    58
	CALL_PLUGIN_API(P_POST, ret_init, pfnName, pfn_args, MRES_OVERRIDE, dllapi_post_table);
darope@0
    59
darope@0
    60
darope@0
    61
// The "new" api routines (just 3 right now), functions returning "void".
darope@0
    62
#define META_NEWAPI_HANDLE_void(FN_TYPE, pfnName, pfn_args) \
darope@0
    63
	SETUP_API_CALLS_void(FN_TYPE, pfnName, newapi_info); \
darope@0
    64
	CALL_PLUGIN_API_void(P_PRE, pfnName, pfn_args, newapi_table); \
darope@0
    65
	CALL_GAME_API_void(pfnName, pfn_args, newapi_table); \
darope@0
    66
	CALL_PLUGIN_API_void(P_POST, pfnName, pfn_args, newapi_post_table);
darope@0
    67
darope@0
    68
// The "new" api routines (just 3 right now), functions returning an actual value.
darope@0
    69
#define META_NEWAPI_HANDLE(ret_t, ret_init, FN_TYPE, pfnName, pfn_args) \
darope@0
    70
	SETUP_API_CALLS(ret_t, ret_init, FN_TYPE, pfnName, newapi_info); \
darope@0
    71
	CALL_PLUGIN_API(P_PRE, ret_init, pfnName, pfn_args, MRES_SUPERCEDE, newapi_table); \
darope@0
    72
	CALL_GAME_API(pfnName, pfn_args, newapi_table); \
darope@0
    73
	CALL_PLUGIN_API(P_POST, ret_init, pfnName, pfn_args, MRES_OVERRIDE, newapi_post_table);
darope@0
    74
darope@0
    75
darope@0
    76
// From SDK dlls/game.cpp:
darope@0
    77
void mm_GameDLLInit(void) {
darope@0
    78
	META_DLLAPI_HANDLE_void(FN_GAMEINIT, pfnGameInit, ());
darope@0
    79
	RETURN_API_void();
darope@0
    80
}
darope@0
    81
darope@0
    82
// From SDK dlls/cbase.cpp:
darope@0
    83
int mm_DispatchSpawn(edict_t *pent) {
darope@0
    84
	// 0==Success, -1==Failure ?
darope@0
    85
	META_DLLAPI_HANDLE(int, 0, FN_DISPATCHSPAWN, pfnSpawn, (pent));
darope@0
    86
	RETURN_API();
darope@0
    87
}
darope@0
    88
void mm_DispatchThink(edict_t *pent) {
darope@0
    89
	META_DLLAPI_HANDLE_void(FN_DISPATCHTHINK, pfnThink, (pent));
darope@0
    90
	RETURN_API_void();
darope@0
    91
}
darope@0
    92
void mm_DispatchUse(edict_t *pentUsed, edict_t *pentOther) {
darope@0
    93
	META_DLLAPI_HANDLE_void(FN_DISPATCHUSE, pfnUse, (pentUsed, pentOther));
darope@0
    94
	RETURN_API_void();
darope@0
    95
}
darope@0
    96
void mm_DispatchTouch(edict_t *pentTouched, edict_t *pentOther) {
darope@0
    97
	META_DLLAPI_HANDLE_void(FN_DISPATCHTOUCH, pfnTouch, (pentTouched, pentOther));
darope@0
    98
	RETURN_API_void();
darope@0
    99
}
darope@0
   100
void mm_DispatchBlocked(edict_t *pentBlocked, edict_t *pentOther) {
darope@0
   101
	META_DLLAPI_HANDLE_void(FN_DISPATCHBLOCKED, pfnBlocked, (pentBlocked, pentOther));
darope@0
   102
	RETURN_API_void();
darope@0
   103
}
darope@0
   104
void mm_DispatchKeyValue(edict_t *pentKeyvalue, KeyValueData *pkvd) {
darope@0
   105
	META_DLLAPI_HANDLE_void(FN_DISPATCHKEYVALUE, pfnKeyValue, (pentKeyvalue, pkvd));
darope@0
   106
	RETURN_API_void();
darope@0
   107
}
darope@0
   108
void mm_DispatchSave(edict_t *pent, SAVERESTOREDATA *pSaveData) {
darope@0
   109
	META_DLLAPI_HANDLE_void(FN_DISPATCHSAVE, pfnSave, (pent, pSaveData));
darope@0
   110
	RETURN_API_void();
darope@0
   111
}
darope@0
   112
int mm_DispatchRestore(edict_t *pent, SAVERESTOREDATA *pSaveData, int globalEntity) {
darope@0
   113
	// 0==Success, -1==Failure ?
darope@0
   114
	META_DLLAPI_HANDLE(int, 0, FN_DISPATCHRESTORE, pfnRestore, (pent, pSaveData, globalEntity));
darope@0
   115
	RETURN_API();
darope@0
   116
}
darope@0
   117
void mm_DispatchObjectCollsionBox(edict_t *pent) {
darope@0
   118
	META_DLLAPI_HANDLE_void(FN_DISPATCHOBJECTCOLLISIONBOX, pfnSetAbsBox, (pent));
darope@0
   119
	RETURN_API_void();
darope@0
   120
}
darope@0
   121
void mm_SaveWriteFields(SAVERESTOREDATA *pSaveData, const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount) {
darope@0
   122
	META_DLLAPI_HANDLE_void(FN_SAVEWRITEFIELDS, pfnSaveWriteFields, (pSaveData, pname, pBaseData, pFields, fieldCount));
darope@0
   123
	RETURN_API_void();
darope@0
   124
}
darope@0
   125
void mm_SaveReadFields(SAVERESTOREDATA *pSaveData, const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount) {
darope@0
   126
	META_DLLAPI_HANDLE_void(FN_SAVEREADFIELDS, pfnSaveReadFields, (pSaveData, pname, pBaseData, pFields, fieldCount));
darope@0
   127
	RETURN_API_void();
darope@0
   128
}
darope@0
   129
darope@0
   130
// From SDK dlls/world.cpp:
darope@0
   131
void mm_SaveGlobalState(SAVERESTOREDATA *pSaveData) {
darope@0
   132
	META_DLLAPI_HANDLE_void(FN_SAVEGLOBALSTATE, pfnSaveGlobalState, (pSaveData));
darope@0
   133
	RETURN_API_void();
darope@0
   134
}
darope@0
   135
void mm_RestoreGlobalState(SAVERESTOREDATA *pSaveData) {
darope@0
   136
	META_DLLAPI_HANDLE_void(FN_RESTOREGLOBALSTATE, pfnRestoreGlobalState, (pSaveData));
darope@0
   137
	RETURN_API_void();
darope@0
   138
}
darope@0
   139
void mm_ResetGlobalState(void) {
darope@0
   140
	META_DLLAPI_HANDLE_void(FN_RESETGLOBALSTATE, pfnResetGlobalState, ());
darope@0
   141
	RETURN_API_void();
darope@0
   142
}
darope@0
   143
darope@0
   144
// From SDK dlls/client.cpp:
darope@0
   145
BOOL mm_ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char szRejectReason[ 128 ] ) {
darope@158
   146
	g_Players.clear_player_cvar_query(pEntity);
darope@0
   147
	META_DLLAPI_HANDLE(BOOL, TRUE, FN_CLIENTCONNECT, pfnClientConnect, (pEntity, pszName, pszAddress, szRejectReason));
darope@0
   148
	RETURN_API();
darope@0
   149
}
darope@0
   150
void mm_ClientDisconnect(edict_t *pEntity) {
darope@158
   151
	g_Players.clear_player_cvar_query(pEntity);
darope@0
   152
	META_DLLAPI_HANDLE_void(FN_CLIENTDISCONNECT, pfnClientDisconnect, (pEntity));
darope@0
   153
	RETURN_API_void();
darope@0
   154
}
darope@0
   155
void mm_ClientKill(edict_t *pEntity) {
darope@0
   156
	META_DLLAPI_HANDLE_void(FN_CLIENTKILL, pfnClientKill, (pEntity));
darope@0
   157
	RETURN_API_void();
darope@0
   158
}
darope@0
   159
void mm_ClientPutInServer(edict_t *pEntity) {
darope@0
   160
	META_DLLAPI_HANDLE_void(FN_CLIENTPUTINSERVER, pfnClientPutInServer, (pEntity));
darope@0
   161
	RETURN_API_void();
darope@0
   162
}
darope@0
   163
void mm_ClientCommand(edict_t *pEntity) {
darope@0
   164
	if(strmatch(CMD_ARGV(0), "meta")) {
darope@0
   165
		client_meta(pEntity);
darope@0
   166
	}
darope@0
   167
	META_DLLAPI_HANDLE_void(FN_CLIENTCOMMAND, pfnClientCommand, (pEntity));
darope@0
   168
	RETURN_API_void();
darope@0
   169
}
darope@0
   170
void mm_ClientUserInfoChanged(edict_t *pEntity, char *infobuffer) {
darope@0
   171
	META_DLLAPI_HANDLE_void(FN_CLIENTUSERINFOCHANGED, pfnClientUserInfoChanged, (pEntity, infobuffer));
darope@0
   172
	RETURN_API_void();
darope@0
   173
}
darope@0
   174
void mm_ServerActivate(edict_t *pEdictList, int edictCount, int clientMax) {
darope@0
   175
	META_DLLAPI_HANDLE_void(FN_SERVERACTIVATE, pfnServerActivate, (pEdictList, edictCount, clientMax));
darope@0
   176
	RETURN_API_void();
darope@0
   177
}
darope@0
   178
void mm_ServerDeactivate(void) {
darope@0
   179
	META_DLLAPI_HANDLE_void(FN_SERVERDEACTIVATE, pfnServerDeactivate, ());
darope@0
   180
	// Update loaded plugins.  Look for new plugins in inifile, as well as
darope@0
   181
	// any plugins waiting for a changelevel to load.  
darope@0
   182
	//
darope@0
   183
	// This is done in ServerDeactivate rather than Activate, as the latter
darope@0
   184
	// isn't actually the first routine to be called on a new map.  In
darope@0
   185
	// particular DispatchKeyValue and DispatchSpawn are called before
darope@0
   186
	// Activate, and we want any newly loaded plugins to be able to catch
darope@0
   187
	// these.
darope@0
   188
	//
darope@0
   189
	// So, we do this from Deactivate, which is the _last_ routine called
darope@0
   190
	// from the previous map.  It's also called right before shutdown,
darope@0
   191
	// which means whenever hlds quits, it'll reload the plugins just
darope@0
   192
	// before it exits, which is rather silly, but oh well.
darope@0
   193
	Plugins->refresh(PT_CHANGELEVEL);
darope@0
   194
	Plugins->unpause_all();
darope@0
   195
	// Plugins->retry_all(PT_CHANGELEVEL);
darope@158
   196
	g_Players.clear_all_cvar_queries();
bailopan@195
   197
	requestid_counter = 0;
darope@0
   198
	RETURN_API_void();
darope@0
   199
}
darope@0
   200
void mm_PlayerPreThink(edict_t *pEntity) {
darope@0
   201
	META_DLLAPI_HANDLE_void(FN_PLAYERPRETHINK, pfnPlayerPreThink, (pEntity));
darope@0
   202
	RETURN_API_void();
darope@0
   203
}
darope@0
   204
void mm_PlayerPostThink(edict_t *pEntity) {
darope@0
   205
	META_DLLAPI_HANDLE_void(FN_PLAYERPOSTTHINK, pfnPlayerPostThink, (pEntity));
darope@0
   206
	RETURN_API_void();
darope@0
   207
}
darope@0
   208
void mm_StartFrame(void) {
darope@0
   209
	META_DLLAPI_HANDLE_void(FN_STARTFRAME, pfnStartFrame, ());
darope@0
   210
	RETURN_API_void();
darope@0
   211
}
darope@0
   212
void mm_ParmsNewLevel(void) {
darope@0
   213
	META_DLLAPI_HANDLE_void(FN_PARMSNEWLEVEL, pfnParmsNewLevel, ());
darope@0
   214
	RETURN_API_void();
darope@0
   215
}
darope@0
   216
void mm_ParmsChangeLevel(void) {
darope@0
   217
	META_DLLAPI_HANDLE_void(FN_PARMSCHANGELEVEL, pfnParmsChangeLevel, ());
darope@0
   218
	RETURN_API_void();
darope@0
   219
}
darope@0
   220
const char *mm_GetGameDescription(void) {
darope@0
   221
	META_DLLAPI_HANDLE(const char *, NULL, FN_GETGAMEDESCRIPTION, pfnGetGameDescription, ());
darope@0
   222
	RETURN_API();
darope@0
   223
}
darope@0
   224
void mm_PlayerCustomization(edict_t *pEntity, customization_t *pCust) {
darope@0
   225
	META_DLLAPI_HANDLE_void(FN_PLAYERCUSTOMIZATION, pfnPlayerCustomization, (pEntity, pCust));
darope@0
   226
	RETURN_API_void();
darope@0
   227
}
darope@0
   228
void mm_SpectatorConnect(edict_t *pEntity) {
darope@0
   229
	META_DLLAPI_HANDLE_void(FN_SPECTATORCONNECT, pfnSpectatorConnect, (pEntity));
darope@0
   230
	RETURN_API_void();
darope@0
   231
}
darope@0
   232
void mm_SpectatorDisconnect(edict_t *pEntity) {
darope@0
   233
	META_DLLAPI_HANDLE_void(FN_SPECTATORDISCONNECT, pfnSpectatorDisconnect, (pEntity));
darope@0
   234
	RETURN_API_void();
darope@0
   235
}
darope@0
   236
void mm_SpectatorThink(edict_t *pEntity) {
darope@0
   237
	META_DLLAPI_HANDLE_void(FN_SPECTATORTHINK, pfnSpectatorThink, (pEntity));
darope@0
   238
	RETURN_API_void();
darope@0
   239
}
darope@0
   240
void mm_Sys_Error(const char *error_string) {
darope@0
   241
	META_DLLAPI_HANDLE_void(FN_SYS_ERROR, pfnSys_Error, (error_string));
darope@0
   242
	RETURN_API_void();
darope@0
   243
}
darope@0
   244
darope@0
   245
// From SDK pm_shared/pm_shared.c:
darope@0
   246
void mm_PM_Move (struct playermove_s *ppmove, int server) {
darope@0
   247
	META_DLLAPI_HANDLE_void(FN_PM_MOVE, pfnPM_Move, (ppmove, server));
darope@0
   248
	RETURN_API_void();
darope@0
   249
}
darope@0
   250
void mm_PM_Init(struct playermove_s *ppmove) {
darope@0
   251
	META_DLLAPI_HANDLE_void(FN_PM_INIT, pfnPM_Init, (ppmove));
darope@0
   252
	RETURN_API_void();
darope@0
   253
}
darope@0
   254
char mm_PM_FindTextureType(char *name) {
darope@0
   255
	META_DLLAPI_HANDLE(char, '\0', FN_PM_FINDTEXTURETYPE, pfnPM_FindTextureType, (name));
darope@0
   256
	RETURN_API();
darope@0
   257
}
darope@0
   258
darope@0
   259
// From SDK dlls/client.cpp:
darope@0
   260
void mm_SetupVisibility(edict_t *pViewEntity, edict_t *pClient, unsigned char **pvs, unsigned char **pas) {
darope@0
   261
	META_DLLAPI_HANDLE_void(FN_SETUPVISIBILITY, pfnSetupVisibility, (pViewEntity, pClient, pvs, pas));
darope@0
   262
	RETURN_API_void();
darope@0
   263
}
darope@0
   264
void mm_UpdateClientData (const struct edict_s *ent, int sendweapons, struct clientdata_s *cd) {
darope@0
   265
	META_DLLAPI_HANDLE_void(FN_UPDATECLIENTDATA, pfnUpdateClientData, (ent, sendweapons, cd));
darope@0
   266
	RETURN_API_void();
darope@0
   267
}
darope@0
   268
int mm_AddToFullPack(struct entity_state_s *state, int e, edict_t *ent, edict_t *host, int hostflags, int player, unsigned char *pSet) {
darope@0
   269
	META_DLLAPI_HANDLE(int, 0, FN_ADDTOFULLPACK, pfnAddToFullPack, (state, e, ent, host, hostflags, player, pSet));
darope@0
   270
	RETURN_API();
darope@0
   271
}
darope@0
   272
void mm_CreateBaseline(int player, int eindex, struct entity_state_s *baseline, struct edict_s *entity, int playermodelindex, vec3_t player_mins, vec3_t player_maxs) {
darope@0
   273
	META_DLLAPI_HANDLE_void(FN_CREATEBASELINE, pfnCreateBaseline, (player, eindex, baseline, entity, playermodelindex, player_mins, player_maxs));
darope@0
   274
	RETURN_API_void();
darope@0
   275
}
darope@0
   276
void mm_RegisterEncoders(void) {
darope@0
   277
	META_DLLAPI_HANDLE_void(FN_REGISTERENCODERS, pfnRegisterEncoders, ());
darope@0
   278
	RETURN_API_void();
darope@0
   279
}
darope@0
   280
int mm_GetWeaponData(struct edict_s *player, struct weapon_data_s *info) {
darope@0
   281
	META_DLLAPI_HANDLE(int, 0, FN_GETWEAPONDATA, pfnGetWeaponData, (player, info));
darope@0
   282
	RETURN_API();
darope@0
   283
}
darope@0
   284
void mm_CmdStart(const edict_t *player, const struct usercmd_s *cmd, unsigned int random_seed) {
darope@0
   285
	META_DLLAPI_HANDLE_void(FN_CMDSTART, pfnCmdStart, (player, cmd, random_seed));
darope@0
   286
	RETURN_API_void();
darope@0
   287
}
darope@0
   288
void mm_CmdEnd (const edict_t *player) {
darope@0
   289
	META_DLLAPI_HANDLE_void(FN_CMDEND, pfnCmdEnd, (player));
darope@0
   290
	RETURN_API_void();
darope@0
   291
}
darope@0
   292
int mm_ConnectionlessPacket(const struct netadr_s *net_from, const char *args, char *response_buffer, int *response_buffer_size) {
darope@0
   293
	META_DLLAPI_HANDLE(int, 0, FN_CONNECTIONLESSPACKET, pfnConnectionlessPacket, (net_from, args, response_buffer, response_buffer_size));
darope@0
   294
	RETURN_API();
darope@0
   295
}
darope@0
   296
int mm_GetHullBounds(int hullnumber, float *mins, float *maxs) {
darope@0
   297
	META_DLLAPI_HANDLE(int, 0, FN_GETHULLBOUNDS, pfnGetHullBounds, (hullnumber, mins, maxs));
darope@0
   298
	RETURN_API();
darope@0
   299
}
darope@0
   300
void mm_CreateInstancedBaselines (void) {
darope@0
   301
	META_DLLAPI_HANDLE_void(FN_CREATEINSTANCEDBASELINES, pfnCreateInstancedBaselines, ());
darope@0
   302
	RETURN_API_void();
darope@0
   303
}
darope@0
   304
int mm_InconsistentFile(const edict_t *player, const char *filename, char *disconnect_message) {
darope@0
   305
	META_DLLAPI_HANDLE(int, 0, FN_INCONSISTENTFILE, pfnInconsistentFile, (player, filename, disconnect_message));
darope@0
   306
	RETURN_API();
darope@0
   307
}
darope@0
   308
int mm_AllowLagCompensation(void) {
darope@0
   309
	META_DLLAPI_HANDLE(int, 0, FN_ALLOWLAGCOMPENSATION, pfnAllowLagCompensation, ());
darope@0
   310
	RETURN_API();
darope@0
   311
}
darope@0
   312
darope@0
   313
darope@0
   314
// New API functions
darope@0
   315
// From SDK ?
darope@0
   316
void mm_OnFreeEntPrivateData(edict_t *pEnt) {
darope@0
   317
	META_NEWAPI_HANDLE_void(FN_ONFREEENTPRIVATEDATA, pfnOnFreeEntPrivateData, (pEnt));
darope@0
   318
	RETURN_API_void();
darope@0
   319
}
darope@0
   320
void mm_GameShutdown(void) {
darope@0
   321
	META_NEWAPI_HANDLE_void(FN_GAMESHUTDOWN, pfnGameShutdown, ());
darope@0
   322
	RETURN_API_void();
darope@0
   323
}
darope@0
   324
int mm_ShouldCollide(edict_t *pentTouched, edict_t *pentOther) {
darope@0
   325
	META_NEWAPI_HANDLE(int, 1, FN_SHOULDCOLLIDE, pfnShouldCollide, (pentTouched, pentOther));
darope@0
   326
	RETURN_API();
darope@0
   327
}
darope@171
   328
// Added 2005-08-11 (no SDK update)
bailopan@143
   329
void mm_CvarValue(const edict_t *pEdict, const char *value) {
darope@158
   330
	g_Players.clear_player_cvar_query(pEdict);
bailopan@143
   331
	META_NEWAPI_HANDLE_void(FN_CVARVALUE, pfnCvarValue, (pEdict, value));
bailopan@143
   332
	RETURN_API_void();
bailopan@143
   333
}
darope@0
   334
darope@171
   335
//Added 2005-11-22 (no SDK update)
bailopan@170
   336
void mm_CvarValue2(const edict_t *pEdict, int requestID, const char *cvarName, const char *value) {
bailopan@170
   337
	META_NEWAPI_HANDLE_void(FN_CVARVALUE2, pfnCvarValue2, (pEdict, requestID, cvarName, value));
bailopan@170
   338
	RETURN_API_void();
bailopan@170
   339
}
bailopan@170
   340
darope@0
   341
// From SDK dlls/cbase.cpp:
darope@0
   342
// "(wd)" indicates my comments on the functions
darope@217
   343
static DLL_FUNCTIONS sFunctionTable = 
darope@0
   344
{
darope@0
   345
	mm_GameDLLInit,				//! pfnGameInit()				Initialize the game (one-time call after loading of game .dll)
darope@0
   346
	mm_DispatchSpawn,				//! pfnSpawn()
darope@0
   347
	mm_DispatchThink,				//! pfnThink()
darope@0
   348
	mm_DispatchUse,				//! pfnUse()
darope@0
   349
	mm_DispatchTouch,				//! pfnTouch()
darope@0
   350
	mm_DispatchBlocked,			//! pfnBlocked()
darope@0
   351
	mm_DispatchKeyValue,			//! pfnKeyValue()
darope@0
   352
	mm_DispatchSave,				//! pfnSave()
darope@0
   353
	mm_DispatchRestore,			//! pfnRestore()
darope@0
   354
	mm_DispatchObjectCollsionBox,	//! pfnSetAbsBox()
darope@0
   355
darope@0
   356
	mm_SaveWriteFields,			//! pfnSaveWriteFields()
darope@0
   357
	mm_SaveReadFields,				//! pfnSaveReadFields()
darope@0
   358
darope@0
   359
	mm_SaveGlobalState,			//! pfnSaveGlobalState()
darope@0
   360
	mm_RestoreGlobalState,			//! pfnRestoreGlobalState()
darope@0
   361
	mm_ResetGlobalState,			//! pfnResetGlobalState()
darope@0
   362
darope@0
   363
	mm_ClientConnect,				//! pfnClientConnect()			(wd) Client has connected
darope@0
   364
	mm_ClientDisconnect,			//! pfnClientDisconnect()		(wd) Player has left the game
darope@0
   365
	mm_ClientKill,					//! pfnClientKill()				(wd) Player has typed "kill"
darope@0
   366
	mm_ClientPutInServer,			//! pfnClientPutInServer()		(wd) Client is entering the game
darope@0
   367
	mm_ClientCommand,				//! pfnClientCommand()			(wd) Player has sent a command (typed, or from a bind)
darope@0
   368
	mm_ClientUserInfoChanged,		//! pfnClientUserInfoChanged()	(wd) Client has updated their setinfo structure
darope@0
   369
	mm_ServerActivate,				//! pfnServerActivate()			(wd) Server is starting a new map
darope@0
   370
	mm_ServerDeactivate,			//! pfnServerDeactivate()		(wd) Server is leaving the map (shutdown, or changelevel); SDK2
darope@0
   371
darope@0
   372
	mm_PlayerPreThink,				//! pfnPlayerPreThink()
darope@0
   373
	mm_PlayerPostThink,			//! pfnPlayerPostThink()
darope@0
   374
darope@0
   375
	mm_StartFrame,					//! pfnStartFrame()
darope@0
   376
	mm_ParmsNewLevel,				//! pfnParmsNewLevel()
darope@0
   377
	mm_ParmsChangeLevel,			//! pfnParmsChangeLevel()
darope@0
   378
darope@0
   379
	mm_GetGameDescription,			//! pfnGetGameDescription()		Returns string describing current .dll.  E.g. "TeamFotrress 2", "Half-Life"
darope@0
   380
	mm_PlayerCustomization,		//! pfnPlayerCustomization()	Notifies .dll of new customization for player.
darope@0
   381
darope@0
   382
	mm_SpectatorConnect,			//! pfnSpectatorConnect()		Called when spectator joins server
darope@0
   383
	mm_SpectatorDisconnect,		//! pfnSpectatorDisconnect()	Called when spectator leaves the server
darope@0
   384
	mm_SpectatorThink,				//! pfnSpectatorThink()			Called when spectator sends a command packet (usercmd_t)
darope@0
   385
darope@0
   386
	mm_Sys_Error,					//! pfnSys_Error()				Notify game .dll that engine is going to shut down.  Allows mod authors to set a breakpoint.  SDK2
darope@0
   387
darope@0
   388
	mm_PM_Move,					//! pfnPM_Move()				(wd) SDK2
darope@0
   389
	mm_PM_Init,					//! pfnPM_Init()				Server version of player movement initialization; (wd) SDK2
darope@0
   390
	mm_PM_FindTextureType,			//! pfnPM_FindTextureType()		(wd) SDK2
darope@0
   391
darope@0
   392
	mm_SetupVisibility,			//! pfnSetupVisibility()		Set up PVS and PAS for networking for this client; (wd) SDK2
darope@0
   393
	mm_UpdateClientData,			//! pfnUpdateClientData()		Set up data sent only to specific client; (wd) SDK2
darope@0
   394
	mm_AddToFullPack,				//! pfnAddToFullPack()			(wd) SDK2
darope@0
   395
	mm_CreateBaseline,				//! pfnCreateBaseline()			Tweak entity baseline for network encoding, allows setup of player baselines, too.; (wd) SDK2
darope@0
   396
	mm_RegisterEncoders,			//! pfnRegisterEncoders()		Callbacks for network encoding; (wd) SDK2
darope@0
   397
	mm_GetWeaponData,				//! pfnGetWeaponData()			(wd) SDK2
darope@0
   398
	mm_CmdStart,					//! pfnCmdStart()				(wd) SDK2
darope@0
   399
	mm_CmdEnd,						//! pfnCmdEnd()					(wd) SDK2
darope@0
   400
	mm_ConnectionlessPacket,		//! pfnConnectionlessPacket()	(wd) SDK2
darope@0
   401
	mm_GetHullBounds,				//! pfnGetHullBounds()			(wd) SDK2
darope@0
   402
	mm_CreateInstancedBaselines,	//! pfnCreateInstancedBaselines()	(wd) SDK2
darope@0
   403
	mm_InconsistentFile,			//! pfnInconsistentFile()		(wd) SDK2
darope@0
   404
	mm_AllowLagCompensation,		//! pfnAllowLagCompensation()	(wd) SDK2
darope@0
   405
};
darope@0
   406
darope@217
   407
DLL_FUNCTIONS *pHookedDllFunctions = &sFunctionTable;
bailopan@209
   408
darope@0
   409
// It's not clear what the difference is between GetAPI and GetAPI2; they
darope@0
   410
// both appear to return the exact same function table.  
darope@0
   411
//
darope@0
   412
// Only one of them appears to be ever called, though.  If the DLL provides
darope@0
   413
// GetAPI2, the engine/hlds will call that, and will not call GetAPI.  If
darope@0
   414
// the engine couldn't find GetAPI2 in the DLL, it appears to fall back to
darope@0
   415
// GetAPI.
darope@0
   416
//
darope@0
   417
// So, GetAPI2 appears to replace GetAPI, and appears to have been added
darope@0
   418
// with SDK 2.0.  My best guess is that, with the new SDK, interface
darope@0
   419
// version checking became important, and without the int ptr used in
darope@0
   420
// GetAPI2, the engine can't find out the version of the DLL via GetAPI.
darope@0
   421
//
darope@0
   422
// It's unclear whether a DLL coded under SDK2 needs to provide the older
darope@0
   423
// GetAPI or not..
darope@0
   424
darope@0
   425
C_DLLEXPORT int GetEntityAPI(DLL_FUNCTIONS *pFunctionTable, int interfaceVersion)
darope@0
   426
{
darope@0
   427
	META_DEBUG(3, ("called: GetEntityAPI; version=%d", interfaceVersion));
darope@0
   428
	if(!pFunctionTable) {
darope@0
   429
		META_ERROR("GetEntityAPI called with null pFunctionTable");
darope@0
   430
		return(FALSE);
darope@0
   431
	}
darope@0
   432
	else if(interfaceVersion != INTERFACE_VERSION) {
darope@0
   433
		META_ERROR("GetEntityAPI version mismatch; requested=%d ours=%d", interfaceVersion, INTERFACE_VERSION);
darope@0
   434
		return(FALSE);
darope@0
   435
	}
darope@217
   436
darope@217
   437
//+TBR
darope@217
   438
#ifdef MM_DEBUG	
darope@217
   439
	META_LOG( "==DEBUG==> [GetEntityAPI] Setting up engine's DLL_FUNCTIONS struct @%p, version %u.", 
darope@217
   440
				pFunctionTable, interfaceVersion);
darope@217
   441
	META_LOG( "==DEBUG==> [GetEntityAPI] Our version is %u. Our current table size is %u, %u entries.", 
darope@217
   442
				INTERFACE_VERSION,  sizeof(DLL_FUNCTIONS),
darope@217
   443
				sizeof(DLL_FUNCTIONS) / sizeof(void(*)()));
darope@217
   444
#endif
darope@217
   445
//-TBR
darope@217
   446
	
darope@217
   447
	memcpy(pFunctionTable, &sFunctionTable, sizeof(DLL_FUNCTIONS));
darope@217
   448
darope@217
   449
//+TBR
darope@217
   450
#ifdef MM_DEBUG
darope@217
   451
	if ( g_engfuncs.pfnIsDedicatedServer() ) {
darope@217
   452
		META_LOG( "==DEBUG==> [GetEntityAPI] Engine says this is a dedicated server.\n" );
darope@217
   453
	} 
darope@217
   454
	else {
darope@217
   455
		META_LOG( "==DEBUG==> [GetEntityAPI] Engine says this is a listenserver.\n" );
darope@217
   456
	}
darope@217
   457
#endif
darope@217
   458
//-TBR
darope@217
   459
darope@0
   460
	return(TRUE);
darope@0
   461
}
darope@0
   462
darope@0
   463
C_DLLEXPORT int GetEntityAPI2(DLL_FUNCTIONS *pFunctionTable, int *interfaceVersion)
darope@0
   464
{
darope@0
   465
	META_DEBUG(3, ("called: GetEntityAPI2; version=%d", *interfaceVersion));
darope@217
   466
darope@0
   467
	if(!pFunctionTable) {
darope@0
   468
		META_ERROR("GetEntityAPI2 called with null pFunctionTable");
darope@0
   469
		return(FALSE);
darope@0
   470
	}
darope@0
   471
	else if(*interfaceVersion != INTERFACE_VERSION) {
darope@0
   472
		META_ERROR("GetEntityAPI2 version mismatch; requested=%d ours=%d", *interfaceVersion, INTERFACE_VERSION);
darope@0
   473
		//! Tell engine what version we had, so it can figure out who is out of date.
darope@0
   474
		*interfaceVersion = INTERFACE_VERSION;
darope@0
   475
		return(FALSE);
darope@0
   476
	}
darope@217
   477
darope@217
   478
//+TBR
darope@217
   479
#ifdef MM_DEBUG
darope@217
   480
	META_LOG( "==DEBUG==> [GetEntityAPI2] Setting up engine's DLL_FUNCTIONS struct @%p, version %u.", 
darope@217
   481
				pFunctionTable, *interfaceVersion);
darope@217
   482
	META_LOG( "==DEBUG==> [GetEntityAPI2] Our version is %u. Our current table size is %u, %u entries.", 
darope@217
   483
				INTERFACE_VERSION,  sizeof(DLL_FUNCTIONS),
darope@217
   484
				sizeof(DLL_FUNCTIONS) / sizeof(void(*)()) );
darope@217
   485
#endif
darope@217
   486
//-TBR
darope@217
   487
	
darope@217
   488
	memcpy(pFunctionTable, &sFunctionTable, sizeof(DLL_FUNCTIONS));
darope@217
   489
darope@217
   490
//+TBR
darope@217
   491
#ifdef MM_DEBUG
darope@217
   492
	if ( g_engfuncs.pfnIsDedicatedServer() ) {
darope@217
   493
		META_LOG( "==DEBUG==> [GetEntityAPI2] Engine says this is a dedicated server.\n" );
darope@217
   494
	} 
darope@217
   495
	else {
darope@217
   496
		META_LOG( "==DEBUG==> [GetEntityAPI2] Engine says this is a listenserver.\n" );
darope@217
   497
	}
darope@217
   498
#endif
darope@217
   499
//-TBR
darope@217
   500
darope@0
   501
	return(TRUE);
darope@0
   502
}
darope@0
   503
darope@0
   504
darope@0
   505
// I could find _no_ documentation or examples for the intended use of
darope@0
   506
// NEW_DLL_FUNCTIONS.  I wouldn't have even _known_ about the
darope@0
   507
// GetNewDLLFunctions() function except for the reference in Adminmod..  It
darope@0
   508
// appears to be new with SDK 2.0.
darope@0
   509
//
darope@0
   510
// Obviously, it seems to provide additional functions to the engine, but
darope@0
   511
// it's unclear why a new table and interface were added, rather than
darope@0
   512
// appending new functions to the GetAPI table/interface.
darope@0
   513
//
darope@0
   514
// Interestingly, it appears to be called by the engine _before_ GetAPI.
darope@0
   515
darope@217
   516
static meta_new_dll_functions_t sNewFunctionTable (
darope@217
   517
	&mm_OnFreeEntPrivateData,		//! pfnOnFreeEntPrivateData()	Called right before the object's memory is freed.  Calls its destructor.
darope@217
   518
	&mm_GameShutdown,				//! pfnGameShutdown()
darope@217
   519
	&mm_ShouldCollide,				//! pfnShouldCollide()
darope@171
   520
	// Added 2005-08-11 (no SDK update)
darope@217
   521
	&mm_CvarValue,					//! pfnCvarValue()      (fz) Obsolete!  Use mm_CvarValue2 instead
darope@171
   522
	// Added 2005-11-22 (no SDK update)
darope@217
   523
	&mm_CvarValue2					//! pfnCvarValue2()     (fz) When pfnQueryClientCvarValue2() completes it will call
darope@171
   524
									//!                     pfnCvarValue2() with the request ID supplied earlier, the name of
darope@171
   525
									//!                     the cvar requested and the value of that cvar.
darope@217
   526
);
darope@0
   527
darope@217
   528
darope@217
   529
NEW_DLL_FUNCTIONS *pHookedNewDllFunctions = &sNewFunctionTable;
bailopan@209
   530
darope@0
   531
C_DLLEXPORT int GetNewDLLFunctions(NEW_DLL_FUNCTIONS *pNewFunctionTable, int *interfaceVersion) 
darope@0
   532
{
darope@0
   533
	META_DEBUG(6, ("called: GetNewDLLFunctions; version=%d", *interfaceVersion));
bailopan@195
   534
#if 0 // ~dvander - but then you can't use cvar querying on many mods...
darope@184
   535
	// Don't provide these functions to engine if gamedll doesn't provide
darope@184
   536
	// them.  Otherwise, we're in the position of having to provide answers
darope@184
   537
	// we can't necessarily provide (for instance, ShouldCollide())...
darope@184
   538
	if(!GameDLL.funcs.newapi_table)
darope@184
   539
		return(FALSE);
bailopan@195
   540
#endif
darope@0
   541
darope@0
   542
	if(!pNewFunctionTable) {
darope@0
   543
		META_ERROR("GetNewDLLFunctions called with null pNewFunctionTable");
darope@0
   544
		return(FALSE);
darope@0
   545
	}
darope@0
   546
	else if(*interfaceVersion != NEW_DLL_FUNCTIONS_VERSION) {
darope@0
   547
		META_ERROR("GetNewDLLFunctions version mismatch; requested=%d ours=%d", *interfaceVersion, NEW_DLL_FUNCTIONS_VERSION);
darope@0
   548
		//! Tell engine what version we had, so it can figure out who is out of date.
darope@0
   549
		*interfaceVersion = NEW_DLL_FUNCTIONS_VERSION;
darope@0
   550
		return(FALSE);
darope@0
   551
	}
darope@217
   552
//+TBR
darope@217
   553
#ifdef MM_DEBUG
darope@217
   554
	META_LOG( "==DEBUG==> [GetNewDLLFunctions] Setting up engine's NEW_DLL_FUNCTIONS struct @%p, version %u.",
darope@217
   555
				pNewFunctionTable, *interfaceVersion);
darope@217
   556
	META_LOG( "==DEBUG==> [GetNewDLLFunctions] Our version is %u. Our current table size is %u, %u entries.",
darope@217
   557
				NEW_DLL_FUNCTIONS_VERSION,  sizeof(NEW_DLL_FUNCTIONS),
darope@217
   558
				sizeof(NEW_DLL_FUNCTIONS) / sizeof(void(*)()) );
darope@217
   559
#endif
darope@217
   560
//-TBR
darope@217
   561
darope@217
   562
	sNewFunctionTable.copy_to(pNewFunctionTable);
darope@217
   563
darope@217
   564
//+TBR
darope@217
   565
#ifdef MM_DEBUG
darope@217
   566
	if ( g_engfuncs.pfnIsDedicatedServer() ) {
darope@217
   567
		META_LOG( "==DEBUG==> [GetNewDLLFunctions] Engine says this is a dedicated server.\n" );
darope@217
   568
	} 
darope@217
   569
	else {
darope@217
   570
		META_LOG( "==DEBUG==> [GetNewDLLFunctions] Engine says this is a listenserver.\n" );
bailopan@170
   571
	}
darope@217
   572
#endif
darope@217
   573
//-TBR
bailopan@170
   574
darope@0
   575
	return(TRUE);
darope@0
   576
}
darope@217
   577