/// <summary> /// Downloads the latest updates manifest from the Paint.NET web server. /// </summary> /// <returns>The latest updates manifest, or null if there was an error in which case the exception argument will be non-null.</returns> // TODO: throw the exception, don't return it via an out parameter private static PdnVersionManifest GetUpdatesManifest(out Exception exception) { try { string versionsUrl = VersionManifestUrl; Uri versionsUri = new Uri(versionsUrl); byte[] manifestBuffer = Utility.DownloadSmallFile(versionsUri); string manifestText = System.Text.Encoding.UTF8.GetString(manifestBuffer); string[] manifestLines = BreakIntoLines(manifestText); NameValueCollection nameValues = LinesToNameValues(manifestLines); string downloadPageUrl = nameValues[downloadPageUrlName]; string stableVersionsStrings = nameValues[stableVersionsName]; Version[] stableVersions = VersionStringToArray(stableVersionsStrings); string[] stableNames = BuildVersionValueMapping(nameValues, stableVersions, nameNameFormat); string[] stableNetFxVersions = BuildVersionValueMapping(nameValues, stableVersions, netFxVersionNameFormat); string[] stableInfoUrls = BuildVersionValueMapping(nameValues, stableVersions, infoUrlNameFormat); string[] stableZipUrls = BuildVersionValueMapping(nameValues, stableVersions, zipUrlListNameFormat); string[] stableFullZipUrls = BuildVersionValueMapping(nameValues, stableVersions, fullZipUrlListNameFormat); string betaVersionsStrings = nameValues[betaVersionsName]; Version[] betaVersions = VersionStringToArray(betaVersionsStrings); string[] betaNames = BuildVersionValueMapping(nameValues, betaVersions, nameNameFormat); string[] betaNetFxVersions = BuildVersionValueMapping(nameValues, betaVersions, netFxVersionNameFormat); string[] betaInfoUrls = BuildVersionValueMapping(nameValues, betaVersions, infoUrlNameFormat); string[] betaZipUrls = BuildVersionValueMapping(nameValues, betaVersions, zipUrlListNameFormat); string[] betaFullZipUrls = BuildVersionValueMapping(nameValues, betaVersions, fullZipUrlListNameFormat); PdnVersionInfo[] versionInfos = new PdnVersionInfo[betaVersions.Length + stableVersions.Length]; int cursor = 0; for (int i = 0; i < stableVersions.Length; ++i) { List<string> zipUrlList = new List<string>(); SplitUrlList(stableZipUrls[i], zipUrlList); List<string> fullZipUrlList = new List<string>(); SplitUrlList(stableFullZipUrls[i], fullZipUrlList); PdnVersionInfo info = new PdnVersionInfo(stableVersions[i], stableNames[i], new Version(stableNetFxVersions[i]), stableInfoUrls[i], zipUrlList.ToArray(), fullZipUrlList.ToArray(), true); versionInfos[cursor] = info; ++cursor; } for (int i = 0; i < betaVersions.Length; ++i) { List<string> zipUrlList = new List<string>(); SplitUrlList(betaZipUrls[i], zipUrlList); List<string> fullZipUrlList = new List<string>(); SplitUrlList(betaFullZipUrls[i], fullZipUrlList); PdnVersionInfo info = new PdnVersionInfo(betaVersions[i], betaNames[i], new Version(betaNetFxVersions[i]), betaInfoUrls[i], zipUrlList.ToArray(), fullZipUrlList.ToArray(), false); versionInfos[cursor] = info; ++cursor; } PdnVersionManifest manifest = new PdnVersionManifest(downloadPageUrl, versionInfos); exception = null; return manifest; } catch (Exception ex) { exception = ex; return null; } }
private static void CheckForUpdates( out PdnVersionManifest manifestResult, out int latestVersionIndexResult, out Exception exception) { exception = null; PdnVersionManifest manifest = null; manifestResult = null; latestVersionIndexResult = -1; int retries = 2; while (retries > 0) { try { manifest = GetUpdatesManifest(out exception); retries = 0; } catch (Exception ex) { exception = ex; --retries; if (retries == 0) { manifest = null; } } } if (manifest != null) { int stableIndex = manifest.GetLatestStableVersionIndex(); int betaIndex = manifest.GetLatestBetaVersionIndex(); // Check for betas as well? bool checkForBetas = ("1" == Settings.SystemWide.GetString(PdnSettings.AlsoCheckForBetas, "0")); // Figure out which version we want to compare against the current version int latestIndex = stableIndex; if (checkForBetas) { // If they like betas, and if the beta is newer than the latest stable release, // then offer it to them. if (betaIndex != -1 && (stableIndex == -1 || manifest.VersionInfos[betaIndex].Version >= manifest.VersionInfos[stableIndex].Version)) { latestIndex = betaIndex; } } // Now compare that version against the current version if (latestIndex != -1) { if (PdnInfo.IsTestMode || manifest.VersionInfos[latestIndex].Version > PdnInfo.GetVersion()) { manifestResult = manifest; latestVersionIndexResult = latestIndex; } } } }