载入中...
搜索中...
未找到
EditorHost.cpp
浏览该文件的文档.
1#include "editor/EditorHost.h"
2
3#include "common/Capability.h"
5#include "editor/Editor.h"
9
10#include <algorithm>
11#include <utility>
12
13namespace eve::editor {
14namespace {
15
16bool ownerAllowed(const std::vector<std::string>& owners, std::string_view owner) {
17 if (owners.empty()) return true;
18 return std::find(owners.begin(), owners.end(), owner) != owners.end();
19}
20
21} // namespace
22
24 : editor_(&editor),
25 commands_(&editor.commandService()),
26 options_(std::move(options)),
27 workspace_(options_.id, options_.title.empty() ? options_.id : options_.title),
28 selection_(std::make_unique<EditorSelectionRouter>(*this)),
29 undo_(std::make_unique<EditorUndoCoordinator>(*this)) {
30 // Default-constructed HostProfile is Developer but denies commands; promote to the
31 // unrestricted developer profile unless the caller already configured one.
32 if (options_.profile.kind() == HostKind::Developer && !options_.profile.allowsAllCommands())
34}
35
40
42
44 const std::vector<std::string>& owners, const std::unordered_map<std::string, EditorHostMountOverride>& overrides) {
46 if (!owners.empty() && eve::cap::listenerCount<editing::IEditorPackageProvider>() == 0)
47 return eve::editing::failed<EditorHostMountReport>(EditorStatus::NotFound, RuleId("editor.host.no-providers"),
48 "No IEditorPackageProvider capabilities are loaded");
49
50 eve::cap::forEach<editing::IEditorPackageProvider>([&](editing::IEditorPackageProvider* provider) {
51 if (!provider) return;
52 const editing::EditorPackage& package = provider->package();
53 if (!ownerAllowed(owners, package.ownerModule)) return;
54
56 if (const auto found = overrides.find(package.ownerModule); found != overrides.end())
57 override = found->second;
58 else
59 override.undoPolicy = package.undoPolicy;
60
61 auto entry = mountPackage(package, override);
62 if (!entry.ok()) {
64 skipped.ownerModule = package.ownerModule;
65 skipped.skipped = true;
66 skipped.skipReason = entry.status().describe();
67 report.entries.push_back(std::move(skipped));
68 return;
69 }
70 if (!entry.value().skipped) ++report.mountedPackages;
71 report.entries.push_back(std::move(entry).takeValue());
72 });
73 return eve::editing::applied<EditorHostMountReport>(std::move(report));
74}
75
76Result<void> EditorHost::unmount(std::string_view ownerModule) {
77 const auto found = mounted_.find(std::string(ownerModule));
78 if (found == mounted_.end()) return eve::editing::noOp();
79 for (const std::string& panelId : found->second.panelIds) (void)workspace_.removePanel(panelId);
80 mounted_.erase(found);
81 hasPrimaryViewport_ = false;
82 for (const auto& [_, mounted] : mounted_)
83 for (const editing::EditorPanelSpec& panel : mounted.package.panels)
84 if (panel.primaryViewport) hasPrimaryViewport_ = true;
85 return eve::editing::applied<void>();
86}
87
89 const std::vector<std::string> owners = [&] {
90 std::vector<std::string> values;
91 values.reserve(mounted_.size());
92 for (const auto& [owner, _] : mounted_) values.push_back(owner);
93 return values;
94 }();
95 for (const std::string& owner : owners)
96 unmount(owner).ignore("EditorHost teardown discards per-owner unmount status");
97}
98
99bool EditorHost::isMounted(std::string_view ownerModule) const { return mounted_.contains(std::string(ownerModule)); }
100
101std::vector<std::string> EditorHost::mountedOwnerNames() const {
102 std::vector<std::string> owners;
103 owners.reserve(mounted_.size());
104 for (const auto& [owner, _] : mounted_) owners.push_back(owner);
105 std::sort(owners.begin(), owners.end());
106 return owners;
107}
108
109std::vector<std::string> EditorHost::mountedTargetTypeNames() const {
110 std::vector<std::string> types;
111 for (const auto& [_, mounted] : mounted_)
112 for (const editing::EditorTargetTypeSpec& target : mounted.package.targetTypes) types.push_back(target.type);
113 std::sort(types.begin(), types.end());
114 types.erase(std::unique(types.begin(), types.end()), types.end());
115 return types;
116}
117
118editing::EditorUndoPolicy EditorHost::mountedUndoPolicy(std::string_view ownerModule) const {
119 const auto found = mounted_.find(std::string(ownerModule));
120 if (found == mounted_.end()) return editing::EditorUndoPolicy::Share;
121 return found->second.package.undoPolicy;
122}
123
125
127
128Result<EditorHostMountEntry> EditorHost::mountPackage(const editing::EditorPackage& package,
129 const EditorHostMountOverride& override) {
130 auto valid = editing::validateEditorPackage(package, override.allowIncomplete);
131 if (!valid.ok()) return Result<EditorHostMountEntry>::failure(valid.status());
132
133 if (mounted_.contains(package.ownerModule))
134 return eve::editing::failed<EditorHostMountEntry>(EditorStatus::Conflict, RuleId("editor.host.already-mounted"),
135 "Editor package already mounted: " + package.ownerModule);
136
137 for (const editing::EditorPanelSpec& panel : package.panels) {
138 if (panel.primaryViewport && hasPrimaryViewport_)
139 return eve::editing::failed<EditorHostMountEntry>(
140 EditorStatus::Conflict, RuleId("editor.host.primary-viewport"),
141 "Only one primaryViewport panel may be mounted on a host");
142 }
143
144 std::vector<std::string> panelIds;
145 auto installed = installPanels(package, panelIds);
146 if (!installed.ok()) {
147 for (const std::string& panelId : panelIds) (void)workspace_.removePanel(panelId);
148 return Result<EditorHostMountEntry>::failure(installed.status());
149 }
150
151 for (const editing::EditorPanelSpec& panel : package.panels)
152 if (panel.primaryViewport) hasPrimaryViewport_ = true;
153
154 MountedPackage mounted;
155 mounted.package = package;
156 mounted.package.undoPolicy = override.undoPolicy;
157 mounted.panelIds = std::move(panelIds);
158 mounted_.emplace(package.ownerModule, std::move(mounted));
159
160 EditorHostMountEntry entry;
161 entry.ownerModule = package.ownerModule;
162 entry.panelCount = package.panels.size();
163 entry.commandCount = package.commands.size();
164 entry.targetCount = package.targetTypes.size();
165 return eve::editing::applied<EditorHostMountEntry>(std::move(entry));
166}
167
168Result<void> EditorHost::installPanels(const editing::EditorPackage& package, std::vector<std::string>& panelIds) {
169 for (const editing::EditorPanelSpec& panel : package.panels) {
170 WorkspacePanelDescriptor descriptor;
171 descriptor.id = panel.id;
172 descriptor.title = panel.title;
173 descriptor.region = panel.region;
174 descriptor.capability = panel.capability;
175 descriptor.context = panel.context;
176 descriptor.order = panel.order;
177 auto registered = workspace_.registerPanel(std::move(descriptor));
178 if (!registered.ok())
179 return eve::editing::failed<void>(registered.code(), RuleId("editor.host.panel-conflict"),
180 "Could not install package panel: " + panel.id);
181 panelIds.push_back(panel.id);
182 }
183 return eve::editing::applied<void>();
184}
185
187 if (!commands_)
188 return eve::editing::failed<EditorValue>(EditorStatus::Failed, RuleId("editor.host.no-commands"),
189 "EditorHost has no command service");
190 const auto owners = mountedOwnerNames();
191 const bool filter = !owners.empty();
193 for (const CommandDescriptor& command : commands_->commands(options_.profile)) {
194 if (filter && std::find(owners.begin(), owners.end(), command.ownerModule) == owners.end()) continue;
196 item["id"] = command.id.value();
197 item["displayName"] = command.displayName;
198 item["category"] = command.category;
199 item["owner"] = command.ownerModule;
200 item["automationAllowed"] = command.automationAllowed;
201 if (!command.panelCapability.empty()) item["panelCapability"] = command.panelCapability;
202 if (!command.panelContext.empty()) item["panelContext"] = command.panelContext;
203 if (!command.targetKinds.empty()) {
204 EditorValue::Array kinds;
205 for (const std::string& kind : command.targetKinds) kinds.emplace_back(kind);
206 item["targetKinds"] = std::move(kinds);
207 }
208 auto schema = editing::propertySchemaToJsonSchema(command.payloadSchema, command.id.value());
209 if (schema.ok()) item["inputSchema"] = std::move(schema).takeValue();
210 commands.emplace_back(std::move(item));
211 }
212 return eve::editing::applied<EditorValue>(EditorValue::Object{{"commands", std::move(commands)}});
213}
214
216 std::unordered_map<std::string, editing::PropertySchema> createSchemas;
217 for (const auto& [_, mounted] : mounted_) {
218 for (const editing::EditorTargetTypeSpec& target : mounted.package.targetTypes)
219 createSchemas[target.type] = target.createSchema;
220 }
221
222 const auto allowed = mountedTargetTypeNames();
223 const bool filter = !allowed.empty();
224 EditorValue::Array types;
225 eve::cap::forEach<IEditorAutomationTargetFactory>([&](IEditorAutomationTargetFactory* factory) {
226 if (!factory) return;
227 for (const std::string_view type : factory->types()) {
228 if (filter && std::find(allowed.begin(), allowed.end(), type) == allowed.end()) continue;
230 item["type"] = std::string(type);
231 if (const auto found = createSchemas.find(std::string(type)); found != createSchemas.end()) {
232 auto schema = editing::propertySchemaToJsonSchema(found->second, type);
233 if (schema.ok()) item["createSchema"] = std::move(schema).takeValue();
234 }
235 types.emplace_back(std::move(item));
236 }
237 });
238 std::sort(types.begin(), types.end(), [](const EditorValue& left, const EditorValue& right) {
239 const auto* leftObject = left.getIf<EditorValue::Object>();
240 const auto* rightObject = right.getIf<EditorValue::Object>();
241 auto typeOf = [](const EditorValue::Object* object) -> std::string {
242 if (!object) return {};
243 const auto found = object->find("type");
244 if (found == object->end()) return {};
245 const auto* value = found->second.getIf<std::string>();
246 return value ? *value : std::string{};
247 };
248 return typeOf(leftObject) < typeOf(rightObject);
249 });
250 return eve::editing::applied<EditorValue>(EditorValue::Object{{"supportedTypes", std::move(types)}});
251}
252
253} // namespace eve::editor
LogicalId target
std::string descriptor
std::string title
HexVec3 left
HexVec3 right
std::string id
bool valid
std::shared_ptr< OceanGpuSpectrum > owner
std::set< std::string > values
bool found
std::string filter
std::string kind
const SquirrelValueOptions & options
UIHostHandle host
std::vector< VegetationPresetCommand > commands
void ignore(std::string_view reason={}) const noexcept
Explicitly discard this result after documenting the reason.
Definition Result.h:571
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
Capability-discovered package advertisement from a domain editor module.
Deterministic owning value tree shared by authoring hosts.
std::vector< Value > Array
std::map< std::string, Value > Object
std::vector< CommandDescriptor > commands(const HostProfile &profile) const
Return descriptors visible to a host profile.
Result< EditorValue > exportCommandCatalog() const
Export mounted command catalog (id + inputSchema) for tests/MCP.
editing::EditorUndoPolicy mountedUndoPolicy(std::string_view ownerModule) const
Undo policy recorded for a mounted owner (Share when unknown).
EditorHost(Editor &editor, Options options)
Create a host bound to an Editor module's command service.
std::vector< std::string > mountedOwnerNames() const
Mounted package owner names. @cost Linear in mounted package count; returns an owning vector copy.
std::vector< std::string > mountedTargetTypeNames() const
Target types contributed by mounted packages. @cost Linear in mounted target types; returns a sorted ...
bool isMounted(std::string_view ownerModule) const
True when owner is currently mounted.
Result< EditorValue > exportTargetCatalog() const
Export discovered target types with createSchema when known.
Result< EditorHostMountReport > autoMount()
Discover every IEditorPackageProvider and mount complete packages.
Result< EditorHostMountReport > mount(const std::vector< std::string > &owners={}, const std::unordered_map< std::string, EditorHostMountOverride > &overrides={})
Mount an explicit owner allowlist.
void unmountAll()
Unmount every mounted package.
Result< void > exportMcp(EditorMcpExportMode mode=EditorMcpExportMode::Commands)
Attach this host as the active Mode A MCP projection scope.
~EditorHost()
Destroy host, detach MCP projection if attached, and unmount packages.
Result< EditorValue > auditPackages() const
Completeness audit (incomplete packages + orphan factory types).
Result< void > unmount(std::string_view ownerModule)
Unmount one owner and remove its workspace panels.
Result< EditorValue > auditCompleteness() const
Audit incomplete packages and orphan automation factories.
void detach()
Clear the active attachment.
Result< void > attach(EditorHost *host, EditorMcpExportMode mode=EditorMcpExportMode::Commands)
Attach a borrowed host as the active MCP projection scope.
Default selection → inspector panel activation for a mounted host.
Tracks the focused package undo stack (Share vs Private).
Result< WorkspacePanelDescriptor > removePanel(const StableId &id)
Remove a panel and return its final owning descriptor.
Result< WorkspacePanelDescriptor > registerPanel(WorkspacePanelDescriptor descriptor)
Register a checked owning panel descriptor.
Editor building blocks — not a shipped 3D/map editor app. Script: editor <- eve.Editor();
Definition Editor.h:47
bool allowsAllCommands() const
Allows all commands.
static HostProfile developer()
Unrestricted developer profile used by legacy sessions.
HostKind kind() const
Kind.
Multi-provider extension point for domain-specific automation target creation.
virtual std::vector< std::string_view > types() const =0
Stable target type names this factory accepts.
Result< Value > propertySchemaToJsonSchema(const PropertySchema &schema, std::string_view schemaId)
Project a PropertySchema into a JSON Schema object tree (pointer-free Value).
EditorUndoPolicy
Undo participation policy for a mounted package.
Result< void > validateEditorPackage(const EditorPackage &package, bool allowIncomplete)
Validate package completeness for Host mount.
Result< void > noOp(std::vector< Diagnostic > diagnostics={})
Construct a successful void result for an operation that made no change.
EditorMcpProjector & editorMcpProjector()
Process-wide projector used by EditorAutomationProvider and eve.mcp.attachEditor.
eve::editing::RuleId RuleId
Definition EditorIds.h:32
EditorMcpExportMode
MCP export mode for an attached EditorHost.
Single domain-editor deliverable for Host discover/autoMount.
EditorUndoPolicy undoPolicy
std::vector< EditorTargetTypeSpec > targetTypes
std::vector< EditingCommandDescriptor > commands
std::vector< EditorPanelSpec > panels
UI-neutral panel descriptor contributed by a domain editor package.
Target type advertised for automation create, with optional createSchema.
Presentation and execution metadata for a registered command.
Summary of one successful or skipped package mount.
Definition EditorHost.h:32
Per-owner mount overrides.
Definition EditorHost.h:26
Aggregate mount report returned by autoMount/mount.
Definition EditorHost.h:42
std::vector< EditorHostMountEntry > entries
Definition EditorHost.h:43