Skip to content

Commit bcca973

Browse files
committed
Server: Add auto-updater
1 parent 1b80792 commit bcca973

17 files changed

+294
-59
lines changed

include/CommonLib/UpdaterAppComponent.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ namespace tsom
3030

3131
void CancelUpdate();
3232

33-
void DownloadAndUpdate(const UpdateInfo& updateInfo, bool downloadAssets, bool downloadBinaries);
33+
void DownloadAndUpdate(const UpdateInfo& updateInfo, bool downloadAssets, bool downloadBinaries, bool downloadUpdater, bool startUpdateWhenReady);
3434

35-
void FetchLastVersion(std::function<void(Nz::Result<UpdateInfo, std::string>&& updateInfo)>&& callback);
35+
void FetchLastVersion(bool server, std::function<void(Nz::Result<UpdateInfo, std::string>&& updateInfo)>&& callback);
3636

3737
UpdaterAppComponent& operator=(const UpdaterAppComponent&) = delete;
3838
UpdaterAppComponent& operator=(UpdaterAppComponent&&) = delete;
3939

4040
NazaraSignal(OnDownloadProgress, std::size_t /*activeDownloadCount*/, Nz::UInt64 /*totalDownloaded*/, Nz::UInt64 /*totalSize*/);
4141
NazaraSignal(OnUpdateFailed);
42+
NazaraSignal(OnUpdateReady);
4243
NazaraSignal(OnUpdateStarting);
4344

4445
private:
4546
void StartUpdaterAndQuit();
46-
void Update(Nz::Time elapsedTime) override;
4747
void UpdateProgression();
4848

4949
Nz::FixedVector<std::shared_ptr<const DownloadManager::Download>, 3> m_activeDownloads;

include/CommonLib/Version.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace tsom
1818
TSOM_COMMONLIB_API extern std::uint32_t GamePatchVersion;
1919
TSOM_COMMONLIB_API extern std::uint32_t GameVersion;
2020

21-
TSOM_COMMONLIB_API extern std::string_view BuildConfig;
21+
TSOM_COMMONLIB_API extern std::string_view BuildPlatform;
22+
TSOM_COMMONLIB_API extern std::string_view BuildArch;
2223
TSOM_COMMONLIB_API extern std::string_view BuildSystem;
2324
TSOM_COMMONLIB_API extern std::string_view BuildBranch;
2425
TSOM_COMMONLIB_API extern std::string_view BuildCommit;

include/ServerLib/ServerInstance.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace tsom
5050

5151
template<typename... Args> NetworkSessionManager& AddSessionManager(Args&&... args);
5252

53-
void BroadcastChatMessage(std::string message, std::optional<PlayerIndex> senderIndex);
53+
void BroadcastChatMessage(std::string message, std::optional<PlayerIndex> senderIndex = std::nullopt);
5454

5555
ServerPlayer* CreateAnonymousPlayer(NetworkSession* session, std::string nickname);
5656
ServerPlayer* CreateAuthenticatedPlayer(NetworkSession* session, const Nz::Uuid& uuid, std::string nickname, PlayerPermissionFlags permissions);

include/ServerLib/ServerInstanceAppComponent.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace tsom
2525

2626
template<typename... Args> ServerInstance& AddInstance(Args&&... args);
2727

28+
template<typename F> void ForEachInstance(F&& callback);
29+
2830
void Update(Nz::Time elapsedTime) override;
2931

3032
ServerInstanceAppComponent& operator=(const ServerInstanceAppComponent&) = delete;

include/ServerLib/ServerInstanceAppComponent.inl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ namespace tsom
99
{
1010
return *m_instances.emplace_back(std::make_unique<ServerInstance>(GetApp(), std::forward<Args>(args)...));
1111
}
12+
13+
template<typename F>
14+
void ServerInstanceAppComponent::ForEachInstance(F&& callback)
15+
{
16+
for (auto& instancePtr : m_instances)
17+
callback(*instancePtr);
18+
}
1219
}

