/
portable.h
199 lines (166 loc) · 5.13 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
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
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;
}
bool IsCustomIniExist()
{
std::wstring path = GetAppDir() + L"\\config.ini";
if (PathFileExists(path.data()))
{
return true;
}
return false;
}
// GetUserDataDir retrieves the user data directory path from the config file.
// It first tries to read the data dir from the "data" key in the "dir_setting" section.
// If that fails, it falls back to a default relative to the app dir.
// It expands any environment variables in the path.
std::wstring GetUserDataDir()
{
std::wstring configFilePath = GetAppDir() + L"\\config.ini";
if (!PathFileExists(configFilePath.c_str()))
{
return GetAppDir() + L"\\..\\Data";
}
TCHAR userDataBuffer[MAX_PATH];
::GetPrivateProfileStringW(L"dir_setting", L"data", L"", userDataBuffer, MAX_PATH, configFilePath.c_str());
std::wstring expandedPath = ExpandEnvironmentPath(userDataBuffer);
// Expand %app%
ReplaceStringInPlace(expandedPath, L"%app%", GetAppDir());
std::wstring dataDir;
dataDir = GetAbsolutePath(expandedPath);
wcscpy(userDataBuffer, dataDir.c_str());
return std::wstring(userDataBuffer);
}
// GetDiskCacheDir retrieves the disk cache directory path from the config file.
// It first tries to read the cache dir from the "cache" key in the "dir_setting" section.
// If that fails, it falls back to a default relative to the app dir.
// It expands any environment variables in the path.
std::wstring GetDiskCacheDir()
{
std::wstring configFilePath = GetAppDir() + L"\\config.ini";
if (!PathFileExists(configFilePath.c_str()))
{
return GetAppDir() + L"\\..\\Cache";
}
TCHAR cacheDirBuffer[MAX_PATH];
::GetPrivateProfileStringW(L"dir_setting", L"cache", L"", cacheDirBuffer, MAX_PATH, configFilePath.c_str());
std::wstring expandedPath = ExpandEnvironmentPath(cacheDirBuffer);
// Expand %app%
ReplaceStringInPlace(expandedPath, L"%app%", GetAppDir());
std::wstring cacheDir;
cacheDir = GetAbsolutePath(expandedPath);
wcscpy(cacheDirBuffer, cacheDir.c_str());
return std::wstring(cacheDirBuffer);
}
// 构造新命令行
// Parses the command line param into individual arguments and inserts
// additional arguments for portable mode.
//
// param: The command line passed to the application.
//
// Returns: The modified command line with additional args.
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"--gopher");
// 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);
}
}