Skip to content

Commit

Permalink
campaignd: Move and document more ancillary functions
Browse files Browse the repository at this point in the history
Since the move is between two files, I took this opportunity to rename
a "campaign" parameter to "addon" and tweak the code style for
consistency.

(Backported 42ee57c from master.)
  • Loading branch information
irydacea committed Oct 11, 2014
1 parent 8a65dfc commit 25846d0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 48 deletions.
52 changes: 51 additions & 1 deletion src/campaign_server/addon_utils.cpp
@@ -1,5 +1,6 @@
/*
Copyright (C) 2013 - 2014 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
Copyright (C) 2003 - 2014 by David White <dave@whitevine.net>
2013 - 2014 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
Expand All @@ -15,10 +16,16 @@
#include "campaign_server/addon_utils.hpp"

#include "config.hpp"
#include "filesystem.hpp"
#include "game_config.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"

#include <boost/foreach.hpp>

static lg::log_domain log_network("network");
#define LOG_CS if (lg::err.dont_log(log_network)) ; else lg::err(log_network, false)

namespace {

typedef std::map<std::string, std::string> plain_string_map;
Expand Down Expand Up @@ -85,4 +92,47 @@ std::string format_addon_feedback_url(const std::string& format, const config& p
return std::string();
}

void find_translations(const config& base_dir, config& addon)
{
BOOST_FOREACH(const config &dir, base_dir.child_range("dir"))
{
if(dir["name"] == "LC_MESSAGES") {
addon.add_child("translation")["language"] = base_dir["name"];
} else {
find_translations(dir, addon);
}
}
}

void add_license(config& cfg)
{
config& dir = cfg.find_child("dir", "name", cfg["campaign_name"]);

// No top-level directory? Hm..
if(!dir) {
return;
}

// Don't add if it already exists.
if(dir.find_child("file", "name", "COPYING.txt")
|| dir.find_child("file", "name", "COPYING")
|| dir.find_child("file", "name", "copying.txt")
|| dir.find_child("file", "name", "Copying.txt")
|| dir.find_child("file", "name", "COPYING.TXT"))
{
return;
}

// Copy over COPYING.txt
const std::string& contents = read_file("COPYING.txt");
if (contents.empty()) {
LOG_CS << "Could not find COPYING.txt, path is \"" << game_config::path << "\"\n";
return;
}

config& copying = dir.add_child("file");
copying["name"] = "COPYING.txt";
copying["contents"] = contents;
}

} // end namespace campaignd
32 changes: 31 additions & 1 deletion src/campaign_server/addon_utils.hpp
@@ -1,5 +1,6 @@
/*
Copyright (C) 2013 - 2014 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
Copyright (C) 2003 - 2014 by David White <dave@whitevine.net>
2013 - 2014 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -49,6 +50,35 @@ inline bool is_text_markup_char(char c)
*/
std::string format_addon_feedback_url(const std::string& format, const config& params);


/**
* Scans an add-on archive directory for translations.
*
* Any subdirectories of @a base_dir containing a subdirectory named
* 'LC_MESSAGES' are assumed to be translation dirs. The names of the
* subdirectories thus located are recorded into the @a addon WML node in
* [translation] children nodes like the following (comments included for
* documentation purposes):
*
* @verbatim
* [translation]
* language="es" # translations/es/LC_MESSAGES/
* [/translation]
* [translation]
* language="ja" # translations/ja/LC_MESSAGES/
* [/translation]
* @endverbatim
*/
void find_translations(const config& base_dir, config& addon);

/**
* Adds a COPYING.txt file with the full text of the GNU GPL to an add-on.
*
* This only has an effect if the add-on archive @a cfg does not already
* contain an equivalent file ('copying.txt', 'COPYING', etc.).
*/
void add_license(config& cfg);

}

#endif
46 changes: 0 additions & 46 deletions src/campaign_server/campaign_server.cpp
Expand Up @@ -230,52 +230,6 @@ void server::send_error(const std::string& msg, network::connection sock)
network::send_data(cfg, sock);
}

} // end namespace campaignd

namespace {
void find_translations(const config& cfg, config& campaign)
{
BOOST_FOREACH(const config &dir, cfg.child_range("dir"))
{
if (dir["name"] == "LC_MESSAGES") {
config &language = campaign.add_child("translation");
language["language"] = cfg["name"];
} else {
find_translations(dir, campaign);
}
}
}

// Add a file COPYING.txt with the GPL to an uploaded campaign.
void add_license(config &data)
{
config &dir = data.find_child("dir", "name", data["campaign_name"]);
// No top-level directory? Hm..
if (!dir) return;

// Don't add if it already exists.
if (dir.find_child("file", "name", "COPYING.txt")) return;
if (dir.find_child("file", "name", "COPYING")) return;
if (dir.find_child("file", "name", "copying.txt")) return;
if (dir.find_child("file", "name", "Copying.txt")) return;
if (dir.find_child("file", "name", "COPYING.TXT")) return;

// Copy over COPYING.txt
std::string contents = read_file("COPYING.txt");
if (contents.empty()) {
LOG_CS << "Could not find COPYING.txt, path is \""
<< game_config::path << "\"\n";
return;
}
config &copying = dir.add_child("file");
copying["name"] = "COPYING.txt";
copying["contents"] = contents;

}
} // end anonymous namespace 2

namespace campaignd {

void server::run()
{
if(read_only_) {
Expand Down

0 comments on commit 25846d0

Please sign in to comment.