@@ -7,17 +7,20 @@ public class SteamDepotDownloaderHostedService : BackgroundService
77 public IServiceProvider Services { get ; }
88
99 private readonly DeveLanCacheConfiguration _deveLanCacheConfiguration ;
10+ private readonly SteamDepotEnricherHostedService _steamDepotEnricherHostedService ;
1011 private readonly ILogger < SteamDepotDownloaderHostedService > _logger ;
1112 private readonly HttpClient _httpClient ;
1213
1314 private const string DeveLanCacheUISteamDepotFinderLatestUrl = "https://api.github.com/repos/devedse/DeveLanCacheUI_SteamDepotFinder_Runner/releases/latest" ;
1415
1516 public SteamDepotDownloaderHostedService ( IServiceProvider services ,
1617 DeveLanCacheConfiguration deveLanCacheConfiguration ,
18+ SteamDepotEnricherHostedService steamDepotEnricherHostedService ,
1719 ILogger < SteamDepotDownloaderHostedService > logger )
1820 {
1921 Services = services ;
2022 _deveLanCacheConfiguration = deveLanCacheConfiguration ;
23+ _steamDepotEnricherHostedService = steamDepotEnricherHostedService ;
2124 _logger = logger ;
2225 _httpClient = new HttpClient ( ) ;
2326 _httpClient . DefaultRequestHeaders . Add ( "User-Agent" , "request" ) ;
@@ -40,39 +43,42 @@ private async Task GoRun(CancellationToken stoppingToken)
4043
4144 var depotFileDirectory = Path . Combine ( deveLanCacheUIDataDirectory , "depotdir" ) ;
4245
43- if ( ! Directory . Exists ( depotFileDirectory ) )
46+ if ( Directory . Exists ( depotFileDirectory ) )
4447 {
45- Directory . CreateDirectory ( depotFileDirectory ) ;
48+ // This is purely cleanup since we don't use the depot directory anymore. We just directly download and process.
49+ Directory . Delete ( depotFileDirectory , true ) ;
4650 }
4751
4852 while ( ! stoppingToken . IsCancellationRequested )
4953 {
5054 var shouldDownload = await NewDepotFileAvailable ( ) ;
5155
56+ _logger . LogInformation ( "New Depot File Available: {NewVersionAvailable}, LatestVersion: {LatestVersion}, DownloadUrl: {DownloadUrl}" ,
57+ shouldDownload . NewVersionAvailable , shouldDownload . LatestVersion , shouldDownload . DownloadUrl ) ;
58+
5259 if ( shouldDownload . NewVersionAvailable )
5360 {
54- var createdFileName = await GoDownload ( depotFileDirectory , shouldDownload ) ;
61+ if ( shouldDownload . LatestVersion == null || string . IsNullOrWhiteSpace ( shouldDownload . DownloadUrl ) )
62+ {
63+ throw new UnreachableException ( $ "New version available, but LatestVersion or DownloadUrl is null. This should not happen. LatestVersion: { shouldDownload . LatestVersion } , DownloadUrl: { shouldDownload . DownloadUrl } ") ;
64+ }
65+ var downloadedBytes = await GoDownload ( depotFileDirectory , shouldDownload ) ;
5566
56- //Remove all other csv files in DepotDir except this one
57- var depotFiles = Directory . GetFiles ( depotFileDirectory ) . Where ( t => Path . GetExtension ( t ) . Equals ( ".csv" , StringComparison . OrdinalIgnoreCase ) && ! t . EndsWith ( createdFileName , StringComparison . OrdinalIgnoreCase ) ) ;
58- foreach ( var depotFile in depotFiles )
67+ if ( downloadedBytes != null )
68+ {
69+ await _steamDepotEnricherHostedService . GoProcess ( shouldDownload . LatestVersion , downloadedBytes , stoppingToken ) ;
70+ }
71+ else
5972 {
60- try
61- {
62- File . Delete ( depotFile ) ;
63- }
64- catch ( Exception ex )
65- {
66- _logger . LogWarning ( "Could not delete depot file: {DepotFile}, exception: {Exception}" , depotFile , ex ) ;
67- }
73+ _logger . LogWarning ( "Downloaded depot file was null, not processing further." ) ;
6874 }
6975 }
7076
7177 await Task . Delay ( TimeSpan . FromHours ( 1 ) ) ;
7278 }
7379 }
7480
75- private async Task < string ? > GoDownload ( string depotFileDirectory , ( bool NewVersionAvailable , Version ? LatestVersion , string ? DownloadUrl ) shouldDownload )
81+ private async Task < byte [ ] ? > GoDownload ( string depotFileDirectory , ( bool NewVersionAvailable , Version ? LatestVersion , string ? DownloadUrl ) shouldDownload )
7682 {
7783 _logger . LogInformation ( "Detected that new version '{NewVersionAvailable}' of Depot File is available, so downloading: {DownloadUrl}..." , shouldDownload . NewVersionAvailable , shouldDownload . DownloadUrl ) ;
7884
@@ -84,31 +90,7 @@ private async Task GoRun(CancellationToken stoppingToken)
8490 }
8591
8692 var bytes = await downloadedCsv . Content . ReadAsByteArrayAsync ( ) ;
87-
88- var fileName = $ "depot_{ shouldDownload . LatestVersion } .csv";
89- var filePath = Path . Combine ( depotFileDirectory , fileName ) ;
90- File . WriteAllBytes ( filePath , bytes ) ;
91-
92- await using ( var scope = Services . CreateAsyncScope ( ) )
93- {
94- using var dbContext = scope . ServiceProvider . GetRequiredService < DeveLanCacheUIDbContext > ( ) ;
95- var foundSetting = await dbContext . Settings . FirstOrDefaultAsync ( ) ;
96- if ( foundSetting == null || foundSetting . Value == null )
97- {
98- foundSetting = new DbSetting ( )
99- {
100- Key = DbSetting . SettingKey_DepotVersion ,
101- Value = shouldDownload . LatestVersion ? . ToString ( )
102- } ;
103- dbContext . Settings . Add ( foundSetting ) ;
104- }
105- else
106- {
107- foundSetting . Value = shouldDownload . LatestVersion ? . ToString ( ) ;
108- }
109- await dbContext . SaveChangesAsync ( ) ;
110- }
111- return fileName ;
93+ return bytes ;
11294 }
11395
11496 private async Task < ( bool NewVersionAvailable , Version ? LatestVersion , string ? DownloadUrl ) > NewDepotFileAvailable ( )
@@ -148,7 +130,7 @@ private async Task GoRun(CancellationToken stoppingToken)
148130 await using ( var scope = Services . CreateAsyncScope ( ) )
149131 {
150132 using var dbContext = scope . ServiceProvider . GetRequiredService < DeveLanCacheUIDbContext > ( ) ;
151- var foundSetting = await dbContext . Settings . FirstOrDefaultAsync ( ) ;
133+ var foundSetting = await dbContext . Settings . FirstOrDefaultAsync ( t => t . Key == DbSetting . SettingKey_DepotVersion ) ;
152134 if ( foundSetting == null || foundSetting . Value == null )
153135 {
154136 _logger . LogInformation ( "Update of Depot File required because CurrentVersion could not be found" ) ;
0 commit comments