Initial
This commit is contained in:
1 parent
2cff35f215
commit
53e6e612c9
360 files changed
+253990
-74
No files matched your search
@@ -0,0 +1,305 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO.Enumeration;
|
||||
using System.Text.Json;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace EonaCat.XSBOX.Launcher;
|
||||
|
||||
internal sealed partial class LauncherForm
|
||||
{
|
||||
const string ScarlettExe = "game_scarlett_master.exe";
|
||||
const string GameConfig = "MicrosoftGame.config";
|
||||
static readonly string[] CommonFolderNames = { "XboxGames", "Games", "Xbox", "GameDumps", "GTA6" };
|
||||
static readonly JsonSerializerOptions LibraryJson = new() { WriteIndented = true };
|
||||
|
||||
// Icons are read from disk once per game, not on every redraw of the tile list.
|
||||
readonly Dictionary<string, Bitmap?> tileIcons = new(StringComparer.OrdinalIgnoreCase);
|
||||
string? lastLibraryJson;
|
||||
bool scanning;
|
||||
|
||||
static EnumerationOptions ScanOptions(int depth) => new()
|
||||
{
|
||||
RecurseSubdirectories = true,
|
||||
IgnoreInaccessible = true,
|
||||
AttributesToSkip = FileAttributes.ReparsePoint | FileAttributes.System,
|
||||
MaxRecursionDepth = depth
|
||||
};
|
||||
|
||||
void BuildGamesPage()
|
||||
{
|
||||
var tab = new TabPage("Installed games") { Name = "Installed games", BackColor = BackColor, ForeColor = ForeColor };
|
||||
tab.Controls.Add(gameTiles);
|
||||
tab.Controls.Add(CreatePageHeader("Installed games", "Detected Xbox game folders · Select a tile to choose the active title"));
|
||||
tabs.TabPages.Add(tab);
|
||||
}
|
||||
|
||||
void LoadGameLibrary()
|
||||
{
|
||||
Directory.CreateDirectory(home);
|
||||
try
|
||||
{
|
||||
if (File.Exists(gameListPath))
|
||||
{
|
||||
var json = File.ReadAllText(gameListPath);
|
||||
games.AddRange(JsonSerializer.Deserialize<List<GameEntry>>(json) ?? new List<GameEntry>());
|
||||
}
|
||||
}
|
||||
catch { games.Clear(); }
|
||||
var last = File.Exists(selectedGamePath) ? File.ReadAllText(selectedGamePath).Trim() : "";
|
||||
if (string.IsNullOrWhiteSpace(last))
|
||||
{
|
||||
var legacy = Path.Combine(home, "launcher-game.txt");
|
||||
if (File.Exists(legacy)) last = File.ReadAllText(legacy).Trim();
|
||||
}
|
||||
selectedGame = games.FirstOrDefault(g => string.Equals(g.Executable, last, StringComparison.OrdinalIgnoreCase) && File.Exists(g.Executable));
|
||||
if (selectedGame == null && File.Exists(last))
|
||||
{
|
||||
var dir = Path.GetDirectoryName(last)!;
|
||||
selectedGame = new GameEntry(new DirectoryInfo(dir).Parent?.Name ?? new DirectoryInfo(dir).Name, last, dir);
|
||||
AddOrUpdateGame(selectedGame, false);
|
||||
}
|
||||
if (selectedGame == null && File.Exists(Path.Combine(root, "game_scarlett_master.exe")))
|
||||
{
|
||||
var exePath = Path.Combine(root, "game_scarlett_master.exe");
|
||||
selectedGame = new GameEntry(new DirectoryInfo(root).Name, exePath, root);
|
||||
AddOrUpdateGame(selectedGame, false);
|
||||
}
|
||||
if (selectedGame != null) gamePath.Text = selectedGame.Executable;
|
||||
RenderGameTiles();
|
||||
}
|
||||
|
||||
void SaveGameLibrary()
|
||||
{
|
||||
Directory.CreateDirectory(home);
|
||||
var json = JsonSerializer.Serialize(games.OrderBy(g => g.Name).ToList(), LibraryJson);
|
||||
if (json != lastLibraryJson) // skip the disk write when nothing changed
|
||||
{
|
||||
File.WriteAllText(gameListPath + ".tmp", json);
|
||||
File.Move(gameListPath + ".tmp", gameListPath, true);
|
||||
lastLibraryJson = json;
|
||||
}
|
||||
if (selectedGame != null) File.WriteAllText(selectedGamePath, selectedGame.Executable);
|
||||
}
|
||||
|
||||
void AddOrUpdateGame(GameEntry game, bool select)
|
||||
{
|
||||
var index = games.FindIndex(g => string.Equals(g.Executable, game.Executable, StringComparison.OrdinalIgnoreCase));
|
||||
if (index >= 0) games[index] = game; else games.Add(game);
|
||||
if (select) selectedGame = game;
|
||||
SaveGameLibrary(); RenderGameTiles();
|
||||
}
|
||||
|
||||
void SelectGame(GameEntry game)
|
||||
{
|
||||
if (!File.Exists(game.Executable)) { MessageBox.Show(this, "The selected game's executable no longer exists. Scan again or remove the old entry.", "Game not found", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; }
|
||||
selectedGame = game;
|
||||
gamePath.Text = game.Executable;
|
||||
SaveGameLibrary(); RenderGameTiles();
|
||||
status.Text = $"Selected {game.Name}. Click Launch game to start it.";
|
||||
}
|
||||
|
||||
Bitmap? GetTileIcon(GameEntry game)
|
||||
{
|
||||
var key = !string.IsNullOrWhiteSpace(game.IconPath) && File.Exists(game.IconPath) ? game.IconPath : game.Executable;
|
||||
if (tileIcons.TryGetValue(key, out var cached)) return cached;
|
||||
Bitmap? bitmap = null;
|
||||
try
|
||||
{
|
||||
if (key == game.IconPath)
|
||||
{
|
||||
using var supplied = new Icon(key, Ui.S(48), Ui.S(48));
|
||||
bitmap = supplied.ToBitmap();
|
||||
}
|
||||
else
|
||||
{
|
||||
using var embedded = Icon.ExtractAssociatedIcon(key);
|
||||
bitmap = embedded?.ToBitmap();
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
tileIcons[key] = bitmap;
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
void RenderGameTiles()
|
||||
{
|
||||
if (gameTiles.IsDisposed) return;
|
||||
gameTiles.SuspendLayout();
|
||||
var old = gameTiles.Controls.Cast<Control>().ToList();
|
||||
gameTiles.Controls.Clear();
|
||||
foreach (var control in old) control.Dispose(); // Controls.Clear() alone leaks the tiles
|
||||
|
||||
var present = games.Where(g => File.Exists(g.Executable)).OrderBy(g => g.Name, StringComparer.OrdinalIgnoreCase).ToList();
|
||||
foreach (var game in present)
|
||||
{
|
||||
var isSelected = selectedGame != null && string.Equals(selectedGame.Executable, game.Executable, StringComparison.OrdinalIgnoreCase);
|
||||
var tile = new GameTile(game, GetTileIcon(game), isSelected);
|
||||
tile.GameChosen += SelectGame;
|
||||
gameTiles.Controls.Add(tile);
|
||||
}
|
||||
if (present.Count == 0)
|
||||
gameTiles.Controls.Add(new Label { Text = "No Xbox game dumps detected yet. Use Scan for Xbox games or Add game folder…", ForeColor = Color.Gainsboro, AutoSize = true, MaximumSize = new Size(Ui.S(700), 0), UseMnemonic = false, Margin = new Padding(Ui.S(12)) });
|
||||
selectedGameLabel.Text = selectedGame == null ? "No game selected" : $"Selected: {selectedGame.Name}";
|
||||
gameTiles.ResumeLayout();
|
||||
}
|
||||
|
||||
async Task ScanForGamesAsync()
|
||||
{
|
||||
if (scanning) return;
|
||||
scanning = true;
|
||||
if (scanButton != null) scanButton.Enabled = false;
|
||||
try
|
||||
{
|
||||
status.Text = "Scanning common Xbox game locations…";
|
||||
var found = await Task.Run(DiscoverGames);
|
||||
foreach (var game in found)
|
||||
{
|
||||
var index = games.FindIndex(g => string.Equals(g.Executable, game.Executable, StringComparison.OrdinalIgnoreCase));
|
||||
if (index >= 0) games[index] = game; else games.Add(game);
|
||||
}
|
||||
if (selectedGame == null && games.Count > 0) selectedGame = games.FirstOrDefault(g => File.Exists(g.Executable));
|
||||
if (selectedGame != null) gamePath.Text = selectedGame.Executable;
|
||||
SaveGameLibrary(); RenderGameTiles();
|
||||
status.Text = $"Game scan complete: found {found.Count} Xbox-compatible game folder(s).";
|
||||
}
|
||||
catch (Exception ex) { status.Text = "Game scan failed: " + ex.Message; }
|
||||
finally
|
||||
{
|
||||
scanning = false;
|
||||
if (scanButton is { IsDisposed: false }) scanButton.Enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>One filesystem walk per root that looks for both marker files at once.</summary>
|
||||
static IEnumerable<string> EnumerateMarkers(string folder) =>
|
||||
new FileSystemEnumerable<string>(folder, (ref FileSystemEntry entry) => entry.ToFullPath(), ScanOptions(8))
|
||||
{
|
||||
ShouldIncludePredicate = (ref FileSystemEntry entry) =>
|
||||
!entry.IsDirectory &&
|
||||
(MemoryExtensions.Equals(entry.FileName, ScarlettExe, StringComparison.OrdinalIgnoreCase) ||
|
||||
MemoryExtensions.Equals(entry.FileName, GameConfig, StringComparison.OrdinalIgnoreCase))
|
||||
};
|
||||
|
||||
GameEntry? GameFromConfig(string config)
|
||||
{
|
||||
var folder = Path.GetDirectoryName(config)!;
|
||||
string title = new DirectoryInfo(folder).Name;
|
||||
string? executable = null;
|
||||
try
|
||||
{
|
||||
var doc = XDocument.Load(config);
|
||||
title = doc.Descendants().FirstOrDefault(e => e.Name.LocalName.Equals("Title", StringComparison.OrdinalIgnoreCase))?.Value.Trim() ?? title;
|
||||
var node = doc.Descendants().FirstOrDefault(e => e.Name.LocalName.Equals("Executable", StringComparison.OrdinalIgnoreCase));
|
||||
executable = node?.Attributes().FirstOrDefault(a => new[] { "Name", "Path", "Target" }.Contains(a.Name.LocalName, StringComparer.OrdinalIgnoreCase))?.Value ?? node?.Value;
|
||||
}
|
||||
catch { }
|
||||
var candidates = new List<string>();
|
||||
if (!string.IsNullOrWhiteSpace(executable))
|
||||
{
|
||||
var clean = executable.Replace('/', Path.DirectorySeparatorChar).TrimStart(Path.DirectorySeparatorChar);
|
||||
candidates.Add(Path.Combine(folder, clean));
|
||||
candidates.Add(Path.Combine(folder, "Content", clean));
|
||||
}
|
||||
candidates.Add(Path.Combine(folder, "game.exe"));
|
||||
candidates.Add(Path.Combine(folder, "Content", "game.exe"));
|
||||
var chosen = candidates.FirstOrDefault(f => File.Exists(f) && !Path.GetFileName(f).StartsWith("unins", StringComparison.OrdinalIgnoreCase) && !Path.GetFileName(f).Contains("crash", StringComparison.OrdinalIgnoreCase));
|
||||
return chosen == null ? null : new GameEntry(title, chosen, folder);
|
||||
}
|
||||
|
||||
List<GameEntry> DiscoverGames()
|
||||
{
|
||||
var candidates = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
void AddRoot(string? path) { if (!string.IsNullOrWhiteSpace(path) && Directory.Exists(path)) candidates.Add(Path.GetFullPath(path).TrimEnd(Path.DirectorySeparatorChar)); }
|
||||
AddRoot(root);
|
||||
var profile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
||||
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||||
var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
|
||||
var programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
|
||||
foreach (var name in CommonFolderNames)
|
||||
{
|
||||
AddRoot(Path.Combine(profile, name));
|
||||
AddRoot(Path.Combine(documents, name));
|
||||
AddRoot(Path.Combine(profile, "Downloads", name));
|
||||
AddRoot(Path.Combine(programFiles, name));
|
||||
AddRoot(Path.Combine(programFilesX86, name));
|
||||
}
|
||||
try
|
||||
{
|
||||
foreach (var drive in DriveInfo.GetDrives().Where(d => d.IsReady && d.DriveType == DriveType.Fixed))
|
||||
foreach (var name in CommonFolderNames) AddRoot(Path.Combine(drive.RootDirectory.FullName, name));
|
||||
}
|
||||
catch { }
|
||||
|
||||
// A root that sits inside another root is already covered by the outer walk.
|
||||
var all = candidates.OrderBy(r => r.Length).ToList();
|
||||
var roots = all.Where(r => !all.Any(o => o.Length < r.Length && r.StartsWith(o + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))).ToList();
|
||||
|
||||
var result = new ConcurrentDictionary<string, GameEntry>(StringComparer.OrdinalIgnoreCase);
|
||||
Parallel.ForEach(roots, new ParallelOptions { MaxDegreeOfParallelism = Math.Min(4, Math.Max(1, roots.Count)) }, searchRoot =>
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var file in EnumerateMarkers(searchRoot))
|
||||
{
|
||||
if (string.Equals(Path.GetFileName(file), ScarlettExe, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var dir = Path.GetDirectoryName(file)!;
|
||||
var name = Directory.GetParent(dir)?.Name ?? new DirectoryInfo(dir).Name;
|
||||
result[file] = new GameEntry(name, file, dir);
|
||||
}
|
||||
else if (GameFromConfig(file) is { } game) result[game.Executable] = game;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
});
|
||||
return result.Values.OrderBy(g => g.Name, StringComparer.OrdinalIgnoreCase).ToList();
|
||||
}
|
||||
|
||||
void AddGameFolder()
|
||||
{
|
||||
using var dialog = new FolderBrowserDialog { Description = "Select an Xbox game dump folder containing game_scarlett_master.exe or MicrosoftGame.config", UseDescriptionForTitle = true };
|
||||
if (dialog.ShowDialog(this) != DialogResult.OK) return;
|
||||
var found = DiscoverGamesInFolder(dialog.SelectedPath);
|
||||
if (found.Count == 0) { MessageBox.Show(this, "No supported Xbox game executable or MicrosoftGame.config was found in that folder.", "No game found", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }
|
||||
foreach (var game in found) AddOrUpdateGame(game, false);
|
||||
SelectGame(found[0]);
|
||||
}
|
||||
|
||||
List<GameEntry> DiscoverGamesInFolder(string folder)
|
||||
{
|
||||
var found = new List<GameEntry>();
|
||||
var configs = new List<string>();
|
||||
try
|
||||
{
|
||||
foreach (var file in EnumerateMarkers(folder))
|
||||
{
|
||||
if (string.Equals(Path.GetFileName(file), ScarlettExe, StringComparison.OrdinalIgnoreCase))
|
||||
found.Add(new GameEntry(new DirectoryInfo(Path.GetDirectoryName(file)!).Parent?.Name ?? new DirectoryInfo(folder).Name, file, Path.GetDirectoryName(file)!));
|
||||
else configs.Add(file);
|
||||
}
|
||||
if (found.Count == 0)
|
||||
{
|
||||
foreach (var config in configs)
|
||||
{
|
||||
var dir = Path.GetDirectoryName(config)!;
|
||||
var exeFile = Directory.EnumerateFiles(dir, "*.exe", ScanOptions(3)).FirstOrDefault();
|
||||
if (exeFile != null) found.Add(new GameEntry(new DirectoryInfo(dir).Name, exeFile, dir));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
return found;
|
||||
}
|
||||
|
||||
void BrowseGame()
|
||||
{
|
||||
using var dialog = new OpenFileDialog { Filter = "Xbox game executable|game_scarlett_master.exe;*.exe|Executable files|*.exe", Title = "Select your legally obtained Xbox game executable" };
|
||||
if (dialog.ShowDialog(this) == DialogResult.OK)
|
||||
{
|
||||
var dir = Path.GetDirectoryName(dialog.FileName)!;
|
||||
var game = new GameEntry(new DirectoryInfo(dir).Parent?.Name ?? new DirectoryInfo(dir).Name, dialog.FileName, dir);
|
||||
AddOrUpdateGame(game, true);
|
||||
gamePath.Text = dialog.FileName;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user