2016-12-28 19:45:01 +01:00
|
|
|
/*****************************************************************************
|
2020-07-21 15:04:34 +02:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2016-12-28 19:45:01 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2016-12-28 19:45:01 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-12-28 19:45:01 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2018-06-22 23:25:16 +02:00
|
|
|
#include "FileClassifier.h"
|
|
|
|
|
2021-10-27 14:21:14 +02:00
|
|
|
#include "ParkFile.h"
|
2017-11-22 21:56:28 +01:00
|
|
|
#include "core/Console.hpp"
|
2020-11-07 13:42:04 +01:00
|
|
|
#include "core/FileStream.h"
|
2017-07-28 19:35:07 +02:00
|
|
|
#include "core/Path.hpp"
|
2020-11-07 13:42:04 +01:00
|
|
|
#include "core/String.hpp"
|
2017-02-05 13:18:07 +01:00
|
|
|
#include "rct12/SawyerChunkReader.h"
|
2018-01-02 18:58:43 +01:00
|
|
|
#include "scenario/Scenario.h"
|
2017-12-13 13:02:24 +01:00
|
|
|
#include "util/SawyerCoding.h"
|
2016-12-28 19:45:01 +01:00
|
|
|
|
2021-10-27 14:21:14 +02:00
|
|
|
static bool TryClassifyAsPark(OpenRCT2::IStream* stream, ClassifiedFileInfo* result);
|
2020-07-30 23:01:22 +02:00
|
|
|
static bool TryClassifyAsS6(OpenRCT2::IStream* stream, ClassifiedFileInfo* result);
|
|
|
|
static bool TryClassifyAsS4(OpenRCT2::IStream* stream, ClassifiedFileInfo* result);
|
|
|
|
static bool TryClassifyAsTD4_TD6(OpenRCT2::IStream* stream, ClassifiedFileInfo* result);
|
2016-12-28 19:45:01 +01:00
|
|
|
|
2018-06-22 23:25:16 +02:00
|
|
|
bool TryClassifyFile(const std::string& path, ClassifiedFileInfo* result)
|
2016-12-28 19:45:01 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-07-30 23:01:22 +02:00
|
|
|
auto fs = OpenRCT2::FileStream(path, OpenRCT2::FILE_MODE_OPEN);
|
2016-12-28 19:45:01 +01:00
|
|
|
return TryClassifyFile(&fs, result);
|
|
|
|
}
|
2018-06-22 23:25:16 +02:00
|
|
|
catch (const std::exception&)
|
2016-12-28 19:45:01 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 23:01:22 +02:00
|
|
|
bool TryClassifyFile(OpenRCT2::IStream* stream, ClassifiedFileInfo* result)
|
2016-12-28 19:45:01 +01:00
|
|
|
{
|
|
|
|
// TODO Currently track designs get classified as SC4s because they use the
|
|
|
|
// same checksum algorithm. The only way after to tell the difference
|
|
|
|
// between them is to decode it. Decoding however is currently not protected
|
|
|
|
// against invalid compression data for that decoding algorithm and will crash.
|
|
|
|
|
2021-10-27 14:21:14 +02:00
|
|
|
// Park detection
|
|
|
|
if (TryClassifyAsPark(stream, result))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-28 19:45:01 +01:00
|
|
|
// S6 detection
|
|
|
|
if (TryClassifyAsS6(stream, result))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// S4 detection
|
|
|
|
if (TryClassifyAsS4(stream, result))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TD6 detection
|
|
|
|
if (TryClassifyAsTD4_TD6(stream, result))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:21:14 +02:00
|
|
|
static bool TryClassifyAsPark(OpenRCT2::IStream* stream, ClassifiedFileInfo* result)
|
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
uint64_t originalPosition = stream->GetPosition();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto magic = stream->ReadValue<uint32_t>();
|
|
|
|
if (magic == OpenRCT2::PARK_FILE_MAGIC)
|
|
|
|
{
|
|
|
|
result->Type = FILE_TYPE::PARK;
|
|
|
|
result->Version = 0;
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
log_verbose(e.what());
|
|
|
|
}
|
|
|
|
stream->SetPosition(originalPosition);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2020-07-30 23:01:22 +02:00
|
|
|
static bool TryClassifyAsS6(OpenRCT2::IStream* stream, ClassifiedFileInfo* result)
|
2016-12-28 19:45:01 +01:00
|
|
|
{
|
2017-06-07 00:42:28 +02:00
|
|
|
bool success = false;
|
2018-06-20 17:28:51 +02:00
|
|
|
uint64_t originalPosition = stream->GetPosition();
|
2017-02-05 13:18:07 +01:00
|
|
|
try
|
2016-12-28 19:45:01 +01:00
|
|
|
{
|
2017-02-05 13:18:07 +01:00
|
|
|
auto chunkReader = SawyerChunkReader(stream);
|
|
|
|
auto s6Header = chunkReader.ReadChunkAs<rct_s6_header>();
|
2016-12-28 19:45:01 +01:00
|
|
|
if (s6Header.type == S6_TYPE_SAVEDGAME)
|
|
|
|
{
|
|
|
|
result->Type = FILE_TYPE::SAVED_GAME;
|
|
|
|
}
|
|
|
|
else if (s6Header.type == S6_TYPE_SCENARIO)
|
|
|
|
{
|
|
|
|
result->Type = FILE_TYPE::SCENARIO;
|
|
|
|
}
|
|
|
|
result->Version = s6Header.version;
|
2017-06-07 00:42:28 +02:00
|
|
|
success = true;
|
2016-12-28 19:45:01 +01:00
|
|
|
}
|
2018-06-22 23:25:16 +02:00
|
|
|
catch (const std::exception& e)
|
2017-02-05 13:18:07 +01:00
|
|
|
{
|
2018-01-02 19:23:14 +01:00
|
|
|
// Exceptions are likely to occur if file is not S6 format
|
2018-01-02 20:23:22 +01:00
|
|
|
log_verbose(e.what());
|
2017-02-05 13:18:07 +01:00
|
|
|
}
|
2017-06-07 00:42:28 +02:00
|
|
|
stream->SetPosition(originalPosition);
|
|
|
|
return success;
|
2016-12-28 19:45:01 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:01:22 +02:00
|
|
|
static bool TryClassifyAsS4(OpenRCT2::IStream* stream, ClassifiedFileInfo* result)
|
2016-12-28 19:45:01 +01:00
|
|
|
{
|
2017-11-17 22:37:13 +01:00
|
|
|
bool success = false;
|
2018-06-20 17:28:51 +02:00
|
|
|
uint64_t originalPosition = stream->GetPosition();
|
2017-11-17 22:37:13 +01:00
|
|
|
try
|
|
|
|
{
|
2020-06-07 23:18:11 +02:00
|
|
|
size_t dataLength = static_cast<size_t>(stream->GetLength());
|
2021-02-12 18:30:53 +01:00
|
|
|
auto data = stream->ReadArray<uint8_t>(dataLength);
|
2017-11-17 22:37:13 +01:00
|
|
|
stream->SetPosition(originalPosition);
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t fileTypeVersion = sawyercoding_detect_file_type(data.get(), dataLength);
|
2016-12-28 19:45:01 +01:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t type = fileTypeVersion & FILE_TYPE_MASK;
|
|
|
|
int32_t version = fileTypeVersion & FILE_VERSION_MASK;
|
2016-12-28 19:45:01 +01:00
|
|
|
|
2017-11-17 22:37:13 +01:00
|
|
|
if (type == FILE_TYPE_SV4)
|
|
|
|
{
|
|
|
|
result->Type = FILE_TYPE::SAVED_GAME;
|
|
|
|
result->Version = version;
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
else if (type == FILE_TYPE_SC4)
|
|
|
|
{
|
|
|
|
result->Type = FILE_TYPE::SCENARIO;
|
|
|
|
result->Version = version;
|
|
|
|
success = true;
|
|
|
|
}
|
2016-12-28 19:45:01 +01:00
|
|
|
}
|
2018-06-22 23:25:16 +02:00
|
|
|
catch (const std::exception& e)
|
2016-12-28 19:45:01 +01:00
|
|
|
{
|
2018-01-02 20:23:22 +01:00
|
|
|
Console::Error::WriteLine(e.what());
|
2016-12-28 19:45:01 +01:00
|
|
|
}
|
2017-03-07 20:38:11 +01:00
|
|
|
|
2017-11-17 22:37:13 +01:00
|
|
|
stream->SetPosition(originalPosition);
|
|
|
|
return success;
|
2016-12-28 19:45:01 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:01:22 +02:00
|
|
|
static bool TryClassifyAsTD4_TD6(OpenRCT2::IStream* stream, ClassifiedFileInfo* result)
|
2016-12-28 19:45:01 +01:00
|
|
|
{
|
|
|
|
bool success = false;
|
2018-06-20 17:28:51 +02:00
|
|
|
uint64_t originalPosition = stream->GetPosition();
|
2017-11-17 22:37:13 +01:00
|
|
|
try
|
2016-12-28 19:45:01 +01:00
|
|
|
{
|
2020-06-07 23:18:11 +02:00
|
|
|
size_t dataLength = static_cast<size_t>(stream->GetLength());
|
2021-02-12 18:30:53 +01:00
|
|
|
|
|
|
|
auto data = stream->ReadArray<uint8_t>(dataLength);
|
2017-11-17 22:37:13 +01:00
|
|
|
stream->SetPosition(originalPosition);
|
|
|
|
|
2017-11-18 21:48:23 +01:00
|
|
|
if (sawyercoding_validate_track_checksum(data.get(), dataLength))
|
2016-12-28 19:45:01 +01:00
|
|
|
{
|
2018-06-22 23:25:16 +02:00
|
|
|
std::unique_ptr<uint8_t, decltype(&Memory::Free<uint8_t>)> td6data(
|
|
|
|
Memory::Allocate<uint8_t>(0x10000), &Memory::Free<uint8_t>);
|
2017-11-18 21:48:23 +01:00
|
|
|
size_t td6len = sawyercoding_decode_td6(data.get(), td6data.get(), dataLength);
|
2017-11-17 22:37:13 +01:00
|
|
|
if (td6data != nullptr && td6len >= 8)
|
2016-12-28 19:45:01 +01:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t version = (td6data.get()[7] >> 2) & 3;
|
2017-11-17 22:37:13 +01:00
|
|
|
if (version <= 2)
|
|
|
|
{
|
|
|
|
result->Type = FILE_TYPE::TRACK_DESIGN;
|
|
|
|
result->Version = version;
|
|
|
|
success = true;
|
|
|
|
}
|
2016-12-28 19:45:01 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-17 22:37:13 +01:00
|
|
|
}
|
2018-01-02 20:23:22 +01:00
|
|
|
catch (const std::exception& e)
|
2017-11-17 22:37:13 +01:00
|
|
|
{
|
2018-01-02 20:23:22 +01:00
|
|
|
Console::Error::WriteLine(e.what());
|
2016-12-28 19:45:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
2017-07-28 19:35:07 +02:00
|
|
|
|
2018-06-22 23:25:16 +02:00
|
|
|
uint32_t get_file_extension_type(const utf8* path)
|
2017-07-28 19:35:07 +02:00
|
|
|
{
|
2018-02-01 18:49:14 +01:00
|
|
|
auto extension = Path::GetExtension(path);
|
2021-07-28 21:25:51 +02:00
|
|
|
if (String::Equals(extension, ".dat", true) || String::Equals(extension, ".pob", true))
|
2018-06-22 23:25:16 +02:00
|
|
|
return FILE_EXTENSION_DAT;
|
|
|
|
if (String::Equals(extension, ".sc4", true))
|
|
|
|
return FILE_EXTENSION_SC4;
|
|
|
|
if (String::Equals(extension, ".sv4", true))
|
|
|
|
return FILE_EXTENSION_SV4;
|
|
|
|
if (String::Equals(extension, ".td4", true))
|
|
|
|
return FILE_EXTENSION_TD4;
|
|
|
|
if (String::Equals(extension, ".sc6", true))
|
|
|
|
return FILE_EXTENSION_SC6;
|
2020-09-26 18:05:54 +02:00
|
|
|
if (String::Equals(extension, ".sea", true))
|
|
|
|
return FILE_EXTENSION_SC6;
|
2018-06-22 23:25:16 +02:00
|
|
|
if (String::Equals(extension, ".sv6", true))
|
|
|
|
return FILE_EXTENSION_SV6;
|
2018-08-16 16:16:56 +02:00
|
|
|
if (String::Equals(extension, ".sv7", true))
|
|
|
|
return FILE_EXTENSION_SV6;
|
2018-06-22 23:25:16 +02:00
|
|
|
if (String::Equals(extension, ".td6", true))
|
|
|
|
return FILE_EXTENSION_TD6;
|
2021-10-27 14:21:14 +02:00
|
|
|
if (String::Equals(extension, ".park", true))
|
|
|
|
return FILE_EXTENSION_PARK;
|
2018-02-01 18:49:14 +01:00
|
|
|
return FILE_EXTENSION_UNKNOWN;
|
2017-07-28 19:35:07 +02:00
|
|
|
}
|