add preprocess_string method and use preprocessed loyal trait

This commit is contained in:
Subhraman Sarkar 2024-11-19 17:46:14 +05:30 committed by Gunter Labes
parent 26fd5513b6
commit a50f047c7c
3 changed files with 50 additions and 8 deletions

View file

@ -732,7 +732,7 @@ config map_context::to_config()
// [unit]s
config traits;
preproc_map traits_map;
read(traits, *(preprocess_file(game_config::path+"/data/core/macros/traits.cfg", &traits_map)));
read(traits, *(preprocess_file(game_config::path + "/data/core/macros/traits.cfg", &traits_map)));
for(const auto& unit : units_) {
config& u = event.add_child("unit");
@ -756,10 +756,11 @@ config map_context::to_config()
u["unrenamable"] = unit.unrenamable();
}
config& mods = u.add_child("modifications");
if(unit.loyal()) {
config trait_loyal;
read(trait_loyal, traits_map["TRAIT_LOYAL"].value);
u.append(trait_loyal);
read(trait_loyal, preprocess_string("{TRAIT_LOYAL}", &traits_map, "wesnoth-editor"));
mods.append(trait_loyal);
}
//TODO this entire block could also be replaced by unit.write(u, true)
//however, the resultant config is massive and contains many attributes we don't need.

View file

@ -1747,6 +1747,31 @@ filesystem::scoped_istream preprocess_file(const std::string& fname, preproc_map
return filesystem::scoped_istream(new preprocessor_scope_helper(fname, defines));
}
std::string preprocess_string(const std::string& contents, preproc_map* defines, const std::string& textdomain)
{
log_scope("preprocessing string " + contents.substr(0, 10) + " ...");
std::unique_ptr<preprocessor_streambuf> buf;
std::unique_ptr<preproc_map> local_defines;
//
// If no defines were provided, we create a new local preproc_map and assign
// it to defines temporarily. In this case, the map will be deleted once this
// object is destroyed and defines will still be subsequently null.
//
if(!defines) {
local_defines.reset(new preproc_map);
defines = local_defines.get();
}
buf.reset(new preprocessor_streambuf(defines));
// Begin processing.
buf->add_preprocessor<preprocessor_data>(
std::unique_ptr<std::istream>(new std::istringstream(contents)), "<string>", "", 1, game_config::path, textdomain, nullptr);
return formatter() << buf.get();
}
void preprocess_resource(const std::string& res_name,
preproc_map* defines_map,
bool write_cfg,

View file

@ -143,8 +143,24 @@ std::ostream& operator<<(std::ostream& stream, const preproc_map::value_type& de
*/
filesystem::scoped_istream preprocess_file(const std::string& fname, preproc_map* defines = nullptr);
void preprocess_resource(const std::string& res_name,
preproc_map* defines_map,
bool write_cfg = false,
bool write_plain_cfg = false,
const std::string& target_directory = "");
/**
* Function to use the WML preprocessor on a string.
*
* @param defines A map of symbols defined.
* @param contents The string to be preprocessed.
* @param textdomain The textdomain to associate the contents.
* Default: wesnoth
*
* @returns The resulting preprocessed string.
*/
std::string preprocess_string(
const std::string& contents,
preproc_map* defines,
const std::string& textdomain = "wesnoth");
void preprocess_resource(
const std::string& res_name,
preproc_map* defines_map,
bool write_cfg = false,
bool write_plain_cfg = false,
const std::string& target_directory = "");