summaryrefslogtreecommitdiff
path: root/indra/newview/llvelopack.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'indra/newview/llvelopack.cpp')
-rw-r--r--indra/newview/llvelopack.cpp231
1 files changed, 222 insertions, 9 deletions
diff --git a/indra/newview/llvelopack.cpp b/indra/newview/llvelopack.cpp
index 28e989c4ba..6a667925b3 100644
--- a/indra/newview/llvelopack.cpp
+++ b/indra/newview/llvelopack.cpp
@@ -330,14 +330,14 @@ static std::wstring get_desktop_path()
return L"";
}
-static bool create_shortcut(const std::wstring& shortcut_path,
+static HRESULT create_shortcut(const std::wstring& shortcut_path,
const std::wstring& target_path,
const std::wstring& arguments,
const std::wstring& description,
const std::wstring& icon_path)
{
HRESULT hr = CoInitialize(NULL);
- if (FAILED(hr)) return false;
+ if (FAILED(hr)) return hr;
IShellLinkW* shell_link = nullptr;
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
@@ -365,7 +365,7 @@ static bool create_shortcut(const std::wstring& shortcut_path,
}
CoUninitialize();
- return SUCCEEDED(hr);
+ return hr;
}
static void register_protocol_handler(const std::wstring& protocol,
@@ -405,9 +405,172 @@ static void register_protocol_handler(const std::wstring& protocol,
}
}
-void clear_nsis_links()
+static bool get_shortcut_target(const std::wstring& lnk_path, std::wstring& target_path_str)
+{
+ // Resolve the shortcut to check its target
+ wchar_t target_path[MAX_PATH] = { 0 };
+ HRESULT hr = CoInitialize(NULL);
+ bool res = false;
+ if (SUCCEEDED(hr))
+ {
+ IShellLinkW* psl = nullptr;
+ hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (void**)&psl);
+ if (SUCCEEDED(hr))
+ {
+ IPersistFile* ppf = nullptr;
+ hr = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
+ if (SUCCEEDED(hr))
+ {
+ hr = ppf->Load(lnk_path.c_str(), STGM_READ);
+ if (SUCCEEDED(hr))
+ {
+ // Resolve() in theory may retarget to a different executable,
+ // but we should be fine as long as folder stays the same,
+ // otherwise we shouldn't clear it.
+ hr = psl->Resolve(NULL, SLR_NO_UI | SLR_NOUPDATE | SLR_ANY_MATCH);
+ if (SUCCEEDED(hr))
+ {
+ hr = psl->GetPath(target_path, MAX_PATH, nullptr, 0);
+ if (SUCCEEDED(hr))
+ {
+ target_path_str = std::wstring(target_path);
+ res = true;
+ }
+ }
+ }
+ ppf->Release();
+ }
+ psl->Release();
+ }
+ CoUninitialize();
+ }
+ return res;
+}
+
+static bool paths_are_equal(const std::wstring& path1, const std::wstring& path2)
+{
+ try
+ {
+ std::error_code ec1, ec2;
+ std::filesystem::path p1(path1);
+ std::filesystem::path p2(path2);
+
+ // Get canonical (absolute, normalized) paths
+ auto canonical1 = std::filesystem::canonical(p1, ec1);
+ auto canonical2 = std::filesystem::canonical(p2, ec2);
+
+ // If either path doesn't exist, fall back to string comparison
+ if (ec1 || ec2)
+ {
+ // Normalize case for comparison
+ std::wstring lower1 = path1;
+ std::wstring lower2 = path2;
+ std::transform(lower1.begin(), lower1.end(), lower1.begin(), ::towlower);
+ std::transform(lower2.begin(), lower2.end(), lower2.begin(), ::towlower);
+ while (!lower1.empty() && (lower1.back() == L'\\' || lower1.back() == L'/'))
+ {
+ lower1.pop_back();
+ }
+ while (!lower2.empty() && (lower2.back() == L'\\' || lower2.back() == L'/'))
+ {
+ lower2.pop_back();
+ }
+ return lower1 == lower2;
+ }
+
+ // Use filesystem::equivalent which handles all path variations
+ return std::filesystem::equivalent(canonical1, canonical2, ec1);
+ }
+ catch (const std::filesystem::filesystem_error&)
+ {
+ // Fallback to case-insensitive string comparison
+ std::wstring lower1 = path1;
+ std::wstring lower2 = path2;
+ std::transform(lower1.begin(), lower1.end(), lower1.begin(), ::towlower);
+ std::transform(lower2.begin(), lower2.end(), lower2.begin(), ::towlower);
+ while (!lower1.empty() && (lower1.back() == L'\\' || lower1.back() == L'/'))
+ {
+ lower1.pop_back();
+ }
+ while (!lower2.empty() && (lower2.back() == L'\\' || lower2.back() == L'/'))
+ {
+ lower2.pop_back();
+ }
+ return lower1 == lower2;
+ }
+}
+
+static void update_taskbar_shortcut(const std::string& nsis_folder_path, const std::wstring& appdata_path, const std::wstring& app_name, const std::wstring& link_name)
+{
+ std::wstring taskbar_path = appdata_path;
+ taskbar_path += L"\\Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\TaskBar\\";
+ taskbar_path += link_name;
+
+ if (!PathFileExistsW(taskbar_path.c_str()))
+ {
+ // User didn't create one
+ return;
+ }
+
+ // Verify we are removing or updating the right thing.
+ // It is not warranteed that the shortcut points to NSIS.
+ std::wstring target_path;
+ if (get_shortcut_target(taskbar_path, target_path))
+ {
+ // Strip the filename part to get just the folder path
+ std::filesystem::path trim_path(target_path);
+ if (trim_path.has_filename())
+ {
+ target_path = trim_path.parent_path().wstring();
+ }
+ std::wstring nsis_path_w = ll_convert<std::wstring>(nsis_folder_path);
+ if (!paths_are_equal(nsis_path_w, target_path))
+ {
+ // Shortcut points to something else, don't mess with it
+ LL_INFOS("Velopack") << "Found a matching shortcut, but it points to a different channel. Expected: "
+ << ll_convert_wide_to_string(nsis_path_w)
+ << ", actual: " << ll_convert_wide_to_string(target_path)
+ << LL_ENDL;
+ return;
+ }
+ }
+
+ // First try to overwrite shortcut
+ std::wstring current_exe = ll_convert<std::wstring>(gDirUtilp->getExecutablePathAndName());
+ HRESULT hr = create_shortcut(taskbar_path, current_exe, L"", app_name, current_exe);
+ if (SUCCEEDED(hr))
+ {
+ LL_INFOS("Velopack") << "Successfully updated taskbar shortcut to point to current exe" << LL_ENDL;
+ return;
+ }
+
+ // Try deleting and if possible, recreating separately. We should not leave hanging shortcuts behind.
+ if (!DeleteFileW(taskbar_path.c_str()))
+ {
+ DWORD error = GetLastError();
+ if (error != ERROR_FILE_NOT_FOUND)
+ {
+ LL_WARNS("Velopack") << "Failed to delete NSIS taskbar shortcut: "
+ << ll_convert_wide_to_string(taskbar_path)
+ << " (error: " << error << ")" << LL_ENDL;
+ }
+ }
+ else
+ {
+ // Deleted user created link, recreate it with new target if possible.
+ LL_INFOS("Velopack") << "Updating taskbar shortcut to point to current exe" << LL_ENDL;
+ HRESULT hr = create_shortcut(taskbar_path, current_exe, L"", app_name, current_exe);
+ if (FAILED(hr))
+ {
+ LL_WARNS("Velopack") << "Failed to re-create taskbar shortcut, error: " << std::hex << hr << ", NSIS shortcut was removed" << LL_ENDL;
+ }
+ }
+}
+
+void clear_nsis_links(const std::string& nsis_folder_path)
{
wchar_t path[MAX_PATH];
+ std::wstring app_name = get_app_name();
// 1. The 'start' shortcuts set by nsis would be global, like app shortcut:
// C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Second Life Viewer\Second Life Viewer.lnk
@@ -415,7 +578,7 @@ void clear_nsis_links()
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_COMMON_PROGRAMS, NULL, 0, path)))
{
std::wstring start_menu_path = path;
- std::wstring folder_path = start_menu_path + L"\\" + get_app_name();
+ std::wstring folder_path = start_menu_path + L"\\" + app_name;
std::error_code ec;
std::filesystem::path dir(folder_path);
@@ -431,11 +594,11 @@ void clear_nsis_links()
}
// 2. Desktop link, also a global one.
- // C:\Users\Public\Desktop
+ // C:\Users\Public\Desktop\Second Life Viewer.lnk
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_COMMON_DESKTOPDIRECTORY, NULL, 0, path)))
{
std::wstring desktop_path = path;
- std::wstring shortcut_path = desktop_path + L"\\" + get_app_name() + L".lnk";
+ std::wstring shortcut_path = desktop_path + L"\\" + app_name + L".lnk";
if (!DeleteFileW(shortcut_path.c_str()))
{
DWORD error = GetLastError();
@@ -447,6 +610,19 @@ void clear_nsis_links()
}
}
}
+
+ // 3. Taskbar links, which are user-specific and located at:
+ // %AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\
+ // Note that it can be a link to velopack already or to a different NSIS viewer.
+ // Name might also be different based on method of creation.
+ if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, path)))
+ {
+ update_taskbar_shortcut(nsis_folder_path, path, app_name, L"Second Life.lnk"); // Window class name
+ update_taskbar_shortcut(nsis_folder_path, path, app_name, L"Second Life(1).lnk"); // Just in case user somehow did it twice and removed first
+ update_taskbar_shortcut(nsis_folder_path, path, app_name, L"SecondLifeViewer.lnk"); // Executable name
+ update_taskbar_shortcut(nsis_folder_path, path, app_name, L"secondlife-bin.lnk"); // Debug builds
+ update_taskbar_shortcut(nsis_folder_path, path, app_name, app_name + L".lnk"); // Default name
+ }
}
static void parse_version(const wchar_t* version_str, int& major, int& minor, int& patch, uint64_t& build)
@@ -462,7 +638,8 @@ bool get_nsis_version(
int& nsis_major,
int& nsis_minor,
int& nsis_patch,
- uint64_t& nsis_build)
+ uint64_t& nsis_build,
+ std::string& nsis_path)
{
// Test for presence of NSIS viewer registration, then
// attempt to read uninstall info
@@ -515,12 +692,21 @@ bool get_nsis_version(
}
std::error_code ec;
std::filesystem::path path(path_buffer);
+ // check if uninstaller exists
if (!std::filesystem::exists(path, ec))
{
return false;
}
- // Todo: check codesigning?
+ // We found a valid NSIS installation, trim the path to get the folder
+ wchar_t* pos = wcsstr(path_buffer, L"uninst.exe");
+ if (pos)
+ {
+ // Trim uninst.exe from the path by setting null terminator before it
+ *pos = L'\0';
+ }
+ // Note that it has a trailing backslash.
+ nsis_path = ll_convert_wide_to_string(path_buffer);
return true;
}
@@ -647,6 +833,33 @@ static void on_first_run(void* p_user_data, const char* app_version)
MultiByteToWideChar(CP_UTF8, 0, app_version, -1, &version[0], len);
register_uninstall_info(install_dir, app_name, version);
+
+ // Drop install related settings
+ // Unfortunately gDirUtilp is not initialized yet and it's shouldn't
+ // be possible to change location of the settings. For now it's simpler
+ // to hardcode the location.
+ std::optional<std::string> app_data = LLStringUtil::getoptenv("APPDATA");
+ if (app_data)
+ {
+ // Strip trailing delimiter if present
+ std::string app_data_path = *app_data;
+ if (!app_data_path.empty() && (app_data_path.back() == '\\' || app_data_path.back() == '/'))
+ {
+ app_data_path.pop_back();
+ }
+
+ std::string user_settings_path = app_data_path + "\\SecondLife\\user_settings\\settings.xml";
+ LLControlGroup settings("global");
+ if (settings.loadFromFile(user_settings_path))
+ {
+ // If user reinstalls or updates, we want to recheck for nsis leftovers.
+ if (settings.controlExists("PreviousInstallChecked"))
+ {
+ settings.setBOOL("PreviousInstallChecked", false);
+ }
+ settings.saveToFile(user_settings_path, true);
+ }
+ }
}
static void on_after_install(void* user_data, const char* app_version)