From 6ed3a145c501a1c9f453d1ee7111f22822936af8 Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Fri, 12 Apr 2024 11:25:59 -0500 Subject: [PATCH 1/2] Tolerate null repo URLs --- Core/Net/NetFileCache.cs | 2 +- Core/Repositories/Repository.cs | 2 +- Core/Repositories/RepositoryDataManager.cs | 15 ++++++++------- GUI/Dialogs/SettingsDialog.cs | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Core/Net/NetFileCache.cs b/Core/Net/NetFileCache.cs index 614fe1e04e..98097b979a 100644 --- a/Core/Net/NetFileCache.cs +++ b/Core/Net/NetFileCache.cs @@ -555,7 +555,7 @@ public static string CreateURLHash(Uri url) { using (SHA1 sha1 = SHA1.Create()) { - byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(url.ToString())); + byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(url?.ToString() ?? "")); return BitConverter.ToString(hash).Replace("-", "").Substring(0, 8); } diff --git a/Core/Repositories/Repository.cs b/Core/Repositories/Repository.cs index 8dd85c5f5c..6e449599b2 100644 --- a/Core/Repositories/Repository.cs +++ b/Core/Repositories/Repository.cs @@ -49,7 +49,7 @@ public bool Equals(Repository other) => other != null && uri == other.uri; public override int GetHashCode() - => uri.GetHashCode(); + => uri?.GetHashCode() ?? 0; public override string ToString() => string.Format("{0} ({1}, {2})", name, priority, uri); diff --git a/Core/Repositories/RepositoryDataManager.cs b/Core/Repositories/RepositoryDataManager.cs index 3b014e3ed2..8ad0ea4167 100644 --- a/Core/Repositories/RepositoryDataManager.cs +++ b/Core/Repositories/RepositoryDataManager.cs @@ -91,7 +91,7 @@ public IEnumerable GetAllAvailableModules(IEnumerable repos, IProgress percentProgress) { // Look up the sizes of repos that have uncached files - var reposAndSizes = repos.Where(r => !repositoriesData.ContainsKey(r)) + var reposAndSizes = repos.Where(r => r.uri != null && !repositoriesData.ContainsKey(r)) .Select(r => new Tuple(r, GetRepoDataPath(r))) .Where(tuple => File.Exists(tuple.Item2)) .Select(tuple => new Tuple(tuple.Item1, @@ -142,11 +142,12 @@ public UpdateResult Update(Repository[] repos, user.RaiseProgress(Properties.Resources.NetRepoCheckingForUpdates, 0); var toUpdate = repos.DefaultIfEmpty(new Repository("default", game.DefaultRepositoryURL)) .DistinctBy(r => r.uri) - .Where(r => r.uri.IsFile - || skipETags - || (!etags.TryGetValue(r.uri, out string etag) - || !File.Exists(GetRepoDataPath(r)) - || etag != Net.CurrentETag(r.uri))) + .Where(r => r.uri != null + && (r.uri.IsFile + || skipETags + || (!etags.TryGetValue(r.uri, out string etag) + || !File.Exists(GetRepoDataPath(r)) + || etag != Net.CurrentETag(r.uri)))) .ToArray(); if (toUpdate.Length < 1) { @@ -300,7 +301,7 @@ private IEnumerable GetRepoDatas(IEnumerable repos) new Dictionary(); private string GetRepoDataPath(Repository repo) - => GetRepoDataPath(repo, NetFileCache.CreateURLHash(repo.uri)); + => GetRepoDataPath(repo, NetFileCache.CreateURLHash(repo?.uri)); private string GetRepoDataPath(Repository repo, string hash) => Directory.EnumerateFiles(reposDir) diff --git a/GUI/Dialogs/SettingsDialog.cs b/GUI/Dialogs/SettingsDialog.cs index 5cadd1215d..dc6dc74a28 100644 --- a/GUI/Dialogs/SettingsDialog.cs +++ b/GUI/Dialogs/SettingsDialog.cs @@ -134,7 +134,7 @@ private void RefreshReposListBox(bool saveChanges = true) .OrderBy(r => r.priority) .Select(r => new ListViewItem(new string[] { - r.name, r.uri.ToString(), + r.name, r.uri?.ToString() ?? "", }) { Tag = r, From 1d117495b35c67d6d2b87a4f70cff3f63f84d2da Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Fri, 12 Apr 2024 11:36:29 -0500 Subject: [PATCH 2/2] Better fallback default repo name --- Core/Repositories/RepositoryDataManager.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Core/Repositories/RepositoryDataManager.cs b/Core/Repositories/RepositoryDataManager.cs index 8ad0ea4167..4e76e83633 100644 --- a/Core/Repositories/RepositoryDataManager.cs +++ b/Core/Repositories/RepositoryDataManager.cs @@ -140,7 +140,8 @@ public UpdateResult Update(Repository[] repos, // Check if any ETags have changed, quit if not user.RaiseProgress(Properties.Resources.NetRepoCheckingForUpdates, 0); - var toUpdate = repos.DefaultIfEmpty(new Repository("default", game.DefaultRepositoryURL)) + var toUpdate = repos.DefaultIfEmpty(new Repository($"{game.ShortName}-{Repository.default_ckan_repo_name}", + game.DefaultRepositoryURL)) .DistinctBy(r => r.uri) .Where(r => r.uri != null && (r.uri.IsFile