Skip to content

Check it yourself

stepmaniay-verification Public

Tests anyone can run against StepManiaY's published builds and ITGmania 1.3.0, the release it grew from.

Clone

Clone with HTTPS

Plain git over HTTPS: no account, no app, nothing else to install.

Download the source tarball 3.0 MB

154 lines · 7.0 KB

Raw
"""Adds StepManiaY's timing harness, and nothing else, to an ITGmania 1.3.0 checkout: the
self-test that presses the panels through the input system at exact moments (TimingSelfTest) and
the flight recorder that writes every judged step and miss to Logs/timing-<time>.csv
(TimingRecorder), with the few lines that hook them into gameplay. None of StepManiaY's fixes come
with it: ITGmania judges every step its own way, and the recorder only writes down what it decided.

    python3 make_patch.py <ITGmania 1.3.0 checkout> <StepManiaY engine source>

Then `git diff` in the checkout is tests/timing/itgmania-1.3.0-timing-harness.patch. Flags, the
same on both engines: --smytest=timing turns the self-test on when gameplay starts;
--timingoffsets=16 (ms, late positive, taken in turn row by row), --timingrate=1.5 and
--timingstutter=60 (one frame that long every second) as in StepManiaY.
"""

from __future__ import annotations

import shutil
import sys
from pathlib import Path


def edit(path: Path, before: str, after: str) -> None:
    text = path.read_text()
    assert text.count(before) == 1, f"{path.name}: {before[:70]!r}"
    path.write_text(text.replace(before, after))


