Extract function printing

This commit is contained in:
Marijn van der Werf 2016-10-16 18:18:54 +02:00
parent d6772865f5
commit 28db02885d
5 changed files with 160 additions and 103 deletions

View File

@ -207,6 +207,7 @@
C686F9581CDBC4C7009F9BFC /* vehicle_paint.c in Sources */ = {isa = PBXBuildFile; fileRef = C686F9561CDBC4C7009F9BFC /* vehicle_paint.c */; };
C6B5A7D41CDFE4CB00C9C006 /* S6Exporter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6B5A7D01CDFE4CB00C9C006 /* S6Exporter.cpp */; };
C6B5A7D51CDFE4CB00C9C006 /* S6Importer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6B5A7D21CDFE4CB00C9C006 /* S6Importer.cpp */; };
C6BDA02B1DB3DFD600271C0A /* Printer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6BDA0291DB3DFC900271C0A /* Printer.cpp */; };
D41B73EF1C2101890080A7B9 /* libcurl.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = D41B73EE1C2101890080A7B9 /* libcurl.tbd */; };
D41B741D1C210A7A0080A7B9 /* libiconv.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = D41B741C1C210A7A0080A7B9 /* libiconv.tbd */; };
D41B74731C2125E50080A7B9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D41B74721C2125E50080A7B9 /* Assets.xcassets */; };
@ -615,6 +616,7 @@
C6B5A7D11CDFE4CB00C9C006 /* S6Exporter.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; fileEncoding = 4; path = S6Exporter.h; sourceTree = "<group>"; };
C6B5A7D21CDFE4CB00C9C006 /* S6Importer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = S6Importer.cpp; sourceTree = "<group>"; };
C6B5A7D31CDFE4CB00C9C006 /* S6Importer.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; fileEncoding = 4; path = S6Importer.h; sourceTree = "<group>"; };
C6BDA0291DB3DFC900271C0A /* Printer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Printer.cpp; sourceTree = "<group>"; };
D41B73EE1C2101890080A7B9 /* libcurl.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libcurl.tbd; path = usr/lib/libcurl.tbd; sourceTree = SDKROOT; };
D41B741C1C210A7A0080A7B9 /* libiconv.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libiconv.tbd; path = usr/lib/libiconv.tbd; sourceTree = SDKROOT; };
D41B74721C2125E50080A7B9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Assets.xcassets; path = distribution/macos/Assets.xcassets; sourceTree = SOURCE_ROOT; };
@ -1178,6 +1180,7 @@
85AFA2101D7DB83E00221B42 /* intercept.h */,
85AFA20F1D7DB83E00221B42 /* main.cpp */,
C65A9EAE1DB2C01B003C3557 /* PaintIntercept.cpp */,
C6BDA0291DB3DFC900271C0A /* Printer.cpp */,
);
name = Paint;
path = test/testpaint;
@ -2393,6 +2396,7 @@
C64FDA7B1D6D9A2100F259B9 /* stand_up_roller_coaster.c in Sources */,
C64FDA7C1D6D9A2100F259B9 /* steeplechase.c in Sources */,
C64FDA7D1D6D9A2100F259B9 /* suspended_swinging_coaster.c in Sources */,
C6BDA02B1DB3DFD600271C0A /* Printer.cpp in Sources */,
C64FDA7E1D6D9A2100F259B9 /* twister_roller_coaster.c in Sources */,
C64FDA7F1D6D9A2100F259B9 /* vertical_drop_roller_coaster.c in Sources */,
C64FDA801D6D9A2100F259B9 /* virginia_reel.c in Sources */,

150
test/testpaint/Printer.cpp Normal file
View File

