Repository navigation
Expand file tree
/
Copy pathMainWindow.McpSession.cpp
More file actions
165 lines (151 loc) · 6.86 KB
/
Copy pathMainWindow.McpSession.cpp
File metadata and controls
165 lines (151 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// MainWindow partial: this window as an MCP session on the broker hub, and
// the shim copy MCP clients launch.
#include "pch.h"
#include "MainWindow.xaml.h"
#include "Mcp/McpPeerIdentity.h"
#include "Version.h"
#include <appmodel.h>
#include <shlobj.h>
#include <shobjidl_core.h>
namespace winrt::ShaderLab::implementation
{
// The hub's AUMID for a client's shim to activate:
// "<PackageFamilyName>!Hub". Empty when unpackaged (dev), where the shim
// works only against a hub that is already running.
std::wstring MainWindow::HubAumid()
{
UINT32 len = 0;
LONG rc = ::GetCurrentPackageFamilyName(&len, nullptr);
if (rc != ERROR_INSUFFICIENT_BUFFER)
return {}; // APPMODEL_ERROR_NO_PACKAGE -> unpackaged
std::wstring pfn(len, L'\0');
if (::GetCurrentPackageFamilyName(&len, pfn.data()) != ERROR_SUCCESS)
return {};
pfn.resize(len ? len - 1 : 0); // drop the null terminator
return pfn + L"!Hub";
}
// Copies the shim (ShaderLabMcpBroker.exe, beside ShaderLab.exe in the
// package) to %LOCALAPPDATA%\ShaderLab\bin\. Unpackaged, that copy
// survives MSIX updates and uninstalls, so an MCP client keeps working
// across an upgrade. Returns the copy's path, or empty on failure.
//
// A running shim holds the file open, so the old copy is renamed aside
// (allowed while it runs) and the new one written in its place for the
// next launch. Old copies whose shim has exited are deleted.
std::wstring MainWindow::EnsureShimDistributed()
{
wchar_t exePath[MAX_PATH * 2]{};
if (::GetModuleFileNameW(nullptr, exePath, ARRAYSIZE(exePath)) == 0)
return {};
std::wstring dir = exePath;
auto slash = dir.find_last_of(L'\\');
if (slash == std::wstring::npos) return {};
std::wstring source = dir.substr(0, slash) + L"\\ShaderLabMcpBroker.exe";
if (::GetFileAttributesW(source.c_str()) == INVALID_FILE_ATTRIBUTES)
return {}; // no broker payload (unexpected in a real build)
PWSTR local = nullptr;
if (FAILED(::SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &local)))
return {};
std::wstring binDir = std::wstring(local) + L"\\ShaderLab\\bin";
::CoTaskMemFree(local);
::SHCreateDirectoryExW(nullptr, binDir.c_str(), nullptr);
std::wstring target = binDir + L"\\ShaderLabMcpBroker.exe";
if (::GetFileAttributesW(target.c_str()) != INVALID_FILE_ATTRIBUTES)
{
std::wstring aside = target + L"." + std::to_wstring(::GetTickCount64()) + L".old";
::MoveFileExW(target.c_str(), aside.c_str(), MOVEFILE_REPLACE_EXISTING);
}
::CopyFileW(source.c_str(), target.c_str(), FALSE);
WIN32_FIND_DATAW fd{};
HANDLE h = ::FindFirstFileW((target + L".*.old").c_str(), &fd);
if (h != INVALID_HANDLE_VALUE)
{
do { ::DeleteFileW((binDir + L"\\" + fd.cFileName).c_str()); }
while (::FindNextFileW(h, &fd));
::FindClose(h);
}
return (::GetFileAttributesW(target.c_str()) != INVALID_FILE_ATTRIBUTES)
? target : std::wstring{};
}
// Registers this window with the hub as a session. Requests arrive
// sealed and are routed through m_mcpServer, so tool calls reach the same
// routes and engine events as any other caller. Idempotent.
void MainWindow::StartMcpSession()
{
if (m_sessionClient || !m_mcpServer)
return;
// Refresh the shim on disk; a shim a client already runs keeps going.
EnsureShimDistributed();
// Activate the hub before the first connect: the session client only
// connects, and with no hub running this window would otherwise wait
// unregistered until some shim happened to activate one. A second
// activation of the singleton hub is a no-op. Best effort: without it
// the client keeps retrying until a hub appears.
if (const std::wstring aumid = HubAumid(); !aumid.empty())
{
winrt::com_ptr<IApplicationActivationManager> activation;
if (SUCCEEDED(::CoCreateInstance(CLSID_ApplicationActivationManager, nullptr,
CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(activation.put()))))
{
const std::wstring args = std::format(L"--hub --pipe {}", ::ShaderLab::Mcp::ResolvePipeBaseName({}));
DWORD pid = 0;
if (FAILED(activation->ActivateApplication(aumid.c_str(), args.c_str(), AO_NONE, &pid)))
OutputDebugStringW(L"[ShaderLab] MCP hub activation failed; the session keeps retrying\n");
}
}
if (m_mcpSessionId.empty())
{
GUID g{};
CoCreateGuid(&g);
wchar_t buf[64]{};
StringFromGUID2(g, buf, ARRAYSIZE(buf));
m_mcpSessionId = buf; // stable for this window's lifetime, not an ordinal
}
::ShaderLab::Mcp::SessionClientOptions opts;
opts.sessionId = m_mcpSessionId;
opts.label = std::format(L"ShaderLab {} (pid {})",
std::wstring(::ShaderLab::VersionString), GetCurrentProcessId());
m_sessionClient = std::make_unique<::ShaderLab::Mcp::McpSessionClient>(
*m_mcpServer, std::move(opts));
m_sessionThread = std::thread([this] { m_sessionClient->Run(); });
}
// Runs before the render dispatcher shuts down: a request still in
// flight could otherwise be waiting on a worker that is gone.
void MainWindow::StopMcpSession()
{
if (m_sessionClient)
m_sessionClient->Stop();
if (m_sessionThread.joinable())
m_sessionThread.join();
m_sessionClient.reset();
}
// Toolbar label: whether this window is exposed, and if so whether the
// hub has it registered. The tooltip carries the reason when it is not.
void MainWindow::UpdateMcpStatusLabel()
{
auto label = McpServerLabel();
if (!label)
return;
using ::ShaderLab::Mcp::SessionState;
std::wstring text = L"MCP: off";
std::wstring detail;
if (m_sessionClient)
{
const auto status = m_sessionClient->Status();
detail = status.detail;
switch (status.state)
{
case SessionState::Registered: text = L"MCP: on"; break;
case SessionState::NoHub: text = L"MCP: no hub"; break;
case SessionState::Refused: text = L"MCP: refused"; break;
default: text = L"MCP: connecting"; break;
}
}
if (label.Text() != text)
{
label.Text(text);
winrt::Microsoft::UI::Xaml::Controls::ToolTipService::SetToolTip(label,
detail.empty() ? nullptr : winrt::box_value(winrt::hstring(detail)));
}
}
}