Skip to content

Commit

Permalink
Fix SoF bad defaults when new hardware/del config
Browse files Browse the repository at this point in the history
  • Loading branch information
d3nd3 committed Jul 14, 2024
1 parent 47697eb commit fb5de69
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 15 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

## Usage
### Wine/Proton
If you are on linux, I highly recommend using wine instead of proton.
`wine SoF.exe +set console 1 +set cddir CDDIR +set wine %command%`
If you are on linux, I highly recommend using wine instead of proton. There is less screen tearing, when using true fullscreen (drm.modeset), only noticeable in slight moments.
`wine SoF.exe +set console 1 +set cddir CDDIR +set wine %command%`
However, if you do decide to use Proton, I recommend <= 4.11-13 , else you have to adjust sound frequency each startup. (It sticks to 11, but 22 is better).

Because wine has less tearing and less glitchy gun effects/visuals.
### Installation + Setup
Expand Down
5 changes: 5 additions & 0 deletions hdr/sof_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,8 @@ extern void (*orig_GL_BuildPolygonFromSurface)(void *fa);
extern int (*orig_R_Init)( void *hinstance, void *hWnd, void * unknown );
extern void (*orig_drawTeamIcons)(void * param1,void * param2,void * param3,void * param4);

//util access
extern void (*orig_Cmd_ExecuteString)(const char * string);

extern void InitDefaults(void);

9 changes: 8 additions & 1 deletion hdr/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ void writeUnsignedIntegerAt(void * addr, unsigned int value);
void writeIntegerAt(void * addr, int value);
void WriteE8Call(void * where,void * dest);
void WriteE9Jmp(void * where, void * dest);
void WriteByte(void * where, unsigned char value);
void WriteByte(void * where, unsigned char value);

cvar_t * findCvar(char * cvarname);
void setCvarUnsignedInt(cvar_t * which,unsigned int val);
void setCvarInt(cvar_t * which,int val);
void setCvarByte(cvar_t * which, unsigned char val);
void setCvarFloat(cvar_t * which, float val);
void setCvarString(cvar_t * which, char * newstr);
27 changes: 24 additions & 3 deletions src/features/ref_fixes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

#include "features.h"
#include "../DetourXS/detourxs.h"
#include "util.h"

#include "sof_compat.h"
#include "util.h"

