Skip to content

Commit

Permalink
Added: Sort packagesList before writing to JSON to enhance determinism
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Oct 17, 2024
1 parent f5c9de1 commit e92e9d7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions source/Reloaded.Mod.Loader.Update/Index/IndexBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public Structures.Index RemoveNotInBuilder(Structures.Index index)
{
await WriteToDiskAsync(index);
var allPackages = await index.GetPackagesFromAllSourcesAsync();
allPackages.SortByIdAndThenName();
await WriteToDiskAsync(index.BaseUrl, allPackages, Routes.AllPackages);
allPackages.RemoveNonDependencyInfo();
await WriteToDiskAsync(index.BaseUrl, allPackages, Routes.AllDependencies);
Expand Down Expand Up @@ -134,6 +135,7 @@ private async Task BuildNuGetSourceAsync(Structures.Index index, IndexSourceEntr
var relativePath = Routes.Build.GetNuGetPackageListPath(indexSourceEntry.NuGetUrl!);
var path = Path.Combine(outputFolder, relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
packagesList.SortByIdAndThenName();
var bytes = Compression.Compress(JsonSerializer.SerializeToUtf8Bytes(packagesList, Serializer.Options));
await File.WriteAllBytesAsync(path, bytes);
index.Sources[Routes.Source.GetNuGetIndexKey(indexSourceEntry.NuGetUrl!)] = relativePath;
Expand All @@ -152,6 +154,7 @@ private async Task BuildGameBananaSourceAsync(Structures.Index index, IndexSourc
var relativePath = Routes.Build.GetGameBananaPackageListPath(indexSourceEntry.GameBananaId!.Value);
var fullPath = Path.Combine(outputFolder, relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(fullPath)!);
packagesList.SortByIdAndThenName();
var bytes = Compression.Compress(JsonSerializer.SerializeToUtf8Bytes(packagesList, Serializer.Options));
await File.WriteAllBytesAsync(fullPath, bytes);
index.Sources[Routes.Source.GetGameBananaIndex(indexSourceEntry.GameBananaId!.Value)] = relativePath;
Expand Down
11 changes: 11 additions & 0 deletions source/Reloaded.Mod.Loader.Update/Index/Structures/PackageList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public static PackageList Create()
};
}

/// <summary/>
public void SortByIdAndThenName()
{
Packages.Sort((x, y) =>
{
var nameComparison = string.Compare(x.Name, y.Name, StringComparison.Ordinal);
// If names are the same, compare by Id, otherwise by name.
return nameComparison == 0 ? string.Compare(x.Id, y.Id, StringComparison.Ordinal) : nameComparison;
});
}

/// <summary>
/// Removes the info that is not needed for dependency resolution.
/// </summary>
Expand Down

0 comments on commit e92e9d7

Please sign in to comment.