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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/*
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>
class Executable;
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
{
// an application shortcut that can be either on the desktop or the start menu
//
class Shortcut
{
public:
// location of a shortcut
//
enum Locations
{
None = 0,
// on the desktop
Desktop,
// in the start menu
StartMenu
};
// empty shortcut
//
Shortcut();
// shortcut from an executable
//
explicit Shortcut(const Executable& exe);
// sets the name of the shortcut, shown on icons and start menu entries
//
Shortcut& name(const QString& s);
// the program to start
//
Shortcut& target(const QString& s);
// arguments to pass
//
Shortcut& arguments(const QString& s);
// shows in the status bar of explorer, for example
//
Shortcut& description(const QString& s);
// path to a binary that contains the icon and its index
//
Shortcut& icon(const QString& s, int index=0);
// "start in" option for this shortcut
//
Shortcut& workingDirectory(const QString& s);
// returns whether this shortcut already exists at the given location; this
// does not check whether the shortcut parameters are different, it merely if
// the .lnk file exists
//
bool exists(Locations loc) const;
// calls remove() if exists(), or add()
//
bool toggle(Locations loc);
// adds the shortcut to the given location
//
bool add(Locations loc);
// removes the shortcut from the given location
//
bool remove(Locations loc);
private:
QString m_name;
QString m_target;
QString m_arguments;
QString m_description;
QString m_icon;
int m_iconIndex;
QString m_workingDirectory;
// returns the path where the shortcut file should be saved
//
QString shortcutPath(Locations loc) const;
// returns the directory where the shortcut file should be saved
//
QString shortcutDirectory(Locations loc) const;
// returns the filename of the shortcut file that should be used when saving
//
QString shortcutFilename() const;
};
// 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;
};
// represents a security product, such as an antivirus or a firewall
//
class SecurityProduct
{
public:
SecurityProduct(
QString name, int provider,
bool active, bool upToDate);
// display name of the product
//
const QString& name() const;
// a bunch of _WSC_SECURITY_PROVIDER flags
//
int provider() const;
// whether the product is active
//
bool active() const;
// whether its definitions are up-to-date
//
bool upToDate() const;
// string representation of the above
//
QString toString() const;
private:
QString m_name;
int m_provider;
bool m_active;
bool m_upToDate;
};
// 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 security products
//
const std::vector<SecurityProduct>& securityProducts() const;
private:
std::vector<Module> m_modules;
WindowsInfo m_windows;
std::vector<SecurityProduct> m_security;
std::vector<Module> getLoadedModules() const;
std::vector<SecurityProduct> getSecurityProducts() const;
std::vector<SecurityProduct> getSecurityProductsFromWMI() const;
std::optional<SecurityProduct> getWindowsFirewall() const;
};
enum class CoreDumpTypes
{
Mini = 1,
Data,
Full
};
// creates a minidump file for the given process
//
bool coredump(CoreDumpTypes type);
// finds another process with the same name as this one and creates a minidump
// file for it
//
bool coredumpOther(CoreDumpTypes type);
} // namespace env
MOBase::VersionInfo createVersionInfo();
} // namespace MOShared
#endif // UTIL_H
|