OpenRCT2/test/testpaint/main.cpp

645 lines
17 KiB
C++
Raw Normal View History

2016-08-31 14:33:18 +02:00
/*****************************************************************************
* Copyright (c) 2014-2018 OpenRCT2 developers
2016-08-31 14:33:18 +02:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2016-08-31 14:33:18 +02:00
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-08-31 14:33:18 +02:00
*****************************************************************************/
2016-09-03 16:34:09 +02:00
#include <algorithm>
2017-06-12 19:00:53 +02:00
#include <cstdarg>
2018-03-18 22:40:46 +01:00
#include <cstdio>
2018-03-14 12:44:16 +01:00
#include <cstring>
2016-09-03 16:34:09 +02:00
#include <string>
#include <vector>
2016-08-31 14:33:18 +02:00
2016-09-06 14:55:44 +02:00
#if defined(__unix__)
2018-07-21 17:27:15 +02:00
# include <sys/mman.h>
# include <unistd.h>
2016-09-06 14:55:44 +02:00
#endif // defined(__unix__)
2018-06-22 22:29:03 +02:00
#include "Data.h"
#include "PaintIntercept.hpp"
2016-10-16 21:15:40 +02:00
#include "TestTrack.hpp"
2016-10-16 18:44:57 +02:00
#include "Utils.hpp"
2018-01-09 10:40:42 +01:00
#include <openrct2/rct2/RCT2.h>
2017-12-31 13:21:34 +01:00
#include <openrct2/ride/Ride.h>
2018-01-10 08:48:39 +01:00
#include <openrct2/ride/RideData.h>
2017-10-17 13:51:47 +02:00
#include <openrct2/ride/Track.h>
2017-10-16 12:02:23 +02:00
#include <openrct2/ride/TrackData.h>
2016-08-31 14:33:18 +02:00
2018-06-22 22:29:03 +02:00
struct TestCase
{
uint8_t rideType;
std::vector<uint8_t> trackTypes;
};
2016-09-04 08:12:48 +02:00
2018-06-22 22:29:03 +02:00
enum CLIColour
{
DEFAULT,
RED,
YELLOW,
GREEN,
2016-09-05 17:25:39 +02:00
};
bool gTestColor = true;
2016-10-19 01:55:18 +02:00
Verbosity _verbosity = NORMAL;
2016-09-05 17:25:39 +02:00
2018-06-22 22:29:03 +02:00
static bool CStringEquals(const char* lhs, const char* rhs)
{
if (lhs == nullptr)
return rhs == nullptr;
2016-09-05 17:25:39 +02:00
2018-06-22 22:29:03 +02:00
if (rhs == nullptr)
return false;
2016-09-05 17:25:39 +02:00
return strcmp(lhs, rhs) == 0;
2016-09-05 17:25:39 +02:00
}
2018-06-22 22:29:03 +02:00
enum COLOUR_METHOD
{
COLOUR_METHOD_NONE,
COLOUR_METHOD_ANSI,
COLOUR_METHOD_WINDOWS,
};
static COLOUR_METHOD GetColourMethod()
{
2018-06-22 22:29:03 +02:00
if (!gTestColor)
{
return COLOUR_METHOD_NONE;
}
const char* const term = getenv("TERM");
2018-06-22 22:29:03 +02:00
const bool term_supports_color = CStringEquals(term, "xterm") || CStringEquals(term, "xterm-color")
|| CStringEquals(term, "xterm-256color") || CStringEquals(term, "screen") || CStringEquals(term, "screen-256color")
|| CStringEquals(term, "tmux") || CStringEquals(term, "tmux-256color") || CStringEquals(term, "rxvt-unicode")
|| CStringEquals(term, "rxvt-unicode-256color") || CStringEquals(term, "linux") || CStringEquals(term, "cygwin");
if (term_supports_color)
{
return COLOUR_METHOD_ANSI;
}
#ifdef __WINDOWS__
return COLOUR_METHOD_WINDOWS;
#else
return COLOUR_METHOD_NONE;
#endif
2016-09-05 17:25:39 +02:00
}
2018-06-22 22:29:03 +02:00
static const char* GetAnsiColorCode(CLIColour color)
{
switch (color)
{
case RED:
return "1";
case GREEN:
return "2";
case YELLOW:
return "3";
2018-06-22 22:29:03 +02:00
default:
return nullptr;
};
2016-09-05 17:25:39 +02:00
}
#ifdef __WINDOWS__
static WORD GetCurrentWindowsConsoleAttribute(HANDLE hConsoleOutput)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsoleOutput, &csbi);
return csbi.wAttributes;
}
static WORD GetWindowsConsoleAttribute(CLIColour color, WORD defaultAttr)
{
2018-06-22 22:29:03 +02:00
switch (color)
{
case RED:
return FOREGROUND_RED;
case GREEN:
return FOREGROUND_GREEN;
case YELLOW:
return FOREGROUND_RED | FOREGROUND_GREEN;
default:
return defaultAttr;
};
}
#endif
2018-06-22 22:29:03 +02:00
static void Write_VA(Verbosity verbosity, CLIColour colour, const char* fmt, va_list args)
{
2018-06-22 22:29:03 +02:00
if (_verbosity < verbosity)
return;
2016-09-05 17:25:39 +02:00
COLOUR_METHOD colourMethod = GetColourMethod();
2018-06-22 22:29:03 +02:00
if (colour == CLIColour::DEFAULT || colourMethod == COLOUR_METHOD_NONE)
{
vprintf(fmt, args);
2018-06-22 22:29:03 +02:00
}
else if (colourMethod == COLOUR_METHOD_ANSI)
{
printf("\033[0;3%sm", GetAnsiColorCode(colour));
vprintf(fmt, args);
printf("\033[m");
2018-06-22 22:29:03 +02:00
}
else if (colourMethod == COLOUR_METHOD_WINDOWS)
{
#ifdef __WINDOWS__
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
WORD defaultAttr = GetCurrentWindowsConsoleAttribute(hStdOut);
SetConsoleTextAttribute(hStdOut, GetWindowsConsoleAttribute(colour, defaultAttr));
vprintf(fmt, args);
SetConsoleTextAttribute(hStdOut, defaultAttr);
#endif
}
2016-10-19 01:55:18 +02:00
}
2018-06-22 22:29:03 +02:00
static void Write(Verbosity verbosity, CLIColour colour, const char* fmt, ...)
2016-10-19 01:55:18 +02:00
{
va_list args;
va_start(args, fmt);
Write_VA(verbosity, colour, fmt, args);
va_end(args);
2016-10-19 01:55:18 +02:00
}
2018-06-22 22:29:03 +02:00
static void Write(Verbosity verbosity, const char* fmt, ...)
2016-10-19 01:55:18 +02:00
{
va_list args;
va_start(args, fmt);
Write_VA(verbosity, DEFAULT, fmt, args);
va_end(args);
2016-10-19 01:55:18 +02:00
}
2018-06-22 22:29:03 +02:00
static void Write(CLIColour colour, const char* fmt, ...)
2016-10-19 01:55:18 +02:00
{
va_list args;
va_start(args, fmt);
Write_VA(NORMAL, colour, fmt, args);
va_end(args);
2016-10-19 01:55:18 +02:00
}
2018-06-22 22:29:03 +02:00
static void Write(const char* fmt, ...)
2016-10-19 01:55:18 +02:00
{
va_list args;
va_start(args, fmt);
Write_VA(NORMAL, DEFAULT, fmt, args);
va_end(args);
2016-09-05 17:25:39 +02:00
}
#if defined(__WINDOWS__)
2018-07-21 17:27:15 +02:00
# include <shellapi.h>
2018-06-22 22:29:03 +02:00
int main(int argc, char* argv[]);
2018-07-21 17:27:15 +02:00
# define OPENRCT2_DLL_MODULE_NAME "openrct2.dll"
2018-01-29 17:06:01 +01:00
static HMODULE _dllModule = nullptr;
2018-06-22 22:29:03 +02:00
utf8* utf8_write_codepoint(utf8* dst, uint32_t codepoint)
{
2018-06-22 22:29:03 +02:00
if (codepoint <= 0x7F)
{
dst[0] = (utf8)codepoint;
return dst + 1;
2018-06-22 22:29:03 +02:00
}
else if (codepoint <= 0x7FF)
{
dst[0] = 0xC0 | ((codepoint >> 6) & 0x1F);
dst[1] = 0x80 | (codepoint & 0x3F);
return dst + 2;
2018-06-22 22:29:03 +02:00
}
else if (codepoint <= 0xFFFF)
{
dst[0] = 0xE0 | ((codepoint >> 12) & 0x0F);
dst[1] = 0x80 | ((codepoint >> 6) & 0x3F);
dst[2] = 0x80 | (codepoint & 0x3F);
return dst + 3;
2018-06-22 22:29:03 +02:00
}
else
{
dst[0] = 0xF0 | ((codepoint >> 18) & 0x07);
dst[1] = 0x80 | ((codepoint >> 12) & 0x3F);
dst[2] = 0x80 | ((codepoint >> 6) & 0x3F);
dst[3] = 0x80 | (codepoint & 0x3F);
return dst + 4;
}
}
2018-06-22 22:29:03 +02:00
utf8* widechar_to_utf8(const wchar_t* src)
{
2018-06-22 22:29:03 +02:00
utf8* result = (utf8*)malloc((wcslen(src) * 4) + 1);
utf8* dst = result;
2018-06-22 22:29:03 +02:00
for (; *src != 0; src++)
{
dst = utf8_write_codepoint(dst, *src);
}
*dst++ = 0;
size_t size = (size_t)(dst - result);
2018-06-22 22:29:03 +02:00
return (utf8*)realloc(result, size);
}
2018-06-22 22:29:03 +02:00
utf8** windows_get_command_line_args(int* outNumArgs)
{
int argc;
// Get command line arguments as widechar
LPWSTR commandLine = GetCommandLineW();
2018-06-22 22:29:03 +02:00
LPWSTR* argvW = CommandLineToArgvW(commandLine, &argc);
// Convert to UTF-8
2018-06-22 22:29:03 +02:00
utf8** argvUtf8 = (utf8**)malloc(argc * sizeof(utf8*));
for (int i = 0; i < argc; i++)
{
argvUtf8[i] = widechar_to_utf8(argvW[i]);
}
LocalFree(argvW);
*outNumArgs = argc;
return argvUtf8;
}
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
_dllModule = (HMODULE)hModule;
return TRUE;
}
__declspec(dllexport) int StartOpenRCT2(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
2018-06-22 22:29:03 +02:00
if (_dllModule == nullptr)
{
_dllModule = GetModuleHandleA(OPENRCT2_DLL_MODULE_NAME);
}
int argc;
2018-06-22 22:29:03 +02:00
char** argv = (char**)windows_get_command_line_args(&argc);
int gExitCode = main(argc, argv);
// Free argv
2018-06-22 22:29:03 +02:00
for (int i = 0; i < argc; i++)
{
free(argv[i]);
}
free(argv);
2016-11-13 20:17:49 +01:00
exit(gExitCode);
return gExitCode;
}
#endif
2018-06-22 22:29:03 +02:00
char* segments = (char*)(GOOD_PLACE_FOR_DATA_SEGMENT);
2016-09-06 14:55:44 +02:00
static uint32_t sawyercoding_calculate_checksum(const uint8_t* buffer, size_t length)
2016-09-06 14:55:44 +02:00
{
size_t i;
uint32_t checksum = 0;
for (i = 0; i < length; i++)
checksum += buffer[i];
2016-09-06 14:55:44 +02:00
return checksum;
2016-09-06 14:55:44 +02:00
}
/**
* Loads RCT2's data model and remaps the addresses.
* @returns true if the data integrity check succeeded, otherwise false.
*/
static bool openrct2_setup_rct2_segment()
{
// OpenRCT2 on Linux and macOS is wired to have the original Windows PE sections loaded
// necessary. Windows does not need to do this as OpenRCT2 runs as a DLL loaded from the Windows PE.
2016-10-11 00:53:31 +02:00
// in some configurations err and len may be unused
[[maybe_unused]] int len = 0x01429000 - 0x8a4000; // 0xB85000, 12079104 bytes or around 11.5MB
[[maybe_unused]] int err = 0;
2016-09-06 14:55:44 +02:00
#if defined(__unix__)
int pageSize = getpagesize();
int numPages = (len + pageSize - 1) / pageSize;
2018-06-22 22:29:03 +02:00
unsigned char* dummy = (unsigned char*)malloc(numPages);
2018-06-22 22:29:03 +02:00
err = mincore((void*)segments, len, dummy);
bool pagesMissing = false;
if (err != 0)
{
err = errno;
2018-07-21 17:27:15 +02:00
# ifdef __LINUX__
// On Linux ENOMEM means all requested range is unmapped
if (err != ENOMEM)
{
pagesMissing = true;
perror("mincore");
}
2018-07-21 17:27:15 +02:00
# else
pagesMissing = true;
perror("mincore");
2018-07-21 17:27:15 +02:00
# endif // __LINUX__
2018-06-22 22:29:03 +02:00
}
else
{
for (int i = 0; i < numPages; i++)
{
if (dummy[i] != 1)
{
pagesMissing = true;
2018-06-22 22:29:03 +02:00
void* start = (void*)(segments + i * pageSize);
void* end = (void*)(segments + (i + 1) * pageSize - 1);
log_warning("required page %p - %p is not in memory!", start, end);
}
}
}
free(dummy);
if (pagesMissing)
{
log_error("At least one of required pages was not found in memory. This can cause segfaults later on.");
}
// section: text
2018-06-22 22:29:03 +02:00
err = mprotect((void*)0x401000, 0x8a4000 - 0x401000, PROT_READ | PROT_EXEC | PROT_WRITE);
if (err != 0)
{
perror("mprotect");
}
// section: rw data
2018-06-22 22:29:03 +02:00
err = mprotect((void*)segments, 0x01429000 - 0x8a4000, PROT_READ | PROT_WRITE);
if (err != 0)
{
perror("mprotect");
}
2016-09-06 14:55:44 +02:00
#endif // defined(__unix__)
// Check that the expected data is at various addresses.
// Start at 0x9a6000, which is start of .data, to skip the region containing addresses to DLL
// calls, which can be changed by windows/wine loader.
2018-06-22 22:29:03 +02:00
const uint32_t c1 = sawyercoding_calculate_checksum(
(const uint8_t*)(segments + (uintptr_t)(0x009A6000 - 0x8a4000)), 0x009E0000 - 0x009A6000);
const uint32_t c2 = sawyercoding_calculate_checksum(
(const uint8_t*)(segments + (uintptr_t)(0x01428000 - 0x8a4000)), 0x014282BC - 0x01428000);
const uint32_t exp_c1 = 10114815;
const uint32_t exp_c2 = 23564;
2018-06-22 22:29:03 +02:00
if (c1 != exp_c1 || c2 != exp_c2)
{
log_warning("c1 = %u, expected %u, match %d", c1, exp_c1, c1 == exp_c1);
log_warning("c2 = %u, expected %u, match %d", c2, exp_c2, c2 == exp_c2);
return false;
}
return true;
2016-09-06 14:55:44 +02:00
}
2016-10-02 10:30:28 +02:00
static void PrintRideTypes()
{
2020-06-17 19:10:39 +02:00
for (uint8_t rideType = 0; rideType < RCT2_RIDE_TYPE_COUNT; rideType++)
2018-06-22 22:29:03 +02:00
{
CLIColour colour = CLIColour::DEFAULT;
bool implemented = Utils::rideIsImplemented(rideType);
2018-06-22 22:29:03 +02:00
const char* rideName = RideNames[rideType];
const char* status = "";
if (implemented)
{
status = " [IMPLEMENTED]";
colour = CLIColour::GREEN;
}
Write(colour, "%2d: %-30s%s\n", rideType, rideName, status);
}
}
2016-10-17 15:15:40 +02:00
#include "GeneralSupportHeightCall.hpp"
2016-10-17 00:13:46 +02:00
2018-06-22 22:29:03 +02:00
static void TestGeneralSupportHeightCall()
{
SupportCall callA = { 16, 0x20 };
SupportCall callB = { 32, 0x20 };
SupportCall callC = { 48, 0x20 };
SupportCall callD = { 48, 0x1F };
2018-06-22 22:29:03 +02:00
SupportCall out = { 0, 0 };
bool success;
2018-06-22 22:29:03 +02:00
SupportCall groupA[4] = { callA, callA, callA, callA };
success = GeneralSupportHeightCall::FindMostCommonSupportCall(groupA, &out);
assert(success);
assert(out == callA);
2018-06-22 22:29:03 +02:00
SupportCall groupB[4] = { callB, callA, callA, callA };
success = GeneralSupportHeightCall::FindMostCommonSupportCall(groupB, &out);
assert(success);
assert(out == callA);
2018-06-22 22:29:03 +02:00
SupportCall groupC[4] = { callB, callA, callB, callA };
success = GeneralSupportHeightCall::FindMostCommonSupportCall(groupC, &out);
assert(!success);
2018-06-22 22:29:03 +02:00
SupportCall groupD[4] = { callB, callC, callB, callA };
success = GeneralSupportHeightCall::FindMostCommonSupportCall(groupD, &out);
assert(!success);
2018-06-22 22:29:03 +02:00
SupportCall groupE[4] = { callD, callC, callB, callA };
success = GeneralSupportHeightCall::FindMostCommonSupportCall(groupE, &out);
assert(!success);
2016-10-17 00:13:46 +02:00
}
2018-06-22 22:29:03 +02:00
int main(int argc, char* argv[])
{
#if !defined(__i386__)
fprintf(stderr, "Testpaint can only be properly executed on x86\n");
return 1;
#else
TestGeneralSupportHeightCall();
2016-10-17 00:13:46 +02:00
std::vector<TestCase> testCases;
2016-08-31 14:33:18 +02:00
bool generate = false;
uint8_t specificRideType = 0xFF;
2018-06-22 22:29:03 +02:00
for (int i = 0; i < argc; ++i)
{
char* arg = argv[i];
if (strcmp(arg, "--gtest_color=no") == 0)
{
gTestColor = false;
}
2018-06-22 22:29:03 +02:00
else if (strcmp(arg, "--quiet") == 0)
{
_verbosity = Verbosity::QUIET;
}
2018-06-22 22:29:03 +02:00
else if (strcmp(arg, "--ride-type") == 0)
{
if (i + 1 < argc)
{
i++;
specificRideType = atoi(argv[i]);
2018-06-22 22:29:03 +02:00
}
else
{
PrintRideTypes();
return 2;
}
}
2018-06-22 22:29:03 +02:00
else if (strcmp(arg, "--generate") == 0)
{
generate = true;
}
}
2018-06-22 22:29:03 +02:00
if (generate)
{
if (specificRideType > 90)
{
fprintf(stderr, "No ride or invalid ride specified.\n");
return 1;
}
openrct2_setup_rct2_segment();
PaintIntercept::InitHooks();
return generatePaintCode(specificRideType);
}
2020-06-17 19:10:39 +02:00
for (uint8_t rideType = 0; rideType < RCT2_RIDE_TYPE_COUNT; rideType++)
2018-06-22 22:29:03 +02:00
{
if (specificRideType != RIDE_TYPE_NULL && rideType != specificRideType)
{
continue;
}
2018-06-22 22:29:03 +02:00
if (!Utils::rideIsImplemented(rideType))
{
continue;
2016-10-17 20:04:13 +02:00
}
2018-06-05 15:28:19 +02:00
TestCase testCase = {};
testCase.rideType = rideType;
if (GetRideTypeDescriptor(rideType).HasFlag(RIDE_TYPE_FLAG_FLAT_RIDE))
2018-06-22 22:29:03 +02:00
{
testCase.trackTypes.push_back(GetRideTypeDescriptor(rideType).StartTrackPiece);
2018-06-22 22:29:03 +02:00
}
else
{
for (int trackType = 0; trackType < 256; trackType++)
{
if (Utils::rideSupportsTrackType(rideType, trackType))
{
testCase.trackTypes.push_back(trackType);
}
}
}
testCases.push_back(testCase);
}
2018-06-22 22:29:03 +02:00
int testCaseCount = (int)testCases.size();
int testCount = 0;
2018-06-22 22:29:03 +02:00
for (auto&& tc : testCases)
{
testCount += tc.trackTypes.size();
}
Write(CLIColour::GREEN, "[==========] ");
Write("Running %d tests from %d test cases.\n", testCount, testCaseCount);
Write(CLIColour::GREEN, "[----------] ");
Write("Global test environment set-up.\n");
openrct2_setup_rct2_segment();
PaintIntercept::InitHooks();
int successCount = 0;
std::vector<utf8string> failures;
2018-06-22 22:29:03 +02:00
for (auto&& tc : testCases)
{
const utf8string rideTypeName = RideNames[tc.rideType];
Write(CLIColour::GREEN, "[----------] ");
Write("%d tests from %s\n", (int)tc.trackTypes.size(), rideTypeName);
2018-06-22 22:29:03 +02:00
for (auto&& trackType : tc.trackTypes)
{
utf8string trackTypeName;
if (GetRideTypeDescriptor(tc.rideType).HasFlag(RIDE_TYPE_FLAG_FLAT_RIDE))
2018-06-22 22:29:03 +02:00
{
trackTypeName = FlatTrackNames[trackType];
2018-06-22 22:29:03 +02:00
}
else
{
trackTypeName = TrackNames[trackType];
}
Write(CLIColour::GREEN, "[ RUN ] ");
Write("%s.%s\n", rideTypeName, trackTypeName);
std::string out;
int retVal = TestTrack::TestPaintTrackElement(tc.rideType, trackType, &out);
Write("%s", out.c_str());
2018-06-22 22:29:03 +02:00
switch (retVal)
{
case TEST_SUCCESS:
Write(CLIColour::GREEN, "[ OK ] ");
Write("%s.%s (0 ms)\n", rideTypeName, trackTypeName);
successCount++;
break;
case TEST_SKIPPED:
Write("Skipped\n");
// Outputting this as OK because CLion only allows FAILED or OK
Write(CLIColour::YELLOW, "[ OK ] ");
Write("%s.%s (0 ms)\n", rideTypeName, trackTypeName);
successCount++;
break;
case TEST_FAILED:
utf8string testCaseName = new utf8[64];
snprintf(testCaseName, 64, "%s.%s", rideTypeName, trackTypeName);
Write(CLIColour::RED, "[ FAILED ] ");
Write("%s (0 ms)\n", testCaseName);
failures.push_back(testCaseName);
break;
}
}
Write(CLIColour::GREEN, "[----------] ");
2018-06-22 22:29:03 +02:00
Write("%d tests from %s (0 ms total)\n", (int)tc.trackTypes.size(), rideTypeName);
}
Write("\n");
Write(CLIColour::GREEN, "[----------] ");
Write("Global test environment tear-down\n");
Write(CLIColour::GREEN, "[==========] ");
Write("%d tests from %d test cases ran. (0 ms total).\n", testCount, testCaseCount);
Write(Verbosity::QUIET, CLIColour::GREEN, "[ PASSED ] ");
Write(Verbosity::QUIET, "%d tests.\n", successCount);
if (!failures.empty())
2018-06-22 22:29:03 +02:00
{
Write(Verbosity::QUIET, CLIColour::RED, "[ FAILED ] ");
Write(Verbosity::QUIET, "%d tests, listed below:\n", (int)failures.size());
2018-06-22 22:29:03 +02:00
for (auto&& failure : failures)
{
Write(Verbosity::QUIET, CLIColour::RED, "[ FAILED ] ");
Write(Verbosity::QUIET, "%s\n", failure);
2018-06-22 22:29:03 +02:00
delete[] failure;
}
Write(Verbosity::QUIET, "\n");
Write(Verbosity::QUIET, "%d FAILED TESTS\n", (int)failures.size());
return 1;
}
return 0;
#endif
2016-09-04 08:12:48 +02:00
}