17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-26 15:54:53 +01:00
Files
maszyna/launcher/textures_scanner.cpp
maj00r 4512ed4e0b fix(editor): guard the places where the editor could crash outright
Found with an address and undefined behaviour sanitizer build, random
monkey sessions on the test sceneries and broken inputs:

- A deleted model left its pointer in the pick list of the renderer, the
  bound node of the panels, the bindings of the infrastructure and the
  selection: the next pick or panel frame read freed memory. The renderer
  forgets it, and one erase_model does the cleaning for all the places
  which deleted a model by hand.
- The plan band of a route read the vertex before the first one in the
  frame a drag was let go.
- A scenery which can't be opened goes back to the launcher with the
  reason in the log, a track of an unknown type or a tributary loads as
  a normal track or a river with a message, and a spline with no finite
  length or step gets a sane number of segments.
- Typed numbers which aren't (NaN, inf, ends 0 m or 1000 km apart, a
  vector of 1e30, a diverging track part of 5 km, a curve threshold of 0,
  a zero history size, radius norms below zero) are refused or clamped,
  where they made the geometry run away or divide by zero.
- Folders which can't be read and file names which aren't valid in the
  locale no longer throw out of the scans of sceneries, textures, sweeps,
  roads and signal plates; the scans skip them.
- The launcher: Ctrl+Del with no trainset selected, a dropped vehicle
  whose skin is gone and a text with a percent sign.
- A terrain mesh made for another resolution of its chunk isn't put into
  the chunk, and a slot is reused only for a mesh of the same size.
- Vehicles on a track changed by the editor are moved over a copy of the
  list they leave.
- The table of the keyboard shortcuts gives its stretched column a
  weight, else ImGui divides 0 by 0 in the first frame of the window.
2026-10-21 08:42:29 +02:00

270 lines
6.9 KiB
C++

