A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
KTestSystem.cc
Go to the documentation of this file.
1#include "KTestSystem.hh"
2
4
5#include <egg/core/Heap.hh>
6
9
10#include <abstract/File.hh>
11
12namespace Kinoko {
13
14// We use an unscoped enum to avoid static_casting in all usecases
15// This is defined in the source due to its lack of scoping
24
33
49
53 constexpr u32 TEST_HEADER_SIGNATURE = 0x54535448; // TSTH
54 constexpr u32 TEST_FOOTER_SIGNATURE = 0x54535446; // TSTF
55 constexpr u16 SUITE_MAJOR_VER = 1;
56 constexpr u16 SUITE_MAX_MINOR_VER = 0;
57
58 u16 numTestCases = m_stream.read_u16();
59 u16 testMajorVer = m_stream.read_u16();
60 u16 testMinorVer = m_stream.read_u16();
61
62 if (testMajorVer != SUITE_MAJOR_VER || testMinorVer > SUITE_MAX_MINOR_VER) {
63 PANIC("Version not supported! Provided file is %d.%d while Kinoko supports up to %d.%d",
64 testMajorVer, testMinorVer, SUITE_MAJOR_VER, SUITE_MAX_MINOR_VER);
65 }
66
67 for (u16 i = 0; i < numTestCases; ++i) {
68 // Validate alignment
69 if (m_stream.read_u32() != TEST_HEADER_SIGNATURE) {
70 PANIC("Invalid binary data for test case!");
71 }
72
73 u16 totalSize = m_stream.read_u16();
74 TestCase testCase;
75
76 u16 nameLen = m_stream.read_u16();
77 testCase.name = m_stream.read_string();
78 if (nameLen != testCase.name.size() + 1) {
79 PANIC("Test case name length mismatch!");
80 }
81
82 u16 rkgPathLen = m_stream.read_u16();
83 testCase.rkgPath = m_stream.read_string();
84 if (rkgPathLen != testCase.rkgPath.size() + 1) {
85 PANIC("Test case RKG Path length mismatch!");
86 }
87
88 u16 krkgPathLen = m_stream.read_u16();
89 testCase.krkgPath = m_stream.read_string();
90 if (krkgPathLen != testCase.krkgPath.size() + 1) {
91 PANIC("Test case KRKG Path length mismatch!");
92 }
93
94 testCase.targetFrame = m_stream.read_u16();
95
96 // Validate alignment
97 if (m_stream.read_u32() != TEST_FOOTER_SIGNATURE) {
98 PANIC("Invalid binary data for test case!");
99 }
100
101 if (totalSize != sizeof(u16) * 4 + nameLen + rkgPathLen + krkgPathLen) {
102 PANIC("Unexpected bytes in test case");
103 }
104
105 m_testCases.push(testCase);
106 }
107}
108
111 m_sceneMgr->calc();
112}
113
118 bool success = true;
119
120 while (true) {
121 success &= runTest();
122
123 if (!popTestCase()) {
124 break;
125 }
126
127 // TODO: Use a system heap! We currently have a dependency on the scene heap
131 }
132
133 return success;
134}
135
140void KTestSystem::parseOptions(int argc, char **argv) {
141 if (argc < 2) {
142 PANIC("Expected suite/ghost/krkg argument!");
143 }
144
145 std::optional<char *> rkgPath;
146 std::optional<char *> krkgPath;
147 std::optional<u16> target;
148
149 for (int i = 0; i < argc; ++i) {
150 std::optional<Host::EOption> flag = Host::Option::CheckFlag(argv[i]);
151 if (!flag || *flag == Host::EOption::Invalid) {
152 WARN("Expected a flag! Got: %s", argv[i]);
153 continue;
154 }
155
156 switch (*flag) {
159 PANIC("Mode was already set!");
160 }
161
163
164 ASSERT(i + 1 < argc);
165
166 size_t size;
167 u8 *data = Abstract::File::LoadHost(argv[++i], size);
168
169 if (size == 0) {
170 PANIC("Failed to load suite data!");
171 }
172
173 m_stream = EGG::RamStream(data, size);
174 m_stream.setEndian(std::endian::big);
175
176 } break;
179 PANIC("Mode was already set!");
180 }
181
183 ASSERT(i + 1 < argc);
184 rkgPath = argv[++i];
185
186 break;
189 PANIC("Mode was already set!");
190 }
191
193 ASSERT(i + 1 < argc);
194 krkgPath = argv[++i];
195
196 break;
198 ASSERT(i + 1 < argc);
199 {
200 if (strlen(argv[++i]) > 5) {
201 PANIC("Target has too many digits");
202 }
203 target = atoi(argv[i]);
204 if (target < 0 || target > std::numeric_limits<u16>::max()) {
205 PANIC("Target is out of bounds (expected 0-65535), got %d\n", *target);
206 }
207 }
208
209 break;
211 default:
212 PANIC("Invalid flag!");
213 break;
214 }
215 }
216
217 if (target && m_testMode != Host::EOption::Ghost) {
218 PANIC("'--framecount' is only supported in a single ghost test");
219 }
220
222 if (!rkgPath) {
223 PANIC("Missing ghost argument!");
224 }
225
226 if (!krkgPath) {
227 PANIC("Missing KRKG argument!");
228 }
229
230 if (!target) {
231 target = 0;
232 }
233
234 m_testCases.emplace(*rkgPath, *rkgPath, *krkgPath, *target);
235 }
236}
237
243
246 auto *instance = s_instance;
247 s_instance = nullptr;
248 EGG::egg_delete(instance);
249}
250
251KTestSystem::KTestSystem() : m_testMode(Host::EOption::Invalid) {}
252
254 if (s_instance) {
255 s_instance = nullptr;
256 WARN("KTestSystem instance not explicitly handled!");
257 }
258}
259
262 constexpr u32 KRKG_SIGNATURE = 0x4b524b47; // KRKG
263
264 size_t size;
265 u8 *krkg = Abstract::File::LoadHost(getCurrentTestCase().krkgPath.data(), size);
266 m_stream = EGG::RamStream(krkg, static_cast<u32>(size));
267 m_currentFrame = -1;
268 m_sync = true;
269
270 // Initialize endianness for the RAM stream
271 u16 mark = reinterpret_cast<TestHeader *>(krkg)->byteOrderMark;
272 std::endian endian = parse<u16>(mark) == 0xfeff ? std::endian::big : std::endian::little;
273 m_stream.setEndian(endian);
274
275 ASSERT(m_stream.read_u32() == KRKG_SIGNATURE);
276 m_stream.skip(2);
280
282
283 // If we're in Ghost mode instead of Suite mode and framecount not specified, then target the
284 // total framecount of the KRKG.
286 ASSERT(m_testCases.size() == 1);
287 auto &front = m_testCases.front();
288 if (front.targetFrame == 0) {
289 front.targetFrame = m_frameCount;
290 }
291
292 front.targetFrame = std::min(front.targetFrame, m_frameCount);
293 }
294}
295
299 ASSERT(m_testCases.size() > 0);
300 m_testCases.pop();
301 delete[] m_stream.data();
302
303 return !m_testCases.empty();
304}
305
310
311 // Check if we're out of frames
312 u16 targetFrame = getCurrentTestCase().targetFrame;
313 ASSERT(targetFrame <= m_frameCount);
314 if (m_currentFrame > targetFrame) {
315 REPORT("Test Case Passed: %s [%d / %d]", getCurrentTestCase().name.c_str(), targetFrame,
317 return false;
318 }
319
320 // Test the current frame
322 return m_sync;
323}
324
328 EGG::Vector3f pos;
329 EGG::Quatf fullRot;
330 EGG::Vector3f extVel;
331 EGG::Vector3f intVel;
332 f32 speed = 0.0f;
333 f32 acceleration = 0.0f;
334 f32 softSpeedLimit = 0.0f;
335 EGG::Quatf mainRot = EGG::Quatf::ident; // Initialize to avoid maybe-uninitialized warning
336 EGG::Vector3f angVel2;
337 f32 raceCompletion = 0.0f;
338 u16 checkpointId = 0;
339 u8 jugemId = 0;
340
341 pos.read(m_stream);
342 fullRot.read(m_stream);
343
345 extVel.read(m_stream);
346 }
347
349 intVel.read(m_stream);
350 }
351
353 speed = m_stream.read_f32();
354 acceleration = m_stream.read_f32();
355 softSpeedLimit = m_stream.read_f32();
356 }
357
359 mainRot.read(m_stream);
360 angVel2.read(m_stream);
361 }
362
364 raceCompletion = m_stream.read_f32();
365 checkpointId = m_stream.read_u16();
366 jugemId = m_stream.read_u8();
367 m_stream.skip(1);
368 }
369
370 TestData data;
371 data.pos = pos;
372 data.fullRot = fullRot;
373 data.extVel = extVel;
374 data.intVel = intVel;
375 data.speed = speed;
376 data.acceleration = acceleration;
377 data.softSpeedLimit = softSpeedLimit;
378 data.mainRot = mainRot;
379 data.angVel2 = angVel2;
380 data.raceCompletion = raceCompletion;
381 data.checkpointId = checkpointId;
382 data.jugemId = jugemId;
383 return data;
384}
385
389 auto *object = Kart::KartObjectManager::Instance()->object(0);
390 const auto &pos = object->pos();
391 const auto &fullRot = object->fullRot();
392 const auto &extVel = object->extVel();
393 const auto &intVel = object->intVel();
394 f32 speed = object->speed();
395 f32 acceleration = object->acceleration();
396 f32 softSpeedLimit = object->softSpeedLimit();
397 const auto &mainRot = object->mainRot();
398 const auto &angVel2 = object->angVel2();
399
400 const auto &player = System::RaceManager::Instance()->player();
401 f32 raceCompletion = player.raceCompletion();
402 u16 checkpointId = player.checkpointId();
403 u8 jugemId = player.jugemId();
404
405 switch (m_versionMinor) {
407 checkDesync(data.raceCompletion, raceCompletion, "raceCompletion");
408 checkDesync(data.checkpointId, checkpointId, "checkpointId");
409 checkDesync(data.jugemId, jugemId, "jugemId");
410 [[fallthrough]];
412 checkDesync(data.mainRot, mainRot, "mainRot");
413 checkDesync(data.angVel2, angVel2, "angVel2");
414 [[fallthrough]];
416 checkDesync(data.speed, speed, "speed");
417 checkDesync(data.acceleration, acceleration, "acceleration");
418 checkDesync(data.softSpeedLimit, softSpeedLimit, "softSpeedLimit");
419 [[fallthrough]];
421 checkDesync(data.intVel, intVel, "intVel");
422 [[fallthrough]];
424 checkDesync(data.extVel, extVel, "extVel");
425 [[fallthrough]];
426 default:
427 checkDesync(data.pos, pos, "pos");
428 checkDesync(data.fullRot, fullRot, "fullRot");
429 }
430}
431
436 while (calcTest()) {
437 calc();
438 }
439
440 // TODO: Use a system heap! std::string relies on heap allocation
441 // The heap is destroyed after this and there is no further allocation, so it's not re-disabled
444 return m_sync;
445}
446
450 std::string outStr(getCurrentTestCase().name.data());
451 outStr += "\n" + std::string(m_sync ? "1" : "0") + "\n";
452 outStr += std::to_string(getCurrentTestCase().targetFrame) + "\n";
453 outStr += std::to_string(m_frameCount) + "\n";
454 Abstract::File::Append("results.txt", outStr.c_str(), outStr.size());
455}
456
461 ASSERT(!m_testCases.empty());
462 return m_testCases.front();
463}
464
468void KTestSystem::OnInit(System::RaceConfig *config, void * /* arg */) {
469 size_t size;
470 u8 *rkg = Abstract::File::LoadHost(Instance()->getCurrentTestCase().rkgPath.data(), size);
471 config->setGhost(rkg);
472 delete[] rkg;
473
475}
476
477} // namespace Kinoko
#define ASSERT(e)
Definition Logger.hh:25
#define REPORT(m,...)
Definition Logger.hh:38
#define PANIC(m,...)
Definition Logger.hh:27
#define WARN(m,...)
Definition Logger.hh:33
void enableAllocation()
Definition Heap.hh:52
A stream of data stored in memory.
Definition Stream.hh:81
const char * read_string()
Definition Stream.cc:122
Scene * currentScene() const
void changeScene(int nextSceneId)
void createScene(int nextSceneId, Scene *parent)
void destroyScene(Scene *scene)
Heap * heap() const
Definition Scene.hh:61
void setEndian(std::endian endian)
Definition Stream.hh:22
void skip(u32 count)
Definition Stream.cc:11
u32 index() const
Definition Stream.hh:26
static KSystem * s_instance
Definition KSystem.hh:33
void checkDesync(const T &t0, const T &t1, const char *name)
Host::EOption m_testMode
Differentiates between test suite and ghost+krkg.
static KTestSystem * CreateInstance()
bool run() override
Executes a run.
void parseOptions(int argc, char **argv) override
Parses non-generic command line options.
static void OnInit(System::RaceConfig *config, void *arg)
Initializes the race configuration as needed for test cases.
const TestCase & getCurrentTestCase() const
Gets the current test case.
EGG::SceneManager * m_sceneMgr
void startNextTestCase()
Starts the next test case.
EGG::RamStream m_stream
void writeTestOutput() const
Writes details about the current test to file.
static void DestroyInstance()
void testFrame(const TestData &data)
Tests the frame against the provided test data.
void calc() override
Executes a frame.
TestData findCurrentFrameEntry()
Finds the test data of the current frame.
std::queue< TestCase, std::deque< TestCase, EGG::Allocator< TestCase > > > m_testCases
bool runTest()
Runs a single test case, and ends when the test is finished or when a desync is found.
void init() override
Initializes the system.
bool popTestCase()
Pops the current test case and frees the KRKG buffer.
static KTestSystem * Instance()
bool calcTest()
Checks one frame in the test.
~KTestSystem() override
KartObject * object(size_t i) const
static KartObjectManager * Instance()
const EGG::Vector3f & pos() const
Initializes the player with parameters specified in the provided ghost file.
Definition RaceConfig.hh:23
const Scenario & raceScenario() const
Definition RaceConfig.hh:62
void setGhost(const u8 *rkg)
Definition RaceConfig.hh:70
static void RegisterInitCallback(const InitCallback &callback, void *arg)
Definition RaceConfig.hh:74
static RaceManager * Instance()
const Player & player() const
int Remove(const char *path)
Definition File.cc:83
void Append(const char *path, const char *data, size_t size)
Definition File.cc:77
u8 * LoadHost(const char *path, size_t &size)
Loads files from host system.
Definition File.cc:57
void egg_delete(const T *ptr)
Definition Heap.hh:154
T * egg_new(Args &&...args)
Definition Heap.hh:148
std::optional< EOption > CheckFlag(const char *arg)
Definition Option.cc:7
Represents the host application.
Definition HeapCommon.hh:11
float f32
Definition Memory.hh:21
uint8_t u8
Definition Memory.hh:16
uint16_t u16
Definition Memory.hh:17
@ AddedCheckpoints
@ AddedExtVel
@ AddedRotation
@ AddedIntVel
uint32_t u32
Definition Memory.hh:18
A quaternion, used to represent 3D rotation.
Definition Quat.hh:12
static const Quatf ident
Definition Quat.hh:259
void read(Stream &stream)
Definition Quat.hh:238
A 3D float vector.
Definition Vector.hh:108
void read(Stream &stream)
Initializes a Vector3f by reading 12 bytes from the stream.
Definition Vector.hh:376
std::array< Player, 12 > players
Definition RaceConfig.hh:50