def main() -> None:
    itg, smy = Path(sys.argv[1]), Path(sys.argv[2])
    src = itg / "src"
    for name in ("TimingSelfTest.h", "TimingSelfTest.cpp", "TimingRecorder.h", "TimingRecorder.cpp"):
        shutil.copy(smy / "src" / name, src / name)

    edit(src / "CMakeData-actor.cmake", '            "Player.cpp"', '            "Player.cpp"\n            "TimingRecorder.cpp"\n            "TimingSelfTest.cpp"')

    player = src / "Player.cpp"
    edit(player, '#include "Player.h"\n', '#include "Player.h"\n#include "TimingRecorder.h"\n')
    edit(player, "void Player::Step(\n    int col, int row, const RageTimer& tm, bool bHeld, bool bRelease) {",
         """// The flight recorder's line for a judged step (TimingRecorder).
static void RecordStep(const PlayerState* ps, int col, int row, int iSearchRow, bool bRowGiven,
                       bool bHeld, bool bRelease, const RageTimer& tm,
                       float fStepMusicSeconds, float fNoteSeconds, float fOffset,
                       TapNoteScore tns) {
  if (!TimingRecorder::Enabled()) {
    return;
  }
  TimingRecorder::StepRecord r;
  r.pn = ps->m_PlayerNumber;
  r.iCol = col;
  r.iRow = row;
  r.iSearchRow = iSearchRow;
  r.bRowGiven = bRowGiven;
  r.bHeld = bHeld;
  r.bRelease = bRelease;
  r.iInputUs = TimingRecorder::Microseconds(tm);
  r.iNowUs = TimingRecorder::Microseconds(RageTimer());
  r.iUpdateUs = TimingRecorder::Microseconds(ps->m_Position.m_LastBeatUpdate);
  r.fMusicSecondsAtUpdate = ps->m_Position.m_fMusicSeconds;
  r.fStepMusicSeconds = fStepMusicSeconds;
  r.fNoteSeconds = fNoteSeconds;
  r.fOffset = fOffset;
  r.tns = tns;
  TimingRecorder::Step(r);
}

void Player::Step(
    int col, int row, const RageTimer& tm, bool bHeld, bool bRelease) {""")
    edit(player, "    const float fStepSeconds = m_Timing->GetElapsedTimeFromBeat(fStepBeat);\n",
         "    const float fStepSeconds = m_Timing->GetElapsedTimeFromBeat(fStepBeat);\n    float fStepMusicSeconds = fStepSeconds;\n")
    edit(player, """      // The offset from the actual step in seconds:
      fNoteOffset = (fStepSeconds - fMusicSeconds) /""", """      fStepMusicSeconds = fMusicSeconds;
      // The offset from the actual step in seconds:
      fNoteOffset = (fStepSeconds - fMusicSeconds) /""")
    edit(player, "    m_LastTapNoteScore = score;\n    if (GAMESTATE->GetCurrentGame()->m_bCountNotesSeparately) {",
         "    m_LastTapNoteScore = score;\n    RecordStep(m_pPlayerState, col, iRowOfOverlappingNoteOrRow, iSongRow, row != -1, bHeld,\n"
         "               bRelease, tm, fStepMusicSeconds, fStepSeconds, fNoteOffset, score);\n"
         "    if (GAMESTATE->GetCurrentGame()->m_bCountNotesSeparately) {")
    edit(player, """      FlashGhostRow(iRowOfOverlappingNoteOrRow);
    }
  }

  if (score == TNS_None) {""", """      FlashGhostRow(iRowOfOverlappingNoteOrRow);
    }
  } else if (!bRelease) {
    RecordStep(m_pPlayerState, col, -1, iSongRow, row != -1, bHeld, bRelease, tm, 0, 0, 0,
               TNS_None);
  }

  if (score == TNS_None) {""")
    edit(player, """      } else {
        tn.result.tns = TNS_Miss;
      }
    }""", """      } else {
        tn.result.tns = TNS_Miss;
      }
      TimingRecorder::Miss(
          m_pPlayerState->m_PlayerNumber, iter.Track(), iter.Row(),
          m_Timing->GetElapsedTimeFromBeat(NoteRowToBeat(iter.Row())),
          m_pPlayerState->m_Position.m_fMusicSeconds,
          TimingRecorder::Microseconds(RageTimer()));
    }""")

    gameplay = src / "ScreenGameplay.cpp"
    edit(gameplay, '#include "Song.h"\n', '#include "Song.h"\n#include "StepMania.h"\n#include "TimingRecorder.h"\n#include "TimingSelfTest.h"\n')
    edit(gameplay, "void ScreenGameplay::Init() {\n", """void ScreenGameplay::Init() {
  // --smytest=timing: the timing self-test plays this stage (TimingSelfTest).
  std::string sTest;
  if (GetCommandlineArgument("smytest", &sTest) && sTest == "timing") {
    std::string sOffsets, sRate, sStutter;
    GetCommandlineArgument("timingoffsets", &sOffsets);
    GetCommandlineArgument("timingrate", &sRate);
    GetCommandlineArgument("timingstutter", &sStutter);
    std::vector<std::string> vsOffsets;
    split(sOffsets, ",", vsOffsets);
    std::vector<float> vOffsets;
    for (const std::string& s : vsOffsets) {
      vOffsets.push_back(StringToFloat(s));
    }
    if (!sRate.empty()) {
      SongOptions so = GAMESTATE->m_SongOptions.GetPreferred();
      so.m_fMusicRate = StringToFloat(sRate);
      GAMESTATE->m_SongOptions.Assign(ModsLevel_Preferred, so);
    }
    TimingSelfTest::Start(vOffsets, sStutter.empty() ? 0.0f : StringToFloat(sStutter));
  }
""")
    edit(gameplay, '  LOG->Trace("ScreenGameplay::~ScreenGameplay()");\n', """  LOG->Trace("ScreenGameplay::~ScreenGameplay()");
  if (TimingRecorder::Enabled()) {
    const Song* pSong = GAMESTATE->m_pCurSong;
    TimingRecorder::Flush(ssprintf(
        "%s, rate %.2f", pSong != nullptr ? pSong->GetDisplayFullTitle().c_str() : "no song",
        GAMESTATE->m_SongOptions.GetCurrent().m_fMusicRate));
  }
  TimingSelfTest::Stop();
""")
    edit(gameplay, """  const float fSeconds = m_pSoundMusic->GetPositionSeconds(&tm);
  GAMESTATE->UpdateSongPosition(""", """  const float fSeconds = m_pSoundMusic->GetPositionSeconds(&tm);
  TimingRecorder::PositionUpdate(TimingRecorder::Microseconds(tm), fSeconds);
  GAMESTATE->UpdateSongPosition(""")
    edit(gameplay, "  UpdateSongPosition(fDeltaTime);\n", """  UpdateSongPosition(fDeltaTime);
  if (TimingSelfTest::Active() && m_pSoundMusic->IsPlaying() &&
      GAMESTATE->m_pCurSteps[PLAYER_1] != nullptr) {
    RageTimer tm;
    const float fSeconds = m_pSoundMusic->GetPositionSeconds(&tm);
    TimingSelfTest::Update(PLAYER_1, m_vPlayerInfo[PLAYER_1].m_pPlayer->GetNoteData(),
                           *GAMESTATE->m_pCurSteps[PLAYER_1]->GetTimingData(), fSeconds, tm,
                           GAMESTATE->m_SongOptions.GetCurrent().m_fMusicRate);
  }
""")
    print("patched", itg)


if __name__ == "__main__":
    main()