| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2019 - 2026 * |
| 4 | * * |
| 5 | * Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 * |
| 6 | * Copyright (C) 2010 by Andrey Afletdinov <[email protected]> * |
| 7 | * * |
| 8 | * This program is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * This program is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with this program; if not, write to the * |
| 20 | * Free Software Foundation, Inc., * |
| 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 22 | ***************************************************************************/ |
| 23 | |
| 24 | #include "tinyconfig.h" |
| 25 | |
| 26 | #include <algorithm> |
| 27 | #include <cassert> |
| 28 | #include <cctype> |
| 29 | #include <charconv> |
| 30 | #include <cstddef> |
| 31 | #include <cstdint> |
| 32 | #include <limits> |
| 33 | #include <ostream> |
| 34 | #include <regex> |
| 35 | #include <string_view> |
| 36 | #include <system_error> |
| 37 | #include <type_traits> |
| 38 | #include <utility> |
| 39 | #include <vector> |
| 40 | |
| 41 | #include "logging.h" |
| 42 | #include "serialize.h" |
| 43 | #include "tools.h" |
| 44 | |
| 45 | namespace |
| 46 | { |
| 47 | std::string ModifyKey( const std::string & str ) |
| 48 | { |
| 49 | std::string key = StringTrim( StringLower( str ) ); |
| 50 | |
| 51 | // Replace consecutive space-like characters with only one such character |
| 52 | key.erase( std::unique( key.begin(), key.end(), []( const unsigned char a, const unsigned char b ) { return std::isspace( a ) && std::isspace( b ); } ), |
| 53 | key.end() ); |
| 54 | |
| 55 | // Replace all space-like characters with spaces |
| 56 | std::replace_if( |
| 57 | key.begin(), key.end(), []( const unsigned char c ) { return std::isspace( c ); }, '\x20' ); |
| 58 | |
| 59 | return key; |
| 60 | } |
| 61 | |
| 62 | template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true> |
| 63 | bool convertToInt( const std::string_view str, T & intValue ) |
| 64 | { |
| 65 | const char * first = str.data(); |
| 66 | const char * last = str.data() + str.size(); |
| 67 | |
| 68 | const auto [ptr, ec] = std::from_chars( first, last, intValue ); |
| 69 | |
| 70 | return ( ptr == last && ec == std::errc() ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | TinyConfig::TinyConfig( const char sep, const char com ) |
| 75 | : separator( sep ) |
| 76 | , comment( com ) |
| 77 | {} |
| 78 | |
| 79 | bool TinyConfig::Load( const std::string & cfile ) |
| 80 | { |
| 81 | StreamFile sf; |
| 82 | if ( !sf.open( cfile, "rb" ) ) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | for ( const std::string & line : StringSplit( sf.getString(), '\n' ) ) { |
| 87 | std::string str = StringTrim( line ); |
| 88 | |
| 89 | if ( str.empty() || str[0] == comment ) { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | const size_t pos = str.find( separator ); |
| 94 | if ( pos == std::string::npos ) { |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | const std::string key = ModifyKey( StringTrim( str.substr( 0, pos ) ) ); |
| 99 | const std::string val = StringTrim( str.substr( pos + 1, str.length() - pos - 1 ) ); |
| 100 | |
| 101 | if ( const auto [dummy, inserted] = emplace( key, val ); !inserted ) { |
| 102 | ERROR_LOG( "Duplicate key '" << key << "' was found when reading the config file " << cfile ) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | int TinyConfig::IntParams( const std::string & key ) const |
| 110 | { |
| 111 | const auto it = find( ModifyKey( key ) ); |
| 112 | if ( it == end() ) { |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int result; |
| 117 | |
| 118 | if ( !convertToInt( it->second, result ) ) { |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | std::string TinyConfig::StrParams( const std::string & key ) const |
| 126 | { |
| 127 | const auto it = find( ModifyKey( key ) ); |
| 128 | return it != end() ? it->second : ""; |
| 129 | } |
| 130 | |
| 131 | fheroes2::Point TinyConfig::PointParams( const std::string & key, const fheroes2::Point & fallbackValue ) const |
| 132 | { |
| 133 | const auto it = find( ModifyKey( key ) ); |
| 134 | if ( it == end() ) { |
| 135 | return fallbackValue; |
| 136 | } |
| 137 | |
| 138 | static const std::regex pointRegex( "^\\[ *(-?[0-9]+) *, *(-?[0-9]+) *]$", std::regex_constants::extended ); |
| 139 | |
| 140 | std::smatch pointRegexMatch; |
| 141 | |
| 142 | if ( !std::regex_match( it->second, pointRegexMatch, pointRegex ) ) { |
| 143 | return fallbackValue; |
| 144 | } |
| 145 | |
| 146 | assert( pointRegexMatch.size() == 3 ); |
| 147 | |
| 148 | fheroes2::Point result; |
| 149 | |
| 150 | static_assert( std::is_integral<decltype( result.x )>::value && std::is_integral<decltype( result.y )>::value, |
| 151 | "The type of result fields is not integer, check the logic of this method" ); |
| 152 | |
| 153 | if ( !convertToInt( pointRegexMatch[1].str(), result.x ) ) { |
| 154 | return fallbackValue; |
| 155 | } |
| 156 | if ( !convertToInt( pointRegexMatch[2].str(), result.y ) ) { |
| 157 | return fallbackValue; |
| 158 | } |
| 159 | |
| 160 | return result; |
| 161 | } |
| 162 | |
| 163 | fheroes2::ResolutionInfo TinyConfig::ResolutionParams( const std::string & key, const fheroes2::ResolutionInfo & fallbackValue ) const |
| 164 | { |
| 165 | const auto it = find( ModifyKey( key ) ); |
| 166 | if ( it == end() ) { |
| 167 | return fallbackValue; |
| 168 | } |
| 169 | |
| 170 | { |
| 171 | // Current format: in-game width x in-game height : on-screen width x on-screen height |
| 172 | static const std::regex resolutionRegex( "^ *([0-9]+) *x *([0-9]+) *: *([0-9]+) *x *([0-9]+) *$", std::regex_constants::extended ); |
| 173 | |
| 174 | std::smatch resolutionRegexMatch; |
| 175 | |
| 176 | if ( std::regex_match( it->second, resolutionRegexMatch, resolutionRegex ) ) { |
| 177 | assert( resolutionRegexMatch.size() == 5 ); |
| 178 | |
| 179 | int32_t gameWidth{ 0 }; |
| 180 | int32_t gameHeight{ 0 }; |
| 181 | int32_t screenWidth{ 0 }; |
| 182 | int32_t screenHeight{ 0 }; |
| 183 | |
| 184 | if ( !convertToInt( resolutionRegexMatch[1].str(), gameWidth ) ) { |
| 185 | return fallbackValue; |
| 186 | } |
| 187 | if ( !convertToInt( resolutionRegexMatch[2].str(), gameHeight ) ) { |
| 188 | return fallbackValue; |
| 189 | } |
| 190 | if ( !convertToInt( resolutionRegexMatch[3].str(), screenWidth ) ) { |
| 191 | return fallbackValue; |
| 192 | } |
| 193 | if ( !convertToInt( resolutionRegexMatch[4].str(), screenHeight ) ) { |
| 194 | return fallbackValue; |
| 195 | } |
| 196 | |
| 197 | return { gameWidth, gameHeight, screenWidth, screenHeight }; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | { |
| 202 | // Old format: in-game width x in-game height x on-screen multiplier |
| 203 | static const std::regex resolutionRegex( "^ *([0-9]+) *x *([0-9]+) *x *([0-9]+) *$", std::regex_constants::extended ); |
| 204 | |
| 205 | std::smatch resolutionRegexMatch; |
| 206 | |
| 207 | if ( std::regex_match( it->second, resolutionRegexMatch, resolutionRegex ) ) { |
| 208 | assert( resolutionRegexMatch.size() == 4 ); |
| 209 | |
| 210 | int32_t gameWidth{ 0 }; |
| 211 | int32_t gameHeight{ 0 }; |
| 212 | int32_t screenMultiplier{ 0 }; |
| 213 | |
| 214 | if ( !convertToInt( resolutionRegexMatch[1].str(), gameWidth ) ) { |
| 215 | return fallbackValue; |
| 216 | } |
| 217 | if ( !convertToInt( resolutionRegexMatch[2].str(), gameHeight ) ) { |
| 218 | return fallbackValue; |
| 219 | } |
| 220 | if ( !convertToInt( resolutionRegexMatch[3].str(), screenMultiplier ) ) { |
| 221 | return fallbackValue; |
| 222 | } |
| 223 | |
| 224 | assert( gameWidth >= 0 && gameHeight >= 0 && screenMultiplier >= 0 ); |
| 225 | |
| 226 | if ( screenMultiplier == 0 ) { |
| 227 | return fallbackValue; |
| 228 | } |
| 229 | // Check for potential signed overflow on multiplication |
| 230 | if ( gameWidth > std::numeric_limits<int32_t>::max() / screenMultiplier || gameHeight > std::numeric_limits<int32_t>::max() / screenMultiplier ) { |
| 231 | return fallbackValue; |
| 232 | } |
| 233 | |
| 234 | return { gameWidth, gameHeight, gameWidth * screenMultiplier, gameHeight * screenMultiplier }; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | { |
| 239 | // Abbreviated format: in-game width x in-game height with on-screen multiplier 1 |
| 240 | static const std::regex resolutionRegex( "^ *([0-9]+) *x *([0-9]+) *$", std::regex_constants::extended ); |
| 241 | |
| 242 | std::smatch resolutionRegexMatch; |
| 243 | |
| 244 | if ( std::regex_match( it->second, resolutionRegexMatch, resolutionRegex ) ) { |
| 245 | assert( resolutionRegexMatch.size() == 3 ); |
| 246 | |
| 247 | int32_t gameWidth{ 0 }; |
| 248 | int32_t gameHeight{ 0 }; |
| 249 | |
| 250 | if ( !convertToInt( resolutionRegexMatch[1].str(), gameWidth ) ) { |
| 251 | return fallbackValue; |
| 252 | } |
| 253 | if ( !convertToInt( resolutionRegexMatch[2].str(), gameHeight ) ) { |
| 254 | return fallbackValue; |
| 255 | } |
| 256 | |
| 257 | return { gameWidth, gameHeight }; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return fallbackValue; |
| 262 | } |
| 263 | |
| 264 | bool TinyConfig::Exists( const std::string & key ) const |
| 265 | { |
| 266 | return end() != find( ModifyKey( key ) ); |
| 267 | } |