@ -0,0 +1,150 @@
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
#include <string>
#include "Printer.h"
class Printer {
public:
static std::string PrintFunction(function_call call, uint16 baseHeight) {
std::string imageId = GetImageIdString(call.paint.image_id);
switch (call.function) {
case SUPPORTS_WOOD_A:
return StringFormat("wooden_a_supports_paint_setup(%d, %d, %s, %s)", call.supports.type, call.supports.special,
GetHeightOffset(call.supports.height, baseHeight).c_str(), imageId.c_str());
case SUPPORTS_WOOD_B:
return StringFormat("wooden_b_supports_paint_setup(%d, %d, %s, %s)", call.supports.type, call.supports.special,
call.supports.height, imageId.c_str());
case SUPPORTS_METAL_A:
return StringFormat("metal_a_supports_paint_setup(%d, %d, %d, %s, %s)", call.supports.type,
call.supports.segment, call.supports.special, GetHeightOffset(call.supports.height, baseHeight).c_str(), imageId.c_str());
case SUPPORTS_METAL_B:
return StringFormat("metal_b_supports_paint_setup(%d, %d, %d, %s, %s)", call.supports.type,
call.supports.segment, call.supports.special, GetHeightOffset(call.supports.height, baseHeight).c_str(), imageId.c_str());
case SET_SEGMENT_HEIGHT:
return "paint_util_set_segment_support_height";
}
std::string functionCallName = "?";
switch (call.function) {
case PAINT_98196C:
functionCallName = "sub_98196C";
break;
case PAINT_98197C:
functionCallName = "sub_98197C";
break;
case PAINT_98198C:
functionCallName = "sub_98198C";
break;
case PAINT_98199C:
functionCallName = "sub_98199C";
break;
}
std::string s = StringFormat("%s(", functionCallName.c_str());
s += StringFormat("%s, ", imageId.c_str());
s += StringFormat("%d, %d, ", call.paint.offset.x, call.paint.offset.y);
s += StringFormat(
"%d, %d, %d, ",
call.paint.bound_box_length.x, call.paint.bound_box_length.y, call.paint.bound_box_length.z
);
s += StringFormat("%s, ", GetHeightOffset(call.paint.z_offset, baseHeight).c_str());
if (call.function != PAINT_98196C) {
s += StringFormat(
"%d, %d, %s, ",
call.paint.bound_box_offset.x, call.paint.bound_box_offset.y, GetHeightOffset(call.paint.bound_box_offset.z, baseHeight).c_str()
);
}
s += StringFormat("%d)", call.paint.rotation);
if (call.function != PAINT_98196C) {
s += StringFormat(
" = { %d, %d, %s }, { %d, %d, %s }, { %d, %d, %d }",
call.paint.offset.x, call.paint.offset.y, GetHeightOffset(call.paint.z_offset, baseHeight).c_str(),
call.paint.bound_box_offset.x, call.paint.bound_box_offset.y,
GetHeightOffset(call.paint.bound_box_offset.z, baseHeight).c_str(),
call.paint.bound_box_length.x, call.paint.bound_box_length.y, call.paint.bound_box_length.z);
}
return s;
}
private:
static std::string GetImageIdString(uint32 imageId)
{
std::string result;
uint32 image = imageId & 0x7FFFF;
uint32 palette = imageId & ~0x7FFFF;
std::string paletteName;
if (palette == Intercept2::DEFAULT_SCHEME_TRACK) paletteName = "SCHEME_TRACK";
else if (palette == Intercept2::DEFAULT_SCHEME_SUPPORTS) paletteName = "SCHEME_SUPPORTS";
else if (palette == Intercept2::DEFAULT_SCHEME_MISC) paletteName = "SCHEME_MISC";
else if (palette == Intercept2::DEFAULT_SCHEME_3) paletteName = "SCHEME_3";
else {
paletteName = StringFormat("0x%08X", palette);
}
if (image == 0) {
result = paletteName;
} else if (image & 0x70000) {
result = StringFormat("%s | vehicle.base_image_id + %d", paletteName.c_str(), image & ~0x70000);
} else {
result = StringFormat("%s | %d", paletteName.c_str(), image);
}
return result;
}
static std::string GetHeightOffset(uint16 height, uint16 baseHeight) {
int offset = height - baseHeight;
return StringFormat("height%s", GetOffsetExpressionString(offset).c_str());
}
static std::string GetOffsetExpressionString(int offset)
{
if (offset < 0) return std::string(" - ") + std::to_string(-offset);
if (offset > 0) return std::string(" + ") + std::to_string(offset);
return std::string();
}
static std::string StringFormat(const char *format, ...) {
va_list args;
char buffer[512];
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
return std::string(buffer);
}
};
extern "C" {
void printFunctionCall(utf8string out, size_t len, function_call call, uint16 baseHeight) {
snprintf(out, len, "%s", Printer::PrintFunction(call, baseHeight).c_str());
}
}

View File

