diff --git a/src/openrct2/config.c b/src/openrct2/config.c index 4452ca17e8..2186bc470a 100644 --- a/src/openrct2/config.c +++ b/src/openrct2/config.c @@ -339,7 +339,7 @@ notification_configuration gConfigNotifications; font_configuration gConfigFonts; bool config_open(const utf8string path); -static bool config_save(const utf8string path); +bool config_save(const utf8string path); static void config_read_properties(config_section_definition **currentSection, const_utf8string line); static void config_save_property_value(SDL_RWops *file, uint8 type, value_union *value); static bool config_read_enum(void *dest, sint32 destSize, const utf8 *key, sint32 keySize, config_enum_definition *enumDefinitions); @@ -557,7 +557,7 @@ static bool config_open_2(const utf8string path) return true; } -bool config_save(const utf8string path) +static bool config_save_2(const utf8string path) { SDL_RWops *file; sint32 i, j; diff --git a/src/openrct2/config/Config.cpp b/src/openrct2/config/Config.cpp index cf7d1b6ecd..263eef6dfc 100644 --- a/src/openrct2/config/Config.cpp +++ b/src/openrct2/config/Config.cpp @@ -20,6 +20,7 @@ #include "../network/network.h" #include "Config.h" #include "IniReader.h" +#include "IniWriter.h" extern "C" { @@ -190,6 +191,10 @@ namespace Config } } + static void WriteGeneral(IIniWriter * writer) + { + } + static void ReadInterface(IIniReader * reader) { if (reader->ReadSection("interface")) @@ -207,6 +212,10 @@ namespace Config } } + static void WriteInterface(IIniWriter * writer) + { + } + static void ReadSound(IIniReader * reader) { if (reader->ReadSection("sound")) @@ -223,6 +232,10 @@ namespace Config } } + static void WriteSound(IIniWriter * writer) + { + } + static void ReadNetwork(IIniReader * reader) { if (reader->ReadSection("network")) @@ -246,6 +259,10 @@ namespace Config } } + static void WriteNetwork(IIniWriter * writer) + { + } + static void ReadNotifications(IIniReader * reader) { if (reader->ReadSection("notifications")) @@ -271,6 +288,10 @@ namespace Config } } + static void WriteNotifications(IIniWriter * writer) + { + } + static void ReadTwitch(IIniReader * reader) { if (reader->ReadSection("sound")) @@ -285,6 +306,10 @@ namespace Config } } + static void WriteTwitch(IIniWriter * writer) + { + } + static void ReadFont(IIniReader * reader) { if (reader->ReadSection("font")) @@ -305,6 +330,10 @@ namespace Config } } + static void WriteFont(IIniWriter * writer) + { + } + static bool ReadFile(const std::string &path) { try @@ -324,6 +353,26 @@ namespace Config return false; } } + + static bool WriteFile(const std::string &path) + { + try + { + auto writer = std::unique_ptr(CreateIniWriter(path)); + WriteGeneral(writer.get()); + WriteInterface(writer.get()); + WriteSound(writer.get()); + WriteNetwork(writer.get()); + WriteNotifications(writer.get()); + WriteTwitch(writer.get()); + WriteFont(writer.get()); + return true; + } + catch (const Exception &) + { + return false; + } + } } extern "C" @@ -332,4 +381,9 @@ extern "C" { return Config::ReadFile(path); } + + bool config_save(const utf8 * path) + { + return Config::WriteFile(path); + } } diff --git a/src/openrct2/config/Config.h b/src/openrct2/config/Config.h index 13057fa82f..5adf8efd3c 100644 --- a/src/openrct2/config/Config.h +++ b/src/openrct2/config/Config.h @@ -21,4 +21,5 @@ extern "C" { bool config_open(const utf8 * path); + bool config_save(const utf8 * path); } diff --git a/src/openrct2/config/IniWriter.cpp b/src/openrct2/config/IniWriter.cpp new file mode 100644 index 0000000000..bfee401403 --- /dev/null +++ b/src/openrct2/config/IniWriter.cpp @@ -0,0 +1,96 @@ +#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers +/***************************************************************************** + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * OpenRCT2 is the work of many authors, a full list can be found in contributors.md + * For more information, visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 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. + * + * A full copy of the GNU General Public License can be found in licence.txt + *****************************************************************************/ +#pragma endregion + +#include +#include "../core/FileStream.hpp" +#include "../platform/platform.h" +#include "IniWriter.h" + +class IniWriter final : public IIniWriter +{ +private: + FileStream _fs; + +public: + IniWriter(const std::string &path) + : _fs(path, FILE_MODE_WRITE) + { + } + + void WriteSection(const std::string &name) override + { + WriteLine("[" + name + "]"); + } + + void WriteBoolean(const std::string &name, bool value) + { + WriteProperty(name, value ? "true" : "false"); + } + + void WriteSint32(const std::string &name, sint32 value) override + { + WriteProperty(name, std::to_string(value)); + } + + void WriteFloat(const std::string &name, float value) override + { + WriteProperty(name, std::to_string(value)); + } + + void WriteString(const std::string &name, const std::string &value) override + { + std::ostringstream buffer; + buffer << '"'; + for (char c : value) + { + if (c == '\\' || c == '"') + { + buffer << '\\'; + } + buffer << c; + } + buffer << '"'; + + WriteProperty(name, buffer.str()); + } + + void WriteEnum(const std::string &name, const std::string &key) override + { + WriteProperty(name, key); + } + +private: + void WriteProperty(const std::string &name, const std::string &value) + { + WriteLine(name + " = " + value); + } + + void WriteLine(const std::string &line) + { + _fs.Write(line.c_str(), line.size()); + _fs.Write(PLATFORM_NEWLINE, String::SizeOf(PLATFORM_NEWLINE)); + } +}; + +void IIniWriter::WriteString(const std::string &name, const utf8 * value) +{ + WriteString(name, String::ToStd(value)); +} + +IIniWriter * CreateIniWriter(const std::string &path) +{ + return new IniWriter(path); +} diff --git a/src/openrct2/config/IniWriter.h b/src/openrct2/config/IniWriter.h new file mode 100644 index 0000000000..5aa6e82534 --- /dev/null +++ b/src/openrct2/config/IniWriter.h @@ -0,0 +1,50 @@ +#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers +/***************************************************************************** + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * OpenRCT2 is the work of many authors, a full list can be found in contributors.md + * For more information, visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 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. + * + * A full copy of the GNU General Public License can be found in licence.txt + *****************************************************************************/ +#pragma endregion + +#include +#include "../common.h" +#include "ConfigEnum.h" + +interface IIniWriter +{ + virtual ~IIniWriter() = default; + + virtual void WriteSection(const std::string &name) abstract; + + virtual void WriteBoolean(const std::string &name, bool value) abstract; + virtual void WriteSint32(const std::string &name, sint32 value) abstract; + virtual void WriteFloat(const std::string &name, float value) abstract; + virtual void WriteString(const std::string &name, const std::string &value) abstract; + virtual void WriteEnum(const std::string &name, const std::string &key) abstract; + + template + void WriteEnum(const std::string &name, T value, const IConfigEnum &configEnum) + { + std::string key = configEnum.GetName(value); + if (key.empty) + { + WriteSint32(value); + } + else + { + WriteEnum(name, key); + } + } + + void WriteString(const std::string &name, const utf8 * value); +}; + +IIniWriter * CreateIniWriter(const std::string &path); diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 2cb0e509af..689c958415 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -74,6 +74,7 @@ + @@ -424,6 +425,7 @@ +