-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_data.hpp
More file actions
177 lines (149 loc) · 5.58 KB
/
update_data.hpp
File metadata and controls
177 lines (149 loc) · 5.58 KB
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
#pragma once
#include <juce_core/juce_core.h>
namespace pluginshared {
class UpdateData {
public:
static constexpr int kNetworkTimeout = 500; // ms
struct GithubInfo {
std::string_view owner;
std::string_view repo_name;
};
UpdateData(GithubInfo info)
: update_thread_(info)
, github_info_(info) {
is_thread_run_ = update_thread_.startThread();
}
~UpdateData() {
if (is_thread_run_) {
update_thread_.Quit();
}
}
void BeginCheck() {
update_thread_.BeginCheck();
}
bool IsComplete() const {
return update_thread_.is_complete_;
}
juce::String GetUpdateMessage() const {
return update_thread_.GetUpdateMessage();
}
juce::String GetButtonLabel() const {
return update_thread_.GetButtonLabel();
}
bool HaveNewVersion() const {
return update_thread_.have_new_version_;
}
juce::String GetPluginReleaseUrl() {
juce::String s{"https://github.com/"};
s << github_info_.owner.data() << "/" << github_info_.repo_name.data() << "/releases/latest";
return s;
}
private:
class UpdateThread : public juce::Thread {
public:
UpdateThread(GithubInfo info)
: juce::Thread("version check")
, github_info_(info) {}
void run() override {
for (;;) {
get_sem_.wait();
if (threadShouldExit()) {
return;
}
juce::String api_url{"https://api.github.com/repos/"};
api_url << github_info_.owner.data() << "/" << github_info_.repo_name.data() << "/releases/latest";
juce::URL url(api_url);
auto op =
juce::URL::InputStreamOptions{juce::URL::ParameterHandling::inAddress}.withConnectionTimeoutMs(
kNetworkTimeout);
auto stream = url.createInputStream(op);
if (!stream) {
SetUpdateMessage("Network error or API rate limit");
SetButtonLabel("ok");
is_complete_ = true;
continue;
}
// 解析 JSON
auto response = stream->readEntireStreamAsString();
auto json = juce::JSON::fromString(response);
if (json.isObject()) {
if (json.hasProperty("status")) {
if (json.getProperty("status", "404").toString() == "404") {
SetUpdateMessage("Plugin not found on Github, Please report this issue");
SetButtonLabel("ok");
is_complete_ = true;
continue;
}
}
// 获取最新版本号,例如 "v1.2.0"
juce::String latest_version = json.getProperty("tag_name", "v" JucePlugin_VersionString).toString();
// GitHub 标签通常带 'v',对比前处理掉
juce::String current_version = JucePlugin_VersionString;
if (latest_version.startsWithIgnoreCase("v")) latest_version = latest_version.substring(1);
if (latest_version != current_version) {
juce::String msg;
msg << "New version available: " << latest_version << "\n"
<< "Current version: " << current_version << "\n\n"
<< json.getProperty("body", "What? No update description?").toString();
SetUpdateMessage(msg);
SetButtonLabel("ok");
have_new_version_ = true;
}
else {
SetUpdateMessage("You are up to date.");
SetButtonLabel("ok");
have_new_version_ = false;
}
}
else {
SetUpdateMessage("Payload error from GitHub");
SetButtonLabel("ok");
}
is_complete_ = true;
}
}
void BeginCheck() {
is_complete_ = false;
have_new_version_ = false;
get_sem_.signal();
{
juce::ScopedLock lock{data_lock_};
update_message_.clear();
button_text_.clear();
}
}
void Quit() {
get_sem_.signal();
stopThread(-1);
}
juce::String GetUpdateMessage() const {
juce::ScopedLock lock{data_lock_};
return update_message_;
}
juce::String GetButtonLabel() const {
juce::ScopedLock lock{data_lock_};
return button_text_;
}
private:
void SetUpdateMessage(const juce::String& message) {
juce::ScopedLock lock{data_lock_};
update_message_ = message;
}
void SetButtonLabel(const juce::String& label) {
juce::ScopedLock lock{data_lock_};
button_text_ = label;
}
friend class UpdateData;
std::atomic<bool> is_complete_{false};
std::atomic<bool> have_new_version_{false};
juce::WaitableEvent get_sem_{0};
juce::CriticalSection data_lock_;
juce::String update_message_;
juce::String button_text_;
GithubInfo github_info_;
};
UpdateThread update_thread_;
bool is_thread_run_{};
GithubInfo github_info_;
};
} // namespace pluginshared