The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
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
22namespace savegame
23{
24class save_index_class;
25
26/** Filename and modification date for a file list */
28{
29private:
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)
36 , save_index_(index)
38 {
39 }
40
41public:
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
56private:
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. */
72config read_save_file(const std::string& dir, const std::string& name);
73
75{
76public:
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
82class save_index_class : public std::enable_shared_from_this<save_index_class>
83{
84public:
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 */
121 {
122 return read_only_;
123 }
124
125private:
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
134 std::map<std::string, std::chrono::system_clock::time_point> modified_;
135 const std::string dir_;
136 /**
137 * The instance for default_saves_dir() writes a cache file. For other instances,
138 * write_save_index() and delete() are no-ops.
139 */
141 /** Flag to only run the clean_up_index method once. */
143};
144} // end of namespace savegame
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
std::shared_ptr< save_index_class > manager_
save_info operator()(const std::string &filename) const
void clean_up_index()
Deletes non-existent save files from the index.
create_for_default_saves_dir
Syntatic sugar for choosing which constructor to use.
static std::shared_ptr< save_index_class > default_saves_dir()
Returns an instance for managing saves in filesystem::get_saves_dir()
std::vector< save_info > get_saves_list(const std::string *filter=nullptr)
Get a list of available saves.
void remove(const std::string &name)
Delete a savegame from the index, without deleting the underlying file.
bool read_only_
The instance for default_saves_dir() writes a cache file.
void write_save_index()
Sync to disk, no-op if read_only_ is set.
const std::string & dir() const
void set_modified(const std::string &name, const std::chrono::system_clock::time_point &modified)
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).
bool read_only()
If true, all of delete_game, delete_old_auto_saves and write_save_index will be no-ops.
void delete_game(const std::string &name)
Delete a savegame, including deleting the underlying file.
void rebuild(const std::string &name)
std::map< std::string, std::chrono::system_clock::time_point > modified_
bool clean_up_index_
Flag to only run the clean_up_index method once.
static void fix_leader_image_path(config &data)
Filename and modification date for a file list.
save_info(const std::string &name, const std::shared_ptr< save_index_class > &index, const std::chrono::system_clock::time_point &modified)
const config & summary() const
const auto & modified() const
std::string format_time_local() const
std::string format_time_summary() const
std::shared_ptr< save_index_class > save_index_
std::chrono::system_clock::time_point modified_
const std::string & name() const
Definitions for the interface to Wesnoth Markup Language (WML).
config read_save_file(const std::string &dir, const std::string &name)
Read the complete config information out of a savefile.
std::string filename
Filename.
A structure for comparing to save_info objects based on their modified time.
bool operator()(const save_info &a, const save_info &b) const
#define b