The Battle for Wesnoth  1.15.12+dev
addon_utils.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  2013 - 2015 by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
17 
18 #include "config.hpp"
19 #include "filesystem.hpp"
20 #include "game_config.hpp"
21 #include "log.hpp"
23 #include "addon/validation.hpp"
24 
25 #include <boost/algorithm/string.hpp>
26 
27 static lg::log_domain log_network("network");
28 #define LOG_CS LOG_STREAM_NAMELESS(err, log_network)
29 
30 namespace {
31 
32 typedef std::map<std::string, std::string> plain_string_map;
33 
34 /**
35  * Quick and dirty alternative to @a utils::interpolate_variables_into_string
36  * that doesn't require formula AI code. It is definitely NOT safe for normal
37  * use since it doesn't do strict checks on where variable placeholders
38  * ("$foobar") end and doesn't support pipe ("|") terminators.
39  *
40  * @param str The format string.
41  * @param symbols The symbols table.
42  */
43 std::string fast_interpolate_variables_into_string(const std::string &str, const plain_string_map * const symbols)
44 {
45  std::string res = str;
46 
47  if(symbols) {
48  for(const plain_string_map::value_type& sym : *symbols) {
49  boost::replace_all(res, "$" + sym.first, sym.second);
50  }
51  }
52 
53  return res;
54 }
55 
56 } // end anonymous namespace
57 
58 namespace campaignd {
59 
60 // Markup characters recognized by GUI1 code. These must be
61 // the same as the constants defined in marked-up_text.cpp.
62 const std::string illegal_markup_chars = "*`~{^}|@#<&";
63 
64 std::string format_addon_feedback_url(const std::string& format, const config& params)
65 {
66  if(!format.empty() && !params.empty()) {
67  plain_string_map escaped;
68 
70 
71  // Percent-encode parameter values for URL interpolation. This is
72  // VERY important since otherwise people could e.g. alter query
73  // strings from the format string.
74  for(const config::attribute& a : attrs) {
75  escaped[a.first] = utils::urlencode(a.second.str());
76  }
77 
78  // FIXME: We cannot use utils::interpolate_variables_into_string
79  // because it is implemented using a lot of formula AI junk
80  // that really doesn't belong in campaignd.
81  const std::string& res =
82  fast_interpolate_variables_into_string(format, &escaped);
83 
84  if(res != format) {
85  return res;
86  }
87 
88  // If we get here, that means that no interpolation took place; in
89  // that case, the parameters table probably contains entries that
90  // do not match the format string expectations.
91  }
92 
93  return std::string();
94 }
95 
96 void support_translation(config& addon, const std::string& locale_id)
97 {
98  config* locale = &addon.find_child("translation", "language", locale_id);
99  if(!*locale) {
100  locale = &addon.add_child("translation");
101  (*locale)["language"] = locale_id;
102  }
103  (*locale)["supported"] = true;
104 }
105 
106 void find_translations(const config& base_dir, config& addon)
107 {
108  for(const config& file : base_dir.child_range("file")) {
109  const std::string& fn = file["name"].str();
110  if(filesystem::ends_with(fn, ".po")) {
111  support_translation(addon, filesystem::base_name(fn, true));
112  }
113  }
114 
115  for(const config &dir : base_dir.child_range("dir"))
116  {
117  if(dir["name"] == "LC_MESSAGES") {
118  support_translation(addon, base_dir["name"]);
119  } else {
120  find_translations(dir, addon);
121  }
122  }
123 }
124 
125 void add_license(config& cfg)
126 {
127  config& dir = cfg.find_child("dir", "name", cfg["campaign_name"]);
128 
129  // No top-level directory? Hm..
130  if(!dir) {
131  return;
132  }
133 
134  // Don't add if it already exists.
135  if(dir.find_child("file", "name", "COPYING.txt")
136  || dir.find_child("file", "name", "COPYING")
137  || dir.find_child("file", "name", "copying.txt")
138  || dir.find_child("file", "name", "Copying.txt")
139  || dir.find_child("file", "name", "COPYING.TXT"))
140  {
141  return;
142  }
143 
144  // Copy over COPYING.txt
145  const std::string& contents = filesystem::read_file("COPYING.txt");
146  if (contents.empty()) {
147  LOG_CS << "Could not find COPYING.txt, path is \"" << game_config::path << "\"\n";
148  return;
149  }
150 
151  config& copying = dir.add_child("file");
152  copying["name"] = "COPYING.txt";
153  copying["contents"] = contents;
154 }
155 
156 std::map<version_info, config> get_version_map(config& addon)
157 {
158  std::map<version_info, config> version_map;
159 
160  for(config& version : addon.child_range("version")) {
161  version_map.emplace(version_info(version["version"]), version);
162  }
163 
164  return version_map;
165 }
166 
167 bool data_apply_removelist(config& data, const config& removelist)
168 {
169  for(const config& f : removelist.child_range("file")) {
170  data.remove_children("file", [&f](const config& d) { return f["name"] == d["name"]; });
171  }
172 
173  for(const config& dir : removelist.child_range("dir")) {
174  config& data_dir = data.find_child("dir", "name", dir["name"]);
175  if(data_dir && !data_apply_removelist(data_dir, dir)) {
176  // Delete empty directories
177  data.remove_children("dir", [&dir](const config& d) { return dir["name"] == d["name"]; });
178  }
179  }
180 
181  return data.has_child("file") || data.has_child("dir");
182 }
183 
184 void data_apply_addlist(config& data, const config& addlist)
185 {
186  for(const config& f : addlist.child_range("file")) {
187  // Just add it since we have already checked the data for duplicates
188  data.add_child("file", f);
189  }
190 
191  for(const config& dir : addlist.child_range("dir")) {
192  config* data_dir = &data.find_child("dir", "name", dir["name"]);
193  if(!*data_dir) {
194  data_dir = &data.add_child("dir");
195  (*data_dir)["name"] = dir["name"];
196  }
197  data_apply_addlist(*data_dir, dir);
198  }
199 }
200 
201 } // end namespace campaignd
std::string urlencode(const std::string &str)
Percent-escape characters in a UTF-8 string intended to be part of a URL.
config & find_child(config_key_type key, const std::string &name, const std::string &value)
Returns the first child of tag key with a name attribute containing value.
Definition: config.cpp:860
void support_translation(config &addon, const std::string &locale_id)
Definition: addon_utils.cpp:96
#define a
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:406
bool ends_with(const std::string &str, const std::string &suffix)
std::map< version_info, config > get_version_map(config &addon)
child_itors child_range(config_key_type key)
Definition: config.cpp:356
attribute_map::value_type attribute
Definition: config.hpp:220
bool data_apply_removelist(config &data, const config &removelist)
#define d
Definitions for the interface to Wesnoth Markup Language (WML).
void data_apply_addlist(config &data, const config &addlist)
const_attr_itors attribute_range() const
Definition: config.cpp:833
void add_license(config &cfg)
Adds a COPYING.txt file with the full text of the GNU GPL to an add-on.
static lg::log_domain log_network("network")
std::string path
Definition: game_config.cpp:38
boost::iterator_range< const_attribute_iterator > const_attr_itors
Definition: config.hpp:280
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
Definition: filesystem.cpp:995
Declarations for File-IO.
Represents version numbers.
config & add_child(config_key_type key)
Definition: config.cpp:500
#define LOG_CS
Definition: addon_utils.cpp:28
std::string base_name(const std::string &file, const bool remove_extension)
Returns the base filename of a file, with directory name stripped.
#define f
Standard logging facilities (interface).
void find_translations(const config &base_dir, config &addon)
Scans an add-on archive directory for translations.
const std::string illegal_markup_chars
Markup characters recognized by GUI1 code.
Definition: addon_utils.cpp:62
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
std::string format_addon_feedback_url(const std::string &format, const config &params)
Format a feedback URL for an add-on.
Definition: addon_utils.cpp:64
bool empty() const
Definition: config.cpp:916
void remove_children(config_key_type key, std::function< bool(const config &)> p)
Removes all children with tag key for which p returns true.
Definition: config.cpp:731