@ -157,109 +157,10 @@ static bool assertFunctionCallArrayEquals(function_call expected[], uint8 expect
return true;
}
static void printImageId(uint32 input, utf8string out, size_t len) {
uint32 image = input & 0x7FFFF;
uint32 palette = input & ~0x7FFFF;
utf8string paletteName;
if (palette == DEFAULT_SCHEME_TRACK)paletteName = "SCHEME_TRACK";
else if (palette == DEFAULT_SCHEME_SUPPORTS)paletteName = "SCHEME_SUPPORTS";
else if (palette == DEFAULT_SCHEME_MISC)paletteName = "SCHEME_MISC";
else if (palette == DEFAULT_SCHEME_3)paletteName = "SCHEME_3";
else {
paletteName = malloc(16);
snprintf(paletteName, 16, "0x%08X", palette);
}
if (image == 0) {
snprintf(out, len, "%s", paletteName);
} else if (image & 0x70000) {
snprintf(out, len, "%s | vehicle.base_image_id + %d", paletteName, image & ~0x70000);
} else {
snprintf(out, len, "%s | %d", paletteName, image);
}
}
static void printFunctionCall(utf8string out, size_t len, function_call call) {
utf8string imageId = malloc(64);
printImageId(call.supports.colour_flags, imageId, 64);
switch (call.function) {
case SUPPORTS_WOOD_A:
snprintf(out, len, "wooden_a_supports_paint_setup(%d, %d, %d, %s)", call.supports.type, call.supports.special, call.supports.height, imageId);
return;
case SUPPORTS_WOOD_B:
snprintf(out, len, "wooden_b_supports_paint_setup(%d, %d, %d, %s)", call.supports.type, call.supports.special, call.supports.height, imageId);
return;
case SUPPORTS_METAL_A:
snprintf(out, len, "metal_a_supports_paint_setup(%d, %d, %d, %d, %s)", call.supports.type, call.supports.segment, call.supports.special, call.supports.height, imageId);
return;
case SUPPORTS_METAL_B:
snprintf(out, len, "metal_b_supports_paint_setup(%d, %d, %d, %d, %s)", call.supports.type, call.supports.segment, call.supports.special, call.supports.height, imageId);
return;
case SET_SEGMENT_HEIGHT:
snprintf(out, len, "paint_util_set_segment_support_height");
return;
}
utf8string name = "_default";
switch (call.function) {
case PAINT_98196C:
name = "sub_98196C";
break;
case PAINT_98197C:
name = "sub_98197C";
break;
case PAINT_98198C:
name = "sub_98198C";
break;
case PAINT_98199C:
name = "sub_98199C";
break;
}
size_t slen;
printImageId(call.paint.image_id, imageId, 64);
slen = snprintf(
out,
len,
"%s(%s, %d, %d, %d, %d, %d, %d, ",
name,
imageId,
call.paint.offset.x, call.paint.offset.y,
call.paint.bound_box_length.x, call.paint.bound_box_length.y, call.paint.bound_box_length.z,
call.paint.z_offset
);
if (slen >= len) return;
if (call.function != PAINT_98196C) {
if (slen < len)
slen += snprintf(
out + slen,
len - slen,
"%d, %d, %d, ",
call.paint.bound_box_offset.x, call.paint.bound_box_offset.y, call.paint.bound_box_offset.z
);
}
if (slen < len)
slen += snprintf(out + slen, len - slen, "%d)", call.paint.rotation);
if (call.function != PAINT_98196C) {
if (slen < len)
snprintf(out + slen, len - slen, " = { %d, %d, %d }, { %d, %d, %d }, { %d, %d, %d }",
call.paint.offset.x, call.paint.offset.y, call.paint.z_offset - 48,
call.paint.bound_box_offset.x, call.paint.bound_box_offset.y, call.paint.bound_box_offset.z - 48,
call.paint.bound_box_length.x, call.paint.bound_box_length.y, call.paint.bound_box_length.z);
}
}
static void printFunctionCallArray(utf8string out, size_t len, function_call calls[], uint8 count) {
static void printFunctionCallArray(utf8string out, size_t len, function_call calls[], uint8 count, uint16 baseHeight) {
for (int i = 0; i < count; i++) {
utf8string callOut = malloc(1024);
printFunctionCall(callOut, 1024, calls[i]);
printFunctionCall(callOut, 1024, calls[i], baseHeight);
size_t slen = strlen(out);
if (slen < len)
snprintf(out + slen, len - slen, "%s\n", callOut);
@ -410,12 +311,12 @@ static uint8 testTrackElement(uint8 rideType, uint8 trackType, utf8string error,
utf8string diff = malloc(2048);
snprintf(diff, 2048, "<<< EXPECTED\n");
printFunctionCallArray(diff, 2048, oldCalls, oldCallCount);
printFunctionCallArray(diff, 2048, oldCalls, oldCallCount, height);
size_t slen = strlen(diff);
if (slen < 2048)
snprintf(diff + slen, 2048 - slen, "====\n");
printFunctionCallArray(diff, 2048, newCalls, newCallCount);
printFunctionCallArray(diff, 2048, newCalls, newCallCount, height);
slen = strlen(diff);
if (slen < 2048)

View File

@ -95,6 +95,7 @@ extern "C"
void intercept_reset_segment_heights();
void intercept_reset_tunnels();
void intercept_simulate_wooden_supports(bool enabled);
void printFunctionCall(utf8string out, size_t len, function_call call, uint16 baseHeight);
bool assertFunctionCallEquals(function_call expected, function_call actual);
int generatePaintCode(uint8 rideType);

View File

@ -100,6 +100,7 @@
<ClCompile Include="intercept_2.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="PaintIntercept.cpp" />
<ClCompile Include="Printer.cpp" />
<ClCompile Include="..\..\src\addresses.c" />
<ClCompile Include="..\..\src\diagnostic.c" />
<ClCompile Include="..\..\src\hook.c" />