Skip to content

Commit

Permalink
Merge #4077 Tolerate null repo URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Apr 12, 2024
2 parents 0e0760c + 1d11749 commit 4de7a79
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ All notable changes to this project will be documented in this file.
- [GUI] Use better console hiding API (#4051 by: HebaruSan)
- [Core] Trigger progress updates for frozen downloads (#4052 by: HebaruSan)
- [GUI] Fix NRE on trying to update all when there's nothing to update (#4054 by: HebaruSan)
- [Core] Tolerate null repo URLs (#4077 by: HebaruSan)

### Internal

Expand Down
2 changes: 1 addition & 1 deletion Core/Net/NetFileCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion Core/Repositories/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 10 additions & 8 deletions Core/Repositories/RepositoryDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public IEnumerable<AvailableModule> GetAllAvailableModules(IEnumerable<Repositor
public void Prepopulate(List<Repository> repos, IProgress<int> 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<Repository, string>(r, GetRepoDataPath(r)))
.Where(tuple => File.Exists(tuple.Item2))
.Select(tuple => new Tuple<Repository, long>(tuple.Item1,
Expand Down Expand Up @@ -140,13 +140,15 @@ public enum UpdateResult

// 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.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)
{
Expand Down Expand Up @@ -300,7 +302,7 @@ private IEnumerable<RepositoryData> GetRepoDatas(IEnumerable<Repository> repos)
new Dictionary<Repository, RepositoryData>();

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)
Expand Down
2 changes: 1 addition & 1 deletion GUI/Dialogs/SettingsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4de7a79

Please sign in to comment.