Files
OpenRCT2-Web/test/tests/S6ImportExportTests.cpp
T

292 lines
8.9 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "TestData.h"
#include <gtest/gtest.h>
#include <openrct2/Cheats.h>
#include <openrct2/Context.h>
2024-07-09 16:23:31 +02:00
#include <openrct2/Diagnostic.h>
#include <openrct2/Game.h>
#include <openrct2/GameState.h>
#include <openrct2/GameStateSnapshots.h>
#include <openrct2/OpenRCT2.h>
#include <openrct2/ParkImporter.h>
#include <openrct2/audio/AudioContext.h>
2020-07-10 19:02:51 +01:00
#include <openrct2/core/Crypt.h>
#include <openrct2/core/File.h>
#include <openrct2/core/MemoryStream.h>
#include <openrct2/core/Path.hpp>
#include <openrct2/core/String.hpp>
2021-11-24 16:58:01 +02:00
#include <openrct2/entity/EntityRegistry.h>
2021-11-24 16:48:33 +02:00
#include <openrct2/entity/EntityTweener.h>
2025-02-24 04:43:06 -05:00
#include <openrct2/network/Network.h>
#include <openrct2/object/ObjectManager.h>
#include <openrct2/park/ParkFile.h>
2022-02-18 21:57:00 +01:00
#include <openrct2/platform/Platform.h>
2023-12-10 19:35:17 +01:00
#include <openrct2/rct2/RCT2.h>
#include <openrct2/ride/Ride.h>
2021-10-27 14:21:14 +02:00
#include <openrct2/scenario/Scenario.h>
#include <openrct2/world/MapAnimation.h>
2021-11-26 15:03:14 +00:00
#include <openrct2/world/Scenery.h>
2019-10-19 23:45:45 +02:00
#include <stdio.h>
#include <string>
using namespace OpenRCT2;
static bool LoadFileToBuffer(MemoryStream& stream, const std::string& filePath)
{
FILE* fp = fopen(filePath.c_str(), "rb");
EXPECT_NE(fp, nullptr);
if (fp == nullptr)
return false;
uint8_t buf[1024];
size_t bytesRead = fread(buf, 1, sizeof(buf), fp);
while (bytesRead > 0)
{
stream.Write(buf, bytesRead);
bytesRead = fread(buf, 1, sizeof(buf), fp);
}
fclose(fp);
return true;
}
static void GameInit(bool retainSpatialIndices)
{
2025-08-31 14:29:36 +02:00
auto& gameState = getGameState();
if (!retainSpatialIndices)
2025-08-31 14:29:36 +02:00
gameState.entities.ResetEntitySpatialIndices();
ResetAllSpriteQuadrantPlacements();
LoadPalette();
EntityTweener::Get().Reset();
MapAnimations::MarkAllTiles();
FixInvalidVehicleSpriteSizes();
gGameSpeed = 1;
}
2021-10-27 14:21:14 +02:00
static bool ImportS6(MemoryStream& stream, std::unique_ptr<IContext>& context, bool retainSpatialIndices)
{
stream.SetPosition(0);
auto& objManager = context->GetObjectManager();
auto importer = ParkImporter::CreateS6(context->GetObjectRepository());
auto loadResult = importer->LoadFromStream(&stream, false);
2021-09-13 19:24:42 +01:00
objManager.LoadObjects(loadResult.RequiredObjects);
MapAnimations::ClearAll();
// TODO: Have a separate GameState and exchange once loaded.
auto& gameState = getGameState();
importer->Import(gameState);
GameInit(retainSpatialIndices);
return true;
}
2021-10-27 14:21:14 +02:00
static bool ImportPark(MemoryStream& stream, std::unique_ptr<IContext>& context, bool retainSpatialIndices)
{
stream.SetPosition(0);
auto& objManager = context->GetObjectManager();
auto importer = ParkImporter::CreateParkFile(context->GetObjectRepository());
auto loadResult = importer->LoadFromStream(&stream, false);
objManager.LoadObjects(loadResult.RequiredObjects);
// TODO: Have a separate GameState and exchange once loaded.
auto& gameState = getGameState();
importer->Import(gameState);
2021-10-27 14:21:14 +02:00
GameInit(retainSpatialIndices);
return true;
}
static bool ExportSave(MemoryStream& stream, std::unique_ptr<IContext>& context)
{
auto& objManager = context->GetObjectManager();
2021-10-27 14:21:14 +02:00
auto exporter = std::make_unique<ParkFileExporter>();
exporter->ExportObjectsList = objManager.GetPackableObjects();
auto& gameState = getGameState();
exporter->Export(gameState, stream, OpenRCT2::kParkFileSaveCompressionLevel);
return true;
}
static void RecordGameStateSnapshot(std::unique_ptr<IContext>& context, MemoryStream& snapshotStream)
{
auto* snapshots = context->GetGameStateSnapshots();
auto& snapshot = snapshots->CreateSnapshot();
snapshots->Capture(snapshot);
snapshots->LinkSnapshot(snapshot, getGameState().currentTicks, ScenarioRandState().s0);
DataSerialiser snapShotDs(true, snapshotStream);
snapshots->SerialiseSnapshot(snapshot, snapShotDs);
}
static void AdvanceGameTicks(uint32_t ticks, std::unique_ptr<IContext>& context)
{
for (uint32_t i = 0; i < ticks; i++)
{
2024-03-26 15:08:17 +01:00
gameStateUpdateLogic();
}
}
static void CompareStates(MemoryStream& importBuffer, MemoryStream& exportBuffer, MemoryStream& snapshotStream)
{
if (importBuffer.GetLength() != exportBuffer.GetLength())
{
LOG_WARNING(
"Inconsistent export size! Import Size: %llu bytes, Export Size: %llu bytes",
static_cast<unsigned long long>(importBuffer.GetLength()),
static_cast<unsigned long long>(exportBuffer.GetLength()));
}
std::unique_ptr<IContext> context = CreateContext();
EXPECT_NE(context, nullptr);
bool initialised = context->Initialise();
ASSERT_TRUE(initialised);
DataSerialiser ds(false, snapshotStream);
IGameStateSnapshots* snapshots = GetContext()->GetGameStateSnapshots();
GameStateSnapshot_t& importSnapshot = snapshots->CreateSnapshot();
snapshots->SerialiseSnapshot(importSnapshot, ds);
GameStateSnapshot_t& exportSnapshot = snapshots->CreateSnapshot();
snapshots->SerialiseSnapshot(exportSnapshot, ds);
try
{
2023-01-19 15:13:23 +00:00
GameStateCompareData cmpData = snapshots->Compare(importSnapshot, exportSnapshot);
// Find out if there are any differences between the two states
auto res = std::find_if(
cmpData.spriteChanges.begin(), cmpData.spriteChanges.end(),
2023-01-19 15:13:23 +00:00
[](const GameStateSpriteChange& diff) { return diff.changeType != GameStateSpriteChange::EQUAL; });
if (res != cmpData.spriteChanges.end())
{
LOG_WARNING("Snapshot data differences. %s", snapshots->GetCompareDataText(cmpData).c_str());
FAIL();
}
}
catch (const std::runtime_error& err)
{
LOG_WARNING("Snapshot data failed to be read. Snapshot not compared. %s", err.what());
FAIL();
}
}
TEST(S6ImportExportBasic, all)
{
gOpenRCT2Headless = true;
gOpenRCT2NoGraphics = true;
MemoryStream importBuffer;
MemoryStream exportBuffer;
MemoryStream snapshotStream;
// Load initial park data.
{
std::unique_ptr<IContext> context = CreateContext();
EXPECT_NE(context, nullptr);
bool initialised = context->Initialise();
ASSERT_TRUE(initialised);
std::string testParkPath = TestData::GetParkPath("BigMapTest.sv6");
ASSERT_TRUE(LoadFileToBuffer(importBuffer, testParkPath));
2021-10-27 14:21:14 +02:00
ASSERT_TRUE(ImportS6(importBuffer, context, false));
RecordGameStateSnapshot(context, snapshotStream);
ASSERT_TRUE(ExportSave(exportBuffer, context));
}
// Import the exported version.
{
std::unique_ptr<IContext> context = CreateContext();
EXPECT_NE(context, nullptr);
bool initialised = context->Initialise();
ASSERT_TRUE(initialised);
2021-10-27 14:21:14 +02:00
ASSERT_TRUE(ImportPark(exportBuffer, context, true));
RecordGameStateSnapshot(context, snapshotStream);
}
snapshotStream.SetPosition(0);
CompareStates(importBuffer, exportBuffer, snapshotStream);
SUCCEED();
}
TEST(S6ImportExportAdvanceTicks, all)
{
gOpenRCT2Headless = true;
gOpenRCT2NoGraphics = true;
MemoryStream importBuffer;
MemoryStream exportBuffer;
MemoryStream snapshotStream;
// Load initial park data.
{
std::unique_ptr<IContext> context = CreateContext();
EXPECT_NE(context, nullptr);
bool initialised = context->Initialise();
ASSERT_TRUE(initialised);
std::string testParkPath = TestData::GetParkPath("BigMapTest.sv6");
ASSERT_TRUE(LoadFileToBuffer(importBuffer, testParkPath));
2021-10-27 14:21:14 +02:00
ASSERT_TRUE(ImportS6(importBuffer, context, false));
AdvanceGameTicks(1000, context);
ASSERT_TRUE(ExportSave(exportBuffer, context));
RecordGameStateSnapshot(context, snapshotStream);
}
// Import the exported version.
{
std::unique_ptr<IContext> context = CreateContext();
EXPECT_NE(context, nullptr);
bool initialised = context->Initialise();
ASSERT_TRUE(initialised);
2021-10-27 14:21:14 +02:00
ASSERT_TRUE(ImportPark(exportBuffer, context, true));
RecordGameStateSnapshot(context, snapshotStream);
}
snapshotStream.SetPosition(0);
CompareStates(importBuffer, exportBuffer, snapshotStream);
SUCCEED();
}
2020-07-10 19:02:51 +01:00
TEST(SeaDecrypt, DecryptSea)
{
auto path = TestData::GetParkPath("volcania.sea");
auto decrypted = DecryptSea(path);
auto sha1 = Crypt::SHA1(decrypted.data(), decrypted.size());
2021-10-17 15:31:56 +02:00
std::array<uint8_t, 20> expected = {
0x1B, 0x85, 0xFC, 0xC0, 0xE8, 0x9B, 0xBE, 0x72, 0xD9, 0x1F, 0x6E, 0xC8, 0xB1, 0xFF, 0xEC, 0x70, 0x2A, 0x72, 0x05, 0xBB,
};
2020-07-10 19:02:51 +01:00
ASSERT_EQ(sha1, expected);
}