From e99fe0913744de136c81b272c17df7d579e35e11 Mon Sep 17 00:00:00 2001 From: NovaRain Date: Sat, 13 Mar 2021 13:47:27 +0800 Subject: [PATCH] Moved INI reading functions from main.cpp to IniReader.cpp --- sfall/CRC.cpp | 4 +- sfall/CheckAddress.cpp | 4 +- sfall/IniReader.cpp | 123 ++++++++++++++++++++++ sfall/IniReader.h | 78 ++++++++++++++ sfall/Logging.cpp | 8 +- sfall/Modules/AI.cpp | 2 +- sfall/Modules/BugFixes.cpp | 53 +++++----- sfall/Modules/DebugEditor.cpp | 6 +- sfall/Modules/Graphics.cpp | 2 +- sfall/Modules/HookScripts.cpp | 2 +- sfall/Modules/ScriptExtender.cpp | 2 +- sfall/Modules/Scripting/Handlers/Misc.cpp | 4 +- sfall/Modules/Scripting/Opcodes.cpp | 2 +- sfall/Modules/Sound.cpp | 2 +- sfall/ddraw.vcxproj | 2 + sfall/ddraw.vcxproj.filters | 2 + sfall/main.cpp | 82 ++------------- sfall/main.h | 40 +------ 18 files changed, 259 insertions(+), 159 deletions(-) create mode 100644 sfall/IniReader.cpp create mode 100644 sfall/IniReader.h diff --git a/sfall/CRC.cpp b/sfall/CRC.cpp index 82f94616..2ede92db 100644 --- a/sfall/CRC.cpp +++ b/sfall/CRC.cpp @@ -83,7 +83,7 @@ void CRC(const char* filepath) { DWORD size = GetFileSize(h, 0), crc; bool sizeMatch = (size == ExpectedSize); - if (!sizeMatch && iniGetInt("Debugging", "SkipSizeCheck", 0, ::sfall::ddrawIni)) { + if (!sizeMatch && IniReader::GetIntDefaultConfig("Debugging", "SkipSizeCheck", 0)) { sizeMatch = true; } @@ -99,7 +99,7 @@ void CRC(const char* filepath) { bool matchedCRC = false; - auto extraCrcList = GetIniList("Debugging", "ExtraCRC", "", 512, ',', ::sfall::ddrawIni); + auto extraCrcList = IniReader::GetListDefaultConfig("Debugging", "ExtraCRC", "", 512, ','); if (!extraCrcList.empty()) { matchedCRC = std::any_of(extraCrcList.begin(), extraCrcList.end(), [crc](const std::string& testCrcStr) { auto testedCrc = strtoul(testCrcStr.c_str(), 0, 16); diff --git a/sfall/CheckAddress.cpp b/sfall/CheckAddress.cpp index b5980591..5f1a1a3e 100644 --- a/sfall/CheckAddress.cpp +++ b/sfall/CheckAddress.cpp @@ -41,7 +41,7 @@ static std::vector hackAddr = { {0x4C06D1, 5}, // module: AI - {0x4290B6, 5}, + //{0x4290B6, 5}, // module: BarBoxes {0x461342, 5}, {0x461243, 5}, {0x461493, 5}, //{0x461495, 1}, @@ -128,7 +128,7 @@ static std::vector hackAddr = { // Checking for conflicts requires all options in ddraw.ini to be enabled void PrintAddrList() { - long level = iniGetInt("Debugging", "Enable", 0, ::sfall::ddrawIni); + long level = IniReader::GetIntDefaultConfig("Debugging", "Enable", 0); if (level < 10) hackAddr.clear(); std::vector sortAddr(hackAddr); diff --git a/sfall/IniReader.cpp b/sfall/IniReader.cpp new file mode 100644 index 00000000..8819de9b --- /dev/null +++ b/sfall/IniReader.cpp @@ -0,0 +1,123 @@ +/* + * sfall + * Copyright (C) 2008-2021 The sfall team + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "Utils.h" + +#include "IniReader.h" + +namespace sfall +{ + +DWORD IniReader::modifiedIni; + +static const char* ddrawIni = ".\\ddraw.ini"; +static char ini[65] = ".\\"; +static char translationIni[65]; + +const char* IniReader::GetConfigFile() { + return ini; +} + +void IniReader::SetDefaultConfigFile() { + std::strcpy(&ini[2], &ddrawIni[2]); +} + +void IniReader::SetConfigFile(const char* iniFile) { + strcat_s(ini, iniFile); +} + +int IniReader::GetIntDefaultConfig(const char* section, const char* setting, int defaultValue) { + return iniGetInt(section, setting, defaultValue, ddrawIni); +} + +std::vector IniReader::GetListDefaultConfig(const char* section, const char* setting, const char* defaultValue, size_t bufSize, char delimiter) { + return GetIniList(section, setting, defaultValue, bufSize, delimiter, ddrawIni); +} + +size_t IniReader::Translate(const char* section, const char* setting, const char* defaultValue, char* buffer, size_t bufSize) { + return iniGetString(section, setting, defaultValue, buffer, bufSize, translationIni); +} + +int IniReader::SetConfigInt(const char* section, const char* setting, int value) { + char buf[33]; + _itoa_s(value, buf, 33, 10); + int result = WritePrivateProfileStringA(section, setting, buf, ini); + return result; +} + +//////////////////////////////////////////////////////////////////////////////// + +int iniGetInt(const char* section, const char* setting, int defaultValue, const char* iniFile) { + return GetPrivateProfileIntA(section, setting, defaultValue, iniFile); +} + +size_t iniGetString(const char* section, const char* setting, const char* defaultValue, char* buf, size_t bufSize, const char* iniFile) { + return GetPrivateProfileStringA(section, setting, defaultValue, buf, bufSize, iniFile); +} + +std::string GetIniString(const char* section, const char* setting, const char* defaultValue, size_t bufSize, const char* iniFile) { + char* buf = new char[bufSize]; + iniGetString(section, setting, defaultValue, buf, bufSize, iniFile); + std::string str(buf); + delete[] buf; + return str; +} + +std::vector GetIniList(const char* section, const char* setting, const char* defaultValue, size_t bufSize, char delimiter, const char* iniFile) { + auto list = split(GetIniString(section, setting, defaultValue, bufSize, iniFile), delimiter); + std::transform(list.cbegin(), list.cend(), list.begin(), trim); + return list; +} + +/* + For ddraw.ini config +*/ +unsigned int GetConfigInt(const char* section, const char* setting, int defaultValue) { + return iniGetInt(section, setting, defaultValue, ini); +} + +std::string GetConfigString(const char* section, const char* setting, const char* defaultValue, size_t bufSize) { + return trim(GetIniString(section, setting, defaultValue, bufSize, ini)); +} + +size_t GetConfigString(const char* section, const char* setting, const char* defaultValue, char* buf, size_t bufSize) { + return iniGetString(section, setting, defaultValue, buf, bufSize, ini); +} + +std::vector GetConfigList(const char* section, const char* setting, const char* defaultValue, size_t bufSize) { + return GetIniList(section, setting, defaultValue, bufSize, ',', ini); +} + +std::vector TranslateList(const char* section, const char* setting, const char* defaultValue, char delimiter, size_t bufSize) { + return GetIniList(section, setting, defaultValue, bufSize, delimiter, translationIni); +} + +std::string Translate(const char* section, const char* setting, const char* defaultValue, size_t bufSize) { + return GetIniString(section, setting, defaultValue, bufSize, translationIni); +} + +size_t Translate(const char* section, const char* setting, const char* defaultValue, char* buffer, size_t bufSize) { + return iniGetString(section, setting, defaultValue, buffer, bufSize, translationIni); +} + +void IniReader::init() { + modifiedIni = GetConfigInt("Main", "ModifiedIni", 0); + GetConfigString("Main", "TranslationsINI", ".\\Translations.ini", translationIni, 65); +} + +} diff --git a/sfall/IniReader.h b/sfall/IniReader.h new file mode 100644 index 00000000..20d4c00f --- /dev/null +++ b/sfall/IniReader.h @@ -0,0 +1,78 @@ +/* + * sfall + * Copyright (C) 2008-2021 The sfall team + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +namespace sfall +{ + +class IniReader { +public: + static void init(); + + static DWORD IniReader::modifiedIni; + + static void IniReader::SetDefaultConfigFile(); + static void IniReader::SetConfigFile(const char* iniFile); + static const char* IniReader::GetConfigFile(); + + // Gets the integer value from the default config (i.e. ddraw.ini) + static int IniReader::GetIntDefaultConfig(const char* section, const char* setting, int defaultValue); + + // Gets a list of values separated by the delimiter from the default config (i.e. ddraw.ini) + static std::vector IniReader::GetListDefaultConfig(const char* section, const char* setting, const char* defaultValue, size_t bufSize, char delimiter); + + // Translates given string using sfall translation INI file and puts the result into given buffer + static size_t IniReader::Translate(const char* section, const char* setting, const char* defaultValue, char* buffer, size_t bufSize); + + static int IniReader::SetConfigInt(const char* section, const char* setting, int value); +}; + +// Gets the integer value from given INI file +int iniGetInt(const char* section, const char* setting, int defaultValue, const char* iniFile); + +// Gets the string value from given INI file +size_t iniGetString(const char* section, const char* setting, const char* defaultValue, char* buf, size_t bufSize, const char* iniFile); + +// Gets the string value from given INI file +std::string GetIniString(const char* section, const char* setting, const char* defaultValue, size_t bufSize, const char* iniFile); + +// Parses the comma-separated list setting from given INI file +std::vector GetIniList(const char* section, const char* setting, const char* defaultValue, size_t bufSize, char delimiter, const char* iniFile); + +// Gets the integer value from sfall configuration INI file +unsigned int GetConfigInt(const char* section, const char* setting, int defaultValue); + +// Gets the string value from sfall configuration INI file with trim function +std::string GetConfigString(const char* section, const char* setting, const char* defaultValue, size_t bufSize = 128); + +// Loads the string value from sfall configuration INI file into the provided buffer +size_t GetConfigString(const char* section, const char* setting, const char* defaultValue, char* buffer, size_t bufSize = 128); + +// Parses the comma-separated list from the settings from sfall configuration INI file +std::vector GetConfigList(const char* section, const char* setting, const char* defaultValue, size_t bufSize = 128); + +std::vector TranslateList(const char* section, const char* setting, const char* defaultValue, char delimiter, size_t bufSize = 256); + +// Translates given string using sfall translation INI file +std::string Translate(const char* section, const char* setting, const char* defaultValue, size_t bufSize = 128); + +// Translates given string using sfall translation INI file and puts the result into given buffer +size_t Translate(const char* section, const char* setting, const char* defaultValue, char* buffer, size_t bufSize = 128); + +} diff --git a/sfall/Logging.cpp b/sfall/Logging.cpp index 4a00c069..f262c8d8 100644 --- a/sfall/Logging.cpp +++ b/sfall/Logging.cpp @@ -74,16 +74,16 @@ void devlog_f(...) {} void LoggingInit() { Log.open("sfall-log.txt", std::ios_base::out | std::ios_base::trunc); - if (iniGetInt("Debugging", "Init", 0, ::sfall::ddrawIni)) { + if (IniReader::GetIntDefaultConfig("Debugging", "Init", 0)) { DebugTypes |= DL_INIT; } - if (iniGetInt("Debugging", "Hook", 0, ::sfall::ddrawIni)) { + if (IniReader::GetIntDefaultConfig("Debugging", "Hook", 0)) { DebugTypes |= DL_HOOK; } - if (iniGetInt("Debugging", "Script", 0, ::sfall::ddrawIni)) { + if (IniReader::GetIntDefaultConfig("Debugging", "Script", 0)) { DebugTypes |= DL_SCRIPT; } - if (iniGetInt("Debugging", "Criticals", 0, ::sfall::ddrawIni)) { + if (IniReader::GetIntDefaultConfig("Debugging", "Criticals", 0)) { DebugTypes |= DL_CRITICALS; } } diff --git a/sfall/Modules/AI.cpp b/sfall/Modules/AI.cpp index 3150c6b6..64097915 100644 --- a/sfall/Modules/AI.cpp +++ b/sfall/Modules/AI.cpp @@ -544,7 +544,7 @@ void AI::init() { } #ifndef NDEBUG - if (iniGetInt("Debugging", "AIBugFixes", 1, ::sfall::ddrawIni) == 0) return; + if (IniReader::GetIntDefaultConfig("Debugging", "AIBugFixes", 1) == 0) return; #endif // Fix for NPCs not fully reloading a weapon if it has an ammo capacity more than a box of ammo diff --git a/sfall/Modules/BugFixes.cpp b/sfall/Modules/BugFixes.cpp index 7097a746..3f9708ab 100644 --- a/sfall/Modules/BugFixes.cpp +++ b/sfall/Modules/BugFixes.cpp @@ -18,7 +18,7 @@ static DWORD critterBody = 0; static DWORD sizeOnBody = 0; static DWORD weightOnBody = 0; -static char tempBuffer[355]; +static char messageBuffer[355]; /* Saving a list of PIDs for saved drug effects @@ -1594,27 +1594,27 @@ static bool showItemDescription = false; static void __stdcall AppendText(const char* text, const char* desc) { if (showItemDescription && currDescLen == 0) { - strncpy_s(tempBuffer, desc, 161); - int len = strlen(tempBuffer); + strncpy_s(messageBuffer, desc, 161); + int len = strlen(messageBuffer); if (len > 160) { len = 158; - tempBuffer[len++] = '.'; - tempBuffer[len++] = '.'; - tempBuffer[len++] = '.'; + messageBuffer[len++] = '.'; + messageBuffer[len++] = '.'; + messageBuffer[len++] = '.'; } - tempBuffer[len++] = ' '; - tempBuffer[len] = 0; + messageBuffer[len++] = ' '; + messageBuffer[len] = 0; currDescLen = len; } else if (currDescLen == 0) { - tempBuffer[0] = 0; + messageBuffer[0] = 0; } - strncat(tempBuffer, text, 64); + strncat(messageBuffer, text, 64); currDescLen += strlen(text); if (currDescLen < 300) { - tempBuffer[currDescLen++] = '.'; - tempBuffer[currDescLen++] = ' '; - tempBuffer[currDescLen] = 0; + messageBuffer[currDescLen++] = '.'; + messageBuffer[currDescLen++] = ' '; + messageBuffer[currDescLen] = 0; } } @@ -1639,7 +1639,7 @@ static void __declspec(naked) obj_examine_func_hack_ammo1() { push eax; call AppendText; mov currDescLen, 0; - lea eax, [tempBuffer]; + lea eax, [messageBuffer]; jmp fo::funcoffs::gdialogDisplayMsg_; skip: jmp dword ptr [esp + 0x1AC - 0x14 + 4]; @@ -1656,9 +1656,9 @@ static void __declspec(naked) obj_examine_func_hack_weapon() { call AppendText; mov eax, currDescLen; sub eax, 2; - mov byte ptr tempBuffer[eax], 0; // cutoff last character + mov byte ptr messageBuffer[eax], 0; // cutoff last character mov currDescLen, 0; - lea eax, [tempBuffer]; + lea eax, [messageBuffer]; skip: jmp ObjExamineFuncWeapon_Ret; } @@ -1762,7 +1762,7 @@ static void __declspec(naked) wmSetupRandomEncounter_hook() { push eax; // text 2 push edi; // text 1 push 0x500B64; // fmt '%s %s' - lea edi, tempBuffer; + lea edi, messageBuffer; push edi; // buf call fo::funcoffs::sprintf_; add esp, 16; @@ -2289,13 +2289,11 @@ dude: } } -static char pickupMessageBuf[65] = {0}; +static char pickupMessage[65] = {0}; + static const char* __fastcall GetPickupMessage(const char* name) { - if (pickupMessageBuf[0] == 0) { - Translate("sfall", "NPCPickupFail", "%s cannot pick up the item.", pickupMessageBuf, 64); - } - sprintf(tempBuffer, pickupMessageBuf, name); - return tempBuffer; + std::sprintf(messageBuffer, pickupMessage, name); + return messageBuffer; } static void __declspec(naked) obj_pickup_hook_message() { @@ -2703,11 +2701,11 @@ skip0: call fo::funcoffs::item_caps_total_; push eax; // caps push 0x502B1C; // fmt: $%d - lea eax, tempBuffer; + lea eax, messageBuffer; push eax; call fo::funcoffs::sprintf_; add esp, 3 * 4; - lea eax, tempBuffer; + lea eax, messageBuffer; call ds:[FO_VAR_text_width]; mov edx, 60; // max width mov ebx, eax; // ebx - textWidth @@ -2721,7 +2719,7 @@ skip0: push 36; // y sar eax, 1; sub ecx, eax; // x shift - lea edx, tempBuffer; + lea edx, messageBuffer; mov eax, ds:[FO_VAR_dialogueWindow]; call fo::funcoffs::win_print_; test ebp, ebp; @@ -2887,7 +2885,7 @@ void BugFixes::init() { #ifndef NDEBUG LoadGameHook::OnBeforeGameClose() += PrintAddrList; - if (iniGetInt("Debugging", "BugFixes", 1, ::sfall::ddrawIni) == 0) return; + if (IniReader::GetIntDefaultConfig("Debugging", "BugFixes", 1) == 0) return; #endif // Missing game initialization @@ -3495,6 +3493,7 @@ void BugFixes::init() // up an item due to not enough space in the inventory HookCall(0x49B6E7, obj_pickup_hook); HookCall(0x49B71C, obj_pickup_hook_message); + IniReader::Translate("sfall", "NPCPickupFail", "%s cannot pick up the item.", pickupMessage, 64); // Fix for anim_move_to_tile_ engine function ignoring the distance argument for the player HookCall(0x416D44, anim_move_to_tile_hook); diff --git a/sfall/Modules/DebugEditor.cpp b/sfall/Modules/DebugEditor.cpp index 7a2f1614..e10ab3ac 100644 --- a/sfall/Modules/DebugEditor.cpp +++ b/sfall/Modules/DebugEditor.cpp @@ -405,7 +405,7 @@ static const DWORD addrNewLineChar[] = { }; static void DebugModePatch() { - int dbgMode = iniGetInt("Debugging", "DebugMode", 0, ::sfall::ddrawIni); + int dbgMode = IniReader::GetIntDefaultConfig("Debugging", "DebugMode", 0); if (dbgMode > 0) { dlog("Applying debugmode patch.", DL_INIT); // If the player is using an exe with the debug patch already applied, just skip this block without erroring @@ -427,7 +427,7 @@ static void DebugModePatch() { } else { SafeWrite32(0x4C6D9C, (DWORD)debugGnw); } - if (iniGetInt("Debugging", "HideObjIsNullMsg", 0, ::sfall::ddrawIni)) { + if (IniReader::GetIntDefaultConfig("Debugging", "HideObjIsNullMsg", 0)) { MakeJump(0x453FD2, dbg_error_hack); } // prints a debug message about a missing critter art file to both debug.log and the message window in sfall debugging mode @@ -462,7 +462,7 @@ static void DebugModePatch() { } static void DontDeleteProtosPatch() { - if (iniGetInt("Debugging", "DontDeleteProtos", 0, ::sfall::ddrawIni)) { + if (IniReader::GetIntDefaultConfig("Debugging", "DontDeleteProtos", 0)) { dlog("Applying permanent protos patch.", DL_INIT); SafeWrite8(0x48007E, CodeType::JumpShort); dlogr(" Done", DL_INIT); diff --git a/sfall/Modules/Graphics.cpp b/sfall/Modules/Graphics.cpp index f6552c53..975e76e7 100644 --- a/sfall/Modules/Graphics.cpp +++ b/sfall/Modules/Graphics.cpp @@ -1216,7 +1216,7 @@ void Graphics::exit() { if (Graphics::mode) { if (Graphics::mode == 5) { int data = windowTop | (windowLeft << 16); - if (data >= 0 && data != windowData) SetConfigInt("Graphics", "WindowData", data); + if (data >= 0 && data != windowData) IniReader::SetConfigInt("Graphics", "WindowData", data); } CoUninitialize(); } diff --git a/sfall/Modules/HookScripts.cpp b/sfall/Modules/HookScripts.cpp index c0c34f18..ae4b3865 100644 --- a/sfall/Modules/HookScripts.cpp +++ b/sfall/Modules/HookScripts.cpp @@ -255,7 +255,7 @@ void HookScripts::init() { LoadGameHook::OnGameModeChange() += HookCommon::GameModeChangeHook; LoadGameHook::OnAfterGameStarted() += SourceUseSkillOnInit; - injectAllHooks = isDebug && (iniGetInt("Debugging", "InjectAllGameHooks", 0, ::sfall::ddrawIni) != 0); + injectAllHooks = isDebug && (IniReader::GetIntDefaultConfig("Debugging", "InjectAllGameHooks", 0) != 0); if (injectAllHooks) dlogr("Injecting all game hooks.", DL_HOOK|DL_INIT); } diff --git a/sfall/Modules/ScriptExtender.cpp b/sfall/Modules/ScriptExtender.cpp index ba98cc27..d8c1491a 100644 --- a/sfall/Modules/ScriptExtender.cpp +++ b/sfall/Modules/ScriptExtender.cpp @@ -943,7 +943,7 @@ void ScriptExtender::init() { } } - alwaysFindScripts = isDebug && (iniGetInt("Debugging", "AlwaysFindScripts", 0, ::sfall::ddrawIni) != 0); + alwaysFindScripts = isDebug && (IniReader::GetIntDefaultConfig("Debugging", "AlwaysFindScripts", 0) != 0); if (alwaysFindScripts) dlogr("Always searching for global/hook scripts behavior enabled.", DL_SCRIPT); MakeJump(0x4A390C, scr_find_sid_from_program_hack); diff --git a/sfall/Modules/Scripting/Handlers/Misc.cpp b/sfall/Modules/Scripting/Handlers/Misc.cpp index 968eaf9a..d5aabbe2 100644 --- a/sfall/Modules/Scripting/Handlers/Misc.cpp +++ b/sfall/Modules/Scripting/Handlers/Misc.cpp @@ -101,7 +101,7 @@ void __declspec(naked) op_eax_available() { } static bool IsSpecialIni(const char* str, const char* end) { - const char* pos = strfind(str, &::sfall::ddrawIni[2]); + const char* pos = strfind(str, &IniReader::GetConfigFile()[2]); // TODO test if (pos && pos < end) return true; pos = strfind(str, "f2_res.ini"); if (pos && pos < end) return true; @@ -345,7 +345,7 @@ fail: void __declspec(naked) op_modified_ini() { __asm { - mov edx, modifiedIni; + mov edx, IniReader::modifiedIni; _J_RET_VAL_TYPE(VAR_TYPE_INT); } } diff --git a/sfall/Modules/Scripting/Opcodes.cpp b/sfall/Modules/Scripting/Opcodes.cpp index a0a87225..5da383c2 100644 --- a/sfall/Modules/Scripting/Opcodes.cpp +++ b/sfall/Modules/Scripting/Opcodes.cpp @@ -289,7 +289,7 @@ void InitNewOpcodes() { ForceEncounterRestore(); // restore if the encounter did not happen }; - if (int unsafe = iniGetInt("Debugging", "AllowUnsafeScripting", 0, ::sfall::ddrawIni)) { + if (int unsafe = IniReader::GetIntDefaultConfig("Debugging", "AllowUnsafeScripting", 0)) { if (unsafe == 2) checkValidMemAddr = false; dlogr(" Unsafe opcodes enabled.", DL_SCRIPT); opcodes[0x1cf] = op_write_byte; diff --git a/sfall/Modules/Sound.cpp b/sfall/Modules/Sound.cpp index 4b228616..aae1551f 100644 --- a/sfall/Modules/Sound.cpp +++ b/sfall/Modules/Sound.cpp @@ -1008,7 +1008,7 @@ void Sound::init() { HookCall(0x42B849, ai_print_msg_hook); //Yes, I did leave this in on purpose. Will be of use to anyone trying to add in the sound effects - if (isDebug && iniGetInt("Debugging", "Test_ForceFloats", 0, ::sfall::ddrawIni)) { + if (isDebug && IniReader::GetIntDefaultConfig("Debugging", "Test_ForceFloats", 0)) { SafeWrite8(0x42B6F5, CodeType::JumpShort); // bypass chance } } diff --git a/sfall/ddraw.vcxproj b/sfall/ddraw.vcxproj index 7e86a0c3..1c8d6953 100644 --- a/sfall/ddraw.vcxproj +++ b/sfall/ddraw.vcxproj @@ -297,6 +297,7 @@ + @@ -405,6 +406,7 @@ + diff --git a/sfall/ddraw.vcxproj.filters b/sfall/ddraw.vcxproj.filters index d3a04ec1..423262de 100644 --- a/sfall/ddraw.vcxproj.filters +++ b/sfall/ddraw.vcxproj.filters @@ -338,6 +338,7 @@ Modules\Scripting\Handlers + @@ -626,6 +627,7 @@ Modules\Scripting\Handlers + diff --git a/sfall/main.cpp b/sfall/main.cpp index 0786d486..b2c0f9c5 100644 --- a/sfall/main.cpp +++ b/sfall/main.cpp @@ -83,7 +83,6 @@ #include "SimplePatch.h" #include "Logging.h" #include "ReplacementFuncs.h" -#include "Utils.h" #include "Version.h" #include "main.h" @@ -98,78 +97,12 @@ bool isDebug = false; bool hrpIsEnabled = false; bool hrpVersionValid = false; // HRP 4.1.8 version validation -const char ddrawIni[] = ".\\ddraw.ini"; -static char ini[65] = ".\\"; -static char translationIni[65]; - -DWORD modifiedIni; DWORD hrpDLLBaseAddr = 0x10000000; DWORD HRPAddress(DWORD addr) { return (hrpDLLBaseAddr + (addr & 0xFFFFF)); } -int iniGetInt(const char* section, const char* setting, int defaultValue, const char* iniFile) { - return GetPrivateProfileIntA(section, setting, defaultValue, iniFile); -} - -size_t iniGetString(const char* section, const char* setting, const char* defaultValue, char* buf, size_t bufSize, const char* iniFile) { - return GetPrivateProfileStringA(section, setting, defaultValue, buf, bufSize, iniFile); -} - -std::string GetIniString(const char* section, const char* setting, const char* defaultValue, size_t bufSize, const char* iniFile) { - char* buf = new char[bufSize]; - iniGetString(section, setting, defaultValue, buf, bufSize, iniFile); - std::string str(buf); - delete[] buf; - return str; -} - -std::vector GetIniList(const char* section, const char* setting, const char* defaultValue, size_t bufSize, char delimiter, const char* iniFile) { - auto list = split(GetIniString(section, setting, defaultValue, bufSize, iniFile), delimiter); - std::transform(list.cbegin(), list.cend(), list.begin(), trim); - return list; -} - -/* - For ddraw.ini config -*/ -unsigned int GetConfigInt(const char* section, const char* setting, int defaultValue) { - return iniGetInt(section, setting, defaultValue, ini); -} - -std::string GetConfigString(const char* section, const char* setting, const char* defaultValue, size_t bufSize) { - return trim(GetIniString(section, setting, defaultValue, bufSize, ini)); -} - -size_t GetConfigString(const char* section, const char* setting, const char* defaultValue, char* buf, size_t bufSize) { - return iniGetString(section, setting, defaultValue, buf, bufSize, ini); -} - -std::vector GetConfigList(const char* section, const char* setting, const char* defaultValue, size_t bufSize) { - return GetIniList(section, setting, defaultValue, bufSize, ',', ini); -} - -std::vector TranslateList(const char* section, const char* setting, const char* defaultValue, char delimiter, size_t bufSize) { - return GetIniList(section, setting, defaultValue, bufSize, delimiter, translationIni); -} - -std::string Translate(const char* section, const char* setting, const char* defaultValue, size_t bufSize) { - return GetIniString(section, setting, defaultValue, bufSize, translationIni); -} - -size_t Translate(const char* section, const char* setting, const char* defaultValue, char* buffer, size_t bufSize) { - return iniGetString(section, setting, defaultValue, buffer, bufSize, translationIni); -} - -int SetConfigInt(const char* section, const char* setting, int value) { - char* buf = new char[33]; - _itoa_s(value, buf, 33, 10); - int result = WritePrivateProfileStringA(section, setting, buf, ini); - delete[] buf; - return result; -} - void InitReplacementHacks() { game::Inventory::init(); game::Items::init(); @@ -280,7 +213,7 @@ static void CompatModeCheck(HKEY root, const char* filepath, int extra) { inline void SfallInit() { // enabling debugging features - isDebug = (iniGetInt("Debugging", "Enable", 0, ::sfall::ddrawIni) != 0); + isDebug = (IniReader::GetIntDefaultConfig("Debugging", "Enable", 0) != 0); if (isDebug) { LoggingInit(); if (!ddraw.dll) dlog("Error: Cannot load the original ddraw.dll library.\n", DL_MAIN); @@ -291,7 +224,7 @@ inline void SfallInit() { CRC(filepath); - if (!isDebug || !iniGetInt("Debugging", "SkipCompatModeCheck", 0, ::sfall::ddrawIni)) { + if (!isDebug || !IniReader::GetIntDefaultConfig("Debugging", "SkipCompatModeCheck", 0)) { int is64bit; typedef int (__stdcall *chk64bitproc)(HANDLE, int*); HMODULE h = LoadLibrary("Kernel32.dll"); @@ -309,7 +242,7 @@ inline void SfallInit() { // ini file override bool cmdlineexists = false; char* cmdline = GetCommandLineA(); - if (iniGetInt("Main", "UseCommandLine", 0, ::sfall::ddrawIni)) { + if (IniReader::GetIntDefaultConfig("Main", "UseCommandLine", 0)) { while (cmdline[0] == ' ') cmdline++; bool InQuote = false; int count = -1; @@ -335,7 +268,7 @@ inline void SfallInit() { HANDLE h = CreateFileA(cmdline, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); if (h != INVALID_HANDLE_VALUE) { CloseHandle(h); - strcat_s(ini, cmdline); + IniReader::SetConfigFile(cmdline); } else { MessageBoxA(0, "You gave a command line argument to Fallout, but it couldn't be matched to a file.\n" \ "Using default ddraw.ini instead.", "Warning", MB_TASKMODAL | MB_ICONWARNING); @@ -343,12 +276,9 @@ inline void SfallInit() { } } else { defaultIni: - strcpy(&ini[2], &::sfall::ddrawIni[2]); + IniReader::SetDefaultConfigFile(); } - GetConfigString("Main", "TranslationsINI", ".\\Translations.ini", translationIni, 65); - modifiedIni = GetConfigInt("Main", "ModifiedIni", 0); - hrpIsEnabled = (*(DWORD*)0x4E4480 != 0x278805C7); // check if HRP is enabled if (hrpIsEnabled) { LoadHRPModule(); @@ -361,6 +291,8 @@ defaultIni: } std::srand(GetTickCount()); + IniReader::init(); + InitReplacementHacks(); InitModules(); } diff --git a/sfall/main.h b/sfall/main.h index f309a85a..2b0e9e0b 100644 --- a/sfall/main.h +++ b/sfall/main.h @@ -35,6 +35,7 @@ #include "SafeWrite.h" #include "Logging.h" +#include "IniReader.h" struct ddrawDll { HMODULE dll; @@ -74,51 +75,14 @@ namespace sfall #endif // Trap for Debugger -#define BREAKPOINT __asm int 3 +#define BREAKPOINT __debugbreak // Macros for quick replacement of assembler opcodes pushad/popad #define pushadc __asm push eax __asm push edx __asm push ecx #define popadc __asm pop ecx __asm pop edx __asm pop eax -// Gets the integer value from given INI file. -int iniGetInt(const char* section, const char* setting, int defaultValue, const char* iniFile); - -// Gets the string value from given INI file. -size_t iniGetString(const char* section, const char* setting, const char* defaultValue, char* buf, size_t bufSize, const char* iniFile); - -// Gets the string value from given INI file. -std::string GetIniString(const char* section, const char* setting, const char* defaultValue, size_t bufSize, const char* iniFile); - -// Parses the comma-separated list setting from given INI file. -std::vector GetIniList(const char* section, const char* setting, const char* defaultValue, size_t bufSize, char delimiter, const char* iniFile); - -// Gets the integer value from Sfall configuration INI file. -unsigned int GetConfigInt(const char* section, const char* setting, int defaultValue); - -// Gets the string value from Sfall configuration INI file with trim function. -std::string GetConfigString(const char* section, const char* setting, const char* defaultValue, size_t bufSize = 128); - -// Loads the string value from Sfall configuration INI file into the provided buffer. -size_t GetConfigString(const char* section, const char* setting, const char* defaultValue, char* buffer, size_t bufSize = 128); - -// Parses the comma-separated list from the settings from Sfall configuration INI file. -std::vector GetConfigList(const char* section, const char* setting, const char* defaultValue, size_t bufSize = 128); - -std::vector TranslateList(const char* section, const char* setting, const char* defaultValue, char delimiter, size_t bufSize = 256); - -// Translates given string using Sfall translation INI file. -std::string Translate(const char* section, const char* setting, const char* defaultValue, size_t bufSize = 128); - -// Translates given string using Sfall translation INI file and puts the result into given buffer. -size_t Translate(const char* section, const char* setting, const char* defaultValue, char* buffer, size_t bufSize = 128); - -int SetConfigInt(const char* section, const char* setting, int value); - DWORD HRPAddress(DWORD addr); -extern const char ddrawIni[]; -extern DWORD modifiedIni; - extern bool hrpIsEnabled; extern bool hrpVersionValid;