typedef struct {
unsigned short w;
Expand Down Expand Up @@ -45,6 +46,7 @@ int (*orig_R_SetMode)(void * deviceMode) = NULL;

extern void scaledFont_init(void);

HMODULE __stdcall RefInMemory(LPCSTR lpLibFileName);
void on_ref_init(void);
void initDefaultTexSizes(void);

Expand Down Expand Up @@ -273,6 +275,8 @@ void __stdcall orig_glTexParameterf_mag_ui(int target_tex, int param_name, float
}

void refFixes_early(void) {
WriteE8Call(0x20066E75,&RefInMemory);
WriteByte(0x20066E7A,0x90);
orig_VID_LoadRefresh = DetourCreate((void*)0x20066E10,(void*)&my_VID_LoadRefresh,DETOUR_TYPE_JMP,5);
}
/*
Expand Down Expand Up @@ -308,7 +312,8 @@ void refFixes_apply(void)

_sofbuddy_minfilter_mipped = orig_Cvar_Get("_sofbuddy_minfilter_mipped","GL_LINEAR_MIPMAP_LINEAR",CVAR_ARCHIVE,&minfilter_change);
//I like GL_NEAREST here, the detail textures look crisper? debateable.
_sofbuddy_magfilter_mipped = orig_Cvar_Get("_sofbuddy_magfilter_mipped","GL_NEAREST",CVAR_ARCHIVE,&magfilter_change);
//Since it affects the bullet fire light, it should be LINEAR.
_sofbuddy_magfilter_mipped = orig_Cvar_Get("_sofbuddy_magfilter_mipped","GL_LINEAR",CVAR_ARCHIVE,&magfilter_change);

// I don't see a reason to have this set to anything but GL_NEAREST
_sofbuddy_minfilter_ui = orig_Cvar_Get("_sofbuddy_minfilter_ui","GL_NEAREST",CVAR_ARCHIVE,&minfilter_change);
Expand All @@ -326,7 +331,8 @@ re.init() is called inside VID_LoadRefresh
ref_gl.dll is loaded, until too late and r_init has already been called'
*/
qboolean my_VID_LoadRefresh( char *name )
{
{

//vid_ref loaded.
qboolean ret = orig_VID_LoadRefresh(name);

Expand Down Expand Up @@ -508,9 +514,24 @@ int my_R_SetMode(void * deviceMode) {

return ret;
}
HMODULE (__stdcall *orig_LoadLibraryA)(LPCSTR lpLibFileName) = *(unsigned int*)0x20111178;
HMODULE __stdcall RefInMemory(LPCSTR lpLibFileName)
{
HMODULE ret = orig_LoadLibraryA(lpLibFileName);
if (ret) {
WriteE8Call(0x3000FA26,&InitDefaults);
WriteByte(0x3000FA2B,0x90);
}

return ret;
}

/*
After R_Init()
*/
void on_ref_init(void)
{

setup_minmag_filters();

#ifdef FEATURE_HD_TEX
Expand Down
63 changes: 54 additions & 9 deletions src/sof_buddy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ void my_FS_InitFilesystem(void);
void my_orig_Qcommon_Init(int argc, char **argv);
qboolean my_Cbuf_AddLateCommands(void);

cvar_t *(*orig_Cvar_Get)(const char * name, const char * value, int flags, cvarcommand_t command) = 0x20021AE0;
void ( *orig_Com_Printf)(char * msg, ...) = 0x2001C6E0;
void (*orig_Qcommon_Frame) (int msec) = 0x2001F720;

void (*orig_Qcommon_Init) (int argc, char **argv) = 0x2001F390;
qboolean (*orig_Cbuf_AddLateCommands)(void) = NULL;



/*
DllMain of WSOCK32 library
Implicit Linking (Static Linking to the Import Library)
Expand Down Expand Up @@ -92,28 +101,54 @@ void afterWsockInit(void)

orig_FS_InitFilesystem = DetourCreate(0x20026980, &my_FS_InitFilesystem,DETOUR_TYPE_JMP,6);
orig_Cbuf_AddLateCommands = DetourCreate(0x20018740,&my_Cbuf_AddLateCommands,DETOUR_TYPE_JMP,5);


PrintOut(PRINT_GOOD,"SoF Buddy fully initialised!\n");
}

/*
Fix bug on proton:
Proton 3.7.8
GloriousEggProton
if vid_card or cpu_memory_using become 'modified'
causes a cascade of low performance cvars to kick in. (drivers/alldefs.cfg,geforce.cfg,cpu4.cfg,memory1.cfg)
which have very bad values in them.
they can become modified if new hardware values differ from config.cfg
*/
void InitDefaults(void)
{
orig_Cmd_ExecuteString("exec drivers/highest.cfg\nset fx_maxdebrisonscreen 128\n");

PrintOut(PRINT_GOOD,"Fixed defaults\n");
}

//Every cvar here would trigger its modified, because no cvars exist prior.
//But its loaded with its default value.
/*
Cvar_Get -> If the variable already exists, the value will not be set
This is earlier than Cbuf_AddLateCommands(), just before exec default.cfg and exec config.cfg IN Qcommon_Init()
Cvar flags are or'ed in, thus multiple calls to Cvar_Get stack the flags.
Does the command with NULL, remove the command or create multiple?
NULL does not remove previous set modified callbacks.
With a new modified callback, it overrides the currently set one.
If you want your cvar's onChange callback to be triggered by config.cfg (which sets cvars).
They have to be first created here, thus it would have 2 calls to the callback, if the config.cfg contains a different value,
than the default we supply here.
*/
void my_FS_InitFilesystem(void) {
orig_FS_InitFilesystem();

//Best place for cvars if you want config.cfg induced triggering of modified events.
//Best (Earliest)(before exec config.cfg) place for cvar creation, if you want config.cfg induced triggering of modified events.
refFixes_apply();
#ifdef FEATURE_FONT_SCALING
scaledFont_apply();
#endif



//orig_Com_Printf("End FS_InitFilesystem\n");
}
/*
Expand All @@ -126,8 +161,21 @@ void my_orig_Qcommon_Init(int argc, char **argv)
}





/*
Safest Init Location for wanting to check command line values.
Safest(Earliest) Init Location for wanting to check command line values.
Cbuf_AddEarlyCommands
processes all `+set` commands first.
Later in frame...
Cbuf_AddLateCommands
all other commands that start with + are processed.
You can mark the end of the command with `-`.
"public 1" must be set inside dedicated.cfg because dedicated.cfg overrides it? If no dedicated.cfg in user, the dedicated.cfg from pak0.pak is used.
*/
//this is called inside Qcommon_Init()
qboolean my_Cbuf_AddLateCommands(void)
Expand All @@ -136,13 +184,10 @@ qboolean my_Cbuf_AddLateCommands(void)
#ifdef FEATURE_MEDIA_TIMERS
mediaTimers_apply();
#endif


/*
CL_Init() already called, which handled VID_Init, and R_Init()
*/
return ret;
}

cvar_t *(*orig_Cvar_Get)(const char * name, const char * value, int flags, cvarcommand_t command) = 0x20021AE0;
void ( *orig_Com_Printf)(char * msg, ...) = 0x2001C6E0;
void (*orig_Qcommon_Frame) (int msec) = 0x2001F720;

void (*orig_Qcommon_Init) (int argc, char **argv) = 0x2001F390;
qboolean (*orig_Cbuf_AddLateCommands)(void) = NULL;
68 changes: 68 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
//#define __TERMINALOUT__


void (*orig_Z_Free) (void * v) = 0x2001EBC0;
char *(*orig_CopyString) (char *in) = 0x2001EA20;
void (*orig_Cmd_ExecuteString)(const char * string) = 0x200194F0;

FILE * go_logfile = NULL;

void PrintOut(int mode,char *msg,...)
Expand Down Expand Up @@ -118,6 +122,7 @@ void PrintOut(int mode,char *msg,...)
orig_Com_Printf("%s",ac_tmp);
}
#endif

}


Expand Down Expand Up @@ -165,4 +170,67 @@ void WriteByte(void * where, unsigned char value)
VirtualProtect(where, 1, PAGE_READWRITE, &dwProt);
*(unsigned char*)where = value;
VirtualProtect(where, 1, dwProt, &dwProt);
}

