mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-18 21:43:48 +01:00
Merge pull request #5564 from IntelOrca/fix/download-park-temp-file
Fix #4959: Compiler warning: tmpnam
This commit is contained in:
@@ -226,9 +226,8 @@ if (NOT DISABLE_TTF AND UNIX AND NOT APPLE)
|
||||
target_include_directories(${PROJECT} PRIVATE ${FONTCONFIG_INCLUDE_DIRS})
|
||||
endif ()
|
||||
|
||||
# macOS builds fail on the use of tmpnam otherwise (#4959)
|
||||
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()
|
||||
|
||||
# Compiler flags
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "core/File.h"
|
||||
#include "core/FileStream.hpp"
|
||||
#include "core/Guard.hpp"
|
||||
#include "core/MemoryStream.h"
|
||||
#include "core/String.hpp"
|
||||
#include "FileClassifier.h"
|
||||
#include "network/network.h"
|
||||
@@ -322,14 +323,16 @@ namespace OpenRCT2
|
||||
{
|
||||
#ifndef DISABLE_HTTP
|
||||
// Download park and open it using its temporary filename
|
||||
char tmpPath[MAX_PATH];
|
||||
if (!http_download_park(gOpenRCT2StartupActionPath, tmpPath))
|
||||
void * data;
|
||||
size_t dataSize = http_download_park(gOpenRCT2StartupActionPath, &data);
|
||||
if (dataSize == 0)
|
||||
{
|
||||
title_load();
|
||||
break;
|
||||
}
|
||||
|
||||
parkLoaded = OpenParkAutoDetectFormat(tmpPath);
|
||||
auto ms = MemoryStream(data, dataSize, MEMORY_ACCESS::OWNER);
|
||||
parkLoaded = OpenParkAutoDetectFormat(&ms);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
@@ -491,10 +494,10 @@ namespace OpenRCT2
|
||||
sprite_position_tween_restore();
|
||||
}
|
||||
|
||||
bool OpenParkAutoDetectFormat(const utf8 * path)
|
||||
bool OpenParkAutoDetectFormat(IStream * stream)
|
||||
{
|
||||
ClassifiedFile info;
|
||||
if (TryClassifyFile(path, &info))
|
||||
if (TryClassifyFile(stream, &info))
|
||||
{
|
||||
if (info.Type == FILE_TYPE::SAVED_GAME ||
|
||||
info.Type == FILE_TYPE::SCENARIO)
|
||||
@@ -513,7 +516,7 @@ namespace OpenRCT2
|
||||
{
|
||||
try
|
||||
{
|
||||
parkImporter->LoadSavedGame(path);
|
||||
parkImporter->LoadFromStream(stream, false);
|
||||
parkImporter->Import();
|
||||
game_fix_save_vars();
|
||||
sprite_position_tween_reset();
|
||||
@@ -531,7 +534,7 @@ namespace OpenRCT2
|
||||
{
|
||||
try
|
||||
{
|
||||
parkImporter->LoadScenario(path);
|
||||
parkImporter->LoadFromStream(stream, true);
|
||||
parkImporter->Import();
|
||||
game_fix_save_vars();
|
||||
sprite_position_tween_reset();
|
||||
|
||||
@@ -71,6 +71,8 @@ bool TryClassifyFile(IStream * stream, ClassifiedFile * result)
|
||||
|
||||
static bool TryClassifyAsS6(IStream * stream, ClassifiedFile * result)
|
||||
{
|
||||
bool success = false;
|
||||
uint64 originalPosition = stream->GetPosition();
|
||||
try
|
||||
{
|
||||
auto chunkReader = SawyerChunkReader(stream);
|
||||
@@ -84,12 +86,13 @@ static bool TryClassifyAsS6(IStream * stream, ClassifiedFile * result)
|
||||
result->Type = FILE_TYPE::SCENARIO;
|
||||
}
|
||||
result->Version = s6Header.version;
|
||||
return true;
|
||||
success = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
return false;
|
||||
stream->SetPosition(originalPosition);
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool TryClassifyAsS4(IStream * stream, ClassifiedFile * result)
|
||||
|
||||
@@ -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
|
||||
HttpRequest2 request;
|
||||
@@ -307,35 +307,24 @@ bool http_download_park(const char *url, char tmpPath[L_tmpnam + 10])
|
||||
if (response != NULL) {
|
||||
http_request_dispose(response);
|
||||
}
|
||||
return false;
|
||||
|
||||
*outData = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Generate temporary filename that includes the original extension
|
||||
if (tmpnam(tmpPath) == NULL) {
|
||||
Console::Error::WriteLine("Failed to generate temporary filename for downloaded park '%s'", request.Url.c_str());
|
||||
http_request_dispose(response);
|
||||
return false;
|
||||
size_t dataSize = response->size - 1;
|
||||
void * data = malloc(dataSize);
|
||||
if (data == NULL) {
|
||||
dataSize = 0;
|
||||
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);
|
||||
|
||||
return true;
|
||||
*outData = data;
|
||||
return dataSize;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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);
|
||||
|
||||
// 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
|
||||
|
||||
// These callbacks are defined anyway, but are dummy if HTTP is disabled
|
||||
|
||||
Reference in New Issue
Block a user