Skip to content

Commit

Permalink
Add checking for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaoses-Ib committed May 1, 2022
1 parent 241cbf4 commit cc0502d
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 16 deletions.
17 changes: 16 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ FetchContent_Declare(IbPinyin
GIT_REPOSITORY https://github.com/Chaoses-Ib/IbPinyinLib.git
GIT_TAG e576e81ca06a297436bba7b124630b5d64e3106f
)
FetchContent_MakeAvailable(IbWinCpp IbDllHijack IbEverything IbPinyin)
FetchContent_Declare(IbUpdate
GIT_REPOSITORY https://github.com/Chaoses-Ib/IbUpdateLib.git
GIT_TAG eaa36c76e29409aeb89b1d7f493f48d0b54d819c
)
FetchContent_MakeAvailable(IbWinCpp IbDllHijack IbEverything IbPinyin IbUpdate)

find_package(yaml-cpp CONFIG REQUIRED)
# Detours
Expand Down Expand Up @@ -72,6 +76,17 @@ target_link_libraries(IbEverythingExt
PRIVATE yaml-cpp
)


# update
add_executable(update WIN32
update/update.cpp
)
target_link_libraries(update
PRIVATE yaml-cpp
PRIVATE IbUpdate
)


# test
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
option(BUILD_TESTING "Build the testing tree." OFF)
Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* [热键](#热键)
* [键列表](#键列表)
* [配置](#配置-1)
* [其它](#其它)
* [检查更新](#检查更新)
* [使用技巧](#使用技巧)
* [快速启动器](#快速启动器)
* [硬盘清理](#硬盘清理)
Expand Down Expand Up @@ -64,7 +66,7 @@
如果使用的是 Everything v1.5a,因为 Alpha 版默认启用了命名实例,大部分程序都不支持调用,需要[通过配置关闭命名实例](https://github.com/Chaoses-Ib/IbEverythingExt/issues/5)

### 配置
`IbEverythingExt.yaml` 文件:
`config.yaml` 文件:
```yaml
# 拼音搜索
pinyin_search:
Expand Down Expand Up @@ -125,7 +127,7 @@ Edit 模式详见 [Edit 模式](docs/pinyin_search/edit_mode.md)。
![](docs/quick_select_dark_mode.png)

### 配置
`IbEverythingExt.yaml` 文件:
`config.yaml` 文件:
```yaml
# 快速选择
quick_select:
Expand Down Expand Up @@ -167,6 +169,19 @@ quick_select:
input_mode: Auto
```

## 其它
### 检查更新
`config.yaml` 文件:
```yaml
# 更新
update:
# 检查更新
check: true
# 包括预览版
prerelease: false
```

## 使用技巧
一些 Everything 的使用技巧。

Expand Down
26 changes: 20 additions & 6 deletions source/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ bool config_init() {

config.enable = false;

wchar_t path[MAX_PATH];
GetModuleFileNameW(nullptr, path, std::size(path));
PathRemoveFileSpecW(path);
PathAppendW(path, L"IbEverythingExt.yaml");
wchar_t root_path[MAX_PATH];
GetModuleFileNameW(nullptr, root_path, std::size(root_path));
PathRemoveFileSpecW(root_path);
PathAppendW(root_path, LR"(IbEverythingExt\)");

wchar_t config_path[MAX_PATH];
wcscpy_s(config_path, root_path);
PathAppendW(config_path, LR"(config.yaml)");

std::ifstream in(path);
std::ifstream in(config_path);
if (!in) {
MessageBoxW(nullptr, L"配置文件 IbEverythingExt.yaml 不存在!", L"IbEverythingExt", MB_ICONERROR);
MessageBoxW(nullptr, L"配置文件 config.yaml 不存在!", L"IbEverythingExt", MB_ICONERROR);
return false;
}
try {
Expand Down Expand Up @@ -114,6 +118,16 @@ bool config_init() {
}()
};
}

if (config.update.check = root["update"]["check"].as<bool>()) {
wchar_t update_path[MAX_PATH];
wcscpy_s(update_path, root_path);
PathAppendW(update_path, L"update.exe");
config.update.update_path = update_path;

// to avoid checking on system startup and when calling Everything through command line,
// we do the check after the main window is created
}
}
catch (YAML::Exception& e) {
MessageBoxA(nullptr, ("配置文件读取错误:\n"s + e.what()).c_str(), "IbEverythingExt", MB_ICONERROR);
Expand Down
5 changes: 5 additions & 0 deletions source/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ struct Config {

quick::InputMode input_mode;
} quick_select;

struct {
bool check;
std::wstring update_path;
} update;
};
extern Config config;

Expand Down
15 changes: 15 additions & 0 deletions source/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ HWND WINAPI CreateWindowExW_detour(
if (class_name == class_everything) {
if (config.pinyin_search.enable)
pinyin_search->everything_created();

if (config.update.check) {
wchar_t cmd_line[] = L"--quiet";
STARTUPINFOW startup_info{ sizeof STARTUPINFOW };
PROCESS_INFORMATION process_info{};

if (CreateProcessW(config.update.update_path.c_str(), cmd_line, nullptr, nullptr, false, 0, nullptr, nullptr, &startup_info, &process_info)) {
CloseHandle(process_info.hThread);
CloseHandle(process_info.hProcess);
}

// only check once
config.update.check = false;
config.update.update_path = {};
}
} else if (class_name == L"Edit"sv) {
if (config.pinyin_search.enable) {
wchar_t buf[std::size(L"EVERYTHING_TOOLBAR")];
Expand Down
14 changes: 7 additions & 7 deletions source/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// Chinese (Simplified, PRC) resources
// Chinese (Simplified, China) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
Expand Down Expand Up @@ -51,8 +51,8 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 0,5,0,1
PRODUCTVERSION 0,5,0,1
FILEVERSION 0,6,0,3
PRODUCTVERSION 0,6,0,3
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -68,13 +68,13 @@ BEGIN
BLOCK "080404b0"
BEGIN
VALUE "CompanyName", "https://github.com/Chaoses-Ib/IbEverythingExt"
VALUE "FileDescription", "Everything ƴ��������չ"
VALUE "FileVersion", "0.5.0.1"
VALUE "FileDescription", "Everything ƴ������������ѡ����չ"
VALUE "FileVersion", "0.6.0.3"
VALUE "InternalName", "IbEverythingExt.dll"
VALUE "LegalCopyright", "Copyright (C) 2022 ����Ib"
VALUE "OriginalFilename", "WindowsCodecs.dll"
VALUE "ProductName", "IbEverythingExt"
VALUE "ProductVersion", "0.5.0.1"
VALUE "ProductVersion", "0.6.0.3"
END
END
BLOCK "VarFileInfo"
Expand All @@ -83,7 +83,7 @@ BEGIN
END
END

#endif // Chinese (Simplified, PRC) resources
#endif // Chinese (Simplified, China) resources
/////////////////////////////////////////////////////////////////////////////


Expand Down
100 changes: 100 additions & 0 deletions update/update.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <Shlwapi.h>
#include <fstream>
#include <yaml-cpp/yaml.h>
#include <IbUpdate/GitHubUpdater.hpp>

#pragma comment(lib, "Shlwapi.lib")

std::wstring u8_to_u16(std::string u8) {
int u16_size = MultiByteToWideChar(CP_UTF8, 0, u8.c_str(), (int)u8.length(), nullptr, 0);
std::wstring u16(u16_size, L'\0');
MultiByteToWideChar(CP_UTF8, 0, u8.c_str(), (int)u8.length(), u16.data(), u16_size);
return u16;
}

/// <summary>
///
/// </summary>
/// <param name="s"></param>
/// <param name="lines"></param>
/// <returns>Ends with '\n'</returns>
std::string truncate_lines(std::string s, size_t lines) {
std::istringstream in(s);
std::ostringstream out;

size_t i = 0;
for (std::string line; i < lines && std::getline(in, line); i++) {
out << line << '\n';
}
if (i == lines)
out << u8"……\n";
return out.str();
}

bool check_for_update(bool prerelease, bool quiet, bool silent_error) {
try {
GitHubUpdater updater{ "Chaoses-Ib", "IbEverythingExt", "v0.6-alpha.3" };
YAML::Node release = updater.check_for_new_release(prerelease);
if (release.IsNull()) {
if (!quiet)
MessageBoxW(nullptr, L"无可用更新", L"IbEverythingExt", MB_ICONINFORMATION);
return true;
}

std::wostringstream ss;
ss << L"检测到新版本 " << u8_to_u16(release["tag_name"].as<std::string>())
<< L"\n发布时间:" << u8_to_u16(release["published_at"].as<std::string>())
<< L"\n更新内容:\n" << u8_to_u16(truncate_lines(release["body"].as<std::string>(), 10))
<< L"\n是否打开发布页面?";
if (MessageBoxW(nullptr, ss.str().c_str(), L"IbEverythingExt", MB_ICONINFORMATION | MB_YESNO) == IDYES) {
ShellExecuteW(nullptr, nullptr, u8_to_u16(release["html_url"].as<std::string>()).c_str(), nullptr, nullptr, SW_SHOWNORMAL);
}
return true;
}
catch (std::runtime_error& e) {
if (!silent_error) {
std::ostringstream ss;
ss << "检查更新失败:\n" << e.what();
MessageBoxA(nullptr, ss.str().c_str(), "IbEverythingExt", MB_ICONWARNING);
}
return false;
}
}

int wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nShowCmd) {
using namespace std::literals;

// lpCmdLine is empty for unknown reason
//MessageBoxW(nullptr, lpCmdLine, nullptr, 0);
//MessageBoxW(nullptr, GetCommandLineW(), nullptr, 0);
lpCmdLine = GetCommandLineW();

bool quiet = false;
if (std::wstring_view(lpCmdLine) == L"--quiet")
quiet = true;

wchar_t path[MAX_PATH];
GetModuleFileNameW(nullptr, path, std::size(path));
PathRemoveFileSpecW(path);
PathAppendW(path, L"config.yaml");

std::ifstream in(path);
if (!in) {
MessageBoxW(nullptr, L"配置文件 config.yaml 不存在!", L"IbEverythingExt", MB_ICONERROR);
return 1;
}
try {
YAML::Node root = YAML::Load(in);
YAML::Node update = root["update"];
if (update["check"].as<bool>()) {
// try twice
if (!check_for_update(update["prerelease"].as<bool>(), quiet, true))
check_for_update(update["prerelease"].as<bool>(), quiet, false);
}
}
catch (YAML::Exception& e) {
MessageBoxA(nullptr, ("配置文件读取错误:\n"s + e.what()).c_str(), "IbEverythingExt", MB_ICONERROR);
return 1;
}
return 0;
}

0 comments on commit cc0502d

Please sign in to comment.