-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathUpdaterBackend.cpp
More file actions
259 lines (205 loc) · 10.2 KB
/
Copy pathUpdaterBackend.cpp
File metadata and controls
259 lines (205 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// __________ ___ ______ _
// / ____/ __ \____ / (_)___ ___ / ____/___ ____ _(_)___ ___
// / /_ / / / / __ \/ / / __ \/ _ \ / __/ / __ \/ __ `/ / __ \/ _ `
// / __/ / /_/ / / / / / / / / / __/ / /___/ / / / /_/ / / / / / __/
// /_/ \____/_/ /_/_/_/_/ /_/\___/ /_____/_/ /_/\__, /_/_/ /_/\___/
// /____/
// FOnline Engine
// https://fonline.ru
// https://github.com/cvet/fonline
//
// MIT License
//
// Copyright (c) 2006 - 2026, Anton Tsvetinskiy aka cvet <cvet@tut.by>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include "UpdaterBackend.h"
#include "DataSerialization.h"
#include "DiskFileSystem.h"
#include "Logging.h"
#include "SafeArithmetics.h"
#include "ServerConnection.h"
#include "StringUtils.h"
FO_BEGIN_NAMESPACE
static auto ReadUpdateFilePortion(string_view disk_path, uint64_t start_offset, vector<uint8_t>& data) -> bool
{
FO_STACK_TRACE_ENTRY();
auto file = fs_open_ifstream(disk_path);
if (!file) {
return false;
}
file.seekg(numeric_cast<std::streamoff>(start_offset), std::ios::beg);
if (!file) {
return false;
}
return data.empty() || stream_read_exact(file, data.data(), data.size());
}
void UpdaterBackend::LoadFromClientResources(const GlobalSettings& settings)
{
FO_STACK_TRACE_ENTRY();
_updateFiles.clear();
_commonUpdateFiles.clear();
_commonUpdateFilesDesc.clear();
_binaryTargetUpdateFiles.clear();
_binaryTargetUpdateFilesDesc.clear();
WriteLog("Load client data packs for synchronization");
const auto add_sync_file = [&settings, this](string_view disk_path, string_view client_path, UpdateFileTarget target) -> UpdateFileInfo {
UpdateFileData data {};
auto file = fs_open_ifstream(disk_path);
if (!file) {
throw UpdaterException("Resource pack for client not found", disk_path);
}
const size_t file_size = stream_get_size(file);
if (settings.UpdateFilesInMemory) {
data.InMemory = true;
data.MemoryData.resize(file_size);
if (file_size != 0 && !stream_read_exact(file, data.MemoryData.data(), file_size)) {
throw UpdaterException("Can't read resource pack for client", disk_path);
}
data.Size = numeric_cast<uint64_t>(file_size);
data.Hash = fs_hash_data(data.MemoryData.data(), data.MemoryData.size());
}
else {
data.DiskPath = string(disk_path);
data.Size = numeric_cast<uint64_t>(file_size);
const auto file_hash = fs_hash_file(disk_path);
if (!file_hash.has_value()) {
throw UpdaterException("Can't hash resource pack for client", disk_path);
}
data.Hash = *file_hash;
}
_updateFiles.emplace_back(std::move(data));
UpdateFileInfo info {};
info.FileIndex = numeric_cast<uint32_t>(_updateFiles.size() - 1);
info.ClientPath = string(client_path);
info.Target = target;
return info;
};
const auto client_resources_dir = std::filesystem::path {fs_make_path(settings.ClientResources)};
for (const auto& resource_entry : settings.ClientResourceEntries) {
if (resource_entry != "Embedded") {
const auto pack_name = strex("{}.zip", resource_entry).str();
const auto pack_disk_path = fs_path_to_string(client_resources_dir / pack_name);
auto info = add_sync_file(pack_disk_path, pack_name, UpdateFileTarget::ClientResources);
_commonUpdateFiles.emplace_back(std::move(info));
}
}
const auto platform_binaries_dir = std::filesystem::path {fs_make_path(settings.PlatformBinaries)};
const auto platform_binaries_path = fs_path_to_string(platform_binaries_dir);
if (std::filesystem::exists(platform_binaries_dir)) {
FO_VERIFY_AND_THROW(std::filesystem::is_directory(platform_binaries_dir), "Platform binaries path exists but is not a directory", platform_binaries_path);
for (const auto& platform_entry : std::filesystem::directory_iterator {platform_binaries_dir}) {
if (!platform_entry.is_directory()) {
continue;
}
const auto binary_target_name = fs_path_to_string(platform_entry.path().filename());
FO_VERIFY_AND_THROW(!binary_target_name.empty(), "Updater backend found a platform binaries directory entry with an empty target name", platform_binaries_path, fs_path_to_string(platform_entry.path()));
for (const auto& binary_entry : std::filesystem::recursive_directory_iterator {platform_entry.path()}) {
FO_VERIFY_AND_THROW(binary_entry.is_regular_file(), "Updater backend binary target contains a non-file entry", binary_target_name, fs_path_to_string(binary_entry.path()));
const auto disk_path = fs_path_to_string(binary_entry.path());
const auto client_file_name = fs_path_to_string(binary_entry.path().filename());
auto info = add_sync_file(disk_path, client_file_name, UpdateFileTarget::ClientBinaries);
_binaryTargetUpdateFiles[string(binary_target_name)].emplace_back(std::move(info));
}
}
}
const auto build_update_desc = [this](vector<uint8_t>& desc, const vector<UpdateFileInfo>* platform_files) {
auto writer = DataWriter(desc);
const auto write_file_info = [this, &writer](const UpdateFileInfo& info) {
const auto& data = _updateFiles[info.FileIndex];
writer.Write<int16_t>(numeric_cast<int16_t>(info.ClientPath.length()));
writer.WritePtr(info.ClientPath.data(), info.ClientPath.length());
writer.Write<uint64_t>(data.Size);
writer.Write<uint64_t>(data.Hash);
writer.Write<UpdateFileTarget>(info.Target);
writer.Write<uint32_t>(info.FileIndex);
};
for (const auto& info : _commonUpdateFiles) {
write_file_info(info);
}
if (platform_files != nullptr) {
for (const auto& info : *platform_files) {
write_file_info(info);
}
}
writer.Write<int16_t>(const_numeric_cast<int16_t>(-1));
};
build_update_desc(_commonUpdateFilesDesc, nullptr);
for (auto& [binary_target_name, files] : _binaryTargetUpdateFiles) {
auto& desc = _binaryTargetUpdateFilesDesc[binary_target_name];
build_update_desc(desc, &files);
}
}
auto UpdaterBackend::GetUpdateDescriptor(string_view binary_target_name) const -> const vector<uint8_t>&
{
FO_STACK_TRACE_ENTRY();
const auto desc_it = _binaryTargetUpdateFilesDesc.find(string(binary_target_name));
return desc_it != _binaryTargetUpdateFilesDesc.end() ? desc_it->second : _commonUpdateFilesDesc;
}
void UpdaterBackend::ProcessUpdateFile(ServerConnection* connection, int32_t update_file_max_portion_size)
{
FO_STACK_TRACE_ENTRY();
auto in_buf = connection->ReadBuf();
const auto file_index = in_buf->Read<uint32_t>();
const auto start_offset = in_buf->Read<uint64_t>();
in_buf.Unlock();
if (file_index >= _updateFiles.size()) {
WriteLog(LogType::Warning, "Wrong file index {}, from host '{}'", file_index, connection->GetHost());
connection->HardDisconnect();
return;
}
if (update_file_max_portion_size <= 0) {
WriteLog(LogType::Warning, "Wrong update file max portion size {}, client host '{}'", update_file_max_portion_size, connection->GetHost());
connection->HardDisconnect();
return;
}
const auto& update_file = _updateFiles[file_index];
const auto file_size = update_file.Size;
if (start_offset > file_size) {
WriteLog(LogType::Warning, "Wrong update file offset {}, file index {}, client host '{}'", start_offset, file_index, connection->GetHost());
connection->HardDisconnect();
return;
}
const uint64_t update_portion_limit = numeric_cast<uint64_t>(update_file_max_portion_size);
const uint64_t remaining_size = file_size - start_offset;
const uint64_t update_portion = std::min(update_portion_limit, remaining_size);
const size_t update_portion_size = numeric_cast<size_t>(update_portion);
vector<uint8_t> disk_update_data {};
if (update_portion_size != 0 && !update_file.InMemory) {
disk_update_data.resize(update_portion_size);
if (!ReadUpdateFilePortion(update_file.DiskPath, start_offset, disk_update_data)) {
WriteLog(LogType::Warning, "Can't read update file '{}', file index {}, client host '{}'", update_file.DiskPath, file_index, connection->GetHost());
connection->HardDisconnect();
return;
}
}
auto out_buf = connection->WriteMsg(NetMessage::UpdateFileData);
out_buf->Write(numeric_cast<int32_t>(update_portion_size));
if (update_portion_size != 0) {
if (update_file.InMemory) {
const size_t offset = numeric_cast<size_t>(start_offset);
out_buf->Push(update_file.MemoryData.data() + offset, update_portion_size);
}
else {
out_buf->Push(disk_update_data.data(), update_portion_size);
}
}
}
FO_END_NAMESPACE