/
manager.cpp
1571 lines (1336 loc) · 52.3 KB
/
manager.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id$ */
/*
Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
2008 - 2012 by Ignacio R. 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 "global.hpp"
#include "addon/manager.hpp"
#include "dialogs.hpp"
#include "filesystem.hpp"
#include "formatter.hpp"
#include "game_display.hpp"
#include "game_preferences.hpp"
#include "gettext.hpp"
#include "gui/dialogs/addon_connect.hpp"
#include "gui/dialogs/addon_list.hpp"
#include "gui/dialogs/addon/description.hpp"
#include "gui/dialogs/addon/uninstall_list.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/dialogs/network_transmission.hpp"
#include "gui/dialogs/simple_item_selector.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "log.hpp"
#include "marked-up_text.hpp"
#include "serialization/parser.hpp"
#include "version.hpp"
#include "wml_separators.hpp"
#include "formula_string_utils.hpp"
#include <boost/foreach.hpp>
static lg::log_domain log_config("config");
#define ERR_CFG LOG_STREAM(err , log_config)
#define LOG_CFG LOG_STREAM(info, log_config)
#define WRN_CFG LOG_STREAM(warn, log_config)
static lg::log_domain log_filesystem("filesystem");
#define ERR_FS LOG_STREAM(err , log_filesystem)
static lg::log_domain log_network("network");
#define ERR_NET LOG_STREAM(err , log_network)
#define LOG_NET LOG_STREAM(info, log_network)
bool have_addon_in_vcs_tree(const std::string& addon_name)
{
static const std::string parentd = get_addon_campaigns_dir();
return
file_exists(parentd+"/"+addon_name+"/.svn") ||
file_exists(parentd+"/"+addon_name+"/.git") ||
file_exists(parentd+"/"+addon_name+"/.hg");
}
bool have_addon_pbl_info(const std::string& addon_name)
{
static const std::string parentd = get_addon_campaigns_dir();
return
file_exists(parentd+"/"+addon_name+".pbl") ||
file_exists(parentd+"/"+addon_name+"/_server.pbl");
}
bool get_addon_info(const std::string& addon_name, config& cfg)
{
const std::string parentd = get_addon_campaigns_dir();
// Cope with old-style or new-style file organization
const std::string exterior = parentd + "/" + addon_name + ".pbl";
const std::string interior = parentd + "/" + addon_name + "/_server.pbl";
const bool is_old_style = file_exists(exterior);
const std::string pbl_file = (is_old_style ? exterior : interior);
scoped_istream stream = istream_file(pbl_file);
read(cfg, *stream);
return is_old_style;
}
void set_addon_info(const std::string& addon_name, const config& cfg, const bool is_old_style)
{
const std::string parentd = get_addon_campaigns_dir();
scoped_ostream stream = ostream_file(parentd + "/" + addon_name + "/_server.pbl");
if(is_old_style) stream = ostream_file(parentd + "/" + addon_name + ".pbl");
write(*stream, cfg);
}
bool remove_local_addon(const std::string& addon)
{
bool ret = true;
const std::string addon_dir = get_addon_campaigns_dir() + "/" + addon;
LOG_CFG << "removing local add-on: " << addon << '\n';
if(file_exists(addon_dir) && !delete_directory(addon_dir, true)) {
ERR_CFG << "Failed to delete directory/file: " << addon_dir << '\n';
ret = false;
}
if(file_exists(addon_dir + ".cfg") && !delete_directory(addon_dir + ".cfg", true)) {
ERR_CFG << "Failed to delete directory/file: " << addon_dir << ".cfg\n";
ret = false;
}
if(!ret) {
ERR_CFG << "removal of add-on " << addon << " failed!\n";
}
return ret;
}
std::vector<std::string> available_addons()
{
std::vector<std::string> res;
std::vector<std::string> files, dirs;
const std::string parentd = get_addon_campaigns_dir();
get_files_in_dir(parentd,&files,&dirs);
for(std::vector<std::string>::const_iterator i = dirs.begin(); i != dirs.end(); ++i) {
const std::string external_cfg_file = *i + ".cfg";
const std::string internal_cfg_file = *i + "/_main.cfg";
const std::string external_pbl_file = *i + ".pbl";
const std::string internal_pbl_file = *i + "/_server.pbl";
if((std::find(files.begin(),files.end(),external_cfg_file) != files.end() || file_exists(parentd + "/" + internal_cfg_file)) &&
(std::find(files.begin(),files.end(),external_pbl_file) != files.end() || (file_exists(parentd + "/" + internal_pbl_file)))) {
res.push_back(*i);
}
}
for(std::vector<std::string>::const_iterator i = files.begin(); i != files.end(); ++i) {
const size_t length = i->size() - 4;
if (i->rfind(".cfg", length) != length) continue;
const std::string name = i->substr(0, length);
// Continue if there is a dir (which we already processed).
if (std::find(dirs.begin(), dirs.end(), name) != dirs.end()) continue;
if (std::find(files.begin(), files.end(), name + ".pbl") != files.end()) {
res.push_back(name);
}
}
return res;
}
std::vector<std::string> installed_addons()
{
std::vector<std::string> res;
const std::string parentd = get_addon_campaigns_dir();
std::vector<std::string> files, dirs;
get_files_in_dir(parentd,&files,&dirs);
for(std::vector<std::string>::const_iterator i = dirs.begin(); i != dirs.end(); ++i) {
const std::string external_cfg_file = *i + ".cfg";
const std::string internal_cfg_file = *i + "/_main.cfg";
if(std::find(files.begin(),files.end(),external_cfg_file) != files.end() || file_exists(parentd + "/" + internal_cfg_file)) {
res.push_back(*i);
}
}
return res;
}
static inline bool IsCR(const char& c)
{
return c == '\x0D';
}
static std::string strip_cr(std::string str, bool strip)
{
if(!strip)
return str;
std::string::iterator new_end = std::remove_if(str.begin(), str.end(), IsCR);
str.erase(new_end, str.end());
return str;
}
namespace {
void append_default_ignore_patterns(std::pair<std::vector<std::string>, std::vector<std::string> >& patterns)
{
std::vector<std::string>& files = patterns.first;
std::vector<std::string>& dirs = patterns.second;
/* Don't upload dot-files/dirs, which are hidden files in
UNIX platforms */
files.push_back(".*");
dirs.push_back(".*");
/* MacOS X metadata-like cruft (http://floatingsun.net/2007/02/07/whats-with-__macosx-in-zip-files/) */
dirs.push_back("__MACOSX");
files.push_back("#*#");
files.push_back("*~");
files.push_back("*-bak");
files.push_back("*.swp");
files.push_back("*.pbl");
files.push_back("*.ign");
files.push_back("_info.cfg");
files.push_back("*.exe");
files.push_back("*.bat");
files.push_back("*.cmd");
files.push_back("*.com");
files.push_back("*.scr");
files.push_back("*.sh");
files.push_back("*.js");
files.push_back("*.vbs");
files.push_back("*.o");
/* Remove junk created by certain file manager ;) */
files.push_back("Thumbs.db");
/* Eclipse plugin */
files.push_back("*.wesnoth");
files.push_back("*.project");
}
}
static std::pair<std::vector<std::string>, std::vector<std::string> > read_ignore_patterns(const std::string& addon_name)
{
const std::string parentd = get_addon_campaigns_dir();
const std::string exterior = parentd + "/" + addon_name + ".ign";
const std::string interior = parentd + "/" + addon_name + "/_server.ign";
std::pair<std::vector<std::string>, std::vector<std::string> > patterns;
std::string ign_file;
LOG_CFG << "searching for .ign file for '" << addon_name << "'...\n";
if (file_exists(interior)) {
ign_file = interior;
} else if (file_exists(exterior)) {
ign_file = exterior;
} else {
LOG_CFG << "no .ign file found for '" << addon_name << "'\n"
<< "inserting default ignore patterns...\n";
append_default_ignore_patterns(patterns);
return patterns; // just default patterns
}
LOG_CFG << "found .ign file: " << ign_file << '\n';
std::istream *stream = istream_file(ign_file);
std::string line;
while (std::getline(*stream, line)) {
utils::strip(line);
const size_t l = line.size();
if (line[l - 1] == '/') { // directory; we strip the last /
patterns.second.push_back(line.substr(0, l - 1));
} else { // file
patterns.first.push_back(line);
}
}
return patterns;
}
static void archive_file(const std::string& path, const std::string& fname, config& cfg)
{
cfg["name"] = fname;
const bool is_cfg = (fname.size() > 4 ? (fname.substr(fname.size() - 4) == ".cfg") : false);
cfg["contents"] = encode_binary(strip_cr(read_file(path + '/' + fname),is_cfg));
}
static void archive_dir(const std::string& path, const std::string& dirname, config& cfg, std::pair<std::vector<std::string>, std::vector<std::string> >& ignore_patterns)
{
cfg["name"] = dirname;
const std::string dir = path + '/' + dirname;
std::vector<std::string> files, dirs;
get_files_in_dir(dir,&files,&dirs);
for(std::vector<std::string>::const_iterator i = files.begin(); i != files.end(); ++i) {
bool valid = !looks_like_pbl(*i);
for(std::vector<std::string>::const_iterator p = ignore_patterns.first.begin(); p != ignore_patterns.first.end(); ++p) {
if (utils::wildcard_string_match(*i, *p)) {
valid = false;
break;
}
}
if (valid) {
archive_file(dir,*i,cfg.add_child("file"));
}
}
for(std::vector<std::string>::const_iterator j = dirs.begin(); j != dirs.end(); ++j) {
bool valid = true;
for(std::vector<std::string>::const_iterator p = ignore_patterns.second.begin(); p != ignore_patterns.second.end(); ++p) {
if (utils::wildcard_string_match(*j, *p)) {
valid = false;
break;
}
}
if (valid) {
archive_dir(dir,*j,cfg.add_child("dir"),ignore_patterns);
}
}
}
void archive_addon(const std::string& addon_name, config& cfg)
{
const std::string parentd = get_addon_campaigns_dir();
std::pair<std::vector<std::string>, std::vector<std::string> > ignore_patterns;
// External .cfg may not exist; newer campaigns have a _main.cfg
const std::string external_cfg = addon_name + ".cfg";
if (file_exists(parentd + "/" + external_cfg)) {
archive_file(parentd, external_cfg, cfg.add_child("file"));
}
ignore_patterns = read_ignore_patterns(addon_name);
archive_dir(parentd, addon_name, cfg.add_child("dir"), ignore_patterns);
}
static void unarchive_file(const std::string& path, const config& cfg)
{
write_file(path + '/' + cfg["name"].str(), unencode_binary(cfg["contents"]));
}
static void unarchive_dir(const std::string& path, const config& cfg)
{
std::string dir;
if (cfg["name"].empty())
dir = path;
else
dir = path + '/' + cfg["name"].str();
make_directory(dir);
BOOST_FOREACH(const config &d, cfg.child_range("dir")) {
unarchive_dir(dir, d);
}
BOOST_FOREACH(const config &f, cfg.child_range("file")) {
unarchive_file(dir, f);
}
}
void unarchive_addon(const config& cfg)
{
const std::string parentd = get_addon_campaigns_dir();
unarchive_dir(parentd, cfg);
}
namespace {
/** Class to handle showing add-on descriptions. */
class display_description : public gui::dialog_button_action
{
display& disp_;
std::vector<addon_info> infov_;
gui::filter_textbox* filter_;
public:
display_description(display& disp, std::vector<addon_info> const& infov, gui::filter_textbox* filter)
: disp_(disp)
, infov_(infov)
, filter_(filter)
{}
virtual gui::dialog_button_action::RESULT button_pressed(int filter_choice)
{
assert(filter_ != NULL);
const int menu_selection = filter_->get_index(filter_choice);
if(menu_selection < 0) { return gui::CONTINUE_DIALOG; }
size_t const uchoice = static_cast<size_t>(menu_selection);
if(uchoice < infov_.size()) {
gui2::taddon_description::display(
infov_[uchoice]
, disp_.video());
}
return gui::CONTINUE_DIALOG;
}
};
/**
* Strip the ".cfg" extension..
*
* @param files List of files in the add-ons directory.
* @param dirs List of subdirectories in the add-ons directory.
* @param parent_dir Path to the add-ons directory.
*/
void prepare_addons_list_for_display(std::vector<std::string>& files,
std::vector<std::string>& dirs,
const std::string& parent_dir)
{
std::vector<std::string>::iterator i = files.begin();
while(i != files.end())
{
const std::string::size_type pos = i->rfind(".cfg", i->size());
if(pos == std::string::npos) {
i = files.erase(i);
} else {
i->erase(pos);
// remove it from the directory list too
for(std::vector<std::string>::iterator j = dirs.begin(); j != dirs.end() ; ++j) {
if (*i == *j) {
dirs.erase(j);
break;
}
};
++i;
}
}
// process items of type Addon/_main.cfg too
i = dirs.begin();
while(i != dirs.end())
{
if (file_exists(parent_dir + *i + "/_main.cfg")) {
files.push_back(*i);
++i;
} else {
i = dirs.erase(i);
}
}
}
/**
* Creates a more human-readable representation of a file size.
*
* @returns Representation of file size in the biggest byte multiply
* possible.
*/
static std::string format_file_size(double size)
{
if(size > 0.0) {
return utils::si_string(size, true, _("unit_byte^B"));
} else {
return "";
}
}
/**
* Return a short string describing an add-on's type.
*
* @param type Numerical add-on type.
*
* @return A string, translated to the current locale.
*/
std::string get_translatable_addon_type(ADDON_TYPE type)
{
switch (type) {
case ADDON_SP_CAMPAIGN:
return _("addon_type^Campaign");
case ADDON_SP_SCENARIO:
return _("addon_type^Scenario");
case ADDON_MP_ERA:
return _("addon_type^MP era");
case ADDON_MP_FACTION:
return _("addon_type^MP faction");
case ADDON_MP_MAPS:
return _("addon_type^MP map-pack");
case ADDON_MP_SCENARIO:
return _("addon_type^MP scenario");
case ADDON_MP_CAMPAIGN:
return _("addon_type^MP campaign");
case ADDON_MEDIA:
return _("addon_type^Resources");
case ADDON_OTHER:
return _("addon_type^Other");
default:
return _("addon_type^(unknown)");
}
}
/** Replaces underscores to dress up file or dirnames as add-on names.
*
* @todo In the future we should store more local information about add-ons and use
* this only as a fallback; it could be desirable to fetch translated names as well
* somehow.
*/
std::string get_addon_name(const std::string& id)
{
std::string retv(id);
std::replace(retv.begin(), retv.end(), '_', ' ');
return retv;
}
void upload_addon_to_server(game_display& disp, const std::string& addon, network_asio::connection& connection)
{
config request;
request.add_child("request_terms");
config response;
connection.transfer(request, response);
gui2::tnetwork_transmission request_terms_dialog(connection, _("Requesting terms"), "");
bool result = request_terms_dialog.show(disp.video());
if(!result) return;
if (const config &c = response.child("error")) {
std::string error_message = _("The server responded with an error: \"$error|\"");
utils::string_map symbols;
symbols["error"] = c["message"].str();
error_message = utils::interpolate_variables_into_string(error_message, &symbols);
gui2::show_error_message(disp.video(), error_message);
return;
} else if (const config &c = response.child("message")) {
if(gui2::show_message(disp.video()
,_("Terms")
, c["message"]
, gui2::tmessage::ok_cancel_buttons) != gui2::twindow::OK) {
return;
}
}
config cfg;
const bool is_old_style = get_addon_info(addon,cfg);
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_info(addon,cfg,is_old_style);
}
cfg["name"] = addon;
config addon_data;
archive_addon(addon,addon_data);
request.clear();
response.clear();
request.add_child("upload",cfg).add_child("data",addon_data);
LOG_NET << "uploading add-on...\n";
connection.transfer(request, response);
gui2::tnetwork_transmission upload_dialog(connection, _("Sending add-on"), "", true);
result = upload_dialog.show(disp.video());
if(!result) return;
if (const config &c = response.child("error")) {
gui2::show_error_message(disp.video(), _("The server responded with an error: \"") +
c["message"].str() + '"');
} else if (const config &c = response.child("message")) {
gui2::show_transient_message(disp.video(), _("Response"), c["message"]);
}
}
void delete_remote_addon(game_display& disp, const std::string& addon, network_asio::connection& connection)
{
config cfg;
get_addon_info(addon,cfg);
config msg;
msg["name"] = addon;
msg["passphrase"] = cfg["passphrase"];
config request, response;
request.add_child("delete",msg);
connection.transfer(request, response);
gui2::tnetwork_transmission network_connect(connection, _("Requesting the addon to be deleted"), "");
bool result = network_connect.show(disp.video());
if(!result) return;
if (const config &c = response.child("error")) {
gui2::show_error_message(disp.video(), _("The server responded with an error: \"") +
c["message"].str() + '"');
} else if (const config &c = response.child("message")) {
gui2::show_transient_message(disp.video(), _("Response"), c["message"]);
}
}
bool install_addon(game_display& disp,
const std::string& addon_id, const std::string& addon_title,
const std::string& addon_type_str, const std::string& addon_uploads_str,
const std::string& addon_version_str,
network_asio::connection& connection, bool* do_refresh,
bool show_result = true)
{
// Proceed to download and install
config request;
request.add_child("request_campaign")["name"] = addon_id;
utils::string_map syms;
syms["addon_title"] = addon_title;
const std::string& download_dlg_title =
utils::interpolate_variables_into_string(_("Downloading add-on: $addon_title|..."), &syms);
// WML structure where the add-on archive, or any error messages, are stored.
config cfg;
connection.transfer(request, cfg);
gui2::tnetwork_transmission network_connect(connection, download_dlg_title, _("Downloading..."));
bool result = network_connect.show(disp.video());
if(!result) {
return false;
}
if (config const &dlerror = cfg.child("error")) {
gui2::show_error_message(disp.video(), dlerror["message"]);
return false;
}
if(!check_names_legal(cfg)) {
gui2::show_error_message(disp.video(), _("The add-on has an invalid file or directory name and cannot be installed."));
return false;
}
// add revision info to the addon archive
config *maindir = &cfg.find_child("dir", "name", addon_id);
if (!*maindir) {
LOG_CFG << "downloaded addon '" << addon_id << "' is missing its own directory, creating...\n";
maindir = &cfg.add_child("dir");
(*maindir)["name"] = addon_id;
}
LOG_CFG << "generating version info for addon '" << addon_id << "'\n";
config f;
f["name"] = "_info.cfg";
utils::string_map info_tab;
info_tab["type"] = !addon_type_str.empty() ? addon_type_str : std::string("unknown");
info_tab["uploads"] = addon_uploads_str;
info_tab["version"] = addon_version_str;
static std::string const info_template =
"#\n"
"# File automatically generated by Wesnoth to keep track\n"
"# of version information on installed add-ons. DO NOT EDIT!\n"
"#\n"
"[info]\n"
" type=\"$type\"\n"
" uploads=\"$uploads\"\n"
" version=\"$version\"\n"
"[/info]\n";
std::string const info_file_contents =
utils::interpolate_variables_into_string(info_template, &info_tab);
f["contents"] = info_file_contents;
maindir->add_child("file", f);
LOG_CFG << "generated version info, unpacking...\n";
// remove any existing versions of the just downloaded add-on,
// assuming it consists of a dir and a cfg file
if(!remove_local_addon(addon_id)) {
WRN_CFG << "failed to uninstall existing add-on version before installing; add-on may not work properly\n";
}
unarchive_addon(cfg);
LOG_CFG << "addon unpacked successfully\n";
if(show_result) {
const std::string& message =
utils::interpolate_variables_into_string(_("The add-on '$addon_title|' has been successfully installed."), &syms);
gui2::show_transient_message(disp.video(), _("Add-on Installed"), message);
}
if(do_refresh != NULL)
*do_refresh = true;
return true;
}
void do_addon_icon_fixups(std::string& icon, const std::string& addon_name)
{
static const std::string default_icon = "misc/blank-hex.png";
static const std::string debug_default_icon = game_config::images::missing;
if(icon.empty()) {
ERR_CFG << "remote add-on '" << addon_name << "' has no icon\n";
icon = default_icon;
}
else if(!image::exists(icon)) {
ERR_CFG << "remote add-on '" << addon_name << "' has an icon which cannot be found: '" << icon << "'\n";
if(game_config::debug) {
icon = debug_default_icon;
}
else {
icon = default_icon;
}
}
else if(icon.find("units/") != std::string::npos && icon.find_first_of('~') == std::string::npos) {
// a hack to prevent magenta icons, because they look awful
LOG_CFG << "remote add-on '" << addon_name << "' has an icon without TC/RC specification\n";
icon.append("~RC(magenta>red)");
}
}
/**
* Checks if an add-on's dependencies are met.
*
* @param disp Object to be used for displaying interactive messages.
* @param deplist List of dependencies (add-on identifiers).
*
* @returns true if dependencies are met; false otherwise.
*/
bool addon_dependencies_met(game_display &disp, config const& addons_tree,
const std::string &addon_id,
network_asio::connection& connection,
bool* do_refresh)
{
const config &selected_campaign = addons_tree.find_child("campaign", "name", addon_id);
assert(selected_campaign);
// Get all dependencies which are not already installed.
// TODO: Somehow determine if the version is outdated.
std::vector<std::string> dependencies = utils::split(selected_campaign["dependencies"]);
const std::vector<std::string>& installed = installed_addons();
std::vector<std::string>::const_iterator i;
std::string missing = "";
size_t count_missing = 0;
BOOST_FOREACH(const std::string& i, dependencies) {
if (std::find(installed.begin(), installed.end(), i) == installed.end()) {
missing += "\n" + i;
++count_missing;
}
}
// If there are any, display a message.
// TODO: Somehow offer to automatically download
// the missing dependencies.
if (!missing.empty()) {
const config::const_child_itors &remote_addons_list = addons_tree.child_range("campaign");
std::vector<const config *> remote_matches_cfgs;
std::vector< std::string > safe_matches;
std::vector< std::string > unsafe_matches;
std::ostringstream unsafe_list;
std::map<std::string, version_info> remote_version_map;
BOOST_FOREACH(const config &remote_addon, remote_addons_list)
{
const std::string& name = remote_addon["name"];
if (std::find(dependencies.begin(), dependencies.end(), name) != dependencies.end()) {
const std::string& version = remote_addon["version"];
try {
remote_version_map.insert(std::make_pair(name, version_info(version)));
} catch(version_info::not_sane_exception const&) {
ERR_CFG << "remote add-on '" << name << "' has invalid version string '" << version << "', skipping from updates check...\n";
continue;
}
std::vector<std::string>::const_iterator local_match =
std::find(installed.begin(), installed.end(), name);
if(local_match == installed.end()) {
safe_matches.push_back(name);
remote_matches_cfgs.push_back(&remote_addon);
}
}
}
if(!safe_matches.empty()) {
// column contents
std::vector<std::string> addons, titles, versions, options, filtered_opts;
std::vector<int> sizes;
std::vector<std::string> types, uploads;
std::string sep(1, COLUMN_SEPARATOR);
const std::string& heading =
(formatter() << HEADING_PREFIX << sep << _("Name") << sep << _("Version") << sep
<< _("Author") << sep << _("Type") << sep << _("Size")).str();
options.push_back(heading);
filtered_opts.push_back(heading);
assert(safe_matches.size() == remote_matches_cfgs.size());
for(size_t i = 0; i < safe_matches.size(); ++i) {
const config& c = *(remote_matches_cfgs[i]);
uploads.push_back(c["uploads"]);
const std::string& type = c["type"];
const std::string& name = c["name"];
int size = c["size"];
std::string sizef = format_file_size(size);
const std::string& version = remote_version_map[name];
std::string author = c["author"];
std::string title = c["title"];
if(title.empty()) {
title = name;
std::replace(title.begin(), title.end(), '_', ' ');
}
utils::truncate_as_wstring(title, 20);
utils::truncate_as_wstring(author, 16);
//add negative sizes to reverse the sort order
sizes.push_back(-size);
types.push_back(type);
titles.push_back(title);
addons.push_back(name);
versions.push_back(version);
std::string icon = c["icon"];
do_addon_icon_fixups(icon, name);
const std::string text_columns =
title + COLUMN_SEPARATOR +
version + COLUMN_SEPARATOR +
author + COLUMN_SEPARATOR +
type + COLUMN_SEPARATOR +
sizef + COLUMN_SEPARATOR;
options.push_back(IMAGE_PREFIX + icon + COLUMN_SEPARATOR + font::NULL_MARKUP + text_columns);
filtered_opts.push_back(text_columns);
}
// Create dialog
gui::dialog upd_dialog(disp,
_("Install dependencies"),
_n("The selected add-on has the following dependency. Do you want to install it?",
"The selected add-on has the following dependencies. Do you want to install them?",
safe_matches.size()
), gui::YES_NO);
gui::menu* addon_menu =
new gui::menu(disp.video(), options, false, -1,
gui::dialog::max_menu_width, NULL, NULL, false);
// Add widgets
upd_dialog.set_menu(addon_menu);
// Activate
int index = upd_dialog.show();
if(index < 0)
return true;
bool result = true;
std::vector<std::string> failed_titles;
for(size_t i = 0; i < addons.size() && i < remote_matches_cfgs.size(); ++i)
{
if (!install_addon(disp, addons[i], titles[i],
types[i], uploads[i], versions[i], connection,
do_refresh, false)) {
result=false;
failed_titles.push_back(titles[i]);
} else {
if (!addon_dependencies_met(disp, addons_tree, addons[i], connection, do_refresh)) {
const std::string err_title = _("Installation of a dependency failed");
const std::string err_message =
_("While the add-on has been installed, a dependency is missing. Try to update the installed add-ons.");
gui2::show_message(disp.video(), err_title, err_message);
}
}
}
if(!result) {
assert(failed_titles.empty() == false);
std::string failed_titles_list_fmt;
BOOST_FOREACH(const std::string& entry, failed_titles) {
failed_titles_list_fmt += '\n';
failed_titles_list_fmt += entry;
}
const std::string err_title = _("Installation failed");
const std::string err_message =
_n("The following add-on could not be downloaded or updated successfully:",
"The following add-ons could not be downloaded or updated successfully:",
failed_titles.size()) + failed_titles_list_fmt;
gui2::show_message(disp.video(), err_title, err_message);
return true;
}
}
}
return true;
}
void addons_update_dlg(game_display &disp, config const& addons_tree, const config::const_child_itors &remote_addons_list,
network_asio::connection& connection,
bool* do_refresh)
{
std::vector<const config *> remote_matches_cfgs;
std::vector< std::string > safe_matches;
std::vector< std::string > unsafe_matches;
std::ostringstream unsafe_list;
const std::vector< std::string >& all_local = installed_addons();
// Add-ons that can be published and are outdated will not be offered for update,
// but a message will be displayed warning about them to the user.
const std::vector< std::string >& all_publish = available_addons();
std::vector<version_info> safe_local_versions;
std::vector<version_info> unsafe_local_versions;
std::map<std::string, version_info> remote_version_map;
BOOST_FOREACH(const config &remote_addon, remote_addons_list)
{
const std::string& name = remote_addon["name"];
const std::string& version = remote_addon["version"];
try {
remote_version_map.insert(std::make_pair(name, version_info(version)));
} catch(version_info::not_sane_exception const&) {
ERR_CFG << "remote add-on '" << name << "' has invalid version string '" << version << "', skipping from updates check...\n";
continue;
}
std::vector<std::string>::const_iterator local_match =
std::find(all_local.begin(), all_local.end(), name);
try {
if(local_match != all_local.end()) {
const version_info& local_version = get_addon_version_info(name);
if(remote_version_map[name] > local_version) {
if(std::find(all_publish.begin(), all_publish.end(), name) != all_publish.end()) {
unsafe_matches.push_back(name);
unsafe_local_versions.push_back(local_version);
unsafe_list << '\n';
unsafe_list << name << " (local: " << local_version.str() << ", remote: " << version << ")";
} else {
safe_matches.push_back(name);
safe_local_versions.push_back(local_version);
remote_matches_cfgs.push_back(&remote_addon);
}
}
}
} catch(version_info::not_sane_exception const&) {
ERR_CFG << "local add-on '" << name << "' has invalid or no version info, skipping from updates check...\n";
continue;
}
}
if(!unsafe_matches.empty()) {
const std::string warn_title = _("Outdated add-ons");
const std::string warn_entrytxt = _n(
"An outdated local add-on has publishing information attached. It will not be offered for updating.",
"Some outdated local add-ons have publishing information attached. They will not be offered for updating.",
unsafe_matches.size());
gui2::show_transient_message(disp.video()
, warn_title
, warn_entrytxt + unsafe_list.str());
}
if(safe_matches.empty()) {
gui2::show_transient_message(disp.video()
, _("No add-ons to update")
, _("Could not find any updated add-ons on this server."));
return;
}
// column contents
std::vector<std::string> addons, titles, oldversions, newversions, options, filtered_opts;
std::vector<int> sizes;
std::vector<std::string> types, uploads;
std::string sep(1, COLUMN_SEPARATOR);
const std::string& heading =
(formatter() << HEADING_PREFIX << sep << _("Name") << sep << _("Old version") << sep << _("New version") << sep
<< _("Author") << sep << _("Size")).str();
options.push_back(heading);
filtered_opts.push_back(heading);
assert(safe_matches.size() == safe_local_versions.size());
assert(safe_matches.size() == remote_matches_cfgs.size());
for(size_t i = 0; i < safe_matches.size(); ++i) {
const config& c = *(remote_matches_cfgs[i]);
types.push_back(c["type"]);
uploads.push_back(c["uploads"]);
const std::string& name = c["name"];
int size = c["size"];
std::string sizef = format_file_size(size);
const std::string& oldver = safe_local_versions[i];
const std::string& newver = remote_version_map[name];
std::string author = c["author"];
std::string title = c["title"];
if(title.empty()) {
title = name;
std::replace(title.begin(), title.end(), '_', ' ');
}
utils::truncate_as_wstring(title, 20);
utils::truncate_as_wstring(author, 16);
//add negative sizes to reverse the sort order
sizes.push_back(-size);
addons.push_back(name);
titles.push_back(title);
oldversions.push_back(oldver);
newversions.push_back(newver);
std::string icon = c["icon"];
do_addon_icon_fixups(icon, name);
const std::string text_columns =
title + COLUMN_SEPARATOR +
oldver + COLUMN_SEPARATOR +
newver + COLUMN_SEPARATOR +
author + COLUMN_SEPARATOR +
sizef + COLUMN_SEPARATOR;
options.push_back(IMAGE_PREFIX + icon + COLUMN_SEPARATOR + font::NULL_MARKUP + text_columns);
filtered_opts.push_back(text_columns);
}
// Create dialog
gui::dialog upd_dialog(disp,
_("Update add-ons"),
_("Select an add-on to update:"), gui::OK_CANCEL);
// Create widgets
gui::menu::basic_sorter sorter;
sorter.set_alpha_sort(1).set_alpha_sort(2).set_alpha_sort(3).set_alpha_sort(4).set_position_sort(5,sizes);
gui::menu::imgsel_style addon_style(gui::menu::bluebg_style);
addon_style.scale_images(font::relative_size(72), font::relative_size(72));
gui::menu* addon_menu =
new gui::menu(disp.video(), options, false, -1,
gui::dialog::max_menu_width, &sorter,
&addon_style, false);