载入中...
搜索中...
未找到
ProjectIndexGraph.cpp
浏览该文件的文档.
1#include <Poco/JSON/Array.h>
2#include <Poco/JSON/Parser.h>
3#include <algorithm>
4#include <stdexcept>
9
11namespace {
12using Array = Poco::JSON::Array;
13struct Match {
14 std::string path, pointer, hash;
15 unsigned line = 0;
16 Object::Ptr value;
17 std::string body;
18};
19void diagnostic(Snapshot& snapshot, const std::string& code, const std::string& id, const std::string& message) {
20 Object::Ptr out = new Object();
21 out->set("code", code);
22 out->set("id", id);
23 out->set("message", message);
24 snapshot.diagnostics.push_back(out);
25}
26std::string valueType(const Poco::Dynamic::Var& value) {
27 if (value.isEmpty()) return "null";
28 if (value.type() == typeid(Object::Ptr)) return "object";
29 if (value.type() == typeid(Array::Ptr)) return "array";
30 if (value.isString()) return "string";
31 if (value.isBoolean()) return "boolean";
32 if (value.isInteger()) return "integer";
33 return value.isNumeric() ? "number" : "unknown";
34}
35std::string escapedPointer(const std::string& name) {
36 std::string out;
37 for (const char c : name) out += c == '~' ? "~0" : c == '/' ? "~1" : std::string(1, c);
38 return out;
39}
40Object::Ptr source(const Match& match) {
41 Object::Ptr out = new Object();
42 out->set("file", match.path);
43 out->set("pointer", match.pointer);
44 if (match.line) out->set("line", match.line);
45 out->set("inputHash", match.hash);
46 return out;
47}
48Object::Ptr node(const std::string& id, const std::string& kind, const std::string& label) {
49 Object::Ptr out = new Object();
50 out->set("id", id);
51 out->set("kind", kind);
52 out->set("label", label);
53 out->set("status", "resolved");
54 return out;
55}
56void addEdge(Snapshot& snapshot, const std::string& from, const std::string& to, const std::string& relation,
57 const std::string& evidence, Object::Ptr origin = {}) {
58 Object::Ptr out = new Object();
59 out->set("from", from);
60 out->set("to", to);
61 out->set("relation", relation);
62 out->set("evidence", evidence);
63 if (origin) out->set("source", origin);
64 snapshot.edges.push_back(out);
65}
66void flatten(Snapshot& snapshot, std::vector<Match>& objects, const Poco::Dynamic::Var& value, const Match& location,
67 const std::string& parent = {}) {
68 if (snapshot.nodes.size() >= 16384) throw std::runtime_error("Project structure budget exceeded");
69 const auto id = "json:" + location.path + "#" + location.pointer;
70 auto item = node(id, "json_" + valueType(value), location.pointer.empty() ? location.path : location.pointer);
71 item->set("source", source(location));
72 item->set("evidence", "parsed-json");
73 snapshot.nodes.emplace(id, item);
74 if (!parent.empty()) addEdge(snapshot, parent, id, "contains", "parsed-json");
75 if (value.type() == typeid(Object::Ptr)) {
76 auto object = value.extract<Object::Ptr>();
77 auto match = location;
78 match.value = object;
79 objects.push_back(match);
80 std::vector<std::string> names;
81 object->getNames(names);
82 Array::Ptr fields = new Array();
83 for (const auto& name : names) {
84 Object::Ptr field = new Object();
85 field->set("name", name);
86 field->set("type", valueType(object->get(name)));
87 fields->add(field);
88 auto child = location;
89 child.pointer += "/" + escapedPointer(name);
90 flatten(snapshot, objects, object->get(name), child, id);
91 }
92 item->set("fields", fields);
93 } else if (value.type() == typeid(Array::Ptr)) {
94 auto values = value.extract<Array::Ptr>();
95 item->set("count", values->size());
96 for (size_t index = 0; index < values->size(); ++index) {
97 auto child = location;
98 child.pointer += "/" + std::to_string(index);
99 flatten(snapshot, objects, values->get(static_cast<unsigned>(index)), child, id);
100 }
101 } else if (value.isString()) {
102 const auto text = value.convert<std::string>();
103 item->set("value", text.substr(0, 512));
104 item->set("valueTruncated", text.size() > 512);
105 } else
106 item->set("value", value);
107}
108std::vector<Match> resolve(Object::Ptr declaration, const std::vector<Match>& objects,
109 const std::map<std::string, std::vector<Match>>& functions) {
110 std::vector<Match> matches;
111 if (static_cast<int>(declaration->has("symbol")) + static_cast<int>(declaration->has("jsonId")) != 1)
112 throw std::runtime_error("Source declaration requires exactly one symbol or jsonId selector");
113 if (declaration->has("symbol")) {
114 const auto symbol = requiredString(declaration, "symbol");
115 const auto found = functions.find(symbol);
116 if (found != functions.end()) matches = found->second;
117 if (declaration->has("contains")) {
118 const auto anchor = requiredString(declaration, "contains");
119 std::erase_if(matches, [&](const Match& match) { return match.body.find(anchor) == std::string::npos; });
120 }
121 } else if (declaration->has("jsonId")) {
122 const auto id = requiredString(declaration, "jsonId");
123 for (const auto& match : objects) {
124 if (!match.value->has("id") || !match.value->get("id").isString() ||
125 match.value->getValue<std::string>("id") != id)
126 continue;
127 bool valid = true;
128 if (declaration->has("fields")) {
129 auto fields = declaration->getArray("fields");
130 if (!fields) throw std::runtime_error("fields must be an array");
131 for (size_t index = 0; index < fields->size(); ++index) {
132 const auto field = fields->get(static_cast<unsigned>(index));
133 if (!field.isString()) throw std::runtime_error("field name must be a string");
134 if (!match.value->has(field.convert<std::string>())) valid = false;
135 }
136 }
137 if (valid) matches.push_back(match);
138 }
139 } else
140 throw std::runtime_error("Semantic node requires api, symbol or jsonId");
141 return matches;
142}
143} // namespace
144Snapshot project(Files files, Object::Ptr stats, std::string defaultProjectId, std::string projectDir) {
146 out.files = std::move(files);
147 out.stats = stats;
148 out.projectDir = std::move(projectDir);
149 const auto manifestFile = out.files.find("game.semantic.json");
150 Object::Ptr manifest;
151 if (manifestFile == out.files.end()) {
152 manifest = new Object();
153 manifest->set("schema", "eve.project-semantic");
154 manifest->set("schemaVersion", 2);
155 manifest->set("projectId", defaultProjectId);
156 } else {
157 if (!manifestFile->second->error.empty() || manifestFile->second->json.type() != typeid(Object::Ptr))
158 throw std::runtime_error("Missing or invalid game.semantic.json");
159 manifest = manifestFile->second->json.extract<Object::Ptr>();
160 }
161 if (requiredString(manifest, "schema") != "eve.project-semantic" || !manifest->has("schemaVersion") ||
162 !manifest->get("schemaVersion").isInteger() ||
163 (manifest->getValue<int>("schemaVersion") != 1 && manifest->getValue<int>("schemaVersion") != 2))
164 throw std::runtime_error("Unsupported project semantic schema");
165 out.projectId = requiredString(manifest, "projectId");
166 std::string version = out.projectId + '\0';
167 std::vector<Match> objects;
168 std::map<std::string, std::vector<Match>> functions;
169 for (const auto& [path, file] : out.files) {
170 version += path + '\0' + file->hash + '\0';
171 auto item = node("file:" + path, "file", path);
172 item->set("inputHash", file->hash);
173 item->set("parser", std::filesystem::path(path).extension() == ".dnut" ? "opaque-dsl" : "source-metadata");
174 out.nodes.emplace("file:" + path, item);
175 if (!file->error.empty()) {
176 diagnostic(out, "parse_error", path, file->error);
177 continue;
178 }
179 if (!file->json.isEmpty())
180 flatten(out, objects, file->json, Match{path, "", file->hash, 0, {}, {}}, "file:" + path);
181 for (const auto& function : file->functions) {
182 Match match{path, "", file->hash, function.line, {}, function.body};
183 functions[function.name].push_back(match);
184 const auto id = "symbol:" + path + ":" + function.name + ":" + std::to_string(function.line);
185 auto symbol = node(id, "script_symbol", function.name);
186 symbol->set("source", source(match));
187 symbol->set("erasedType", "dynamic");
188 symbol->set("evidence", "source-metadata");
189 out.nodes.emplace(id, symbol);
190 addEdge(out, "file:" + path, id, "declares", "source-metadata");
191 }
192 for (const auto& reference : file->references) {
193 const auto target = pathText(sourcePath(reference).lexically_normal());
194 if (sourcePath(reference).is_absolute() || target.starts_with("../") || !out.files.contains(target))
195 diagnostic(out, "missing_script_reference", path, reference);
196 else
197 addEdge(out, "file:" + path, "file:" + target, "loads", "literal-dofile-reference");
198 }
199 }
200 if (manifest->getValue<int>("schemaVersion") == 2) {
201 if (manifest->has("nodes") || manifest->has("links"))
202 throw std::runtime_error("Compiler-derived projects do not accept manual nodes or links");
203 out.automatic = true;
205 out.revision = digest(version);
206 return out;
207 }
208 auto declarations = manifest->getArray("nodes"), links = manifest->getArray("links");
209 if (!declarations || !links || declarations->size() > 256 || links->size() > 1024)
210 throw std::runtime_error("Invalid semantic nodes/links budget");
211 for (size_t index = 0; index < declarations->size(); ++index) {
212 auto declaration = declarations->getObject(static_cast<unsigned>(index));
213 const auto id = requiredString(declaration, "id"), kind = requiredString(declaration, "kind");
214 if (static_cast<int>(declaration->has("api")) + static_cast<int>(declaration->has("symbol")) +
215 static_cast<int>(declaration->has("jsonId")) !=
216 1)
217 throw std::runtime_error("Semantic node requires exactly one api, symbol or jsonId selector");
218 if (out.nodes.contains(id)) throw std::runtime_error("Duplicate semantic node ID: " + id);
219 auto item = node(id, kind, id);
220 item->set("evidence", "project-declaration");
221 if (declaration->has("api")) {
222 const auto key = requiredString(declaration, "api");
224 version += key + '\0' + (contract ? contract->toJson() : "unavailable") + '\0';
225 if (contract) {
226 Poco::JSON::Parser parser;
227 item->set("contract", parser.parse(contract->toJson()));
228 item->set("evidence", "engine-build-contract");
229 } else
230 item->set("status", "unavailable");
231 } else {
232 const auto matches = resolve(declaration, objects, functions);
233 if (matches.size() == 1) {
234 item->set("source", source(matches.front()));
235 if (matches.front().value) item->set("definition", matches.front().value);
236 if (!matches.front().body.empty()) {
237 item->set("snippet", matches.front().body.substr(0, 2048));
238 item->set("snippetTruncated", matches.front().body.size() > 2048);
239 }
240 } else
241 item->set("status", matches.empty() ? "missing" : "ambiguous");
242 }
243 if (item->getValue<std::string>("status") != "resolved")
244 diagnostic(out, "unresolved_semantic_node", id, item->getValue<std::string>("status"));
245 out.nodes.emplace(id, item);
246 }
247 for (size_t index = 0; index < links->size(); ++index) {
248 auto link = links->getObject(static_cast<unsigned>(index));
249 const auto from = requiredString(link, "from"), to = requiredString(link, "to");
250 const auto relation = requiredString(link, "relation");
251 const auto matches = resolve(link, objects, functions);
252 if (!out.nodes.contains(from) || !out.nodes.contains(to) || matches.size() != 1 ||
253 out.nodes.at(from)->getValue<std::string>("status") != "resolved" ||
254 out.nodes.at(to)->getValue<std::string>("status") != "resolved") {
255 diagnostic(out, "unresolved_semantic_link", from + " -> " + to, relation);
256 continue;
257 }
258 addEdge(out, from, to, relation, "checked-project-declaration", source(matches.front()));
259 }
260 out.revision = digest(version);
261 return out;
262}
263} // namespace eve::dev::project_index
LogicalId target
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
std::vector< std::string > fields
Definition ECS.cpp:42
EnvironmentMetricContract contract
std::string message
DiagnosticCode code
std::int32_t c
PhysicsJointLink link
const ColourScope * parent
std::string to
std::string label
std::string relation
std::string evidence
std::string from
Snapshot snapshot
bool valid
glm::dvec3 origin
std::string path
Definition PlayHost.cpp:111
std::map< std::string, std::vector< Function > > functions
std::set< std::string > values
std::string file
std::string body
Object::Ptr value
std::string pointer
std::string hash
std::vector< EquipmentStat > stats
const RoadNode * node
bool found
MigrationFunction function
unsigned line
std::string text
ScriptStructure out
std::string_view source
std::string kind
std::string digest
std::string string
std::string sourcePath
Json object
uint32_t index
Value get(const char *key) const
Returns the value.
Definition Json.cpp:337
bool isString() const
True when string.
Definition Json.cpp:331
const BindingContract * find(std::string_view key) const noexcept
Finds a binding contract.
StateValue objects
Definition Snapshot.cpp:110
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
std::string pathText(const std::filesystem::path &path)
UTF-8 source path, including Windows project names.
void automaticGraph(Snapshot &snapshot, std::string &version)
Project typed compiler syntax through generated engine contracts; no project declarations required.
std::map< std::string, std::shared_ptr< const File > > Files
Snapshot project(Files files, Object::Ptr stats, std::string defaultProjectId, std::string projectDir)
Build a candidate evidence graph without touching the published snapshot.
std::string requiredString(Object::Ptr object, const std::string &field)
Strict source string fields; invalid manifest shapes throw a diagnostic exception.
ShapeState * child(const JPH::Body &body, const JPH::SubShapeID &id)
Resolves a native child record for a synchronous callback or query. @ownership Borrowed from the worl...
const BindingContractRegistry & generatedBindingContracts()
Process-wide generated Binding Contracts for this linked engine build.
enum EVENGINE_API_FOUNDATION Array
OT_ARRAY (editable via Runtime array APIs / ReflectedPropertyModel).
Definition Runtime.h:120
std::map< std::string, Object::Ptr > nodes
std::vector< Object::Ptr > diagnostics