src/CommonLib/UpdaterAppComponent.cpp

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,15 @@ namespace tsom
6363
m_downloadManager.Cancel();
6464
}
6565

66-
void UpdaterAppComponent::DownloadAndUpdate(const UpdateInfo& updateInfo, bool downloadAssets, bool downloadBinaries)
66+
void UpdaterAppComponent::DownloadAndUpdate(const UpdateInfo& updateInfo, bool downloadAssets, bool downloadBinaries, bool downloadUpdater, bool startUpdateWhenReady)
6767
{
6868
assert(downloadAssets || downloadBinaries);
69+
assert(!startUpdateWhenReady || downloadUpdater);
70+
71+
// just in case
72+
m_activeDownloads.clear();
73+
m_updaterDownload.reset();
6974

70-
m_activeDownloads.clear(); //< just in case
7175
auto QueueDownload = [&](std::string_view filename, const UpdateInfo::DownloadInfo& info, bool isExecutable = false)
7276
{
7377
auto download = m_downloadManager.QueueDownload(Nz::Utf8Path(filename), info.downloadUrl, info.size, info.sha256, false, isExecutable);
@@ -82,7 +86,8 @@ namespace tsom
8286
if (downloadBinaries)
8387
m_updateArchives.push_back(QueueDownload("autoupdate_binaries", updateInfo.binaries));
8488

85-
m_updaterDownload = QueueDownload("this_updater_of_mine", updateInfo.updater, true);
89+
if (downloadUpdater)
90+
m_updaterDownload = QueueDownload("this_updater_of_mine", updateInfo.updater, true);
8691

8792
for (auto& downloadPtr : m_activeDownloads)
8893
{
@@ -96,19 +101,27 @@ namespace tsom
96101
UpdateProgression();
97102
});
98103

99-
downloadPtr->OnDownloadFinished.Connect([this](const DownloadManager::Download&)
104+
downloadPtr->OnDownloadFinished.Connect([this, startUpdateWhenReady](const DownloadManager::Download&)
100105
{
101106
if (!m_downloadManager.HasDownloadInProgress())
102-
StartUpdaterAndQuit();
107+
{
108+
OnUpdateReady();
109+
if (startUpdateWhenReady)
110+
StartUpdaterAndQuit();
111+
}
103112
});
104113
}
105114

106115
// Can happen if all files were already downloaded
107116
if (!m_downloadManager.HasDownloadInProgress())
108-
StartUpdaterAndQuit();
117+
{
118+
OnUpdateReady();
119+
if (startUpdateWhenReady)
120+
StartUpdaterAndQuit();
121+
}
109122
}
110123

111-
void UpdaterAppComponent::FetchLastVersion(std::function<void(Nz::Result<UpdateInfo, std::string>&& updateInfo)>&& callback)
124+
void UpdaterAppComponent::FetchLastVersion(bool server, std::function<void(Nz::Result<UpdateInfo, std::string>&& updateInfo)>&& callback)
112125
{
113126
auto* webService = GetApp().TryGetComponent<Nz::WebServiceAppComponent>();
114127
if (!webService)
@@ -117,10 +130,10 @@ namespace tsom
117130
return;
118131
}
119132

