The Battle for Wesnoth  1.15.0-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 
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 
63 std::string format_addon_feedback_url(const std::string& format, const config& params)
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& file : base_dir.child_range("file")) {
98  const std::string& fn = file["name"].str();
99  if(filesystem::ends_with(fn, ".po")) {
100  addon.add_child("translation")["language"] = filesystem::base_name(fn, true);
101  }
102  }
103 
104  for(const config &dir : base_dir.child_range("dir"))
105  {
106  if(dir["name"] == "LC_MESSAGES") {
107  addon.add_child("translation")["language"] = base_dir["name"];
108  } else {
109  find_translations(dir, addon);
110  }
111  }
112 }
113 
114 void add_license(config& cfg)
115 {
116  config& dir = cfg.find_child("dir", "name", cfg["campaign_name"]);
117 
118  // No top-level directory? Hm..
119  if(!dir) {
120  return;
121  }
122 
123  // Don't add if it already exists.
124  if(dir.find_child("file", "name", "COPYING.txt")
125  || dir.find_child("file", "name", "COPYING")
126  || dir.find_child("file", "name", "copying.txt")
127  || dir.find_child("file", "name", "Copying.txt")
128  || dir.find_child("file", "name", "COPYING.TXT"))
129  {
130  return;
131  }
132 
133  // Copy over COPYING.txt
134  const std::string& contents = filesystem::read_file("COPYING.txt");
135  if (contents.empty()) {
136  LOG_CS << "Could not find COPYING.txt, path is \"" << game_config::path << "\"\n";
137  return;
138  }
139 
140  config& copying = dir.add_child("file");
141  copying["name"] = "COPYING.txt";
142  copying["contents"] = contents;
143 }
144 
145 } // 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:838
#define a
bool ends_with(const std::string &str, const std::string &suffix)
child_itors child_range(config_key_type key)
Definition: config.cpp:366
attribute_map::value_type attribute
Definition: config.hpp:256
Definitions for the interface to Wesnoth Markup Language (WML).
const_attr_itors attribute_range() const
Definition: config.cpp:811
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:39
boost::iterator_range< const_attribute_iterator > const_attr_itors
Definition: config.hpp:316
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
Definition: filesystem.cpp:857
Declarations for File-IO.
config & add_child(config_key_type key)
Definition: config.cpp:479
#define LOG_CS
Definition: addon_utils.cpp:27
std::string base_name(const std::string &file, const bool remove_extension)
Returns the base filename of a file, with directory name stripped.
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:92
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
bool empty() const
Definition: config.cpp:886