|
35 | 35 | #include <type_traits> |
36 | 36 | #include <Windows.h> |
37 | 37 | #include <stdio.h> |
38 | | -#include <CRT.h> |
39 | 38 |
|
40 | 39 | //! Parses strings into one or more elements of another type. |
41 | 40 | /*! |
@@ -261,35 +260,45 @@ inline bool Parser<int>::TryParse(const char* pValue, OutType* outValue) |
261 | 260 | template<> |
262 | 261 | inline bool Parser<double>::TryParse(const char* pValue, OutType* outValue) |
263 | 262 | { |
| 263 | + errno = 0; |
| 264 | + char* end; |
264 | 265 |
|
265 | | - // Game doesn't use double precision when parsing, using double here would create inconsistency. |
266 | | - float buffer = 0.0; |
| 266 | + // Nov 23, 2025 - Starkku: strtod() + cast result to float produces results |
| 267 | + // more similar to game's CRT functions than using sscanf_s. |
| 268 | + double value = strtod(pValue, &end); |
267 | 269 |
|
268 | | - // Use game's sscanf function, the C library one has different precision/rounding. |
269 | | - if (CRT::sscanf(pValue, "%f", &buffer) == 1) |
| 270 | + if (pValue == end || errno == ERANGE) |
| 271 | + return false; |
| 272 | + |
| 273 | + float floatValue = static_cast<float>(value); |
| 274 | + |
| 275 | + if (strchr(pValue, '%')) |
270 | 276 | { |
271 | | - if (strchr(pValue, '%')) |
272 | | - { |
273 | | - buffer *= 0.01f; |
274 | | - } |
275 | | - if (outValue) |
276 | | - { |
277 | | - *outValue = buffer; |
278 | | - } |
279 | | - return true; |
| 277 | + floatValue *= 0.01f; |
280 | 278 | } |
281 | | - return false; |
| 279 | + if (outValue) |
| 280 | + { |
| 281 | + *outValue = floatValue; |
| 282 | + } |
| 283 | + return true; |
282 | 284 | }; |
283 | 285 |
|
284 | 286 | template<> |
285 | 287 | inline bool Parser<float>::TryParse(const char* pValue, OutType* outValue) |
286 | 288 | { |
287 | 289 | double buffer = 0.0; |
| 290 | + |
288 | 291 | if (Parser<double>::TryParse(pValue, &buffer)) |
289 | 292 | { |
| 293 | + float floatValue = static_cast<float>(buffer); |
| 294 | + |
| 295 | + if (strchr(pValue, '%')) |
| 296 | + { |
| 297 | + floatValue *= 0.01f; |
| 298 | + } |
290 | 299 | if (outValue) |
291 | 300 | { |
292 | | - *outValue = static_cast<float>(buffer); |
| 301 | + *outValue = floatValue; |
293 | 302 | } |
294 | 303 | return true; |
295 | 304 | } |
|
0 commit comments