diff --git a/3rdParty/Radon/LICENSE b/3rdParty/Radon/LICENSE deleted file mode 100644 index 71e60e010..000000000 --- a/3rdParty/Radon/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2017 Dmitro Szewczuk - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/3rdParty/Radon/Radon/include/File.hpp b/3rdParty/Radon/Radon/include/File.hpp deleted file mode 100644 index ce7012945..000000000 --- a/3rdParty/Radon/Radon/include/File.hpp +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright Dmitro bjornus Szewczuk 2017 under zlib license - -#pragma once - -#include -#include -#include - -namespace radon -{ - class Section; - - class File - { - public: - - File(const std::string & path, bool reading = true); - - Section* getSection(const std::string & name); - - void addSection(const std::string & name); - - void saveToFile(); - - private: - std::vector
sections; - std::string path; - }; -} \ No newline at end of file diff --git a/3rdParty/Radon/Radon/include/Key.hpp b/3rdParty/Radon/Radon/include/Key.hpp deleted file mode 100644 index 2e9843758..000000000 --- a/3rdParty/Radon/Radon/include/Key.hpp +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright Dmitro bjornus Szewczuk 2017 under zlib license - -#pragma once - -#include - -#include "Named.hpp" - -namespace radon -{ - class Key - : public Named - { - public: - - Key(); - - Key(const std::string & name, const std::string & value); - - Key(const std::string & name, float value); - - std::string getStringValue(); - - float getFloatValue(); - - void setValue(float value); - - void setValue(std::string & value); - - private: - std::string value; - - friend class File; - }; -} \ No newline at end of file diff --git a/3rdParty/Radon/Radon/include/Named.hpp b/3rdParty/Radon/Radon/include/Named.hpp deleted file mode 100644 index dbbdd1bb0..000000000 --- a/3rdParty/Radon/Radon/include/Named.hpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright Dmitro bjornus Szewczuk 2017 under zlib license - -#pragma once - -#include - -namespace radon -{ - class Named - { - public: - - Named(); - - Named(const std::string & name); - - void setName(const std::string & name); - - std::string getName(); - - protected: - std::string name; - }; -} \ No newline at end of file diff --git a/3rdParty/Radon/Radon/include/Radon.hpp b/3rdParty/Radon/Radon/include/Radon.hpp deleted file mode 100644 index de898f963..000000000 --- a/3rdParty/Radon/Radon/include/Radon.hpp +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright Dmitro bjornus Szewczuk 2017 under zlib license - -#pragma once - -#include "File.hpp" -#include "Key.hpp" -#include "Named.hpp" -#include "Radon.hpp" -#include "Section.hpp" \ No newline at end of file diff --git a/3rdParty/Radon/Radon/include/Section.hpp b/3rdParty/Radon/Radon/include/Section.hpp deleted file mode 100644 index 89412d947..000000000 --- a/3rdParty/Radon/Radon/include/Section.hpp +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright Dmitro bjornus Szewczuk 2017 under zlib license - -#pragma once - -#include -#include - -#include "Named.hpp" - -namespace radon -{ - class Key; - - class Section - : public Named - { - public: - - Section(); - - Section(const std::string & name); - - Key *getKey(const std::string & name); - - void addKey(Key variable); - - private: - std::vector keys; - - friend class File; - }; -} \ No newline at end of file diff --git a/3rdParty/Radon/Radon/source/File.cpp b/3rdParty/Radon/Radon/source/File.cpp deleted file mode 100644 index 488d2d084..000000000 --- a/3rdParty/Radon/Radon/source/File.cpp +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright Dmitro bjornus Szewczuk 2017 - -#include "../include/Radon.hpp" - -#include -#include -#include -#include -#include - -namespace radon -{ - File::File(const std::string & path, bool reading) - { - this->path = path; - if (reading) - { - std::ifstream stream(path.c_str()); - - if (stream.is_open()) - { - std::string buffer; - std::string nameOfCurrent = ""; - - while (std::getline(stream, buffer)) - { - if (buffer[0] == ';' || buffer[0] == '#') continue; - if (buffer[0] == '[') - { - nameOfCurrent = buffer.substr(buffer.find("[") + 1, buffer.find("]") - 1); - sections.push_back(Section(nameOfCurrent)); - } - else - { - int equalsPosition = buffer.find('='); - - std::string nameOfElement = buffer.substr(0, equalsPosition); - std::string valueOfElement = buffer.substr(equalsPosition + 1, buffer.size()); - - sections.back().addKey(Key(nameOfElement, valueOfElement)); - } - } - } - } - } - - - Section *File::getSection(const std::string & name) - { - for (int i = 0; i < (int)sections.size(); i++) - { - if (sections[i].getName() == name) - { - return §ions[i]; - } - } - - return NULL; - } - - - void File::addSection(const std::string & name) - { - sections.push_back(Section(name)); - } - - - void File::saveToFile() - { - std::ofstream file(path.data(), std::ios::out | std::ios::trunc); - - for (int i = 0; i < (int)sections.size(); i++) - { - file << "[" << sections[i].getName() << "] \n"; - for(int j = 0; j < (int)sections[i].keys.size(); j++) - { - file << sections[i].keys[j].getName() << "=" << sections[i].keys[j].getStringValue() << "\n"; - } - } - file.close(); - } -} \ No newline at end of file diff --git a/3rdParty/Radon/Radon/source/Key.cpp b/3rdParty/Radon/Radon/source/Key.cpp deleted file mode 100644 index 9e63b41ec..000000000 --- a/3rdParty/Radon/Radon/source/Key.cpp +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright Dmitro bjornus Szewczuk 2017 - -#include "../include/Radon.hpp" -#include - -std::string Float2String(float fVal) -{ - std::ostringstream ss; - ss << fVal; - std::string s(ss.str()); - return s; -} - -namespace radon -{ - Key::Key() - : Named() - { - } - - - Key::Key(const std::string & name, const std::string & value) - : Named(name), value(value) - { - } - - - Key::Key(const std::string & name, float value) - : Named(name), value(Float2String(value)) - { - } - - - std::string Key::getStringValue() - { - return value; - } - - - float Key::getFloatValue() - { - return (float)(atof(value.data())); - } - - - void Key::setValue(float value) - { - this->value = Float2String(value); - } - - - void Key::setValue(std::string & value) - { - this->value = value; - } -} \ No newline at end of file diff --git a/3rdParty/Radon/Radon/source/Named.cpp b/3rdParty/Radon/Radon/source/Named.cpp deleted file mode 100644 index d6b26f27f..000000000 --- a/3rdParty/Radon/Radon/source/Named.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright Dmitro bjornus Szewczuk 2017 - -#include "../include/Radon.hpp" - -namespace radon -{ - Named::Named() - { - setName("You need to set name!"); - } - - - Named::Named(const std::string & name) - { - setName(name); - } - - - void Named::setName(const std::string & name) - { - this->name = name; - } - - - std::string Named::getName() - { - return name; - } -} \ No newline at end of file diff --git a/3rdParty/Radon/Radon/source/Section.cpp b/3rdParty/Radon/Radon/source/Section.cpp deleted file mode 100644 index f9ad56af0..000000000 --- a/3rdParty/Radon/Radon/source/Section.cpp +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright Dmitro bjornus Szewczuk 2017 - -#include "../include/Radon.hpp" - -#include - -namespace radon -{ - Section::Section() - : Named() - { - } - - - Section::Section(const std::string & name) - : Named(name) - { - } - - - Key *Section::getKey(const std::string & name) - { - for (int i = 0; i < (int)keys.size(); i++) - { - if (keys[i].getName() == name) - return &keys[i]; - } - - return NULL; - } - - - void Section::addKey(Key key) - { - keys.push_back(key); - } -} \ No newline at end of file diff --git a/3rdParty/simpleini/CMakeLists.txt b/3rdParty/simpleini/CMakeLists.txt new file mode 100644 index 000000000..e857fa21d --- /dev/null +++ b/3rdParty/simpleini/CMakeLists.txt @@ -0,0 +1,11 @@ +include(FetchContent_MakeAvailableExcludeFromAll) + +include(FetchContent) +FetchContent_Declare(simpleini + GIT_REPOSITORY https://github.com/brofield/simpleini.git + GIT_TAG 7bca74f6535a37846162383e52071f380c99a43a +) +FetchContent_MakeAvailableExcludeFromAll(simpleini) + +add_library(simpleini INTERFACE) +target_include_directories(simpleini INTERFACE ${simpleini_SOURCE_DIR}) diff --git a/CMakeLists.txt b/CMakeLists.txt index 440c1fcf0..1e9ab2055 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -275,12 +275,7 @@ add_library(smacker STATIC 3rdParty/libsmacker/smacker.c) target_include_directories(smacker PUBLIC 3rdParty/libsmacker) -add_library(Radon STATIC - 3rdParty/Radon/Radon/source/File.cpp - 3rdParty/Radon/Radon/source/Key.cpp - 3rdParty/Radon/Radon/source/Named.cpp - 3rdParty/Radon/Radon/source/Section.cpp) -target_include_directories(Radon PUBLIC 3rdParty/Radon/Radon/include) +add_subdirectory(3rdParty/simpleini) add_library(StormLib STATIC 3rdParty/StormLib/src/FileStream.cpp @@ -597,7 +592,7 @@ target_link_libraries(libdevilutionx PUBLIC PKWare StormLib smacker - Radon) + simpleini) if(NOT NONET) if(NOT DISABLE_TCP) diff --git a/Source/storm/storm.cpp b/Source/storm/storm.cpp index 7964e49c5..16fa0c476 100644 --- a/Source/storm/storm.cpp +++ b/Source/storm/storm.cpp @@ -1,5 +1,3 @@ -#include "storm/storm.h" - #include #include #include @@ -7,8 +5,10 @@ #include #include -#include "Radon.hpp" +#define SI_SUPPORT_IOSTREAMS +#include +#include "storm/storm.h" #include "DiabloUI/diabloui.h" #include "options.h" #include "utils/file_util.h" @@ -37,6 +37,27 @@ std::string *SBasePath = nullptr; SdlMutex Mutex; +std::string getIniPath() +{ + auto path = paths::ConfigPath() + std::string("diablo.ini"); + return path; +} + +CSimpleIni &getIni() +{ + static CSimpleIni ini; + static bool isIniLoaded = false; + if (!isIniLoaded) { + auto path = getIniPath(); + auto stream = CreateFileStream(path.c_str(), std::fstream::in | std::fstream::binary); + ini.SetSpaces(false); + if (stream != nullptr) + ini.LoadData(*stream); + isIniLoaded = true; + } + return ini; +} + } // namespace bool SFileReadFileThreadSafe(HANDLE hFile, void *buffer, DWORD nNumberOfBytesToRead, DWORD *read, int *lpDistanceToMoveHigh) @@ -51,12 +72,6 @@ bool SFileCloseFileThreadSafe(HANDLE hFile) return SFileCloseFile(hFile); } -radon::File &getIni() -{ - static radon::File ini(paths::ConfigPath() + "diablo.ini"); - return ini; -} - // Converts ASCII characters to lowercase // Converts slash (0x2F) / backslash (0x5C) to system file-separator unsigned char AsciiToLowerTable_Path[256] = { @@ -159,68 +174,48 @@ bool getIniBool(const char *sectionName, const char *keyName, bool defaultValue) float getIniFloat(const char *sectionName, const char *keyName, float defaultValue) { - radon::Section *section = getIni().getSection(sectionName); - if (section == nullptr) - return defaultValue; - - radon::Key *key = section->getKey(keyName); - if (key == nullptr) + const auto value = getIni().GetValue(sectionName, keyName); + if (value == nullptr) return defaultValue; - return key->getFloatValue(); + return atof(value); } bool getIniValue(const char *sectionName, const char *keyName, char *string, int stringSize, const char *defaultString) { - strncpy(string, defaultString, stringSize); - - radon::Section *section = getIni().getSection(sectionName); - if (section == nullptr) - return false; + auto value = getIni().GetValue(sectionName, keyName); - radon::Key *key = section->getKey(keyName); - if (key == nullptr) + if (value == nullptr) { + strncpy(string, defaultString, stringSize); return false; + } - std::string value = key->getStringValue(); - - if (string != nullptr) - strncpy(string, value.c_str(), stringSize); + strncpy(string, value, stringSize); return true; } void setIniValue(const char *sectionName, const char *keyName, const char *value, int len) { - radon::File &ini = getIni(); - - radon::Section *section = ini.getSection(sectionName); - if (section == nullptr) { - ini.addSection(sectionName); - section = ini.getSection(sectionName); - } + auto &ini = getIni(); std::string stringValue(value, len ? len : strlen(value)); - radon::Key *key = section->getKey(keyName); - if (key == nullptr) { - section->addKey(radon::Key(keyName, stringValue)); - } else { - key->setValue(stringValue); - } + ini.SetValue(sectionName, keyName, stringValue.c_str()); } void SaveIni() { - getIni().saveToFile(); + auto iniPath = getIniPath(); + auto stream = CreateFileStream(iniPath.c_str(), std::fstream::out | std::fstream::trunc | std::fstream::binary); + getIni().Save(*stream, true); } int getIniInt(const char *keyname, const char *valuename, int defaultValue) { char string[10]; - if (!getIniValue(keyname, valuename, string, sizeof(string))) { + if (!getIniValue(keyname, valuename, string, sizeof(string))) return defaultValue; - } return strtol(string, nullptr, sizeof(string)); }