Skip to content
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 DeveLanCacheUI_Backend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public static void Main(string[] args)
}
else
{
builder.Services.AddHostedService<SteamDepotEnricherHostedService>();
builder.Services.AddHostedService<SteamDepotDownloaderHostedService>();
builder.Services.AddSingleton<SteamDepotEnricherHostedService>();
builder.Services.AddSingleton<ISteamAppObtainerService, OriginalSteamAppObtainerService>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ public class SteamDepotDownloaderHostedService : BackgroundService
public IServiceProvider Services { get; }

private readonly DeveLanCacheConfiguration _deveLanCacheConfiguration;
private readonly SteamDepotEnricherHostedService _steamDepotEnricherHostedService;
private readonly ILogger<SteamDepotDownloaderHostedService> _logger;
private readonly HttpClient _httpClient;

private const string DeveLanCacheUISteamDepotFinderLatestUrl = "https://api.github.com/repos/devedse/DeveLanCacheUI_SteamDepotFinder_Runner/releases/latest";

public SteamDepotDownloaderHostedService(IServiceProvider services,
DeveLanCacheConfiguration deveLanCacheConfiguration,
SteamDepotEnricherHostedService steamDepotEnricherHostedService,
ILogger<SteamDepotDownloaderHostedService> logger)
{
Services = services;
_deveLanCacheConfiguration = deveLanCacheConfiguration;
_steamDepotEnricherHostedService = steamDepotEnricherHostedService;
_logger = logger;
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("User-Agent", "request");
Expand All @@ -40,39 +43,42 @@ private async Task GoRun(CancellationToken stoppingToken)

var depotFileDirectory = Path.Combine(deveLanCacheUIDataDirectory, "depotdir");

if (!Directory.Exists(depotFileDirectory))
if (Directory.Exists(depotFileDirectory))
{
Directory.CreateDirectory(depotFileDirectory);
// This is purely cleanup since we don't use the depot directory anymore. We just directly download and process.
Directory.Delete(depotFileDirectory, true);
Comment thread
devedse marked this conversation as resolved.
}

while (!stoppingToken.IsCancellationRequested)
{
var shouldDownload = await NewDepotFileAvailable();

_logger.LogInformation("New Depot File Available: {NewVersionAvailable}, LatestVersion: {LatestVersion}, DownloadUrl: {DownloadUrl}",
shouldDownload.NewVersionAvailable, shouldDownload.LatestVersion, shouldDownload.DownloadUrl);

if (shouldDownload.NewVersionAvailable)
{
var createdFileName = await GoDownload(depotFileDirectory, shouldDownload);
if (shouldDownload.LatestVersion == null || string.IsNullOrWhiteSpace(shouldDownload.DownloadUrl))
{
throw new UnreachableException($"New version available, but LatestVersion or DownloadUrl is null. This should not happen. LatestVersion: {shouldDownload.LatestVersion}, DownloadUrl: {shouldDownload.DownloadUrl}");
Comment thread
devedse marked this conversation as resolved.
}
var downloadedBytes = await GoDownload(depotFileDirectory, shouldDownload);

//Remove all other csv files in DepotDir except this one
var depotFiles = Directory.GetFiles(depotFileDirectory).Where(t => Path.GetExtension(t).Equals(".csv", StringComparison.OrdinalIgnoreCase) && !t.EndsWith(createdFileName, StringComparison.OrdinalIgnoreCase));
foreach (var depotFile in depotFiles)
if (downloadedBytes != null)
{
await _steamDepotEnricherHostedService.GoProcess(shouldDownload.LatestVersion, downloadedBytes, stoppingToken);
}
else
{
try
{
File.Delete(depotFile);
}
catch (Exception ex)
{
_logger.LogWarning("Could not delete depot file: {DepotFile}, exception: {Exception}", depotFile, ex);
}
_logger.LogWarning("Downloaded depot file was null, not processing further.");
}
}

await Task.Delay(TimeSpan.FromHours(1));
}
}

private async Task<string?> GoDownload(string depotFileDirectory, (bool NewVersionAvailable, Version? LatestVersion, string? DownloadUrl) shouldDownload)
private async Task<byte[]?> GoDownload(string depotFileDirectory, (bool NewVersionAvailable, Version? LatestVersion, string? DownloadUrl) shouldDownload)
{
_logger.LogInformation("Detected that new version '{NewVersionAvailable}' of Depot File is available, so downloading: {DownloadUrl}...", shouldDownload.NewVersionAvailable, shouldDownload.DownloadUrl);

Expand All @@ -84,31 +90,7 @@ private async Task GoRun(CancellationToken stoppingToken)
}

var bytes = await downloadedCsv.Content.ReadAsByteArrayAsync();

var fileName = $"depot_{shouldDownload.LatestVersion}.csv";
var filePath = Path.Combine(depotFileDirectory, fileName);
File.WriteAllBytes(filePath, bytes);

await using (var scope = Services.CreateAsyncScope())
{
using var dbContext = scope.ServiceProvider.GetRequiredService<DeveLanCacheUIDbContext>();
var foundSetting = await dbContext.Settings.FirstOrDefaultAsync();
if (foundSetting == null || foundSetting.Value == null)
{
foundSetting = new DbSetting()
{
Key = DbSetting.SettingKey_DepotVersion,
Value = shouldDownload.LatestVersion?.ToString()
};
dbContext.Settings.Add(foundSetting);
}
else
{
foundSetting.Value = shouldDownload.LatestVersion?.ToString();
}
await dbContext.SaveChangesAsync();
}
return fileName;
return bytes;
}

private async Task<(bool NewVersionAvailable, Version? LatestVersion, string? DownloadUrl)> NewDepotFileAvailable()
Expand Down Expand Up @@ -148,7 +130,7 @@ private async Task GoRun(CancellationToken stoppingToken)
await using (var scope = Services.CreateAsyncScope())
{
using var dbContext = scope.ServiceProvider.GetRequiredService<DeveLanCacheUIDbContext>();
var foundSetting = await dbContext.Settings.FirstOrDefaultAsync();
var foundSetting = await dbContext.Settings.FirstOrDefaultAsync(t => t.Key == DbSetting.SettingKey_DepotVersion);
if (foundSetting == null || foundSetting.Value == null)
{
_logger.LogInformation("Update of Depot File required because CurrentVersion could not be found");
Expand Down
Loading
Loading