Skip to content

Commit

Permalink
Merge pull request #296 from gfgtdf/AI-boost_filesystem
Browse files Browse the repository at this point in the history
This are the first ~10 commits of AI s boost filesystem branch and mostly a strict refactor. which i cherrypicked to my fork in order to test the boost fileystem. Because there commits are quite hamless but still cause the most merging conflicts i decided to pull them into master so we don't have to resolve these merging conflicts again if we merge boost fileystem into master. (There will be still be merging conflicts but much less). A lot of these commits differ quite much from their originals so i merged the original into this branch again by using "merge -s ours" to make git know that we already have these commits.
  • Loading branch information
gfgtdf committed Oct 14, 2014
2 parents 286bd28 + fbfad1f commit 3de9e1d
Show file tree
Hide file tree
Showing 73 changed files with 577 additions and 549 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Expand Up @@ -210,6 +210,7 @@ set(libwesnoth-core_STAT_SRC
color_range.cpp
config.cpp
filesystem.cpp
filesystem_common.cpp
game_config.cpp
gettext.cpp
hash.cpp
Expand Down Expand Up @@ -1214,6 +1215,7 @@ set(schema_generator_SRC
tools/schema/error_container.cpp
tools/schema/tag.cpp
filesystem.cpp
filesystem_common.cpp
loadscreen_empty.cpp
)

Expand All @@ -1235,6 +1237,7 @@ set(schema_validator_SRC
tools/dummy_video.cpp
tools/schema/tag.cpp
filesystem.cpp
filesystem_common.cpp
config_cache.cpp
utils/sha1.cpp
loadscreen_empty.cpp
Expand Down
3 changes: 2 additions & 1 deletion src/SConscript
Expand Up @@ -63,7 +63,8 @@ if env['default_prefs_file']:

libwesnoth_core_sources.extend([
game_config_env.Object("game_config.cpp"),
filesystem_env.Object("filesystem.cpp")
filesystem_env.Object("filesystem.cpp"),
filesystem_env.Object("filesystem_common.cpp"),
])

libwesnoth_core = [env.Library("wesnoth_core", libwesnoth_core_sources)]
Expand Down
78 changes: 39 additions & 39 deletions src/addon/manager.cpp
Expand Up @@ -58,41 +58,41 @@ static lg::log_domain log_network("network");
namespace {
std::string get_pbl_file_path(const std::string& addon_name)
{
const std::string& parentd = get_addon_campaigns_dir();
const std::string& parentd = filesystem::get_addons_dir();
// Cope with old-style or new-style file organization
const std::string exterior = parentd + "/" + addon_name + ".pbl";
const std::string interior = parentd + "/" + addon_name + "/_server.pbl";
return file_exists(exterior) ? exterior : interior;
return filesystem::file_exists(exterior) ? exterior : interior;
}

inline std::string get_info_file_path(const std::string& addon_name)
{
return get_addon_campaigns_dir() + "/" + addon_name + "/_info.cfg";
return filesystem::get_addons_dir() + "/" + addon_name + "/_info.cfg";
}
}

bool have_addon_in_vcs_tree(const std::string& addon_name)
{
static const std::string parentd = get_addon_campaigns_dir();
static const std::string parentd = filesystem::get_addons_dir();
return
file_exists(parentd+"/"+addon_name+"/.svn") ||
file_exists(parentd+"/"+addon_name+"/.git") ||
file_exists(parentd+"/"+addon_name+"/.hg");
filesystem::file_exists(parentd+"/"+addon_name+"/.svn") ||
filesystem::file_exists(parentd+"/"+addon_name+"/.git") ||
filesystem::file_exists(parentd+"/"+addon_name+"/.hg");
}

bool have_addon_pbl_info(const std::string& addon_name)
{
static const std::string parentd = get_addon_campaigns_dir();
static const std::string parentd = filesystem::get_addons_dir();
return
file_exists(parentd+"/"+addon_name+".pbl") ||
file_exists(parentd+"/"+addon_name+"/_server.pbl");
filesystem::file_exists(parentd+"/"+addon_name+".pbl") ||
filesystem::file_exists(parentd+"/"+addon_name+"/_server.pbl");
}

void get_addon_pbl_info(const std::string& addon_name, config& cfg)
{
const std::string& pbl_path = get_pbl_file_path(addon_name);
try {
scoped_istream stream = istream_file(pbl_path);
filesystem::scoped_istream stream = filesystem::istream_file(pbl_path);
read(cfg, *stream);
} catch(const config::error& e) {
throw invalid_pbl_exception(pbl_path, e.message);
Expand All @@ -101,19 +101,19 @@ void get_addon_pbl_info(const std::string& addon_name, config& cfg)

void set_addon_pbl_info(const std::string& addon_name, const config& cfg)
{
scoped_ostream stream = ostream_file(get_pbl_file_path(addon_name));
filesystem::scoped_ostream stream = filesystem::ostream_file(get_pbl_file_path(addon_name));
write(*stream, cfg);
}

bool have_addon_install_info(const std::string& addon_name)
{
return file_exists(get_info_file_path(addon_name));
return filesystem::file_exists(get_info_file_path(addon_name));
}

void get_addon_install_info(const std::string& addon_name, config& cfg)
{
const std::string& info_path = get_info_file_path(addon_name);
scoped_istream stream = istream_file(info_path);
filesystem::scoped_istream stream = filesystem::istream_file(info_path);
try {
read(cfg, *stream);
} catch(const config::error& e) {
Expand All @@ -126,16 +126,16 @@ void get_addon_install_info(const std::string& addon_name, config& cfg)
bool remove_local_addon(const std::string& addon)
{
bool ret = true;
const std::string addon_dir = get_addon_campaigns_dir() + "/" + addon;
const std::string addon_dir = filesystem::get_addons_dir() + "/" + addon;

LOG_CFG << "removing local add-on: " << addon << '\n';

if(file_exists(addon_dir) && !delete_directory(addon_dir, true)) {
if(filesystem::file_exists(addon_dir) && !filesystem::delete_directory(addon_dir, true)) {
ERR_CFG << "Failed to delete directory/file: " << addon_dir << '\n';
ret = false;
}

if(file_exists(addon_dir + ".cfg") && !delete_directory(addon_dir + ".cfg", true)) {
if(filesystem::file_exists(addon_dir + ".cfg") && !filesystem::delete_directory(addon_dir + ".cfg", true)) {
ERR_CFG << "Failed to delete directory/file: " << addon_dir << ".cfg" << std::endl;
ret = false;
}
Expand All @@ -151,16 +151,16 @@ std::vector<std::string> available_addons()
{
std::vector<std::string> res;
std::vector<std::string> files, dirs;
const std::string parentd = get_addon_campaigns_dir();
get_files_in_dir(parentd,&files,&dirs);
const std::string parentd = filesystem::get_addons_dir();
filesystem::get_files_in_dir(parentd,&files,&dirs);

for(std::vector<std::string>::const_iterator i = dirs.begin(); i != dirs.end(); ++i) {
const std::string external_cfg_file = *i + ".cfg";
const std::string internal_cfg_file = *i + "/_main.cfg";
const std::string external_pbl_file = *i + ".pbl";
const std::string internal_pbl_file = *i + "/_server.pbl";
if((std::find(files.begin(),files.end(),external_cfg_file) != files.end() || file_exists(parentd + "/" + internal_cfg_file)) &&
(std::find(files.begin(),files.end(),external_pbl_file) != files.end() || (file_exists(parentd + "/" + internal_pbl_file)))) {
if((std::find(files.begin(),files.end(),external_cfg_file) != files.end() || filesystem::file_exists(parentd + "/" + internal_cfg_file)) &&
(std::find(files.begin(),files.end(),external_pbl_file) != files.end() || (filesystem::file_exists(parentd + "/" + internal_pbl_file)))) {
res.push_back(*i);
}
}
Expand All @@ -181,14 +181,14 @@ std::vector<std::string> available_addons()
std::vector<std::string> installed_addons()
{
std::vector<std::string> res;
const std::string parentd = get_addon_campaigns_dir();
const std::string parentd = filesystem::get_addons_dir();
std::vector<std::string> files, dirs;
get_files_in_dir(parentd,&files,&dirs);
filesystem::get_files_in_dir(parentd,&files,&dirs);

for(std::vector<std::string>::const_iterator i = dirs.begin(); i != dirs.end(); ++i) {
const std::string external_cfg_file = *i + ".cfg";
const std::string internal_cfg_file = *i + "/_main.cfg";
if(std::find(files.begin(),files.end(),external_cfg_file) != files.end() || file_exists(parentd + "/" + internal_cfg_file)) {
if(std::find(files.begin(),files.end(),external_cfg_file) != files.end() || filesystem::file_exists(parentd + "/" + internal_cfg_file)) {
res.push_back(*i);
}
}
Expand All @@ -198,9 +198,9 @@ std::vector<std::string> installed_addons()

bool is_addon_installed(const std::string& addon_name)
{
const std::string namestem = get_addon_campaigns_dir() + "/" + addon_name;
const std::string namestem = filesystem::get_addons_dir() + "/" + addon_name;

return file_exists(namestem + ".cfg") || file_exists(namestem + "/_main.cfg");
return filesystem::file_exists(namestem + ".cfg") || filesystem::file_exists(namestem + "/_main.cfg");
}

static inline bool IsCR(const char& c)
Expand Down Expand Up @@ -256,16 +256,16 @@ namespace {

static std::pair<std::vector<std::string>, std::vector<std::string> > read_ignore_patterns(const std::string& addon_name)
{
const std::string parentd = get_addon_campaigns_dir();
const std::string parentd = filesystem::get_addons_dir();
const std::string exterior = parentd + "/" + addon_name + ".ign";
const std::string interior = parentd + "/" + addon_name + "/_server.ign";

std::pair<std::vector<std::string>, std::vector<std::string> > patterns;
std::string ign_file;
LOG_CFG << "searching for .ign file for '" << addon_name << "'...\n";
if (file_exists(interior)) {
if (filesystem::file_exists(interior)) {
ign_file = interior;
} else if (file_exists(exterior)) {
} else if (filesystem::file_exists(exterior)) {
ign_file = exterior;
} else {
LOG_CFG << "no .ign file found for '" << addon_name << "'\n"
Expand All @@ -274,7 +274,7 @@ static std::pair<std::vector<std::string>, std::vector<std::string> > read_ignor
return patterns; // just default patterns
}
LOG_CFG << "found .ign file: " << ign_file << '\n';
std::istream *stream = istream_file(ign_file);
std::istream *stream = filesystem::istream_file(ign_file);
std::string line;
while (std::getline(*stream, line)) {
utils::strip(line);
Expand All @@ -292,7 +292,7 @@ static void archive_file(const std::string& path, const std::string& fname, conf
{
cfg["name"] = fname;
const bool is_cfg = (fname.size() > 4 ? (fname.substr(fname.size() - 4) == ".cfg") : false);
cfg["contents"] = encode_binary(strip_cr(read_file(path + '/' + fname),is_cfg));
cfg["contents"] = encode_binary(strip_cr(filesystem::read_file(path + '/' + fname),is_cfg));
}

static void archive_dir(const std::string& path, const std::string& dirname, config& cfg, std::pair<std::vector<std::string>, std::vector<std::string> >& ignore_patterns)
Expand All @@ -301,9 +301,9 @@ static void archive_dir(const std::string& path, const std::string& dirname, con
const std::string dir = path + '/' + dirname;

std::vector<std::string> files, dirs;
get_files_in_dir(dir,&files,&dirs);
filesystem::get_files_in_dir(dir,&files,&dirs);
for(std::vector<std::string>::const_iterator i = files.begin(); i != files.end(); ++i) {
bool valid = !looks_like_pbl(*i);
bool valid = !filesystem::looks_like_pbl(*i);
for(std::vector<std::string>::const_iterator p = ignore_patterns.first.begin(); p != ignore_patterns.first.end(); ++p) {
if (utils::wildcard_string_match(*i, *p)) {
valid = false;
Expand Down Expand Up @@ -331,12 +331,12 @@ static void archive_dir(const std::string& path, const std::string& dirname, con

void archive_addon(const std::string& addon_name, config& cfg)
{
const std::string parentd = get_addon_campaigns_dir();
const std::string parentd = filesystem::get_addons_dir();

std::pair<std::vector<std::string>, std::vector<std::string> > ignore_patterns;
// External .cfg may not exist; newer campaigns have a _main.cfg
const std::string external_cfg = addon_name + ".cfg";
if (file_exists(parentd + "/" + external_cfg)) {
if (filesystem::file_exists(parentd + "/" + external_cfg)) {
archive_file(parentd, external_cfg, cfg.add_child("file"));
}
ignore_patterns = read_ignore_patterns(addon_name);
Expand All @@ -345,7 +345,7 @@ void archive_addon(const std::string& addon_name, config& cfg)

static void unarchive_file(const std::string& path, const config& cfg)
{
write_file(path + '/' + cfg["name"].str(), unencode_binary(cfg["contents"]));
filesystem::write_file(path + '/' + cfg["name"].str(), unencode_binary(cfg["contents"]));
}

static void unarchive_dir(const std::string& path, const config& cfg)
Expand All @@ -356,7 +356,7 @@ static void unarchive_dir(const std::string& path, const config& cfg)
else
dir = path + '/' + cfg["name"].str();

make_directory(dir);
filesystem::make_directory(dir);

BOOST_FOREACH(const config &d, cfg.child_range("dir")) {
unarchive_dir(dir, d);
Expand All @@ -369,7 +369,7 @@ static void unarchive_dir(const std::string& path, const config& cfg)

void unarchive_addon(const config& cfg)
{
const std::string parentd = get_addon_campaigns_dir();
const std::string parentd = filesystem::get_addons_dir();
unarchive_dir(parentd, cfg);
}

Expand Down Expand Up @@ -399,7 +399,7 @@ void refresh_addon_version_info_cache()
const std::string& addon = addons[i];
const std::string& info_file = addon_info_files[i];

if(file_exists(info_file)) {
if(filesystem::file_exists(info_file)) {
config cfg;
get_addon_install_info(addon, cfg);

Expand Down
4 changes: 2 additions & 2 deletions src/addon/manager_ui.cpp
Expand Up @@ -992,8 +992,8 @@ bool addons_manager_ui(display& disp, const std::string& remote_address)
} catch(const network_asio::error& e) {
ERR_NET << "network_asio::error thrown during transaction with add-on server; \""<< e.what() << "\"" << std::endl;
gui2::show_error_message(disp.video(), _("Remote host disconnected."));
} catch(const io_exception& e) {
ERR_FS << "io_exception thrown while installing an addon; \"" << e.what() << "\"" << std::endl;
} catch(const filesystem::io_exception& e) {
ERR_FS << "filesystem::io_exception thrown while installing an addon; \"" << e.what() << "\"" << std::endl;
gui2::show_error_message(disp.video(), _("A problem occurred when trying to create the files necessary to install this add-on."));
} catch(const invalid_pbl_exception& e) {
ERR_CFG << "could not read .pbl file " << e.path << ": " << e.message << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion src/ai/configuration.cpp
Expand Up @@ -204,7 +204,7 @@ config configuration::default_config_ = config();

bool configuration::get_side_config_from_file(const std::string& file, config& cfg ){
try {
scoped_istream stream = preprocess_file(get_wml_location(file));
filesystem::scoped_istream stream = preprocess_file(filesystem::get_wml_location(file));
read(cfg, *stream);
LOG_AI_CONFIGURATION << "Reading AI configuration from file '" << file << "'" << std::endl;
} catch(config::error &) {
Expand Down
4 changes: 2 additions & 2 deletions src/ai/formula/function_table.cpp
Expand Up @@ -500,13 +500,13 @@ class run_file_function : public function_expression {
const std::string filename = var0.string_cast();

//NOTE: get_wml_location also filters file path to ensure it doesn't contain things like "../../top/secret"
std::string path = get_wml_location(filename);
std::string path = filesystem::get_wml_location(filename);
if(path.empty()) {
ERR_AI << "run_file : not found [" << filename <<"]"<< std::endl;
return variant(); //no suitable file
}

std::string formula_string = read_file(path);
std::string formula_string = filesystem::read_file(path);
//need to get function_table from somewhere or delegate to someone who has access to it
formula_ptr parsed_formula = ai_.create_optional_formula(formula_string);
if(parsed_formula == game_logic::formula_ptr()) {
Expand Down
1 change: 0 additions & 1 deletion src/ai/lua/core.cpp
Expand Up @@ -31,7 +31,6 @@
#include "lua_object.hpp" // (Nephro)

#include "../../attack_prediction.hpp"
#include "../../filesystem.hpp"
#include "../../game_display.hpp"
#include "../../log.hpp"
#include "../../map.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/campaign_server/addon_utils.cpp
Expand Up @@ -124,7 +124,7 @@ void add_license(config& cfg)
}

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

0 comments on commit 3de9e1d

Please sign in to comment.