forked from wesnoth/wesnoth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
522 lines (412 loc) · 15.4 KB
/
client.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
2008 - 2015 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "addon/info.hpp"
#include "addon/manager.hpp"
#include "addon/validation.hpp"
#include "cursor.hpp"
#include "formula/string_utils.hpp"
#include "gettext.hpp"
#include "gui/dialogs/addon/install_dependencies.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/widgets/window.hpp"
#include "log.hpp"
#include "serialization/parser.hpp"
#include "serialization/string_utils.hpp"
#include "addon/client.hpp"
static lg::log_domain log_addons_client("addons-client");
#define ERR_ADDONS LOG_STREAM(err , log_addons_client)
#define WRN_ADDONS LOG_STREAM(warn, log_addons_client)
#define LOG_ADDONS LOG_STREAM(info, log_addons_client)
#define DBG_ADDONS LOG_STREAM(debug, log_addons_client)
using gui2::dialogs::network_transmission;
addons_client::addons_client(CVideo& v, const std::string& address)
: v_(v)
, addr_(address)
, host_()
, port_()
, conn_(nullptr)
, stat_(nullptr)
, last_error_()
, last_error_data_()
{
const std::vector<std::string>& address_components =
utils::split(addr_, ':');
if(address_components.empty()) {
throw invalid_server_address();
}
// FIXME: this parsing will break IPv6 numeric addresses! */
host_ = address_components[0];
port_ = address_components.size() == 2 ?
address_components[1] : std::to_string(default_campaignd_port);
}
void addons_client::connect()
{
LOG_ADDONS << "connecting to server " << host_ << " on port " << port_ << '\n';
utils::string_map i18n_symbols;
i18n_symbols["server_address"] = addr_;
conn_.reset(new network_asio::connection(host_, port_));
this->wait_for_transfer_done(
vgettext("Connecting to $server_address|...", i18n_symbols));
}
bool addons_client::request_addons_list(config& cfg)
{
cfg.clear();
config response_buf;
/** @todo FIXME: get rid of this legacy "campaign"/"campaigns" silliness
*/
this->send_simple_request("request_campaign_list", response_buf);
this->wait_for_transfer_done(_("Downloading list of add-ons..."));
cfg = response_buf.child("campaigns");
return !this->update_last_error(response_buf);
}
bool addons_client::request_distribution_terms(std::string& terms)
{
terms.clear();
config response_buf;
this->send_simple_request("request_terms", response_buf);
this->wait_for_transfer_done(_("Requesting distribution terms..."));
if(const config& msg_cfg = response_buf.child("message")) {
terms = msg_cfg["message"].str();
}
return !this->update_last_error(response_buf);
}
bool addons_client::upload_addon(const std::string& id, std::string& response_message, config& cfg)
{
LOG_ADDONS << "preparing to upload " << id << '\n';
response_message.clear();
utils::string_map i18n_symbols;
i18n_symbols["addon_title"] = cfg["title"];
if(i18n_symbols["addon_title"].empty()) {
i18n_symbols["addon_title"] = make_addon_title(id);
}
std::string passphrase = cfg["passphrase"];
// generate a random passphrase and write it to disk
// if the .pbl file doesn't provide one already
if(passphrase.empty()) {
passphrase.resize(8);
for(size_t n = 0; n != 8; ++n) {
passphrase[n] = 'a' + (rand()%26);
}
cfg["passphrase"] = passphrase;
set_addon_pbl_info(id, cfg);
LOG_ADDONS << "automatically generated an initial passphrase for " << id << '\n';
}
cfg["name"] = id;
config addon_data;
try {
archive_addon(id, addon_data);
} catch(utf8::invalid_utf8_exception&){
this->last_error_ =
vgettext("The add-on <i>$addon_title</i> has a file or directory "
"containing invalid characters and cannot be published.", i18n_symbols);
return false;
}
config request_buf, response_buf;
request_buf.add_child("upload", cfg).add_child("data", addon_data);
LOG_ADDONS << "sending " << id << '\n';
this->send_request(request_buf, response_buf);
this->wait_for_transfer_done(vgettext("Sending add-on <i>$addon_title</i>...", i18n_symbols
), true);
if(const config& message_cfg = response_buf.child("message")) {
response_message = message_cfg["message"].str();
LOG_ADDONS << "server response: " << response_message << '\n';
}
return !this->update_last_error(response_buf);
}
bool addons_client::delete_remote_addon(const std::string& id, std::string& response_message)
{
response_message.clear();
config cfg = get_addon_pbl_info(id);
utils::string_map i18n_symbols;
i18n_symbols["addon_title"] = cfg["title"];
if(i18n_symbols["addon_title"].empty()) {
i18n_symbols["addon_title"] = make_addon_title(id);
}
config request_buf, response_buf;
config& request_body = request_buf.add_child("delete");
request_body["name"] = id;
request_body["passphrase"] = cfg["passphrase"];
LOG_ADDONS << "requesting server to delete " << id << '\n';
this->send_request(request_buf, response_buf);
this->wait_for_transfer_done(vgettext("Removing add-on <i>$addon_title</i> from the server...", i18n_symbols
));
if(const config& message_cfg = response_buf.child("message")) {
response_message = message_cfg["message"].str();
LOG_ADDONS << "server response: " << response_message << '\n';
}
return !this->update_last_error(response_buf);
}
bool addons_client::download_addon(config& archive_cfg, const std::string& id, const std::string& title, bool increase_downloads)
{
archive_cfg.clear();
config request_buf;
config& request_body = request_buf.add_child("request_campaign");
request_body["name"] = id;
request_body["increase_downloads"] = increase_downloads;
utils::string_map i18n_symbols;
i18n_symbols["addon_title"] = title;
LOG_ADDONS << "downloading " << id << '\n';
this->send_request(request_buf, archive_cfg);
this->wait_for_transfer_done(vgettext("Downloading add-on <i>$addon_title</i>...", i18n_symbols));
return !this->update_last_error(archive_cfg);
}
bool addons_client::install_addon(config& archive_cfg, const addon_info& info)
{
const cursor::setter cursor_setter(cursor::WAIT);
utils::string_map i18n_symbols;
i18n_symbols["addon_title"] = info.title;
if(!check_names_legal(archive_cfg)) {
gui2::show_error_message(v_,
vgettext("The add-on <i>$addon_title</i> has an invalid file or directory "
"name and cannot be installed.", i18n_symbols));
return false;
}
// Add local version information before unpacking
config* maindir = &archive_cfg.find_child("dir", "name", info.id);
if(!*maindir) {
LOG_ADDONS << "downloaded add-on '" << info.id << "' is missing its directory in the archive; creating it\n";
maindir = &archive_cfg.add_child("dir");
(*maindir)["name"] = info.id;
}
LOG_ADDONS << "generating version info for add-on '" << info.id << "'\n";
std::ostringstream info_contents;
config wml;
info_contents <<
"#\n"
"# File automatically generated by Wesnoth to keep track\n"
"# of version information on installed add-ons. DO NOT EDIT!\n"
"#\n";
info.write_minimal(wml.add_child("info"));
write(info_contents, wml);
config file;
file["name"] = "_info.cfg";
file["contents"] = info_contents.str();
maindir->add_child("file", file);
LOG_ADDONS << "unpacking " << info.id << '\n';
// Remove any previously installed versions
if(!remove_local_addon(info.id)) {
WRN_ADDONS << "failed to uninstall previous version of " << info.id << "; the add-on may not work properly!" << std::endl;
}
unarchive_addon(archive_cfg);
LOG_ADDONS << "unpacking finished\n";
return true;
}
bool addons_client::try_fetch_addon(const addon_info & addon)
{
config archive;
if(!(
download_addon(archive, addon.id, addon.title, !is_addon_installed(addon.id)) &&
install_addon(archive, addon)
)) {
const std::string& server_error = get_last_server_error();
if(!server_error.empty()) {
gui2::show_error_message(v_,
_("The server responded with an error:") + "\n" + server_error);
}
return false;
} else {
return true;
}
}
addons_client::install_result addons_client::do_resolve_addon_dependencies(const addons_list& addons, const addon_info& addon)
{
install_result result;
result.outcome = install_outcome::success;
result.wml_changed = false;
std::unique_ptr<cursor::setter> cursor_setter(new cursor::setter(cursor::WAIT));
// TODO: We don't currently check for the need to upgrade. I'll probably
// work on that when implementing dependency tiers later.
const std::set<std::string>& deps = addon.resolve_dependencies(addons);
std::vector<std::string> missing_deps;
std::vector<std::string> broken_deps;
for(const std::string& dep : deps) {
if(!is_addon_installed(dep)) {
if(addons.find(dep) != addons.end()) {
missing_deps.push_back(dep);
} else {
broken_deps.push_back(dep);
}
}
}
cursor_setter.reset();
if(!broken_deps.empty()) {
std::string broken_deps_report;
broken_deps_report = _n(
"The selected add-on has the following dependency, which is not currently installed or available from the server. Do you wish to continue?",
"The selected add-on has the following dependencies, which are not currently installed or available from the server. Do you wish to continue?",
broken_deps.size());
broken_deps_report += "\n";
for(const std::string& broken_dep_id : broken_deps) {
broken_deps_report += "\n " + font::unicode_bullet + " " + make_addon_title(broken_dep_id);
}
if(gui2::show_message(v_, _("Broken Dependencies"), broken_deps_report, gui2::dialogs::message::yes_no_buttons) != gui2::window::OK) {
result.outcome = install_outcome::abort;
return result; // canceled by user
}
}
if(missing_deps.empty()) {
// No dependencies to install, carry on.
return result;
}
{
addons_list options;
for(const std::string& dep : missing_deps) {
const addon_info& missing_addon = addons.at(dep);
options[dep] = missing_addon;
}
gui2::dialogs::install_dependencies dlg(options);
bool cont = dlg.show(v_);
if(!cont) {
return result; // the user has chosen to continue without installing anything.
}
}
//
// Install dependencies now.
//
std::vector<std::string> failed_titles;
for(const std::string& dep : missing_deps) {
const addon_info& missing_addon = addons.at(dep);
if(!try_fetch_addon(missing_addon)) {
failed_titles.push_back(missing_addon.title);
} else {
result.wml_changed = true;
}
}
if(!failed_titles.empty()) {
const std::string& failed_deps_report = _n(
"The following dependency could not be installed. Do you still wish to continue?",
"The following dependencies could not be installed. Do you still wish to continue?",
failed_titles.size()) + std::string("\n\n") + utils::bullet_list(failed_titles);
result.outcome = gui2::show_message(v_, _("Dependencies Installation Failed"), failed_deps_report, gui2::dialogs::message::yes_no_buttons) == gui2::window::OK ? install_outcome::success : install_outcome::abort; // If the user cancels, return abort. Otherwise, return success, since the user chose to ignore the failure.
return result;
}
return result;
}
bool addons_client::do_check_before_overwriting_addon(const addon_info& addon)
{
const std::string& addon_id = addon.id;
const bool pbl = have_addon_pbl_info(addon_id);
const bool vcs = have_addon_in_vcs_tree(addon_id);
if(!pbl && !vcs) {
return true;
}
utils::string_map symbols;
symbols["addon"] = addon.title;
std::string text;
std::vector<std::string> extra_items;
text = vgettext("The add-on '$addon|' is already installed and contains additional information that will be permanently lost if you continue:", symbols);
text += "\n\n";
if(pbl) {
extra_items.push_back(_("Publishing information file (.pbl)"));
}
if(vcs) {
extra_items.push_back(_("Version control system (VCS) information"));
}
text += utils::bullet_list(extra_items) + "\n\n";
text += _("Do you really wish to continue?");
return gui2::show_message(v_, _("Confirm"), text, gui2::dialogs::message::yes_no_buttons) == gui2::window::OK;
}
addons_client::install_result addons_client::install_addon_with_checks(const addons_list& addons, const addon_info& addon)
{
if(!(do_check_before_overwriting_addon(addon))) {
// Just do nothing and leave.
install_result result;
result.outcome = install_outcome::abort;
result.wml_changed = false;
return result;
}
// Resolve any dependencies
install_result res = do_resolve_addon_dependencies(addons, addon);
if(res.outcome != install_outcome::success) { // this function only returns SUCCESS and ABORT as outcomes
return res; // user aborted
}
if(!try_fetch_addon(addon)) {
res.outcome = install_outcome::failure;
return res; //wml_changed should have whatever value was obtained in resolving dependencies
} else {
res.wml_changed = true;
return res; //we successfully installed something, so now the wml was definitely changed
}
}
bool addons_client::update_last_error(config& response_cfg)
{
if(config const &error = response_cfg.child("error")) {
this->last_error_ = error["message"].str();
this->last_error_data_ = error["extra_data"].str();
ERR_ADDONS << "server error: " << error << '\n';
return true;
} else {
this->last_error_.clear();
this->last_error_data_.clear();
return false;
}
}
void addons_client::check_connected() const
{
assert(conn_ != nullptr);
if(conn_ == nullptr) {
ERR_ADDONS << "not connected to server" << std::endl;
throw not_connected_to_server();
}
}
void addons_client::send_request(const config& request, config& response)
{
check_connected();
response.clear();
this->conn_->transfer(request, response);
}
void addons_client::send_simple_request(const std::string& request_string, config& response)
{
config request;
request.add_child(request_string);
this->send_request(request, response);
}
struct read_addon_connection_data : public network_transmission::connection_data
{
read_addon_connection_data(network_asio::connection& conn) : conn_(conn) {}
size_t total() override { return conn_.bytes_to_read(); }
virtual size_t current() override { return conn_.bytes_read(); }
virtual bool finished() override { return conn_.done(); }
virtual void cancel() override { return conn_.cancel(); }
virtual void poll() override { conn_.poll(); }
network_asio::connection& conn_;
};
struct write_addon_connection_data : public network_transmission::connection_data
{
write_addon_connection_data(network_asio::connection& conn) : conn_(conn) {}
size_t total() override { return conn_.bytes_to_write(); }
virtual size_t current() override { return conn_.bytes_written(); }
virtual bool finished() override { return conn_.done(); }
virtual void cancel() override { return conn_.cancel(); }
virtual void poll() override { conn_.poll(); }
network_asio::connection& conn_;
};
void addons_client::wait_for_transfer_done(const std::string& status_message, bool track_upload)
{
check_connected();
std::unique_ptr<network_transmission::connection_data> cd;
if(track_upload)
cd.reset(new write_addon_connection_data{ *conn_ });
else
cd.reset(new read_addon_connection_data{ *conn_ });
if(!stat_) {
stat_.reset(new network_transmission(*cd, _("Add-ons Manager"), status_message));
} else {
stat_->set_subtitle(status_message);
stat_->set_connection_data(*cd);
}
if(!stat_->show(v_)) {
// Notify the caller chain that the user aborted the operation.
throw user_exit();
}
}