#include "stdafx.h"
#include "textures_scanner.h"
#include "utilities/utilities.h"
void ui::vehicles_bank::scan_textures()
{
std::error_code error;
for (std::filesystem::recursive_directory_iterator f{"dynamic", std::filesystem::directory_options::skip_permission_denied, error}, end; f != end; f.increment(error)) {
if (f->path().filename() != "textures.txt")
continue;
std::fstream stream(f->path(), std::ios_base::binary | std::ios_base::in);
ctx_path = std::filesystem::relative(f->path().parent_path(), "dynamic/", error);
std::string line;
while (std::getline(stream, line))
{
if (line.size() < 3)
continue;
if (line.back() == '\r')
line.pop_back();
parse_entry(line);
}
}
}
void ui::vehicles_bank::parse_entry(const std::string &line)
{
std::string content;
std::shared_ptr<skin_meta> meta;
size_t pos = line.find("//");
if (pos != -1)
meta = parse_meta(line.substr(pos));
content = line.substr(0, pos);
std::istringstream stream(content);
std::string target;
std::getline(stream, target, '=');
std::string param;
while (std::getline(stream, param, '=')) {
if (param.back() == ' ')
param.pop_back();
if (line[0] == '!')
parse_category_entry(param);
else if (line.starts_with("**"))
parse_texture_rule(target.substr(2), param);
else if (line[0] == '*')
parse_coupling_rule(target.substr(1), param);
else if (line[0] == '@')
parse_controllable_entry(target.substr(1), param);
else
parse_texture_info(target, param, meta);
}
}
void ui::vehicles_bank::parse_category_entry(const std::string &param)
{
using enum ui::vehicle_type;
static std::unordered_map<char, vehicle_type> type_map = {
{ 'e', electric_loco },
{ 's', diesel_loco },
{ 'p', steam_loco },
{ 'a', railcar },
{ 'z', emu },
{ 'r', utility },
{ 'd', draisine },
{ 't', tram },
{ 'c', truck },
{ 'b', bus },
{ 'o', car },
{ 'h', man },
{ 'f', animal },
};
ctx_type = unknown;
std::istringstream stream(param);
std::string tok;
std::getline(stream, tok, ',');
if (tok.size() < 1)
return;
if (auto it = type_map.find(tok[0]); it != type_map.end())
ctx_type = it->second;
else if (tok[0] >= 'A' && tok[0] <= 'Z')
ctx_type = carriage;
std::string mini;
std::getline(stream, mini, ',');
category_icons.try_emplace(ctx_type, "textures/mini/" + ToLower(mini) + ".bmp");
}
void ui::vehicles_bank::parse_controllable_entry(const std::string &target, const std::string &param)
{
get_vehicle(target)->controllable =
param.size() >= 1 && param[0] == '1';
}
void ui::vehicles_bank::parse_texture_info(const std::string &target, const std::string &param, std::shared_ptr<skin_meta> meta)
{
std::istringstream stream(param);
std::string model;
std::string mini;
std::string miniplus;
std::getline(stream, model, ',');
std::getline(stream, mini, ',');
std::getline(stream, miniplus, ',');
auto vehicle = get_vehicle(model);
skin_set set;
set.vehicle = vehicle;
set.group = mini;
set.meta = meta;
if (!mini.empty())
group_icons.try_emplace(mini, deferred_image("textures/mini/" + ToLower(mini) + ".bmp"));
if (!miniplus.empty())
set.mini = deferred_image("textures/mini/" + ToLower(miniplus) + ".bmp");
else if (!mini.empty())
set.mini = deferred_image("textures/mini/" + ToLower(mini) + ".bmp");
set.skin = ToLower(target);
erase_extension(set.skin);
/*
std::istringstream tex_stream(target);
std::string texture;
while (std::getline(tex_stream, texture, '|')) {
set.skins.push_back(ctx_path / ToLower(texture));
}
*/
group_map[set.group].insert(vehicle);
vehicle->matching_skinsets.push_back(std::make_shared<skin_set>(std::move(set)));
}
std::shared_ptr<ui::skin_meta> ui::vehicles_bank::parse_meta(const std::string &str) const
{
std::istringstream stream(str);
auto meta = std::make_shared<skin_meta>();
std::string version;
std::getline(stream, version, ',');
std::getline(stream, meta->name, ',');
std::getline(stream, meta->short_id, ',');
std::getline(stream, meta->location, ',');
std::getline(stream, meta->rev_date, ',');
std::getline(stream, meta->rev_company, ',');
std::getline(stream, meta->texture_author, ',');
std::getline(stream, meta->photo_author, ',');
meta->name = win1250_to_utf8(meta->name);
meta->short_id = win1250_to_utf8(meta->short_id);
meta->location = win1250_to_utf8(meta->location);
meta->rev_date = win1250_to_utf8(meta->rev_date);
meta->rev_company = win1250_to_utf8(meta->rev_company);
meta->texture_author = win1250_to_utf8(meta->texture_author);
meta->photo_author = win1250_to_utf8(meta->photo_author);
meta->search_lowered +=
ToLower("n:" + meta->name + ":i:" + meta->short_id + ":d:" + meta->location +
":r:" + meta->rev_date + ":c:" + meta->rev_company + ":t:" + meta->texture_author + ":p:" + meta->photo_author);
std::ranges::replace(meta->location, '_', ' ');
std::ranges::replace(meta->rev_company, '_', ' ');
std::ranges::replace(meta->texture_author, '_', ' ');
std::ranges::replace(meta->photo_author, '_', ' ');
if (!meta->rev_date.empty() && meta->rev_date != "?") {
std::istringstream datestream(meta->rev_date);
std::string day;
std::string month;
std::getline(datestream, day, '.');
std::getline(datestream, month, '.');
datestream >> meta->rev_year;
}
return meta;
}
void ui::vehicles_bank::parse_coupling_rule(const std::string &target, const std::string &param)
{
auto target_vehicle = get_vehicle(target);
if (param == "?")
return;
std::istringstream stream(param);
int coupling_flag;
stream >> coupling_flag;
stream.ignore(1000, ',');
std::string connected;
std::getline(stream, connected, ',');
std::string param1;
std::string param2;
std::getline(stream, param1, ',');
std::getline(stream, param2, ',');
coupling_rule rule;
rule.coupled_vehicle = target_vehicle;
rule.coupling_flag = coupling_flag;
get_vehicle(connected)->coupling_rules.push_back(rule);
}
void ui::vehicles_bank::parse_texture_rule(const std::string &target, const std::string &param)
{
auto target_vehicle = get_vehicle(target);
std::istringstream stream(param);
std::string prev;
std::getline(stream, prev, ',');
std::string replace_rule;
while (std::getline(stream, replace_rule, ',')) {
if (replace_rule == "-")
break;
std::istringstream rule_stream(replace_rule);
std::string src;
std::string dst;
std::getline(rule_stream, src, '-');
std::getline(rule_stream, dst, '-');
texture_rule rule;
rule.previous_vehicle = get_vehicle(prev);
rule.replace_rules.emplace_back(src, dst);
target_vehicle->texture_rules.push_back(rule);
}
}
std::shared_ptr<ui::vehicle_desc> ui::vehicles_bank::get_vehicle(const std::string &name)
{
auto path = ctx_path / ToLower(name);
path = std::filesystem::path(path.generic_string());
auto it = vehicles.find(path);
if (it != vehicles.end()) {
return it->second;
}
else {
auto desc = std::make_shared<vehicle_desc>();
desc->type = ctx_type;
desc->path = path;
vehicles.try_emplace(path, desc);
return desc;
}
}