|
| 1 | +#include "KReplaySystem.hh" |
| 2 | + |
| 3 | +#include "host/Option.hh" |
| 4 | +#include "host/SceneCreatorDynamic.hh" |
| 5 | + |
| 6 | +#include <abstract/File.hh> |
| 7 | + |
| 8 | +#include <game/system/RaceManager.hh> |
| 9 | + |
| 10 | +/// @brief Initializes the system. |
| 11 | +void KReplaySystem::init() { |
| 12 | + ASSERT(m_currentGhostFileName); |
| 13 | + ASSERT(m_currentRawGhost); |
| 14 | + ASSERT(m_currentGhost); |
| 15 | + |
| 16 | + auto *sceneCreator = new Host::SceneCreatorDynamic; |
| 17 | + m_sceneMgr = new EGG::SceneManager(sceneCreator); |
| 18 | + |
| 19 | + System::RaceConfig::RegisterInitCallback(OnInit, nullptr); |
| 20 | + Abstract::File::Remove("results.txt"); |
| 21 | + |
| 22 | + m_sceneMgr->changeScene(0); |
| 23 | +} |
| 24 | + |
| 25 | +/// @brief Executes a frame. |
| 26 | +void KReplaySystem::calc() { |
| 27 | + m_sceneMgr->calc(); |
| 28 | +} |
| 29 | + |
| 30 | +/// @brief Executes a run. |
| 31 | +/// @details A run consists of replaying a ghost. |
| 32 | +/// @return Whether the run was successful or not. |
| 33 | +bool KReplaySystem::run() { |
| 34 | + while (!calcEnd()) { |
| 35 | + calc(); |
| 36 | + } |
| 37 | + |
| 38 | + return success(); |
| 39 | +} |
| 40 | + |
| 41 | +/// @brief Parses non-generic command line options. |
| 42 | +/// @details The only currently accepted option is the ghost flag. |
| 43 | +/// @param argc The number of arguments. |
| 44 | +/// @param argv The arguments. |
| 45 | +void KReplaySystem::parseOptions(int argc, char **argv) { |
| 46 | + if (argc < 2) { |
| 47 | + PANIC("Expected ghost argument!"); |
| 48 | + } |
| 49 | + |
| 50 | + for (int i = 0; i < argc; ++i) { |
| 51 | + std::optional<Host::EOption> flag = Host::Option::CheckFlag(argv[i]); |
| 52 | + if (!flag || *flag == Host::EOption::Invalid) { |
| 53 | + WARN("Expected a flag! Got: %s", argv[i]); |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + switch (*flag) { |
| 58 | + case Host::EOption::Ghost: { |
| 59 | + ASSERT(i + 1 < argc); |
| 60 | + |
| 61 | + m_currentGhostFileName = argv[++i]; |
| 62 | + m_currentRawGhost = Abstract::File::Load(m_currentGhostFileName, m_currentRawGhostSize); |
| 63 | + |
| 64 | + if (m_currentRawGhostSize < System::RKG_HEADER_SIZE || |
| 65 | + m_currentRawGhostSize > System::RKG_UNCOMPRESSED_INPUT_DATA_SECTION_SIZE) { |
| 66 | + PANIC("File cannot be a ghost! Check the file size."); |
| 67 | + } |
| 68 | + |
| 69 | + // Creating the raw ghost file validates it |
| 70 | + System::RawGhostFile file = System::RawGhostFile(m_currentRawGhost); |
| 71 | + |
| 72 | + m_currentGhost = new System::GhostFile(file); |
| 73 | + ASSERT(m_currentGhost); |
| 74 | + } break; |
| 75 | + case Host::EOption::Invalid: |
| 76 | + default: |
| 77 | + PANIC("Invalid flag!"); |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +KReplaySystem *KReplaySystem::CreateInstance() { |
| 84 | + ASSERT(!s_instance); |
| 85 | + s_instance = new KReplaySystem; |
| 86 | + return static_cast<KReplaySystem *>(s_instance); |
| 87 | +} |
| 88 | + |
| 89 | +void KReplaySystem::DestroyInstance() { |
| 90 | + ASSERT(s_instance); |
| 91 | + auto *instance = s_instance; |
| 92 | + s_instance = nullptr; |
| 93 | + delete instance; |
| 94 | +} |
| 95 | + |
| 96 | +KReplaySystem *KReplaySystem::Instance() { |
| 97 | + return static_cast<KReplaySystem *>(s_instance); |
| 98 | +} |
| 99 | + |
| 100 | +KReplaySystem::KReplaySystem() |
| 101 | + : m_currentGhostFileName(nullptr), m_currentGhost(nullptr), m_currentRawGhost(nullptr), |
| 102 | + m_currentRawGhostSize(0) {} |
| 103 | + |
| 104 | +KReplaySystem::~KReplaySystem() { |
| 105 | + if (s_instance) { |
| 106 | + s_instance = nullptr; |
| 107 | + WARN("KReplaySystem instance not explicitly handled!"); |
| 108 | + } |
| 109 | + |
| 110 | + delete m_sceneMgr; |
| 111 | + delete m_currentGhost; |
| 112 | + delete m_currentRawGhost; |
| 113 | +} |
| 114 | + |
| 115 | +bool KReplaySystem::calcEnd() const { |
| 116 | + constexpr u16 MAX_MINUTE_COUNT = 10; |
| 117 | + |
| 118 | + const auto *raceManager = System::RaceManager::Instance(); |
| 119 | + if (raceManager->stage() == System::RaceManager::Stage::FinishGlobal) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + if (raceManager->timerManager().currentTimer().min >= MAX_MINUTE_COUNT) { |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + return false; |
| 128 | +} |
| 129 | + |
| 130 | +void KReplaySystem::reportFail(const char *msg) const { |
| 131 | + std::string report(m_currentGhostFileName); |
| 132 | + report += "\n" + std::string(msg); |
| 133 | + Abstract::File::Append("results.txt", report.c_str(), report.size()); |
| 134 | +} |
| 135 | + |
| 136 | +bool KReplaySystem::success() const { |
| 137 | + const auto *raceManager = System::RaceManager::Instance(); |
| 138 | + if (raceManager->stage() != System::RaceManager::Stage::FinishGlobal) { |
| 139 | + reportFail("Race didn't finish"); |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + s32 desyncingTimerIdx = getDesyncingTimerIdx(); |
| 144 | + if (desyncingTimerIdx != -1) { |
| 145 | + char msgBuffer[128]; |
| 146 | + |
| 147 | + const auto [correct, incorrect] = getDesyncingTimer(desyncingTimerIdx); |
| 148 | + if (desyncingTimerIdx == 0) { |
| 149 | + snprintf(msgBuffer, sizeof(msgBuffer), |
| 150 | + "Final timer desync! Expected [%d:%d:%d], got [%d:%d:%d]", correct.min, |
| 151 | + correct.sec, correct.mil, incorrect.min, incorrect.sec, incorrect.mil); |
| 152 | + } else { |
| 153 | + snprintf(msgBuffer, sizeof(msgBuffer), |
| 154 | + "Lap %d timer desync! Expected [%d:%d:%d], got [%d:%d:%d]", desyncingTimerIdx, |
| 155 | + correct.min, correct.sec, correct.mil, incorrect.min, incorrect.sec, |
| 156 | + incorrect.mil); |
| 157 | + } |
| 158 | + reportFail(msgBuffer); |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + return true; |
| 163 | +} |
| 164 | + |
| 165 | +/// @brief Finds the desyncing timer index, if one exists. |
| 166 | +/// @return -1 if there's no desync, 0 if the final timer desyncs, and 1+ if a lap timer desyncs. |
| 167 | +s32 KReplaySystem::getDesyncingTimerIdx() const { |
| 168 | + const auto &player = System::RaceManager::Instance()->player(); |
| 169 | + if (m_currentGhost->raceTimer() != player.raceTimer()) { |
| 170 | + return 0; |
| 171 | + } |
| 172 | + |
| 173 | + for (size_t i = 0; i < 3; ++i) { |
| 174 | + if (m_currentGhost->lapTimer(i) != player.getLapSplit(i + 1)) { |
| 175 | + return i + 1; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return -1; |
| 180 | +} |
| 181 | + |
| 182 | +/// @brief Gets the desyncing timer according to the index. |
| 183 | +/// @param i Index to the desyncing timer. Cannot be -1. |
| 184 | +/// @return The pair of timers. The first is the correct one, and the second is the incorrect one. |
| 185 | +std::pair<const System::Timer &, const System::Timer &> KReplaySystem::getDesyncingTimer( |
| 186 | + s32 i) const { |
| 187 | + auto cond = i <=> 0; |
| 188 | + ASSERT(cond != std::strong_ordering::less); |
| 189 | + |
| 190 | + if (cond == std::strong_ordering::equal) { |
| 191 | + const auto &correct = m_currentGhost->raceTimer(); |
| 192 | + const auto &incorrect = System::RaceManager::Instance()->player().raceTimer(); |
| 193 | + ASSERT(correct != incorrect); |
| 194 | + return std::pair<const System::Timer &, const System::Timer &>(correct, incorrect); |
| 195 | + } else if (cond == std::strong_ordering::greater) { |
| 196 | + const auto &correct = m_currentGhost->lapTimer(i - 1); |
| 197 | + const auto &incorrect = System::RaceManager::Instance()->player().lapTimer(i - 1); |
| 198 | + ASSERT(correct != incorrect); |
| 199 | + return std::pair<const System::Timer &, const System::Timer &>(correct, incorrect); |
| 200 | + } |
| 201 | + |
| 202 | + // This is unreachable |
| 203 | + return std::pair(System::Timer(), System::Timer()); |
| 204 | +} |
| 205 | + |
| 206 | +void KReplaySystem::OnInit(System::RaceConfig *config, void * /* arg */) { |
| 207 | + config->setGhost(Instance()->m_currentRawGhost); |
| 208 | + config->raceScenario().players[0].type = System::RaceConfig::Player::Type::Ghost; |
| 209 | +} |
0 commit comments