mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 15:17:08 +01:00
d6bace0fda
A fully binary successor of E3D, laid out for the GL 4.6 and Vulkan 1.4 renderers. Stage 1 of the plan: the format
and its tools. The game doesn't load .e4d yet; that is stage 2, behind a switch in eu07.ini.
- docs/e4d-format.md: the spec (in Polish, like world-format-eu07.md).
- model/e4d/e4d_format.h: the records and FourCCs, with static_asserts on sizes and offsets.
- A table of sections with required/optional flags and zstd per section; tables carry count and stride, so a later
version appends fields and an older reader takes the part it knows.
- Nodes in depth-first order with TRS (XFRM for sheared matrices), meshes, 48-byte primitives.
- Split vertex streams: VPOS f32x3, VATT QTangent snorm16x4 + UV f32x2 (28 B a vertex instead of 48 B), VSKN/VSKW
skin weights, VUSR user data, VBND for primitives merged from several nodes (rigid joints, light slots).
- Indices u16 or u32 per primitive, local to its base vertex.
- Typed components on the model, nodes, meshes and vertex groups (PROP, SOCK, JOIN, SPRG, CHAN, VARI, CHNL, LSLT,
LGHT, EMIT, MREF, COLL, LANM, LTXT, STAR), skins, vertex groups, a name index and bounds.
- model/e4d/e4d_io: write and read; the file is untrusted, so every offset, size, index, parent and reference is
checked (validate) before anything is used, and a broken file is refused with a reason.
- model/e4d/e4d_build: QTangent packing, TRS from matrices, the name index and the bounds.
- model/e4d/e4d_convert: a model loaded from E3D or T3D into E4D, and the command line:
eu07 -e4d convert <in.e3d|in.t3d> <out.e4d> [dynamic], eu07 -e4d dump <file>, eu07 -e4d validate <file>.
- zstd moves to eu07_core, which the unit tests link.
- tests/unit/test_e4d.cpp: round trip, section alignment, records of a later version, broken files, random mutations,
QTangent, TRS, name lookup, bounds, merged primitives.
Checked: 250 random .e3d of the installed game convert (E4D 21% of their size with zstd, 60% without), and the
vehicle families en57_v2, t448p_v2 and vectron_v1 (701 files). Unit tests 1188/1188. Not built with GCC here.
248 lines
7.9 KiB
C++
248 lines
7.9 KiB
C++
/*
|
|
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/.
|
|
*/
|
|
/*
|
|
MaSzyna EU07 locomotive simulator
|
|
Copyright (C) 2001-2004 Marcin Wozniak, Maciej Czapkiewicz and others
|
|
*/
|
|
/*
|
|
Authors:
|
|
MarcinW, McZapkie, Shaxbee, ABu, nbmx, youBy, Ra, winger, mamut, Q424,
|
|
Stele, firleju, szociu, hunter, ZiomalCl, OLI_EU and others
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "application/application.h"
|
|
#include "simulation/simulationtest.h"
|
|
#include "utilities/Globals.h"
|
|
#include "utilities/Logs.h"
|
|
#include "utilities/crashdump.h"
|
|
#include "utilities/crashsignals.h"
|
|
#include "utilities/physicslog.h"
|
|
#include "version_info.h"
|
|
#include <cstdlib>
|
|
#include <exception>
|
|
#include <new>
|
|
#include <stdexcept>
|
|
#include <string_view>
|
|
#include <thread>
|
|
#include <vector>
|
|
#ifdef _WIN32
|
|
#include <cstdio>
|
|
#else
|
|
#include <sys/resource.h>
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
|
#ifdef EU07_CPU_PREFLIGHT
|
|
// checks the processor before the C runtime starts, see utilities/cpu_preflight.cpp
|
|
#pragma comment(linker, "/subsystem:windows /ENTRY:eu07_preflight_entry")
|
|
#else
|
|
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
|
|
#endif
|
|
#endif
|
|
|
|
void export_e3d_standalone(std::string in, std::string out, int flags, bool dynamic);
|
|
int export_btf_standalone(std::string in, std::string out);
|
|
namespace e4d {
|
|
int run_command_line(int Argc, char *Argv[]);
|
|
}
|
|
int generate_nodebank_previews(int Argc, char *Argv[]);
|
|
|
|
#include <string>
|
|
#include <utilities/Globals.h>
|
|
|
|
namespace
|
|
{
|
|
|
|
// the log is written by a thread of its own, which the closing of the application stops and waits for. on the ways
|
|
// out that skip the closing (a failed start, an exception) what was logged last would be lost with the process
|
|
void finish_log()
|
|
{
|
|
StopLogService();
|
|
if (auto const logservice{Global.threads.find("LogService")};
|
|
logservice != Global.threads.end() && logservice->second.joinable() && logservice->second.get_id() != std::this_thread::get_id())
|
|
logservice->second.join();
|
|
}
|
|
|
|
// ends a test run with its verdict on the standard output and in the exit code. a test the simulator didn't end
|
|
// (a failed start, an exception) ends with the error and Reason
|
|
[[noreturn]] void end_test_run(std::string_view const Reason)
|
|
{
|
|
simulation::test().conclude(simulation::test_result::error, Reason);
|
|
// the last two seconds of the physics logs, which the runner of the tests reads
|
|
PhysicsLogs.stop();
|
|
finish_log();
|
|
fflush(stdout);
|
|
fflush(stderr);
|
|
// skip destructors, there are ordering errors which causes segfaults
|
|
std::_Exit(simulation::test().exit_code());
|
|
}
|
|
|
|
// a test run (--test): whatever stops it, it ends with its verdict. any other run never gets here and ends as
|
|
// it always has
|
|
[[noreturn]] void run_test(int argc, char *argv[])
|
|
{
|
|
// for what doesn't reach the catch below: an exception leaving a noexcept function, or on Linux one leaving
|
|
// a thread (on Windows the handler is the main thread's only)
|
|
std::set_terminate([] { end_test_run(simulation::describe_exception(std::current_exception())); });
|
|
try
|
|
{
|
|
if (Application.init(argc, argv) == 0)
|
|
{
|
|
Application.run();
|
|
Application.exit();
|
|
}
|
|
}
|
|
catch (...)
|
|
{
|
|
end_test_run(simulation::describe_exception(std::current_exception()));
|
|
}
|
|
end_test_run("the simulator stopped before the test ended");
|
|
}
|
|
|
|
// the physics logs used to keep a file open for every vehicle with a driver, and on a scenery with some five hundred
|
|
// of them they took all the files the C library of Windows lets a process have open by default (512): the files
|
|
// opened after them, the shaders of the map among them, couldn't be read. they keep none open now (physicslog.h),
|
|
// but the limit is raised to the most either system allows still, for whatever else opens many files at once
|
|
void raise_open_files_limit()
|
|
{
|
|
#ifdef _WIN32
|
|
_setmaxstdio(8192);
|
|
#else
|
|
rlimit limit{};
|
|
if (getrlimit(RLIMIT_NOFILE, &limit) == 0 && limit.rlim_cur < limit.rlim_max)
|
|
{
|
|
limit.rlim_cur = limit.rlim_max;
|
|
setrlimit(RLIMIT_NOFILE, &limit);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
raise_open_files_limit();
|
|
crashdump_install();
|
|
// on Linux, where no dump is written, the stack of a crash in the log and on the standard error
|
|
crash_signals::install(VERSION_INFO);
|
|
// init start timestamp
|
|
Global.startTimestamp = std::chrono::steady_clock::now();
|
|
|
|
// a start that failed ends with an error code, so whoever started the simulator (the starter) can tell it
|
|
// from a closing; how the simulator ends after a start that worked stays as it was
|
|
int exitcode{0};
|
|
|
|
// quick short-circuit for standalone e3d export
|
|
if (argc == 6 && std::string(argv[1]) == "-e3d")
|
|
{
|
|
std::string in(argv[2]);
|
|
std::string out(argv[3]);
|
|
int flags = std::stoi(std::string(argv[4]));
|
|
int dynamic = std::stoi(std::string(argv[5]));
|
|
export_e3d_standalone(in, out, flags, dynamic);
|
|
}
|
|
// standalone conversion, listing and checking of E4D models: -e4d convert|dump|validate ...
|
|
else if (argc >= 2 && std::string(argv[1]) == "-e4d")
|
|
{
|
|
auto const result{e4d::run_command_line(argc, argv)};
|
|
fflush(stdout);
|
|
fflush(stderr);
|
|
std::_Exit(result);
|
|
}
|
|
// standalone generation of preview images of the node bank models: -generate-nodebank-previews [options]
|
|
else if (argc >= 2 && std::string(argv[1]) == "-generate-nodebank-previews")
|
|
{
|
|
auto const result{generate_nodebank_previews(argc, argv)};
|
|
fflush(stdout);
|
|
fflush(stderr);
|
|
std::_Exit(result);
|
|
}
|
|
// standalone conversion of text terrain file to binary format: -btf -s <text file> [-o <binary file>]
|
|
else if (argc >= 4 && std::string(argv[1]) == "-btf")
|
|
{
|
|
std::string in;
|
|
std::string out;
|
|
for (int i = 2; i + 1 < argc; i += 2)
|
|
{
|
|
std::string const option(argv[i]);
|
|
if (option == "-s")
|
|
{
|
|
in = argv[i + 1];
|
|
}
|
|
else if (option == "-o")
|
|
{
|
|
out = argv[i + 1];
|
|
}
|
|
}
|
|
auto const result{export_btf_standalone(in, out)};
|
|
fflush(stdout);
|
|
fflush(stderr);
|
|
std::_Exit(result);
|
|
}
|
|
// unattended test run of a scenery, which says its result in the exit code (eu07/maszyna#541)
|
|
else if (simulation::parse_test_command_line(std::vector<char const *>(argv, argv + argc)).enabled)
|
|
{
|
|
run_test(argc, argv);
|
|
}
|
|
else
|
|
{
|
|
// below 0 when the start failed (see eu07_application::init())
|
|
int initresult{0};
|
|
try
|
|
{
|
|
initresult = Application.init(argc, argv);
|
|
if (initresult == 0)
|
|
{
|
|
Application.run();
|
|
Application.exit();
|
|
}
|
|
}
|
|
// an exception that ends the simulator is a crash, whether in the start or later: it says what it was in the
|
|
// log and ends with an error code
|
|
catch (std::bad_alloc const &Error)
|
|
{
|
|
ErrorLog("Critical error, memory allocation failure: " + std::string(Error.what()));
|
|
exitcode = 1;
|
|
}
|
|
#ifdef _WIN32
|
|
catch (std::runtime_error const &Error)
|
|
{
|
|
ErrorLog("Critical error: " + std::string(Error.what()));
|
|
std::string msg = "Simulator crash occured :(\n";
|
|
msg += Error.what();
|
|
MessageBoxA(nullptr, msg.c_str(), "Simulator crashed :(", MB_ICONERROR);
|
|
exitcode = 1;
|
|
}
|
|
#else
|
|
// elsewhere one not caught here ends the simulator through std::terminate, with no word in the log. on
|
|
// Windows it is left to the crash report (utilities/crashdump.cpp), whose dump shows where it was thrown
|
|
catch (std::exception const &Error)
|
|
{
|
|
ErrorLog("Critical error: " + std::string(Error.what()));
|
|
exitcode = 1;
|
|
}
|
|
#endif
|
|
// the last two seconds of the physics logs, also after an error caught above; Application.exit() stops them
|
|
// otherwise, and a second stop only writes what came since
|
|
PhysicsLogs.stop();
|
|
// after a failed start or an error caught above the log thread still runs, and what it hasn't written yet
|
|
// (why the start failed) would go with the process. after Application.exit() it has stopped already
|
|
finish_log();
|
|
if (initresult < 0)
|
|
exitcode = 1;
|
|
}
|
|
#ifndef _WIN32
|
|
fflush(stdout);
|
|
fflush(stderr);
|
|
#endif
|
|
std::_Exit(exitcode); // skip destructors, there are ordering errors which causes segfaults
|
|
}
|