#ifndef NOMINMAX #define NOMINMAX #endif #include "PortableRuntime.h" #include "GalaxyRuntime.h" #include #include #include #include #include #include #include #include #include namespace { QJsonObject readObject(const QString& path) { QFile file(path); if (!file.open(QIODevice::ReadOnly)) throw std::runtime_error(("Cannot open " + path).toUtf8().constData()); QJsonParseError error; auto doc = QJsonDocument::fromJson(file.readAll(), &error); if (error.error != QJsonParseError::NoError || !doc.isObject()) throw std::runtime_error("Invalid portable runtime manifest."); return doc.object(); } void addDllPath(const QString& path) { if (QDir(path).exists() && !AddDllDirectory(reinterpret_cast(QDir::toNativeSeparators(path).utf16()))) throw std::runtime_error(("Cannot add DLL search directory: " + path).toUtf8().constData()); } } QJsonObject preparePortableRuntime(bool checkOnly) { const auto app = QCoreApplication::applicationDirPath(); const auto manifest = readObject(QDir(app).filePath("visionguide.package.json")); if (manifest["schema"].toString() != "visionguide_qmake_runtime_v1") throw std::runtime_error("Unsupported portable runtime manifest."); const auto root = QDir(QDir(app).filePath(manifest["project_root"].toString())).canonicalPath(); if (root.isEmpty() || !QFile::exists(QDir(root).filePath("source/VisionGuide.pro"))) throw std::runtime_error("Copy the complete project folder before running this program."); auto assets = readObject(QDir(root).filePath("portable_assets.json")); if (assets["schema"].toString() != "visionguide_portable_assets_v1") throw std::runtime_error("Unsupported portable asset manifest."); const auto sdk = QDir(root).filePath("runtime/GalaxySDK"); const auto cameraRuntime = configureGalaxyRuntime(sdk); if (!QDir::setCurrent(root)) throw std::runtime_error("Cannot set project working directory."); QSettings settings(QSettings::IniFormat, QSettings::UserScope, "StereoVision", "StereoVision"); const QStringList previousRoots{settings.value("portable/projectRoot").toString(), assets["source_root_at_backup"].toString()}; auto paths = assets["paths"].toObject(); QJsonArray directoryKeys = assets["directory_keys"].toArray(); QJsonObject resolved;QJsonArray missing; for (auto it = paths.begin(); it != paths.end(); ++it) { const auto key = "paths/" + it.key(); const auto current = settings.value(key).toString(); QString desired = QDir(root).filePath(it.value().toString()); QString oldRoot; for (const auto& previous : previousRoots) if (!previous.isEmpty() && QDir::fromNativeSeparators(current).startsWith(QDir::fromNativeSeparators(previous) + "/", Qt::CaseInsensitive)) { oldRoot = previous; break; } bool rebase = !oldRoot.isEmpty(); if (rebase) { const auto candidate = QDir(root).filePath(QDir(oldRoot).relativeFilePath(current)); if (QFileInfo::exists(candidate)) desired = candidate; } // Preserve intentionally selected external files; repair missing or relocated package paths. bool update = current.isEmpty() || !QFileInfo::exists(current) || rebase; if (!update) desired = current; const bool directory = directoryKeys.contains(it.key()); if (directory && !checkOnly && !QDir().mkpath(desired)) throw std::runtime_error("Cannot create portable data directory."); if (!directory && !QFileInfo::exists(desired)) missing.append(it.key() + ": " + desired); if (!checkOnly && update && (directory || QFileInfo::exists(desired))) settings.setValue(key, desired); resolved[it.key()] = desired; } if (!checkOnly) { auto defaults = assets["defaults"].toObject(); for (auto it = defaults.begin(); it != defaults.end(); ++it) if (!settings.contains(it.key())) settings.setValue(it.key(), it.value().toString()); settings.setValue("portable/projectRoot", root);settings.sync(); if (settings.status() != QSettings::NoError) throw std::runtime_error("Cannot save portable application settings."); } return {{"project_root", root}, {"paths", resolved}, {"missing_assets", missing}, {"configuration_written", !checkOnly}, {"settings_file", settings.fileName()}, {"galaxy_genicam_root", sdk + "/GenICam"}, {"camera_runtime", cameraRuntime}}; }