fix: restore existing WeChat instance and add M6 validation tools

This commit is contained in:
2026-09-05 22:49:18 +08:00
parent 1bd0344e8b
commit 1e8e894dba
23 changed files with 1357 additions and 48 deletions
+26 -20
View File
@@ -416,7 +416,8 @@ public static partial class WechatChatClient
TimeSpan duration,
string? checkpointPath,
[EnumeratorCancellation] CancellationToken cancellationToken,
IReadOnlyList<Func<MessageEvent, CancellationToken, Task>>? callbacks = null)
IReadOnlyList<Func<MessageEvent, CancellationToken, Task>>? callbacks = null,
Action? snapshotObserved = null)
{
if (duration <= TimeSpan.Zero)
{
@@ -448,33 +449,31 @@ public static partial class WechatChatClient
IReadOnlyList<ChatMessageSnapshot>? visible = null;
try
{
window = WechatDoctor.FindWechatWindow(automation);
if (window is not null)
await CommandQueue.WaitAsync(cancellationToken);
try
{
window = AttachWindow(automation);
if (!IsFileTransferAssistantOpen(window)) connected = false;
if (!connected)
{
await CommandQueue.WaitAsync(cancellationToken);
try
{
await OpenFileTransferAssistantCoreAsync(window, cancellationToken);
}
finally
{
CommandQueue.Release();
}
await OpenFileTransferAssistantCoreAsync(window, cancellationToken);
subscription?.Dispose();
var list = FindByAutomationId(window, WechatLocators.MessageList);
subscription = list?.RegisterStructureChangedEvent(
var list = FindByAutomationId(window, WechatLocators.MessageList)
?? throw new WxAgentException(WxAgentErrorCode.ControlNotFound, "Listener message list was not found.");
subscription = list.RegisterStructureChangedEvent(
TreeScope.Subtree,
(_, _, _) => signals.Writer.TryWrite(true));
connected = true;
}
// Never attribute the currently selected contact's messages to File Transfer Assistant.
if (!IsFileTransferAssistantOpen(window))
throw new WxAgentException(WxAgentErrorCode.InvalidOperationState, "Listener session changed during snapshot.");
visible = ReadVisible(window);
snapshotObserved?.Invoke();
}
else
finally
{
connected = false;
CommandQueue.Release();
}
}
catch (OperationCanceledException)
@@ -581,11 +580,18 @@ public static partial class WechatChatClient
private static AutomationElement AttachWindow(UIA3Automation automation)
{
var window = WechatDoctor.FindWechatWindow(automation);
if (window is null)
if (window is null || FindByAutomationId(window, WechatLocators.MainView) is null)
{
throw new WxAgentException(WxAgentErrorCode.WindowNotFound, "WeChat main window was not found in the current interactive session.");
if (!WechatTray.TryActivateUia(automation)) WechatTray.Inspect(true, CancellationToken.None);
for (var attempt = 0; attempt < 50; attempt++)
{
Thread.Sleep(100);
window = WechatDoctor.FindWechatWindow(automation);
if (window is not null && FindByAutomationId(window, WechatLocators.MainView) is not null) break;
}
}
if (window is null || FindByAutomationId(window, WechatLocators.MainView) is null)
throw new WxAgentException(WxAgentErrorCode.ControlNotFound, "WeChat tray activation did not restore an accessible main view; no new process was started.");
window.Focus();
return window;
}