ProjectArcade
165 строк · 5.1 Кб
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.IO;6using System.Data.SQLite;7using EmulatorLauncher.Common.FileFormats;8using System.Data.Common;9using EmulatorLauncher.Common.Launchers.Amazon;10
11namespace EmulatorLauncher.Common.Launchers12{
13public static class AmazonLibrary14{15static AmazonLibrary()16{17SQLiteInteropManager.InstallSQLiteInteropDll();18}19
20const string GameLaunchUrl = @"amazon-games://play/{0}";21
22public static LauncherGameInfo[] GetInstalledGames()23{24var games = new List<LauncherGameInfo>();25
26if (!IsInstalled)27return games.ToArray();28
29using (var db = new SQLiteConnection("Data Source = " + GetDatabasePath()))30{31db.Open();32
33var cmd = db.CreateCommand();34cmd.CommandText = "SELECT * FROM DbSet WHERE Installed = 1;";35
36var reader = cmd.ExecuteReader();37
38var list = reader.ReadObjects<AmazonInstalledGameInfo>();39if (list != null)40{41foreach (var app in list)42{43if (!Directory.Exists(app.InstallDirectory))44continue;45
46var game = new LauncherGameInfo()47{48Id = app.Id,49Name = app.ProductTitle,50InstallDirectory = Path.GetFullPath(app.InstallDirectory),51LauncherUrl = string.Format(GameLaunchUrl, app.Id),52ExecutableName = GetAmazonGameExecutable(app.InstallDirectory),53Launcher = GameLauncherType.Amazon54};55
56games.Add(game);57}58}59
60
61db.Close();62}63
64return games.ToArray();65}66
67public static bool IsInstalled68{69get70{71return File.Exists(GetDatabasePath());72}73}74
75static string GetDatabasePath()76{77string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);78string amazonDB = Path.Combine(appData, "Amazon Games", "Data", "Games", "Sql", "GameInstallInfo.sqlite");79if (File.Exists(amazonDB))80return amazonDB;81
82return null;83}84
85public static string GetAmazonGameExecutableName(Uri uri)86{87if (!IsInstalled)88return null;89
90string shorturl = uri.AbsolutePath.Substring(1);91
92string amazonDB = GetDatabasePath();93if (File.Exists(amazonDB))94{95string gameInstallPath = null;96
97using (var db = new SQLiteConnection("Data Source = " + amazonDB))98{99db.Open();100
101var cmd = db.CreateCommand();102cmd.CommandText = "SELECT installDirectory FROM DbSet where Id = '" + shorturl + "'";103
104var reader = cmd.ExecuteReader();105
106if (!reader.HasRows)107{108db.Close();109throw new ApplicationException("There is a problem: the Game is not installed in Amazon Launcher");110}111
112while (reader.Read())113gameInstallPath = reader.GetString(0);114
115db.Close();116}117
118var exe = GetAmazonGameExecutable(gameInstallPath);119if (string.IsNullOrEmpty(exe))120throw new ApplicationException("There is a problem: Game is not installed");121
122return Path.GetFileNameWithoutExtension(exe);123}124
125throw new ApplicationException("There is a problem: Amazon Launcher is not installed or the Game is not installed");126}127
128private static string GetAmazonGameExecutable(string path)129{130string fuelFile = Path.Combine(path, "fuel.json");131string gameexe = null;132
133if (!File.Exists(fuelFile))134throw new ApplicationException("There is a problem: game executable cannot be found");135
136var json = DynamicJson.Load(fuelFile);137
138var jsonMain = json.GetObject("Main");139if (jsonMain == null)140return null;141
142gameexe = jsonMain["Command"];143
144if (!string.IsNullOrEmpty(gameexe))145return gameexe;146
147return null;148}149
150}151}
152
153namespace EmulatorLauncher.Common.Launchers.Amazon154{
155public class AmazonInstalledGameInfo156{157public string Id { get; set; }158public string InstallDirectory { get; set; }159public int Installed { get; set; }160public string ProductTitle { get; set; }161public string ProductAsin { get; set; }162public string ManifestSignature { get; set; }163public string ManifestSignatureKeyId { get; set; }164}165}
166