1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-20 06:12:57 +01:00

Merge pull request #5564 from IntelOrca/fix/download-park-temp-file

Fix #4959: Compiler warning: tmpnam
This commit is contained in:
Ted John
2017-06-07 21:55:05 +01:00
committed by GitHub
5 changed files with 37 additions and 37 deletions

View File

@@ -226,9 +226,8 @@ if (NOT DISABLE_TTF AND UNIX AND NOT APPLE)
target_include_directories(${PROJECT} PRIVATE ${FONTCONFIG_INCLUDE_DIRS}) target_include_directories(${PROJECT} PRIVATE ${FONTCONFIG_INCLUDE_DIRS})
endif () endif ()
# macOS builds fail on the use of tmpnam otherwise (#4959)
if(APPLE) if(APPLE)
set(COMMON_COMPILE_OPTIONS "${COMMON_COMPILE_OPTIONS} -Wno-error=deprecated-declarations -Wno-error=objc-method-access") set(COMMON_COMPILE_OPTIONS "${COMMON_COMPILE_OPTIONS} -Wno-error=objc-method-access")
endif() endif()
# Compiler flags # Compiler flags

View File

@@ -24,6 +24,7 @@
#include "core/File.h" #include "core/File.h"
#include "core/FileStream.hpp" #include "core/FileStream.hpp"
#include "core/Guard.hpp" #include "core/Guard.hpp"
#include "core/MemoryStream.h"
#include "core/String.hpp" #include "core/String.hpp"
#include "FileClassifier.h" #include "FileClassifier.h"
#include "network/network.h" #include "network/network.h"
@@ -322,14 +323,16 @@ namespace OpenRCT2
{ {
#ifndef DISABLE_HTTP #ifndef DISABLE_HTTP
// Download park and open it using its temporary filename // Download park and open it using its temporary filename
char tmpPath[MAX_PATH]; void * data;
if (!http_download_park(gOpenRCT2StartupActionPath, tmpPath)) size_t dataSize = http_download_park(gOpenRCT2StartupActionPath, &data);
if (dataSize == 0)
{ {
title_load(); title_load();
break; break;
} }
parkLoaded = OpenParkAutoDetectFormat(tmpPath); auto ms = MemoryStream(data, dataSize, MEMORY_ACCESS::OWNER);
parkLoaded = OpenParkAutoDetectFormat(&ms);
#endif #endif
} }
else else
@@ -491,10 +494,10 @@ namespace OpenRCT2
sprite_position_tween_restore(); sprite_position_tween_restore();
} }
bool OpenParkAutoDetectFormat(const utf8 * path) bool OpenParkAutoDetectFormat(IStream * stream)
{ {
ClassifiedFile info; ClassifiedFile info;
if (TryClassifyFile(path, &info)) if (TryClassifyFile(stream, &info))
{ {
if (info.Type == FILE_TYPE::SAVED_GAME || if (info.Type == FILE_TYPE::SAVED_GAME ||
info.Type == FILE_TYPE::SCENARIO) info.Type == FILE_TYPE::SCENARIO)
@@ -513,7 +516,7 @@ namespace OpenRCT2
{ {
try try
{ {
parkImporter->LoadSavedGame(path); parkImporter->LoadFromStream(stream, false);
parkImporter->Import(); parkImporter->Import();
game_fix_save_vars(); game_fix_save_vars();
sprite_position_tween_reset(); sprite_position_tween_reset();
@@ -531,7 +534,7 @@ namespace OpenRCT2
{ {
try try
{ {
parkImporter->LoadScenario(path); parkImporter->LoadFromStream(stream, true);
parkImporter->Import(); parkImporter->Import();
game_fix_save_vars(); game_fix_save_vars();
sprite_position_tween_reset(); sprite_position_tween_reset();

View File

@@ -71,6 +71,8 @@ bool TryClassifyFile(IStream * stream, ClassifiedFile * result)
static bool TryClassifyAsS6(IStream * stream, ClassifiedFile * result) static bool TryClassifyAsS6(IStream * stream, ClassifiedFile * result)
{ {
bool success = false;
uint64 originalPosition = stream->GetPosition();
try try
{ {
auto chunkReader = SawyerChunkReader(stream); auto chunkReader = SawyerChunkReader(stream);
@@ -84,12 +86,13 @@ static bool TryClassifyAsS6(IStream * stream, ClassifiedFile * result)
result->Type = FILE_TYPE::SCENARIO; result->Type = FILE_TYPE::SCENARIO;
} }
result->Version = s6Header.version; result->Version = s6Header.version;
return true; success = true;
} }
catch (Exception) catch (Exception)
{ {
} }
return false; stream->SetPosition(originalPosition);
return success;
} }
static bool TryClassifyAsS4(IStream * stream, ClassifiedFile * result) static bool TryClassifyAsS4(IStream * stream, ClassifiedFile * result)

View File

@@ -292,7 +292,7 @@ const char *http_get_extension_from_url(const char *url, const char *fallback)
} }
} }
bool http_download_park(const char *url, char tmpPath[L_tmpnam + 10]) size_t http_download_park(const char * url, void * * outData)
{ {
// Download park to buffer in memory // Download park to buffer in memory
HttpRequest2 request; HttpRequest2 request;
@@ -307,35 +307,24 @@ bool http_download_park(const char *url, char tmpPath[L_tmpnam + 10])
if (response != NULL) { if (response != NULL) {
http_request_dispose(response); http_request_dispose(response);
} }
return false;
*outData = NULL;
return 0;
} }
// Generate temporary filename that includes the original extension size_t dataSize = response->size - 1;
if (tmpnam(tmpPath) == NULL) { void * data = malloc(dataSize);
Console::Error::WriteLine("Failed to generate temporary filename for downloaded park '%s'", request.Url.c_str()); if (data == NULL) {
http_request_dispose(response); dataSize = 0;
return false; Console::Error::WriteLine("Failed to allocate memory for downloaded park.");
} else {
memcpy(data, response->body, dataSize);
} }
size_t remainingBytes = L_tmpnam + 10 - strlen(tmpPath);
const char *ext = http_get_extension_from_url(request.Url.c_str(), ".sv6");
strncat(tmpPath, ext, remainingBytes);
// Store park in temporary file and load it (discard ending NUL in response body)
FILE* tmpFile = fopen(tmpPath, "wb");
if (tmpFile == NULL) {
Console::Error::WriteLine("Failed to write downloaded park '%s' to temporary file", request.Url.c_str());
http_request_dispose(response);
return false;
}
fwrite(response->body, 1, response->size - 1, tmpFile);
fclose(tmpFile);
http_request_dispose(response); http_request_dispose(response);
return true; *outData = data;
return dataSize;
} }
#endif #endif

View File

@@ -60,8 +60,14 @@ void http_request_dispose(http_response_t *response);
const char *http_get_extension_from_url(const char *url, const char *fallback); const char *http_get_extension_from_url(const char *url, const char *fallback);
// Padding for extension that is appended to temporary file name /**
bool http_download_park(const char *url, char tmpPath[L_tmpnam + 10]); * Download a park via HTTP/S from the given URL into a memory buffer. This is
* a blocking operation.
* @param url The URL to download the park from.
* @param outData The data returned.
* @returns The size of the data or 0 if the download failed.
*/
size_t http_download_park(const char * url, void * * outData);
#endif // DISABLE_HTTP #endif // DISABLE_HTTP
// These callbacks are defined anyway, but are dummy if HTTP is disabled // These callbacks are defined anyway, but are dummy if HTTP is disabled