120-
webService->QueueRequest([&](Nz::WebRequest& request)
133+
webService->QueueRequest([&, server](Nz::WebRequest& request)
121134
{
122135
request.SetMethod(Nz::WebRequestMethod::Get);
123-
request.SetURL(fmt::format("{}/game_version?platform={}", m_configFile.GetStringValue("Api.Url"), BuildConfig));
136+
request.SetURL(fmt::format("{}/game_version?platform={}{}_{}", m_configFile.GetStringValue("Api.Url"), BuildPlatform, server ? "-server" : "", BuildArch));
124137
request.SetServiceName("TSOM Version Check");
125138
request.SetResultCallback([cb = std::move(callback)](Nz::WebRequestResult&& result)
126139
{
@@ -158,35 +171,41 @@ namespace tsom
158171
{
159172
OnUpdateStarting();
160173

161-
Nz::Pid pid = Nz::Process::GetCurrentPid();
174+
if (m_updaterDownload)
175+
{
176+
Nz::Pid pid = Nz::Process::GetCurrentPid();
162177

163-
std::vector<std::string> args;
164-
args.push_back(std::to_string(pid)); // pid to wait before starting update
178+
std::vector<std::string> args;
179+
args.push_back(fmt::format("--pid={}", pid)); // pid to wait before starting update
165180

166-
// TODO: Add a way to retrieve executable name (or use argv[0]?)
167-
#ifdef NAZARA_PLATFORM_WINDOWS
168-
args.push_back("ThisSpaceOfMine.exe");
169-
#else
170-
args.push_back("./ThisSpaceOfMine");
171-
#endif
181+
for (auto& downloadPtr : m_updateArchives)
182+
{
183+
args.push_back("--archive");
184+
args.push_back(Nz::PathToString(downloadPtr->filepath));
185+
}
172186

173-
for (auto& downloadPtr : m_updateArchives)
174-
args.push_back(Nz::PathToString(downloadPtr->filepath));
187+
std::span<const char*> applicationArgs = GetApp().GetArgs();
175188

176-
Nz::Result updater = Nz::Process::SpawnDetached(m_updaterDownload->filepath, args);
177-
if (!updater)
178-
{
179-
fmt::print(fg(fmt::color::red), "failed to start autoupdater process: {0}\n", updater.GetError());
180-
return;
189+
args.push_back("--executable");
190+
args.push_back(applicationArgs[0]);
191+
192+
for (std::size_t i = 1; i < applicationArgs.size(); ++i)
193+
{
194+
args.push_back("--arg");
195+
args.push_back(applicationArgs[i]);
196+
}
197+
198+
Nz::Result updater = Nz::Process::SpawnDetached(m_updaterDownload->filepath, args);
199+
if (!updater)
200+
{
201+
fmt::print(fg(fmt::color::red), "failed to start autoupdater process: {0}\n", updater.GetError());
202+
return;
203+
}
181204
}
182205

183206
GetApp().Quit();
184207
}
185208

186-
void UpdaterAppComponent::Update(Nz::Time elapsedTime)
187-
{
188-
}
189-
190209
void UpdaterAppComponent::UpdateProgression()
191210
{
192211
std::size_t activeDownloadCount = 0;

src/Game/GameAppComponent.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ namespace tsom
153153
return false;
154154
}
155155

156-
updater->FetchLastVersion([updater](Nz::Result<UpdateInfo, std::string>&& result)
156+
updater->FetchLastVersion(false, [updater](Nz::Result<UpdateInfo, std::string>&& result)
157157
{
158158
if (!result)
159159
{
@@ -189,7 +189,7 @@ namespace tsom
189189
fmt::print("update is starting...\n");
190190
});
191191

192-
updater->DownloadAndUpdate(result.GetValue(), true, false);
192+
updater->DownloadAndUpdate(result.GetValue(), true, false, true, true);
193193
});
194194
}
195195
else

src/Game/States/CreatePlayerState.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,13 @@
33
// For conditions of distribution and use, see copyright notice in LICENSE
44

55
#include <Game/States/CreatePlayerState.hpp>
6-
#include <CommonLib/GameConstants.hpp>
7-
#include <CommonLib/InternalConstants.hpp>
8-
#include <CommonLib/UpdaterAppComponent.hpp>
9-
#include <CommonLib/Version.hpp>
106
#include <Game/GameConfigAppComponent.hpp>
117
#include <Game/States/ConnectionState.hpp>
128
#include <Game/States/GameState.hpp>
13-
#include <Game/States/UpdateState.hpp>
149
#include <Nazara/Widgets.hpp>
1510
#include <Nazara/Core/ApplicationBase.hpp>
1611
#include <Nazara/Core/StateMachine.hpp>
17-
#include <Nazara/Core/StringExt.hpp>
18-
#include <Nazara/Network/Algorithm.hpp>
1912
#include <Nazara/Network/IpAddress.hpp>
20-
#include <Nazara/Network/Network.hpp>
2113
#include <Nazara/Network/WebServiceAppComponent.hpp>
2214
#include <Nazara/TextRenderer/SimpleTextDrawer.hpp>
2315
#include <fmt/color.h>
@@ -96,7 +88,7 @@ namespace tsom
9688
webService.QueueRequest([&, nickname](Nz::WebRequest& request) mutable
9789
{
9890
request.SetMethod(Nz::WebRequestMethod::Post);
99-
request.SetURL(fmt::format("{}/v1/players", gameConfig.GetStringValue("Api.Url"), BuildConfig));
91+
request.SetURL(fmt::format("{}/v1/players", gameConfig.GetStringValue("Api.Url")));
10092
request.SetServiceName("TSOM Player Create");
10193

10294
nlohmann::json createParams;

src/Game/States/PlayState.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@
33
// For conditions of distribution and use, see copyright notice in LICENSE
44

55
#include <Game/States/PlayState.hpp>
6-
#include <CommonLib/GameConstants.hpp>
7-
#include <CommonLib/InternalConstants.hpp>
86
#include <CommonLib/UpdaterAppComponent.hpp>
9-
#include <CommonLib/Version.hpp>
107
#include <Game/GameConfigAppComponent.hpp>
118
#include <Game/States/ConnectionState.hpp>
129
#include <Game/States/CreatePlayerState.hpp>
1310
#include <Game/States/DirectConnectionState.hpp>
1411
#include <Game/States/GameState.hpp>
15-
#include <Game/States/UpdateState.hpp>
1612
#include <Nazara/Core/ApplicationBase.hpp>
1713
#include <Nazara/Core/StateMachine.hpp>
18-
#include <Nazara/Core/StringExt.hpp>
1914
#include <Nazara/Network/Algorithm.hpp>
2015
#include <Nazara/Network/IpAddress.hpp>
21-
#include <Nazara/Network/Network.hpp>
2216
#include <Nazara/Network/WebServiceAppComponent.hpp>
2317
#include <Nazara/TextRenderer/SimpleTextDrawer.hpp>
2418
#include <Nazara/Widgets/BoxLayout.hpp>
@@ -134,7 +128,7 @@ namespace tsom
134128
webService.QueueRequest([&](Nz::WebRequest& request)
135129
{
136130
request.SetMethod(Nz::WebRequestMethod::Post);
137-
request.SetURL(fmt::format("{}/v1/player/auth", gameConfig.GetStringValue("Api.Url"), BuildConfig));
131+
request.SetURL(fmt::format("{}/v1/player/auth", gameConfig.GetStringValue("Api.Url")));
138132
request.SetServiceName("TSOM Player Info");
139133

140134
nlohmann::json connectBody;
@@ -208,7 +202,7 @@ namespace tsom
208202
webService.QueueRequest([&](Nz::WebRequest& request)
209203
{
210204
request.SetMethod(Nz::WebRequestMethod::Post);
211-
request.SetURL(fmt::format("{}/v1/game/connect", gameConfig.GetStringValue("Api.Url"), BuildConfig));
205+
request.SetURL(fmt::format("{}/v1/game/connect", gameConfig.GetStringValue("Api.Url")));
212206
request.SetServiceName("TSOM Player Info");
213207

214208
nlohmann::json connectBody;

src/Game/States/UpdateState.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace tsom
6565
m_isCancelled = true;
6666
});
6767

68-
updater.DownloadAndUpdate(m_updateInfo, m_updateInfo.assetVersion > currentGameVersion, m_updateInfo.binaryVersion > currentGameVersion);
68+
updater.DownloadAndUpdate(m_updateInfo, m_updateInfo.assetVersion > currentGameVersion, m_updateInfo.binaryVersion > currentGameVersion, true, true);
6969
}
7070

7171
bool UpdateState::Update(Nz::StateMachine& fsm, Nz::Time elapsedTime)

0 commit comments

Comments
 (0)