16 #define GETTEXT_DOMAIN "wesnoth-lib"
28 #include <boost/algorithm/string/replace.hpp>
29 #include <boost/iostreams/filter/gzip.hpp>
32 #define ERR_CACHE LOG_STREAM(err, log_cache)
33 #define LOG_CACHE LOG_STREAM(info, log_cache)
34 #define DBG_CACHE LOG_STREAM(debug, log_cache)
45 target.try_emplace(
"APPLE");
49 target.try_emplace(
"ANDROID");
52 #if defined(MOUSE_TOUCH_EMULATION) || defined(TARGET_OS_IPHONE)
53 target.try_emplace(
"IPHONEOS");
68 : force_valid_cache_(false)
70 , fake_invalid_cache_(false)
109 if(defines.empty()) {
120 for(
const preproc_map::value_type& define : defines) {
121 define.second.write(writer, define.first);
135 add_builtin_defines(res);
148 return { std::move(
cfg), std::move(copy_map) };
158 static const std::string extension =
".gz";
160 std::stringstream defines_string;
161 defines_string << file_path;
163 bool is_valid =
true;
169 if((!define.value.empty() || !define.arguments.empty()) && key !=
"WESNOTH_VERSION") {
171 ERR_CACHE <<
"Invalid preprocessor define: " << key;
175 defines_string <<
" " << key;
182 if(is_valid && !cache_path.empty()) {
184 const std::string fname = cache_path +
"/" +
187 const std::string fname_checksum = fname +
".checksum" + extension;
194 DBG_CACHE <<
"Reading checksum: " << fname_checksum;
198 ERR_CACHE <<
"cache checksum is corrupt";
200 ERR_CACHE <<
"error reading cache checksum";
201 }
catch(
const std::ios_base::failure&) {
202 ERR_CACHE <<
"error reading cache checksum";
207 LOG_CACHE <<
"skipping cache validation (forced)";
211 LOG_CACHE <<
"found valid cache at '" << fname << extension <<
"' with defines_map " << defines_string.str();
216 const std::string define_file = fname +
".define" + extension;
224 ERR_CACHE <<
"cache " << fname << extension <<
" is corrupt. Loading from files: "<<
e.message;
226 ERR_CACHE <<
"error reading cache " << fname << extension <<
". Loading from files";
227 }
catch (
const boost::iostreams::gzip_error&
e) {
229 ERR_CACHE <<
"cache " << fname << extension <<
" is corrupt. Error code: " <<
e.error();
233 LOG_CACHE <<
"no valid cache found. Writing cache to '" << fname << extension <<
" with defines_map "<< defines_string.str() <<
"'";
243 write_file(fname +
".define" + extension, defines);
250 ERR_CACHE <<
"could not write to cache '" << fname <<
"'";
256 LOG_CACHE <<
"Loading plain config instead of cache";
265 DBG_CACHE <<
"Reading cached defines from: " << file_path;
316 DBG_CACHE <<
"adding define: " << define;
327 DBG_CACHE <<
"removing define: " << define;
338 std::vector<std::string> files, dirs;
341 LOG_CACHE <<
"clean_cache(): " << files.size() <<
" files, "
342 << dirs.size() <<
" dirs to check";
358 std::vector<std::string> files, dirs;
361 LOG_CACHE <<
"purge_cache(): deleting " << files.size() <<
" files, "
362 << dirs.size() <<
" dirs";
374 const std::string& exclude_pattern)
376 const bool delete_everything = exclude_pattern.empty();
379 for(
const std::string& file_path : paths)
381 if(!delete_everything) {
385 LOG_CACHE <<
"delete_cache_files(): skipping " << file_path
386 <<
" excluded by '" << exclude_pattern <<
"'";
391 LOG_CACHE <<
"delete_cache_files(): deleting " << file_path;
393 ERR_CACHE <<
"delete_cache_files(): could not delete "
403 : define_filenames_()
435 active_map_.insert(defines_map.begin(), defines_map.end());
449 std::set_difference(new_map.begin(),
453 std::insert_iterator(temp, temp.begin()));
Used in parsing config file.
Class for writing a config out to a file in pieces.
void write(const config &cfg)
A config object defines a single node in a WML file, with access to child nodes.
auto all_children_view() const
In-order iteration over all children.
~config_cache_transaction()
config_cache_transaction()
void add_defines_map_diff(preproc_map &defines_map)
std::vector< std::string > define_filenames_
static config_cache_transaction & instance()
const std::vector< std::string > & get_define_files() const
void lock()
Lock the transaction so no more macros are added.
preproc_map & get_active_map(const preproc_map &defines_map)
static config_cache_transaction * active_
void add_define_file(const std::string &file)
Singleton class to manage game config file caching.
std::string cache_file_prefix_
void add_define(const std::string &define)
Add a entry to preproc defines map.
void recheck_filetree_checksum()
Force cache checksum validation.
config read_cache(const std::string &path, abstract_validator *validator=nullptr)
void write_file(const std::string &file, const config &cfg)
bool purge_cache()
Deletes all cache files.
void set_use_cache(bool use)
Enable/disable caching.
void read_defines_queue()
void set_force_invalid_cache(bool)
config load_configs(const std::string &path, abstract_validator *validator=nullptr)
static config_cache & instance()
Get reference to the singleton object.
preproc_map & make_copy_map()
const preproc_map & get_preproc_map() const
config read_file(const std::string &file)
void remove_define(const std::string &define)
Remove a entry to preproc defines map.
void clear_defines()
Clear stored defines map to default values.
bool clean_cache()
Deletes stale cache files not in use by the game.
config get_config(const std::string &path, abstract_validator *validator=nullptr)
Gets a config object from given path.
bool delete_cache_files(const std::vector< std::string > &paths, const std::string &exclude_pattern="")
std::pair< config, preproc_map > read_configs(const std::string &path, abstract_validator *validator)
void read_defines_file(const std::string &path)
void add_defines_map_diff(preproc_map &)
void set_force_valid_cache(bool force)
Enable/disable cache validation.
Holds a fake cache transaction if no real one is used.
virtual std::string hex_digest() const override
void swap(config &lhs, config &rhs)
Implement non-member swap function for std::swap (calls config::swap).
static lg::log_domain log_cache("cache")
Declarations for File-IO.
Interfaces for manipulating version numbers of engine, add-ons, etc.
Standard logging facilities (interface).
#define log_scope(description)
std::string get_cache_dir()
filesystem::scoped_istream istream_file(const std::string &fname, bool treat_failure_as_error)
void get_files_in_dir(const std::string &dir, std::vector< std::string > *files, std::vector< std::string > *dirs, name_mode mode, filter_mode filter, reorder_mode reorder, file_tree_checksum *checksum)
Get a list of all files and/or directories in a given directory.
std::string base_name(const std::string &file, const bool remove_extension)
Returns the base filename of a file, with directory name stripped.
static bool file_exists(const bfs::path &fpath)
bool delete_directory(const std::string &dirname, const bool keep_pbl)
const file_tree_checksum & data_tree_checksum(bool reset=false)
Get the time at which the data/ tree was last modified at.
filesystem::scoped_ostream ostream_file(const std::string &fname, std::ios_base::openmode mode, bool create_directory)
std::unique_ptr< std::ostream > scoped_ostream
Game configuration data as global variables.
const version_info wesnoth_version(VERSION)
int cache_compression_level
const std::string revision
config read(std::istream &in, abstract_validator *validator)
config read_gz(std::istream &file, abstract_validator *validator)
Might throw a std::ios_base::failure especially a gzip_error.
bool wildcard_string_match(std::string_view str, std::string_view pat) noexcept
Performs pattern matching with wildcards.
fake
For describing the type of faked display, if any.
filesystem::scoped_istream preprocess_file(const std::string &fname, preproc_map &defines)
Function to use the WML preprocessor on a file.
std::map< std::string, struct preproc_define > preproc_map
void write(config &cfg) const
An exception object used when an IO error occurs.
static void insert(preproc_map &, const config &)