|
| 1 | +using System; |
| 2 | +using System.Net; |
| 3 | +using MCPForUnity.Editor.Helpers; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using UnityEditor; |
| 6 | + |
| 7 | +namespace MCPForUnity.Editor.Services |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Service for checking package updates from GitHub |
| 11 | + /// </summary> |
| 12 | + public class PackageUpdateService : IPackageUpdateService |
| 13 | + { |
| 14 | + private const string LastCheckDateKey = "MCPForUnity.LastUpdateCheck"; |
| 15 | + private const string CachedVersionKey = "MCPForUnity.LatestKnownVersion"; |
| 16 | + private const string PackageJsonUrl = "https://raw.githubusercontent.com/CoplayDev/unity-mcp/main/MCPForUnity/package.json"; |
| 17 | + |
| 18 | + /// <inheritdoc/> |
| 19 | + public UpdateCheckResult CheckForUpdate(string currentVersion) |
| 20 | + { |
| 21 | + // Check cache first - only check once per day |
| 22 | + string lastCheckDate = EditorPrefs.GetString(LastCheckDateKey, ""); |
| 23 | + string cachedLatestVersion = EditorPrefs.GetString(CachedVersionKey, ""); |
| 24 | + |
| 25 | + if (lastCheckDate == DateTime.Now.ToString("yyyy-MM-dd") && !string.IsNullOrEmpty(cachedLatestVersion)) |
| 26 | + { |
| 27 | + return new UpdateCheckResult |
| 28 | + { |
| 29 | + CheckSucceeded = true, |
| 30 | + LatestVersion = cachedLatestVersion, |
| 31 | + UpdateAvailable = IsNewerVersion(cachedLatestVersion, currentVersion), |
| 32 | + Message = "Using cached version check" |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + // Don't check for Asset Store installations |
| 37 | + if (!IsGitInstallation()) |
| 38 | + { |
| 39 | + return new UpdateCheckResult |
| 40 | + { |
| 41 | + CheckSucceeded = false, |
| 42 | + UpdateAvailable = false, |
| 43 | + Message = "Asset Store installations are updated via Unity Asset Store" |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + // Fetch latest version from GitHub |
| 48 | + string latestVersion = FetchLatestVersionFromGitHub(); |
| 49 | + |
| 50 | + if (!string.IsNullOrEmpty(latestVersion)) |
| 51 | + { |
| 52 | + // Cache the result |
| 53 | + EditorPrefs.SetString(LastCheckDateKey, DateTime.Now.ToString("yyyy-MM-dd")); |
| 54 | + EditorPrefs.SetString(CachedVersionKey, latestVersion); |
| 55 | + |
| 56 | + return new UpdateCheckResult |
| 57 | + { |
| 58 | + CheckSucceeded = true, |
| 59 | + LatestVersion = latestVersion, |
| 60 | + UpdateAvailable = IsNewerVersion(latestVersion, currentVersion), |
| 61 | + Message = "Successfully checked for updates" |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + return new UpdateCheckResult |
| 66 | + { |
| 67 | + CheckSucceeded = false, |
| 68 | + UpdateAvailable = false, |
| 69 | + Message = "Failed to check for updates (network issue or offline)" |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + /// <inheritdoc/> |
| 74 | + public bool IsNewerVersion(string version1, string version2) |
| 75 | + { |
| 76 | + try |
| 77 | + { |
| 78 | + // Remove any "v" prefix |
| 79 | + version1 = version1.TrimStart('v', 'V'); |
| 80 | + version2 = version2.TrimStart('v', 'V'); |
| 81 | + |
| 82 | + var version1Parts = version1.Split('.'); |
| 83 | + var version2Parts = version2.Split('.'); |
| 84 | + |
| 85 | + for (int i = 0; i < Math.Min(version1Parts.Length, version2Parts.Length); i++) |
| 86 | + { |
| 87 | + if (int.TryParse(version1Parts[i], out int v1Num) && |
| 88 | + int.TryParse(version2Parts[i], out int v2Num)) |
| 89 | + { |
| 90 | + if (v1Num > v2Num) return true; |
| 91 | + if (v1Num < v2Num) return false; |
| 92 | + } |
| 93 | + } |
| 94 | + return false; |
| 95 | + } |
| 96 | + catch |
| 97 | + { |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// <inheritdoc/> |
| 103 | + public bool IsGitInstallation() |
| 104 | + { |
| 105 | + // Git packages are installed via Package Manager and have a package.json in Packages/ |
| 106 | + // Asset Store packages are in Assets/ |
| 107 | + string packageRoot = AssetPathUtility.GetMcpPackageRootPath(); |
| 108 | + |
| 109 | + if (string.IsNullOrEmpty(packageRoot)) |
| 110 | + { |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + // If the package is in Packages/ it's a PM install (likely Git) |
| 115 | + // If it's in Assets/ it's an Asset Store install |
| 116 | + return packageRoot.StartsWith("Packages/", StringComparison.OrdinalIgnoreCase); |
| 117 | + } |
| 118 | + |
| 119 | + /// <inheritdoc/> |
| 120 | + public void ClearCache() |
| 121 | + { |
| 122 | + EditorPrefs.DeleteKey(LastCheckDateKey); |
| 123 | + EditorPrefs.DeleteKey(CachedVersionKey); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Fetches the latest version from GitHub's main branch package.json |
| 128 | + /// </summary> |
| 129 | + private string FetchLatestVersionFromGitHub() |
| 130 | + { |
| 131 | + try |
| 132 | + { |
| 133 | + // GitHub API endpoint (Option 1 - has rate limits): |
| 134 | + // https://api.github.com/repos/CoplayDev/unity-mcp/releases/latest |
| 135 | + // |
| 136 | + // We use Option 2 (package.json directly) because: |
| 137 | + // - No API rate limits (GitHub serves raw files freely) |
| 138 | + // - Simpler - just parse JSON for version field |
| 139 | + // - More reliable - doesn't require releases to be published |
| 140 | + // - Direct source of truth from the main branch |
| 141 | + |
| 142 | + using (var client = new WebClient()) |
| 143 | + { |
| 144 | + client.Headers.Add("User-Agent", "Unity-MCPForUnity-UpdateChecker"); |
| 145 | + string jsonContent = client.DownloadString(PackageJsonUrl); |
| 146 | + |
| 147 | + var packageJson = JObject.Parse(jsonContent); |
| 148 | + string version = packageJson["version"]?.ToString(); |
| 149 | + |
| 150 | + return string.IsNullOrEmpty(version) ? null : version; |
| 151 | + } |
| 152 | + } |
| 153 | + catch (Exception ex) |
| 154 | + { |
| 155 | + // Silent fail - don't interrupt the user if network is unavailable |
| 156 | + McpLog.Info($"Update check failed (this is normal if offline): {ex.Message}"); |
| 157 | + return null; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments