-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
192 lines (156 loc) · 5.64 KB
/
Program.cs
File metadata and controls
192 lines (156 loc) · 5.64 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using HeroicDedupe.Models;
using HeroicDedupe.Readers;
using HeroicDedupe.Services;
using Microsoft.Extensions.Configuration;
// Top-level statements for .NET 10 modern entry point
var logger = ConsoleLogger.Instance;
logger.WriteLine("=== Heroic Library Deduplicator (v1.0) ===");
// 1. Load Configuration
var config = LoadConfiguration(args);
if (!ValidateConfiguration(config, logger))
return 1;
ResolvePaths(config);
logger.WriteLine($"Mode: {(config.DryRun ? "[TEST] DRY RUN (Safe Mode)" : "[LIVE] (Write Mode)")}");
logger.WriteLine($"Priority: {string.Join(" > ", config.GetPriorityOrDefault())}");
// 2. Check Process State (only needed for live mode - dry run doesn't write to disk)
HeroicProcessManager? processManager = null;
if (!config.DryRun)
{
processManager = new HeroicProcessManager(logger);
if (processManager.IsHeroicRunning() && !processManager.RequestClose())
{
logger.WriteLine("Exiting to prevent data corruption.");
return 1;
}
}
// 3. Read Libraries
List<IGameStoreReader> readers =
[
new LegendaryReader(config.LegendaryLibraryPath, logger),
new GogReader(config.GogLibraryPath, logger),
new NileReader(config.NileLibraryPath, logger)
];
List<LocalGame> allGames = [];
foreach (var reader in readers)
{
logger.Write($"Reading {reader.StoreType,-8}... ");
var games = await reader.ReadLibraryAsync();
if (games.Count > 0)
logger.WriteLine($"{games.Count} games found.", ConsoleColor.Cyan);
else
logger.WriteLine("None / File missing.", ConsoleColor.DarkGray);
allGames.AddRange(games);
}
// 4. Optional: Enrich with IGDB metadata
allGames = await EnrichWithMetadataAsync(allGames, config, logger);
// 5. Deduplicate
var deduper = new DeduplicationService(config, logger);
var gamesToHide = deduper.IdentifyDuplicates(allGames);
logger.WriteLine($"\nTotal Duplicates found to hide: {gamesToHide.Count}");
// 6. Apply Changes
if (gamesToHide.Count > 0)
{
var modifier = new HeroicConfigModifier(config, logger);
try
{
await modifier.ApplyHiddenGamesAsync(gamesToHide);
}
catch (Exception ex)
{
logger.WriteLine($"CRITICAL ERROR: {ex.Message}", ConsoleColor.Red);
return 1;
}
}
else
{
logger.WriteLine("Your library is already clean!");
}
// 7. Restart Heroic if we closed it
processManager?.RestartHeroic();
logger.WriteLine("\nPress any key to exit.");
Console.ReadKey();
return 0;
// --- HELPER FUNCTIONS ---
static async Task<List<LocalGame>> EnrichWithMetadataAsync(List<LocalGame> games, AppConfig config,
IConsoleLogger logger)
{
// Skip if IGDB not configured
if (config.Igdb is not { IsConfigured: true })
{
logger.WriteLine("[IGDB] Not configured - using local metadata only.", ConsoleColor.DarkGray);
return games;
}
// Create cache and provider with dependency injection
var cachePath = Path.Combine(AppContext.BaseDirectory, "igdb_cache.json");
var cache = new FileReleaseDateCache(cachePath, logger);
if (config.RefreshCache)
{
logger.WriteLine("[IGDB] --refresh-cache: clearing cached metadata...", ConsoleColor.Cyan);
cache.Clear();
}
using var provider = new IgdbService(config.Igdb, cache, logger);
return await provider.EnrichGamesAsync(games);
}
static AppConfig LoadConfiguration(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", false, false)
.AddCommandLine(args);
var cfg = new AppConfig();
builder.Build().Bind(cfg);
// Handle CLI overrides
foreach (var arg in args)
if (arg.Equals("--dry-run", StringComparison.OrdinalIgnoreCase))
cfg.DryRun = true;
else if (arg.Equals("--live", StringComparison.OrdinalIgnoreCase))
cfg.DryRun = false;
else if (arg.Equals("--refresh-cache", StringComparison.OrdinalIgnoreCase))
cfg.RefreshCache = true;
return cfg;
}
static bool ValidateConfiguration(AppConfig config, IConsoleLogger logger)
{
var hasErrors = false;
if (string.IsNullOrWhiteSpace(config.HeroicConfigPath))
{
logger.WriteLine("[ERROR] HeroicConfigPath is not configured in appsettings.json", ConsoleColor.Red);
hasErrors = true;
}
// At least one library path should be configured
if (string.IsNullOrWhiteSpace(config.LegendaryLibraryPath) &&
string.IsNullOrWhiteSpace(config.GogLibraryPath) &&
string.IsNullOrWhiteSpace(config.NileLibraryPath))
{
logger.WriteLine(
"[ERROR] No library paths configured. Set at least one of: LegendaryLibraryPath, GogLibraryPath, NileLibraryPath",
ConsoleColor.Red);
hasErrors = true;
}
return !hasErrors;
}
static void ResolvePaths(AppConfig config)
{
config.HeroicConfigPath = ResolvePath(config.HeroicConfigPath);
config.LegendaryLibraryPath = ResolvePath(config.LegendaryLibraryPath);
config.GogLibraryPath = ResolvePath(config.GogLibraryPath);
config.NileLibraryPath = ResolvePath(config.NileLibraryPath);
}
static string? ResolvePath(string? path)
{
if (string.IsNullOrEmpty(path))
return null;
// Handle Windows %AppData%
if (path.Contains("%AppData%", StringComparison.OrdinalIgnoreCase))
{
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
return path.Replace("%AppData%", appData, StringComparison.OrdinalIgnoreCase);
}
// Handle Linux ~/
if (path.StartsWith('~'))
{
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
return path.Replace("~", home);
}
return path;
}