refactor: use xash3d physics interface to handle with linkents, so do not hook dlopen/dlsym while running under xash3d

add: force set sv_forcesimulating to 1 while running under xash3d
This commit is contained in:
dmitry 2021-09-30 16:37:40 +03:00
commit 3205e1253f
No known key found for this signature in database
GPG key ID: 8297CE728B7A7E37
10 changed files with 256 additions and 42 deletions

View file

@ -211,7 +211,7 @@ typedef struct enginefuncs_s {
void (*pfnDeltaUnsetFieldByIndex) (struct delta_s *pFields, int fieldNumber); void (*pfnDeltaUnsetFieldByIndex) (struct delta_s *pFields, int fieldNumber);
void (*pfnSetGroupMask) (int mask, int op); void (*pfnSetGroupMask) (int mask, int op);
int (*pfnCreateInstancedBaseline) (int classname, struct entity_state_s *baseline); int (*pfnCreateInstancedBaseline) (int classname, struct entity_state_s *baseline);
void (*pfnCvar_DirectSet) (struct cvar_t *var, char *value); void (*pfnCvar_DirectSet) (struct cvar_t *var, const char *value);
void (*pfnForceUnmodified) (FORCE_TYPE type, float *mins, float *maxs, const char *szFilename); void (*pfnForceUnmodified) (FORCE_TYPE type, float *mins, float *maxs, const char *szFilename);
void (*pfnGetPlayerStats) (const edict_t *client, int *ping, int *packet_loss); void (*pfnGetPlayerStats) (const edict_t *client, int *ping, int *packet_loss);
void (*pfnAddServerCommand) (const char *cmd_name, void (*function) ()); void (*pfnAddServerCommand) (const char *cmd_name, void (*function) ());

164
ext/hlsdk/physint.h Normal file
View file

@ -0,0 +1,164 @@
/*
physint.h - Server Physics Interface
Copyright (C) 2011 Uncle Mike
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifndef PHYSINT_H
#define PHYSINT_H
#define SV_PHYSICS_INTERFACE_VERSION 6
#define STRUCT_FROM_LINK( l, t, m ) ((t *)((byte *)l - (int)&(((t *)0)->m)))
#define EDICT_FROM_AREA( l ) STRUCT_FROM_LINK( l, edict_t, area )
// values that can be returned with pfnServerState
#define SERVER_DEAD 0
#define SERVER_LOADING 1
#define SERVER_ACTIVE 2
// LUMP reading errors
#define LUMP_LOAD_OK 0
#define LUMP_LOAD_COULDNT_OPEN 1
#define LUMP_LOAD_BAD_HEADER 2
#define LUMP_LOAD_BAD_VERSION 3
#define LUMP_LOAD_NO_EXTRADATA 4
#define LUMP_LOAD_INVALID_NUM 5
#define LUMP_LOAD_NOT_EXIST 6
#define LUMP_LOAD_MEM_FAILED 7
#define LUMP_LOAD_CORRUPTED 8
// LUMP saving errors
#define LUMP_SAVE_OK 0
#define LUMP_SAVE_COULDNT_OPEN 1
#define LUMP_SAVE_BAD_HEADER 2
#define LUMP_SAVE_BAD_VERSION 3
#define LUMP_SAVE_NO_EXTRADATA 4
#define LUMP_SAVE_INVALID_NUM 5
#define LUMP_SAVE_ALREADY_EXIST 6
#define LUMP_SAVE_NO_DATA 7
#define LUMP_SAVE_CORRUPTED 8
typedef struct areanode_s
{
int axis; // -1 = leaf node
float dist;
struct areanode_s *children[2];
link_t trigger_edicts;
link_t solid_edicts;
link_t portal_edicts;
} areanode_t;
typedef struct server_physics_api_s
{
// unlink edict from old position and link onto new
void ( *pfnLinkEdict) ( edict_t *ent, qboolean touch_triggers );
double ( *pfnGetServerTime )( void ); // unclamped
double ( *pfnGetFrameTime )( void ); // unclamped
void* ( *pfnGetModel )( int modelindex );
areanode_t* ( *pfnGetHeadnode )( void ); // AABB tree for all physic entities
int ( *pfnServerState )( void );
void ( *pfnHost_Error )( const char *error, ... ); // cause Host Error
// ONLY ADD NEW FUNCTIONS TO THE END OF THIS STRUCT. INTERFACE VERSION IS FROZEN AT 6
struct triangleapi_s *pTriAPI; // draw coliisions etc. Only for local system
// draw debug messages (must be called from DrawOrthoTriangles). Only for local system
int ( *pfnDrawConsoleString )( int x, int y, char *string );
void ( *pfnDrawSetTextColor )( float r, float g, float b );
void ( *pfnDrawConsoleStringLen )( const char *string, int *length, int *height );
void ( *Con_NPrintf )( int pos, const char *fmt, ... );
void ( *Con_NXPrintf )( struct con_nprint_s *info, const char *fmt, ... );
const char *( *pfnGetLightStyle )( int style ); // read custom appreance for selected lightstyle
void ( *pfnUpdateFogSettings )( unsigned int packed_fog );
char **(*pfnGetFilesList)( const char *pattern, int *numFiles, int gamedironly );
struct msurface_s *(*pfnTraceSurface)( edict_t *pTextureEntity, const float *v1, const float *v2 );
const byte *(*pfnGetTextureData)( unsigned int texnum );
// static allocations
void *(*pfnMemAlloc)( size_t cb, const char *filename, const int fileline );
void (*pfnMemFree)( void *mem, const char *filename, const int fileline );
// trace & contents
int (*pfnMaskPointContents)( const float *pos, int groupmask );
struct trace_t (*pfnTrace)( const float *p0, float *mins, float *maxs, const float *p1, int type, edict_t *e );
struct trace_t (*pfnTraceNoEnts)( const float *p0, float *mins, float *maxs, const float *p1, int type, edict_t *e );
int (*pfnBoxInPVS)( const float *org, const float *boxmins, const float *boxmaxs );
// message handler (missed function to write raw bytes)
void (*pfnWriteBytes)( const byte *bytes, int count );
// BSP lump management
int (*pfnCheckLump)( const char *filename, const int lump, int *lumpsize );
int (*pfnReadLump)( const char *filename, const int lump, void **lumpdata, int *lumpsize );
int (*pfnSaveLump)( const char *filename, const int lump, void *lumpdata, int lumpsize );
// FS tools
int (*pfnSaveFile)( const char *filename, const void *data, int len );
const byte *(*pfnLoadImagePixels)( const char *filename, int *width, int *height );
const char* (*pfnGetModelName)( int modelindex );
} server_physics_api_t;
// physic callbacks
typedef struct physics_interface_s
{
int version;
// passed through pfnCreate (0 is attempt to create, -1 is reject)
int ( *SV_CreateEntity )( edict_t *pent, const char *szName );
// run custom physics for each entity (return 0 to use built-in engine physic)
int ( *SV_PhysicsEntity )( edict_t *pEntity );
// spawn entities with internal mod function e.g. for re-arrange spawn order (0 - use engine parser, 1 - use mod parser)
int ( *SV_LoadEntities )( const char *mapname, char *entities );
// update conveyor belt for clients
void ( *SV_UpdatePlayerBaseVelocity )( edict_t *ent );
// The game .dll should return 1 if save game should be allowed
int ( *SV_AllowSaveGame )( void );
// ONLY ADD NEW FUNCTIONS TO THE END OF THIS STRUCT. INTERFACE VERSION IS FROZEN AT 6
// override trigger area checking and touching
int ( *SV_TriggerTouch )( edict_t *pent, edict_t *trigger );
// some engine features can be enabled only through this function
unsigned int ( *SV_CheckFeatures )( void );
// used for draw debug collisions for custom physic engine etc
void ( *DrawDebugTriangles )( void );
// used for draw debug overlay (textured)
void ( *DrawNormalTriangles )( void );
// used for draw debug messages (2d mode)
void ( *DrawOrthoTriangles )( void );
// tracing entities with SOLID_CUSTOM mode on a server (not used by pmove code)
void ( *ClipMoveToEntity)( edict_t *ent, const float *start, float *mins, float *maxs, const float *end, trace_t *trace );
// tracing entities with SOLID_CUSTOM mode on a server (only used by pmove code)
void ( *ClipPMoveToEntity)( struct physent_s *pe, const float *start, float *mins, float *maxs, const float *end, struct pmtrace_s *tr );
// called at end the frame of SV_Physics call
void ( *SV_EndFrame )( void );
// obsolete
void (*pfnPrepWorldFrame)( void );
// called through save\restore process
void (*pfnCreateEntitiesInRestoreList)( SAVERESTOREDATA *pSaveData, int levelMask, qboolean create_world );
// allocate custom string (e.g. using user implementation of stringtable, not engine strings)
string_t (*pfnAllocString)( const char *szValue );
// make custom string (e.g. using user implementation of stringtable, not engine strings)
string_t (*pfnMakeString)( const char *szValue );
// read custom string (e.g. using user implementation of stringtable, not engine strings)
const char* (*pfnGetString)( string_t iString );
// helper for restore custom decals that have custom message (e.g. Paranoia)
int (*pfnRestoreDecal)( struct decallist_s *entry, edict_t *pEdict, qboolean adjacent );
// handle custom trigger touching for player
void (*PM_PlayerTouch)( struct playermove_s *ppmove, edict_t *client );
// alloc or destroy model custom data (called only for dedicated servers, otherwise using an client version)
void (*Mod_ProcessUserData)( struct model_s *mod, qboolean create, const byte *buffer );
// select BSP-hull for trace with specified mins\maxs
void *(*SV_HullForBsp)( edict_t *ent, const float *mins, const float *maxs, float *offset );
// handle player custom think function
int (*SV_PlayerThink)( edict_t *ent, float frametime, double time );
} physics_interface_t;
#endif//PHYSINT_H

View file

@ -442,7 +442,7 @@ public:
} }
void set (const char *val) { void set (const char *val) {
engfuncs.pfnCvar_DirectSet (ptr, const_cast <char *> (val)); engfuncs.pfnCvar_DirectSet (ptr, val);
} }
}; };
@ -635,11 +635,6 @@ public:
} }
}; };
// for android
#if defined (CR_ARCH_ARM)
extern "C" void player (entvars_t *);
#endif
class EntityLinkage : public Singleton <EntityLinkage> { class EntityLinkage : public Singleton <EntityLinkage> {
private: private:
#if defined (CR_WINDOWS) #if defined (CR_WINDOWS)
@ -680,13 +675,7 @@ public:
} }
public: public:
void callPlayerFunction (edict_t *ent) { void callPlayerFunction (edict_t *ent);
#if defined (CR_ARCH_ARM)
player (&ent->v);
#else
reinterpret_cast <EntityFunction> (lookup (Game::instance ().lib ().handle (), "player")) (&ent->v);
#endif
}
public: public:
void enable () { void enable () {

View file

@ -9,6 +9,7 @@
#include <hlsdk/extdll.h> #include <hlsdk/extdll.h>
#include <hlsdk/metamod.h> #include <hlsdk/metamod.h>
#include <hlsdk/physint.h>
#include <crlib/crlib.h> #include <crlib/crlib.h>

View file

@ -1,6 +1,7 @@
YAPB_ABI_1.0 { YAPB_ABI_1.0 {
global: global:
Meta_*; Meta_*;
Server_*;
GiveFnptrsToDll; GiveFnptrsToDll;
GetBotAPI; GetBotAPI;
GetEntityAPI; GetEntityAPI;

View file

@ -671,6 +671,17 @@ void Game::checkCvarsBounds () {
ctrl.msg ("Bogus value for cvar '%s', min is '%.1f' and max is '%.1f', and we're got '%s', value reverted to default '%.1f'.", var.reg.name, var.min, var.max, str, var.initial); ctrl.msg ("Bogus value for cvar '%s', min is '%.1f' and max is '%.1f', and we're got '%s', value reverted to default '%.1f'.", var.reg.name, var.min, var.max, str, var.initial);
} }
} }
// special case for xash3d, by default engine is not calling startframe if no players on server, but our quota management and bot adding
// mechanism assumes that starframe is called even if no players on server, so, set the xash3d's sv_forcesimulating cvar to 1 in case it's not
if (is (GameFlags::Xash3D)) {
static cvar_t *sv_forcesimulating = engfuncs.pfnCVarGetPointer ("sv_forcesimulating");
if (sv_forcesimulating && sv_forcesimulating->value != 1.0f) {
game.print ("Force-enable Xash3D sv_forcesimulating cvar.");
engfuncs.pfnCVarSetFloat ("sv_forcesimulating", 1.0f);
}
}
} }
void Game::registerCvars (bool gameVars) { void Game::registerCvars (bool gameVars) {

View file

@ -7,23 +7,11 @@
#include <yapb.h> #include <yapb.h>
// until hook code will be compatible with ARM, it's here // on other tran win32/linux platforms i.e. arm we're using xash3d engine to run which exposes
#if defined(CR_ARCH_ARM) // nice interface to handle with linkents. if ever rehlds or hlds engine will ever run on ARM or
// other platforms, and you wan't to run bot on it without metamod, consider enabling LINKENT_STATIC_THUNKS
CR_EXPORT int Server_GetBlendingInterface (int version, struct sv_blending_interface_s **ppinterface, struct engine_studio_api_s *pstudio, float *rotationmatrix, float *bonetransform) { // when compiling the bot, to get it supported.
// this function synchronizes the studio model animation blending interface (i.e, what parts #if defined(LINKENT_STATIC_THUNKS)
// of the body move, which bones, which hitboxes and how) between the server and the game DLL.
// some MODs can be using a different hitbox scheme than the standard one.
auto api_GetBlendingInterface = game.lib ().resolve <decltype (&Server_GetBlendingInterface)> (__FUNCTION__);
if (!api_GetBlendingInterface) {
logger.error ("Could not resolve symbol \"%s\" in the game dll. Continuing...", __FUNCTION__);
return false;
}
return api_GetBlendingInterface (version, ppinterface, pstudio, rotationmatrix, bonetransform);
}
void forwardEntity_helper (EntityFunction &addr, const char *name, entvars_t *pev) { void forwardEntity_helper (EntityFunction &addr, const char *name, entvars_t *pev) {
if (!addr) { if (!addr) {
addr = game.lib ().resolve <EntityFunction> (name); addr = game.lib ().resolve <EntityFunction> (name);
@ -39,9 +27,6 @@ void forwardEntity_helper (EntityFunction &addr, const char *name, entvars_t *pe
static EntityFunction addr; \ static EntityFunction addr; \
forwardEntity_helper (addr, __FUNCTION__, pev); \ forwardEntity_helper (addr, __FUNCTION__, pev); \
} }
#else
#define LINK_ENTITY(entityName)
#endif
// entities in counter-strike... // entities in counter-strike...
LINK_ENTITY (DelayedUse) LINK_ENTITY (DelayedUse)
@ -248,3 +233,5 @@ LINK_ENTITY (weapon_xm1014)
LINK_ENTITY (weaponbox) LINK_ENTITY (weaponbox)
LINK_ENTITY (world_items) LINK_ENTITY (world_items)
LINK_ENTITY (worldspawn) LINK_ENTITY (worldspawn)
#endif

View file

@ -948,13 +948,56 @@ DLL_GIVEFNPTRSTODLL GiveFnptrsToDll (enginefuncs_t *table, globalvars_t *glob) {
} }
GetEngineFunctions (table, nullptr); GetEngineFunctions (table, nullptr);
// initialize dynamic linkents // initialize dynamic linkents (no memory hacking with xash3d)
if (!game.is (GameFlags::Xash3D)) {
ents.initialize (); ents.initialize ();
}
// give the engine functions to the other DLL... // give the engine functions to the other DLL...
api_GiveFnptrsToDll (table, glob); api_GiveFnptrsToDll (table, glob);
} }
CR_EXPORT int Server_GetBlendingInterface (int version, struct sv_blending_interface_s **ppinterface, struct engine_studio_api_s *pstudio, float *rotationmatrix, float *bonetransform) {
// this function synchronizes the studio model animation blending interface (i.e, what parts
// of the body move, which bones, which hitboxes and how) between the server and the game DLL.
// some MODs can be using a different hitbox scheme than the standard one.
auto api_GetBlendingInterface = game.lib ().resolve <decltype (&Server_GetBlendingInterface)> (__FUNCTION__);
if (!api_GetBlendingInterface) {
logger.error ("Could not resolve symbol \"%s\" in the game dll. Continuing...", __FUNCTION__);
return HLFalse;
}
return api_GetBlendingInterface (version, ppinterface, pstudio, rotationmatrix, bonetransform);
}
CR_EXPORT int Server_GetPhysicsInterface (int version, server_physics_api_t *physics_api, physics_interface_t *table) {
// this function handle the custom xash3d physics interface, that we're uses just for resolving
// entities between game and engine.
if (!table || !physics_api || version != SV_PHYSICS_INTERFACE_VERSION) {
return HLFalse;
}
table->version = SV_PHYSICS_INTERFACE_VERSION;
table->SV_CreateEntity = [] (edict_t *ent, const char *name) -> int {
auto func = game.lib ().resolve <EntityFunction> (name); // lookup symbol in game dll
// found one in game dll ?
if (func) {
func (&ent->v);
return HLTrue;
}
return -1;
};
table->SV_PhysicsEntity = [] (edict_t *) -> int {
return HLFalse;
};
return HLTrue;
}
DLSYM_RETURN EntityLinkage::lookup (SharedLibrary::Handle module, const char *function) { DLSYM_RETURN EntityLinkage::lookup (SharedLibrary::Handle module, const char *function) {
static const auto &gamedll = game.lib ().handle (); static const auto &gamedll = game.lib ().handle ();
static const auto &self = m_self.handle (); static const auto &self = m_self.handle ();
@ -995,6 +1038,24 @@ DLSYM_RETURN EntityLinkage::lookup (SharedLibrary::Handle module, const char *fu
return nullptr; return nullptr;
} }
void EntityLinkage::callPlayerFunction (edict_t *ent) {
EntityFunction playerFunction = nullptr;
if (game.is (GameFlags::Xash3D)) {
playerFunction = game.lib ().resolve <EntityFunction> ("player");
}
else {
playerFunction = reinterpret_cast <EntityFunction> (lookup (game.lib ().handle (), "player"));
}
if (!playerFunction) {
logger.fatal ("Cannot resolve player () function in gamedll.");
}
else {
playerFunction (&ent->v);
}
}
void EntityLinkage::initialize () { void EntityLinkage::initialize () {
if (plat.arm || game.is (GameFlags::Metamod)) { if (plat.arm || game.is (GameFlags::Metamod)) {
return; return;
@ -1011,7 +1072,7 @@ void EntityLinkage::initialize () {
} }
// add linkents for android // add linkents for android
#include "android.cpp" #include "entities.cpp"
// override new/delete globally, need to be included in .cpp file // override new/delete globally, need to be included in .cpp file
#include <crlib/override.h> #include <crlib/override.h>

View file

@ -57,7 +57,7 @@
<ClInclude Include="..\inc\version.h" /> <ClInclude Include="..\inc\version.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\src\android.cpp" /> <ClCompile Include="..\src\entities.cpp" />
<ClCompile Include="..\src\botlib.cpp" /> <ClCompile Include="..\src\botlib.cpp" />
<ClCompile Include="..\src\chatlib.cpp" /> <ClCompile Include="..\src\chatlib.cpp" />
<ClCompile Include="..\src\combat.cpp" /> <ClCompile Include="..\src\combat.cpp" />

View file

@ -194,7 +194,7 @@
<ClCompile Include="..\src\module.cpp"> <ClCompile Include="..\src\module.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\android.cpp"> <ClCompile Include="..\src\entities.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>