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)
48 #if defined(MOUSE_TOUCH_EMULATION) || defined(TARGET_OS_IPHONE)
64 : force_valid_cache_(false)
66 , fake_invalid_cache_(false)
68 , cache_file_prefix_(
"cache-v" + boost::algorithm::replace_all_copy(
game_config::
revision,
":",
"_") +
"-")
106 if(defines.empty()) {
117 for(
const preproc_map::value_type& define : defines) {
118 define.second.write(writer, define.first);
133 add_builtin_defines(res);
151 static const std::string extension =
".gz";
153 std::stringstream defines_string;
154 defines_string << file_path;
156 bool is_valid =
true;
162 if((!
d.second.value.empty() || !
d.second.arguments.empty()) &&
163 d.first !=
"WESNOTH_VERSION")
166 ERR_CACHE <<
"Invalid preprocessor define: " <<
d.first;
170 defines_string <<
" " <<
d.first;
177 if(is_valid && !cache_path.empty()) {
179 const std::string fname = cache_path +
"/" +
182 const std::string fname_checksum = fname +
".checksum" + extension;
191 DBG_CACHE <<
"Reading checksum: " << fname_checksum;
197 ERR_CACHE <<
"cache checksum is corrupt";
199 ERR_CACHE <<
"error reading cache checksum";
200 }
catch(
const std::ios_base::failure&) {
201 ERR_CACHE <<
"error reading cache checksum";
206 LOG_CACHE <<
"skipping cache validation (forced)";
210 LOG_CACHE <<
"found valid cache at '" << fname << extension <<
"' with defines_map " << defines_string.str();
215 const std::string define_file = fname +
".define" + extension;
223 ERR_CACHE <<
"cache " << fname << extension <<
" is corrupt. Loading from files: "<<
e.message;
225 ERR_CACHE <<
"error reading cache " << fname << extension <<
". Loading from files";
226 }
catch (
const boost::iostreams::gzip_error&
e) {
228 ERR_CACHE <<
"cache " << fname << extension <<
" is corrupt. Error code: " <<
e.error();
232 LOG_CACHE <<
"no valid cache found. Writing cache to '" << fname << extension <<
" with defines_map "<< defines_string.str() <<
"'";
244 write_file(fname +
".define" + extension, copy_map);
251 ERR_CACHE <<
"could not write to cache '" << fname <<
"'";
257 LOG_CACHE <<
"Loading plain config instead of cache";
269 DBG_CACHE <<
"Reading cached defines from: " << file_path;
282 for(
const std::string &
p : files) {
323 DBG_CACHE <<
"adding define: " << define;
335 DBG_CACHE <<
"removing define: " << define;
346 std::vector<std::string> files, dirs;
349 LOG_CACHE <<
"clean_cache(): " << files.size() <<
" files, "
350 << dirs.size() <<
" dirs to check";
366 std::vector<std::string> files, dirs;
369 LOG_CACHE <<
"purge_cache(): deleting " << files.size() <<
" files, "
370 << dirs.size() <<
" dirs";
382 const std::string& exclude_pattern)
384 const bool delete_everything = exclude_pattern.empty();
387 for(
const std::string& file_path : paths)
389 if(!delete_everything) {
393 LOG_CACHE <<
"delete_cache_files(): skipping " << file_path
394 <<
" excluded by '" << exclude_pattern <<
"'";
399 LOG_CACHE <<
"delete_cache_files(): deleting " << file_path;
401 ERR_CACHE <<
"delete_cache_files(): could not delete "
414 : define_filenames_()
446 active_map_.insert(defines_map.begin(), defines_map.end());
458 bool compare_define(
const preproc_map::value_type& a,
const preproc_map::value_type&
b)
460 if(a.first <
b.first) {
464 if(
b.first < a.first) {
468 if(a.second <
b.second) {
481 std::set_difference(new_map.begin(),
485 std::insert_iterator<preproc_map>(temp,temp.begin()),
488 for(
const preproc_map::value_type &def : temp) {
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.
Used to share macros between cache objects You have to create transaction object to load all macros t...
~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)
void insert_to_active(const preproc_map::value_type &def)
Used to let std::for_each insert new defines to active_map map to active.
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 load_configs(const std::string &path, config &cfg, abstract_validator *validator=nullptr)
void add_define(const std::string &define)
Add a entry to preproc defines map.
void recheck_filetree_checksum()
Force cache checksum validation.
void read_cache(const std::string &path, config &cfg, 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)
static config_cache & instance()
Get reference to the singleton object.
preproc_map & make_copy_map()
const preproc_map & get_preproc_map() const
void remove_define(const std::string &define)
Remove a entry to preproc defines map.
void clear_defines()
Clear stored defines map to default values.
void read_configs(const std::string &path, config &cfg, preproc_map &defines, abstract_validator *validator=nullptr)
bool clean_cache()
Deletes stale cache files not in use by the game.
void read_file(const std::string &file, config &cfg)
bool delete_cache_files(const std::vector< std::string > &paths, const std::string &exclude_pattern="")
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.
void get_config(const std::string &path, config &cfg, abstract_validator *validator=nullptr)
Gets a config object from given path.
Holds a fake cache transaction if no real one is used.
virtual std::string hex_digest() const override
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::istream > scoped_istream
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
bool wildcard_string_match(const std::string &str, const std::string &match)
Match using '*' as any number of characters (including none), '+' as one or more characters,...
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 read(config &cfg, std::istream &in, abstract_validator *validator)
void read_gz(config &cfg, std::istream &file, abstract_validator *validator)
Might throw a std::ios_base::failure especially a gzip_error.
void write(config &cfg) const
An exception object used when an IO error occurs.
static preproc_map::value_type read_pair(const config &)