16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-23 17:59:19 +02:00

refactor: create paths namespace instead of global variables

This commit is contained in:
jerrrrycho
2026-03-15 11:55:30 +01:00
parent 383788b30a
commit 9f43dca48b
20 changed files with 112 additions and 100 deletions

View File

@@ -97,17 +97,17 @@ void itemproperties_panel::update(scene::basic_node const *Node)
// 3d shape
auto modelfile{((subnode->pModel != nullptr) ? subnode->pModel->NameGet() : "(none)")};
if (modelfile.find(szModelPath) == 0)
if (modelfile.find(paths::models) == 0)
{
// don't include 'models/' in the path
modelfile.erase(0, std::string{szModelPath}.size());
modelfile.erase(0, std::string{paths::models}.size());
}
// texture
auto texturefile{((subnode->Material()->replacable_skins[1] != null_handle) ? GfxRenderer->Material(subnode->Material()->replacable_skins[1])->GetName() : "(none)")};
if (texturefile.find(szTexturePath) == 0)
if (texturefile.find(paths::textures) == 0)
{
// don't include 'textures/' in the path
texturefile.erase(0, std::string{szTexturePath}.size());
texturefile.erase(0, std::string{paths::textures}.size());
}
text_lines.emplace_back("mesh: " + modelfile, Global.UITextColor);
text_lines.emplace_back("skin: " + texturefile, Global.UITextColor);
@@ -131,14 +131,14 @@ void itemproperties_panel::update(scene::basic_node const *Node)
text_lines.emplace_back(textline, Global.UITextColor);
// textures
auto texturefile{((subnode->m_material1 != null_handle) ? GfxRenderer->Material(subnode->m_material1)->GetName() : "(none)")};
if (texturefile.find(szTexturePath) == 0)
if (texturefile.find(paths::textures) == 0)
{
texturefile.erase(0, std::string{szTexturePath}.size());
texturefile.erase(0, std::string{paths::textures}.size());
}
auto texturefile2{((subnode->m_material2 != null_handle) ? GfxRenderer->Material(subnode->m_material2)->GetName() : "(none)")};
if (texturefile2.find(szTexturePath) == 0)
if (texturefile2.find(paths::textures) == 0)
{
texturefile2.erase(0, std::string{szTexturePath}.size());
texturefile2.erase(0, std::string{paths::textures}.size());
}
textline = "skins:\n " + texturefile + "\n " + texturefile2;
text_lines.emplace_back(textline, Global.UITextColor);

View File

@@ -135,11 +135,11 @@ buffer_manager::create( std::string const &Filename ) {
}
}
// if dynamic-specific and/or direct lookups find nothing, try the default sound folder
lookup = find_buffer( szSoundPath + filename );
lookup = find_buffer( paths::sounds + filename );
if( lookup != null_handle ) {
return lookup;
}
filelookup = find_file( szSoundPath + filename );
filelookup = find_file( paths::sounds + filename );
if( false == filelookup.empty() ) {
return emplace( filelookup );
}

View File

