-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelfCheck.cs
228 lines (191 loc) · 8.94 KB
/
SelfCheck.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.IO;
using MMI_SP.Common;
namespace MMI_SP
{
internal static class SelfCheck
{
private class Dependency
{
public string FileName { get => _fileName; }
private readonly string _fileName;
public string FilePath { get => _filePath; }
private readonly string _filePath;
public string FullPath { get => $@"{_filePath}\{_fileName}"; }
public Version Version { get => _version; }
private readonly Version _version;
public Dependency(string fileName, string version, string filePath = null)
{
if (filePath == null)
{
filePath = AppDomain.CurrentDomain.BaseDirectory;
}
_fileName = fileName;
_filePath = filePath;
_version = new Version(version);
}
}
private enum VisualCppVersion
{
Visual_2017,
Visual_2015,
Visual_2013,
Visual_2012,
Visual_2010,
Visual_2008,
Visual_2005,
}
private static readonly Dictionary<VisualCppVersion, Version> visualCVersion = new Dictionary<VisualCppVersion, Version>
{
{ VisualCppVersion.Visual_2017, new Version("14.1.0.0")},
{ VisualCppVersion.Visual_2015, new Version("14.0.0.0")},
{ VisualCppVersion.Visual_2013, new Version("12.0.0.0")},
{ VisualCppVersion.Visual_2012, new Version("11.0.0.0")},
{ VisualCppVersion.Visual_2010, new Version("10.0.0.0")},
{ VisualCppVersion.Visual_2008, new Version("9.0.0.0")},
{ VisualCppVersion.Visual_2005, new Version("8.0.0.0")}
};
private static Version RequiredVisualCppVersion { get => visualCVersion[_requiredVisualCppVersion]; }
private readonly static VisualCppVersion _requiredVisualCppVersion = VisualCppVersion.Visual_2015;
private static Version RequiredDotNetVersion { get => _requiredDotNetVersion; }
private readonly static Version _requiredDotNetVersion = new Version("4.8.0.0");
private static List<Dependency> Dependencies { get => _dependencies; }
private readonly static List<Dependency> _dependencies = new List<Dependency>
{
new Dependency("ScriptHookVDotNet2.dll", "2.10.13.0", AppDomain.CurrentDomain.BaseDirectory + @"\.."),
new Dependency("SHVDN-Extender.dll", "1.0.0.1"),
new Dependency("iFruitAddon2.dll", "2.1.0.0"),
new Dependency("NativeUI.dll", "1.9.0.0")
};
internal static bool Check()
{
return CheckDependencies() && CheckVisualCppVersion() && CheckDotNetVersion();
}
private static bool CheckDependencies()
{
bool installed = true;
Logger.Debug("Checking dependencies versions...");
foreach (Dependency dependency in Dependencies)
{
Logger.Debug($"Checking {dependency.FileName} version...");
if (File.Exists(dependency.FullPath))
{
Version installedVersion = new Version(FileVersionInfo.GetVersionInfo(dependency.FullPath).ProductVersion);
Logger.Debug($"{dependency.FileName} version: {installedVersion} (expected {dependency.Version})");
if (installedVersion < dependency.Version)
{
if (Config.ShowFileNotification) Utils.ShowNotification("CHAR_BLOCKED", "MMI-SP", dependency.FileName + " " + installedVersion + " is outdated!", "Download and install the latest version.");
Logger.Error(dependency.FileName + " " + installedVersion + " is outdated!");
installed = false;
}
}
else
{
if (Config.ShowFileNotification) Utils.ShowNotification("CHAR_BLOCKED", "MMI-SP", dependency.FileName + " is missing!", "Download and install this file before starting the game.");
Logger.Error(dependency.FullPath + " file is missing!");
installed = false;
}
}
Logger.Debug("Dependencies checked");
return installed;
}
private static bool CheckVisualCppVersion()
{
bool installed = true;
Logger.Debug("Checking Visual C++ version...");
if (GetVisualCppVersion() < RequiredVisualCppVersion)
{
if (Config.ShowVisualCNotification) Utils.ShowNotification("CHAR_BLOCKED", "MMI-SP", "Microsoft Visual C++ is missing!", "Download and install version 2015 or 2017 x64.");
Logger.Error("Microsoft Visual C++ 2015 and 2017 x64 is missing!");
installed = false;
}
Logger.Debug("Visual C++ version checked");
return installed;
}
private static bool CheckDotNetVersion()
{
bool installed = true;
Logger.Debug("Checking .NET version...");
if (GetNETFrameworkVersion() < RequiredDotNetVersion)
{
if (Config.ShowNETFrameworkNotification) Utils.ShowNotification("CHAR_BLOCKED", "MMI-SP", "Microsoft .NET Framework is outdated!", "Download and install version 4.8 or later.");
Logger.Error("Microsoft .NET Framework is outdated!");
installed = false;
}
Logger.Debug(".NET version checked");
return installed;
}
private static Version GetVisualCppVersion()
{
Version highestVersion = new Version(0, 0, 0, 0);
string[] filters = { "msvcp*.dll", "msvcr*.dll" };
List<string> files = new List<string>();
// Gathering Visual C++ system files
Logger.Debug("Gathering Visual C++ system files...");
foreach (string filter in filters)
{
files.AddRange(Directory.GetFiles(Environment.SystemDirectory, filter));
}
Logger.Debug($"Visual C++ files found {string.Join(", ", files.ToArray())}");
// Retrieving highest version
Logger.Debug("Retrieving highest Visual C++ version...");
foreach (string file in files)
{
Version currentFileVersion = new Version(FileVersionInfo.GetVersionInfo(file).ProductVersion);
if (currentFileVersion > highestVersion) highestVersion = currentFileVersion;
}
Logger.Debug($"Highest Visual C++ version found: {highestVersion}");
return highestVersion;
}
private static Version GetNETFrameworkVersion()
{
Version highestVersion = new Version(0, 0, 0, 0);
try
{
// Checking registry key to find .Net Framework v4 version
foreach (string regKey in new string[]{ @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client\", @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" })
{
Logger.Debug("Checking registry key: " + regKey);
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(regKey))
{
Logger.Debug("Checking registry value 'Version'");
if (ndpKey?.GetValue("Version") != null)
{
return new Version((string)ndpKey.GetValue("Version"));
}
Logger.Debug("Checking registry value 'Release'");
if (ndpKey?.GetValue("Release") != null)
{
int releaseKey = (int)ndpKey.GetValue("Release");
if (releaseKey >= 528449)
{
return new Version("4.8.0.0");
}
else if (releaseKey >= 461308)
{
return new Version("4.7.1.0");
}
else if (releaseKey >= 460798)
{
return new Version("4.7.0.0");
}
else
{
return new Version("4.0.0.0");
}
}
}
}
}
catch (Exception e)
{
Logger.Error(e.Message);
}
return highestVersion;
}
}
}