Skip to content

Commit

Permalink
Steam downloader fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Equinox- committed Jul 28, 2023
1 parent 3a91d20 commit 503b4c6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
39 changes: 28 additions & 11 deletions Meds.Watchdog/Steam/CdnPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,44 @@ private async Task RefreshServers()
var servers = await ContentServerDirectoryService
.LoadAsync(_client.Configuration, _cellId, CancellationToken.None)
.ConfigureAwait(false);
foreach (var server in servers)
if (!server.UseAsProxy && !server.SteamChinaOnly)
_servers.Add(server);
_log.ZLogInformation($"Got {_servers.Count} CDN servers.");
SortServers();
lock (_servers)
{
_servers.Clear();
foreach (var server in servers)
if (!server.UseAsProxy && !server.SteamChinaOnly)
_servers.Add(server);
_log.ZLogInformation($"Got {_servers.Count} CDN servers.");
SortServers();
}
}

public async Task<Server> TakeServer()
{
if (_servers.Count == 0)
bool refresh;
lock (_servers)
{
refresh = _servers.Count == 0;
}

if (refresh)
await RefreshServers();
var server = _servers[0];
_servers.RemoveAt(0);
_log.ZLogInformation("Using CDN server {0}", server.Host);
Server server;
lock (_servers)
{
server = _servers[0];
_servers.RemoveAt(0);
}

return server;
}

public void ReturnServer(Server server)
{
_servers.Add(server);
SortServers();
lock (_servers)
{
_servers.Add(server);
SortServers();
}
}
}
}
29 changes: 23 additions & 6 deletions Meds.Watchdog/Steam/InstallJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
using System.Threading.Tasks;
using Meds.Dist;
using Meds.Watchdog.Utils;
using Microsoft.Extensions.Logging;
using ProtoBuf;
using SteamKit2;
using SteamKit2.CDN;
using ZLogger;

namespace Meds.Watchdog.Steam
{
public class InstallJob
{
private readonly ILogger _log;
private readonly List<FileParts> _fileParts = new List<FileParts>();
private readonly ConcurrentStack<ChunkWorkItem> _neededChunks = new ConcurrentStack<ChunkWorkItem>();
private long _finishedChunks;
Expand All @@ -27,6 +30,11 @@ public class InstallJob
private DistFileCache _cache;
private SteamDownloader _downloader;

private InstallJob(ILogger log)
{
_log = log;
}

public float ProgressRatio => Interlocked.Read(ref _finishedChunks) / (float)_totalChunks;

public async Task Execute(SteamDownloader downloader, int workerCount = 8, Action<string> stateCallback = null)
Expand Down Expand Up @@ -93,19 +101,27 @@ private async Task StartWorkerAsync()

_downloader.CdnPool.ReturnServer(server);
}
catch
catch (Exception err)
{
_log.ZLogWarning(
err,
"Failed to download chunk path={0} size={1}. Abandoning CDN server {2}",
workItem.Owner.InstallRelativePath,
workItem.ChunkData.UncompressedLength,
server.Host);
_neededChunks.Push(workItem);
}

_downloader.CdnPool.ReturnClient(client);
}
}

public static InstallJob Upgrade(uint appId, uint depotId, string installPath, DistFileCache localFiles, DepotManifest remoteFiles,
public static InstallJob Upgrade(
ILogger log,
uint appId, uint depotId, string installPath, DistFileCache localFiles, DepotManifest remoteFiles,
Predicate<string> installFilter, HashSet<string> installed, string installPrefix)
{
var job = new InstallJob
var job = new InstallJob(log)
{
_cache = localFiles,
_basePath = installPath,
Expand Down Expand Up @@ -157,7 +173,6 @@ public static InstallJob Upgrade(uint appId, uint depotId, string installPath, D

private class FileParts
{
private readonly string _installRelPath;
private readonly System.IO.FileInfo _destPath;
private readonly DepotManifest.FileData _fileData;
private ConcurrentBag<DepotChunk> _completeChunks;
Expand All @@ -166,20 +181,22 @@ private class FileParts
private bool _started;
private Stopwatch _sw = new Stopwatch();

public string InstallRelativePath { get; }

public bool IsComplete { get; private set; }

public (string relPath, byte[] hash, ulong totalSize, DateTime completionTime) GetCacheDetails()
{
if (!IsComplete)
throw new InvalidOperationException("File is not complete!");

return (_installRelPath, _fileData.FileHash, _fileData.TotalSize, _completionTime);
return (InstallRelativePath, _fileData.FileHash, _fileData.TotalSize, _completionTime);
}

public FileParts(DepotManifest.FileData fileData, string installRelPath, string installBasePath)
{
_fileData = fileData;
_installRelPath = installRelPath;
InstallRelativePath = installRelPath;
_destPath = new System.IO.FileInfo(Path.Combine(installBasePath, installRelPath));
_completeChunks = new ConcurrentBag<DepotChunk>();

Expand Down
2 changes: 1 addition & 1 deletion Meds.Watchdog/Steam/SteamDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ private async Task<InstallResult> InstallInternalAsync(uint appId, uint depotId,
// Get installation details from Steam
var manifest = await GetManifestAsync(appId, depotId, manifestId, branch, branchPasswordHash);

var job = InstallJob.Upgrade(appId, depotId, installPath, localCache, manifest, installFilter, result.InstalledFiles, installPrefix);
var job = InstallJob.Upgrade(_log, appId, depotId, installPath, localCache, manifest, installFilter, result.InstalledFiles, installPrefix);
using (var timer = new Timer(3000) { AutoReset = true })
{
timer.Elapsed += (sender, args) => _log.ZLogInformation($"{debugName} progress: {job.ProgressRatio:0.00%}");
Expand Down

0 comments on commit 503b4c6

Please sign in to comment.