载入中...
搜索中...
未找到
EditorTargetCoordinator.cpp
浏览该文件的文档.
2
6
7#include <map>
8#include <set>
9#include <utility>
10#include <vector>
11
12namespace eve::editor {
13namespace {
14
16 EditorValue::Array capabilities;
17 for (const CapabilityId& capability : descriptor.capabilities)
18 capabilities.emplace_back(capability.value());
19 return EditorValue::Object{{"id", descriptor.id.value()},
20 {"type", descriptor.type},
21 {"revision", static_cast<std::int64_t>(descriptor.revision)},
22 {"readOnly", descriptor.readOnly},
23 {"capabilities", std::move(capabilities)}};
24}
25
26} // namespace
27
29 struct TargetEntry {
30 explicit TargetEntry(IEditableTarget& value, IDomainOperationTarget& operations, std::uint64_t valueGeneration)
31 : target(&value), generation(valueGeneration), authority(&operations), transactions(&authority) {}
32
34 std::shared_ptr<void> lifetime = std::make_shared<int>(0);
35 std::uint64_t generation = 0;
39 };
40
42
44 const auto found = targets.find(id);
45 return found == targets.end() ? nullptr : found->second.get();
46 }
47 const TargetEntry* entry(const TargetId& id) const {
48 const auto found = targets.find(id);
49 return found == targets.end() ? nullptr : found->second.get();
50 }
51
53 TargetEntry* selected = entry(plan.target);
54 if (!selected || !selected->target)
55 return eve::editing::failed<TransactionReceipt>(EditorStatus::NotFound, RuleId("editor.target.not-found"),
56 "Editing target is not registered: " + plan.target.value());
57 if (selected->target->revision() != plan.baseRevision)
58 return eve::editing::failed<TransactionReceipt>(EditorStatus::Conflict,
59 RuleId("editor.target.revision-conflict"),
60 "Editing target changed after planning");
61 if (selected->generation != plan.targetGeneration)
62 return eve::editing::failed<TransactionReceipt>(EditorStatus::Conflict,
63 RuleId("editor.target.generation-conflict"),
64 "Editing target lifetime changed after planning");
65 TransactionSpec specification;
66 specification.id = TransactionId(plan.id.value() + ".transaction");
67 specification.label = plan.command.value();
68 specification.origin = ActionOrigin::Automation;
69 specification.target = plan.target;
70 specification.baseRevision = plan.baseRevision;
71 auto begun = selected->backend->begin(std::move(specification));
72 if (!begun.ok())
73 return eve::editing::failed<TransactionReceipt>(begun.code(), RuleId("editor.target.begin-failed"),
74 "Could not begin the editing transaction");
75 for (const DomainOperation& operation : plan.operations) {
76 auto appended = selected->backend->append(operation);
77 if (appended.ok()) continue;
78 auto discarded = selected->backend->discard();
79 if (!discarded.ok())
80 return eve::editing::failed<TransactionReceipt>(EditorStatus::Failed,
81 RuleId("editor.target.discard-failed"),
82 "Could not discard a rejected transaction");
83 return eve::editing::failed<TransactionReceipt>(appended.code(), RuleId("editor.target.append-failed"),
84 "Could not stage the editing operation");
85 }
86 auto committed = selected->backend->commit();
87 if (!committed.ok()) {
88 // This coordinator owns the one-shot batch; callers cannot retry or
89 // discard it themselves. Leave the existing undo history usable.
90 auto discarded = selected->backend->discard();
91 if (!discarded.ok())
92 return eve::editing::failed<TransactionReceipt>(EditorStatus::Failed,
93 RuleId("editor.target.discard-failed"),
94 "Could not discard a rejected commit");
95 }
96 return committed;
97 }
98
100 std::map<TargetId, std::unique_ptr<TargetEntry>> targets;
103 std::uint64_t generation = 0;
104 };
105 std::map<EditorSession*, SessionBinding> sessions;
106 std::vector<std::pair<CommandId, std::string>> registeredCommands;
107 std::uint64_t nextTargetGeneration = 1;
108 std::shared_ptr<void> lifetime = std::make_shared<int>(0);
109};
110
114 impl_->lifetime.reset();
115 while (!impl_->sessions.empty()) {
116 EditorSession* session = impl_->sessions.begin()->first;
117 if (session) session->coordinatorDestroyed(*this);
118 impl_->sessions.erase(impl_->sessions.begin());
119 }
120 for (const auto& [id, owner] : impl_->registeredCommands)
121 (void)impl_->commands->unregisterCommand(id, owner);
122}
123
126 const CommandId commandId = descriptor.id;
127 const std::string ownerModule = descriptor.ownerModule;
128 CommandDescriptor hostDescriptor;
129 hostDescriptor.id = descriptor.id;
130 hostDescriptor.ownerModule = std::move(descriptor.ownerModule);
131 hostDescriptor.displayName = std::move(descriptor.displayName);
132 hostDescriptor.category = std::move(descriptor.category);
133 hostDescriptor.automationAllowed = descriptor.automationAllowed;
134 hostDescriptor.targetKinds = std::move(descriptor.targetKinds);
135 hostDescriptor.payloadSchema = std::move(descriptor.payloadSchema);
136 hostDescriptor.panelCapability = std::move(descriptor.panelCapability);
137 hostDescriptor.panelContext = std::move(descriptor.panelContext);
138 auto registered = impl_->commands->registerPlannedCommand(
139 std::move(hostDescriptor),
140 [state = impl_.get(), planner = std::move(planner)](const CommandRequest& request) {
141 Impl::TargetEntry* selected = state->entry(request.context.target);
142 if (!selected || !selected->target)
143 return eve::editing::failed<CommandPlan>(EditorStatus::NotFound, RuleId("editor.target.not-found"),
144 "Editing target is not registered");
145 Result<CommandPlan> result = planner(*selected->target, request);
146 if (result.ok()) result.value().targetGeneration = selected->generation;
147 return result;
148 },
149 [state = impl_.get()](const CommandRequest&, const CommandPlan& plan) { return state->execute(plan); });
150 if (!registered.ok()) return Result<void>::failure(registered.status());
151 impl_->registeredCommands.emplace_back(commandId, ownerModule);
152 return eve::editing::applied<void>(registered.diagnostics());
153}
154
156 const std::string& ownerModule) {
157 const std::size_t removed = impl_->commands->unregisterOwner(ownerModule);
158 std::erase_if(impl_->registeredCommands,
159 [&](const auto& registration) { return registration.second == ownerModule; });
160 return eve::editing::applied<std::size_t>(removed);
161}
162
164 auto* operations = dynamic_cast<IDomainOperationTarget*>(&target);
165 if (!operations)
166 return eve::editing::failed<void>(EditorStatus::Unsupported, RuleId("editor.target.operations-unsupported"),
167 "Editing target does not support domain operations");
168 const TargetId id(target.targetId());
169 if (id.empty())
170 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.target.empty-id"),
171 "Editing target id must not be empty");
172 if (const auto found = impl_->targets.find(id); found != impl_->targets.end()) {
173 if (found->second->target == &target) {
174 return eve::editing::noOp();
175 }
176 return eve::editing::failed<void>(EditorStatus::Conflict, RuleId("editor.target.duplicate-id"),
177 "Another editing target already uses id: " + id.value());
178 }
179 impl_->targets.emplace(id,
180 std::make_unique<Impl::TargetEntry>(target, *operations, impl_->nextTargetGeneration++));
181 return eve::editing::applied<void>();
182}
183
184std::weak_ptr<void> EditorTargetCoordinator::lifetime() const { return impl_->lifetime; }
185
188 auto* existing = impl_->entry(target.targetId());
189 if (existing && (existing->target != &target || existing->backend != &backend))
190 return eve::editing::failed<void>(EditorStatus::Conflict, RuleId("editor.target.history-conflict"),
191 "Target already has another history");
192 auto registered = registerTarget(target);
193 if (!registered.ok()) return registered;
194 impl_->entry(target.targetId())->backend = &backend;
195 return eve::editing::applied<void>();
196}
197
200 auto* existing = impl_->entry(target.targetId());
201 if (!existing || existing->target != &target || existing->backend != &backend) return eve::editing::noOp();
202 return unregisterTarget(target.targetId());
203}
204
206 Impl::TargetEntry* selected = impl_->entry(target);
207 if (selected) {
208 std::vector<EditorSession*> sessions;
209 for (const auto& [session, binding] : impl_->sessions)
210 if (binding.target == target && binding.generation == selected->generation) sessions.push_back(session);
212 if (session) session->invalidateTrackedTarget(*this, target, selected->generation);
213 impl_->targets.erase(target);
214 return eve::editing::applied<void>();
215 }
216 return eve::editing::noOp();
217}
218
220 Impl::TargetEntry* selected = impl_->entry(target);
221 if (!selected || !selected->target)
222 return eve::editing::failed<void>(EditorStatus::NotFound, RuleId("editor.target.not-found"),
223 "Editing target is not registered: " + target.value());
224 session.bindTrackedTarget(*selected->target, selected->lifetime, *this, selected->generation);
225 impl_->sessions[&session] = {target, selected->generation};
226 return eve::editing::applied<void>();
227}
228
229void EditorTargetCoordinator::detach(EditorSession& session) noexcept { impl_->sessions.erase(&session); }
230
232 const Impl::TargetEntry* selected = impl_->entry(target);
233 if (!selected || !selected->target)
234 return eve::editing::failed<EditorValue>(EditorStatus::NotFound, RuleId("editor.target.not-found"),
235 "Editing target is not registered: " + target.value());
237 result["descriptor"] = descriptorValue(selected->target->describe());
238 auto snapshot = selected->target->capability<eve::editing::IEditingSnapshotProvider>();
239 result["snapshot"] = snapshot ? snapshot->get().snapshotValue() : EditorValue{};
240 return eve::editing::applied<EditorValue>(EditorValue(std::move(result)));
241}
242
244 Impl::TargetEntry* selected = impl_->entry(target);
245 if (!selected)
246 return eve::editing::failed<TransactionReceipt>(EditorStatus::NotFound, RuleId("editor.target.not-found"),
247 "Editing target is not registered: " + target.value());
248 return selected->backend->undo();
249}
250
252 Impl::TargetEntry* selected = impl_->entry(target);
253 if (!selected)
254 return eve::editing::failed<TransactionReceipt>(EditorStatus::NotFound, RuleId("editor.target.not-found"),
255 "Editing target is not registered: " + target.value());
256 return selected->backend->redo();
257}
258
259std::vector<EditorTargetCoordinator::TargetSummary> EditorTargetCoordinator::targets() const {
260 std::vector<TargetSummary> summaries;
261 summaries.reserve(impl_->targets.size());
262 // std::map keeps the ids ordered, so discovery is deterministic for a caller
263 // that diffs two lists.
264 for (const auto& [id, entry] : impl_->targets) {
265 TargetSummary summary;
266 summary.id = id.value();
267 if (entry) {
268 summary.generation = entry->generation;
269 if (entry->target) {
270 const TargetDescriptor descriptor = entry->target->describe();
271 summary.type = descriptor.type;
272 summary.revision = static_cast<std::uint64_t>(descriptor.revision);
273 }
274 }
275 summaries.push_back(std::move(summary));
276 }
277 return summaries;
278}
279
280} // namespace eve::editor
LogicalId target
ActionParameterOperation operation
std::string selected
std::string descriptor
bool committed
const GltfImportRequest & request
std::string id
Poco::JSON::Object::Ptr result
Snapshot snapshot
int sessions
std::shared_ptr< OceanGpuSpectrum > owner
OceanRenderBackend backend
eve::action::ActionVfxBinding binding
Object::Ptr value
bool found
int removed
std::optional< InteractionSession > session
Definition Tactics.cpp:39
std::vector< VegetationPresetCommand > commands
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
UUID-backed identifier adapter for legacy textual boundaries.
Definition Identity.h:313
const std::string & value() const noexcept
Returns the exact legacy or canonical compatibility spelling.
Definition Identity.h:365
Editable-target capability that applies owning domain operations.
IEditableTarget public API.
Optional capability exposing a deterministic, persistence-safe editing snapshot.
In-process authority for a single editable runtime or document target.
Deterministic owning value tree shared by authoring hosts.
std::vector< Value > Array
std::map< std::string, Value > Object
Type type() const
Return the active value type.
Registry and execution gate for UI-, script- and automation-neutral commands.
Hosts interchangeable editor tools and routes their lifecycle/input.
Result< TransactionReceipt > redo(const TargetId &target)
Redo.
~EditorTargetCoordinator()
Editor target coordinator.
eve::editing::Result< void > registerPlannedCommand(eve::editing::EditingCommandDescriptor descriptor, eve::editing::EditingCommandPlanner planner) override
Registers planned command.
std::weak_ptr< void > lifetime() const
Weak owner-thread lifetime sentinel; expires before coordinator teardown.
Result< void > unregisterTarget(const TargetId &target)
Unregisters target.
Result< EditorValue > inspect(const TargetId &target) const
Inspect.
eve::editing::Result< std::size_t > unregisterOwner(const std::string &ownerModule) override
Unregisters owner.
Result< void > bind(EditorSession &session, const TargetId &target)
Binds .
EditorTargetCoordinator(EditorCommandService &commands)
Editor target coordinator.
Result< TransactionReceipt > undo(const TargetId &target)
Undo.
Result< void > registerSharedHistoryTarget(IEditableTarget &target, IEditorTransactionBackend &backend)
Register a borrowed target and its existing history, rejecting conflicting registrations....
std::vector< TargetSummary > targets() const
Enumerate every registered target, ordered by id.
Result< void > unregisterSharedHistoryTarget(IEditableTarget &target, IEditorTransactionBackend &backend)
Remove only this exact borrowed target/backend registration; absent or replaced entries are NoOp....
Result< void > registerTarget(IEditableTarget &target)
Registers target.
Transaction boundary consumed by editor property and other UI models.
Local transaction coordinator backed by an injected edit authority.
std::function< Result< CommandPlan >(IEditableTarget &, const CommandRequest &)> EditingCommandPlanner
Side-effect-free planner invoked with the resolved target.
Result< void > noOp(std::vector< Diagnostic > diagnostics={})
Construct a successful void result for an operation that made no change.
eve::editing::Value EditorValue
Compatibility alias for the UI-independent authoring value tree.
Definition EditorValue.h:8
eve::editing::RuleId RuleId
Definition EditorIds.h:32
eve::editing::CapabilityId CapabilityId
Definition EditorIds.h:33
eve::editing::TransactionId TransactionId
Definition EditorIds.h:34
CommandPlan public API.
CommandRequest public API.
DomainOperation public API.
Domain-owned metadata for one planned editing command.
Stable discovery metadata exposed by every editable target.
TransactionSpec public API.
Presentation and execution metadata for a registered command.
std::string panelContext
Bound workspace panel context.
std::string panelCapability
Bound workspace panel capability (panel-before-automation).
PropertySchema payloadSchema
Payload schema projected to MCP inputSchema / Inspector.
std::vector< std::string > targetKinds
Target type names this command may bind.
TargetEntry(IEditableTarget &value, IDomainOperationTarget &operations, std::uint64_t valueGeneration)
const TargetEntry * entry(const TargetId &id) const
std::map< EditorSession *, SessionBinding > sessions
Result< TransactionReceipt > execute(const CommandPlan &plan)
std::map< TargetId, std::unique_ptr< TargetEntry > > targets
std::vector< std::pair< CommandId, std::string > > registeredCommands
Discovery metadata for one registered editable target.