mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 22:55:16 +01:00
* fix(editor): a model file named outside the code page no longer crashes the first track selection The first selection of a track builds the track property list, and with it the sleeper and timber model choices. pack_models() walked the whole models/ tree and turned the name of every .t3d/.e3d into a narrow string before looking at whether it was a sleeper at all. On Windows that goes through the ANSI code page, so a single model file anywhere under models/ named with a character the code page lacks threw std::system_error "No mapping for the Unicode character exists in the target multi-byte code page" out of the editor frame and the editor stopped (GE#560). The conversions are now in a try, as in the editor's other folder scans: a file whose name can't be converted is left out (the game couldn't load it by that name anyway) and the scan goes on. Names that convert give the same list as before. The scan itself moved to world/SleeperPack.cpp in the core library as pack_models(root), which pack_models() calls once with models/, so the unit tests can run it on a folder of their own. * fix(launcher): a vehicle folder named outside the code page is skipped by the skin scan scan_textures() keeps the folder of each textures.txt as a wide path and get_vehicle() turns it into a narrow name with no try. On Windows a folder under dynamic/ named with a character the code page lacks threw the same std::system_error out of the editor's vehicle tool, the launcher and the vehicle picker. Such a folder is now left out of the scan, as the model scan does with such a file.
280 lines
7.3 KiB
C++
280 lines
7.3 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;
|
|
|
|
auto const folder{std::filesystem::relative(f->path().parent_path(), "dynamic/", error)};
|
|
try {
|
|
// the vehicles are kept by narrow names. on Windows a folder named with a character the code page lacks
|
|
// can't be given one and would throw at the first vehicle; the game couldn't load it by that name anyway
|
|
[[maybe_unused]] auto const name{folder.generic_string()};
|
|
}
|
|
catch (std::system_error const &) {
|
|
continue;
|
|
}
|
|
ctx_path = folder;
|
|
|
|
std::fstream stream(f->path(), std::ios_base::binary | std::ios_base::in);
|
|
|
|
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 ¶m)
|
|
{
|
|
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 ¶m)
|
|
{
|
|
get_vehicle(target)->controllable =
|
|
param.size() >= 1 && param[0] == '1';
|
|
}
|
|
|
|
void ui::vehicles_bank::parse_texture_info(const std::string &target, const std::string ¶m, 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 ¶m)
|
|
{
|
|
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 ¶m)
|
|
{
|
|
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;
|
|
}
|
|
}
|