18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 15:04:58 +01:00

Merge branch 'master' into feat/renderers-cleanup

This commit is contained in:
2026-11-16 21:44:48 +01:00
264 changed files with 41203 additions and 10041 deletions

View File

@@ -84,12 +84,14 @@ enum class TCommandType
cm_SectionVelocity, //ograniczenie prędkości na odcinku
cm_ShuntVelocity, // prędkość manewrowa na semaforze
cm_SetProximityVelocity, // informacja wstępna o ograniczeniu
cm_PointVelocity, // ograniczenie dla czoła w punkcie podaną odległość za komórką, odczytane przy jej minięciu
cm_ChangeDirection,
cm_PassengerStopPoint,
cm_OutsideStation,
// cm_Shunt, // unused?
cm_EmergencyBrake,
cm_SecuritySystemMagnet,
cm_SignalAreaEnd, // end of the interlocking area of the signal passed last
cm_Command // komenda pobierana z komórki
};

View File

@@ -349,6 +349,8 @@ bool global_settings::ConfigParseGraphics(cParser& Parser, const std::string& to
if (token == "gfxrenderer")
{
ParseOne(Parser, GfxRenderer);
// read in any case, as the starter does: "Legacy" written by hand is the legacy renderer
GfxRenderer = ToLower(GfxRenderer);
// "default": the opengl 4.6 renderer, "vulkan": the vulkan 1.4 one (opengl when it can't run on the machine)
if ((GfxRenderer == "full") || (GfxRenderer == "opengl") || (GfxRenderer == "gl46"))
@@ -570,6 +572,21 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string&
return true;
}
// the physics logs of the trains only, of the drivers of rail vehicles; "physicslog yes" writes them all anyway
if (token == "physicslog.trains")
{
ParseOne(Parser, WriteTrainLogFlag);
return true;
}
if (token == "blackbox")
{
bool blackbox{false};
ParseOne(Parser, blackbox);
BlackBox = blackbox;
return true;
}
if (token == "fullphysics")
{
ParseOne(Parser, FullPhysics);
@@ -767,6 +784,13 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string&
return true;
}
if (token == "frogs")
{
// yes: the rails of the plain tracks and of the switches with the key frogs cut where they cross other rails
ParseOne(Parser, Frogs);
return true;
}
if (token == "timespeed")
{
ParseOne(Parser, default_timespeed, 1, false);
@@ -871,6 +895,12 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string&
return true;
}
if (token == "ai.brakesetting.cw1")
{
ParseOne(Parser, AIBrakeSettingCw1, 1, false);
return true;
}
if (token == "pause")
{
std::string value;
@@ -923,6 +953,16 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string&
return true;
}
if (token == "couplerphysics")
{
// yes: the player's train and the trains it closes in on, all: every train
std::string scope;
ParseOne(Parser, scope, 1);
coupler_physics_all = scope == "all";
coupler_physics = coupler_physics_all || scope == "yes" || scope == "true" || scope == "1";
return true;
}
if (token == "controlcoupling.enginecheck")
{
ParseOne(Parser, control_coupling_engine_check, 1);
@@ -2272,6 +2312,10 @@ global_settings::export_as_text( std::ostream &Output ) const {
export_as_text( Output, "fmradio.cleansound", fmradio_cleansound );
export_as_text( Output, "fmradio.stations", fmradio_stations );
export_as_text( Output, "physicslog", WriteLogFlag );
export_as_text( Output, "physicslog.trains", WriteTrainLogFlag );
if( BlackBox ) {
export_as_text( Output, "blackbox", *BlackBox );
}
export_as_text( Output, "fullphysics", FullPhysics );
export_as_text( Output, "physics.sleepheld", physics_sleepheld );
export_as_text( Output, "physics.roadlod", physics_roadlod );
@@ -2312,6 +2356,12 @@ global_settings::export_as_text( std::ostream &Output ) const {
export_as_text( Output, "scenario.weather.temperature", AirTemperature );
export_as_text( Output, "ai.trainman", AITrainman );
export_as_text( Output, "controlcoupling.enginecheck", control_coupling_engine_check );
std::string couplerphysics { coupler_physics ? "yes" : "no" };
if( coupler_physics_all ) {
couplerphysics = "all";
}
export_as_text( Output, "couplerphysics", couplerphysics );
export_as_text( Output, "ai.brakesetting.cw1", AIBrakeSettingCw1 );
export_as_text( Output, "scalespeculars", ScaleSpecularValues );
export_as_text( Output, "gfxrenderer", GfxRenderer );
export_as_text( Output, "shadows", RenderShadows );
@@ -2334,6 +2384,7 @@ global_settings::export_as_text( std::ostream &Output ) const {
Output << "sleepers3dplain " << ( Sleepers3DPlain.empty() ? std::string{ "none" } : Sleepers3DPlain ) << ' ' << ( Sleepers3DPlainSkin.empty() ? std::string{ "none" } : Sleepers3DPlainSkin ) << ' ' << Sleepers3DPlainSeat << '\n';
export_as_text( Output, "rendercab", render_cab );
export_as_text( Output, "createswitchtrackbeds", CreateSwitchTrackbeds );
export_as_text( Output, "frogs", Frogs );
export_as_text( Output, "gfx.resource.sweep", ResourceSweep );
export_as_text( Output, "gfx.resource.move", ResourceMove );
export_as_text( Output, "gfx.textures.streaming", gfx_texture_streaming );

View File

@@ -81,6 +81,7 @@ struct global_settings {
bool bSmoothTraction{ true };
bool CatenaryProcedural{ true }; // przęsła z dopiskiem "type" rysowane wg typu sieci (lina nośna, wieszaki i Y liczone)
bool CreateSwitchTrackbeds{ true };
bool Frogs{ false }; // whether the rails are cut where they cross other rails into frogs, wing rails and check rails; off, they're laid whole as before
std::string szTexturesTGA{ ".tga" }; // lista tekstur od TGA
std::string szTexturesDDS{ ".dds" }; // lista tekstur od DDS
std::string szDefaultExt{ szTexturesDDS };
@@ -114,6 +115,9 @@ struct global_settings {
int iWriteLogEnabled{ 3 }; // maska bitowa: 1-zapis do pliku, 2-okienko, 4-nazwy torów
bool MultipleLogs{ false };
bool crashdump_full{ false }; // crash dump with the whole memory of the process instead of the small one
// the black box of the session (blackbox_doc.md): "blackbox yes" writes it whatever the physics logs, "blackbox no"
// never, and with no entry it is written along with the physics logs
std::optional<bool> BlackBox;
bool ShowSystemConsole{ false }; // toggle visibility of the OS console window with log output
unsigned int DisabledLogTypes{ 0 };
bool ParserLogIncludes{ true };
@@ -152,6 +156,7 @@ struct global_settings {
bool bJoinEvents{ false }; // czy grupować eventy o tych samych nazwach
int iHiddenEvents{ 1 }; // czy łączyć eventy z torami poprzez nazwę toru
bool AITrainman{ true }; // virtual assistant performing consist coupling/decoupling and other maintenance tasks
bool AIBrakeSettingCw1{ true }; // ai.brakesetting.cw1: the AI wagon inspector sets the brakes (G/P/R/Mg) by the instruction Cw-1, no: as before eu07/maszyna#350
// ui
int PythonScreenUpdateRate{ 200 }; // delay between python-based screen updates, in milliseconds
int iTextMode{ 0 }; // tryb pracy wyświetlacza tekstowego
@@ -321,6 +326,12 @@ struct global_settings {
bool captureonstart = true;
bool render_cab = true;
bool crash_damage = true;
// the physical model of the couplings (McZapkie/couplerphysics.h): progressive buffers and draw gear with hysteresis,
// the play of the coupling, its yield and breaking; the legacy model of TMoverParameters::CouplerForce() without it.
// it covers the player's train and the trains it closes in on (vehicle_table::update_coupler_physics()), or every
// train with coupler_physics_all
bool coupler_physics = false;
bool coupler_physics_all = false;
bool control_coupling_engine_check = true; // no multiple-unit control between electric and diesel traction vehicles (eu07/maszyna#520)
bool gui_defaultwindows = true;
bool gui_showtranscripts = true;

View File

@@ -59,13 +59,13 @@ cParser::cParser(std::string const &Stream, buffertype const Type, std::string P
{
case buffer_FILE:
{
Path.append(Stream);
// the file is read in one go, tokens are then extracted straight from the memory
std::ifstream file(Path, std::ios_base::binary);
std::filesystem::path const name{Path + Stream};
std::ifstream file(name, std::ios_base::binary);
if (file.fail())
{
// named in another case than it's on the disk
if (auto const cased{FileCased(Path)}; false == cased.empty())
if (auto const cased{FileCased(name.string())}; false == cased.empty())
{
file.clear();
file.open(cased, std::ios_base::binary);
@@ -112,7 +112,8 @@ cParser::cParser(std::string const &Stream, buffertype const Type, std::string P
// calculate stream size
if (true == mFailBit)
{
ErrorLog("Failed to open file \"" + Path + "\"");
// only a file fails to open, named by the folder and its name
ErrorLog("Failed to open file \"" + Path + Stream + "\"");
}
else
{

263
utilities/physicslog.cpp Normal file
View File

@@ -0,0 +1,263 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "utilities/physicslog.h"
#include "utilities/Logs.h"
#include <algorithm>
#include <cctype>
#include <exception>
#include <fstream>
#include <future>
#include <memory>
#include <string_view>
#include <system_error>
#include <utility>
physicslog_writer PhysicsLogs;
namespace {
// the most of all the logs kept while their files can't be written, shared by those, a log never less than the
// least: the oldest lines go beyond it
constexpr std::size_t keptwhilefailing{64 * 1024 * 1024};
constexpr std::size_t keptwhilefailingleast{64 * 1024};
// the text cut to its last lines within Most
void keep_last(std::string &Text, std::size_t const Most) {
if (Text.size() <= Most)
return;
auto const cut{Text.find('\n', Text.size() - Most)};
Text.erase(0, cut == std::string::npos ? Text.size() : cut + 1);
}
// the name of the file of the log Name, its extension too. what no file name may have on Windows is replaced, on every
// system, so the logs are named alike everywhere: a spawned vehicle is named <spawn>:<number>, and on NTFS "a:1.dat"
// isn't even an error but the stream 1.dat of the file a, which nothing lists
std::string file_name(std::string const &Name) {
std::string name{Name};
for (auto &character : name) {
if (static_cast<unsigned char>(character) < 0x20 || std::string_view{R"(<>:"/\|?*)"}.find(character) != std::string_view::npos)
character = '_';
}
// a device rather than a file, with any extension: con.dat is the console. told by the name before its first dot
auto stem{name.substr(0, name.find('.'))};
while (false == stem.empty() && stem.back() == ' ')
stem.pop_back();
std::ranges::transform(stem, stem.begin(), [](unsigned char const Character) { return static_cast<char>(std::toupper(Character)); });
auto const numbered{stem.size() == 4 && (stem.starts_with("COM") || stem.starts_with("LPT")) && stem[3] >= '1' && stem[3] <= '9'};
if (numbered || stem == "CON" || stem == "PRN" || stem == "AUX" || stem == "NUL")
name.insert(0, 1, '_');
return name + ".dat";
}
// told in the log of the simulator, if there's the memory for it
void tell(std::string const &Text) noexcept {
try {
ErrorLog(Text);
} catch (...) {
// out of memory for a line of the log too: nothing more to be done about it
}
}
} // namespace
physicslog_writer::physicslog_writer(std::filesystem::path Folder, std::chrono::milliseconds const Interval) :
m_folder{std::move(Folder)}, m_shownfolder{m_folder.generic_string()}, m_interval{Interval} {}
physicslog_writer::~physicslog_writer() {
try {
stop();
} catch (...) {
// at the end of the process: what the logs couldn't keep, memory or a file, isn't to end it worse
}
}
void physicslog_writer::begin(std::string const &Name, std::string_view const Header) {
std::scoped_lock lock{m_mutex};
auto &lines{m_pending[Name]};
lines.fresh = true;
lines.header.assign(Header);
lines.text.clear();
start();
}
void physicslog_writer::append(std::string const &Name, std::string_view const Line) {
std::scoped_lock lock{m_mutex};
m_pending[Name].text.append(Line);
start();
}
void physicslog_writer::flush() {
std::scoped_lock files{m_filemutex};
write();
}
bool physicslog_writer::flush_for(std::chrono::milliseconds const Timeout) {
// on a thread of its own, left behind if it doesn't end in time: a disk which doesn't answer holds it up in the
// writing, not only the lock. for the end of the process, which doesn't wait for it
auto done{std::make_shared<std::promise<void>>()};
auto ended{done->get_future()};
std::thread([this, done] {
flush_safely();
done->set_value();
}).detach();
return ended.wait_for(Timeout) == std::future_status::ready;
}
void physicslog_writer::stop() {
std::jthread thread;
bool itself;
{
std::scoped_lock lock{m_mutex};
m_stopped = true;
// taken by the first to stop, so it's joined once
thread = std::move(m_thread);
itself = m_writer == std::this_thread::get_id();
}
// the stop asked outside the lock, which waking the writer takes
if (thread.joinable())
thread.request_stop();
// stopped from the writer itself, by the end of a run which failed in it: the files may be held by it
if (itself) {
if (thread.joinable())
thread.detach();
return;
}
if (thread.joinable())
thread.join();
flush_safely();
}
std::filesystem::path physicslog_writer::file(std::string const &Name) const {
auto const name{file_name(Name)};
#ifdef _WIN32
// in the code page of the system, as the standard library would convert it, but with the bytes not of it replaced
// instead of an exception thrown: a vehicle of a scenery written in 1250 on a Windows set to UTF-8, say
auto const size{static_cast<int>(name.size())};
std::wstring wide(static_cast<std::size_t>(::MultiByteToWideChar(CP_ACP, 0, name.data(), size, nullptr, 0)), L'\0');
::MultiByteToWideChar(CP_ACP, 0, name.data(), size, wide.data(), static_cast<int>(wide.size()));
return m_folder / wide;
#else
// the bytes are the name of the file as they are
return m_folder / name;
#endif
}
std::string physicslog_writer::shown(std::string const &Name) const {
return m_shownfolder + '/' + file_name(Name);
}
void physicslog_writer::start() {
if (m_stopped || m_thread.joinable())
return;
m_thread = std::jthread([this](std::stop_token const &Stop) { run(Stop); });
m_writer = m_thread.get_id();
}
void physicslog_writer::run(std::stop_token const &Stop) {
// woken by every stop asked, by stop() or by the end of the thread itself. the lock taken once first: the writer is
// either about to wait, and sees the stop, or waiting already
std::stop_callback const wake{Stop, [this] {
{ std::scoped_lock lock{m_mutex}; }
m_wake.notify_all();
}};
while (false == Stop.stop_requested()) {
{
std::unique_lock lock{m_mutex};
// woken early only to stop, which writes the rest itself
if (m_wake.wait_for(lock, m_interval, [&Stop] { return Stop.stop_requested(); }))
return;
}
flush_safely();
}
}
void physicslog_writer::flush_safely() noexcept {
try {
flush();
} catch (std::exception const &Error) {
tell(std::string{"Bad physics log: the writing failed ("} + Error.what() + "), the lines taken for it are lost");
} catch (...) {
tell("Bad physics log: the writing failed, the lines taken for it are lost");
}
}
void physicslog_writer::write() {
std::map<std::string, pending> taken;
{
std::scoped_lock lock{m_mutex};
taken.swap(m_pending);
}
if (taken.empty())
return;
std::error_code error;
std::filesystem::create_directories(m_folder, error);
std::map<std::string, pending> failed;
for (auto &[name, lines] : taken) {
if (lines.text.empty() && false == lines.fresh)
continue;
// a log going wrong otherwise, out of memory say, is kept for the next try as one whose file can't be written:
// the others are written still, and the writer goes on
auto written{false};
std::string problem{"can't write"};
try {
written = write_log(name, lines);
} catch (std::exception const &Error) {
problem = std::string{"failed ("} + Error.what() + ") to write";
}
if (written) {
if (m_failed.erase(name) > 0)
WriteLog("Physics log: " + shown(name) + " written again");
continue;
}
if (m_failed.insert(name).second)
ErrorLog("Bad physics log: " + problem + " " + shown(name) + ", its last lines are kept for the next try");
failed.emplace(name, std::move(lines));
}
if (failed.empty())
return;
// cut before the lock, which the simulation waits on to give its lines: only what came since is added under it
auto const most{std::max(keptwhilefailingleast, keptwhilefailing / failed.size())};
for (auto &[name, lines] : failed)
keep_last(lines.text, most);
// tried again the next time, before what came since; a file held by another program for a while doesn't lose
// them, nor its header. a log begun anew since drops them
std::scoped_lock lock{m_mutex};
for (auto &[name, lines] : failed) {
auto &now{m_pending[name]};
if (now.fresh)
continue;
lines.text.append(now.text);
now = std::move(lines);
}
}
bool physicslog_writer::write_log(std::string const &Name, pending const &Lines) const {
auto const path{file(Name)};
std::error_code error;
// how long it was, to be cut back to if the lines are written in part only, a disk filling up say: else they'd be
// written twice, the next time
auto const before{Lines.fresh ? 0 : std::filesystem::file_size(path, error)};
auto const existed{false == Lines.fresh && false == static_cast<bool>(error)};
// in text mode, as the logs were always written: on Windows their lines, which end with "\r\n", end with "\r\r\n"
std::ofstream stream{path, Lines.fresh ? std::ios::out | std::ios::trunc : std::ios::out | std::ios::app};
if (stream.is_open()) {
if (Lines.fresh)
stream << Lines.header;
stream << Lines.text;
}
stream.close();
if (false == stream.fail())
return true;
if (existed)
std::filesystem::resize_file(path, before, error);
return false;
}

88
utilities/physicslog.h Normal file
View File

@@ -0,0 +1,88 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <chrono>
#include <condition_variable>
#include <filesystem>
#include <map>
#include <mutex>
#include <set>
#include <stop_token>
#include <string>
#include <string_view>
#include <thread>
// the physics logs, <folder>/<name>.dat, what no file name may have on Windows replaced in the name (a spawned vehicle's
// has a ':'), the same on every system. the lines given are kept, and written every two seconds by a thread of its own,
// each file opened only for the moment it is written to: the logs of hundreds of vehicles hold no files open, and the
// simulation doesn't wait for the disk. a crash loses the last two seconds of real time of them. lines whose file can't
// be written, held by another program say, are kept for the next try: the last 64 MB of them all at most
class physicslog_writer {
public:
explicit physicslog_writer(std::filesystem::path Folder = "physicslog", std::chrono::milliseconds Interval = std::chrono::seconds{2});
physicslog_writer(physicslog_writer const &) = delete;
physicslog_writer &operator=(physicslog_writer const &) = delete;
// stops, writing what is kept
~physicslog_writer();
// starts the log Name anew: its file is emptied and Header written into it, the next time the logs are written.
// the lines of it not written yet are dropped
void begin(std::string const &Name, std::string_view Header);
// Line at the end of the log Name. a log not begun is added to as its file stands
void append(std::string const &Name, std::string_view Line);
// writes what is kept, now
void flush();
// as flush, waited for no longer than Timeout: then false, and the writing goes on by itself, on a thread left
// behind. for the end of the process, the files held up or the disk not answering
bool flush_for(std::chrono::milliseconds Timeout);
// writes what is kept and ends the thread. what is given after is written by a flush, or at the end
void stop();
// the file of the log Name: <folder>/<name>.dat, with < > : " / \ | ? * and the control characters of the name
// replaced by _, and _ before a name Windows keeps for a device (con, nul, com1...). the bytes of the code page of a
// scenery (1250) stay as they are, also on a Windows set to UTF-8, where the standard library would throw making a
// path of them: there they become U+FFFD
std::filesystem::path file(std::string const &Name) const;
// the same, for the log of the simulator
std::string shown(std::string const &Name) const;
private:
struct pending {
bool fresh{false}; // its file is to be emptied first, and the header written into it
std::string header;
std::string text;
};
// the thread, on the first line given, unless stopped. called with m_mutex held
void start();
void run(std::stop_token const &Stop);
// as flush, letting nothing out: on the writer's thread, or at the end of the process, an exception would end the
// simulator. what was taken for a writing which failed as a whole is lost, the next one goes on with what came since
void flush_safely() noexcept;
// called with m_filemutex held
void write();
// the lines of the log Name written into its file. returns: true if they all were
bool write_log(std::string const &Name, pending const &Lines) const;
std::filesystem::path const m_folder;
std::string const m_shownfolder;
std::chrono::milliseconds const m_interval;
std::mutex m_mutex; // the lines kept and the thread
std::map<std::string, pending> m_pending;
bool m_stopped{false};
std::condition_variable m_wake;
std::jthread m_thread;
std::thread::id m_writer; // of the thread, kept after it is taken to be joined: the writer knows itself by it
std::mutex m_filemutex; // the files are written one batch at a time, so the lines of a log stay in order
std::set<std::string> m_failed; // the logs whose file couldn't be written, told once
};
// the physics logs of the drivers (physicslog yes), and those the black box writes of the user's vehicles
extern physicslog_writer PhysicsLogs;

View File

@@ -367,6 +367,30 @@ std::string win1250_to_utf8(const std::string &Input)
return output;
}
// counterpart of win1250_to_utf8(). characters outside of the supported set are replaced with question marks
std::string utf8_to_win1250(std::string const &Text)
{
// polish letters, as utf-8 sequences; the same set win1250_to_utf8() handles
static std::unordered_map<std::string, char> const charmap{{"\xC4\x84", '\xA5'}, {"\xC4\x86", '\xC6'}, {"\xC4\x98", '\xCA'}, {"\xC5\x81", '\xA3'}, {"\xC5\x83", '\xD1'}, {"\xC3\x93", '\xD3'},
{"\xC5\x9A", '\x8C'}, {"\xC5\xB9", '\x8F'}, {"\xC5\xBB", '\xAF'}, {"\xC4\x85", '\xB9'}, {"\xC4\x87", '\xE6'}, {"\xC4\x99", '\xEA'},
{"\xC5\x82", '\xB3'}, {"\xC5\x84", '\xF1'}, {"\xC3\xB3", '\xF3'}, {"\xC5\x9B", '\x9C'}, {"\xC5\xBA", '\x9F'}, {"\xC5\xBC", '\xBF'}};
std::string output;
for (std::size_t idx = 0; idx < Text.size();)
{
auto const lead{static_cast<unsigned char>(Text[idx])};
if (lead < 0x80)
{
output += Text[idx++];
continue;
}
auto const length{std::min<std::size_t>((lead & 0xE0) == 0xC0 ? 2 : (lead & 0xF0) == 0xE0 ? 3 : (lead & 0xF8) == 0xF0 ? 4 : 1, Text.size() - idx)};
auto const lookup{charmap.find(Text.substr(idx, length))};
output += (lookup != charmap.end() ? lookup->second : '?');
idx += length;
}
return output;
}
namespace
{
// unicode code points of the upper half of the windows-1250 code page; 0 marks the unassigned bytes
@@ -507,6 +531,140 @@ bool FileExists(std::string const &Filename)
return std::filesystem::exists(Filename);
}
#ifndef _WIN32
namespace
{
// the names in a folder by their lowercase, the time the folder was changed and the time they were read
struct cased_folder
{
bool read{false};
std::filesystem::file_time_type changed{};
std::filesystem::file_time_type listed{};
// when the time of the change was last looked at
std::chrono::steady_clock::time_point checked{};
std::unordered_map<std::string, std::string> names;
};
std::mutex cased_mutex;
std::unordered_map<std::string, cased_folder> cased_folders;
// a folder by the name it's given with (parts split by '/', a '/' at the end): as it's named on the disk, or missing in any case
// since the time given
struct cased_path
{
std::string name;
bool missing{false};
std::chrono::steady_clock::time_point checked{};
};
std::unordered_map<std::string, cased_path> cased_paths;
// the names in the folder into Cached by their lowercase, with the time the folder was changed and the time they were read. a named
// function rather than a lambda capturing Cached, which Sonar takes for a reference to null (S2259)
void read_cased_folder(cased_folder &Cached, std::filesystem::path const &Folder, std::filesystem::file_time_type const Changed)
{
Cached.names.clear();
Cached.changed = Changed;
Cached.listed = std::filesystem::file_time_type::clock::now();
std::error_code error;
for (std::filesystem::directory_iterator entry{Folder, error}, end; (false == bool(error)) && (entry != end); entry.increment(error))
{
auto const name{entry->path().filename().string()};
Cached.names.try_emplace(ToLower(name), name);
}
// a listing cut short isn't kept
Cached.read = (false == bool(error));
}
// the name in the folder matching the one given in any case, empty when there's none. the folder is read again once it was
// changed, as the editor writes files while it runs, the time of the change looked at once a second at most, which keeps a
// missing file as cheap as a single check; a name missing from a folder changed shortly before it was read is looked for again,
// the time of the change being as coarse as the clock of the file system. to be called with cased_mutex held
std::string cased_name(std::string const &Folder, std::string const &Name)
{
std::filesystem::path const folder{Folder.empty() ? std::string{"."} : Folder};
std::error_code error;
auto &cached{cased_folders[Folder]};
auto const now{std::chrono::steady_clock::now()};
auto changed{cached.changed};
if ((false == cached.read) || (now - cached.checked >= std::chrono::seconds{1}))
{
changed = std::filesystem::last_write_time(folder, error);
if (error)
{
// a folder found before may have been renamed or removed since
cased_folders.erase(Folder);
cased_paths.clear();
return {};
}
cached.checked = now;
}
auto const wanted{ToLower(Name)};
if ((false == cached.read) || (cached.changed != changed))
read_cased_folder(cached, folder, changed);
auto found{cached.names.find(wanted)};
// a folder changed in the future, as from another clock, isn't read again on every miss
if (auto const age{cached.listed - cached.changed}; (found == cached.names.end()) && (age >= std::chrono::seconds{0}) && (age < std::chrono::seconds{2}))
{
read_cased_folder(cached, folder, changed);
found = cached.names.find(wanted);
}
return found != cached.names.end() ? found->second : std::string{};
}
// keeps the folder by the name it's given with as Name, or as missing since the time given when Name is empty, and puts Name into
// Cased; false when it's missing
bool keep_cased_path(std::string const &Folder, std::string const &Name, std::chrono::steady_clock::time_point const Now, std::string &Cased)
{
auto &path{cased_paths[Folder]};
path.name = Name;
path.missing = Name.empty();
path.checked = Now;
Cased = Name;
return false == Name.empty();
}
// the folder as it's named on the disk into Cased, from the one given with '/' between its parts and at its end ("" the current
// folder); false when it's missing in any case. the folders found are kept, and the missing ones for a second, so a file missing
// costs a check of its folder alone. to be called with cased_mutex held
bool cased_folder_path(std::string const &Folder, std::string &Cased)
{
if (Folder.empty() || (Folder == "/"))
{
Cased = Folder;
return true;
}
auto const now{std::chrono::steady_clock::now()};
if (auto const found{cased_paths.find(Folder)}; found != cased_paths.end())
{
if (false == found->second.missing)
{
Cased = found->second.name;
return true;
}
if (now - found->second.checked < std::chrono::seconds{1})
return false;
}
std::error_code error;
if (std::filesystem::is_directory(Folder, error))
{
// named as it is on the disk
return keep_cased_path(Folder, Folder, now, Cased);
}
// the parent, then the last part among the names in it
auto const last{Folder.size() - 1};
auto const split{Folder.rfind('/', last - 1)};
auto const parent{split == std::string::npos ? std::string{} : Folder.substr(0, split + 1)};
auto const part{Folder.substr(parent.size(), last - parent.size())};
std::string casedparent;
if (false == cased_folder_path(parent, casedparent))
return keep_cased_path(Folder, {}, now, Cased);
auto const name{((part == ".") || (part == "..")) ? part : cased_name(casedparent, part)};
return keep_cased_path(Folder, name.empty() ? std::string{} : casedparent + name + '/', now, Cased);
}
} // namespace
#endif
std::string FileCased(std::filesystem::path const &Filename)
{
std::error_code error;
@@ -517,16 +675,59 @@ std::string FileCased(std::filesystem::path const &Filename)
// code page to throw on
return {};
#else
// only the name of the file itself, a folder named in another case isn't found
auto const directory{Filename.has_parent_path() ? Filename.parent_path() : std::filesystem::path{"."}};
auto const wanted{ToLower(Filename.filename().string())};
for (std::filesystem::directory_iterator entry{directory, error}, end; entry != end; entry.increment(error))
if (ToLower(entry->path().filename().string()) == wanted)
return Filename.has_parent_path() ? (directory / entry->path().filename()).string() : entry->path().filename().string();
return {};
// the folder, then the file in it: the folders can be named in another case as well as the file
auto text{Filename.string()};
std::replace(text.begin(), text.end(), '\\', '/');
std::string folder{(false == text.empty()) && (text.front() == '/') ? "/" : ""};
std::string file;
for (std::size_t start{0}; start < text.size();)
{
auto end{text.find('/', start)};
if (end == std::string::npos)
end = text.size();
if (end > start)
{
if (false == file.empty())
folder += file + '/';
file = text.substr(start, end - start);
}
start = end + 1;
}
if (file.empty())
return {};
std::string cased;
{
std::scoped_lock const lock{cased_mutex};
if (false == cased_folder_path(folder, cased))
return {};
auto const name{((file == ".") || (file == "..")) ? file : cased_name(cased, file)};
if (name.empty())
return {};
cased += name;
}
return std::filesystem::exists(cased, error) ? cased : std::string{};
#endif
}
std::string FileOnDisk(std::string const &Filename)
{
auto cased{FileCased(Filename)};
return cased.empty() ? Filename : cased;
}
std::string FileStemOnDisk(std::string const &Folder, std::string const &Name, std::vector<std::string> const &Extensions)
{
for (auto const &extension : Extensions)
{
auto const cased{FileCased(Folder + Name + extension)};
if (cased.size() > Folder.size() + extension.size() && cased.starts_with(Folder) && cased.ends_with(extension))
{
return cased.substr(Folder.size(), cased.size() - Folder.size() - extension.size());
}
}
return Name;
}
std::pair<std::string, std::string> FileExists(std::vector<std::string> const &Names, std::vector<std::string> const &Extensions)
{
@@ -534,7 +735,8 @@ std::pair<std::string, std::string> FileExists(std::vector<std::string> const &N
{
for (auto const &extension : Extensions)
{
if (FileExists(name + extension))
// found in any case, given back as named here: the name stays the key of the banks, the file is opened by FileOnDisk()
if (false == FileCased(name + extension).empty())
{
return {name, extension};
}

View File

@@ -172,6 +172,8 @@ void win1250_to_ascii(std::string &Input);
std::string Bezogonkow(std::string Input, bool const Underscorestospaces = false);
std::string win1250_to_utf8(const std::string &input);
// counterpart of win1250_to_utf8(): the polish letters, the other characters outside of ascii as question marks
std::string utf8_to_win1250(std::string const &Text);
// decodes utf-8 encoded text into unicode code points. returns false if the text isn't valid utf-8
bool utf8_to_utf32(std::string const &Text, std::u32string &Output);
// decodes windows-1250 (ansi) encoded text into unicode code points
@@ -223,10 +225,17 @@ template <typename Type_> bool extract_value(Type_ &Variable, std::string const
template <> bool extract_value(bool &Variable, std::string const &Key, std::string const &Input, std::string const &Default);
bool FileExists(std::string const &Filename);
// the file as it's named on the disk when only the case of its name differs, as in the sceneries made on Windows; empty when
// there's none. the folders have to be named in their case; on Windows, finding the names in any case itself, the file given or none
// the file as it's named on the disk when only the case of its name or of its folders differs, as in the sceneries made on Windows;
// empty when there's none. on Windows, finding the names in any case itself, the file given or none
std::string FileCased(std::filesystem::path const &Filename);
// the file to open: as it's named on the disk, the name given when there's none
std::string FileOnDisk(std::string const &Filename);
// the name (relative to the folder, without extension) as it's named on the disk with the first of the extensions it has a file with,
// found in any case; the name given when there's none
std::string FileStemOnDisk(std::string const &Folder, std::string const &Name, std::vector<std::string> const &Extensions);
// the first name with one of the extensions which has a file in any case, in the order given; given back as named here, the file
// is opened by FileOnDisk()
std::pair<std::string, std::string> FileExists(std::vector<std::string> const &Names, std::vector<std::string> const &Extensions);
// returns time of last modification for specified file