1+ #pragma once
2+ #include " LookAndFeel.h"
3+
4+ #include < config.h>
5+ #include < juce_audio_utils/juce_audio_utils.h>
6+
7+ constexpr auto OWNER = " atkAudio" ;
8+ constexpr auto DISPLAY_NAME = PLUGIN_DISPLAY_NAME;
9+ constexpr auto REPO = " PluginForObsRelease" ;
10+ constexpr auto VERSION = PLUGIN_VERSION;
11+ constexpr auto JSON_VALUE = " tag_name" ;
12+ constexpr auto FILENAME = " atkaudio-pluginforobs.zip" ;
13+
14+ class UpdateCheck
15+ : public juce::ModalComponentManager::Callback
16+ , public juce::DeletedAtShutdown
17+ {
18+ public:
19+ UpdateCheck (const juce::String& repoOwner = OWNER, const juce::String& repoName = REPO)
20+ : DeletedAtShutdown()
21+ , owner(repoOwner)
22+ , repo(repoName)
23+ {
24+ checkForUpdate ();
25+ }
26+
27+ juce::String getValueFromJson (const juce::String& jsonString, const juce::String& key)
28+ {
29+ juce::var json = juce::JSON::parse (jsonString);
30+ if (json.isObject ())
31+ {
32+ auto jsonObject = json.getDynamicObject ();
33+ if (jsonObject->hasProperty (key))
34+ return jsonObject->getProperty (key).toString ();
35+ }
36+ return {};
37+ }
38+
39+ bool isNewerVersionThanCurrent (
40+ const juce::String& remoteVersion, //
41+ const juce::String& localVersion = VERSION
42+ )
43+ {
44+ jassert (remoteVersion.isNotEmpty ());
45+
46+ auto remoteTokens = juce::StringArray::fromTokens (remoteVersion, " ." , {});
47+ auto localTokens = juce::StringArray::fromTokens (localVersion, " ." , {});
48+
49+ if (remoteTokens[0 ].getIntValue () == localTokens[0 ].getIntValue ())
50+ {
51+ if (remoteTokens[1 ].getIntValue () == localTokens[1 ].getIntValue ())
52+ return remoteTokens[2 ].getIntValue () > localTokens[2 ].getIntValue ();
53+
54+ return remoteTokens[1 ].getIntValue () > localTokens[1 ].getIntValue ();
55+ }
56+
57+ return remoteTokens[0 ].getIntValue () > localTokens[0 ].getIntValue ();
58+ }
59+
60+ void checkForUpdate ()
61+ {
62+ juce::URL versionURL (" https://api.github.com/repos/" + owner + " /" + repo + " /releases/latest" );
63+
64+ std::unique_ptr<juce::InputStream> inStream (versionURL.createInputStream (
65+ juce::URL::InputStreamOptions (juce::URL::ParameterHandling::inAddress).withConnectionTimeoutMs (5000 )
66+ ));
67+
68+ if (inStream == nullptr )
69+ return ;
70+
71+ auto remoteVersionString = inStream->readEntireStreamAsString ().trim ();
72+
73+ if (remoteVersionString.isEmpty ())
74+ return ;
75+
76+ auto currentVersionString = VERSION;
77+ remoteVersionString = getValueFromJson (remoteVersionString, JSON_VALUE);
78+
79+ auto isRemoteVersionNewer = isNewerVersionThanCurrent (remoteVersionString);
80+
81+ if (isRemoteVersionNewer)
82+ {
83+ auto res = juce::AlertWindow::showOkCancelBox (
84+ juce::AlertWindow::InfoIcon,
85+ PLUGIN_DISPLAY_NAME,
86+ " A new version is available: " + remoteVersionString + " \n Would you like to download it?" ,
87+ " Download" ,
88+ " Cancel" ,
89+ nullptr ,
90+ this
91+ );
92+ }
93+ }
94+
95+ private:
96+ void modalStateFinished (int returnValue) override
97+ {
98+ if (returnValue != 0 )
99+ juce::URL (" https://github.com/" + owner + " /" + repo + " /releases/latest/download/" + FILENAME)
100+ .launchInDefaultBrowser ();
101+ }
102+
103+ juce::String owner;
104+ juce::String repo;
105+
106+ JUCE_DECLARE_SINGLETON (UpdateCheck, true )
107+ };
0 commit comments