The Battle for Wesnoth  1.13.4+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 - 2016 by David White <[email protected]>
3  2013 - 2015 by Ignacio Riquelme Morelle <[email protected]>
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 static lg::log_domain log_network("network");
25 #define LOG_CS if (lg::err().dont_log(log_network)) ; else lg::err()(log_network, false)
26 
27 namespace {
28 
29 typedef std::map<std::string, std::string> plain_string_map;
30 
31 /**
32  * Quick and dirty alternative to @a utils::interpolate_variables_into_string
33  * that doesn't require formula AI code. It is definitely NOT safe for normal
34  * use since it doesn't do strict checks on where variable placeholders
35  * ("$foobar") end and doesn't support pipe ("|") terminators.
36  *
37  * @param str The format string.
38  * @param symbols The symbols table.
39  */
40 std::string fast_interpolate_variables_into_string(const std::string &str, const plain_string_map * const symbols)
41 {
42  std::string res = str;
43 
44  if(symbols) {
45  for(const plain_string_map::value_type& sym : *symbols) {
46  res = utils::replace(res, "$" + sym.first, sym.second);
47  }
48  }
49 
50  return res;
51 }
52 
53 } // end anonymous namespace
54 
55 namespace campaignd {
56 
57 // Markup characters recognized by GUI1 code. These must be
58 // the same as the constants defined in marked-up_text.cpp.
59 const std::string illegal_markup_chars = "*`~{^}|@#<&";
60 
62 {
63  if(!format.empty() && !params.empty()) {
64  plain_string_map escaped;
65 
67 
68  // Percent-encode parameter values for URL interpolation. This is
69  // VERY important since otherwise people could e.g. alter query
70  // strings from the format string.
71  for(const config::attribute& a : attrs) {
72  escaped[a.first] = utils::urlencode(a.second.str());
73  }
74 
75  // FIXME: We cannot use utils::interpolate_variables_into_string
76  // because it is implemented using a lot of formula AI junk
77  // that really doesn't belong in campaignd.
78  const std::string& res =
79  fast_interpolate_variables_into_string(format, &escaped);
80 
81  if(res != format) {
82  return res;
83  }
84 
85  // If we get here, that means that no interpolation took place; in
86  // that case, the parameters table probably contains entries that
87  // do not match the format string expectations.
88  }
89 
90  return std::string();
91 }
92 
93 void find_translations(const config& base_dir, config& addon)
94 {
95  for(const config &dir : base_dir.child_range("dir"))
96  {
97  if(dir["name"] == "LC_MESSAGES") {
98  addon.add_child("translation")["language"] = base_dir["name"];
99  } else {
100  find_translations(dir, addon);
101  }
102  }
103 }
104 
105 void add_license(config& cfg)
106 {
107  config& dir = cfg.find_child("dir", "name", cfg["campaign_name"]);
108 
109  // No top-level directory? Hm..
110  if(!dir) {
111  return;
112  }
113 
114  // Don't add if it already exists.
115  if(dir.find_child("file", "name", "COPYING.txt")
116  || dir.find_child("file", "name", "COPYING")
117  || dir.find_child("file", "name", "copying.txt")
118  || dir.find_child("file", "name", "Copying.txt")
119  || dir.find_child("file", "name", "COPYING.TXT"))
120  {
121  return;
122  }
123 
124  // Copy over COPYING.txt
125  const std::string& contents = filesystem::read_file("COPYING.txt");
126  if (contents.empty()) {
127  LOG_CS << "Could not find COPYING.txt, path is \"" << game_config::path << "\"\n";
128  return;
129  }
130 
131  config& copying = dir.add_child("file");
132  copying["name"] = "COPYING.txt";
133  copying["contents"] = contents;
134 }
135 
136 } // end namespace campaignd
child_itors child_range(const std::string &key)
Definition: config.cpp:613
std::string urlencode(const std::string &str)
Percent-escape characters in a UTF-8 string intended to be part of a URL.
std::pair< const_attribute_iterator, const_attribute_iterator > const_attr_itors
Definition: config.hpp:418
attribute_map::value_type attribute
Definition: config.hpp:393
const GLfloat * params
Definition: glew.h:1499
bool empty() const
Definition: config.cpp:1105
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")
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
config & add_child(const std::string &key)
Definition: config.cpp:743
std::string path
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
GLuint res
Definition: glew.h:9258
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1222
const_attr_itors attribute_range() const
Definition: config.cpp:984
Declarations for File-IO.
#define LOG_CS
Definition: addon_utils.cpp:25
std::string replace(std::string str, const std::string &src, const std::string &dst)
Replace all instances of src in str with dst.
config & find_child(const std::string &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:1010
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:93
const std::string illegal_markup_chars
Markup characters recognized by GUI1 code.
Definition: addon_utils.cpp:59
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
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:61
GLsizei const GLcharARB ** string
Definition: glew.h:4503