Skip to content

Commit

Permalink
Protection against incorrect values and synchronization of graphics m…
Browse files Browse the repository at this point in the history
…ode in ini files
  • Loading branch information
egornovivan committed Feb 24, 2024
1 parent c540ce8 commit ed0d11d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 25 deletions.
105 changes: 87 additions & 18 deletions sfall/HRP/Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,59 @@ void Setting::init(const char* exeFileName, std::string &cmdline) {

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

if (cmdline.find(" -restart") != std::string::npos) {
GetBackupFileName(exeFileName, true); // delete after restart
}

//We neutralize the f2_res.ini file if external HRP is enabled
if (Setting::ExternalEnabled() && sf::IniReader::GetInt("Main", "WINDOWED", 404, f2ResIni)) {
if (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);
}
} else {
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 (sf::IniReader::GetInt("Main", "GRAPHICS_MODE", 404, f2ResIni)) {
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 = sf::IniReader::GetConfigInt("Graphics", "Mode", 0); // 0/1/2/3/4/5/6 (sfall)
if (sf::Graphics::mode < 0 || sf::Graphics::mode > 6) sf::Graphics::mode = 0;
break;
}
sf::IniReader::SetConfigInt("Graphics", "Mode", sf::Graphics::mode);
sf::IniReader::SetInt("Main", "GRAPHICS_MODE", 0, f2ResIni);
sf::IniReader::SetInt("Main", "WINDOWED", 0, f2ResIni);
sf::IniReader::SetInt("Main", "WINDOWED_FULLSCREEN", 0, f2ResIni);
ShellExecuteA(0, 0, exeFileName, 0, 0, SW_SHOWDEFAULT); // restart game
ExitProcess(EXIT_SUCCESS);
}
}

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

SCR_WIDTH = sf::IniReader::GetInt("Main", "SCR_WIDTH", 0, f2ResIni);
SCR_WIDTH = sf::IniReader::GetInt("Main", "SCR_WIDTH", 0, f2ResIni);
SCR_HEIGHT = sf::IniReader::GetInt("Main", "SCR_HEIGHT", 0, f2ResIni);

if (SCR_WIDTH == 0 || SCR_HEIGHT == 0) {
SCR_WIDTH = sf::IniReader::GetConfigInt("Graphics", "GraphicsWidth", 640);
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 (cmdline.find(" -restart") != std::string::npos) {
GetBackupFileName(exeFileName, true); // delete after restart
}
if (!hiResMode) return;

if (Setting::ExternalEnabled()) {
Expand Down Expand Up @@ -217,22 +254,54 @@ 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);
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 (sf::IniReader::GetInt("Main", "GRAPHICS_MODE", 404, f2ResIni)) {
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 = sf::IniReader::GetConfigInt("Graphics", "Mode", 0); // 0/1/2/3/4/5/6 (sfall)
if (sf::Graphics::mode < 0 || sf::Graphics::mode > 6) {
sf::Graphics::mode = 0;
sf::IniReader::SetConfigInt("Graphics", "Mode", sf::Graphics::mode);
}
if (sf::Graphics::mode && GetFileAttributesA(f2ResIni) != INVALID_FILE_ATTRIBUTES) {
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 == 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);
}
}
break;
}

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::extWrapper && sf::Graphics::mode > 1) sf::Graphics::mode = 1;

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;
}
//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);
//}

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
15 changes: 8 additions & 7 deletions sfall/Modules/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1246,14 +1246,15 @@ 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;
if (HRP::Setting::ExternalEnabled()) {
Graphics::mode = IniReader::GetConfigInt("Graphics", "Mode", 0); // 0/1/2/3/4/5/6 (sfall)
if (Graphics::mode < 0 || Graphics::mode > 6) {
Graphics::mode = 0;
IniReader::SetConfigInt("Graphics", "Mode", Graphics::mode);
}
if (extWrapper && Graphics::mode > 1) Graphics::mode = 1;
}

Graphics::IsWindowedMode = (Graphics::mode == 2 || Graphics::mode == 3 || Graphics::mode >= 5);

if (Graphics::mode >= 4) {
Expand Down

0 comments on commit ed0d11d

Please sign in to comment.