@@ -319,9 +319,9 @@ sound_source::export_as_text( std::ostream &Output ) const {
<< m_offset.z << ' ';
// sound data
auto soundfile { audio::renderer.buffer( sound( sound_id::main ).buffer ).name };
if( soundfile.find( szSoundPath ) == 0 ) {
if( soundfile.find( paths::sounds ) == 0 ) {
// don't include 'sounds/' in the path
soundfile.erase( 0, std::string{ szSoundPath }.size() );
soundfile.erase( 0, std::string{ paths::sounds }.size() );
}
Output
<< soundfile << ' ';

View File

@@ -314,7 +314,7 @@ size_t NvTextureManager::FetchTexture(std::string path, int format_hint,
{
const std::array<std::string, 3> filenames{
Global.asCurrentTexturePath + path, path, szTexturePath + path};
Global.asCurrentTexturePath + path, path, paths::textures + path};
for (const auto &filename : filenames) {
if (auto found = m_texture_map.find(filename);
found != m_texture_map.end())

View File

@@ -675,9 +675,9 @@ TAnimModel::export_as_text_( std::ostream &Output ) const {
pModel ?
pModel->NameGet() + ".t3d" : // rainsted requires model file names to include an extension
"none" ) };
if( modelfile.find( szModelPath ) == 0 ) {
if( modelfile.find( paths::models ) == 0 ) {
// don't include 'models/' in the path
modelfile.erase( 0, std::string{ szModelPath }.size() );
modelfile.erase( 0, std::string{ paths::models }.size() );
}
Output << modelfile << ' ';
// texture
@@ -685,9 +685,9 @@ TAnimModel::export_as_text_( std::ostream &Output ) const {
m_materialdata.replacable_skins[ 1 ] != null_handle ?
GfxRenderer->Material( m_materialdata.replacable_skins[ 1 ] )->GetName() :
"none" ) };
if( texturefile.find( szTexturePath ) == 0 ) {
if( texturefile.find( paths::textures ) == 0 ) {
// don't include 'textures/' in the path
texturefile.erase( 0, std::string{ szTexturePath }.size() );
texturefile.erase( 0, std::string{ paths::textures }.size() );
}
if( contains( texturefile, ' ' ) ) {
Output << "\"" << texturefile << "\"" << ' ';

View File

@@ -125,7 +125,7 @@ TModelsManager::find_in_databank( std::string const &Name ) {
std::vector<std::string> filenames {
Name,
szModelPath + Name };
paths::models + Name };
for( auto const &filename : filenames ) {
auto const lookup { m_modelsmap.find( filename ) };
@@ -146,7 +146,7 @@ TModelsManager::find_on_disk( std::string const &Name ) {
auto lookup = (
FileExists( Name + extension ) ? Name :
FileExists( szModelPath + Name + extension ) ? szModelPath + Name :
FileExists( paths::models + Name + extension ) ? paths::models + Name :
"" );
if( false == lookup.empty() ) {
return lookup;

View File

@@ -1467,7 +1467,7 @@ texture_manager::find_in_databank( std::string const &Texturename ) const {
std::vector<std::string> const filenames {
Global.asCurrentTexturePath + Texturename,
Texturename,
szTexturePath + Texturename };
paths::textures + Texturename };
for( auto const &filename : filenames ) {
auto const lookup { m_texturemappings.find( filename ) };
@@ -1486,7 +1486,7 @@ texture_manager::find_on_disk( std::string const &Texturename ) {
std::vector<std::string> const filenames {
Global.asCurrentTexturePath + Texturename,
Texturename,
szTexturePath + Texturename };
paths::textures + Texturename };
auto lookup =
FileExists(

View File

@@ -582,7 +582,7 @@ material_manager::find_in_databank( std::string const &Materialname ) const {
std::vector<std::string> const filenames {
Global.asCurrentTexturePath + Materialname,
Materialname,
szTexturePath + Materialname };
paths::textures + Materialname };
for( auto const &filename : filenames ) {
auto const lookup { m_materialmappings.find( filename ) };
@@ -602,7 +602,7 @@ material_manager::find_on_disk( std::string const &Materialname ) {
auto const materialname { ToLower( Materialname ) };
return (
FileExists(
{ Global.asCurrentTexturePath + materialname, materialname, szTexturePath + materialname },
{ Global.asCurrentTexturePath + materialname, materialname, paths::textures + materialname },
{ ".mat" } ) );
}

View File

@@ -15,7 +15,7 @@ http://mozilla.org/MPL/2.0/.
#include "model/material.h"
#include "model/vertex.h"
#include "rendering/geometrybank.h"
#include "utils/uuid.hpp"
#include "../utilities/uuid.hpp"
struct lighting_data {

View File

@@ -23,18 +23,25 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/Timer.h"
#include "vao.h"
void
global_settings::LoadIniFile(std::string asFileName) {
void global_settings::LoadIniFile(std::string asFileName)
{
// initialize season data in case the main config file doesn't
std::time_t timenow = std::time( 0 );
std::tm *localtime = std::localtime( &timenow );
fMoveLight = localtime->tm_yday + 1; // numer bieżącego dnia w roku
std::time_t timenow = std::time(nullptr);
std::tm tm{};
#ifdef _WIN32
localtime_s(&tm, &timenow);
#else
localtime_r(&timenow, &tm);
#endif
fMoveLight = tm.tm_yday + 1; // numer bieżącego dnia w roku
simulation::Environment.compute_season(fMoveLight);
cParser parser(asFileName, cParser::buffer_FILE);
ConfigParse(parser);
};
}
void
global_settings::ConfigParse(cParser &Parser) {

View File

@@ -65,7 +65,7 @@ struct global_settings {
int iPause{ 0 }; // globalna pauza ruchu: b0=start,b1=klawisz,b2=tło,b3=lagi,b4=wczytywanie
float AirTemperature{ 15.f };
std::string asCurrentSceneryPath{ "scenery/" };
std::string asCurrentTexturePath{ szTexturePath };
std::string asCurrentTexturePath{ paths::textures };
std::string asCurrentDynamicPath;
int CurrentMaxTextureSize{ 4096 };
bool UpdateMaterials{ true };

View File

@@ -158,55 +158,57 @@ void LogService()
}
void WriteLog(const char *str, logtype const Type, bool isError)
bool ShouldSkipLog(std::string_view str, logtype type)
{
if (!str || *str == '\0')
return;
if (TestFlag(Global.DisabledLogTypes, static_cast<unsigned int>(Type)))
return;
// time calculation
auto now = std::chrono::steady_clock::now();
auto elapsed = now - Global.startTimestamp;
double seconds = std::chrono::duration_cast<std::chrono::duration<double>>(elapsed).count();
// time format
std::ostringstream oss;
oss << "[ " << std::fixed << std::setprecision(3) << seconds << " ] ";
// wyrownanie do np. 10 znaków długości + dwie tabulacje
std::ostringstream final;
final << std::setw(10) << oss.str() << "\t\t" << str;
logMutex.lock();
InfoStack.emplace_back(final.str(), isError);
logMutex.unlock();
return str.empty() ||
TestFlag(Global.DisabledLogTypes, static_cast<unsigned int>(type));
}
void ErrorLog(const char *str, logtype const Type)
std::string FormatLogMessage(std::string_view str)
{
if (!str || *str == '\0')
return;
if (TestFlag(Global.DisabledLogTypes, static_cast<unsigned int>(Type)))
const auto now = std::chrono::steady_clock::now();
const auto elapsed = now - Global.startTimestamp;
const double seconds = std::chrono::duration<double>(elapsed).count();
return std::format("[ {:8.3f} ]\t\t{}", seconds, str);
}
void WriteLog(std::string_view str, logtype type, bool isError)
{
if (ShouldSkipLog(str, type))
return;
// time calculation
auto now = std::chrono::steady_clock::now();
auto elapsed = now - Global.startTimestamp;
double seconds = std::chrono::duration_cast<std::chrono::duration<double>>(elapsed).count();
const auto message = FormatLogMessage(str);
// time format
std::ostringstream oss;
oss << "[ " << std::fixed << std::setprecision(3) << seconds << " ] ";
std::lock_guard<std::mutex> lock(logMutex);
InfoStack.push_back({message, isError});
}
// wyrownanie do np. 10 znaków długości + dwie tabulacje
std::ostringstream final;
final << std::setw(10) << oss.str() << "\t\t" << str;
void ErrorLog(std::string_view str, logtype type)
{
if (ShouldSkipLog(str, type))
return;
logMutex.lock();
ErrorStack.emplace_back(final.str());
logMutex.unlock();
const auto message = FormatLogMessage(str);
std::lock_guard<std::mutex> lock(logMutex);
ErrorStack.push_back(message);
}
void WriteLog(const char* str, logtype type, bool isError)
{
if (str == nullptr || *str == '\0')
return;
WriteLog(std::string_view{str}, type, isError);
}
void ErrorLog(const char* str, logtype type)
{
if (str == nullptr || *str == '\0')
return;
ErrorLog(std::string_view{str}, type);
}

View File

@@ -29,12 +29,15 @@ template <typename T> T sign(T x)
#define DegToRad(a) ((M_PI / 180.0) * (a)) //(a) w nawiasie, bo może być dodawaniem
#define RadToDeg(r) ((180.0 / M_PI) * (r))
#define szSceneryPath "scenery/"
#define szTexturePath "textures/"
#define szModelPath "models/"
#define szDynamicPath "dynamic/"
#define szSoundPath "sounds/"
#define szDataPath "data/"
namespace paths
{
inline constexpr const char *scenery = "scenery/";
inline constexpr const char *textures = "textures/";
inline constexpr const char *models = "models/";
inline constexpr const char *dynamic = "dynamic/";
inline constexpr const char *sounds = "sounds/";
inline constexpr const char *data = "data/";
}
#define MAKE_ID4(a, b, c, d) (((std::uint32_t)(d) << 24) | ((std::uint32_t)(c) << 16) | ((std::uint32_t)(b) << 8) | (std::uint32_t)(a))

View File

@@ -4541,7 +4541,7 @@ bool TController::PutCommand( std::string NewCommand, double NewValue1, double N
// NewCommand = Global.asCurrentSceneryPath + NewCommand;
auto lookup =
FileExists(
{ Global.asCurrentSceneryPath + NewCommand, szSoundPath + NewCommand },
{ Global.asCurrentSceneryPath + NewCommand, paths::sounds + NewCommand },
{ ".ogg", ".flac", ".wav" } );
if( false == lookup.first.empty() ) {
// wczytanie dźwięku odjazdu podawanego bezpośrenido
@@ -4552,7 +4552,7 @@ bool TController::PutCommand( std::string NewCommand, double NewValue1, double N
NewCommand += "radio";
lookup =
FileExists(
{ Global.asCurrentSceneryPath + NewCommand, szSoundPath + NewCommand },
{ Global.asCurrentSceneryPath + NewCommand, paths::sounds + NewCommand },
{ ".ogg", ".flac", ".wav" } );
if( false == lookup.first.empty() ) {
// wczytanie dźwięku odjazdu w wersji radiowej (słychać tylko w kabinie)

View File

@@ -63,7 +63,7 @@ TextureTest( std::string const &Name ) {
auto const lookup {
FileExists(
{ Global.asCurrentTexturePath + Name, Name, szTexturePath + Name },
{ Global.asCurrentTexturePath + Name, Name, paths::textures + Name },
{ ".mat", ".dds", ".tga", ".ktx", ".png", ".bmp", ".jpg", ".tex" } ) };
return ( lookup.first + lookup.second );
@@ -1982,7 +1982,7 @@ TDynamicObject::Init(std::string Name, // nazwa pojazdu, np. "EU07-424"
)
{ // Ustawienie początkowe pojazdu
iDirection = (Reversed ? 0 : 1); // Ra: 0, jeśli ma być wstawiony jako obrócony tyłem
asBaseDir = szDynamicPath + BaseDir + "/"; // McZapkie-310302
asBaseDir = paths::dynamic + BaseDir + "/"; // McZapkie-310302
asName = Name;
std::string asAnimName; // zmienna robocza do wyszukiwania osi i wózków
// Ra: zmieniamy znaczenie obsady na jednoliterowe, żeby dosadzić kierownika
@@ -3011,7 +3011,7 @@ void TDynamicObject::LoadUpdate() {
// update bindings between lowpoly sections and potential load chunks placed inside them
update_load_sections();
// z powrotem defaultowa sciezka do tekstur
Global.asCurrentTexturePath = std::string( szTexturePath );
Global.asCurrentTexturePath = std::string( paths::textures );
}
}
@@ -5337,7 +5337,7 @@ void TDynamicObject::LoadMMediaFile( std::string const &TypeName, std::string co
if (ReplacableSkin != "none") {
m_materialdata.assign( ReplacableSkin );
}
Global.asCurrentTexturePath = szTexturePath; // z powrotem defaultowa sciezka do tekstur
Global.asCurrentTexturePath = paths::textures; // z powrotem defaultowa sciezka do tekstur
do {
token = "";
parser.getTokens(); parser >> token;
@@ -6011,7 +6011,7 @@ void TDynamicObject::LoadMMediaFile( std::string const &TypeName, std::string co
mdLoad = LoadMMediaFile_mdload( MoverParameters->LoadType.name );
// z powrotem defaultowa sciezka do tekstur
Global.asCurrentTexturePath = std::string( szTexturePath );
Global.asCurrentTexturePath = std::string( paths::textures );
}
} // models
@@ -7148,7 +7148,7 @@ void TDynamicObject::LoadMMediaFile( std::string const &TypeName, std::string co
attachment->Init();
}
Global.asCurrentTexturePath = szTexturePath; // kiedyś uproszczone wnętrze mieszało tekstury nieba
Global.asCurrentTexturePath = paths::textures; // kiedyś uproszczone wnętrze mieszało tekstury nieba
Global.asCurrentDynamicPath = "";
// position sound emitters which weren't defined in the config file

View File

@@ -9311,7 +9311,7 @@ bool TTrain::InitializeCab(int NewCabNo, std::string const &asFileName)
TModel3d *kabina = TModelsManager::GetModel(DynamicObject->asBaseDir + token, true, true,
(Global.network_servers.empty() && !Global.network_client) ? 0 : id());
// z powrotem defaultowa sciezka do tekstur
Global.asCurrentTexturePath = szTexturePath;
Global.asCurrentTexturePath = paths::textures;
// if (DynamicObject->mdKabina!=k)
if (kabina != nullptr)
{

View File

@@ -1654,7 +1654,7 @@ animation_event::deserialize_( cParser &Input, scene::scratch_data &Scratchpad )
{ // animacja z pliku VMD
{
m_animationfilename = token;
std::ifstream file( szModelPath + m_animationfilename, std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws );
std::ifstream file( paths::models + m_animationfilename, std::ios::binary | std::ios::ate ); file.unsetf( std::ios::skipws );
auto size = file.tellg(); // ios::ate already positioned us at the end of the file
file.seekg( 0, std::ios::beg ); // rewind the caret afterwards
// animation size, previously held in param 7

View File

@@ -2266,9 +2266,9 @@ TTrack::export_as_text_( std::ostream &Output ) const {
m_material1 != null_handle ?
GfxRenderer->Material( m_material1 )->GetName() :
"none" ) };
if( texturefile.find( szTexturePath ) == 0 ) {
if( texturefile.find( paths::textures ) == 0 ) {
// don't include 'textures/' in the path
texturefile.erase( 0, std::string{ szTexturePath }.size() );
texturefile.erase( 0, std::string{ paths::textures }.size() );
}
Output
<< texturefile << ' '
@@ -2278,9 +2278,9 @@ TTrack::export_as_text_( std::ostream &Output ) const {
m_material2 != null_handle ?
GfxRenderer->Material( m_material2 )->GetName() :
"none" );
if( texturefile.find( szTexturePath ) == 0 ) {
if( texturefile.find( paths::textures ) == 0 ) {
// don't include 'textures/' in the path
texturefile.erase( 0, std::string{ szTexturePath }.size() );
texturefile.erase( 0, std::string{ paths::textures }.size() );
}
Output << texturefile << ' ';
@@ -2351,9 +2351,9 @@ TTrack::export_as_text_( std::ostream &Output ) const {
if( ( eType == tt_Switch )
&& ( SwitchExtension->m_material3 != null_handle ) ) {
auto texturefile { GfxRenderer->Material( m_material2 )->GetName() };
if( texturefile.find( szTexturePath ) == 0 ) {
if( texturefile.find( paths::textures ) == 0 ) {
// don't include 'textures/' in the path
texturefile.erase( 0, std::string{ szTexturePath }.size() );
texturefile.erase( 0, std::string{ paths::textures }.size() );
}
Output << "trackbed " << texturefile << ' ';
}
@@ -2384,7 +2384,7 @@ std::string TTrack::tooltip() const
std::pair<std::string, int>
TTrack::fetch_track_rail_profile( std::string const &Profile ) {
auto const railprofilepath { std::string( szModelPath ) + "tory/railprofile_" };
auto const railprofilepath { std::string( paths::models ) + "tory/railprofile_" };
auto const railkeyprefix { std::string( "rail_" ) };
if( m_profiles.empty() ) {
@@ -2414,7 +2414,7 @@ TTrack::fetch_default_profiles() {
if( false == m_profiles.empty() ) { return; }
auto const railprofilepath { std::string( szModelPath ) + "tory/railprofile_" };
auto const railprofilepath { std::string( paths::models ) + "tory/railprofile_" };
auto const railkeyprefix { std::string( "rail_" ) };
m_profiles.emplace_back( deserialize_profile( railprofilepath + "default" ) );

View File

@@ -617,7 +617,7 @@ TTrainParameters::load_sounds() {
auto const lookup {
FileExists(
{ Global.asCurrentSceneryPath + stationname, std::string{ szSoundPath } + "sip/" + stationname },
{ Global.asCurrentSceneryPath + stationname, std::string{ paths::sounds } + "sip/" + stationname },
{ ".ogg", ".flac", ".wav" } ) };
if( lookup.first.empty() ) {
continue;