mirror of
https://github.com/sfall-team/sfall.git
synced 2026-07-27 16:52:34 -07:00
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:
+32
-32
@@ -1,26 +1,26 @@
|
||||
/*
|
||||
* sfall
|
||||
* Copyright (C) 2008-2023 The sfall team
|
||||
* sfall
|
||||
* Copyright (C) 2008-2023 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 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.
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "Config.h"
|
||||
|
||||
#include "Main.h"
|
||||
#include "main.h"
|
||||
#include "FalloutEngine\Fallout2.h"
|
||||
#include "Utils.h"
|
||||
#include "FalloutEngine/Fallout2.h"
|
||||
|
||||
using namespace fo;
|
||||
|
||||
@@ -34,7 +34,7 @@ const Config::Data& Config::data()
|
||||
|
||||
bool Config::read(const char* filePath, bool isDb)
|
||||
{
|
||||
if (filePath == NULL) {
|
||||
if (filePath == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -43,21 +43,21 @@ bool Config::read(const char* filePath, bool isDb)
|
||||
if (isDb) {
|
||||
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);
|
||||
}
|
||||
func::db_fclose(stream);
|
||||
} else {
|
||||
FILE* stream = fopen(filePath, "rt");
|
||||
|
||||
// CE: Return `false` if file does not exists on the file system.
|
||||
if (stream == NULL) {
|
||||
// CE: Return false if file does not exist on the file system.
|
||||
if (stream == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
while (fgets(string, sizeof(string), stream) != NULL) {
|
||||
while (fgets(string, sizeof(string), stream) != nullptr) {
|
||||
parseLine(string);
|
||||
}
|
||||
fclose(stream);
|
||||
@@ -71,13 +71,13 @@ bool Config::read(const char* filePath, bool isDb)
|
||||
// Both key and value are trimmed.
|
||||
bool Config::parseKeyValue(char* string, std::string& key, std::string& value)
|
||||
{
|
||||
if (string == NULL) {
|
||||
if (string == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Find equals character.
|
||||
char* pch = strchr(string, '=');
|
||||
if (pch == NULL) {
|
||||
if (pch == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ bool Config::parseLine(char* string)
|
||||
|
||||
// Find comment marker and truncate the string.
|
||||
pch = strchr(string, ';');
|
||||
if (pch != NULL) {
|
||||
if (pch != nullptr) {
|
||||
*pch = '\0';
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ bool Config::parseLine(char* string)
|
||||
|
||||
// Find closing bracket.
|
||||
pch = strchr(sectionKey, ']');
|
||||
if (pch != NULL) {
|
||||
if (pch != nullptr) {
|
||||
*pch = '\0';
|
||||
trim(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 */)
|
||||
{
|
||||
const std::string* value;
|
||||
if (!getString(sectionKey, key, value)) return false;
|
||||
const std::string* value;
|
||||
if (!getString(sectionKey, key, value)) return false;
|
||||
|
||||
outValue = StrToLong(value->c_str(), base);
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Config::getDouble(const char* sectionKey, const char* key, double& outValue)
|
||||
{
|
||||
const std::string* value;
|
||||
if (!getString(sectionKey, key, value)) return false;
|
||||
const std::string* value;
|
||||
if (!getString(sectionKey, key, value)) return false;
|
||||
|
||||
outValue = strtod(value->c_str(), NULL);
|
||||
return true;
|
||||
outValue = strtod(value->c_str(), nullptr);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -36,7 +36,7 @@ public:
|
||||
typedef std::map<std::string, Section, ci_less> Data;
|
||||
|
||||
bool read(const char* filePath, bool isDb);
|
||||
|
||||
|
||||
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 getDouble(const char* sectionKey, const char* key, double& outValue);
|
||||
@@ -58,4 +58,4 @@ private:
|
||||
bool parseLine(char* string);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -18,11 +18,11 @@
|
||||
|
||||
#include "IniReader.h"
|
||||
|
||||
#include "main.h"
|
||||
#include "FalloutEngine\Fallout2.h"
|
||||
#include "Modules\LoadGameHook.h"
|
||||
#include "Config.h"
|
||||
#include "Main.h"
|
||||
#include "Utils.h"
|
||||
#include "FalloutEngine/Fallout2.h"
|
||||
#include "Modules/LoadGameHook.h"
|
||||
|
||||
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);
|
||||
return strlen(buf);
|
||||
}
|
||||
|
||||
|
||||
strncpy_s(buf, bufSize, value->c_str(), bufSize - 1);
|
||||
return value->size();
|
||||
}
|
||||
|
||||
@@ -140,4 +140,3 @@ void LoggingInit() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -404,33 +404,33 @@ static void __fastcall DuplicateLogToConsole(const char* a, unsigned long displa
|
||||
|
||||
static void __declspec(naked) op_display_debug_msg_hack() {
|
||||
__asm {
|
||||
mov eax, 0x505224; // "\n"
|
||||
mov eax, 0x505224; // "\n"
|
||||
call ds:[FO_VAR_debug_func];
|
||||
mov eax, esi; // actual message
|
||||
mov eax, esi; // actual message
|
||||
call ds:[FO_VAR_debug_func];
|
||||
pushadc;
|
||||
mov ecx, esi;
|
||||
mov edx, [esp + 12];
|
||||
mov ecx, esi;
|
||||
mov edx, [esp + 12];
|
||||
call DuplicateLogToConsole; // duplicate messages to console window
|
||||
popadc;
|
||||
add esp, 4; // eat displayMsg flag
|
||||
pop eax;
|
||||
add eax, 17; // skip to the end of functions
|
||||
jmp eax;
|
||||
add esp, 4; // eat displayMsg flag
|
||||
pop eax;
|
||||
add eax, 17; // skip to the end of functions
|
||||
jmp eax;
|
||||
}
|
||||
}
|
||||
|
||||
static void __declspec(naked) op_display_msg_hack() {
|
||||
__asm {
|
||||
push 1; // displayMsg = true
|
||||
jmp op_display_debug_msg_hack;
|
||||
jmp op_display_debug_msg_hack;
|
||||
}
|
||||
}
|
||||
|
||||
static void __declspec(naked) op_debug_msg_hack() {
|
||||
__asm {
|
||||
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);
|
||||
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(0x45CB4E, op_debug_msg_hack);
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace sfall
|
||||
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,
|
||||
// so total maximum memory/disk footprint of one array is: 16 + (ARRAY_MAX_STRING + 8) * ARRAY_MAX_SIZE
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "..\..\ScriptExtender.h"
|
||||
#include "..\Arrays.h"
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace sfall
|
||||
@@ -145,8 +146,8 @@ static std::string GetSanitizedDBPath(const char* pathArg) {
|
||||
std::replace(path.begin(), path.end(), '/', '\\'); // Normalize directory separators.
|
||||
path.erase(0, path.find_first_not_of(whiteSpaces)); // trim left
|
||||
if (path[0] == '\\' ||
|
||||
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 ||
|
||||
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 ".\"
|
||||
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();
|
||||
size_t numSections = config->data().size();
|
||||
int arrayId = CreateTempArray(numSections, 0);
|
||||
DWORD arrayId = CreateTempArray(numSections, 0);
|
||||
size_t i = 0;
|
||||
for (auto sectIt = data.cbegin(); sectIt != data.cend(); ++sectIt) {
|
||||
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) {
|
||||
auto sectionName = ctx.arg(1).strValue();
|
||||
int arrayId = CreateTempArray(-1, 0); // associative
|
||||
DWORD arrayId = CreateTempArray(-1, 0); // associative
|
||||
ctx.setReturn(arrayId);
|
||||
|
||||
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)));
|
||||
|
||||
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);
|
||||
return;
|
||||
}
|
||||
@@ -242,17 +243,16 @@ void mf_get_ini_config(OpcodeContext& ctx) {
|
||||
if (isDb) {
|
||||
configUniq = std::make_unique<Config>();
|
||||
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);
|
||||
return;
|
||||
}
|
||||
config = configUniq.get();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Request config from IniReader (to take advantage of it's cache).
|
||||
config = IniReader::instance().getIniConfig(filePath.c_str());
|
||||
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);
|
||||
return;
|
||||
}
|
||||
@@ -266,9 +266,9 @@ void mf_get_ini_config(OpcodeContext& ctx) {
|
||||
SetArray(arrayId, sectIt->first.c_str(), subArrayId, false);
|
||||
}
|
||||
// Save new array ID to cache and return it.
|
||||
cache.emplace(filePath, arrayId);
|
||||
cache.emplace(std::move(filePath), arrayId);
|
||||
ctx.setReturn(arrayId);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -83,7 +83,7 @@ long StrToLong(const char* str, int base /* = 0 */) {
|
||||
}
|
||||
char* end;
|
||||
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
@@ -4,7 +4,7 @@
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
namespace sfall
|
||||
namespace sfall
|
||||
{
|
||||
|
||||
// splits a string by given delimiter
|
||||
@@ -51,14 +51,15 @@ struct ci_less
|
||||
struct nocase_compare
|
||||
{
|
||||
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 {
|
||||
return std::lexicographical_compare
|
||||
(s1.begin (), s1.end (), // source range
|
||||
s2.begin (), s2.end (), // dest range
|
||||
nocase_compare ()); // comparison
|
||||
return std::lexicographical_compare(
|
||||
s1.begin (), s1.end (), // source range
|
||||
s2.begin (), s2.end (), // dest range
|
||||
nocase_compare () // comparison
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -452,9 +452,11 @@
|
||||
<ClInclude Include="HLSL\L8PixelShader.h">
|
||||
<Filter>HLSL</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Config.h" />
|
||||
<ClInclude Include="Modules\Scripting\Handlers\IniFiles.h" />
|
||||
<ClInclude Include="ConsoleWindow.h" />
|
||||
<ClInclude Include="Config.h" />
|
||||
<ClInclude Include="Modules\Scripting\Handlers\IniFiles.h">
|
||||
<Filter>Modules\Scripting\Handlers</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp" />
|
||||
@@ -827,9 +829,11 @@
|
||||
<ClCompile Include="Modules\SubModules\DirectDraw.cpp">
|
||||
<Filter>Modules\SubModules</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Config.cpp" />
|
||||
<ClCompile Include="Modules\Scripting\Handlers\IniFiles.cpp" />
|
||||
<ClCompile Include="ConsoleWindow.cpp" />
|
||||
<ClCompile Include="Config.cpp" />
|
||||
<ClCompile Include="Modules\Scripting\Handlers\IniFiles.cpp">
|
||||
<Filter>Modules\Scripting\Handlers</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="version.rc" />
|
||||
|
||||
Reference in New Issue
Block a user