]> git.lizzy.rs Git - minetest.git/blobdiff - src/clientmedia.cpp
Simplify player modification checks
[minetest.git] / src / clientmedia.cpp
index 8260d5f5a5c2f88eda09803caf7768f529de0db1..434eeb2488369c0ad6a11ae9bbf7c78533802289 100644 (file)
@@ -18,10 +18,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "clientmedia.h"
+#include "util/serialize.h"
+#include "util/string.h"
 #include "httpfetch.h"
 #include "client.h"
 #include "clientserver.h"
 #include "filecache.h"
+#include "filesys.h"
 #include "hex.h"
 #include "sha1.h"
 #include "debug.h"
@@ -29,8 +32,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "porting.h"
 #include "settings.h"
 #include "main.h"
-#include "util/serialize.h"
-#include "util/string.h"
 
 static std::string getMediaCacheDir()
 {
@@ -110,14 +111,16 @@ void ClientMediaDownloader::addRemoteServer(std::string baseurl)
 
        #ifdef USE_CURL
 
-       infostream << "Client: Adding remote server \""
-               << baseurl << "\" for media download" << std::endl;
+       if (g_settings->getBool("enable_remote_media_server")) {
+               infostream << "Client: Adding remote server \""
+                       << baseurl << "\" for media download" << std::endl;
 
-       RemoteServerStatus *remote = new RemoteServerStatus;
-       remote->baseurl = baseurl;
-       remote->active_count = 0;
-       remote->request_by_filename = false;
-       m_remotes.push_back(remote);
+               RemoteServerStatus *remote = new RemoteServerStatus;
+               remote->baseurl = baseurl;
+               remote->active_count = 0;
+               remote->request_by_filename = false;
+               m_remotes.push_back(remote);
+       }
 
        #else
 
@@ -138,17 +141,17 @@ void ClientMediaDownloader::step(Client *client)
        // Remote media: check for completion of fetches
        if (m_httpfetch_active) {
                bool fetched_something = false;
-               HTTPFetchResult fetchresult;
+               HTTPFetchResult fetch_result;
 
-               while (httpfetch_async_get(m_httpfetch_caller, fetchresult)) {
+               while (httpfetch_async_get(m_httpfetch_caller, fetch_result)) {
                        m_httpfetch_active--;
                        fetched_something = true;
 
                        // Is this a hashset (index.mth) or a media file?
-                       if (fetchresult.request_id < m_remotes.size())
-                               remoteHashSetReceived(fetchresult);
+                       if (fetch_result.request_id < m_remotes.size())
+                               remoteHashSetReceived(fetch_result);
                        else
-                               remoteMediaReceived(fetchresult, client);
+                               remoteMediaReceived(fetch_result, client);
                }
 
                if (fetched_something)
@@ -257,17 +260,17 @@ void ClientMediaDownloader::initialStep(Client *client)
                        actionstream << "Client: Contacting remote server \""
                                << remote->baseurl << "\"" << std::endl;
 
-                       HTTPFetchRequest fetchrequest;
-                       fetchrequest.url =
+                       HTTPFetchRequest fetch_request;
+                       fetch_request.url =
                                remote->baseurl + MTHASHSET_FILE_NAME;
-                       fetchrequest.caller = m_httpfetch_caller;
-                       fetchrequest.request_id = m_httpfetch_next_id; // == i
-                       fetchrequest.timeout = m_httpfetch_timeout;
-                       fetchrequest.connect_timeout = m_httpfetch_timeout;
-                       fetchrequest.post_fields = required_hash_set;
-                       fetchrequest.extra_headers.push_back(
+                       fetch_request.caller = m_httpfetch_caller;
+                       fetch_request.request_id = m_httpfetch_next_id; // == i
+                       fetch_request.timeout = m_httpfetch_timeout;
+                       fetch_request.connect_timeout = m_httpfetch_timeout;
+                       fetch_request.post_data = required_hash_set;
+                       fetch_request.extra_headers.push_back(
                                "Content-Type: application/octet-stream");
-                       httpfetch_async(fetchrequest);
+                       httpfetch_async(fetch_request);
 
                        m_httpfetch_active++;
                        m_httpfetch_next_id++;
@@ -277,21 +280,21 @@ void ClientMediaDownloader::initialStep(Client *client)
 }
 
 void ClientMediaDownloader::remoteHashSetReceived(
-               const HTTPFetchResult &fetchresult)
+               const HTTPFetchResult &fetch_result)
 {
-       u32 remote_id = fetchresult.request_id;
+       u32 remote_id = fetch_result.request_id;
        assert(remote_id < m_remotes.size());
        RemoteServerStatus *remote = m_remotes[remote_id];
 
        m_outstanding_hash_sets--;
 
-       if (fetchresult.succeeded) {
+       if (fetch_result.succeeded) {
                try {
                        // Server sent a list of file hashes that are
                        // available on it, try to parse the list
 
                        std::set<std::string> sha1_set;
-                       deSerializeHashSet(fetchresult.data, sha1_set);
+                       deSerializeHashSet(fetch_result.data, sha1_set);
 
                        // Parsing succeeded: For every file that is
                        // available on this server, add this server
@@ -315,8 +318,10 @@ void ClientMediaDownloader::remoteHashSetReceived(
        // For compatibility: If index.mth is not found, assume that the
        // server contains files named like the original files (not their sha1)
 
-       if (!fetchresult.succeeded && !fetchresult.timeout &&
-                       fetchresult.response_code == 404) {
+       // Do NOT check for any particular response code (e.g. 404) here,
+       // because different servers respond differently
+
+       if (!fetch_result.succeeded && !fetch_result.timeout) {
                infostream << "Client: Enabling compatibility mode for remote "
                        << "server \"" << remote->baseurl << "\"" << std::endl;
                remote->request_by_filename = true;
@@ -334,7 +339,7 @@ void ClientMediaDownloader::remoteHashSetReceived(
 }
 
 void ClientMediaDownloader::remoteMediaReceived(
-               const HTTPFetchResult &fetchresult,
+               const HTTPFetchResult &fetch_result,
                Client *client)
 {
        // Some remote server sent us a file.
@@ -345,7 +350,7 @@ void ClientMediaDownloader::remoteMediaReceived(
        std::string name;
        {
                std::map<unsigned long, std::string>::iterator it =
-                       m_remote_file_transfers.find(fetchresult.request_id);
+                       m_remote_file_transfers.find(fetch_result.request_id);
                assert(it != m_remote_file_transfers.end());
                name = it->second;
                m_remote_file_transfers.erase(it);
@@ -364,9 +369,9 @@ void ClientMediaDownloader::remoteMediaReceived(
 
        // If fetch succeeded, try to load media file
 
-       if (fetchresult.succeeded) {
+       if (fetch_result.succeeded) {
                bool success = checkAndLoad(name, filestatus->sha1,
-                               fetchresult.data, false, client);
+                               fetch_result.data, false, client);
                if (success) {
                        filestatus->received = true;
                        assert(m_uncached_received_count < m_uncached_count);
@@ -441,14 +446,14 @@ void ClientMediaDownloader::startRemoteMediaTransfers()
                                        << "\"" << name << "\" "
                                        << "\"" << url << "\"" << std::endl;
 
-                               HTTPFetchRequest fetchrequest;
-                               fetchrequest.url = url;
-                               fetchrequest.caller = m_httpfetch_caller;
-                               fetchrequest.request_id = m_httpfetch_next_id;
-                               fetchrequest.timeout = 0; // no data timeout!
-                               fetchrequest.connect_timeout =
+                               HTTPFetchRequest fetch_request;
+                               fetch_request.url = url;
+                               fetch_request.caller = m_httpfetch_caller;
+                               fetch_request.request_id = m_httpfetch_next_id;
+                               fetch_request.timeout = 0; // no data timeout!
+                               fetch_request.connect_timeout =
                                        m_httpfetch_timeout;
-                               httpfetch_async(fetchrequest);
+                               httpfetch_async(fetch_request);
 
                                m_remote_file_transfers.insert(std::make_pair(
                                                        m_httpfetch_next_id,
@@ -480,13 +485,7 @@ void ClientMediaDownloader::startConventionalTransfers(Client *client)
 {
        assert(m_httpfetch_active == 0);
 
-       if (m_uncached_received_count == m_uncached_count) {
-               // In this case all media was found in the cache or
-               // has been downloaded from some remote server;
-               // report this fact to the server
-               client->received_media();
-       }
-       else {
+       if (m_uncached_received_count != m_uncached_count) {
                // Some media files have not been received yet, use the
                // conventional slow method (minetest protocol) to get them
                std::list<std::string> file_requests;