/*
Cvar Util Funcs
*/
cvar_t * findCvar(char * cvarname)
{
cvar_t *var;
cvar_t * cvar_vars = *(unsigned int*)0x2024B1D8;

for (var=cvar_vars ; var ; var=var->next)
if (!strcmp (cvarname, var->name))
return var;
return NULL;
}

void setCvarUnsignedInt(cvar_t * which,unsigned int val){
which->modified = true;
orig_Z_Free(which->string);
char intstring[64];
which->value = (float)val;
sprintf(intstring,"%u",val);
which->string = orig_CopyString(intstring);
}


void setCvarInt(cvar_t * which,int val){
which->modified = true;
orig_Z_Free(which->string);
char intstring[64];
which->value = (float)val;
sprintf(intstring,"%d",val);
which->string = orig_CopyString(intstring);
}

void setCvarByte(cvar_t * which, unsigned char val) {
which->modified = true;
orig_Z_Free(which->string);
char bytestring[64];
sprintf(bytestring,"%hhu",val);
which->value = atof(bytestring);
which->string = orig_CopyString(bytestring);
}


void setCvarFloat(cvar_t * which, float val) {

which->modified = true;
orig_Z_Free(which->string);
char floatstring[64];
sprintf(floatstring,"%f",val);
which->string = orig_CopyString(floatstring);
which->value = val;
}

void setCvarString(cvar_t * which, char * newstr) {

which->modified = true;
orig_Z_Free(which->string);


which->string = orig_CopyString(newstr);
which->value = atof(which->string);
}

0 comments on commit fb5de69

Please sign in to comment.