Sky API: Rename *_tint to fog_*_tint for consistency
[oweals/minetest.git] / src / client / clientmedia.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "irrlichttypes.h"
23 #include "filecache.h"
24 #include <ostream>
25 #include <map>
26 #include <set>
27 #include <vector>
28 #include <unordered_map>
29
30 class Client;
31 struct HTTPFetchResult;
32
33 #define MTHASHSET_FILE_SIGNATURE 0x4d544853 // 'MTHS'
34 #define MTHASHSET_FILE_NAME "index.mth"
35
36 class ClientMediaDownloader
37 {
38 public:
39         ClientMediaDownloader();
40         ~ClientMediaDownloader();
41
42         float getProgress() const {
43                 if (m_uncached_count >= 1)
44                         return 1.0f * m_uncached_received_count /
45                                 m_uncached_count;
46
47                 return 0.0f;
48         }
49
50         bool isStarted() const {
51                 return m_initial_step_done;
52         }
53
54         // If this returns true, the downloader is done and can be deleted
55         bool isDone() const {
56                 return m_initial_step_done &&
57                         m_uncached_received_count == m_uncached_count;
58         }
59
60         // Add a file to the list of required file (but don't fetch it yet)
61         void addFile(const std::string &name, const std::string &sha1);
62
63         // Add a remote server to the list; ignored if not built with cURL
64         void addRemoteServer(const std::string &baseurl);
65
66         // Steps the media downloader:
67         // - May load media into client by calling client->loadMedia()
68         // - May check media cache for files
69         // - May add files to media cache
70         // - May start remote transfers by calling httpfetch_async
71         // - May check for completion of current remote transfers
72         // - May start conventional transfers by calling client->request_media()
73         // - May inform server that all media has been loaded
74         //   by calling client->received_media()
75         // After step has been called once, don't call addFile/addRemoteServer.
76         void step(Client *client);
77
78         // Must be called for each file received through TOCLIENT_MEDIA
79         void conventionalTransferDone(
80                         const std::string &name,
81                         const std::string &data,
82                         Client *client);
83
84 private:
85         struct FileStatus {
86                 bool received;
87                 std::string sha1;
88                 s32 current_remote;
89                 std::vector<s32> available_remotes;
90         };
91
92         struct RemoteServerStatus {
93                 std::string baseurl;
94                 s32 active_count;
95         };
96
97         void initialStep(Client *client);
98         void remoteHashSetReceived(const HTTPFetchResult &fetch_result);
99         void remoteMediaReceived(const HTTPFetchResult &fetch_result,
100                         Client *client);
101         s32 selectRemoteServer(FileStatus *filestatus);
102         void startRemoteMediaTransfers();
103         void startConventionalTransfers(Client *client);
104
105         bool checkAndLoad(const std::string &name, const std::string &sha1,
106                         const std::string &data, bool is_from_cache,
107                         Client *client);
108
109         std::string serializeRequiredHashSet();
110         static void deSerializeHashSet(const std::string &data,
111                         std::set<std::string> &result);
112
113         // Maps filename to file status
114         std::map<std::string, FileStatus*> m_files;
115
116         // Array of remote media servers
117         std::vector<RemoteServerStatus*> m_remotes;
118
119         // Filesystem-based media cache
120         FileCache m_media_cache;
121
122         // Has an attempt been made to load media files from the file cache?
123         // Have hash sets been requested from remote servers?
124         bool m_initial_step_done = false;
125
126         // Total number of media files to load
127         s32 m_uncached_count = 0;
128
129         // Number of media files that have been received
130         s32 m_uncached_received_count = 0;
131
132         // Status of remote transfers
133         unsigned long m_httpfetch_caller;
134         unsigned long m_httpfetch_next_id = 0;
135         long m_httpfetch_timeout = 0;
136         s32 m_httpfetch_active = 0;
137         s32 m_httpfetch_active_limit = 0;
138         s32 m_outstanding_hash_sets = 0;
139         std::unordered_map<unsigned long, std::string> m_remote_file_transfers;
140
141         // All files up to this name have either been received from a
142         // remote server or failed on all remote servers, so those files
143         // don't need to be looked at again
144         // (use m_files.upper_bound(m_name_bound) to get an iterator)
145         std::string m_name_bound = "";
146
147 };