1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
8 /** @file fios.h Declarations for savegames operations */
9 
10 #ifndef FIOS_H
11 #define FIOS_H
12 
13 #include "gfx_type.h"
14 #include "company_base.h"
15 #include "newgrf_config.h"
16 #include "network/core/tcp_content_type.h"
17 
18 
19 /** Special values for save-load window for the data parameter of #InvalidateWindowData. */
20 enum SaveLoadInvalidateWindowData {
21 	SLIWD_RESCAN_FILES,          ///< Rescan all files (when changed directory, ...)
22 	SLIWD_SELECTION_CHANGES,     ///< File selection has changed (user click, ...)
23 	SLIWD_FILTER_CHANGES,        ///< The filename filter has changed (via the editbox)
24 };
25 
26 typedef SmallMap<uint, CompanyProperties *> CompanyPropertiesMap;
27 
28 /**
29  * Container for loading in mode SL_LOAD_CHECK.
30  */
31 struct LoadCheckData {
32 	bool checkable;     ///< True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable.)
33 	StringID error;     ///< Error message from loading. INVALID_STRING_ID if no error.
34 	char *error_data;   ///< Data to pass to SetDParamStr when displaying #error.
35 
36 	uint32 map_size_x, map_size_y;
37 	Date current_date;
38 
39 	GameSettings settings;
40 
41 	CompanyPropertiesMap companies;               ///< Company information.
42 
43 	GRFConfig *grfconfig;                         ///< NewGrf configuration from save.
44 	GRFListCompatibility grf_compatibility;       ///< Summary state of NewGrfs, whether missing files or only compatible found.
45 
46 	struct LoggedAction *gamelog_action;          ///< Gamelog actions
47 	uint gamelog_actions;                         ///< Number of gamelog actions
48 
LoadCheckDataLoadCheckData49 	LoadCheckData() : error_data(nullptr), grfconfig(nullptr),
50 			grf_compatibility(GLC_NOT_FOUND), gamelog_action(nullptr), gamelog_actions(0)
51 	{
52 		this->Clear();
53 	}
54 
55 	/**
56 	 * Don't leak memory at program exit
57 	 */
~LoadCheckDataLoadCheckData58 	~LoadCheckData()
59 	{
60 		this->Clear();
61 	}
62 
63 	/**
64 	 * Check whether loading the game resulted in errors.
65 	 * @return true if errors were encountered.
66 	 */
HasErrorsLoadCheckData67 	bool HasErrors()
68 	{
69 		return this->checkable && this->error != INVALID_STRING_ID;
70 	}
71 
72 	/**
73 	 * Check whether the game uses any NewGrfs.
74 	 * @return true if NewGrfs are used.
75 	 */
HasNewGrfsLoadCheckData76 	bool HasNewGrfs()
77 	{
78 		return this->checkable && this->error == INVALID_STRING_ID && this->grfconfig != nullptr;
79 	}
80 
81 	void Clear();
82 };
83 
84 extern LoadCheckData _load_check_data;
85 
86 /** Deals with finding savegames */
87 struct FiosItem {
88 	FiosType type;
89 	uint64 mtime;
90 	char title[64];
91 	char name[MAX_PATH];
92 	bool operator< (const FiosItem &other) const;
93 };
94 
95 /** List of file information. */
96 class FileList : public std::vector<FiosItem> {
97 public:
98 	void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop);
99 	const FiosItem *FindItem(const char *file);
100 };
101 
102 enum SortingBits {
103 	SORT_ASCENDING  = 0,
104 	SORT_DESCENDING = 1,
105 	SORT_BY_DATE    = 0,
106 	SORT_BY_NAME    = 2
107 };
108 DECLARE_ENUM_AS_BIT_SET(SortingBits)
109 
110 /* Variables to display file lists */
111 extern SortingBits _savegame_sort_order;
112 
113 void ShowSaveLoadDialog(AbstractFileType abstract_filetype, SaveLoadOperation fop);
114 
115 void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list);
116 void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list);
117 void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list);
118 
119 const char *FiosBrowseTo(const FiosItem *item);
120 
121 StringID FiosGetDescText(const char **path, uint64 *total_free);
122 bool FiosDelete(const char *name);
123 std::string FiosMakeHeightmapName(const char *name);
124 std::string FiosMakeSavegameName(const char *name);
125 
126 FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last);
127 
128 /**
129  * A savegame name automatically numbered.
130  */
131 struct FiosNumberedSaveName {
132 	FiosNumberedSaveName(const std::string &prefix);
133 	std::string Filename();
134 	std::string Extension();
135 private:
136 	std::string prefix;
137 	int number;
138 };
139 
140 #endif /* FIOS_H */
141