Пример #1
0
        public static GameBananaItem Load(string itemType, long itemId)
        {
            string response;

            using (var client = new UpdaterWebClient())
                response = client.DownloadString($"https://api.gamebanana.com/Core/Item/Data?itemtype={itemType}&itemid={itemId}&fields=name%2COwner().name%2CUrl().sGetProfileUrl()%2CUpdates().bSubmissionHasUpdates()%2CUpdates().aGetLatestUpdates()%2CFiles().aFiles()&return_keys=1");
            return(JsonConvert.DeserializeObject <GameBananaItem>(response));
        }
Пример #2
0
        public ModDownload CheckModularVersion(ModInfo mod, string folder, List <ModManifestEntry> localManifest,
                                               UpdaterWebClient client, List <string> errors)
        {
            if (!mod.UpdateUrl.StartsWith("http://", StringComparison.InvariantCulture) &&
                !mod.UpdateUrl.StartsWith("https://", StringComparison.InvariantCulture))
            {
                mod.UpdateUrl = "http://" + mod.UpdateUrl;
            }

            if (!mod.UpdateUrl.EndsWith("/", StringComparison.InvariantCulture))
            {
                mod.UpdateUrl += "/";
            }

            var url = new Uri(mod.UpdateUrl);

            url = new Uri(url, "mod.ini");

            ModInfo remoteInfo;

            try
            {
                Dictionary <string, Dictionary <string, string> > dict = IniFile.IniFile.Load(client.OpenRead(url));
                remoteInfo = IniSerializer.Deserialize <ModInfo>(dict);
            }
            catch (Exception ex)
            {
                errors.Add($"[{mod.Name}] Error pulling mod.ini from \"{mod.UpdateUrl}\": {ex.Message}");
                return(null);
            }

            if (!ForceUpdate && remoteInfo.Version == mod.Version)
            {
                return(null);
            }

            string manString;

            try
            {
                manString = client.DownloadString(new Uri(new Uri(mod.UpdateUrl), "mod.manifest"));
            }
            catch (Exception ex)
            {
                errors.Add($"[{mod.Name}] Error pulling mod.manifest from \"{mod.UpdateUrl}\": {ex.Message}");
                return(null);
            }

            List <ModManifestEntry> remoteManifest;

            try
            {
                remoteManifest = ModManifest.FromString(manString);
            }
            catch (Exception ex)
            {
                errors.Add($"[{mod.Name}] Error parsing remote manifest from \"{mod.UpdateUrl}\": {ex.Message}");
                return(null);
            }

            List <ModManifestDiff> diff = ModManifestGenerator.Diff(remoteManifest, localManifest);

            if (diff.Count < 1 || diff.All(x => x.State == ModManifestState.Unchanged))
            {
                return(null);
            }

            string changes;

            if (!string.IsNullOrEmpty(mod.ChangelogUrl))
            {
                try
                {
                    changes = client.DownloadString(new Uri(mod.ChangelogUrl));
                }
                catch (Exception ex)
                {
                    changes = ex.Message;
                }
            }
            else
            {
                try
                {
                    changes = client.DownloadString(new Uri(new Uri(mod.UpdateUrl), "changelog.txt"));
                }
                catch
                {
                    // ignored
                    changes = string.Empty;
                }
            }

            if (!string.IsNullOrEmpty(changes))
            {
                changes = Regex.Replace(changes, "(?<!\r)\n", "\r\n");
            }

            return(new ModDownload(mod, Path.Combine("mods", folder), mod.UpdateUrl, changes, diff));
        }
Пример #3
0
        public ModDownload GetGitHubReleases(ModInfo mod, string folder,
                                             UpdaterWebClient client, List <string> errors)
        {
            List <GitHubRelease> releases;
            string url = "https://api.github.com/repos/" + mod.GitHubRepo + "/releases";

            if (!gitHubCache.ContainsKey(url))
            {
                try
                {
                    string text = client.DownloadString(url);
                    releases = JsonConvert.DeserializeObject <List <GitHubRelease> >(text)
                               .Where(x => !x.Draft && !x.PreRelease)
                               .ToList();

                    if (releases.Count > 0)
                    {
                        gitHubCache[url] = releases;
                    }
                }
                catch (Exception ex)
                {
                    errors.Add($"[{mod.Name}] Error checking for updates at {url}: {ex.Message}");
                    return(null);
                }
            }
            else
            {
                releases = gitHubCache[url];
            }

            if (releases == null || releases.Count == 0)
            {
                // No releases available.
                return(null);
            }

            string   versionPath  = Path.Combine("mods", folder, "mod.version");
            DateTime?localVersion = null;

            if (File.Exists(versionPath))
            {
                localVersion = DateTime.Parse(File.ReadAllText(versionPath).Trim());
            }
            else
            {
                var info = new FileInfo(Path.Combine("mods", folder, "mod.manifest"));
                if (info.Exists)
                {
                    localVersion = info.LastWriteTimeUtc;
                }
            }

            GitHubRelease latestRelease = null;
            GitHubAsset   latestAsset   = null;

            foreach (GitHubRelease release in releases)
            {
                GitHubAsset asset = release.Assets
                                    .FirstOrDefault(x => x.Name.Equals(mod.GitHubAsset, StringComparison.OrdinalIgnoreCase));

                if (asset == null)
                {
                    continue;
                }

                latestRelease = release;

                if (!ForceUpdate && localVersion.HasValue)
                {
                    DateTime uploaded = DateTime.Parse(asset.Uploaded);

                    if (localVersion >= uploaded)
                    {
                        // No updates available.
                        break;
                    }
                }

                latestAsset = asset;
                break;
            }

            if (latestRelease == null)
            {
                errors.Add($"[{mod.Name}] No releases with matching asset \"{mod.GitHubAsset}\" could be found in {releases.Count} release(s).");
                return(null);
            }

            if (latestAsset == null)
            {
                return(null);
            }

            string body = Regex.Replace(latestRelease.Body, "(?<!\r)\n", "\r\n");

            return(new ModDownload(mod, Path.Combine("mods", folder), latestAsset.DownloadUrl, body, latestAsset.Size)
            {
                HomePage = "https://github.com/" + mod.GitHubRepo,
                Name = latestRelease.Name,
                Version = latestRelease.TagName,
                Published = latestRelease.Published,
                Updated = latestAsset.Uploaded,
                ReleaseUrl = latestRelease.HtmlUrl
            });
        }