Files
OrcaSlicer/src/slic3r/Utils/InstanceID.cpp
T
HanifKoh 84657ff11e Add Missing Includes Across the Remaining Sources and Tests (#16071)
* Ignore Clipper, libpng, mcut and Boost.Polygon Internals in clang-tidy

Each only works through a wrapper or umbrella header: libslic3r/clipper.hpp or clipper_z.hpp configure Clipper before including it, png.h pulls in libpng's config headers, and Boost.Polygon's headers only compile through polygon.hpp or voronoi.hpp.

* Ignore minilzo's Config Headers in clang-tidy

lzoconf.h and lzodefs.h are internal to minilzo.h, which is what the code includes.

* Add Missing Includes Across the Remaining Sources and Tests

Covers src/slic3r/Utils, src/slic3r/plugin, src/slic3r/Config, src/libvgcode, src/dev-utils, src/OrcaSlicer.cpp and tests/, the directories left after src/slic3r/GUI and src/libslic3r. Generated with clang-tidy misc-include-cleaner. libvgcode's own headers are included by relative path as in the rest of that library, and Catch2 and pybind11 with angle brackets as elsewhere in the repo.

* Make the GUI and Test Headers Compile on Their Own

Each now includes, or forward-declares, what it uses instead of relying on what its includers happened to include first. Headers that only compile on one platform, or that nothing built includes, are left alone.

* Keep Windows and nanosvg Setup Ahead of the Added Includes

OrcaSlicer.cpp and several tests set _WIN32_WINNT, WIN32_LEAN_AND_MEAN or NOMINMAX before including Windows.h, and the profile validator defines NANOSVG_IMPLEMENTATION before any libslic3r header. The added includes had landed above those blocks, which broke the Windows build.

* Add the GUI Includes the First Pass Missed

Covers headers that only became editable once they compiled on their own, and wx symbols whose suggested header changed as the clang-tidy ignore list grew after the src/slic3r/GUI pass.

* Keep the Added Test Includes Below the NOMINMAX Guard

test_marchingsquares.cpp and test_texture_displacement.cpp had includes inside #ifndef NOMINMAX, which the tests inherit as defined on Windows from libslic3r, so those were skipped there. .clang-tidy also ignores the MSVC STL and UCRT internals, Boost.Multiprecision's fwd.hpp and CPython's Windows include directory, as in #16068.
2026-10-03 13:45:21 +08:00

213 lines
4.8 KiB
C++

#include "InstanceID.hpp"
#include <algorithm>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <cctype>
#include <ios>
#include <mutex>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <boost/log/trivial.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/string_generator.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/nowide/fstream.hpp>
#include "libslic3r/AppConfig.hpp"
#include "libslic3r/Utils.hpp"
namespace Slic3r {
namespace instance_id {
namespace {
constexpr const char* CONFIG_KEY = "updater_iid";
constexpr const char* LEGACY_KEY = "iid";
std::mutex& cache_mutex()
{
static std::mutex mtx;
return mtx;
}
std::string& cached_iid()
{
static std::string value;
return value;
}
bool& cache_ready()
{
static bool ready = false;
return ready;
}
std::optional<std::string> normalize_uuid(std::string value)
{
boost::algorithm::trim(value);
boost::algorithm::to_lower(value);
if (value.size() != 36)
return std::nullopt;
try {
const boost::uuids::uuid parsed = boost::uuids::string_generator()(value);
if (parsed.version() != boost::uuids::uuid::version_random_number_based)
return std::nullopt;
return value;
} catch (...) {
return std::nullopt;
}
}
std::optional<std::string> read_config_value(AppConfig& config)
{
const auto read_key = [&](const char* key) -> std::optional<std::string> {
const std::string raw = config.get(key);
if (raw.empty())
return std::nullopt;
if (auto normalized = normalize_uuid(raw))
return normalized;
return std::nullopt;
};
if (auto value = read_key(CONFIG_KEY))
return value;
return read_key(LEGACY_KEY);
}
void write_config_value(AppConfig& config, const std::string& value)
{
config.set(CONFIG_KEY, value);
if (config.get(LEGACY_KEY) != value)
config.set(LEGACY_KEY, value);
}
void prune_config_value(AppConfig& config)
{
if (config.has(CONFIG_KEY))
config.erase("app", CONFIG_KEY);
if (config.has(LEGACY_KEY))
config.erase("app", LEGACY_KEY);
}
boost::filesystem::path storage_path()
{
const std::string& base_dir = Slic3r::data_dir();
if (base_dir.empty())
return {};
return boost::filesystem::path(base_dir) / ".orcaslicer_machine_id";
}
std::optional<std::string> read_storage_file()
{
const auto path = storage_path();
if (path.empty() || !boost::filesystem::exists(path))
return std::nullopt;
boost::nowide::ifstream file(path.string());
if (!file)
return std::nullopt;
std::string value;
std::getline(file, value);
file.close();
return normalize_uuid(value);
}
bool write_storage_file(const std::string& value)
{
if (value.empty())
return false;
const auto path = storage_path();
if (path.empty())
return false;
const auto parent = path.parent_path();
boost::system::error_code ec;
if (!parent.empty() && !boost::filesystem::exists(parent))
boost::filesystem::create_directories(parent, ec);
if (ec)
return false;
boost::nowide::ofstream file(path.string(), std::ios::trunc);
if (!file)
return false;
file << value;
file.close();
return file.good();
}
std::optional<std::string> read_secure()
{
return read_storage_file();
}
bool write_secure(const std::string& value)
{
return write_storage_file(value);
}
std::string generate_uuid()
{
const auto uuid = boost::uuids::random_generator()();
return boost::uuids::to_string(uuid);
}
} // namespace
std::string ensure(AppConfig& config)
{
std::lock_guard<std::mutex> lock(cache_mutex());
if (cache_ready())
return cached_iid();
if (auto secure = read_secure()) {
cached_iid() = *secure;
cache_ready() = true;
prune_config_value(config);
return cached_iid();
}
if (auto from_config = read_config_value(config)) {
cached_iid() = *from_config;
cache_ready() = true;
if (!write_secure(cached_iid()))
write_config_value(config, cached_iid());
else
prune_config_value(config);
return cached_iid();
}
cached_iid() = generate_uuid();
cache_ready() = true;
if (!write_secure(cached_iid()))
write_config_value(config, cached_iid());
else
prune_config_value(config);
return cached_iid();
}
void reset_cache_for_tests()
{
std::lock_guard<std::mutex> lock(cache_mutex());
cached_iid().clear();
cache_ready() = false;
}
} // namespace instance_id
} // namespace Slic3r