|
| 1 | +using Microsoft.Win32; |
| 2 | +using MonoMod.Utils; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Text.RegularExpressions; |
| 9 | + |
| 10 | +namespace MonoMod.Installer { |
| 11 | + public class EpicFinder : GameFinder { |
| 12 | + |
| 13 | + // "" -> " |
| 14 | + public readonly static Regex DisplayNameRegex = new Regex(@"\s*""DisplayName"": ""(.*)"",", RegexOptions.Compiled); |
| 15 | + public readonly static Regex InstallLocationRegex = new Regex(@"\s*""InstallLocation"": ""(.*)"",", RegexOptions.Compiled); |
| 16 | + |
| 17 | + public override string ID => "epic"; |
| 18 | + |
| 19 | + public override string FindGameDir(string gameid) { |
| 20 | + if ((PlatformHelper.Current & Platform.Windows) != Platform.Windows) |
| 21 | + return null; |
| 22 | + |
| 23 | + string regKey = |
| 24 | + (PlatformHelper.Current & Platform.X64) == Platform.X64 ? |
| 25 | + @"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Epic Games\EpicGamesLauncher" : |
| 26 | + @"HKEY_LOCAL_MACHINE\SOFTWARE\Epic Games\EpicGamesLauncher"; |
| 27 | + // Win32 is case-insensitive. |
| 28 | + string appdata = (string) Registry.GetValue(regKey, "AppDataPath", null); |
| 29 | + if (!Directory.Exists(appdata)) |
| 30 | + return null; |
| 31 | + |
| 32 | + // path ends up pointing to f.e. C:\ProgramData\Epic\EpicGamesLauncher\Data\ |
| 33 | + // Manifest .item (pretty much .json) files are stored in the Manifests subdir. |
| 34 | + // The filenames sadly aren't consistent across machines / accounts, |
| 35 | + // meaning that we'll need to crawl through them to find the right one. |
| 36 | + |
| 37 | + string manifests = Path.Combine(appdata, "Manifests"); |
| 38 | + if (!Directory.Exists(manifests)) |
| 39 | + return null; |
| 40 | + |
| 41 | + foreach (string manifest in Directory.GetFiles(manifests)) { |
| 42 | + if (!manifest.EndsWith(".item", StringComparison.InvariantCultureIgnoreCase)) |
| 43 | + continue; |
| 44 | + |
| 45 | + string displayName = null; |
| 46 | + string installLocation = null; |
| 47 | + |
| 48 | + using (StreamReader reader = new StreamReader(manifest)) |
| 49 | + while (!reader.EndOfStream) { |
| 50 | + string line = reader.ReadLine().Trim(); |
| 51 | + Match match; |
| 52 | + |
| 53 | + match = DisplayNameRegex.Match(line); |
| 54 | + if (match.Success) |
| 55 | + displayName = Regex.Unescape(match.Groups[1].Value); |
| 56 | + |
| 57 | + match = InstallLocationRegex.Match(line); |
| 58 | + if (match.Success) |
| 59 | + installLocation = Regex.Unescape(match.Groups[1].Value); |
| 60 | + } |
| 61 | + |
| 62 | + if (!string.IsNullOrEmpty(displayName) && gameid.Equals(displayName, StringComparison.InvariantCultureIgnoreCase) && |
| 63 | + !string.IsNullOrEmpty(installLocation) && Directory.Exists(installLocation)) |
| 64 | + return installLocation; |
| 65 | + } |
| 66 | + |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | +} |
0 commit comments