|
| 1 | +using UnityEditor; |
| 2 | +using UnityEngine; |
| 3 | + |
| 4 | +namespace MCPForUnity.Editor.Helpers |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Auto-runs legacy/older install detection on package load/update (log-only). |
| 8 | + /// Runs once per embedded server version using an EditorPrefs version-scoped key. |
| 9 | + /// </summary> |
| 10 | + [InitializeOnLoad] |
| 11 | + public static class PackageDetector |
| 12 | + { |
| 13 | + private const string DetectOnceFlagKeyPrefix = "MCPForUnity.LegacyDetectLogged:"; |
| 14 | + |
| 15 | + static PackageDetector() |
| 16 | + { |
| 17 | + try |
| 18 | + { |
| 19 | + string pkgVer = ReadPackageVersionOrFallback(); |
| 20 | + string key = DetectOnceFlagKeyPrefix + pkgVer; |
| 21 | + |
| 22 | + // Always force-run if legacy roots exist or canonical install is missing |
| 23 | + bool legacyPresent = LegacyRootsExist(); |
| 24 | + bool canonicalMissing = !System.IO.File.Exists(System.IO.Path.Combine(ServerInstaller.GetServerPath(), "server.py")); |
| 25 | + |
| 26 | + if (!EditorPrefs.GetBool(key, false) || legacyPresent || canonicalMissing) |
| 27 | + { |
| 28 | + EditorApplication.delayCall += () => |
| 29 | + { |
| 30 | + try |
| 31 | + { |
| 32 | + ServerInstaller.EnsureServerInstalled(); |
| 33 | + } |
| 34 | + catch (System.Exception ex) |
| 35 | + { |
| 36 | + Debug.LogWarning("MCP for Unity: Auto-detect on load failed: " + ex.Message); |
| 37 | + } |
| 38 | + finally |
| 39 | + { |
| 40 | + EditorPrefs.SetBool(key, true); |
| 41 | + } |
| 42 | + }; |
| 43 | + } |
| 44 | + } |
| 45 | + catch { /* ignore */ } |
| 46 | + } |
| 47 | + |
| 48 | + private static string ReadEmbeddedVersionOrFallback() |
| 49 | + { |
| 50 | + try |
| 51 | + { |
| 52 | + if (ServerPathResolver.TryFindEmbeddedServerSource(out var embeddedSrc)) |
| 53 | + { |
| 54 | + var p = System.IO.Path.Combine(embeddedSrc, "server_version.txt"); |
| 55 | + if (System.IO.File.Exists(p)) |
| 56 | + return (System.IO.File.ReadAllText(p)?.Trim() ?? "unknown"); |
| 57 | + } |
| 58 | + } |
| 59 | + catch { } |
| 60 | + return "unknown"; |
| 61 | + } |
| 62 | + |
| 63 | + private static string ReadPackageVersionOrFallback() |
| 64 | + { |
| 65 | + try |
| 66 | + { |
| 67 | + var info = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(PackageDetector).Assembly); |
| 68 | + if (info != null && !string.IsNullOrEmpty(info.version)) return info.version; |
| 69 | + } |
| 70 | + catch { } |
| 71 | + // Fallback to embedded server version if package info unavailable |
| 72 | + return ReadEmbeddedVersionOrFallback(); |
| 73 | + } |
| 74 | + |
| 75 | + private static bool LegacyRootsExist() |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + string home = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile) ?? string.Empty; |
| 80 | + string[] roots = |
| 81 | + { |
| 82 | + System.IO.Path.Combine(home, ".config", "UnityMCP", "UnityMcpServer", "src"), |
| 83 | + System.IO.Path.Combine(home, ".local", "share", "UnityMCP", "UnityMcpServer", "src") |
| 84 | + }; |
| 85 | + foreach (var r in roots) |
| 86 | + { |
| 87 | + try { if (System.IO.File.Exists(System.IO.Path.Combine(r, "server.py"))) return true; } catch { } |
| 88 | + } |
| 89 | + } |
| 90 | + catch { } |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | + |
0 commit comments