ArcdpsExtension
 
Loading...
Searching...
No Matches
UpdateCheckerBase.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <expected>
5#include <filesystem>
6#include <mutex>
7#include <optional>
8#include <string>
9#include <thread>
10#include <vector>
11#include <Windows.h>
12
13namespace ArcdpsExtension {
14 // Expected usage is as follows:
15 // - Optionally, create a new class that inherits from UpdateCheckerBase to modify the functionality, in case your addon
16 // uses a different version format than the default one (3 numbers separated by dots, all letters in tag names ignored),
17 // or to provide a sink for the log lines
18 // - Instantiate your derivative class (or UpdateCheckerBase if there is none) either during static initialization or in
19 // mod_init.
20 // - In mod_init, call the following, in order
21 // - GetCurrentVersion() to get the current version of your addon in UpdateChecker format, store this globally
22 // - ClearFiles() to remove the artifacts from old updates and ensure that a new update would succeed
23 // - CheckForUpdate() to start the check for an update. The update is fetched asynchronously. Store the struct returned
24 // by the function globally
25 // - In mod_release, call FinishPendingTasks() on the update struct and then destruct it
26 // - In mod_imgui, acquire a lock on the update struct and look at UpdateStatus. Show a window displaying there is an
27 // update available if it's not Unknown or Dismissed. If the window is closed, set UpdateStatus to Dismissed. If the
28 // user chooses to perform an update, call PerformInstallOrUpdate() with the update struct still locked.
30 public:
31 using Version = std::array<WORD, 4>;
32
33 // / Dismissed
34 // Unknown -> UpdateAvailable / UpdateSuccessful
35 // \ UpdateInProgress
36 // \ UpdateError
45
46 struct UpdateState {
47 UpdateState(const std::optional<Version>& pVersion, std::string&& pInstallPath);
49
50 void FinishPendingTasks();
51
52 // Changes the status to pNewStatus iff the current value is pExpectedStatus
53 bool ChangeStatus(Status pExpectedStatus, Status pNewStatus);
54
55 const std::optional<Version> CurrentVersion = {};
56 const std::string InstallPath;
57
58 std::mutex Lock; // Protects all non-const members
60 std::vector<std::thread> Tasks;
61
62 // The fields below are only defined if UpdateStatus != Status::Unknown, at which point they are constant
64 std::string DownloadUrl;
65 };
66
67 static std::expected<Version, std::string> GetCurrentVersion(HMODULE dll) noexcept;
68 void ClearFiles(HMODULE pDll) noexcept;
69 std::unique_ptr<UpdateState> CheckForUpdate(HMODULE pDll, const Version& pCurrentVersion, std::string&& pRepo, bool pAllowPreRelease) noexcept;
70 std::unique_ptr<UpdateState> GetInstallState(std::string&& pInstallPath, std::string&& pRepo, bool pAllowPreRelease) noexcept;
71 void PerformInstallOrUpdate(UpdateState& pState) noexcept; // Requires lock to be held on pState already
72
73 virtual std::optional<std::string> GetPathFromHModule(HMODULE pDll) noexcept;
74 std::unique_ptr<UpdateState> GetUpdateInternal(std::string&& pInstallPath, const std::optional<Version>& pCurrentVersion, std::string&& pRepo, bool pAllowPreRelease) noexcept;
75 static std::string GetVersionAsString(const Version& pVersion);
76 virtual bool IsNewer(const Version& pRepoVersion, const Version& pCurrentVersion);
77 virtual void Log(std::string&& pMessage);
78 virtual Version ParseVersion(std::string_view versionString);
79 bool PerformDownload(const std::string& pUrl, const std::string& pDestinationPath);
80
81 // Can throw http errors
82 std::optional<std::tuple<Version, std::string>> GetLatestRelease(std::string&& pRepo, bool pAllowPreRelease);
83 virtual bool HttpDownload(const std::string& pUrl, const std::filesystem::path& pOutputFile) = 0;
84 virtual std::optional<std::string> HttpGet(const std::string& pUrl) = 0;
85 };
86} // namespace ArcdpsExtension
Definition UpdateCheckerBase.h:29
virtual Version ParseVersion(std::string_view versionString)
Definition UpdateCheckerBase.cpp:235
static std::string GetVersionAsString(const Version &pVersion)
Definition UpdateCheckerBase.cpp:222
std::unique_ptr< UpdateState > GetInstallState(std::string &&pInstallPath, std::string &&pRepo, bool pAllowPreRelease) noexcept
Definition UpdateCheckerBase.cpp:108
void PerformInstallOrUpdate(UpdateState &pState) noexcept
Definition UpdateCheckerBase.cpp:115
Status
Definition UpdateCheckerBase.h:37
virtual void Log(std::string &&pMessage)
Definition UpdateCheckerBase.cpp:231
static std::expected< Version, std::string > GetCurrentVersion(HMODULE dll) noexcept
Definition UpdateCheckerBase.cpp:44
bool PerformDownload(const std::string &pUrl, const std::string &pDestinationPath)
Definition UpdateCheckerBase.cpp:284
virtual std::optional< std::string > GetPathFromHModule(HMODULE pDll) noexcept
Definition UpdateCheckerBase.cpp:168
void ClearFiles(HMODULE pDll) noexcept
Definition UpdateCheckerBase.cpp:73
std::optional< std::tuple< Version, std::string > > GetLatestRelease(std::string &&pRepo, bool pAllowPreRelease)
Definition UpdateCheckerBase.cpp:292
std::unique_ptr< UpdateState > GetUpdateInternal(std::string &&pInstallPath, const std::optional< Version > &pCurrentVersion, std::string &&pRepo, bool pAllowPreRelease) noexcept
Definition UpdateCheckerBase.cpp:178
std::array< WORD, 4 > Version
Definition UpdateCheckerBase.h:31
virtual std::optional< std::string > HttpGet(const std::string &pUrl)=0
virtual bool IsNewer(const Version &pRepoVersion, const Version &pCurrentVersion)
Definition UpdateCheckerBase.cpp:226
virtual bool HttpDownload(const std::string &pUrl, const std::filesystem::path &pOutputFile)=0
std::unique_ptr< UpdateState > CheckForUpdate(HMODULE pDll, const Version &pCurrentVersion, std::string &&pRepo, bool pAllowPreRelease) noexcept
Definition UpdateCheckerBase.cpp:96
Definition ArcdpsExtension.h:10
Definition UpdateCheckerBase.h:46
const std::optional< Version > CurrentVersion
Definition UpdateCheckerBase.h:55
bool ChangeStatus(Status pExpectedStatus, Status pNewStatus)
Definition UpdateCheckerBase.cpp:34
~UpdateState()
Definition UpdateCheckerBase.cpp:15
std::mutex Lock
Definition UpdateCheckerBase.h:58
std::string DownloadUrl
Definition UpdateCheckerBase.h:64
Version NewVersion
Definition UpdateCheckerBase.h:63
Status UpdateStatus
Definition UpdateCheckerBase.h:59
void FinishPendingTasks()
Definition UpdateCheckerBase.cpp:22
std::vector< std::thread > Tasks
Definition UpdateCheckerBase.h:60
const std::string InstallPath
Definition UpdateCheckerBase.h:56