|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using MCPForUnity.Editor.Dependencies.Models; |
| 6 | +using MCPForUnity.Editor.Dependencies.PlatformDetectors; |
| 7 | +using MCPForUnity.Editor.Helpers; |
| 8 | +using UnityEditor; |
| 9 | +using UnityEngine; |
| 10 | + |
| 11 | +namespace MCPForUnity.Editor.Dependencies |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Main orchestrator for dependency validation and management |
| 15 | + /// </summary> |
| 16 | + public static class DependencyManager |
| 17 | + { |
| 18 | + private static readonly List<IPlatformDetector> _detectors = new List<IPlatformDetector> |
| 19 | + { |
| 20 | + new WindowsPlatformDetector(), |
| 21 | + new MacOSPlatformDetector(), |
| 22 | + new LinuxPlatformDetector() |
| 23 | + }; |
| 24 | + |
| 25 | + private static IPlatformDetector _currentDetector; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Get the platform detector for the current operating system |
| 29 | + /// </summary> |
| 30 | + public static IPlatformDetector GetCurrentPlatformDetector() |
| 31 | + { |
| 32 | + if (_currentDetector == null) |
| 33 | + { |
| 34 | + _currentDetector = _detectors.FirstOrDefault(d => d.CanDetect); |
| 35 | + if (_currentDetector == null) |
| 36 | + { |
| 37 | + throw new PlatformNotSupportedException($"No detector available for current platform: {RuntimeInformation.OSDescription}"); |
| 38 | + } |
| 39 | + } |
| 40 | + return _currentDetector; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Perform a comprehensive dependency check |
| 45 | + /// </summary> |
| 46 | + public static DependencyCheckResult CheckAllDependencies() |
| 47 | + { |
| 48 | + var result = new DependencyCheckResult(); |
| 49 | + |
| 50 | + try |
| 51 | + { |
| 52 | + var detector = GetCurrentPlatformDetector(); |
| 53 | + McpLog.Info($"Checking dependencies on {detector.PlatformName}...", always: false); |
| 54 | + |
| 55 | + // Check Python |
| 56 | + var pythonStatus = detector.DetectPython(); |
| 57 | + result.Dependencies.Add(pythonStatus); |
| 58 | + |
| 59 | + // Check UV |
| 60 | + var uvStatus = detector.DetectUV(); |
| 61 | + result.Dependencies.Add(uvStatus); |
| 62 | + |
| 63 | + // Check MCP Server |
| 64 | + var serverStatus = detector.DetectMCPServer(); |
| 65 | + result.Dependencies.Add(serverStatus); |
| 66 | + |
| 67 | + // Generate summary and recommendations |
| 68 | + result.GenerateSummary(); |
| 69 | + GenerateRecommendations(result, detector); |
| 70 | + |
| 71 | + McpLog.Info($"Dependency check completed. System ready: {result.IsSystemReady}", always: false); |
| 72 | + } |
| 73 | + catch (Exception ex) |
| 74 | + { |
| 75 | + McpLog.Error($"Error during dependency check: {ex.Message}"); |
| 76 | + result.Summary = $"Dependency check failed: {ex.Message}"; |
| 77 | + result.IsSystemReady = false; |
| 78 | + } |
| 79 | + |
| 80 | + return result; |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Quick check if system is ready for MCP operations |
| 85 | + /// </summary> |
| 86 | + public static bool IsSystemReady() |
| 87 | + { |
| 88 | + try |
| 89 | + { |
| 90 | + var result = CheckAllDependencies(); |
| 91 | + return result.IsSystemReady; |
| 92 | + } |
| 93 | + catch |
| 94 | + { |
| 95 | + return false; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Get a summary of missing dependencies |
| 101 | + /// </summary> |
| 102 | + public static string GetMissingDependenciesSummary() |
| 103 | + { |
| 104 | + try |
| 105 | + { |
| 106 | + var result = CheckAllDependencies(); |
| 107 | + var missing = result.GetMissingRequired(); |
| 108 | + |
| 109 | + if (missing.Count == 0) |
| 110 | + { |
| 111 | + return "All required dependencies are available."; |
| 112 | + } |
| 113 | + |
| 114 | + var names = missing.Select(d => d.Name).ToArray(); |
| 115 | + return $"Missing required dependencies: {string.Join(", ", names)}"; |
| 116 | + } |
| 117 | + catch (Exception ex) |
| 118 | + { |
| 119 | + return $"Error checking dependencies: {ex.Message}"; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Check if a specific dependency is available |
| 125 | + /// </summary> |
| 126 | + public static bool IsDependencyAvailable(string dependencyName) |
| 127 | + { |
| 128 | + try |
| 129 | + { |
| 130 | + var detector = GetCurrentPlatformDetector(); |
| 131 | + |
| 132 | + return dependencyName.ToLowerInvariant() switch |
| 133 | + { |
| 134 | + "python" => detector.DetectPython().IsAvailable, |
| 135 | + "uv" => detector.DetectUV().IsAvailable, |
| 136 | + "mcpserver" or "mcp-server" => detector.DetectMCPServer().IsAvailable, |
| 137 | + _ => false |
| 138 | + }; |
| 139 | + } |
| 140 | + catch |
| 141 | + { |
| 142 | + return false; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// Get installation recommendations for the current platform |
| 148 | + /// </summary> |
| 149 | + public static string GetInstallationRecommendations() |
| 150 | + { |
| 151 | + try |
| 152 | + { |
| 153 | + var detector = GetCurrentPlatformDetector(); |
| 154 | + return detector.GetInstallationRecommendations(); |
| 155 | + } |
| 156 | + catch (Exception ex) |
| 157 | + { |
| 158 | + return $"Error getting installation recommendations: {ex.Message}"; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// Get platform-specific installation URLs |
| 164 | + /// </summary> |
| 165 | + public static (string pythonUrl, string uvUrl) GetInstallationUrls() |
| 166 | + { |
| 167 | + try |
| 168 | + { |
| 169 | + var detector = GetCurrentPlatformDetector(); |
| 170 | + return (detector.GetPythonInstallUrl(), detector.GetUVInstallUrl()); |
| 171 | + } |
| 172 | + catch |
| 173 | + { |
| 174 | + return ("https://python.org/downloads/", "https://docs.astral.sh/uv/getting-started/installation/"); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /// <summary> |
| 179 | + /// Validate that the MCP server can be started |
| 180 | + /// </summary> |
| 181 | + public static bool ValidateMCPServerStartup() |
| 182 | + { |
| 183 | + try |
| 184 | + { |
| 185 | + // Check if Python and UV are available |
| 186 | + if (!IsDependencyAvailable("python") || !IsDependencyAvailable("uv")) |
| 187 | + { |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + // Try to ensure server is installed |
| 192 | + ServerInstaller.EnsureServerInstalled(); |
| 193 | + |
| 194 | + // Check if server files exist |
| 195 | + var serverStatus = GetCurrentPlatformDetector().DetectMCPServer(); |
| 196 | + return serverStatus.IsAvailable; |
| 197 | + } |
| 198 | + catch (Exception ex) |
| 199 | + { |
| 200 | + McpLog.Error($"Error validating MCP server startup: {ex.Message}"); |
| 201 | + return false; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + /// <summary> |
| 206 | + /// Attempt to repair the Python environment |
| 207 | + /// </summary> |
| 208 | + public static bool RepairPythonEnvironment() |
| 209 | + { |
| 210 | + try |
| 211 | + { |
| 212 | + McpLog.Info("Attempting to repair Python environment..."); |
| 213 | + return ServerInstaller.RepairPythonEnvironment(); |
| 214 | + } |
| 215 | + catch (Exception ex) |
| 216 | + { |
| 217 | + McpLog.Error($"Error repairing Python environment: {ex.Message}"); |
| 218 | + return false; |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + /// <summary> |
| 223 | + /// Get detailed dependency information for diagnostics |
| 224 | + /// </summary> |
| 225 | + public static string GetDependencyDiagnostics() |
| 226 | + { |
| 227 | + try |
| 228 | + { |
| 229 | + var result = CheckAllDependencies(); |
| 230 | + var detector = GetCurrentPlatformDetector(); |
| 231 | + |
| 232 | + var diagnostics = new System.Text.StringBuilder(); |
| 233 | + diagnostics.AppendLine($"Platform: {detector.PlatformName}"); |
| 234 | + diagnostics.AppendLine($"Check Time: {result.CheckedAt:yyyy-MM-dd HH:mm:ss} UTC"); |
| 235 | + diagnostics.AppendLine($"System Ready: {result.IsSystemReady}"); |
| 236 | + diagnostics.AppendLine(); |
| 237 | + |
| 238 | + foreach (var dep in result.Dependencies) |
| 239 | + { |
| 240 | + diagnostics.AppendLine($"=== {dep.Name} ==="); |
| 241 | + diagnostics.AppendLine($"Available: {dep.IsAvailable}"); |
| 242 | + diagnostics.AppendLine($"Required: {dep.IsRequired}"); |
| 243 | + |
| 244 | + if (!string.IsNullOrEmpty(dep.Version)) |
| 245 | + diagnostics.AppendLine($"Version: {dep.Version}"); |
| 246 | + |
| 247 | + if (!string.IsNullOrEmpty(dep.Path)) |
| 248 | + diagnostics.AppendLine($"Path: {dep.Path}"); |
| 249 | + |
| 250 | + if (!string.IsNullOrEmpty(dep.Details)) |
| 251 | + diagnostics.AppendLine($"Details: {dep.Details}"); |
| 252 | + |
| 253 | + if (!string.IsNullOrEmpty(dep.ErrorMessage)) |
| 254 | + diagnostics.AppendLine($"Error: {dep.ErrorMessage}"); |
| 255 | + |
| 256 | + diagnostics.AppendLine(); |
| 257 | + } |
| 258 | + |
| 259 | + if (result.RecommendedActions.Count > 0) |
| 260 | + { |
| 261 | + diagnostics.AppendLine("=== Recommended Actions ==="); |
| 262 | + foreach (var action in result.RecommendedActions) |
| 263 | + { |
| 264 | + diagnostics.AppendLine($"- {action}"); |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + return diagnostics.ToString(); |
| 269 | + } |
| 270 | + catch (Exception ex) |
| 271 | + { |
| 272 | + return $"Error generating diagnostics: {ex.Message}"; |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + private static void GenerateRecommendations(DependencyCheckResult result, IPlatformDetector detector) |
| 277 | + { |
| 278 | + var missing = result.GetMissingDependencies(); |
| 279 | + |
| 280 | + if (missing.Count == 0) |
| 281 | + { |
| 282 | + result.RecommendedActions.Add("All dependencies are available. You can start using MCP for Unity."); |
| 283 | + return; |
| 284 | + } |
| 285 | + |
| 286 | + foreach (var dep in missing) |
| 287 | + { |
| 288 | + if (dep.Name == "Python") |
| 289 | + { |
| 290 | + result.RecommendedActions.Add($"Install Python 3.10+ from: {detector.GetPythonInstallUrl()}"); |
| 291 | + } |
| 292 | + else if (dep.Name == "UV Package Manager") |
| 293 | + { |
| 294 | + result.RecommendedActions.Add($"Install UV package manager from: {detector.GetUVInstallUrl()}"); |
| 295 | + } |
| 296 | + else if (dep.Name == "MCP Server") |
| 297 | + { |
| 298 | + result.RecommendedActions.Add("MCP Server will be installed automatically when needed."); |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + if (result.GetMissingRequired().Count > 0) |
| 303 | + { |
| 304 | + result.RecommendedActions.Add("Use the Setup Wizard (Window > MCP for Unity > Setup Wizard) for guided installation."); |
| 305 | + } |
| 306 | + } |
| 307 | + } |
| 308 | +} |
0 commit comments