The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
addon_utils.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
3  2013 - 2015 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project http://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 
24 #include <boost/algorithm/string.hpp>
25 
26 static lg::log_domain log_network("network");
27 #define LOG_CS if (lg::err().dont_log(log_network)) ; else lg::err()(log_network, false)
28 
29 namespace {
30 
31 typedef std::map<std::string, std::string> plain_string_map;
32 
33 /**
34  * Quick and dirty alternative to @a utils::interpolate_variables_into_string
35  * that doesn't require formula AI code. It is definitely NOT safe for normal
36  * use since it doesn't do strict checks on where variable placeholders
37  * ("$foobar") end and doesn't support pipe ("|") terminators.
38  *
39  * @param str The format string.
40  * @param symbols The symbols table.
41  */
42 std::string fast_interpolate_variables_into_string(const std::string &str, const plain_string_map * const symbols)
43 {
44  std::string res = str;
45 
46  if(symbols) {
47  for(const plain_string_map::value_type& sym : *symbols) {
48  boost::replace_all(res, "$" + sym.first, sym.second);
49  }
50  }
51 
52  return res;
53 }
54 
55 } // end anonymous namespace
56 
57 namespace campaignd {
58 
59 // Markup characters recognized by GUI1 code. These must be
60 // the same as the constants defined in marked-up_text.cpp.
61 const std::string illegal_markup_chars = "*`~{^}|@#<&";
62 
64 {
65  if(!format.empty() && !params.empty()) {
66  plain_string_map escaped;
67 
69 
70  // Percent-encode parameter values for URL interpolation. This is
71  // VERY important since otherwise people could e.g. alter query
72  // strings from the format string.
73  for(const config::attribute& a : attrs) {
74  escaped[a.first] = utils::urlencode(a.second.str());
75  }
76 
77  // FIXME: We cannot use utils::interpolate_variables_into_string
78  // because it is implemented using a lot of formula AI junk
79  // that really doesn't belong in campaignd.
80  const std::string& res =
81  fast_interpolate_variables_into_string(format, &escaped);
82 
83  if(res != format) {
84  return res;
85  }
86 
87  // If we get here, that means that no interpolation took place; in
88  // that case, the parameters table probably contains entries that
89  // do not match the format string expectations.
90  }
91 
92  return std::string();
93 }
94 
95 void find_translations(const config& base_dir, config& addon)
96 {
97  for(const config &dir : base_dir.child_range("dir"))
98  {
99  if(dir["name"] == "LC_MESSAGES") {
100  addon.add_child("translation")["language"] = base_dir["name"];
101  } else {
102  find_translations(dir, addon);
103  }
104  }
105 }
106 
107 void add_license(config& cfg)
108 {
109  config& dir = cfg.find_child("dir", "name", cfg["campaign_name"]);
110 
111  // No top-level directory? Hm..
112  if(!dir) {
113  return;
114  }
115 
116  // Don't add if it already exists.
117  if(dir.find_child("file", "name", "COPYING.txt")
118  || dir.find_child("file", "name", "COPYING")
119  || dir.find_child("file", "name", "copying.txt")
120  || dir.find_child("file", "name", "Copying.txt")
121  || dir.find_child("file", "name", "COPYING.TXT"))
122  {
123  return;
124  }
125 
126  // Copy over COPYING.txt
127  const std::string& contents = filesystem::read_file("COPYING.txt");
128  if (contents.empty()) {
129  LOG_CS << "Could not find COPYING.txt, path is \"" << game_config::path << "\"\n";
130  return;
131  }
132 
133  config& copying = dir.add_child("file");
134  copying["name"] = "COPYING.txt";
135  copying["contents"] = contents;
136 }
137 
138 } // end namespace campaignd
std::vector< char_t > string
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:763
#define a
child_itors child_range(config_key_type key)
Definition: config.cpp:343
attribute_map::value_type attribute
Definition: config.hpp:254
bool empty() const
Definition: config.cpp:811
Definitions for the interface to Wesnoth Markup Language (WML).
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:56
boost::iterator_range< const_attribute_iterator > const_attr_itors
Definition: config.hpp:314
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
const_attr_itors attribute_range() const
Definition: config.cpp:736
Declarations for File-IO.
config & add_child(config_key_type key)
Definition: config.cpp:456
#define LOG_CS
Definition: addon_utils.cpp:27
Standard logging facilities (interface).
void find_translations(const config &base_dir, config &addon)
Scans an add-on archive directory for translations.
Definition: addon_utils.cpp:95
const std::string illegal_markup_chars
Markup characters recognized by GUI1 code.
Definition: addon_utils.cpp:61
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
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:63