diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index 906540637b..644d66a3fb 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -19,8 +19,10 @@ #include "Engine/Pokemon_StoryEngine_Tests.h" #include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h" #include "Pokemon_AutoStorySelect_Tests.h" +#include "Pokemon_FrameReplay_Tests.h" #include "Pokemon_StoryRun_Tests.h" #include "Pokemon_AutoStoryStats_Tests.h" +#include "Pokemon_FrameReplay_Tests.h" #include "Pokemon_Interrupts_Tests.h" #include "Pokemon_SceneJudges_Tests.h" #include "Pokemon_ScreenEvents_Tests.h" @@ -59,6 +61,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_SceneJudges(database); add_tests_Interrupts(database); add_tests_Transitions(database); + add_tests_FrameReplay(database); add_tests_StoryActions(database); add_tests_StoryInterrupts(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FrameReplay.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FrameReplay.cpp new file mode 100644 index 0000000000..e1b9df7eac --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FrameReplay.cpp @@ -0,0 +1,121 @@ +/* Frame Replay + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Pokemon_FrameReplay.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +// The time a frame file's name gives ("1250.png" -> 1250), or -1 when the +// name is not all digits or the file not a .png. +long long frame_time_of(const Filesystem::Path& file){ + if (file.extension().string() != ".png"){ + return -1; + } + const std::string stem = file.stem().string(); + if (stem.empty() || stem.size() > 9){ + return -1; + } + for (char c : stem){ + if (!std::isdigit((unsigned char)c)){ + return -1; + } + } + return std::stoll(stem); +} + +} // namespace + + +ReplayFrameSource::ReplayFrameSource(std::vector frames) + : m_frames(std::move(frames)) +{ + std::stable_sort(m_frames.begin(), m_frames.end(), [](const ReplayFrame& a, const ReplayFrame& b){ + return a.at < b.at; + }); +} + +ReplayFrameSource ReplayFrameSource::from_folder(const std::string& folder){ + std::vector frames; + if (Filesystem::is_directory(folder)){ + for (const Filesystem::Path& file : Filesystem::list_directory(folder)){ + const long long ms = frame_time_of(file); + if (ms >= 0){ + frames.push_back(ReplayFrame{.at = Milliseconds(ms), .file = file.string()}); + } + } + } + if (frames.empty()){ + throw FileException(nullptr, PA_CURRENT_FUNCTION, "No .png frames to replay.", folder); + } + return ReplayFrameSource(std::move(frames)); +} + +Milliseconds ReplayFrameSource::last_frame() const{ + return m_frames.empty() ? Milliseconds(0) : m_frames.back().at; +} + +VideoSnapshot ReplayFrameSource::frame_at(Milliseconds at, WallClock origin) const{ + auto after = std::upper_bound(m_frames.begin(), m_frames.end(), at, [](Milliseconds time, const ReplayFrame& frame){ + return time < frame.at; + }); + if (after == m_frames.begin()){ + return VideoSnapshot(); + } + ReplayFrame& frame = *(after - 1); + if (!frame.image){ + frame.image = std::make_shared(frame.file); + } + VideoSnapshot out; + out.frame = frame.image; + out.timestamp = origin + at; + return out; +} + + +ReplayHit replay_until( + const ReplayFrameSource& source, ReplayWindow window, + const std::vector& callbacks, + WallClock origin, Milliseconds period +){ + for (Milliseconds now = window.start; now < window.end; now += period){ + const VideoSnapshot frame = source.frame_at(now, origin); + if (!frame){ + continue; + } + for (size_t c = 0; c < callbacks.size(); c++){ + if (callbacks[c]->process_frame(frame)){ + return ReplayHit{.index = (int)c, .at = now}; + } + } + } + return ReplayHit{}; +} + +ScreenEventLog replay_events( + const ReplayFrameSource& source, ReplayWindow window, + ScreenEventObserver& observer, + WallClock origin, Milliseconds period +){ + ScreenEventLog log; + for (Milliseconds now = window.start; now < window.end; now += period){ + const VideoSnapshot frame = source.frame_at(now, origin); + log.record(observer.observe(frame, origin + now), origin + now); + } + return log; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FrameReplay.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FrameReplay.h new file mode 100644 index 0000000000..f3d56e386a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FrameReplay.h @@ -0,0 +1,125 @@ +/* Frame Replay + * + * From: https://github.com/PokemonAutomation/ + * + * Recorded console frames, replayed through the real detectors on a + * virtual clock, so a console failure becomes a deterministic test. The + * story sims script what a screen shows (a label per frame); a replay + * shows what the console showed and lets the game's own watchers and + * ScreenEventObserver judge it. + * + * A recording is a folder of frames named by their time in ms from its + * start ("0.png", "1250.png"), as the captured transitions + * (Pokemon_Transitions_Tests.cpp). A frame shows until the next one. + * Recordings are too big for this repo; tests read them from + * CommandLineTests//FrameReplay// and skip when it is missing. + * + * From the stream history (an error report's Video.mp4, kept when the + * "Stream History" setting is on), cut the seconds around the failure at + * 20 frames a second, then rename frame N to its time, (N - 1) * 50 ms: + * ffmpeg -ss -t -i Video.mp4 -vf fps=20 %d.png + * + * A failed AutoStory event saves its own (Pokemon_AutoStoryFailureCapture.h) + * in USER_FILE_PATH()//AutoStoryFailures/