17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-05 00:45:42 +01:00

fix for sonar

This commit is contained in:
maj00r
2026-09-14 22:51:33 +02:00
parent e7b64c7397
commit 3fa3853018
2 changed files with 21 additions and 10 deletions

View File

@@ -31,6 +31,7 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/dictionary.h" #include "utilities/dictionary.h"
#include "version_info.h" #include "version_info.h"
#include <chrono> #include <chrono>
#include <format>
#include "utilities/translation.h" #include "utilities/translation.h"
#if WITH_DISCORD_RPC #if WITH_DISCORD_RPC
@@ -320,9 +321,11 @@ int eu07_application::init(int Argc, char *Argv[])
return result; return result;
} }
// the run's seed, settled once and kept in Globals: the engines come out of it // the run's seed, settled once and kept in Globals: the engines come out of it.
// narrowed on purpose - mt19937 and seed_of() both work on 32 bits, and that is
// the seed somebody can write down and pass on
if (!Global.random_seed) if (!Global.random_seed)
Global.random_seed = true_random_seed(); Global.random_seed = static_cast<std::uint32_t>(true_random_seed());
Global.random_engine.seed(Global.random_seed); Global.random_engine.seed(Global.random_seed);
Global.local_random_engine.seed(Global.random_seed); Global.local_random_engine.seed(Global.random_seed);
@@ -1149,7 +1152,7 @@ int eu07_application::init_settings(int Argc, char *Argv[])
if (i + 1 < Argc) if (i + 1 < Argc)
{ {
Global.random_seed = seed_of(Argv[++i]); Global.random_seed = seed_of(Argv[++i]);
WriteLog("seed given on the command line: " + std::string(Argv[i]) + " -> " + std::to_string(Global.random_seed)); WriteLog(std::format("seed given on the command line: {} -> {}", Argv[i], Global.random_seed));
} }
} }
else else
@@ -1434,7 +1437,7 @@ int eu07_application::init_data()
int eu07_application::init_modes() int eu07_application::init_modes()
{ {
Global.local_random_engine.seed(true_random_seed()); Global.local_random_engine.seed(static_cast<std::mt19937::result_type>(true_random_seed()));
if ((!Global.network_servers.empty() || Global.network_client) && Global.SceneryFile.empty()) if ((!Global.network_servers.empty() || Global.network_client) && Global.SceneryFile.empty())
{ {

View File

@@ -29,6 +29,7 @@ Copyright (C) 2007-2014 Maciej Cierniak
#include "utilities/Globals.h" #include "utilities/Globals.h"
#include "utilities/parser.h" #include "utilities/parser.h"
#include "utilities/U8.h" #include "utilities/U8.h"
#include "utilities/Logs.h"
@@ -142,21 +143,28 @@ std::uint64_t true_random_seed()
try try
{ {
std::random_device rd; std::random_device rd;
if (rd.entropy() > 0.0) if (rd.entropy() > 0.0)
{ {
std::uint32_t high = rd(); // NOTE: both halves have to be 64 bit wide before the shift - shifting a std::uint32_t
std::uint32_t low = rd(); // by 32 is undefined behaviour, and drops the high half of the seed
std::uint64_t const high{rd()};
std::uint64_t const low{rd()};
return (high << 32) | low; return (high << 32) | low;
} }
} }
catch (...) {} catch (std::exception const &Error)
{
// std::random_device throws when the machine has no usable hardware entropy source;
// the clock below stands in for it, which is worth saying out loud since it is a
// weaker seed than the caller asked for
WriteLog(std::format("random: no hardware entropy source ({}), seeding from the clock instead", Error.what()));
}
return static_cast<std::uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()); return static_cast<std::uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
} }
std::uint32_t seed_of(std::string const &Text) std::uint32_t seed_of(std::string const &Text)
{ {
std::uint32_t number{}; if (std::uint32_t number{}; std::from_chars(Text.data(), Text.data() + Text.size(), number).ec == std::errc{})
if (std::from_chars(Text.data(), Text.data() + Text.size(), number).ec == std::errc{})
{ {
return number; return number;
} }