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