UpdateCheck.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #pragma once
  2. // In-app update check (round 166b), the logic without JUCE (tested in isolation; the app side is ui/Updater.h):
  3. // - the GitHub releases list of Ka1-S/LYNX-SHARE (JSON) parsed into releases, drafts skipped, prereleases included;
  4. // - semantic-version comparison of a release tag ("v0.1.21-alpha") with the running version (CMake's "0.1.20");
  5. // - the build flavour LYNX runs as (installer, portable zip, Store, macOS, AppImage, tar.gz), the update method and
  6. // the release asset that flavour uses;
  7. // - the release's SHA256SUMS.txt line for a file;
  8. // - the release notes (Markdown) as readable lines;
  9. // - the settings file (automatic check on/off, the skipped version, the time of the last check, the version an update
  10. // replaced);
  11. // - going back to the previous version (round 190gh, B2): which release Help › "Go back to <version>..." installs.
  12. #include <cstdint>
  13. #include <string>
  14. #include <vector>
  15. namespace lynx::update
  16. {
  17. // --- versions ---------------------------------------------------------------------------------------------------
  18. struct Version
  19. {
  20. int major = 0, minor = 0, patch = 0;
  21. int revision = 0; // round 190hl: a fourth number ("0.1.28.1", a hotfix of 0.1.28); 0 when absent
  22. std::vector<std::string> pre; // "alpha", "1" for "-alpha.1"; empty for a release
  23. bool valid = false;
  24. std::string text() const; // "0.1.21-alpha"; "0.1.28.1-alpha" (the fourth number only when it is not 0)
  25. };
  26. // "v0.1.21-alpha", "0.1.21", "0.1" (patch 0), "0.1.28.1" (round 190hl), "V1.2.3-rc.1+build.5" (build metadata
  27. // ignored); invalid otherwise. (LYNX SHARE 0.1.28 and older read only two or three numbers: they drop a release whose
  28. // tag has four, so they never offer it.)
  29. Version parseVersion (const std::string& text);
  30. // <0, 0, >0 like strcmp: the numbers first (major, minor, patch, revision), then the prerelease identifiers compared
  31. // one by one by semver 2.0 precedence (numeric ones as numbers, below alphanumeric ones; a shorter list sorts first
  32. // when equal so far: "1.0.0-alpha" < "1.0.0-alpha.1" < "1.0.0-beta").
  33. // Round 190hl: a version WITHOUT a prerelease equals every prerelease of the same numbers ("0.1.28" == "0.1.28-alpha").
  34. // LYNX SHARE builds carry CMake's bare "0.1.28" while their release tag is "v0.1.28-alpha": the running build IS that
  35. // release, so it is neither an update for it nor a version to go back to (under strict semver "0.1.28-alpha" <
  36. // "0.1.28" and Help › "Go back to..." in 0.1.28 reinstalled 0.1.28 itself). Two prereleases still order by semver.
  37. int compare (const Version& a, const Version& b);
  38. // --- releases ---------------------------------------------------------------------------------------------------
  39. struct Asset
  40. {
  41. std::string name, url; // url = browser_download_url
  42. std::int64_t size = 0;
  43. };
  44. struct Release
  45. {
  46. std::string tag, name, body, page; // page = html_url
  47. bool draft = false, prerelease = false;
  48. std::vector<Asset> assets;
  49. Version version; // from the tag
  50. };
  51. // The GitHub API's answer for /repos/<o>/<r>/releases (an array) or /releases/<id> (one object). Releases whose tag
  52. // is not a version are dropped. `error` (optional) gets the reason when the text is not that JSON.
  53. std::vector<Release> parseReleases (const std::string& json, std::string* error = nullptr);
  54. // The newest non-draft release newer than `running`, or -1.
  55. int newestIndex (const std::vector<Release>& releases, const Version& running);
  56. // --- build flavours ---------------------------------------------------------------------------------------------
  57. enum class Flavour
  58. {
  59. windowsInstaller, // %LOCALAPPDATA%\Programs\LYNX with the NSIS installer's Uninstall.exe beside LYNX.exe
  60. windowsPortable, // the zip (or the bare LYNX.exe)
  61. store, // the Microsoft Store (MSIX) package: the Store updates it; LYNX never checks
  62. macos,
  63. linuxAppImage,
  64. linuxTarball,
  65. unknown
  66. };
  67. struct Facts
  68. {
  69. std::string os; // "windows", "macos", "linux"
  70. bool packaged = false; // Windows: a package identity (MSIX)
  71. bool uninstallerBesideExe = false;
  72. std::string appImagePath; // Linux: $APPIMAGE when it names an existing file
  73. std::string forced; // LYNX_UPDATE_FLAVOUR (tests): a flavourId() name wins over the facts
  74. };
  75. Flavour detectFlavour (const Facts& facts);
  76. const char* flavourId (Flavour); // "installer", "portable", "store", "macos", "appimage", "tarball", "unknown"
  77. Flavour flavourFromId (const std::string& id); // unknown for anything else
  78. // Whether that build looks for updates at all (the Store build never does).
  79. bool checksForUpdates (Flavour);
  80. enum class Method
  81. {
  82. runInstaller, // download setup.exe, verify, save the project, run it silently over the install, relaunch
  83. downloadAndReveal, // download the zip / dmg to Downloads, verify, show it in the file manager
  84. openPage, // open the release page in the browser
  85. appImageUpdate, // appimageupdatetool (zsync) in place, else download the new AppImage beside the old one
  86. none
  87. };
  88. Method methodFor (Flavour);
  89. // The asset a flavour uses (index into `assets`), or -1: the installer "*-setup-x64.exe", the portable
  90. // "*-windows-x64.zip", macOS "*.dmg", the AppImage "*-x86_64.AppImage", the tarball "*-linux-x86_64.tar.gz".
  91. int assetFor (Flavour, const std::vector<Asset>& assets);
  92. int checksumsAsset (const std::vector<Asset>& assets); // "SHA256SUMS.txt", or -1
  93. // --- checksums --------------------------------------------------------------------------------------------------
  94. // The lower-case SHA-256 hex of `fileName` in a `sha256sum` listing ("<hex> <name>" or "<hex> *<name>"), else "".
  95. std::string checksumFor (const std::string& sumsText, const std::string& fileName);
  96. // SHA-256 over a file read in chunks (a 60 MB AppImage is not held in memory twice); same result as sha256Hex.
  97. class Sha256Stream
  98. {
  99. public:
  100. Sha256Stream();
  101. void add (const void* data, std::size_t size);
  102. std::string finish(); // lower-case hex; the object is spent afterwards
  103. private:
  104. void block (const std::uint8_t* p);
  105. std::uint32_t h[8];
  106. std::uint8_t pending[64];
  107. std::size_t pendingSize = 0;
  108. std::uint64_t total = 0;
  109. };
  110. // `actual` (any case) equals `expected` and both are 64 hex digits.
  111. bool checksumMatches (const std::string& expected, const std::string& actual);
  112. // --- release notes ----------------------------------------------------------------------------------------------
  113. struct NoteLine
  114. {
  115. enum class Kind { heading, bullet, text, blank } kind = Kind::text;
  116. int level = 0; // heading: 1..6; bullet: the nesting depth (0 = top)
  117. std::string text; // without the Markdown marks: links keep their text, images, HTML tags and ** ` go
  118. };
  119. // Markdown as readable lines (headings, bullets, paragraphs; at most one blank line in a row; code fences kept as
  120. // plain text).
  121. std::vector<NoteLine> releaseNotes (const std::string& markdown);
  122. std::string releaseNotesText (const std::vector<NoteLine>&); // the same as plain text ("• " bullets)
  123. // --- settings (updates.txt in the settings folder) --------------------------------------------------------------
  124. struct Settings
  125. {
  126. bool automatic = true; // Settings › General › "Check for updates automatically"
  127. std::string skipped; // the version the user skipped (✕ on the bar): that one and older stay quiet
  128. std::int64_t lastCheckMs = 0; // when the release list was last fetched (the cache's age)
  129. // Round 190gh (B2): the version LYNX SHARE ran before its last update (written by the updater when it applies
  130. // or saves an update); Help › "Go back to <version>..." offers it. Empty: none recorded (an update made by a
  131. // version without this field, or none yet).
  132. std::string previous;
  133. };
  134. Settings parseSettings (const std::string& text);
  135. std::string formatSettings (const Settings&);
  136. // (Every version since 0.1.21 reads updates.txt line by line and ignores keys it does not know; "skipped" is read by
  137. // all of them, so a version left by going back is skipped in the older version too.)
  138. // Whether the bar shows `release`: a manual check always, an automatic one unless that version (or a newer one)
  139. // was skipped.
  140. bool shouldShow (const Release& release, const Settings& settings, bool manual);
  141. // --- going back (round 190gh, B2) --------------------------------------------------------------------------------
  142. // The release Help › "Go back to <version>..." installs, or -1: the recorded previous version when the list has it and
  143. // it is older than `running`; otherwise the newest non-draft release older than `running` (the version a user most
  144. // likely came from). Releases newer than or equal to `running` are never chosen.
  145. int rollbackIndex (const std::vector<Release>& releases, const Version& running, const std::string& previous);
  146. // The settings after an update from `from` was applied (or its file saved for the user to install): `from` becomes the
  147. // previous version. Nothing changes when `from` is not a version.
  148. Settings afterUpdate (Settings settings, const Version& from);
  149. // The settings after going back from `left`: `left` is skipped, so the automatic check does not offer it again at once
  150. // (Help › Check for updates still does), and the recorded previous version is used up.
  151. Settings afterRollback (Settings settings, const Version& left);
  152. // Round 190hl: only the skip of afterRollback (an already skipped newer version stays). Written BEFORE the Windows
  153. // installer of the older version runs: 0.1.27 and older don't know about going back and never record the skip after
  154. // their restart, so without this their automatic check offered the version just left again at once.
  155. Settings skipLeft (Settings settings, const Version& left);
  156. // --- installing.txt (round 190hl) -------------------------------------------------------------------------------
  157. // The note the Windows installer path leaves in the update folder for the version it installs (read after the
  158. // restart): the version installed, the one it replaced, whether it went back. LYNX SHARE 0.1.27 and older read
  159. // everything after the first "version=" as the version, so "version=" is written LAST (round 190gh wrote it first
  160. // and 0.1.27 said "updated to 0.0.0"); 0.1.28 and newer read the lines in any order.
  161. struct InstallingNote
  162. {
  163. std::string version, from;
  164. bool rollback = false;
  165. };
  166. std::string formatInstalling (const InstallingNote&);
  167. InstallingNote parseInstalling (const std::string& text);
  168. // An automatic check reuses the cached release list for this long (GitHub allows 60 unauthenticated calls an hour
  169. // per address; LYNX started many times a day asks once every few hours).
  170. constexpr std::int64_t cacheLifetimeMs = 3LL * 60 * 60 * 1000;
  171. bool cacheFresh (std::int64_t lastCheckMs, std::int64_t nowMs);
  172. // The GitHub API address of the release list (prereleases included; drafts are only listed for the repository's
  173. // own token, and dropped anyway).
  174. constexpr const char* releasesUrl = "https://api.github.com/repos/Ka1-S/LYNX-SHARE/releases?per_page=10";
  175. constexpr const char* releasesPage = "https://github.com/Ka1-S/LYNX-SHARE/releases";
  176. } // namespace lynx::update