mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-12-03 18:27:59 -05:00
Let ORCA_SOURCE_DATE_EPOCH Fix the Clock and random() Seed of Custom G-code
Custom G-code that prints the time or calls random() gives different
output on every export, so two slices of the same input cannot be
compared. With ORCA_SOURCE_DATE_EPOCH set to a number of seconds since
the Unix epoch, the time variables hold that time in UTC and random()
starts from the same seed. Without it nothing changes.
The standard SOURCE_DATE_EPOCH is not used: packaging shells export it
for every program they start, and it would freeze {timestamp} in the
output file names of anyone launching the slicer from one.
This commit is contained in:
1 parent
432b205d91
commit
fd94324edb
7 files changed
+129
-4
No files matched your search
@@ -494,6 +494,8 @@ set(lisbslic3r_sources
|
||||
SlicingAdaptive.hpp
|
||||
Slicing.cpp
|
||||
Slicing.hpp
|
||||
SourceDateEpoch.cpp
|
||||
SourceDateEpoch.hpp
|
||||
Support/BeltFloorContext.cpp
|
||||
Support/BeltFloorContext.hpp
|
||||
Support/SupportCommon.cpp
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
#include "GCode/WipeTower.hpp"
|
||||
#include "GCode/WipeTower2.hpp"
|
||||
#include "ShortestPath.hpp"
|
||||
#include "SourceDateEpoch.hpp"
|
||||
#include "GCode/OrderingStrategies.hpp"
|
||||
#include "Print.hpp"
|
||||
#include "Utils.hpp"
|
||||
@@ -68,6 +69,7 @@
|
||||
#include <cstdarg>
|
||||
#include <cstdlib>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
@@ -82,6 +84,7 @@
|
||||
#include <stdexcept>
|
||||
#include <set>
|
||||
#include <optional>
|
||||
#include <random>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
@@ -3591,7 +3594,11 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
|
||||
m_placeholder_parser_integration.parser = print.placeholder_parser();
|
||||
m_placeholder_parser_integration.parser.update_timestamp();
|
||||
m_placeholder_parser_integration.parser.update_user_name();
|
||||
m_placeholder_parser_integration.context.rng = std::mt19937(std::chrono::high_resolution_clock::now().time_since_epoch().count());
|
||||
// ORCA_SOURCE_DATE_EPOCH also fixes the numbers random() draws, so that an export can be repeated.
|
||||
if (const std::optional<std::time_t> fixed_time = source_date_epoch())
|
||||
m_placeholder_parser_integration.context.rng = std::mt19937(static_cast<std::mt19937::result_type>(*fixed_time));
|
||||
else
|
||||
m_placeholder_parser_integration.context.rng = std::mt19937(std::chrono::high_resolution_clock::now().time_since_epoch().count());
|
||||
// Enable passing global variables between PlaceholderParser invocations.
|
||||
m_placeholder_parser_integration.context.global_config = std::make_unique<DynamicConfig>();
|
||||
print.update_object_placeholders(m_placeholder_parser_integration.parser.config_writable(), ".gcode");
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "Flow.hpp"
|
||||
#include "PrintConfig.hpp"
|
||||
#include "Point.hpp"
|
||||
#include "SourceDateEpoch.hpp"
|
||||
#include "Utils.hpp"
|
||||
#include "I18N.hpp"
|
||||
#include "format.hpp"
|
||||
@@ -41,6 +42,7 @@
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include <optional>
|
||||
#include <random>
|
||||
#include <set>
|
||||
#include <memory>
|
||||
@@ -119,9 +121,12 @@ PlaceholderParser::PlaceholderParser(const DynamicConfig *external_config) : m_e
|
||||
|
||||
void PlaceholderParser::update_timestamp(DynamicConfig &config)
|
||||
{
|
||||
time_t rawtime;
|
||||
time(&rawtime);
|
||||
struct tm* timeinfo = localtime(&rawtime);
|
||||
// ORCA_SOURCE_DATE_EPOCH replaces the clock, read as UTC, so that an export can be repeated.
|
||||
const std::optional<std::time_t> fixed_time = source_date_epoch();
|
||||
time_t rawtime = fixed_time.value_or(0);
|
||||
if (!fixed_time)
|
||||
time(&rawtime);
|
||||
struct tm* timeinfo = fixed_time ? gmtime(&rawtime) : localtime(&rawtime);
|
||||
|
||||
{
|
||||
std::ostringstream ss;
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "SourceDateEpoch.hpp"
|
||||
|
||||
#include <charconv>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <optional>
|
||||
#include <system_error>
|
||||
|
||||
#include <boost/nowide/cstdlib.hpp>
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
std::optional<std::time_t> source_date_epoch()
|
||||
{
|
||||
// The last second every platform's gmtime() accepts: Windows stops at the end of the year 3000.
|
||||
// A later time would make gmtime() return null.
|
||||
constexpr std::int64_t last_second = 32535215999;
|
||||
|
||||
// Boost.Nowide's getenv, not std::getenv: on Windows it reads the process environment, so it also
|
||||
// sees a value set after start-up, which the C runtime's copy does not.
|
||||
const char *text = boost::nowide::getenv("ORCA_SOURCE_DATE_EPOCH");
|
||||
if (text == nullptr)
|
||||
return std::nullopt;
|
||||
const char *end = text + std::strlen(text);
|
||||
std::int64_t seconds = 0;
|
||||
const auto [parsed_to, error] = std::from_chars(text, end, seconds);
|
||||
if (error != std::errc() || parsed_to == text || parsed_to != end || seconds < 0 || seconds > last_second)
|
||||
return std::nullopt;
|
||||
return static_cast<std::time_t>(seconds);
|
||||
}
|
||||
|
||||
} // namespace Slic3r
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <ctime>
|
||||
#include <optional>
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
// The time to use in place of the current time: the ORCA_SOURCE_DATE_EPOCH environment variable,
|
||||
// a whole number of seconds since the Unix epoch. Unset, empty or anything else gives nullopt.
|
||||
//
|
||||
// With it set, custom G-code prints that time (in UTC) and random() draws the same numbers on every
|
||||
// export, so two slices of the same input give the same G-code and can be compared. It is not the
|
||||
// standard SOURCE_DATE_EPOCH, which packaging shells export for every program they start.
|
||||
std::optional<std::time_t> source_date_epoch();
|
||||
|
||||
} // namespace Slic3r
|
||||
@@ -90,6 +90,33 @@ TEST_CASE("Kobra 2 layer-change templates do not emit numeric commands", "[GCode
|
||||
});
|
||||
}
|
||||
|
||||
TEST_CASE("ORCA_SOURCE_DATE_EPOCH makes custom G-code print the same time and random numbers on every export", "[GCode]")
|
||||
{
|
||||
const ScopedEnvVar epoch("ORCA_SOURCE_DATE_EPOCH", "1000000000"); // 2001-09-09 01:46:40 UTC
|
||||
const auto config = Test::multifilament_config(1, {
|
||||
{"layer_height", "0.2"},
|
||||
{"first_layer_height", "0.2"},
|
||||
{"layer_change_gcode", "; clock [timestamp] roll {random(0, 1000000)}"},
|
||||
});
|
||||
const auto clock_lines = [&config]() {
|
||||
std::vector<std::string> lines;
|
||||
// A 2 mm cube at 0.2 mm layers changes layer several times, so random() is drawn several times.
|
||||
std::istringstream gcode(Test::slice({Test::cube(2.)}, config));
|
||||
for (std::string line; std::getline(gcode, line);)
|
||||
if (line.rfind("; clock ", 0) == 0)
|
||||
lines.push_back(line);
|
||||
return lines;
|
||||
};
|
||||
|
||||
const std::vector<std::string> first = clock_lines();
|
||||
REQUIRE(first.size() > 1);
|
||||
for (const std::string &line : first)
|
||||
CHECK(line.rfind("; clock 20010909-014640 roll ", 0) == 0);
|
||||
// The sequence still advances from layer to layer. Only its starting point is fixed.
|
||||
CHECK(std::set<std::string>(first.begin(), first.end()).size() > 1);
|
||||
CHECK(clock_lines() == first);
|
||||
}
|
||||
|
||||
TEST_CASE("Klipper object labels name each copy without the characters Klipper cannot parse", "[GCode]")
|
||||
{
|
||||
const auto [name, label] = GENERATE(table<std::string, std::string>({
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
#include <catch2/catch_all.hpp>
|
||||
#include <ctime>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include "libslic3r/Config.hpp"
|
||||
#include "libslic3r/SourceDateEpoch.hpp"
|
||||
#include "test_utils.hpp"
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/catch_approx.hpp>
|
||||
#include <catch2/catch_message.hpp>
|
||||
#include <catch2/generators/catch_generators.hpp>
|
||||
#include <catch2/matchers/catch_matchers.hpp>
|
||||
#include <catch2/matchers/catch_matchers_string.hpp>
|
||||
#include "libslic3r/PlaceholderParser.hpp"
|
||||
@@ -400,6 +405,36 @@ SCENARIO("Placeholder parser names in branches that are not taken", "[Placeholde
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("ORCA_SOURCE_DATE_EPOCH is read as a whole number of seconds", "[PlaceholderParser]") {
|
||||
const ScopedEnvVar epoch("ORCA_SOURCE_DATE_EPOCH", "1000000000");
|
||||
CHECK(source_date_epoch() == std::optional<std::time_t>(1000000000));
|
||||
}
|
||||
|
||||
TEST_CASE("ORCA_SOURCE_DATE_EPOCH is read up to the end of the year 3000", "[PlaceholderParser]") {
|
||||
const ScopedEnvVar epoch("ORCA_SOURCE_DATE_EPOCH", "32535215999"); // 3000-12-31 23:59:59 UTC
|
||||
PlaceholderParser parser;
|
||||
CHECK(parser.process("[timestamp]") == "30001231-235959");
|
||||
}
|
||||
|
||||
TEST_CASE("ORCA_SOURCE_DATE_EPOCH is ignored when it is not a whole number of seconds the clock can hold", "[PlaceholderParser]") {
|
||||
// The last two are later than any platform's gmtime() accepts.
|
||||
const std::string value = GENERATE("", "abc", "-5", "12x", " 12", "1e9", "32535216000", "9223372036854775807");
|
||||
INFO("value: \"" << value << "\"");
|
||||
const ScopedEnvVar epoch("ORCA_SOURCE_DATE_EPOCH", value);
|
||||
CHECK_FALSE(source_date_epoch().has_value());
|
||||
}
|
||||
|
||||
TEST_CASE("ORCA_SOURCE_DATE_EPOCH is ignored when it is not set", "[PlaceholderParser]") {
|
||||
const ScopedEnvVar epoch("ORCA_SOURCE_DATE_EPOCH", std::nullopt);
|
||||
CHECK_FALSE(source_date_epoch().has_value());
|
||||
}
|
||||
|
||||
TEST_CASE("ORCA_SOURCE_DATE_EPOCH replaces the clock in the time variables, read as UTC", "[PlaceholderParser]") {
|
||||
const ScopedEnvVar epoch("ORCA_SOURCE_DATE_EPOCH", "1000000000"); // 2001-09-09 01:46:40 UTC
|
||||
PlaceholderParser parser; // reads the clock when it is built
|
||||
CHECK(parser.process("[timestamp] [year]-[month]-[day] [hour]:[minute]:[second]") == "20010909-014640 2001-9-9 1:46:40");
|
||||
}
|
||||
|
||||
TEST_CASE("Placeholder parser deferred values can be printed but not computed with", "[PlaceholderParser]") {
|
||||
PlaceholderParser parser;
|
||||
parser.set_deferred("total_weight", "@TOTAL_WEIGHT@");
|
||||
|
||||
Reference in new issue
Block a user