升级XR插件版本

This commit is contained in:
Sora丶kong
2026-03-02 17:56:21 +08:00
parent 8962657674
commit 60f512a9bc
1317 changed files with 110305 additions and 48249 deletions

View File

@@ -0,0 +1,80 @@
using System;
using MCPForUnity.Editor.Constants;
using MCPForUnity.Editor.Helpers;
using MCPForUnity.Editor.Services;
using UnityEditor;
using UnityEngine;
namespace MCPForUnity.Editor.Migrations
{
/// <summary>
/// Detects legacy embedded-server preferences and migrates configs to the new uvx/stdio path once.
/// </summary>
[InitializeOnLoad]
internal static class LegacyServerSrcMigration
{
private const string ServerSrcKey = EditorPrefKeys.ServerSrc;
private const string UseEmbeddedKey = EditorPrefKeys.UseEmbeddedServer;
static LegacyServerSrcMigration()
{
if (Application.isBatchMode)
return;
EditorApplication.delayCall += RunMigrationIfNeeded;
}
private static void RunMigrationIfNeeded()
{
EditorApplication.delayCall -= RunMigrationIfNeeded;
bool hasServerSrc = EditorPrefs.HasKey(ServerSrcKey);
bool hasUseEmbedded = EditorPrefs.HasKey(UseEmbeddedKey);
if (!hasServerSrc && !hasUseEmbedded)
{
return;
}
try
{
McpLog.Info("Detected legacy embedded MCP server configuration. Updating all client configs...");
var summary = MCPServiceLocator.Client.ConfigureAllDetectedClients();
if (summary.FailureCount > 0)
{
McpLog.Warn($"Legacy configuration migration finished with errors ({summary.GetSummaryMessage()}). details:");
if (summary.Messages != null)
{
foreach (var message in summary.Messages)
{
McpLog.Warn($" {message}");
}
}
McpLog.Warn("Legacy keys will be removed to prevent migration loop. Please configure failing clients manually.");
}
else
{
McpLog.Info($"Legacy configuration migration complete ({summary.GetSummaryMessage()})");
}
if (hasServerSrc)
{
EditorPrefs.DeleteKey(ServerSrcKey);
McpLog.Info(" ✓ Removed legacy key: MCPForUnity.ServerSrc");
}
if (hasUseEmbedded)
{
EditorPrefs.DeleteKey(UseEmbeddedKey);
McpLog.Info(" ✓ Removed legacy key: MCPForUnity.UseEmbeddedServer");
}
}
catch (Exception ex)
{
McpLog.Error($"Legacy MCP server migration failed: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4436b2149abf4b0d8014f81cd29a2bd0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,148 @@
using System;
using System.IO;
using System.Linq;
using MCPForUnity.Editor.Clients;
using MCPForUnity.Editor.Constants;
using MCPForUnity.Editor.Helpers;
using MCPForUnity.Editor.Models;
using MCPForUnity.Editor.Services;
using Newtonsoft.Json.Linq;
using UnityEditor;
using UnityEngine;
namespace MCPForUnity.Editor.Migrations
{
/// <summary>
/// Keeps stdio MCP clients in sync with the current package version by rewriting their configs when the package updates.
/// </summary>
[InitializeOnLoad]
internal static class StdIoVersionMigration
{
private const string LastUpgradeKey = EditorPrefKeys.LastStdIoUpgradeVersion;
static StdIoVersionMigration()
{
if (Application.isBatchMode)
return;
EditorApplication.delayCall += RunMigrationIfNeeded;
}
private static void RunMigrationIfNeeded()
{
EditorApplication.delayCall -= RunMigrationIfNeeded;
string currentVersion = AssetPathUtility.GetPackageVersion();
if (string.IsNullOrEmpty(currentVersion) || string.Equals(currentVersion, "unknown", StringComparison.OrdinalIgnoreCase))
{
return;
}
string lastUpgradeVersion = string.Empty;
try { lastUpgradeVersion = EditorPrefs.GetString(LastUpgradeKey, string.Empty); } catch { }
if (string.Equals(lastUpgradeVersion, currentVersion, StringComparison.OrdinalIgnoreCase))
{
return; // Already refreshed for this package version
}
bool hadFailures = false;
bool touchedAny = false;
var configurators = McpClientRegistry.All.OfType<McpClientConfiguratorBase>().ToList();
foreach (var configurator in configurators)
{
try
{
if (!configurator.SupportsAutoConfigure)
continue;
// Handle CLI-based configurators (e.g., Claude Code CLI)
// CheckStatus with attemptAutoRewrite=true will auto-reregister if version mismatch
if (configurator is ClaudeCliMcpConfigurator cliConfigurator)
{
var previousStatus = configurator.Status;
configurator.CheckStatus(attemptAutoRewrite: true);
if (configurator.Status != previousStatus)
{
touchedAny = true;
}
continue;
}
// Handle JSON file-based configurators
if (!ConfigUsesStdIo(configurator.Client))
continue;
MCPServiceLocator.Client.ConfigureClient(configurator);
touchedAny = true;
}
catch (Exception ex)
{
hadFailures = true;
McpLog.Warn($"Failed to refresh stdio config for {configurator.DisplayName}: {ex.Message}");
}
}
if (!touchedAny)
{
// Nothing needed refreshing; still record version so we don't rerun every launch
try { EditorPrefs.SetString(LastUpgradeKey, currentVersion); } catch { }
return;
}
if (hadFailures)
{
McpLog.Warn("Stdio MCP upgrade encountered errors; will retry next session.");
return;
}
try
{
EditorPrefs.SetString(LastUpgradeKey, currentVersion);
}
catch { }
McpLog.Info($"Updated stdio MCP configs to package version {currentVersion}.");
}
private static bool ConfigUsesStdIo(McpClient client)
{
return JsonConfigUsesStdIo(client);
}
private static bool JsonConfigUsesStdIo(McpClient client)
{
string configPath = McpConfigurationHelper.GetClientConfigPath(client);
if (string.IsNullOrEmpty(configPath) || !File.Exists(configPath))
{
return false;
}
try
{
var root = JObject.Parse(File.ReadAllText(configPath));
JToken unityNode = null;
if (client.IsVsCodeLayout)
{
unityNode = root.SelectToken("servers.unityMCP")
?? root.SelectToken("mcp.servers.unityMCP");
}
else
{
unityNode = root.SelectToken("mcpServers.unityMCP");
}
if (unityNode == null) return false;
return unityNode["command"] != null;
}
catch
{
return false;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f1d589c8c8684e6f919ffb393c4b4db5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: