Skip to content

Commit

Permalink
Protection against incorrect graphics mode values, including for exte…
Browse files Browse the repository at this point in the history
…rnal HRP
  • Loading branch information
egornovivan committed Feb 28, 2024
1 parent 42ab38a commit 594be8b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 38 deletions.
133 changes: 105 additions & 28 deletions sfall/HRP/Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static bool DisableExtHRP(const char* runFileName, std::string &cmdline) {
cmdline.append(" -restart");

//MessageBoxA(0, "High Resolution Patch has been successfully deactivated.", "sfall", MB_TASKMODAL | MB_ICONINFORMATION);

FreeLibrary((HMODULE)baseDLLAddr);
ShellExecuteA(0, 0, runFileName, cmdline.c_str(), 0, SW_SHOWDEFAULT); // restart game
return true;
}
Expand Down Expand Up @@ -169,22 +169,113 @@ void Setting::init(const char* exeFileName, std::string &cmdline) {

bool hiResMode = sf::IniReader::GetConfigInt("Main", "HiResMode", 1) != 0;

if (!Setting::ExternalEnabled() && !hiResMode) return; // vanilla game mode

SCR_WIDTH = sf::IniReader::GetInt("Main", "SCR_WIDTH", 0, f2ResIni);
SCR_HEIGHT = sf::IniReader::GetInt("Main", "SCR_HEIGHT", 0, f2ResIni);
if (cmdline.find(" -restart") != std::string::npos) GetBackupFileName(exeFileName, true); // delete after restart

if (SCR_WIDTH == 0 || SCR_HEIGHT == 0) {
SCR_WIDTH = sf::IniReader::GetConfigInt("Graphics", "GraphicsWidth", 640);
SCR_HEIGHT = sf::IniReader::GetConfigInt("Graphics", "GraphicsHeight", 480);
if (Setting::ExternalEnabled() && GetFileAttributesA(f2ResIni) == INVALID_FILE_ATTRIBUTES) {
//It looks like f2_res.ini is missing, but external HRP is enabled. Let's turn it off just in case.
if (!DisableExtHRP(exeFileName, cmdline)) {
MessageBoxA(0, "An error occurred while trying to deactivate the High Resolution Patch.", "sfall", MB_TASKMODAL | MB_ICONERROR);
} else {
ExitProcess(EXIT_SUCCESS); //std::exit(EXIT_SUCCESS);
}
}

if (SCR_WIDTH < 640) SCR_WIDTH = 640;
if (SCR_HEIGHT < 480) SCR_HEIGHT = 480;
do {

int mode = sf::IniReader::GetConfigInt("Graphics", "Mode", 0);
int gmode = sf::IniReader::GetInt("Main", "GRAPHICS_MODE", 0, f2ResIni);
sf::Graphics::mode = 0;
if (sf::IniReader::GetInt("Main", "WINDOWED", 0, f2ResIni))
sf::IniReader::GetInt("Main", "WINDOWED_FULLSCREEN", 0, f2ResIni)
? sf::Graphics::mode += 2
: sf::Graphics::mode += 1;
switch (gmode) {
case 1:
sf::Graphics::mode += 1; // DD7: 1 or 2/3 (vanilla)
break;
case 2:
sf::Graphics::mode += 4; // DX9: 4 or 5/6 (sfall)
break;
default:
sf::Graphics::mode = mode;
break;
}

if ((sf::extWrapper && sf::Graphics::mode != 1) || sf::Graphics::mode < 0 || sf::Graphics::mode > 6 || gmode < 0 || gmode > 2) {
if (sf::extWrapper && sf::Graphics::mode != 1) sf::Graphics::mode = 1;
if (sf::Graphics::mode < 0 || sf::Graphics::mode > 6) sf::Graphics::mode = 0;
if (gmode) {
if (sf::Graphics::mode == 0) {
sf::IniReader::SetInt("Main", "GRAPHICS_MODE", 0, f2ResIni);
} else {
if (sf::Graphics::mode < 4) {
sf::IniReader::SetInt("Main", "GRAPHICS_MODE", 1, f2ResIni);
} else {
sf::IniReader::SetInt("Main", "GRAPHICS_MODE", 2, f2ResIni);
}
}
if (sf::Graphics::mode == 0 || sf::Graphics::mode == 1 || sf::Graphics::mode == 4) {
sf::IniReader::SetInt("Main", "WINDOWED", 0, f2ResIni);
} else {
sf::IniReader::SetInt("Main", "WINDOWED", 1, f2ResIni);
}
if (sf::Graphics::mode == 3 || sf::Graphics::mode == 6) {
sf::IniReader::SetInt("Main", "WINDOWED_FULLSCREEN", 1, f2ResIni);
} else {
sf::IniReader::SetInt("Main", "WINDOWED_FULLSCREEN", 0, f2ResIni);
}
} else {
sf::IniReader::SetConfigInt("Graphics", "Mode", sf::Graphics::mode);
}
continue;
}

if (Setting::ExternalEnabled()) {
switch (gmode) {
case 1:
if (mode != 0) {
sf::IniReader::SetConfigInt("Graphics", "Mode", 0);
FreeLibrary((HMODULE)baseDLLAddr);
ShellExecuteA(0, 0, exeFileName, cmdline.c_str(), 0, SW_SHOWDEFAULT); // restart game
ExitProcess(EXIT_SUCCESS);
}
break;
case 2:
if (mode != sf::Graphics::mode) {
sf::IniReader::SetConfigInt("Graphics", "Mode", sf::Graphics::mode);
FreeLibrary((HMODULE)baseDLLAddr);
ShellExecuteA(0, 0, exeFileName, cmdline.c_str(), 0, SW_SHOWDEFAULT); // restart game
ExitProcess(EXIT_SUCCESS);
}
break;
}
}

break;

} while (true);

if (cmdline.find(" -restart") != std::string::npos) {
GetBackupFileName(exeFileName, true); // delete after restart
sf::Graphics::IsWindowedMode = (sf::Graphics::mode == 2 || sf::Graphics::mode == 3 || sf::Graphics::mode >= 5);

if (!Setting::ExternalEnabled() && !hiResMode) return; // vanilla game mode

SCR_WIDTH = sf::IniReader::GetInt("Main", "SCR_WIDTH", 0, f2ResIni);
SCR_HEIGHT = sf::IniReader::GetInt("Main", "SCR_HEIGHT", 0, f2ResIni);
if (!Setting::ExternalEnabled()) { //f2_res.dll reads settings from f2_res.ini, so I don’t know how to implement it all correctly
//This solution is absolutely not suitable for modern FHD/UHD/2K/4K monitors
if (sf::Graphics::mode == 3 || sf::Graphics::mode == 6) {
SCR_WIDTH = GetSystemMetrics(SM_CXSCREEN);
SCR_HEIGHT = GetSystemMetrics(SM_CYSCREEN);
} else {
if (SCR_WIDTH == 0 || SCR_HEIGHT == 0) {
SCR_WIDTH = sf::IniReader::GetConfigInt("Graphics", "GraphicsWidth", 640);
SCR_HEIGHT = sf::IniReader::GetConfigInt("Graphics", "GraphicsHeight", 480);
}
}
if (SCR_WIDTH < 640) SCR_WIDTH = 640;
if (SCR_HEIGHT < 480) SCR_HEIGHT = 480;
}

if (!hiResMode) return;

if (Setting::ExternalEnabled()) {
Expand Down Expand Up @@ -217,22 +308,8 @@ void Setting::init(const char* exeFileName, std::string &cmdline) {

// Read High Resolution config

int windowed = (!sf::extWrapper && sf::IniReader::GetInt("Main", "WINDOWED", 0, f2ResIni) != 0) ? 1 : 0;
if (windowed && sf::IniReader::GetInt("Main", "WINDOWED_FULLSCREEN", 0, f2ResIni)) {
windowed += 1;
SCR_WIDTH = GetSystemMetrics(SM_CXSCREEN);
SCR_HEIGHT = GetSystemMetrics(SM_CYSCREEN);
}

int gMode = sf::IniReader::GetInt("Main", "GRAPHICS_MODE", 0, f2ResIni);
if (gMode < 0 || gMode > 2) gMode = 2;
if (gMode <= 1) sf::Graphics::mode = 1 + windowed; // DD7: 1 or 2/3 (vanilla)
if (gMode == 2) sf::Graphics::mode = 4 + windowed; // DX9: 4 or 5/6 (sfall)

if (sf::Graphics::mode == 1) {
COLOUR_BITS = sf::IniReader::GetInt("Main", "COLOUR_BITS", 32, f2ResIni);
if (COLOUR_BITS != 32 && COLOUR_BITS != 24 && COLOUR_BITS != 16) COLOUR_BITS = 32;
}
COLOUR_BITS = sf::IniReader::GetInt("Main", "COLOUR_BITS", 32, f2ResIni);
if (COLOUR_BITS != 32 && COLOUR_BITS != 24 && COLOUR_BITS != 16 && COLOUR_BITS != 8) COLOUR_BITS = 32;

if (sf::IniReader::GetInt("Main", "SCALE_2X", 0, f2ResIni)) {
if (SCR_HEIGHT < 960 && SCR_WIDTH < 1280) {
Expand Down
4 changes: 4 additions & 0 deletions sfall/IniReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,8 @@ int IniReader::SetDefaultConfigString(const char* section, const char* setting,
return instance().setString(section, setting, value, ddrawIni);
}

int IniReader::SetInt(const char* section, const char* setting, int value, const char* iniFile) {
return setInt(section, setting, value, iniFile);
}

}
2 changes: 2 additions & 0 deletions sfall/IniReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class IniReader {

static int SetDefaultConfigString(const char* section, const char* setting, const char* value);

static int SetInt(const char* section, const char* setting, int value, const char* iniFile);

void init();
void clearCache();

Expand Down
10 changes: 0 additions & 10 deletions sfall/Modules/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1246,16 +1246,6 @@ static __declspec(naked) void dump_screen_hack_replacement() {
}

void Graphics::init() {
int gMode = (HRP::Setting::ExternalEnabled()) // avoid mode mismatch between ddraw.ini and another ini file
? IniReader::GetIntDefaultConfig("Graphics", "Mode", 0)
: IniReader::GetConfigInt("Graphics", "Mode", 0);
if (gMode >= 4) Graphics::mode = gMode;

if (extWrapper || Graphics::mode < 0 || Graphics::mode > 6) {
Graphics::mode = 0;
}
Graphics::IsWindowedMode = (Graphics::mode == 2 || Graphics::mode == 3 || Graphics::mode >= 5);

if (Graphics::mode >= 4) {
dlog("Applying DX9 graphics patch.", DL_INIT);
#define _DLL_NAME "d3dx9_43.dll"
Expand Down

0 comments on commit 594be8b

Please sign in to comment.