ProjectArcade

Форк
0
165 строк · 5.1 Кб
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.IO;
6
using System.Data.SQLite;
7
using EmulatorLauncher.Common.FileFormats;
8
using System.Data.Common;
9
using EmulatorLauncher.Common.Launchers.Amazon;
10

11
namespace EmulatorLauncher.Common.Launchers
12
{
13
    public static class AmazonLibrary
14
    {
15
        static AmazonLibrary()
16
        {
17
            SQLiteInteropManager.InstallSQLiteInteropDll();
18
        }
19

20
        const string GameLaunchUrl = @"amazon-games://play/{0}";
21

22
        public static LauncherGameInfo[] GetInstalledGames()
23
        {
24
            var games = new List<LauncherGameInfo>();
25

26
            if (!IsInstalled)
27
                return games.ToArray();
28

29
            using (var db = new SQLiteConnection("Data Source = " + GetDatabasePath()))
30
            {
31
                db.Open();
32

33
                var cmd = db.CreateCommand();
34
                cmd.CommandText = "SELECT * FROM DbSet WHERE Installed = 1;";
35

36
                var reader = cmd.ExecuteReader();
37

38
                var list = reader.ReadObjects<AmazonInstalledGameInfo>();
39
                if (list != null)
40
                {
41
                    foreach (var app in list)
42
                    {
43
                        if (!Directory.Exists(app.InstallDirectory))
44
                            continue;
45

46
                        var game = new LauncherGameInfo()
47
                        {
48
                            Id = app.Id,
49
                            Name = app.ProductTitle,
50
                            InstallDirectory = Path.GetFullPath(app.InstallDirectory),
51
                            LauncherUrl = string.Format(GameLaunchUrl, app.Id),   
52
                            ExecutableName = GetAmazonGameExecutable(app.InstallDirectory),
53
                            Launcher = GameLauncherType.Amazon
54
                        };
55

56
                        games.Add(game);
57
                    }
58
                }
59

60

61
                db.Close();
62
            }
63
            
64
            return games.ToArray();
65
        }
66

67
        public static bool IsInstalled
68
        {
69
            get
70
            {
71
                return File.Exists(GetDatabasePath());
72
            }
73
        }
74

75
        static string GetDatabasePath()
76
        {
77
            string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
78
            string amazonDB = Path.Combine(appData, "Amazon Games", "Data", "Games", "Sql", "GameInstallInfo.sqlite");
79
            if (File.Exists(amazonDB))
80
                return amazonDB;
81

82
            return null;
83
        }
84

85
        public static string GetAmazonGameExecutableName(Uri uri)
86
        {
87
            if (!IsInstalled)
88
                return null;
89

90
            string shorturl = uri.AbsolutePath.Substring(1);
91

92
            string amazonDB = GetDatabasePath();
93
            if (File.Exists(amazonDB))
94
            {
95
                string gameInstallPath = null;
96

97
                using (var db = new SQLiteConnection("Data Source = " + amazonDB))
98
                {
99
                    db.Open();
100

101
                    var cmd = db.CreateCommand();
102
                    cmd.CommandText = "SELECT installDirectory FROM DbSet where Id = '" + shorturl + "'";
103

104
                    var reader = cmd.ExecuteReader();
105

106
                    if (!reader.HasRows)
107
                    {
108
                        db.Close();
109
                        throw new ApplicationException("There is a problem: the Game is not installed in Amazon Launcher");
110
                    }
111

112
                    while (reader.Read())
113
                        gameInstallPath = reader.GetString(0);
114

115
                    db.Close();
116
                }
117

118
                var exe = GetAmazonGameExecutable(gameInstallPath);
119
                if (string.IsNullOrEmpty(exe))
120
                    throw new ApplicationException("There is a problem: Game is not installed");
121

122
                return Path.GetFileNameWithoutExtension(exe);
123
            }
124

125
            throw new ApplicationException("There is a problem: Amazon Launcher is not installed or the Game is not installed");
126
        }
127

128
        private static string GetAmazonGameExecutable(string path)
129
        {
130
            string fuelFile = Path.Combine(path, "fuel.json");
131
            string gameexe = null;
132

133
            if (!File.Exists(fuelFile))
134
                throw new ApplicationException("There is a problem: game executable cannot be found");
135

136
            var json = DynamicJson.Load(fuelFile);
137

138
            var jsonMain = json.GetObject("Main");
139
            if (jsonMain == null)
140
                return null;
141

142
            gameexe = jsonMain["Command"];
143

144
            if (!string.IsNullOrEmpty(gameexe))
145
                return gameexe;
146

147
            return null;
148
        }
149

150
    }
151
}
152

153
namespace EmulatorLauncher.Common.Launchers.Amazon
154
{
155
    public class AmazonInstalledGameInfo
156
    {
157
        public string Id { get; set; }
158
        public string InstallDirectory { get; set; }
159
        public int Installed { get; set; }
160
        public string ProductTitle { get; set; }
161
        public string ProductAsin { get; set; }
162
        public string ManifestSignature { get; set; }
163
        public string ManifestSignatureKeyId { get; set; }
164
    }
165
}
166

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.