Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
TaskGraphMigrator_v3_to_v4.cpp
Go to the documentation of this file.
1/**
2 * @file TaskGraphMigrator_v3_to_v4.cpp
3 * @brief Implementation of the v3->v4 task graph JSON migrator.
4 * @author Olympe Engine
5 * @date 2026-03-08
6 *
7 * C++14 compliant - no C++17/20 features.
8 */
9
11#include "../system/system_utils.h"
12
13#include <fstream>
14#include <sstream>
15
16namespace Olympe {
17
18// ============================================================================
19// Migrate (public static)
20// ============================================================================
21
23 const std::string& outputPath,
24 std::vector<std::string>& outErrors)
25{
26 // --- Read input file ---
27 std::ifstream inFile(inputPath);
28 if (!inFile.is_open())
29 {
30 outErrors.push_back("TaskGraphMigrator: cannot open input file: " + inputPath);
31 return false;
32 }
33
35 try
36 {
37 inFile >> v3data;
38 }
39 catch (const std::exception& e)
40 {
41 outErrors.push_back(std::string("TaskGraphMigrator: JSON parse error: ") + e.what());
42 return false;
43 }
44 inFile.close();
45
46 // --- Migrate ---
48 if (v4data.empty())
49 {
50 return false;
51 }
52
53 // --- Write output file ---
54 std::ofstream outFile(outputPath);
55 if (!outFile.is_open())
56 {
57 outErrors.push_back("TaskGraphMigrator: cannot open output file: " + outputPath);
58 return false;
59 }
60
61 try
62 {
63 outFile << v4data.dump(4);
64 }
65 catch (const std::exception& e)
66 {
67 outErrors.push_back(std::string("TaskGraphMigrator: JSON write error: ") + e.what());
68 return false;
69 }
70 outFile.close();
71
72 SYSTEM_LOG << "[TaskGraphMigrator] Migrated '" << inputPath
73 << "' -> '" << outputPath << "' (schema v3 -> v4)\n";
74 return true;
75}
76
77// ============================================================================
78// MigrateJson (public static)
79// ============================================================================
80
82 std::vector<std::string>& outErrors)
83{
84 // --- Validate schema_version ---
85 if (!v3data.contains("schema_version") || v3data["schema_version"] != 3)
86 {
87 outErrors.push_back("TaskGraphMigrator: input is not schema_version 3");
88 return json{};
89 }
90
91 json v4;
92
93 // --- Copy metadata fields ---
94 if (v3data.contains("name")) v4["name"] = v3data["name"];
95 if (v3data.contains("description")) v4["description"] = v3data["description"];
96
97 // Support both "blackboard" and "localBlackboard" naming conventions.
98 if (v3data.contains("blackboard"))
99 {
100 v4["blackboard"] = v3data["blackboard"];
101 }
102 if (v3data.contains("localBlackboard"))
103 {
104 v4["localBlackboard"] = v3data["localBlackboard"];
105 }
106
107 // --- Set v4 fields ---
108 v4["schema_version"] = 4;
109 v4["graphType"] = "VisualScript";
110
111 // --- Migrate nodes ---
112 json outNodes = json::array();
113 json execConnections = json::array();
114 json dataConnections = json::array();
115
116 if (v3data.contains("nodes") && v3data["nodes"].is_array())
117 {
118 for (const json& node : v3data["nodes"])
119 {
120 const int nodeID = node.value("nodeID", -1);
121
122 // Build ExecConnections from NextOnSuccess / NextOnFailure.
123 {
124 const int nextSuccess = node.value("NextOnSuccess", -1);
125 if (nextSuccess != -1)
126 {
127 json conn;
128 conn["SourceNodeID"] = nodeID;
129 conn["SourcePinName"] = "Out";
130 conn["TargetNodeID"] = nextSuccess;
131 conn["TargetPinName"] = "In";
132 execConnections.push_back(conn);
133 }
134 }
135 {
136 const int nextFailure = node.value("NextOnFailure", -1);
137 if (nextFailure != -1)
138 {
139 json conn;
140 conn["SourceNodeID"] = nodeID;
141 conn["SourcePinName"] = "OutFailure";
142 conn["TargetNodeID"] = nextFailure;
143 conn["TargetPinName"] = "In";
144 execConnections.push_back(conn);
145 }
146 }
147
148 // Build the v4 node object (copy relevant fields, drop v3-specific ones).
150 for (auto it = node.begin(); it != node.end(); ++it)
151 {
152 const std::string& key = it.key();
153 if (key == "NextOnSuccess" || key == "NextOnFailure")
154 {
155 // These fields are replaced by ExecConnections in v4.
156 continue;
157 }
158 outNode[key] = it.value();
159 }
160 outNodes.push_back(outNode);
161 }
162 }
163
164 v4["nodes"] = outNodes;
165 v4["ExecConnections"] = execConnections;
166 v4["DataConnections"] = dataConnections;
167
168 SYSTEM_LOG << "[TaskGraphMigrator] MigrateJson: " << outNodes.size()
169 << " nodes, " << execConnections.size() << " ExecConnections\n";
170
171 return v4;
172}
173
174} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Migrates task graph JSON from schema v3 (BT-style) to schema v4 (VS-style).
static json MigrateJson(const json &v3data, std::vector< std::string > &outErrors)
Performs the v3->v4 JSON transformation in memory.
static bool Migrate(const std::string &inputPath, const std::string &outputPath, std::vector< std::string > &outErrors)
Migrates a v3 JSON file to v4 and writes the result to disk.
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json
#define SYSTEM_LOG