载入中...
搜索中...
未找到
ProjectIndexScan.cpp
浏览该文件的文档.
1#include <Poco/JSON/Array.h>
2#include <Poco/JSON/Parser.h>
3#include <Poco/SHA2Engine.h>
4#include <algorithm>
5#include <fstream>
6#include <regex>
7#include <set>
8#include <stdexcept>
11
13std::string digest(const std::string& text) {
14 Poco::SHA2Engine engine;
15 engine.update(text);
16 return Poco::DigestEngine::digestToHex(engine.digest());
17}
18std::string pathText(const std::filesystem::path& path) {
19 const auto text = path.generic_u8string();
20 return std::string(text.begin(), text.end());
21}
22std::string requiredString(Object::Ptr object, const std::string& field) {
23 if (!object || !object->has(field) || !object->get(field).isString())
24 throw std::runtime_error("Expected string: " + field);
25 auto value = object->getValue<std::string>(field);
26 if (value.empty() || value.size() > 4096) throw std::runtime_error("Invalid string: " + field);
27 return value;
28}
29namespace {
30// Preserve offsets and line endings, but remove comments and optionally strings.
31std::string masked(const std::string& text, bool strings) {
32 std::string out = text;
33 bool line = false, block = false, escaped = false, verbatim = false;
34 char quote = 0;
35 for (size_t i = 0; i < text.size(); ++i) {
36 const char c = text[i], next = i + 1 < text.size() ? text[i + 1] : 0;
37 if (line) {
38 if (c == '\n')
39 line = false;
40 else
41 out[i] = ' ';
42 continue;
43 }
44 if (block) {
45 if (c != '\n' && c != '\r') out[i] = ' ';
46 if (c == '*' && next == '/') {
47 out[++i] = ' ';
48 block = false;
49 }
50 continue;
51 }
52 if (quote) {
53 if (strings && c != '\n' && c != '\r') out[i] = ' ';
54 if (escaped) {
55 escaped = false;
56 continue;
57 }
58 if (!verbatim && c == '\\') {
59 escaped = true;
60 continue;
61 }
62 if (c == quote) {
63 if (verbatim && next == quote) {
64 if (strings) out[i + 1] = ' ';
65 ++i;
66 } else
67 quote = 0;
68 }
69 continue;
70 }
71 if (c == '/' && (next == '/' || next == '*')) {
72 out[i] = out[i + 1] = ' ';
73 ++i;
74 line = next == '/';
75 block = next == '*';
76 continue;
77 }
78 if (c == '"' || c == '\'') {
79 quote = c;
80 verbatim = c == '"' && i > 0 && text[i - 1] == '@';
81 if (strings) out[i] = ' ';
82 }
83 }
84 return out;
85}
86std::shared_ptr<const File> parse(const std::string& path, std::string text, const std::string& hash) {
87 auto file = std::make_shared<File>();
88 file->text = std::move(text);
89 file->hash = hash;
90 try {
91 const auto extension = std::filesystem::path(path).extension().string();
92 if (extension == ".json") {
93 Poco::JSON::Parser parser;
94 parser.setDepth(64);
95 file->json = parser.parse(file->text);
96 } else if (extension == ".nut") {
97 const auto code = masked(file->text, true);
98 const auto comments = masked(file->text, false);
99 const auto metadata = eve::script::ScriptCompiler::analyze(file->text, path);
100 file->structure = metadata.structure;
101 if (!file->structure.diagnostics.empty()) file->error = file->structure.diagnostics.front();
102 for (const auto& function : file->structure.functions) {
103 file->functions.push_back(
104 {function.name, comments.substr(function.begin, function.end - function.begin), function.line});
105 }
106 static const std::regex reference(R"REF(\bdofile\s*\‍(\s*"([^"\n]+)"\s*\))REF");
107 for (auto it = std::sregex_iterator(comments.begin(), comments.end(), reference);
108 it != std::sregex_iterator(); ++it)
109 if (code[static_cast<size_t>(it->position())] == 'd') file->references.push_back((*it)[1].str());
110 }
111 } catch (const std::exception& e) {
112 file->error = e.what();
113 file->json = {};
114 file->functions.clear();
115 file->structure = {};
116 }
117 return file;
118}
119} // namespace
120Files scan(const std::filesystem::path& root, const Files& previous, Object::Ptr stats) {
121 Files next;
122 std::vector<std::string> newPaths;
123 size_t total = 0;
124 int parsed = 0, reused = 0, added = 0, modified = 0, removed = 0;
125 const std::set<std::string> excluded{".git", ".eve", "build", "third-party", "node_modules"};
126 for (auto it = std::filesystem::recursive_directory_iterator(root);
127 it != std::filesystem::recursive_directory_iterator(); ++it) {
128 const auto& entry = *it;
129 // Also exclude directory junctions/reparse aliases on Windows. The root
130 // itself has already been canonicalized by the caller.
131 if (entry.is_symlink() || std::filesystem::canonical(entry.path()) != entry.path()) {
132 it.disable_recursion_pending();
133 continue;
134 }
135 if (entry.is_directory()) {
136 const auto name = pathText(entry.path().filename());
137 if (name.starts_with('.') || excluded.contains(name)) it.disable_recursion_pending();
138 continue;
139 }
140 if (!entry.is_regular_file()) continue;
141 const auto extension = entry.path().extension().string();
142 if (extension != ".json" && extension != ".nut" && extension != ".dnut" && extension != ".md") continue;
143 const auto size = entry.file_size();
144 total += static_cast<size_t>(size);
145 if (size > 2 * 1024 * 1024 || total > 32 * 1024 * 1024 || next.size() >= 512)
146 throw std::runtime_error("Project input budget exceeded");
147 std::ifstream input(entry.path(), std::ios::binary);
148 if (!input) throw std::runtime_error("Cannot read project source");
149 std::string text((std::istreambuf_iterator<char>(input)), {});
150 if (input.bad() || text.size() != size) throw std::runtime_error("Project source changed during read");
151 const auto path = pathText(entry.path().lexically_relative(root));
152 const auto hash = digest(text);
153 const auto old = previous.find(path);
154 if (old != previous.end() && old->second->hash == hash) {
155 next[path] = old->second;
156 ++reused;
157 } else if (old != previous.end()) {
158 next[path] = parse(path, std::move(text), hash);
159 ++parsed;
160 ++modified;
161 } else {
162 auto pending = std::make_shared<File>();
163 pending->hash = hash;
164 pending->text = std::move(text);
165 next[path] = pending;
166 newPaths.push_back(path);
167 ++added;
168 }
169 }
170 Poco::JSON::Array::Ptr renames = new Poco::JSON::Array();
171 std::map<std::string, std::vector<std::string>> deletedHashes, addedHashes;
172 std::set<std::string> reusedRenames;
173 for (const auto& [path, file] : previous)
174 if (!next.contains(path)) {
175 ++removed;
176 deletedHashes[file->hash].push_back(path);
177 }
178 for (const auto& [path, file] : next)
179 if (!previous.contains(path)) addedHashes[file->hash].push_back(path);
180 for (const auto& [hash, paths] : deletedHashes) {
181 const auto created = addedHashes.find(hash);
182 if (paths.size() != 1 || created == addedHashes.end() || created->second.size() != 1) continue;
183 if (std::filesystem::path(paths.front()).extension() !=
184 std::filesystem::path(created->second.front()).extension())
185 continue;
186 Object::Ptr rename = new Object();
187 rename->set("from", paths.front());
188 rename->set("to", created->second.front());
189 renames->add(rename);
190 // Content is identical; preserve the parsed records across this rename.
191 next[created->second.front()] = previous.at(paths.front());
192 ++reused;
193 --added;
194 --removed;
195 reusedRenames.insert(created->second.front());
196 }
197 for (const auto& path : newPaths)
198 if (!reusedRenames.contains(path)) {
199 next[path] = parse(path, next.at(path)->text, next.at(path)->hash);
200 ++parsed;
201 }
202 stats->set("parsed", parsed);
203 stats->set("reused", reused);
204 stats->set("added", added);
205 stats->set("modified", modified);
206 stats->set("removed", removed);
207 stats->set("renames", renames);
208 stats->set("files", next.size());
209 stats->set("bytes", total);
210 return next;
211}
212} // namespace eve::dev::project_index
int root
Definition AnimSmr.cpp:119
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
std::vector< QuestEvent > pending
std::string name
Definition ECS.cpp:154
EvpackChunkInput input
Definition Evpack.cpp:170
DiagnosticCode code
std::int32_t c
bool verbatim
char quote
std::vector< std::filesystem::path > paths
std::shared_ptr< const OceanSprayFrame > previous
std::string path
Definition PlayHost.cpp:111
std::map< std::string, std::vector< Function > > functions
std::string file
Object::Ptr value
std::string hash
std::vector< EquipmentStat > stats
int created
int removed
int reused
MigrationFunction function
unsigned line
std::string text
ScriptStructure out
std::string digest
Json object
float size
Definition TreeMesh.cpp:158
static ScriptMetadata analyze(std::string_view source, std::string_view canonicalUri)
Performs source-only metadata extraction without executing code.
std::string extension(std::string_view name)
Extension.
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.
std::map< std::string, std::shared_ptr< const File > > Files
Files scan(const std::filesystem::path &root, const Files &previous, Object::Ptr stats)
Bounded filesystem scan. Parses only changed files; symlinks are excluded.
std::string requiredString(Object::Ptr object, const std::string &field)
Strict source string fields; invalid manifest shapes throw a diagnostic exception.
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
Definition HexMetrics.h:76
ModelData parse(std::span< const uint8_t > bytes)
Parse.