-
Notifications
You must be signed in to change notification settings - Fork 2
/
Helper.cpp
244 lines (200 loc) · 6.58 KB
/
Helper.cpp
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
#include "stdafx.h"
#include "Helper.h"
#include <io.h>
#include <assert.h>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <regex>
const static uint32_t MAX_PRINT_SIZE = 8192;
bool EShellMenu::FileExists(const tstring& fileName)
{
DWORD fileAttr;
fileAttr = GetFileAttributes(fileName.c_str());
switch( fileAttr )
{
case 0xFFFFFFFF:
return false;
case FILE_ATTRIBUTE_DIRECTORY:
return false;
default: // its a file
return true;
}
}
bool EShellMenu::DirectoryExists(const tstring& fileName)
{
DWORD fileAttr;
fileAttr = GetFileAttributes(fileName.c_str());
switch( fileAttr )
{
case 0xFFFFFFFF:
return false;
case FILE_ATTRIBUTE_DIRECTORY:
return true;
default: // its a file
return false;
}
}
bool EShellMenu::ExecuteCommand( const tstring& command, bool showWindow, bool block )
{
STARTUPINFO si;
memset( &si, 0, sizeof( si ) );
si.lpReserved = wxT("");
PROCESS_INFORMATION pi;
memset( &pi, 0, sizeof( pi ) );
bool success = true;
if ( !::CreateProcess(
NULL, // No module name (use command line)
(LPTSTR) command.c_str(), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
showWindow ? CREATE_NEW_CONSOLE : CREATE_NO_WINDOW, // Creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) ) // Pointer to PROCESS_INFORMATION structure
{
success = false;
}
if ( block )
{
DWORD waitResult = ::WaitForSingleObject( pi.hProcess, INFINITE );
DWORD error = 0x00;
BOOL getError = ::GetExitCodeProcess( pi.hProcess, &error );
if ( success && getError && error != 0x00 )
{
success = false;
}
}
::CloseHandle( pi.hProcess );
::CloseHandle( pi.hThread );
return success;
}
tstring EShellMenu::Capitalize( const tstring& str, const bool isGameName )
{
const tregex matchGameNamePattern( wxT("^.*[0-9]+.*$"), std::regex::icase );
tstring newStr = str;
if ( !newStr.empty() )
{
tsmatch matchResults;
if ( !isGameName || !std::regex_match( str, matchResults, matchGameNamePattern ) || newStr.length() > 4 )
{
// capitolize the first letter
std::transform( newStr.begin(), newStr.begin() + 1, newStr.begin(), toupper);
}
else
{
// capitolize the whole string
std::transform( newStr.begin(), newStr.end(), newStr.begin(), toupper);
}
}
return newStr;
}
tstring EShellMenu::RemoveSlashes( const tstring& str, const tstring& replace )
{
const tregex slash( wxT("[\\\\/]+") );
return std::regex_replace( str, slash, replace );
}
// Returns true if the version of the specified file could be found and passes back the
// version number. The version number is a 64-bit number that is made up of four parts:
// Compatible/Feature/Patch/Build. This app does not use the build number so it will
// always be zero.
bool EShellMenu::GetFileVersion( const tstring& path, uint64_t& version )
{
bool succeeded = false;
DWORD dwHandle = 0;
if ( !FileExists( path ) )
{
return false;
}
DWORD size = GetFileVersionInfoSize( ( LPTSTR )path.c_str(), &dwHandle );
if ( size > 0 )
{
tchar_t* versionBuf = new tchar_t[size];
if ( GetFileVersionInfo( ( LPTSTR )path.c_str(), dwHandle, size, ( LPVOID )versionBuf ) )
{
LPVOID buffer = NULL;
uint32_t length = 0;
if ( VerQueryValue( ( LPVOID )versionBuf, wxT("\\"), &buffer, &length ) )
{
VS_FIXEDFILEINFO* fileInfo = ( VS_FIXEDFILEINFO* )buffer;
version = ( ( ( uint64_t )fileInfo->dwFileVersionMS << 32 ) | fileInfo->dwFileVersionLS );
succeeded = true;
}
}
delete [] versionBuf;
}
return succeeded;
}
tstring EShellMenu::GetFileVersionString( uint64_t version )
{
tstringstream versionStr;
// Version number is 4 parts, but we are going to skip the build number.
versionStr
<< ( uint16_t )( version >> ( 16 * 3 ) ) << "."
<< ( uint16_t )( version >> ( 16 * 2 ) ) << "."
<< ( uint16_t )( version >> ( 16 * 1 ) );
return versionStr.str();
}
void EShellMenu::ConsolePrint(const tchar_t *fmt,...)
{
va_list args;
va_start(args, fmt);
static tchar_t string[ MAX_PRINT_SIZE ];
int size = _vsntprintf(string, sizeof(string), fmt, args);
string[ ( sizeof(string) / sizeof(tchar_t) ) - 1 ] = 0;
assert(size >= 0);
_ftprintf(stdout, wxT("%s"), string);
fflush(stdout);
va_end(args);
}
bool EShellMenu::GetEnvVar( const tstring& envVarName, tstring& envVarValue )
{
tchar_t envVarSetting[8192];
if ( ::GetEnvironmentVariable( envVarName.c_str(), envVarSetting, sizeof(envVarSetting) / sizeof(tchar_t) ) )
{
envVarValue = envVarSetting;
return true;
}
return false;
}
tstring EShellMenu::GetUserFile( const tstring& basename, const tstring& ext )
{
wxFileName name ( wxFileName::GetHomeDir(), basename );
name.AppendDir( ".eshell" );
name.SetExt( ext );
return tstring( name.GetFullPath().c_str() );
}
void EShellMenu::LoadTextFile( const tstring& file, std::set< tstring >& contents )
{
tifstream in( file.c_str() );
if ( !in.good() )
{
return;
}
tstring line;
while( getline( in, line ) )
{
contents.insert( line );
}
in.close();
}
bool EShellMenu::SaveTextFile( const tstring& file, const std::set< tstring >& contents )
{
wxFileName name ( file.c_str() );
name.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
tofstream out( file.c_str() );
if ( !out.good() )
{
return false;
}
std::set< tstring >::const_iterator itr = contents.begin();
std::set< tstring >::const_iterator end = contents.end();
for( ; itr != end; ++itr )
{
out << (*itr) << "\n";
}
out.close();
return true;
}