-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmulatorService.cs
More file actions
200 lines (169 loc) · 8.25 KB
/
Copy pathEmulatorService.cs
File metadata and controls
200 lines (169 loc) · 8.25 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
193
194
195
196
197
198
199
200
using Playnite.SDK;
using Playnite.SDK.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace GameSnapPlugin
{
public class EmulatorScreenshot
{
public string FilePath { get; set; } = "";
public string GameName { get; set; } = "";
public string Emulator { get; set; } = "";
}
public class EmulatorService
{
private readonly IPlayniteAPI _playniteApi;
private readonly GameSnapSettings _settings;
private readonly GameSnapLogger _logger;
public EmulatorService(IPlayniteAPI playniteApi, GameSnapSettings settings, GameSnapLogger logger)
{
_playniteApi = playniteApi;
_settings = settings;
_logger = logger;
}
// ──────────────────────────────────────────────
// Static folder resolver — used by EmulatorProfile for status display
// ──────────────────────────────────────────────
public static string? GetDefaultFolder(string emulatorName)
{
var appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var docs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
switch (emulatorName)
{
case "RetroArch":
return Check(Path.Combine(appdata, "RetroArch", "screenshots"));
case "PCSX2":
return Check(Path.Combine(docs, "PCSX2", "snaps"))
?? Check(Path.Combine(docs, "PCSX2 1.7.0", "snaps"));
case "Dolphin":
return Check(Path.Combine(docs, "Dolphin Emulator", "ScreenShots"));
case "RPCS3":
// No standard path — user must set custom
return null;
case "Cemu":
return Check(Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ProgramFilesX86), "Cemu", "screenshots"))
?? Check(Path.Combine("C:\\Cemu", "screenshots"));
case "PPSSPP":
return Check(Path.Combine(docs, "PPSSPP", "screenshots"));
case "mGBA":
return null; // no standard path
case "DuckStation":
return Check(Path.Combine(docs, "DuckStation", "screenshots"));
default:
return null;
}
}
private static string? Check(string path)
=> Directory.Exists(path) ? path : null;
// ──────────────────────────────────────────────
// Returns all pending screenshots from active profiles
// ──────────────────────────────────────────────
public List<EmulatorScreenshot> GetPendingScreenshots()
{
var result = new List<EmulatorScreenshot>();
foreach (var profile in _settings.EmulatorProfiles)
{
if (!profile.Enabled) continue;
var folder = profile.ResolvedPath;
if (folder == null) continue;
try
{
foreach (var file in Directory.GetFiles(folder, "*", SearchOption.AllDirectories))
{
var ext = Path.GetExtension(file).ToLowerInvariant();
if (ext != ".png" && ext != ".jpg" && ext != ".jpeg") continue;
var gameName = ResolveGameName(file, profile.Name);
if (string.IsNullOrEmpty(gameName)) continue;
result.Add(new EmulatorScreenshot
{
FilePath = file,
GameName = gameName,
Emulator = profile.Name
});
}
}
catch (Exception ex)
{
_logger.Error($"EmulatorService [{profile.Name}]: {ex.Message}");
}
}
return result;
}
// ──────────────────────────────────────────────
// Resolve game name from file path
// ──────────────────────────────────────────────
private string? ResolveGameName(string filePath, string emulatorName)
{
// Strategy 1: file is inside a subfolder named after the game
var parent = Path.GetFileName(Path.GetDirectoryName(filePath) ?? "");
if (!string.IsNullOrEmpty(parent) &&
!parent.Equals(emulatorName, StringComparison.OrdinalIgnoreCase) &&
parent.Length > 2)
{
var match = FindPlayniteGame(parent);
if (match != null) return match;
return CleanRomName(parent);
}
// Strategy 2: filename starts with game name (RetroArch: GameName_YYYY-MM-DD.png)
var fileName = Path.GetFileNameWithoutExtension(filePath);
var m = Regex.Match(fileName, @"^(.+?)[\s_-]\d{4}");
if (m.Success)
{
var candidate = m.Groups[1].Value.Trim();
var match = FindPlayniteGame(candidate);
if (match != null) return match;
return CleanRomName(candidate);
}
// Strategy 3: use full filename without extension as game name
var cleaned = CleanRomName(Path.GetFileNameWithoutExtension(filePath));
if (!string.IsNullOrEmpty(cleaned))
{
var match = FindPlayniteGame(cleaned);
return match ?? cleaned;
}
return null;
}
// ──────────────────────────────────────────────
// Remove ROM-specific suffixes: (USA), [!], (Rev 1), etc.
// ──────────────────────────────────────────────
private static string CleanRomName(string name)
{
// Remove parentheses content: (USA), (Europe), (Rev 1), etc.
name = Regex.Replace(name, @"\s*\([^)]*\)", "");
// Remove bracket content: [!], [b], etc.
name = Regex.Replace(name, @"\s*\[[^\]]*\]", "");
// Remove trailing dashes and underscores
name = name.Trim(' ', '-', '_');
return name;
}
// ──────────────────────────────────────────────
// Match against Playnite library
// ──────────────────────────────────────────────
private string? FindPlayniteGame(string name)
{
var norm = DictionaryService.Normalize(name);
if (string.IsNullOrEmpty(norm)) return null;
string? bestMatch = null;
int bestDistance = int.MaxValue;
foreach (var game in _playniteApi.Database.Games)
{
var normGame = DictionaryService.Normalize(game.Name);
if (normGame == norm) return game.Name; // exact match
if (normGame.Contains(norm) || norm.Contains(normGame))
{
var dist = Math.Abs(normGame.Length - norm.Length);
if (dist < bestDistance)
{
bestDistance = dist;
bestMatch = game.Name;
}
}
}
return bestMatch;
}
}
}