ProjectArcade
207 строк · 6.4 Кб
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Runtime.Serialization;
6using System.IO;
7using EmulatorLauncher.Common.FileFormats;
8using Microsoft.Win32;
9using EmulatorLauncher.Common.Launchers.Epic;
10
11namespace EmulatorLauncher.Common.Launchers
12{
13public class EpicLibrary
14{
15const string GameLaunchUrl = @"com.epicgames.launcher://apps/{0}?action=launch&silent=true";
16
17public static string GetEpicGameExecutableName(Uri uri)
18{
19string shorturl = uri.LocalPath.ExtractString("/", ":");
20
21var modSdkMetadataDir = GetMetadataPath();
22if (modSdkMetadataDir != null)
23{
24string manifestPath = modSdkMetadataDir.ToString();
25
26string gameExecutable = null;
27
28if (Directory.Exists(manifestPath))
29{
30foreach (var manifest in GetInstalledManifests())
31{
32if (shorturl.Equals(manifest.CatalogNamespace))
33{
34gameExecutable = manifest.LaunchExecutable;
35break;
36}
37}
38}
39
40if (gameExecutable == null)
41throw new ApplicationException("There is a problem: The Game is not installed");
42
43return Path.GetFileNameWithoutExtension(gameExecutable);
44}
45
46throw new ApplicationException("There is a problem: Epic Launcher is not installed");
47}
48
49public static LauncherGameInfo[] GetInstalledGames()
50{
51var games = new List<LauncherGameInfo>();
52
53if (!IsInstalled)
54return games.ToArray();
55
56var appList = GetInstalledAppList();
57var manifests = GetInstalledManifests();
58
59if (appList == null || manifests == null)
60return games.ToArray();
61
62foreach (var app in appList)
63{
64if (app.AppName.StartsWith("UE_"))
65continue;
66
67var manifest = manifests.FirstOrDefault(a => a.AppName == app.AppName);
68if (manifest == null)
69continue;
70
71// Skip DLCs
72if (manifest.AppName != manifest.MainGameAppName)
73continue;
74
75// Skip Plugins
76if (manifest.AppCategories == null || manifest.AppCategories.Any(a => a == "plugins" || a == "plugins/engine"))
77continue;
78
79var gameName = manifest.DisplayName ?? Path.GetFileName(app.InstallLocation);
80
81var installLocation = manifest.InstallLocation ?? app.InstallLocation;
82if (string.IsNullOrEmpty(installLocation))
83continue;
84
85var game = new LauncherGameInfo()
86{
87Id = app.AppName,
88Name = gameName,
89LauncherUrl = string.Format(GameLaunchUrl, manifest.AppName),
90InstallDirectory = Path.GetFullPath(installLocation),
91ExecutableName = manifest.LaunchExecutable,
92Launcher = GameLauncherType.Epic
93};
94
95games.Add(game);
96}
97
98return games.ToArray();
99}
100
101
102static string AllUsersPath { get { return Path.Combine(Environment.ExpandEnvironmentVariables("%PROGRAMDATA%"), "Epic"); } }
103
104public static bool IsInstalled
105{
106get
107{
108return File.Exists(GetExecutablePath());
109}
110}
111
112static string GetExecutablePath()
113{
114var modSdkMetadataDir = Registry.GetValue("HKEY_CURRENT_USER\\Software\\Epic Games\\EOS", "ModSdkCommand", null);
115if (modSdkMetadataDir != null)
116return modSdkMetadataDir.ToString();
117
118return null;
119}
120
121static string GetMetadataPath()
122{
123var modSdkMetadataDir = Registry.GetValue("HKEY_CURRENT_USER\\Software\\Epic Games\\EOS", "ModSdkMetadataDir", null);
124if (modSdkMetadataDir != null)
125return modSdkMetadataDir.ToString();
126
127return null;
128}
129
130static List<LauncherInstalled.InstalledApp> GetInstalledAppList()
131{
132var installListPath = Path.Combine(AllUsersPath, "UnrealEngineLauncher", "LauncherInstalled.dat");
133if (!File.Exists(installListPath))
134return new List<LauncherInstalled.InstalledApp>();
135
136var list = JsonSerializer.DeserializeString<LauncherInstalled>(File.ReadAllText(installListPath));
137return list.InstallationList;
138}
139
140static IEnumerable<EpicGame> GetInstalledManifests()
141{
142var installListPath = GetMetadataPath();
143if (Directory.Exists(installListPath))
144{
145foreach (var manFile in Directory.GetFiles(installListPath, "*.item"))
146{
147EpicGame manifest = null;
148
149try { manifest = JsonSerializer.DeserializeString<EpicGame>(File.ReadAllText(manFile)); }
150catch { }
151
152if (manifest != null)
153yield return manifest;
154}
155}
156}
157
158}
159}
160
161namespace EmulatorLauncher.Common.Launchers.Epic
162{
163[DataContract]
164public class LauncherInstalled
165{
166[DataContract]
167public class InstalledApp
168{
169[DataMember]
170public string InstallLocation { get; set; }
171[DataMember]
172public string AppName { get; set; }
173[DataMember]
174public long AppID { get; set; }
175[DataMember]
176public string AppVersion { get; set; }
177}
178
179[DataMember]
180public List<InstalledApp> InstallationList { get; set; }
181}
182
183[DataContract]
184public class EpicGame
185{
186[DataMember]
187public string AppName { get; set; }
188
189[DataMember]
190public string CatalogNamespace { get; set; }
191
192[DataMember]
193public string LaunchExecutable { get; set; }
194
195[DataMember]
196public string InstallLocation;
197
198[DataMember]
199public string MainGameAppName;
200
201[DataMember]
202public string DisplayName;
203
204[DataMember]
205public List<string> AppCategories { get; set; }
206}
207}
208