The Battle for Wesnoth  1.19.0-dev
addon_utils.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
4  Copyright (C) 2013 - 2015 by Iris Morelle <shadowm2006@gmail.com>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
18 
19 #include "config.hpp"
20 #include "filesystem.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).ptr();
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  auto dir = cfg.optional_child("dir");
128 
129  // No top-level directory? Hm..
130  if(!dir) {
131  LOG_CS << "Could not find toplevel [dir] tag";
132  return;
133  }
134 
135  // Don't add if it already exists.
136  if(dir->find_child("file", "name", "COPYING.txt")
137  || dir->find_child("file", "name", "COPYING")
138  || dir->find_child("file", "name", "copying.txt")
139  || dir->find_child("file", "name", "Copying.txt")
140  || dir->find_child("file", "name", "COPYING.TXT"))
141  {
142  return;
143  }
144 
145  // Copy over COPYING.txt
146  const std::string& contents = filesystem::read_file("COPYING.txt");
147  if (contents.empty()) {
148  LOG_CS << "Could not find COPYING.txt, path is \"" << game_config::path << "\"";
149  return;
150  }
151 
152  config& copying = dir->add_child("file");
153  copying["name"] = "COPYING.txt";
154  copying["contents"] = contents;
155 }
156 
157 std::map<version_info, config> get_version_map(config& addon)
158 {
159  std::map<version_info, config> version_map;
160 
161  for(config& version : addon.child_range("version")) {
162  version_map.emplace(version_info(version["version"]), version);
163  }
164 
165  return version_map;
166 }
167 
168 bool data_apply_removelist(config& data, const config& removelist)
169 {
170  for(const config& f : removelist.child_range("file")) {
171  data.remove_children("file", [&f](const config& d) { return f["name"] == d["name"]; });
172  }
173 
174  for(const config& dir : removelist.child_range("dir")) {
175  auto data_dir = data.find_child("dir", "name", dir["name"]);
176  if(data_dir && !data_apply_removelist(*data_dir, dir)) {
177  // Delete empty directories
178  data.remove_children("dir", [&dir](const config& d) { return dir["name"] == d["name"]; });
179  }
180  }
181 
182  return data.has_child("file") || data.has_child("dir");
183 }
184 
185 void data_apply_addlist(config& data, const config& addlist)
186 {
187  for(const config& f : addlist.child_range("file")) {
188  // Just add it since we have already checked the data for duplicates
189  data.add_child("file", f);
190  }
191 
192  for(const config& dir : addlist.child_range("dir")) {
193  config* data_dir = data.find_child("dir", "name", dir["name"]).ptr();
194  if(!data_dir) {
195  data_dir = &data.add_child("dir");
196  (*data_dir)["name"] = dir["name"];
197  }
198  data_apply_addlist(*data_dir, dir);
199  }
200 }
201 
202 } // end namespace campaignd
#define LOG_CS
Definition: addon_utils.cpp:28
static lg::log_domain log_network("network")
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
const_attr_itors attribute_range() const
Definition: config.cpp:763
optional_config_impl< 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:787
child_itors child_range(config_key_type key)
Definition: config.cpp:273
boost::iterator_range< const_attribute_iterator > const_attr_itors
Definition: config.hpp:359
attribute_map::value_type attribute
Definition: config.hpp:299
bool empty() const
Definition: config.cpp:852
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Euivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:385
config & add_child(config_key_type key)
Definition: config.cpp:441
Represents version numbers.
Declarations for File-IO.
Standard logging facilities (interface).
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
void data_apply_addlist(config &data, const config &addlist)
const std::string illegal_markup_chars
Markup characters recognized by GUI1 code.
Definition: addon_utils.cpp:62
void find_translations(const config &base_dir, config &addon)
Scans an add-on archive directory for translations.
bool data_apply_removelist(config &data, const config &removelist)
void support_translation(config &addon, const std::string &locale_id)
Definition: addon_utils.cpp:96
std::map< version_info, config > get_version_map(config &addon)
void add_license(config &cfg)
Adds a COPYING.txt file with the full text of the GNU GPL to an add-on.
std::string base_name(const std::string &file, const bool remove_extension)
Returns the base filename of a file, with directory name stripped.
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
bool ends_with(const std::string &str, const std::string &suffix)
std::string path
Definition: filesystem.cpp:83
std::string urlencode(const std::string &str)
Percent-escape characters in a UTF-8 string intended to be part of a URL.
std::string_view data
Definition: picture.cpp:194
#define d
#define f
#define a