/
portable.h
146 lines (123 loc) · 3.32 KB
/
portable.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
std::wstring QuoteSpaceIfNeeded(const std::wstring &str)
{
if (str.find(L' ') == std::wstring::npos)
return std::move(str);
std::wstring escaped(L"\"");
for (auto c : str)
{
if (c == L'"')
escaped += L'"';
escaped += c;
}
escaped += L'"';
return std::move(escaped);
}
std::wstring JoinArgsString(std::vector<std::wstring> lines, const std::wstring &delimiter)
{
std::wstring text;
bool first = true;
for (auto &line : lines)
{
if (!first)
text += delimiter;
else
first = false;
text += QuoteSpaceIfNeeded(line);
}
return text;
}
bool IsExistsPortable()
{
std::wstring path = GetAppDir() + L"\\portable";
if (PathFileExists(path.data()))
{
return true;
}
return false;
}
bool IsNeedPortable()
{
return true;
static bool need_portable = IsExistsPortable();
return need_portable;
}
std::wstring GetUserDataDir()
{
std::wstring path = GetAppDir() + L"\\..\\Data";
TCHAR temp[MAX_PATH];
::PathCanonicalize(temp, path.data());
return temp;
}
std::wstring GetDiskCacheDir()
{
std::wstring path = GetAppDir() + L"\\..\\Cache";
TCHAR temp[MAX_PATH];
::PathCanonicalize(temp, path.data());
return temp;
}
// 构造新命令行
std::wstring GetCommand(LPWSTR param)
{
std::vector<std::wstring> args;
int argc;
LPWSTR *argv = CommandLineToArgvW(param, &argc);
int insert_pos = 0;
for (int i = 0; i < argc; i++)
{
if (wcscmp(argv[i], L"--") == 0)
{
break;
}
if (wcscmp(argv[i], L"--single-argument") == 0)
{
break;
}
insert_pos = i;
}
for (int i = 0; i < argc; i++)
{
// 保留原来参数
if (i)
args.push_back(argv[i]);
// 追加参数
if (i == insert_pos)
{
args.push_back(L"--shuax");
// args.push_back(L"--force-local-ntp");
// args.push_back(L"--disable-background-networking");
args.push_back(L"--disable-features=RendererCodeIntegrity,FlashDeprecationWarning");
// if (IsNeedPortable())
{
auto diskcache = GetDiskCacheDir();
wchar_t temp[MAX_PATH];
wsprintf(temp, L"--disk-cache-dir=%s", diskcache.c_str());
args.push_back(temp);
}
{
auto userdata = GetUserDataDir();
wchar_t temp[MAX_PATH];
wsprintf(temp, L"--user-data-dir=%s", userdata.c_str());
args.push_back(temp);
}
}
}
LocalFree(argv);
return JoinArgsString(args, L" ");
}
void Portable(LPWSTR param)
{
wchar_t path[MAX_PATH];
::GetModuleFileName(NULL, path, MAX_PATH);
std::wstring args = GetCommand(param);
SHELLEXECUTEINFO sei = {0};
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
sei.lpVerb = L"open";
sei.lpFile = path;
sei.nShow = SW_SHOWNORMAL;
sei.lpParameters = args.c_str();
if (ShellExecuteEx(&sei))
{
ExitProcess(0);
}
}