mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-17 13:19:20 +02:00
A baked heightfield was trusted for as long as its file was readable, and the logic container while the .scn was older than it: an edited include went unnoticed by both, and node keys leaned on file dates, so merely touching a file changed them. And cooking only ever happened as a side effect of starting a scenery. Every cooked artifact now has a manifest beside it - <scenario>.ehf.deps, <scenario>.logic.deps - listing each file it was made from with its size, modification time and a hash of its content (simulation/cookdeps). The parser records what it includes as it reads, so the list is exactly what the load read. An artifact is current when every listed file still has its content: size and time are the quick check, and when either differs the content is hashed, so a touched or copied file does not force a rebuild and an edited one always does. A file added to the scenery cannot be in a manifest; the files read are compared with it after the load, and a mismatch sends the terrain to be baked on the next start - the new file's nodes, whose keys are unknown, are drawn as geometry meanwhile - and the logic, cooked from the loaded state, is cooked at once. The terrain depends on what the scenario includes and not on the scenario file itself, which the launcher rewrites on every start; the logic depends on both, since events can be written in either, and costs milliseconds to cook again. With the manifest vouching for file content, a triangle node's key is its file, line, include parameters and placement, without the date. -cook loads a scenery, brings whatever is stale up to date, and quits: the step that prepares a scenery ahead of play. It closes the window directly, as there is no simulation to wind down; a quit command waited a minute in the queue for one. Baking on a first start stays as the fallback, and -nobaketerrain still turns it off. Tried on a copy of baltyk with a terrain include of its own: a fresh cook bakes and cooks everything, a second finds nothing to do, a touched file changes nothing, an edited include is caught by its hash and both artifacts are rebuilt, and a new include has the logic recooked at once and the terrain on the next run. A cooking run of baltyk takes 5.5 s with nothing stale and 10.7 s with everything to do.
150 lines
4.7 KiB
C++
150 lines
4.7 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/.
|
|
*/
|
|
|
|
module;
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <ctime>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <system_error>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
module eu07.simulation.cookdeps;
|
|
import eu07.utilities.logs;
|
|
import eu07.utilities.utilities;
|
|
|
|
namespace simulation::cookdeps {
|
|
|
|
namespace {
|
|
|
|
constexpr char const *magic { "eu07-cookdeps 1" };
|
|
|
|
struct entry {
|
|
std::uintmax_t size { 0 };
|
|
std::int64_t time { 0 };
|
|
std::uint64_t hash { 0 };
|
|
std::string path;
|
|
};
|
|
|
|
// fnv-1a over the file's bytes. stable across runs and builds, which is what a manifest needs;
|
|
// returns false when the file cannot be read
|
|
bool
|
|
hash_of( std::string const &Path, std::uintmax_t &Size, std::uint64_t &Hash ) {
|
|
|
|
std::ifstream input( Path, std::ios::binary );
|
|
if( false == input.good() ) { return false; }
|
|
Hash = 0xcbf29ce484222325ull;
|
|
Size = 0;
|
|
std::array<char, 1 << 16> buffer;
|
|
while( input ) {
|
|
input.read( buffer.data(), static_cast<std::streamsize>( buffer.size() ) );
|
|
auto const count { static_cast<std::size_t>( input.gcount() ) };
|
|
for( std::size_t index = 0; index < count; ++index ) {
|
|
Hash ^= static_cast<unsigned char>( buffer[ index ] );
|
|
Hash *= 0x100000001b3ull;
|
|
}
|
|
Size += count;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::vector<std::string>
|
|
unique( std::vector<std::string> Files ) {
|
|
|
|
std::sort( Files.begin(), Files.end() );
|
|
Files.erase( std::unique( Files.begin(), Files.end() ), Files.end() );
|
|
return Files;
|
|
}
|
|
|
|
bool
|
|
read( std::string const &Path, std::vector<entry> &Entries ) {
|
|
|
|
std::ifstream input( Path );
|
|
std::string line;
|
|
if( ( false == static_cast<bool>( std::getline( input, line ) ) ) || ( line != magic ) ) { return false; }
|
|
while( std::getline( input, line ) ) {
|
|
std::istringstream fields( line );
|
|
entry item;
|
|
if( false == static_cast<bool>( fields >> item.size >> item.time >> std::hex >> item.hash >> std::dec ) ) { return false; }
|
|
fields.get(); // the space before the path, which may itself hold spaces
|
|
std::getline( fields, item.path );
|
|
Entries.push_back( item );
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
bool
|
|
write( std::string const &Path, std::vector<std::string> const &Files ) {
|
|
|
|
std::ostringstream text;
|
|
text << magic << '\n';
|
|
for( auto const &file : unique( Files ) ) {
|
|
entry item;
|
|
item.path = file;
|
|
if( false == hash_of( file, item.size, item.hash ) ) {
|
|
// a file that was read a moment ago and cannot be now; the artifact is recorded as
|
|
// depending on its absence, which any later read will contradict
|
|
item.size = 0;
|
|
item.hash = 0;
|
|
}
|
|
item.time = static_cast<std::int64_t>( last_modified( file ) );
|
|
text << item.size << ' ' << item.time << ' ' << std::hex << item.hash << std::dec << ' ' << item.path << '\n';
|
|
}
|
|
|
|
std::ofstream output( Path, std::ios::trunc );
|
|
output << text.str();
|
|
if( false == output.good() ) {
|
|
ErrorLog( "Cooked data: cannot write \"" + Path + "\"" );
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
current( std::string const &Path ) {
|
|
|
|
std::vector<entry> entries;
|
|
if( false == read( Path, entries ) ) { return false; }
|
|
for( auto const &item : entries ) {
|
|
auto const time { static_cast<std::int64_t>( last_modified( item.path ) ) };
|
|
std::error_code error;
|
|
auto const onsize { std::filesystem::file_size( item.path, error ) };
|
|
if( ( false == static_cast<bool>( error ) ) && ( time != 0 ) && ( time == item.time ) && ( onsize == item.size ) ) {
|
|
// same size and modification time: taken as unchanged without reading it
|
|
continue;
|
|
}
|
|
std::uintmax_t size { 0 };
|
|
std::uint64_t hash { 0 };
|
|
if( ( false == hash_of( item.path, size, hash ) ) || ( size != item.size ) || ( hash != item.hash ) ) {
|
|
WriteLog( "Cooked data: \"" + item.path + "\" has changed since \"" + Path + "\" was written" );
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
lists( std::string const &Path, std::vector<std::string> const &Files ) {
|
|
|
|
std::vector<entry> entries;
|
|
if( false == read( Path, entries ) ) { return false; }
|
|
std::vector<std::string> listed;
|
|
listed.reserve( entries.size() );
|
|
for( auto const &item : entries ) { listed.push_back( item.path ); }
|
|
return unique( listed ) == unique( Files );
|
|
}
|
|
|
|
} // namespace simulation::cookdeps
|