Skip to content

Commit

Permalink
Refactoring the download logic so that it is slightly easier to read.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpill90 committed May 26, 2024
1 parent 4b9556d commit c3ea575
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 116 deletions.
2 changes: 1 addition & 1 deletion BattleNetPrefill.Integration.Test/LogFileUpToDateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void LogFilesAreUpToDate(string productCode)
private static VersionsEntry GetLatestCdnVersion(TactProduct product)
{
// Finding the latest version of the game
ConfigFileHandler configFileHandler = new ConfigFileHandler(new CdnRequestManager(AppConfig.BattleNetPatchUri, new TestConsole()));
ConfigFileHandler configFileHandler = new ConfigFileHandler(new CdnRequestManager(new TestConsole()));
VersionsEntry cdnVersion = configFileHandler.GetLatestVersionEntryAsync(product).Result;
return cdnVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task BuildConfig_ShouldHaveNoUnknownKeyPairs(string productCode)

// Setting up required classes
AppConfig.SkipDownloads = true;
CdnRequestManager cdnRequestManager = new CdnRequestManager(AppConfig.BattleNetPatchUri, new TestConsole());
CdnRequestManager cdnRequestManager = new CdnRequestManager(new TestConsole());
await cdnRequestManager.InitializeAsync(tactProduct);
var configFileHandler = new ConfigFileHandler(cdnRequestManager);
VersionsEntry targetVersion = await configFileHandler.GetLatestVersionEntryAsync(tactProduct);
Expand Down
12 changes: 3 additions & 9 deletions BattleNetPrefill/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@ static AppConfig()

public static readonly string UserSelectedAppsPath = Path.Combine(ConfigDir, "selectedAppsToPrefill.json");

//TODO comment
private static bool _verboseLogs;
public static bool VerboseLogs
{
get => _verboseLogs;
set
{
_verboseLogs = value;
AnsiConsoleExtensions.WriteVerboseLogs = value;
}
get => AnsiConsoleExtensions.WriteVerboseLogs;
set => AnsiConsoleExtensions.WriteVerboseLogs = value;
}

/// <summary>
Expand All @@ -56,7 +50,7 @@ public static bool VerboseLogs
public static readonly string LogFileBasePath = @$"{DirectorySearch.TryGetSolutionDirectory()}/Logs";

/// <summary>
/// /// When enabled, will skip using any locally cached index files from disk.
/// When enabled, will skip using any locally cached index files from disk.
/// The disk cache can speed up repeated runs, however it can use up a non-trivial amount
/// of storage in some cases (Wow uses several hundred mb of index files). Intended for debugging.
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion BattleNetPrefill/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ private static List<string> ParseHiddenFlags()
// Have to skip the first argument, since its the path to the executable
var args = Environment.GetCommandLineArgs().Skip(1).ToList();

// TODO comment
if (args.Any(e => e.Contains("--compare-requests")))
{
AnsiConsole.Console.LogMarkupLine($"Using {LightYellow("--compare-requests")} flag. Running comparison logic...");
Expand Down
8 changes: 4 additions & 4 deletions BattleNetPrefill/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
"commandName": "Project",
"commandLineArgs": "prefill --all --force"
},
"Prefill All - No Download": {
"Prefill Test Products - No Download": {
"commandName": "Project",
"commandLineArgs": "prefill --all --no-download --force"
"commandLineArgs": "prefill -p s1 s2 pro wow wow_classic --no-download --force"
},
"Prefill All - No Download - No Cache": {
"commandName": "Project",
"commandLineArgs": "prefill --all --no-download --nocache --force"
},
"Prefill Starcraft 1": {
"commandName": "Project",
"commandLineArgs": "prefill -p s1 s2 pro wow --no-download --force"
"commandLineArgs": "prefill -p s1 --force"
},
"Prefill Overwatch": {
"commandName": "Project",
"commandLineArgs": "prefill -p pro --force"
},
"Prefill WOW - Compare Requests": {
"commandName": "Project",
"commandLineArgs": "prefill -p wow_classic --compare-requests --no-download --force"
"commandLineArgs": "prefill -p wow --compare-requests --no-download --force"
},
"Select Apps": {
"commandName": "Project",
Expand Down
10 changes: 9 additions & 1 deletion BattleNetPrefill/TactProductHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ public async Task ProcessMultipleProductsAsync(List<TactProduct> productsToProce
/// </summary>
public async Task<ComparisonResult> ProcessProductAsync(TactProduct product)
{
//TODO it would be nice if this was only displayed when there is something to download
_ansiConsole.LogMarkupLine($"Starting {Cyan(product.DisplayName)}");

var metadataTimer = Stopwatch.StartNew();

// Initializing classes, now that we have our CDN info loaded
using var cdnRequestManager = new CdnRequestManager(AppConfig.BattleNetPatchUri, _ansiConsole, AppConfig.NoLocalCache);
using var cdnRequestManager = new CdnRequestManager(_ansiConsole);
var downloadFileHandler = new DownloadFileHandler(cdnRequestManager);
var configFileHandler = new ConfigFileHandler(cdnRequestManager);
var installFileHandler = new InstallFileHandler(cdnRequestManager);
Expand Down Expand Up @@ -87,6 +88,13 @@ await Task.WhenAll(archiveIndexHandler.BuildArchiveIndexesAsync(cdnConfig, ctx),

_ansiConsole.LogMarkupLine("Retrieved product metadata", metadataTimer);

//TODO this breaks things so that it no longer correctly shows the total amount of bytes downloaded
if (AppConfig.SkipDownloads)
{
_ansiConsole.MarkupLine("");
return new ComparisonResult();
}

// Actually start the download of any deferred requests
var downloadSuccessful = await cdnRequestManager.DownloadQueuedRequestsAsync(_prefillSummaryResult);
if (downloadSuccessful)
Expand Down
25 changes: 9 additions & 16 deletions BattleNetPrefill/Utils/Debug/Models/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
//TODO probably shouldn't be in debug namespace
/// <summary>
/// Model that represents a request that could be made to a CDN.
/// Model that represents a request that could be made to a CDN.
/// </summary>
public sealed class Request
{
Expand All @@ -18,7 +18,6 @@ public Request(string productRootUri, RootFolder rootFolder, MD5Hash cdnKey, lon
RootFolder = rootFolder;
CdnKey = cdnKey;
IsIndex = isIndex;
WriteToDevNull = writeToDevNull;

if (startBytes != null && endBytes != null)
{
Expand Down Expand Up @@ -47,32 +46,26 @@ public Request(string productRootUri, RootFolder rootFolder, MD5Hash cdnKey, lon
public long LowerByteRange { get; set; }
public long UpperByteRange { get; set; }

//TODO this name kind of sucks
public bool WriteToDevNull { get; set; }

// Bytes are an inclusive range. Ex bytes 0->9 == 10 bytes
public long TotalBytes => (UpperByteRange - LowerByteRange) + 1;

/// <summary>
/// Request path, without a host name. Agnostic towards host name, since we will combine with it later if needed to make a real request.
/// Example :
/// tpr/sc1live/data/b5/20/b520b25e5d4b5627025aeba235d60708
/// tpr/sc1live/data/b5/20/b520b25e5d4b5627025a6ba235d60708
/// </summary>
private string _uri;
public string Uri
{
get
{
if (_uri == null)
var hashId = CdnKey.ToStringLower();
var uri = $"{ProductRootUri}/{RootFolder.Name}/{hashId.Substring(0, 2)}/{hashId.Substring(2, 2)}/{hashId}";

if (IsIndex)
{
var hashId = CdnKey.ToStringLower();
_uri = $"{ProductRootUri}/{RootFolder.Name}/{hashId.Substring(0, 2)}/{hashId.Substring(2, 2)}/{hashId}";
if (IsIndex)
{
_uri = $"{_uri}.index";
}
return $"{uri}.index";
}
return _uri;
return uri;
}
}

Expand All @@ -92,7 +85,7 @@ public bool Overlaps(Request request2, bool isBattleNetClient)
int overlap = 1;
if (isBattleNetClient)
{
// For some reason, the real Battle.Net client seems to combine requests if their ranges are within 4kb of each other.
// For some reason, the real Battle.Net client seems to combine requests if their ranges are within 4kb of each other.
// This does not make intuitive sense, as looking at the entries in the Archive Index shows that the range should not be requested,
// ex. only bytes 0-340 and bytes 1400-2650 should be individually requested. However these two individual requests get combined into 0-2650
overlap = 4096;
Expand Down
4 changes: 2 additions & 2 deletions BattleNetPrefill/Utils/Debug/RequestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static List<Request> CoalesceRequests(List<Request> initialRequests, bool
{
var coalesced = new List<Request>();

// Coalescing any requests to the same URI that have sequential/overlapping byte ranges.
// Coalescing any requests to the same URI that have sequential/overlapping byte ranges.
var requestsGroupedByUri = initialRequests.GroupBy(e => new { e.RootFolder, e.CdnKey, e.IsIndex }).ToList();
foreach (var grouping in requestsGroupedByUri)
{
Expand All @@ -32,7 +32,7 @@ public static List<Request> CoalesceRequests(Dictionary<MD5Hash, List<Request>>
{
var coalesced = new List<Request>();

// Coalescing any requests to the same URI that have sequential/overlapping byte ranges.
// Coalescing any requests to the same URI that have sequential/overlapping byte ranges.
foreach (var grouping in initialRequests.Values)
{
grouping.Sort((x, y) => x.LowerByteRange.CompareTo(y.LowerByteRange));
Expand Down
Loading

0 comments on commit c3ea575

Please sign in to comment.