Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tolerate null repo URLs #4077

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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.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
Loading