The Battle for Wesnoth  1.19.10+dev
save_index.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
3  by Jörg Hinrichs, David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "config.hpp"
19 
20 #include <chrono>
21 
22 namespace savegame
23 {
24 class save_index_class;
25 
26 /** Filename and modification date for a file list */
27 class save_info
28 {
29 private:
30  friend class create_save_info;
31 
32  save_info(const std::string& name,
33  const std::shared_ptr<save_index_class>& index,
34  const std::chrono::system_clock::time_point& modified)
35  : name_(name)
38  {
39  }
40 
41 public:
42  const std::string& name() const
43  {
44  return name_;
45  }
46 
47  const auto& modified() const
48  {
49  return modified_;
50  }
51 
52  std::string format_time_summary() const;
53  std::string format_time_local() const;
54  const config& summary() const;
55 
56 private:
57  std::string name_;
58  std::shared_ptr<save_index_class> save_index_;
59  std::chrono::system_clock::time_point modified_;
60 };
61 
62 /**
63  * A structure for comparing to save_info objects based on their modified time.
64  * If the times are equal, will order based on the name.
65  */
67 {
68  bool operator()(const save_info& a, const save_info& b) const;
69 };
70 
71 /** Read the complete config information out of a savefile. */
72 void read_save_file(const std::string& dir, const std::string& name, config& cfg, std::string* error_log);
73 
75 {
76 public:
77  explicit create_save_info(const std::shared_ptr<save_index_class>&);
78  save_info operator()(const std::string& filename) const;
79  std::shared_ptr<save_index_class> manager_;
80 };
81 
82 class save_index_class : public std::enable_shared_from_this<save_index_class>
83 {
84 public:
85  /**
86  * Constructor for a read-only instance. To get a writable instance, call default_saves_dir().
87  */
88  explicit save_index_class(const std::string& dir);
89  /** Syntatic sugar for choosing which constructor to use. */
92 
93  /** Returns an instance for managing saves in filesystem::get_saves_dir() */
94  static std::shared_ptr<save_index_class> default_saves_dir();
95 
96  std::vector<save_info> get_saves_list(const std::string* filter=nullptr);
97 
98  /** Delete a savegame, including deleting the underlying file. */
99  void delete_game(const std::string& name);
100 
101  void rebuild(const std::string& name);
102  void rebuild(const std::string& name, const std::chrono::system_clock::time_point& modified);
103 
104  /** Delete a savegame from the index, without deleting the underlying file. */
105  void remove(const std::string& name);
106  void set_modified(const std::string& name, const std::chrono::system_clock::time_point& modified);
107 
108  config& get(const std::string& name);
109  const std::string& dir() const;
110 
111  /** Delete autosaves that are no longer needed (according to the autosave policy in the preferences). */
112  void delete_old_auto_saves(const int autosavemax, const int infinite_auto_saves);
113 
114  /** Sync to disk, no-op if read_only_ is set */
115  void write_save_index();
116 
117  /**
118  * If true, all of delete_game, delete_old_auto_saves and write_save_index will be no-ops.
119  */
120  bool read_only()
121  {
122  return read_only_;
123  }
124 
125 private:
126  config& data(const std::string& name);
127  config& data();
128 
129  static void fix_leader_image_path(config& data);
130  /** Deletes non-existent save files from the index. */
131  void clean_up_index();
132 
133  bool loaded_;
135  std::map<std::string, std::chrono::system_clock::time_point> modified_;
136  const std::string dir_;
137  /**
138  * The instance for default_saves_dir() writes a cache file. For other instances,
139  * write_save_index() and delete() are no-ops.
140  */
142  /** Flag to only run the clean_up_index method once. */
144 };
145 } // end of namespace savegame
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
std::shared_ptr< save_index_class > manager_
Definition: save_index.hpp:79
save_info operator()(const std::string &filename) const
Definition: save_index.cpp:389
create_save_info(const std::shared_ptr< save_index_class > &)
Definition: save_index.cpp:384
void clean_up_index()
Deletes non-existent save files from the index.
Definition: save_index.cpp:100
create_for_default_saves_dir
Syntatic sugar for choosing which constructor to use.
Definition: save_index.hpp:90
static std::shared_ptr< save_index_class > default_saves_dir()
Returns an instance for managing saves in filesystem::get_saves_dir()
Definition: save_index.cpp:210
std::vector< save_info > get_saves_list(const std::string *filter=nullptr)
Get a list of available saves.
Definition: save_index.cpp:217
void remove(const std::string &name)
Delete a savegame from the index, without deleting the underlying file.
Definition: save_index.cpp:70
bool read_only_
The instance for default_saves_dir() writes a cache file.
Definition: save_index.hpp:141
void write_save_index()
Sync to disk, no-op if read_only_ is set.
Definition: save_index.cpp:116
const std::string & dir() const
Definition: save_index.cpp:95
config & get(const std::string &name)
Definition: save_index.cpp:82
void set_modified(const std::string &name, const std::chrono::system_clock::time_point &modified)
Definition: save_index.cpp:77
const std::string dir_
Definition: save_index.hpp:136
void delete_old_auto_saves(const int autosavemax, const int infinite_auto_saves)
Delete autosaves that are no longer needed (according to the autosave policy in the preferences).
Definition: save_index.cpp:348
bool read_only()
If true, all of delete_game, delete_old_auto_saves and write_save_index will be no-ops.
Definition: save_index.hpp:120
void delete_game(const std::string &name)
Delete a savegame, including deleting the underlying file.
Definition: save_index.cpp:372
void rebuild(const std::string &name)
Definition: save_index.cpp:44
std::map< std::string, std::chrono::system_clock::time_point > modified_
Definition: save_index.hpp:135
save_index_class(const std::string &dir)
Constructor for a read-only instance.
Definition: save_index.cpp:144
bool clean_up_index_
Flag to only run the clean_up_index method once.
Definition: save_index.hpp:143
static void fix_leader_image_path(config &data)
Definition: save_index.cpp:200
Filename and modification date for a file list.
Definition: save_index.hpp:28
save_info(const std::string &name, const std::shared_ptr< save_index_class > &index, const std::chrono::system_clock::time_point &modified)
Definition: save_index.hpp:32
const std::string & name() const
Definition: save_index.hpp:42
const config & summary() const
Definition: save_index.cpp:245
std::string format_time_local() const
Definition: save_index.cpp:250
std::string format_time_summary() const
Definition: save_index.cpp:263
std::string name_
Definition: save_index.hpp:57
std::shared_ptr< save_index_class > save_index_
Definition: save_index.hpp:58
std::chrono::system_clock::time_point modified_
Definition: save_index.hpp:59
const auto & modified() const
Definition: save_index.hpp:47
Definitions for the interface to Wesnoth Markup Language (WML).
void read_save_file(const std::string &dir, const std::string &name, config &cfg, std::string *error_log)
Read the complete config information out of a savefile.
Definition: save_index.cpp:308
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
constexpr auto filter
Definition: ranges.hpp:38
std::string filename
Filename.
A structure for comparing to save_info objects based on their modified time.
Definition: save_index.hpp:67
bool operator()(const save_info &a, const save_info &b) const
Definition: save_index.cpp:268
#define b