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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.
This file is part of Mod Organizer.
Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UTIL_H
#define UTIL_H
#include <string>
#include <optional>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <versioninfo.h>
namespace MOShared {
/// Test if a file (or directory) by the specified name exists
bool FileExists(const std::string &filename);
bool FileExists(const std::wstring &filename);
bool FileExists(const std::wstring &searchPath, const std::wstring &filename);
std::string ToString(const std::wstring &source, bool utf8);
std::wstring ToWString(const std::string &source, bool utf8);
std::string &ToLower(std::string &text);
std::string ToLower(const std::string &text);
std::wstring &ToLower(std::wstring &text);
std::wstring ToLower(const std::wstring &text);
bool CaseInsensitiveEqual(const std::wstring &lhs, const std::wstring &rhs);
namespace env
{
// represents one module
//
class Module
{
public:
explicit Module(QString path, std::size_t fileSize);
// returns the module's path
//
const QString& path() const;
// returns the module's path in lowercase and using forward slashes
//
QString displayPath() const;
// returns the size in bytes, may be 0
//
std::size_t fileSize() const;
// returns the x.x.x.x version embedded from the version info, may be empty
//
const QString& version() const;
// returns the FileVersion entry from the resource file, returns
// "(no version)" if not available
//
const QString& versionString() const;
// returns the build date from the version info, or the creation time of the
// file on the filesystem, may be empty
//
const QDateTime& timestamp() const;
// returns the md5 of the file, may be empty for system files
//
const QString& md5() const;
// converts timestamp() to a string for display, returns "(no timestamp)" if
// not available
//
QString timestampString() const;
// returns a string with all the above information on one line
//
QString toString() const;
private:
// contains the information from the version resource
//
struct FileInfo
{
VS_FIXEDFILEINFO ffi;
QString fileDescription;
};
QString m_path;
std::size_t m_fileSize;
QString m_version;
QDateTime m_timestamp;
QString m_versionString;
QString m_md5;
// returns information from the version resource
//
FileInfo getFileInfo() const;
// uses VS_FIXEDFILEINFO to build the version string
//
QString getVersion(const VS_FIXEDFILEINFO& fi) const;
// uses the file date from VS_FIXEDFILEINFO if available, or gets the
// creation date on the file
//
QDateTime getTimestamp(const VS_FIXEDFILEINFO& fi) const;
// returns the md5 hash unless the path contains "\windows\"
//
QString getMD5() const;
// gets VS_FIXEDFILEINFO from the file version info buffer
//
VS_FIXEDFILEINFO getFixedFileInfo(std::byte* buffer) const;
// gets FileVersion from the file version info buffer
//
QString getFileDescription(std::byte* buffer) const;
};
// a variety of information on windows
//
class WindowsInfo
{
public:
struct Version
{
DWORD major=0, minor=0, build=0;
QString toString() const
{
return QString("%1.%2.%3").arg(major).arg(minor).arg(build);
}
friend bool operator==(const Version& a, const Version& b)
{
return
a.major == b.major &&
a.minor == b.minor &&
a.build == b.build;
}
friend bool operator!=(const Version& a, const Version& b)
{
return !(a == b);
}
};
struct Release
{
// the BuildLab entry from the registry, may be empty
QString buildLab;
// product name such as "Windows 10 Pro", may not be in English, may be
// empty
QString productName;
// release ID such as 1809, may be mepty
QString ID;
// some sub-build number, undocumented, may be empty
DWORD UBR;
Release()
: UBR(0)
{
}
};
WindowsInfo();
// tries to guess whether this process is running in compatibility mode
//
bool compatibilityMode() const;
// returns the Windows version, may not correspond to the actual version
// if the process is running in compatibility mode
//
const Version& reportedVersion() const;
// tries to guess the real Windows version that's running, can be empty
//
const Version& realVersion() const;
// various information about the current release
//
const Release& release() const;
// whether this process is running as administrator, may be empty if the
// information is not available
std::optional<bool> isElevated() const;
// returns a string with all the above information on one line
//
QString toString() const;
private:
Version m_reported, m_real;
Release m_release;
std::optional<bool> m_elevated;
// uses RtlGetVersion() to get the version number as reported by Windows
//
Version getReportedVersion(HINSTANCE ntdll) const;
// uses RtlGetNtVersionNumbers() to get the real version number
//
Version getRealVersion(HINSTANCE ntdll) const;
// gets various information from the registry
//
Release getRelease() const;
// gets whether the process is elevated
//
std::optional<bool> getElevated() const;
};
class SecurityFeature
{
public:
SecurityFeature(QString name, DWORD state);
const QString& name() const;
QString toString() const;
private:
QString m_name;
DWORD m_state;
};
// represents the process's environment
//
class Environment
{
public:
Environment();
// list of loaded modules in the current process
//
const std::vector<Module>& loadedModules();
// information about the operating system
//
const WindowsInfo& windowsInfo() const;
// information about the installed antivirus
//
const std::vector<SecurityFeature>& securityFeatures() const;
private:
std::vector<Module> m_modules;
WindowsInfo m_windows;
std::vector<SecurityFeature> m_security;
void getLoadedModules();
void getSecurityFeatures();
};
} // namespace env
MOBase::VersionInfo createVersionInfo();
} // namespace MOShared
#endif // UTIL_H
|