forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkinInstaller.cpp
243 lines (214 loc) · 6.43 KB
/
SkinInstaller.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
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
/* Copyright (C) 2011 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#include "StdAfx.h"
#include "DialogPackage.h"
#include "DialogInstall.h"
#include "resource.h"
#include "SkinInstaller.h"
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
GlobalData g_Data;
OsNameVersion g_OsNameVersions[] =
{
{ L"XP", L"5.1" },
{ L"Vista", L"6.0" },
{ L"7", L"6.1" },
{ L"8", L"6.2" },
{ L"10", L"10.0" }
};
/*
** Entry point
**
*/
EXTERN_C int SkinInstallerMain(LPWSTR lpCmdLine)
{
// Avoid loading a dll from current directory
SetDllDirectory(L"");
HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (FAILED(hr)) return 1;
InitCommonControls();
if (lpCmdLine[0] == L'"')
{
// Strip quotes
++lpCmdLine;
WCHAR* pos = wcsrchr(lpCmdLine, L'"');
if (pos)
{
*pos = L'\0';
}
}
WCHAR buffer[MAX_PATH] = { 0 };
GetModuleFileName(GetInstanceHandle(), buffer, _countof(buffer));
// Remove the module's name from the path
WCHAR* pos = wcsrchr(buffer, L'\\');
if (pos)
{
*(pos + 1) = L'\0';
}
g_Data.programPath = g_Data.settingsPath = buffer;
wcscat(buffer, L"Rainmeter.ini");
// Find the settings file and read skins path off it
if (_waccess_s(buffer, 0) == 0)
{
g_Data.iniFile = buffer;
if (GetPrivateProfileString(L"Rainmeter", L"SkinPath", L"", buffer, _countof(buffer), buffer) > 0)
{
g_Data.skinsPath = buffer;
if (g_Data.skinsPath.back() != L'\\' && g_Data.skinsPath.back() != L'/')
{
g_Data.skinsPath += L'\\';
}
}
else
{
g_Data.skinsPath = g_Data.programPath;
g_Data.skinsPath += L"Skins\\";
}
}
else
{
hr = SHGetFolderPath(nullptr, CSIDL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, buffer);
wcscat(buffer, L"\\Rainmeter\\");
g_Data.settingsPath = buffer;
wcscat(buffer, L"Rainmeter.ini");
g_Data.iniFile = buffer;
if (SUCCEEDED(hr) && _waccess_s(buffer, 0) == 0)
{
if (GetPrivateProfileString(L"Rainmeter", L"SkinPath", L"", buffer, _countof(buffer), buffer) > 0)
{
g_Data.skinsPath = buffer;
if (g_Data.skinsPath.back() != L'\\' && g_Data.skinsPath.back() != L'/')
{
g_Data.skinsPath += L'\\';
}
}
else
{
std::wstring error = L"SkinPath not found.\nMake sure that Rainmeter has been run at least once.";
MessageBox(nullptr, error.c_str(), L"Rainmeter Skin Installer", MB_ERROR);
return 1;
}
}
else
{
std::wstring error = L"Rainmeter.ini not found.\nMake sure that Rainmeter has been run at least once.";
MessageBox(nullptr, error.c_str(), L"Rainmeter Skin Installer", MB_ERROR);
return 1;
}
}
std::wstring layoutsPath = g_Data.settingsPath + L"Layouts\\";
if (_waccess_s(layoutsPath.c_str(), 0) != 0)
{
// Migrate Themes into Layouts for backwards compatibility and rename
// Rainmeter.thm to Rainmeter.ini and RainThemes.bmp to Wallpaper.bmp.
std::wstring themesPath = g_Data.settingsPath + L"Themes";
if (_waccess_s(themesPath.c_str(), 0) == 0)
{
// Migrate Themes into Layouts for backwards compatibility and rename
// Rainmeter.thm to Rainmeter.ini and RainThemes.bmp to Wallpaper.bmp.
MoveFile(themesPath.c_str(), layoutsPath.c_str());
layoutsPath += L'*'; // For FindFirstFile.
WIN32_FIND_DATA fd = { 0 };
HANDLE hFind = FindFirstFile(layoutsPath.c_str(), &fd);
layoutsPath.pop_back(); // Remove '*'.
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
wcscmp(L".", fd.cFileName) != 0 &&
wcscmp(L"..", fd.cFileName) != 0)
{
std::wstring layoutFolder = layoutsPath + fd.cFileName;
layoutFolder += L'\\';
std::wstring file = layoutFolder + L"Rainmeter.thm";
if (_waccess_s(file.c_str(), 0) == 0)
{
std::wstring newFile = layoutFolder + L"Rainmeter.ini";
MoveFile(file.c_str(), newFile.c_str());
}
file = layoutFolder + L"RainThemes.bmp";
if (_waccess_s(file.c_str(), 0) == 0)
{
std::wstring newFile = layoutFolder + L"Wallpaper.bmp";
MoveFile(file.c_str(), newFile.c_str());
}
}
}
while (FindNextFile(hFind, &fd));
FindClose(hFind);
}
}
}
if (_wcsnicmp(lpCmdLine, L"/LoadTheme ", 11) == 0)
{
// For backwards compatibility.
std::wstring args = L"!LoadLayout \"";
args += &lpCmdLine[11]; // Skip "/LoadTheme ".
args += L'"';
std::wstring file = g_Data.programPath + L"Rainmeter.exe";
SHELLEXECUTEINFO si = { sizeof(SHELLEXECUTEINFO) };
si.fMask = SEE_MASK_UNICODE;
si.lpFile = file.c_str();
si.lpParameters = args.c_str();
si.lpDirectory = g_Data.programPath.c_str();
si.nShow = SW_SHOWNORMAL;
ShellExecuteEx(&si);
return 0;
}
else if (wcscmp(lpCmdLine, L"/Packager") == 0)
{
DialogPackage::Create(GetInstanceHandle(), lpCmdLine);
}
else
{
DialogInstall::Create(GetInstanceHandle(), lpCmdLine);
}
return 0;
}
bool CloseRainmeterIfActive()
{
// Close Rainmeter.exe
HWND hwnd = FindWindow(L"DummyRainWClass", L"Rainmeter control window");
if (hwnd)
{
DWORD pID = 0UL, exitCode = 0UL;
GetWindowThreadProcessId(hwnd, &pID);
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, FALSE, pID);
PostMessage(hwnd, WM_DESTROY, 0, 0);
// Wait up to 5 seconds for Rainmeter to close
WaitForSingleObject(hProcess, 5000);
GetExitCodeProcess(hProcess, &exitCode);
CloseHandle(hProcess);
if (exitCode == STILL_ACTIVE)
{
return false;
}
}
return true;
}
HINSTANCE GetInstanceHandle()
{
return (HINSTANCE)&__ImageBase;
}
// -----------------------------------------------------------------------------------------------
// Stolen functions from Rainmeter Litestep.cpp, System.cpp, and Application.cpp
// -----------------------------------------------------------------------------------------------
bool IsRunning(const WCHAR* name, HANDLE* hMutex)
{
// Create mutex
HANDLE hMutexTmp = CreateMutex(nullptr, FALSE, name);
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
*hMutex = nullptr;
return true;
}
else
{
*hMutex = hMutexTmp;
return false;
}
}