forked from rime/weasel
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathWeaselSetup.cpp
199 lines (176 loc) · 6.36 KB
/
WeaselSetup.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
// WeaselSetup.cpp : main source file for WeaselSetup.exe
//
#include "stdafx.h"
#include "resource.h"
#include "WeaselUtility.h"
#include <thread>
#include "InstallOptionsDlg.h"
#include <ShellScalingApi.h>
#pragma comment(lib, "Shcore.lib")
CAppModule _Module;
static int Run(LPTSTR lpCmdLine);
int WINAPI _tWinMain(HINSTANCE hInstance,
HINSTANCE /*hPrevInstance*/,
LPTSTR lpstrCmdLine,
int /*nCmdShow*/) {
HRESULT hRes = ::CoInitialize(NULL);
ATLASSERT(SUCCEEDED(hRes));
AtlInitCommonControls(
ICC_BAR_CLASSES); // add flags to support other controls
hRes = _Module.Init(NULL, hInstance);
ATLASSERT(SUCCEEDED(hRes));
LANGID langId = get_language_id();
SetThreadUILanguage(langId);
SetThreadLocale(langId);
int nRet = Run(lpstrCmdLine);
_Module.Term();
::CoUninitialize();
return nRet;
}
int install(bool hant, bool silent, bool old_ime_support);
int uninstall(bool silent);
bool has_installed();
static std::wstring install_dir() {
WCHAR exe_path[MAX_PATH] = {0};
GetModuleFileNameW(GetModuleHandle(NULL), exe_path, _countof(exe_path));
std::wstring dir(exe_path);
size_t pos = dir.find_last_of(L"\\");
dir.resize(pos);
return dir;
}
static int CustomInstall(bool installing) {
bool hant = false;
bool silent = false;
bool old_ime_support = false;
std::wstring user_dir;
const WCHAR KEY[] = L"Software\\Rime\\Weasel";
HKEY hKey;
LSTATUS ret = RegOpenKey(HKEY_CURRENT_USER, KEY, &hKey);
if (ret == ERROR_SUCCESS) {
WCHAR value[MAX_PATH];
DWORD len = sizeof(value);
DWORD type = 0;
DWORD data = 0;
ret =
RegQueryValueEx(hKey, L"RimeUserDir", NULL, &type, (LPBYTE)value, &len);
if (ret == ERROR_SUCCESS && type == REG_SZ) {
user_dir = value;
}
len = sizeof(data);
ret = RegQueryValueEx(hKey, L"Hant", NULL, &type, (LPBYTE)&data, &len);
if (ret == ERROR_SUCCESS && type == REG_DWORD) {
hant = (data != 0);
if (installing)
silent = true;
}
RegCloseKey(hKey);
}
bool _has_installed = has_installed();
if (!silent) {
InstallOptionsDialog dlg;
dlg.installed = _has_installed;
dlg.hant = hant;
dlg.user_dir = user_dir;
if (IDOK != dlg.DoModal()) {
if (!installing)
return 1; // aborted by user
} else {
hant = dlg.hant;
user_dir = dlg.user_dir;
old_ime_support = dlg.old_ime_support;
_has_installed = dlg.installed;
}
}
if (!_has_installed)
if (0 != install(hant, silent, old_ime_support))
return 1;
ret = SetRegKeyValue(HKEY_CURRENT_USER, KEY, L"RimeUserDir", user_dir.c_str(),
REG_SZ, false);
if (FAILED(HRESULT_FROM_WIN32(ret))) {
MSG_BY_IDS(IDS_STR_ERR_WRITE_USER_DIR, IDS_STR_INSTALL_FAILED,
MB_ICONERROR | MB_OK);
return 1;
}
ret = SetRegKeyValue(HKEY_CURRENT_USER, KEY, L"Hant", (hant ? 1 : 0),
REG_DWORD, false);
if (FAILED(HRESULT_FROM_WIN32(ret))) {
MSG_BY_IDS(IDS_STR_ERR_WRITE_HANT, IDS_STR_INSTALL_FAILED,
MB_ICONERROR | MB_OK);
return 1;
}
if (_has_installed) {
std::wstring dir(install_dir());
std::thread th([dir]() {
ShellExecuteW(NULL, NULL, (dir + L"\\WeaselServer.exe").c_str(), L"/q",
NULL, SW_SHOWNORMAL);
Sleep(500);
ShellExecuteW(NULL, NULL, (dir + L"\\WeaselServer.exe").c_str(), L"",
NULL, SW_SHOWNORMAL);
Sleep(500);
ShellExecuteW(NULL, NULL, (dir + L"\\WeaselDeployer.exe").c_str(),
L"/deploy", NULL, SW_SHOWNORMAL);
});
th.detach();
MSG_BY_IDS(IDS_STR_MODIFY_SUCCESS_INFO, IDS_STR_MODIFY_SUCCESS_CAP,
MB_ICONINFORMATION | MB_OK);
}
return 0;
}
LPCTSTR GetParamByPrefix(LPCTSTR lpCmdLine, LPCTSTR prefix) {
return (wcsncmp(lpCmdLine, prefix, wcslen(prefix)) == 0)
? (lpCmdLine + wcslen(prefix))
: 0;
}
static int Run(LPTSTR lpCmdLine) {
constexpr bool silent = true;
constexpr bool old_ime_support = false;
bool uninstalling = !wcscmp(L"/u", lpCmdLine);
if (uninstalling)
return uninstall(silent);
if (auto res = GetParamByPrefix(lpCmdLine, L"/userdir:")) {
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel",
L"RimeUserDir", res, REG_SZ);
}
if (!wcscmp(L"/ls", lpCmdLine)) {
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel",
L"Language", L"chs", REG_SZ);
} else if (!wcscmp(L"/lt", lpCmdLine)) {
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel",
L"Language", L"cht", REG_SZ);
} else if (!wcscmp(L"/le", lpCmdLine)) {
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel",
L"Language", L"eng", REG_SZ);
}
if (!wcscmp(L"/eu", lpCmdLine)) {
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel\\Updates",
L"CheckForUpdates", L"1", REG_SZ);
}
if (!wcscmp(L"/du", lpCmdLine)) {
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel\\Updates",
L"CheckForUpdates", L"0", REG_SZ);
}
if (!wcscmp(L"/toggleime", lpCmdLine)) {
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel",
L"ToggleImeOnOpenClose", L"yes", REG_SZ);
}
if (!wcscmp(L"/toggleascii", lpCmdLine)) {
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel",
L"ToggleImeOnOpenClose", L"no", REG_SZ);
}
if (!wcscmp(L"/testing", lpCmdLine)) {
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel",
L"UpdateChannel", L"testing", REG_SZ);
}
if (!wcscmp(L"/release", lpCmdLine)) {
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel",
L"UpdateChannel", L"release", REG_SZ);
}
bool hans = !wcscmp(L"/s", lpCmdLine);
if (hans)
return install(false, silent, old_ime_support);
bool hant = !wcscmp(L"/t", lpCmdLine);
if (hant)
return install(true, silent, old_ime_support);
bool installing = !wcscmp(L"/i", lpCmdLine);
return CustomInstall(installing);
}