forked from Wargus/wargus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstratagus-gameutils.h
325 lines (280 loc) · 8.86 KB
/
stratagus-gameutils.h
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
/*
_________ __ __
/ _____// |_____________ _/ |______ ____ __ __ ______
\_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
/ \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
/_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
\/ \/ \//_____/ \/
______________________ ______________________
T H E W A R B E G I N S
Stratagus - A free fantasy real time strategy game engine
stratagus-game-launcher.h - Stratagus Game Launcher
Copyright (C) 2015-2016 The Stratagus Developers
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef STRATAGUS_GAMEUTILS_H
#define STRATAGUS_GAMEUTILS_H
void error(const char* title, const char* text);
void mkdir_p(const char* path);
void copy_dir(const char* source_folder, const char* target_folder);
#if __APPLE__
#define USE_MAC
#endif
#if ( defined (_MSC_VER) || defined (_WIN32) || defined (_WIN64) ) && ! defined (WIN32)
#define WIN32 1
#endif
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#endif
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifdef WIN32
// set everything to winxp sp2 compatiblity
#define NTDDI_VERSION 0x05010200
#define _WIN32_WINNT 0x0501
#define WINVER 0x0501
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
#include <Shlwapi.h>
#include <Shlobj.h>
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "Shlwapi.lib")
#include <direct.h>
#define mkdir(f, m) _mkdir(f)
// PathRemoveFileSpec on a drive (e.g. when extracting from CD) will leave the trailing \... remove that
#define parentdir(x) PathRemoveFileSpec(x); if (x[strlen(x) - 1] == '\\') x[strlen(x) - 1] = '\0'
#else
#if defined(USE_MAC)
#define parentdir(x) strcpy(x, dirname(x))
#else
#define parentdir(x) dirname(x)
#endif
#endif
#ifdef WIN32
#include <windows.h>
#include <wincon.h>
#include <process.h>
#define QUOTE "\""
#else
#include <ftw.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/wait.h>
#define QUOTE "'"
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <istream>
#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#elif __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
error "Missing the <filesystem> header."
#endif
#include "stratagus-tinyfiledialogs.h"
#ifdef WIN32
#define BUFF_SIZE MAX_PATH
#else
#define BUFF_SIZE 4096
#endif
void error(const char* title, const char* text) {
tinyfd_messageBox(title, text, "ok", "error", 1);
exit(-1);
}
void mkdir_p(const char* path) {
fs::create_directories(path);
}
void copy_dir(fs::path source_folder, fs::path target_folder) {
if (fs::exists(target_folder)) {
if (fs::equivalent(source_folder, target_folder)) {
return;
}
// first delete the target_folder, if it exists, to ensure clean slate
fs::remove_all(target_folder);
} else {
// make the parentdir of the target folder
fs::create_directories(target_folder.parent_path());
}
// now copy the new folder in its place
fs::copy(source_folder, target_folder, fs::copy_options::recursive | fs::copy_options::overwrite_existing);
}
#ifdef WIN32
char* GetExtractionLogPath(const char* game_name, char* data_path) {
static char *marker = (char*)calloc(MAX_PATH, sizeof(char));
if (marker[0] != '\0') {
return marker;
}
char logname[MAX_PATH];
strcpy(logname, game_name);
strcat(logname, "-extraction.log");
if (PathCombine(marker, data_path, "portable-install")) {
if (PathFileExists(marker)) {
PathCombine(marker, data_path, logname);
return marker;
}
}
SHGetFolderPathA(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL, 0, marker);
PathAppend(marker, "Stratagus");
mkdir_p(marker);
PathAppend(marker, logname);
return marker;
}
#endif
#ifdef WIN32
// quoting logic taken from "Everyone quotes command line arguments the wrong way" by Daniel Colascione
/*++
Routine Description:
This routine appends the given argument to a command line such
that CommandLineToArgvW will return the argument string unchanged.
Arguments in a command line should be separated by spaces; this
function does not add these spaces.
Arguments:
Argument - Supplies the argument to encode.
CommandLine - Supplies the command line to which we append the encoded argument string.
Force - Supplies an indication of whether we should quote
the argument even if it does not contain any characters that would
ordinarily require quoting.
Return Value:
None.
Environment:
Arbitrary.
--*/
void ArgvQuote(const std::wstring& Argument, std::wstring& CommandLine, bool Force) {
//
// Unless we're told otherwise, don't quote unless we actually
// need to do so --- hopefully avoid problems if programs won't
// parse quotes properly
//
if (Force == false && Argument.empty () == false && Argument.find_first_of(L" \t\n\v\"") == Argument.npos) {
CommandLine.append(Argument);
} else {
CommandLine.push_back(L'"');
for (auto It = Argument.begin(); ; ++It) {
unsigned NumberBackslashes = 0;
while (It != Argument.end() && *It == L'\\') {
++It;
++NumberBackslashes;
}
if (It == Argument.end()) {
//
// Escape all backslashes, but let the terminating
// double quotation mark we add below be interpreted
// as a metacharacter.
//
CommandLine.append(NumberBackslashes * 2, L'\\');
break;
} else if (*It == L'"') {
//
// Escape all backslashes and the following
// double quotation mark.
//
CommandLine.append(NumberBackslashes * 2 + 1, L'\\');
CommandLine.push_back(*It);
} else {
//
// Backslashes aren't special here.
//
CommandLine.append(NumberBackslashes, L'\\');
CommandLine.push_back(*It);
}
}
CommandLine.push_back(L'"');
}
}
int runCommand(std::wstring& file, std::vector<std::wstring> argv, bool echo = false, std::wstring *outputCommandline = NULL) {
std::wstring cmdline;
std::wstring executable;
ArgvQuote(file, executable, false);
for (size_t i = 0; i < argv.size(); i++) {
std::wstring arg = argv[i];
ArgvQuote(arg, cmdline, false);
if (i + 1 < argv.size()) {
cmdline.push_back(L' ');
}
}
std::wstring cmdcmdline;
for (auto c : cmdline) {
if (c == L'(' || c == L')' || c == L'%' || c == L'!' || c == L'^' || c == L'"' || c == L'<' || c == L'>' || c == L'&' || c == L'|') {
cmdcmdline.push_back(L'^');
}
cmdcmdline.push_back(c);
}
if (argv.size() > 0) {
cmdcmdline = std::wstring(L"@") + executable + std::wstring(L" ") + cmdcmdline;
} else {
cmdcmdline = std::wstring(L"@") + executable;
}
if (outputCommandline != NULL) {
outputCommandline->append(cmdcmdline);
}
if (echo) {
std::wcout << executable << L' ' << cmdline << L'\n';
std::wcout << cmdcmdline << L'\n';
}
_flushall();
int code = _wsystem(cmdcmdline.c_str());
if (code == -1) {
std::wcout << _wcserror(errno) << L'\n';
}
return code;
}
#else
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int runCommand(const char *file, char *const argv[], bool echo = false, std::string *outputCommandline = NULL) {
pid_t pid = fork();
if (echo || outputCommandline) {
std::string commandline = file;
for (int i = 0; ; i++) {
if (argv[i] == NULL) {
break;
}
commandline += " ";
commandline += argv[i];
}
if (echo) {
std::cout << commandline << std::endl;
}
if (outputCommandline) {
outputCommandline->append(commandline);
}
}
if (pid == 0) {
// child
exit(execvp(file, argv));
} else {
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
return WEXITSTATUS(status);
} else {
return -1;
}
}
}
#endif
#endif