-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApplication.cs
More file actions
141 lines (117 loc) · 5.06 KB
/
Copy pathApplication.cs
File metadata and controls
141 lines (117 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using BFGDL.NET.Models;
using BFGDL.NET.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace BFGDL.NET;
public sealed class Application(
IServiceProvider serviceProvider,
AppConfiguration appConfiguration,
IBigFishGamesClient apiClient,
IDownloadService downloadService,
ILogger<Application> logger)
{
public async Task RunAsync(CommandLineOptions options)
{
Console.WriteLine("Big Fish Games Downloader - .NET");
if (options.CacheCatalog)
{
var fetcher = serviceProvider.GetRequiredService<CatalogFetchService>();
var cache = serviceProvider.GetRequiredService<CatalogCache>();
await cache.MigrateAsync(CancellationToken.None);
var languages = options.AllLanguages
? CatalogFetchService.SupportedLanguages
: (IReadOnlyList<Language>)[appConfiguration.Language];
Console.WriteLine(options.AllLanguages
? $"Caching full catalog for {appConfiguration.Platform} in all {languages.Count} languages..."
: $"Caching full catalog for {appConfiguration.Platform} / {appConfiguration.Language}...");
await fetcher.FetchAllAsync(
appConfiguration.Platform, languages,
concurrencyLevel: options.CacheConcurrency,
ct: CancellationToken.None);
Console.WriteLine("[OK] Catalog cache complete.");
return;
}
if (options.ExportInstallersJson is not null)
{
var jobs = options.MaxConcurrentDownloads;
var exporter = serviceProvider.GetRequiredService<InstallerListExporter>();
Console.WriteLine(
$"Exporting installer lists to JSON ({options.ExportInstallersJson}) for {appConfiguration.Platform}...");
await exporter.ExportAsync(options.ExportInstallersJson, jobs, options.ExportLimit, CancellationToken.None);
Console.WriteLine("[OK] Installer JSON export completed.");
return;
}
// Get WrapIDs
var wrapIds = await GetWrapIdsAsync(options);
if (wrapIds.Count == 0)
{
Console.Error.WriteLine("No WrapIDs found. Use -h for help.");
return;
}
Console.WriteLine($"Processing {wrapIds.Count} game(s)...");
// Fetch game information
var games = new List<GameInfo>();
foreach (var wrapId in wrapIds)
try
{
var game = await apiClient.GetGameInfoAsync(wrapId);
games.Add(game);
}
catch (Exception ex)
{
if (logger.IsEnabled(LogLevel.Error))
logger.LogError(ex, "Failed to fetch game info for WrapID: {WrapId}", wrapId);
Console.Error.WriteLine($"Failed to fetch info for {wrapId}: {ex.Message}");
}
if (games.Count == 0)
{
Console.Error.WriteLine("No valid games found.");
return;
}
// Download or generate list
if (options.Download)
await DownloadGamesAsync(games);
else
await GenerateDownloadListAsync(games);
Console.WriteLine("[OK] Operation completed!");
}
private async Task<List<string>> GetWrapIdsAsync(CommandLineOptions options)
{
// Priority 1: Fetch from installers
if (options.FetchFromInstallers)
{
Console.WriteLine("Scanning for installers...");
var installerFetcher = serviceProvider.GetRequiredService<InstallerWrapIdFetcher>();
var wrapIds = await installerFetcher.FetchWrapIdsAsync(int.MaxValue);
return [.. wrapIds];
}
// Priority 2: Use provided WrapIDs
if (options.WrapIds.Count > 0) return options.WrapIds;
Console.Error.WriteLine(
"No WrapIDs specified. Use -e to scan installers or provide WrapIDs as arguments.");
return [];
}
private async Task DownloadGamesAsync(List<GameInfo> games)
{
Console.WriteLine($"Starting downloads with {games.Sum(g => g.Segments.Count)} segments...");
foreach (var game in games)
try
{
await downloadService.DownloadGameAsync(game, progress: null);
}
catch (Exception ex)
{
if (logger.IsEnabled(LogLevel.Error))
logger.LogError(ex, "Failed to download game: {GameName}", game.Name);
Console.Error.WriteLine($"Failed to download {game.Name}: {ex.Message}");
}
}
private async Task GenerateDownloadListAsync(List<GameInfo> games)
{
var downloadList = await downloadService.GenerateDownloadListAsync(games);
var outputFile = Path.Combine(AppContext.BaseDirectory, "download-list.txt");
await File.WriteAllTextAsync(outputFile, downloadList);
Console.WriteLine($"[OK] Download list saved to: {outputFile}");
Console.WriteLine($"Use with: BFGDL.NET -d $(cat {outputFile})");
}
}