/
PlatformAbstraction.cpp
122 lines (111 loc) · 2.86 KB
/
PlatformAbstraction.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
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2001, Sven Eberhardt
* Copyright (c) 2010-2016, The OpenClonk Team and contributors
*
* Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details.
*
* "Clonk" is a registered trademark of Matthes Bender, used with permission.
* See accompanying file "TRADEMARK" for details.
*
* To redistribute this file separately, substitute the full license texts
* for the above references.
*/
#include "C4Include.h"
#include "game/C4Application.h"
#ifdef _WIN32
#include "platform/C4windowswrapper.h"
#include <shellapi.h>
bool OpenURL(const char *szURL)
{
return (intptr_t)ShellExecuteW(nullptr, L"open", GetWideChar(szURL), nullptr, nullptr, SW_SHOW) > 32;
}
bool EraseItemSafe(const char *szFilename)
{
char Filename[_MAX_PATH+1];
SCopy(szFilename, Filename, _MAX_PATH);
Filename[SLen(Filename)+1]=0;
auto wide_filename = GetWideChar(Filename, true); // wide_filename holds the buffer
SHFILEOPSTRUCTW shs;
shs.hwnd=nullptr;
shs.wFunc=FO_DELETE;
shs.pFrom = wide_filename;
shs.pTo=nullptr;
shs.fFlags=FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT;
shs.fAnyOperationsAborted=false;
shs.hNameMappings=nullptr;
shs.lpszProgressTitle=nullptr;
auto error = SHFileOperationW(&shs);
return !error;
}
bool IsGermanSystem()
{
return PRIMARYLANGID(GetUserDefaultLangID()) == LANG_GERMAN;
}
#elif !defined(__APPLE__)
bool IsGermanSystem()
{
if (strstr(setlocale(LC_MESSAGES, nullptr), "de"))
return true;
else
return false;
}
bool EraseItemSafe(const char *szFilename)
{
return false;
}
#if defined(WITH_QT_EDITOR)
#undef LineFeed
#include <QDesktopServices>
#include <QUrl>
bool OpenURL(char const* url)
{
return QDesktopServices::openUrl(QUrl::fromUserInput(url));
}
#else
bool OpenURL(char const*) {return 0;}
#endif
#endif
bool RestartApplication(std::vector<const char *> parameters)
{
// restart with given parameters
bool success = false;
#ifdef _WIN32
wchar_t buf[_MAX_PATH];
DWORD sz = ::GetModuleFileName(::GetModuleHandle(nullptr), buf, _MAX_PATH);
if (sz)
{
StdStrBuf params;
for (auto p : parameters)
{
params += R"(")";
params += p;
params += R"(" )";
}
intptr_t iError = (intptr_t)::ShellExecute(nullptr, nullptr, buf, params.GetWideChar(), Config.General.ExePath.GetWideChar(), SW_SHOW);
if (iError > 32) success = true;
}
#elif defined(PROC_SELF_EXE)
pid_t pid;
switch (pid = fork())
{
case -1: break; // error message shown below
case 0:
{
std::vector<const char*> params = {"openclonk"};
params.insert(params.end(), parameters.begin(), parameters.end());
params.push_back(nullptr);
execv(PROC_SELF_EXE, const_cast<char *const *>(params.data()));
perror("editor launch failed");
exit(1);
}
default:
success = true;
}
#endif
// must quit ourselves for new instance to be shown
if (success) Application.Quit();
return success;
}