Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
SubgraphMigrator.h
Go to the documentation of this file.
1/**
2 * @file SubgraphMigrator.h
3 * @brief Phase 8 — Migrates legacy blueprint data to the flat-dictionary subgraph format.
4 *
5 * @details
6 * **Legacy format** (any schema_version ≤ 4 that has data.nodes directly):
7 * @code
8 * { "data": { "nodes": [...], "links": [...] } }
9 * @endcode
10 *
11 * **New flat-dictionary format** (schema_version 5):
12 * @code
13 * {
14 * "data": {
15 * "rootGraph": { "nodes": [...], "links": [...] },
16 * "subgraphs": {}
17 * }
18 * }
19 * @endcode
20 *
21 * SubgraphMigrator is stateless; all methods are const-correct or static.
22 * C++14 compliant — no C++17/20 features.
23 */
24
25#pragma once
26
27#include <string>
28#include "../../Source/third_party/nlohmann/json.hpp"
29
30namespace Olympe
31{
32
33/**
34 * @class SubgraphMigrator
35 * @brief Converts legacy blueprint JSON to the Phase 8 subgraph flat-dict format.
36 *
37 * Usage:
38 * @code
39 * SubgraphMigrator m;
40 * if (m.NeedsMigration(blueprint))
41 * blueprint = m.Migrate(blueprint);
42 * @endcode
43 */
45{
46public:
47 SubgraphMigrator() = default;
48 ~SubgraphMigrator() = default;
49
50 // -----------------------------------------------------------------------
51 // Detection
52 // -----------------------------------------------------------------------
53
54 /**
55 * @brief Returns true when the blueprint uses the legacy format
56 * (data.nodes exists at the top level of the data object).
57 *
58 * Both BehaviorTree and HFSM variants are detected.
59 */
60 bool NeedsMigration(const nlohmann::json& blueprint) const;
61
62 /**
63 * @brief Returns true when the blueprint is already in the Phase 8 format
64 * (data.rootGraph exists).
65 */
66 bool IsNewFormat(const nlohmann::json& blueprint) const;
67
68 // -----------------------------------------------------------------------
69 // Migration
70 // -----------------------------------------------------------------------
71
72 /**
73 * @brief Migrates a legacy blueprint to the flat-dictionary format.
74 *
75 * @param blueprint Input JSON (may be legacy or already new format).
76 * @return Migrated JSON in new format. If already new, a copy is returned.
77 */
79
80 // -----------------------------------------------------------------------
81 // Subgraph helpers (used by the editor to build subgraph definitions)
82 // -----------------------------------------------------------------------
83
84 /**
85 * @brief Creates an empty subgraph definition JSON object.
86 *
87 * @param name Human-readable name shown in the tab.
88 * @param uuid Pre-generated UUID string for this subgraph.
89 * @param blueprintType "BehaviorTree" or "HFSM".
90 * @return JSON object suitable for insertion into data.subgraphs[uuid].
91 */
92 static nlohmann::json MakeEmptySubgraph(const std::string& name,
93 const std::string& uuid,
94 const std::string& blueprintType);
95
96 /**
97 * @brief Validates that every subgraphUUID referenced by SubGraph nodes
98 * actually exists in data.subgraphs and that there are no circular
99 * dependencies (A -> B -> A).
100 *
101 * @param blueprint Full blueprint JSON in new format.
102 * @param outError Filled with a human-readable message when false is returned.
103 * @return true if the blueprint is self-consistent; false otherwise.
104 */
106 std::string& outError);
107
108private:
109 // Move data.nodes/data.links/data.rootNodeId into data.rootGraph.
111
112 // DFS-based cycle detection over subgraph UUID dependency graph.
113 static bool HasCycle(const std::string& start,
115 std::vector<std::string>& visited,
116 std::vector<std::string>& inStack);
117
118 // Collect all subgraphUUID values referenced within a graph object.
119 static std::vector<std::string> CollectSubgraphRefs(const nlohmann::json& graphObj);
120};
121
122} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Converts legacy blueprint JSON to the Phase 8 subgraph flat-dict format.
static bool HasCycle(const std::string &start, const nlohmann::json &subgraphs, std::vector< std::string > &visited, std::vector< std::string > &inStack)
void MigrateDataSection(nlohmann::json &blueprint) const
bool NeedsMigration(const nlohmann::json &blueprint) const
Returns true when the blueprint uses the legacy format (data.nodes exists at the top level of the dat...
static nlohmann::json MakeEmptySubgraph(const std::string &name, const std::string &uuid, const std::string &blueprintType)
Creates an empty subgraph definition JSON object.
bool IsNewFormat(const nlohmann::json &blueprint) const
Returns true when the blueprint is already in the Phase 8 format (data.rootGraph exists).
static bool ValidateSubgraphReferences(const nlohmann::json &blueprint, std::string &outError)
Validates that every subgraphUUID referenced by SubGraph nodes actually exists in data....
nlohmann::json Migrate(const nlohmann::json &blueprint) const
Migrates a legacy blueprint to the flat-dictionary format.
static std::vector< std::string > CollectSubgraphRefs(const nlohmann::json &graphObj)
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json