Some format/textual edits to code

* line indents.
* remove trailing spaces.
* use nullptr instead of NULL for pointers.

Edited the error messages of get_ini_config.
This commit is contained in:
NovaRain
2023-06-19 12:14:31 +08:00
parent 14f6de0329
commit ae7921fe26
10 changed files with 77 additions and 73 deletions
+32 -32
View File
@@ -1,26 +1,26 @@
/* /*
* sfall * sfall
* Copyright (C) 2008-2023 The sfall team * Copyright (C) 2008-2023 The sfall team
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "Config.h" #include "Config.h"
#include "Main.h" #include "main.h"
#include "FalloutEngine\Fallout2.h"
#include "Utils.h" #include "Utils.h"
#include "FalloutEngine/Fallout2.h"
using namespace fo; using namespace fo;
@@ -34,7 +34,7 @@ const Config::Data& Config::data()
bool Config::read(const char* filePath, bool isDb) bool Config::read(const char* filePath, bool isDb)
{ {
if (filePath == NULL) { if (filePath == nullptr) {
return false; return false;
} }
@@ -43,21 +43,21 @@ bool Config::read(const char* filePath, bool isDb)
if (isDb) { if (isDb) {
DbFile* stream = func::db_fopen(filePath, "rb"); DbFile* stream = func::db_fopen(filePath, "rb");
if (stream == NULL) return false; if (stream == nullptr) return false;
while (func::db_fgets(string, sizeof(string), stream) != NULL) { while (func::db_fgets(string, sizeof(string), stream) != nullptr) {
parseLine(string); parseLine(string);
} }
func::db_fclose(stream); func::db_fclose(stream);
} else { } else {
FILE* stream = fopen(filePath, "rt"); FILE* stream = fopen(filePath, "rt");
// CE: Return `false` if file does not exists on the file system. // CE: Return false if file does not exist on the file system.
if (stream == NULL) { if (stream == nullptr) {
return false; return false;
} }
while (fgets(string, sizeof(string), stream) != NULL) { while (fgets(string, sizeof(string), stream) != nullptr) {
parseLine(string); parseLine(string);
} }
fclose(stream); fclose(stream);
@@ -71,13 +71,13 @@ bool Config::read(const char* filePath, bool isDb)
// Both key and value are trimmed. // Both key and value are trimmed.
bool Config::parseKeyValue(char* string, std::string& key, std::string& value) bool Config::parseKeyValue(char* string, std::string& key, std::string& value)
{ {
if (string == NULL) { if (string == nullptr) {
return false; return false;
} }
// Find equals character. // Find equals character.
char* pch = strchr(string, '='); char* pch = strchr(string, '=');
if (pch == NULL) { if (pch == nullptr) {
return false; return false;
} }
@@ -99,7 +99,7 @@ bool Config::parseLine(char* string)
// Find comment marker and truncate the string. // Find comment marker and truncate the string.
pch = strchr(string, ';'); pch = strchr(string, ';');
if (pch != NULL) { if (pch != nullptr) {
*pch = '\0'; *pch = '\0';
} }
@@ -114,7 +114,7 @@ bool Config::parseLine(char* string)
// Find closing bracket. // Find closing bracket.
pch = strchr(sectionKey, ']'); pch = strchr(sectionKey, ']');
if (pch != NULL) { if (pch != nullptr) {
*pch = '\0'; *pch = '\0';
trim(sectionKey); trim(sectionKey);
_lastSection = sectionKey; _lastSection = sectionKey;
@@ -155,20 +155,20 @@ bool Config::getString(const char* sectionKey, const char* key, const std::strin
bool Config::getInt(const char* sectionKey, const char* key, int& outValue, unsigned char base /* = 0 */) bool Config::getInt(const char* sectionKey, const char* key, int& outValue, unsigned char base /* = 0 */)
{ {
const std::string* value; const std::string* value;
if (!getString(sectionKey, key, value)) return false; if (!getString(sectionKey, key, value)) return false;
outValue = StrToLong(value->c_str(), base); outValue = StrToLong(value->c_str(), base);
return true; return true;
} }
bool Config::getDouble(const char* sectionKey, const char* key, double& outValue) bool Config::getDouble(const char* sectionKey, const char* key, double& outValue)
{ {
const std::string* value; const std::string* value;
if (!getString(sectionKey, key, value)) return false; if (!getString(sectionKey, key, value)) return false;
outValue = strtod(value->c_str(), NULL); outValue = strtod(value->c_str(), nullptr);
return true; return true;
} }
} }
+2 -2
View File
@@ -36,7 +36,7 @@ public:
typedef std::map<std::string, Section, ci_less> Data; typedef std::map<std::string, Section, ci_less> Data;
bool read(const char* filePath, bool isDb); bool read(const char* filePath, bool isDb);
bool getString(const char* sectionKey, const char* key, const std::string*& outValue); bool getString(const char* sectionKey, const char* key, const std::string*& outValue);
bool getInt(const char* sectionKey, const char* key, int& outValue, unsigned char base = 0); bool getInt(const char* sectionKey, const char* key, int& outValue, unsigned char base = 0);
bool getDouble(const char* sectionKey, const char* key, double& outValue); bool getDouble(const char* sectionKey, const char* key, double& outValue);
@@ -58,4 +58,4 @@ private:
bool parseLine(char* string); bool parseLine(char* string);
}; };
} }
+4 -4
View File
@@ -18,11 +18,11 @@
#include "IniReader.h" #include "IniReader.h"
#include "main.h"
#include "FalloutEngine\Fallout2.h"
#include "Modules\LoadGameHook.h"
#include "Config.h" #include "Config.h"
#include "Main.h"
#include "Utils.h" #include "Utils.h"
#include "FalloutEngine/Fallout2.h"
#include "Modules/LoadGameHook.h"
namespace sfall namespace sfall
{ {
@@ -79,7 +79,7 @@ static size_t getString(const char* section, const char* setting, const char* de
strncpy_s(buf, bufSize, defaultValue, bufSize - 1); strncpy_s(buf, bufSize, defaultValue, bufSize - 1);
return strlen(buf); return strlen(buf);
} }
strncpy_s(buf, bufSize, value->c_str(), bufSize - 1); strncpy_s(buf, bufSize, value->c_str(), bufSize - 1);
return value->size(); return value->size();
} }
-1
View File
@@ -140,4 +140,3 @@ void LoggingInit() {
} }
} }
+11 -11
View File
@@ -404,33 +404,33 @@ static void __fastcall DuplicateLogToConsole(const char* a, unsigned long displa
static void __declspec(naked) op_display_debug_msg_hack() { static void __declspec(naked) op_display_debug_msg_hack() {
__asm { __asm {
mov eax, 0x505224; // "\n" mov eax, 0x505224; // "\n"
call ds:[FO_VAR_debug_func]; call ds:[FO_VAR_debug_func];
mov eax, esi; // actual message mov eax, esi; // actual message
call ds:[FO_VAR_debug_func]; call ds:[FO_VAR_debug_func];
pushadc; pushadc;
mov ecx, esi; mov ecx, esi;
mov edx, [esp + 12]; mov edx, [esp + 12];
call DuplicateLogToConsole; // duplicate messages to console window call DuplicateLogToConsole; // duplicate messages to console window
popadc; popadc;
add esp, 4; // eat displayMsg flag add esp, 4; // eat displayMsg flag
pop eax; pop eax;
add eax, 17; // skip to the end of functions add eax, 17; // skip to the end of functions
jmp eax; jmp eax;
} }
} }
static void __declspec(naked) op_display_msg_hack() { static void __declspec(naked) op_display_msg_hack() {
__asm { __asm {
push 1; // displayMsg = true push 1; // displayMsg = true
jmp op_display_debug_msg_hack; jmp op_display_debug_msg_hack;
} }
} }
static void __declspec(naked) op_debug_msg_hack() { static void __declspec(naked) op_debug_msg_hack() {
__asm { __asm {
push 0; // displayMsg = false push 0; // displayMsg = false
jmp op_display_debug_msg_hack; jmp op_display_debug_msg_hack;
} }
} }
@@ -485,7 +485,7 @@ static void DebugModePatch() {
MakeCall(0x4C703F, debug_log_hack); MakeCall(0x4C703F, debug_log_hack);
BlockCall(0x4C7044); // just nop code BlockCall(0x4C7044); // just nop code
} }
// replace calling debug_printf_ with _debug_func, to avoid buffer overflow with messages longer than 260-bytes. // replace calling debug_printf_ with _debug_func, to avoid buffer overflow with messages longer than 260 bytes
MakeCall(0x45540F, op_display_msg_hack); MakeCall(0x45540F, op_display_msg_hack);
MakeCall(0x45CB4E, op_debug_msg_hack); MakeCall(0x45CB4E, op_debug_msg_hack);
+1 -1
View File
@@ -29,7 +29,7 @@ namespace sfall
namespace script namespace script
{ {
#define ARRAY_MAX_STRING (1024) // maximum length of string to be stored as array key or value (including null-terminator) #define ARRAY_MAX_STRING (1024) // maximum length of string to be stored as array key or value (including null terminator)
#define ARRAY_MAX_SIZE (100000) // maximum number of array elements, #define ARRAY_MAX_SIZE (100000) // maximum number of array elements,
// so total maximum memory/disk footprint of one array is: 16 + (ARRAY_MAX_STRING + 8) * ARRAY_MAX_SIZE // so total maximum memory/disk footprint of one array is: 16 + (ARRAY_MAX_STRING + 8) * ARRAY_MAX_SIZE
+11 -11
View File
@@ -23,6 +23,7 @@
#include "..\..\ScriptExtender.h" #include "..\..\ScriptExtender.h"
#include "..\Arrays.h" #include "..\Arrays.h"
#include <memory>
#include <unordered_map> #include <unordered_map>
namespace sfall namespace sfall
@@ -145,8 +146,8 @@ static std::string GetSanitizedDBPath(const char* pathArg) {
std::replace(path.begin(), path.end(), '/', '\\'); // Normalize directory separators. std::replace(path.begin(), path.end(), '/', '\\'); // Normalize directory separators.
path.erase(0, path.find_first_not_of(whiteSpaces)); // trim left path.erase(0, path.find_first_not_of(whiteSpaces)); // trim left
if (path[0] == '\\' || if (path[0] == '\\' ||
path.find(':') != std::string::npos || path.find(':') != std::string::npos ||
path.find("..") != std::string::npos) return ""; // don't allow absolute paths or going outside of root path.find("..") != std::string::npos) return ""; // don't allow absolute paths or going outside of root
if (path.find(".\\") == 0) path.erase(0, 2); // remove leading ".\" if (path.find(".\\") == 0) path.erase(0, 2); // remove leading ".\"
path.erase(path.find_last_not_of(whiteSpaces) + 1); // trim right path.erase(path.find_last_not_of(whiteSpaces) + 1); // trim right
@@ -181,7 +182,7 @@ void mf_get_ini_sections(OpcodeContext& ctx) {
} }
const auto& data = config->data(); const auto& data = config->data();
size_t numSections = config->data().size(); size_t numSections = config->data().size();
int arrayId = CreateTempArray(numSections, 0); DWORD arrayId = CreateTempArray(numSections, 0);
size_t i = 0; size_t i = 0;
for (auto sectIt = data.cbegin(); sectIt != data.cend(); ++sectIt) { for (auto sectIt = data.cbegin(); sectIt != data.cend(); ++sectIt) {
arrays[arrayId].val[i].set(sectIt->first.c_str(), sectIt->first.size()); arrays[arrayId].val[i].set(sectIt->first.c_str(), sectIt->first.size());
@@ -198,7 +199,7 @@ static void CopyConfigSectionToArray(DWORD arrayId, const Config::Section& secti
void mf_get_ini_section(OpcodeContext& ctx) { void mf_get_ini_section(OpcodeContext& ctx) {
auto sectionName = ctx.arg(1).strValue(); auto sectionName = ctx.arg(1).strValue();
int arrayId = CreateTempArray(-1, 0); // associative DWORD arrayId = CreateTempArray(-1, 0); // associative
ctx.setReturn(arrayId); ctx.setReturn(arrayId);
Config* config = IniReader::instance().getIniConfig(GetIniFilePathFromArg(ctx.arg(0)).c_str()); Config* config = IniReader::instance().getIniConfig(GetIniFilePathFromArg(ctx.arg(0)).c_str());
@@ -218,7 +219,7 @@ void mf_get_ini_config(OpcodeContext& ctx) {
: GetIniFilePathFromArg(ctx.arg(0))); : GetIniFilePathFromArg(ctx.arg(0)));
if (filePath.size() == 0) { if (filePath.size() == 0) {
ctx.printOpcodeError("Invalid INI path: %s", ctx.arg(0).strValue()); ctx.printOpcodeError("%s() - invalid config file path: %s", ctx.getMetaruleName(), ctx.arg(0).strValue());
ctx.setReturn(0); ctx.setReturn(0);
return; return;
} }
@@ -242,17 +243,16 @@ void mf_get_ini_config(OpcodeContext& ctx) {
if (isDb) { if (isDb) {
configUniq = std::make_unique<Config>(); configUniq = std::make_unique<Config>();
if (!configUniq->read(filePath.c_str(), isDb)) { if (!configUniq->read(filePath.c_str(), isDb)) {
ctx.printOpcodeError("Could not read config file from DAT: %s", filePath.c_str()); ctx.printOpcodeError("%s() - cannot read config file from DAT: %s", ctx.getMetaruleName(), filePath.c_str());
ctx.setReturn(0); ctx.setReturn(0);
return; return;
} }
config = configUniq.get(); config = configUniq.get();
} } else {
else {
// Request config from IniReader (to take advantage of it's cache). // Request config from IniReader (to take advantage of it's cache).
config = IniReader::instance().getIniConfig(filePath.c_str()); config = IniReader::instance().getIniConfig(filePath.c_str());
if (config == nullptr) { if (config == nullptr) {
ctx.printOpcodeError("Could not read config file: %s", filePath.c_str()); ctx.printOpcodeError("%s() - cannot read config file: %s", ctx.getMetaruleName(), filePath.c_str());
ctx.setReturn(0); ctx.setReturn(0);
return; return;
} }
@@ -266,9 +266,9 @@ void mf_get_ini_config(OpcodeContext& ctx) {
SetArray(arrayId, sectIt->first.c_str(), subArrayId, false); SetArray(arrayId, sectIt->first.c_str(), subArrayId, false);
} }
// Save new array ID to cache and return it. // Save new array ID to cache and return it.
cache.emplace(filePath, arrayId); cache.emplace(std::move(filePath), arrayId);
ctx.setReturn(arrayId); ctx.setReturn(arrayId);
} }
} }
} }
+1 -1
View File
@@ -83,7 +83,7 @@ long StrToLong(const char* str, int base /* = 0 */) {
} }
char* end; char* end;
errno = 0; errno = 0;
return strtol(str, &end, base); // see https://stackoverflow.com/a/6154614 return strtol(str, &end, base); // see https://stackoverflow.com/a/6154614
} }
+7 -6
View File
@@ -4,7 +4,7 @@
#include <sstream> #include <sstream>
#include <vector> #include <vector>
namespace sfall namespace sfall
{ {
// splits a string by given delimiter // splits a string by given delimiter
@@ -51,14 +51,15 @@ struct ci_less
struct nocase_compare struct nocase_compare
{ {
bool operator() (const unsigned char& c1, const unsigned char& c2) const { bool operator() (const unsigned char& c1, const unsigned char& c2) const {
return tolower(c1) < tolower(c2); return tolower(c1) < tolower(c2);
} }
}; };
bool operator() (const std::string &s1, const std::string &s2) const { bool operator() (const std::string &s1, const std::string &s2) const {
return std::lexicographical_compare return std::lexicographical_compare(
(s1.begin (), s1.end (), // source range s1.begin (), s1.end (), // source range
s2.begin (), s2.end (), // dest range s2.begin (), s2.end (), // dest range
nocase_compare ()); // comparison nocase_compare () // comparison
);
} }
}; };
+8 -4
View File
@@ -452,9 +452,11 @@
<ClInclude Include="HLSL\L8PixelShader.h"> <ClInclude Include="HLSL\L8PixelShader.h">
<Filter>HLSL</Filter> <Filter>HLSL</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Config.h" />
<ClInclude Include="Modules\Scripting\Handlers\IniFiles.h" />
<ClInclude Include="ConsoleWindow.h" /> <ClInclude Include="ConsoleWindow.h" />
<ClInclude Include="Config.h" />
<ClInclude Include="Modules\Scripting\Handlers\IniFiles.h">
<Filter>Modules\Scripting\Handlers</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
@@ -827,9 +829,11 @@
<ClCompile Include="Modules\SubModules\DirectDraw.cpp"> <ClCompile Include="Modules\SubModules\DirectDraw.cpp">
<Filter>Modules\SubModules</Filter> <Filter>Modules\SubModules</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Config.cpp" />
<ClCompile Include="Modules\Scripting\Handlers\IniFiles.cpp" />
<ClCompile Include="ConsoleWindow.cpp" /> <ClCompile Include="ConsoleWindow.cpp" />
<ClCompile Include="Config.cpp" />
<ClCompile Include="Modules\Scripting\Handlers\IniFiles.cpp">
<Filter>Modules\Scripting\Handlers</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="version.rc" /> <ResourceCompile Include="version.rc" />