MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Decoder.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "Schema.hpp"
5
6namespace MayaFlux::Nexus {
7
8/**
9 * @class StateDecoder
10 * @brief Patches or rebuilds Fabric state from a previously encoded EXR + JSON schema pair.
11 *
12 * Does what the classes allow and reports the rest. Emitters, Sensors, Agents
13 * (including Locus and Presence) are patched by id or constructed; their
14 * Expanses are restored. Optional fields (position, orientation, color, size)
15 * are applied only when the schema records them, except that an absent position
16 * clears the entity's position. Callable name mismatches between schema and
17 * live entity are warned but do not abort the patch.
18 *
19 * Live objects are never invented. Sinks, typed influences and perceptions,
20 * attachments, a Locus's view targets and every wiring source that needs a
21 * window, a network source or a callable are reported as warnings, and a wiring
22 * that cannot be restored falls back to commit_driven. A Hull needs a buffer
23 * and is skipped; a Mantle comes back as its plain box. Files from schema
24 * versions k_schema_min_version up to k_schema_version are read, with fields
25 * added since taking their defaults.
26 */
27class MAYAFLUX_API StateDecoder {
28public:
29 StateDecoder() = default;
30 ~StateDecoder() = default;
31
32 StateDecoder(const StateDecoder&) = delete;
36
37 /**
38 * @brief Result of a reconstruct() call.
39 */
41 int constructed { 0 };
42 int patched { 0 };
43 int skipped { 0 };
44 std::vector<std::string> warnings;
45 nlohmann::json user_state;
46 };
47
48 /**
49 * @brief Decode and apply to @p fabric.
50 * @param fabric Target fabric. Must already contain entities with
51 * ids matching the schema. Expanses are restored too; one
52 * that matches an Expanse already on the Fabric is left
53 * in place and only has its box updated.
54 * @param base_path Path stem without extension, same value passed to
55 * StateEncoder::encode.
56 * @return True on success. Partial patches (some ids missing) still
57 * return true; failures are logged.
58 */
59 [[nodiscard]] bool decode(Fabric& fabric, const std::string& base_path);
60
61 /**
62 * @brief Patch existing entities and construct missing ones from schema.
63 *
64 * Entities whose id exists in the fabric are patched in place. Missing
65 * entities are constructed, their callables resolved via the fabric's
66 * function registry (left empty with a warning if absent), and wired.
67 * Restorable wiring kinds: every (with its scheduler tokens), move_to and
68 * commit_driven. Any other kind falls back to commit_driven with a warning.
69 * Entity ids are assigned by the Fabric, so a reconstructed entity may get a
70 * different id, which is warned.
71 * Hard failure (bad schema, unreadable EXR, dimension mismatch) sets
72 * last_error() and returns a zeroed result.
73 *
74 * @param fabric Target fabric. May be empty, partial, or full.
75 * @param base_path Path stem without extension.
76 * @return Counts and per-entity warnings.
77 */
78 [[nodiscard]] ReconstructionResult reconstruct(Fabric& fabric, const std::string& base_path);
79
80 /**
81 * @brief Reconstruct all Fabrics in @p tapestry from @p base_dir.
82 *
83 * Reads tapestry.json, creates or finds Fabrics by name (a created Fabric gets
84 * the cell size its file recorded), calls reconstruct(fabric, base_path) for
85 * each, then restores Tapestry-level named Expanses and registers them on the
86 * listed Fabrics. A name the Tapestry already holds is reused, not recreated.
87 *
88 * @param tapestry Target Tapestry. May be empty or partially populated.
89 * @param base_dir Directory path matching the one passed to encode().
90 * @return ReconstructionResult aggregated across all Fabrics.
91 */
92 [[nodiscard]] ReconstructionResult reconstruct(Tapestry& tapestry, const std::string& base_dir);
93
94 /**
95 * @brief Last error message, empty if no error.
96 */
97 [[nodiscard]] const std::string& last_error() const { return m_last_error; }
98
99 /**
100 * @brief Number of Emitters actually patched on the last decode call.
101 */
102 [[nodiscard]] size_t patched_count() const { return m_patched_count; }
103
104 /**
105 * @brief Number of ids present in the schema but missing from the fabric.
106 */
107 [[nodiscard]] size_t missing_count() const { return m_missing_count; }
108
109private:
110 std::string m_last_error;
111 size_t m_patched_count { 0 };
112 size_t m_missing_count { 0 };
113};
114
115} // namespace MayaFlux::Nexus
Orchestrates spatial indexing and scheduling for Nexus objects.
Definition Fabric.hpp:38
const std::string & last_error() const
Last error message, empty if no error.
Definition Decoder.hpp:97
StateDecoder(const StateDecoder &)=delete
StateDecoder & operator=(StateDecoder &&)=default
size_t patched_count() const
Number of Emitters actually patched on the last decode call.
Definition Decoder.hpp:102
StateDecoder(StateDecoder &&)=default
size_t missing_count() const
Number of ids present in the schema but missing from the fabric.
Definition Decoder.hpp:107
StateDecoder & operator=(const StateDecoder &)=delete
Patches or rebuilds Fabric state from a previously encoded EXR + JSON schema pair.
Definition Decoder.hpp:27
Owner of one or more Fabrics and the shared state they rely on.
Definition Tapestry.hpp:25