aboutsummaryrefslogtreecommitdiff
path: root/src/core/movie.h
blob: 729bc459b83de07ee2b6e9767b8f0501ee6aa248 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <functional>
#include <span>
#include <boost/serialization/vector.hpp>
#include "common/common_types.h"

namespace Service {
namespace HID {
struct AccelerometerDataEntry;
struct GyroscopeDataEntry;
struct PadState;
struct TouchDataEntry;
} // namespace HID
namespace IR {
struct ExtraHIDResponse;
union PadState;
} // namespace IR
} // namespace Service

namespace Core {

class System;
struct CTMHeader;
struct ControllerState;

class Movie {
public:
    enum class PlayMode : u32 {
        None,
        Recording,
        Playing,
        MovieFinished,
    };

    enum class ValidationResult : u32 {
        OK,
        RevisionDismatch,
        InputCountDismatch,
        Invalid,
    };

    explicit Movie(const Core::System& system);
    ~Movie();

    void SetPlaybackCompletionCallback(std::function<void()> completion_callback);
    void StartPlayback(const std::string& movie_file);
    void StartRecording(const std::string& movie_file, const std::string& author);

    /**
     * Sets the read-only status.
     * When true, movies will be opened in read-only mode. Loading a state will resume playback
     * from that state.
     * When false, movies will be opened in read/write mode. Loading a state will start recording
     * from that state (rerecording). To start rerecording without loading a state, one can save
     * and then immediately load while in R/W.
     *
     * The default is true.
     */
    void SetReadOnly(bool read_only);

    /// Prepare to override the clock before playing back movies
    void PrepareForPlayback(const std::string& movie_file);

    /// Prepare to override the clock before recording movies
    void PrepareForRecording();

    ValidationResult ValidateMovie(const std::string& movie_file) const;

    /// Get the init time that would override the one in the settings
    u64 GetOverrideInitTime() const;

    /// Get the base system ticks value that would override the one generated by core timing
    s64 GetOverrideBaseTicks() const;

    struct MovieMetadata {
        u64 program_id;
        std::string author;
        u32 rerecord_count;
        u64 input_count;
    };
    MovieMetadata GetMovieMetadata(const std::string& movie_file) const;

    /// Get the current movie's unique ID. Used to provide separate savestate slots for movies.
    u64 GetCurrentMovieID() const {
        return id;
    }

    void Shutdown();

    /**
     * When recording: Takes a copy of the given input states so they can be used for playback
     * When playing: Replaces the given input states with the ones stored in the playback file
     */
    void HandlePadAndCircleStatus(Service::HID::PadState& pad_state, s16& circle_pad_x,
                                  s16& circle_pad_y);

    /**
     * When recording: Takes a copy of the given input states so they can be used for playback
     * When playing: Replaces the given input states with the ones stored in the playback file
     */
    void HandleTouchStatus(Service::HID::TouchDataEntry& touch_data);

    /**
     * When recording: Takes a copy of the given input states so they can be used for playback
     * When playing: Replaces the given input states with the ones stored in the playback file
     */
    void HandleAccelerometerStatus(Service::HID::AccelerometerDataEntry& accelerometer_data);

    /**
     * When recording: Takes a copy of the given input states so they can be used for playback
     * When playing: Replaces the given input states with the ones stored in the playback file
     */
    void HandleGyroscopeStatus(Service::HID::GyroscopeDataEntry& gyroscope_data);

    /**
     * When recording: Takes a copy of the given input states so they can be used for playback
     * When playing: Replaces the given input states with the ones stored in the playback file
     */
    void HandleIrRst(Service::IR::PadState& pad_state, s16& c_stick_x, s16& c_stick_y);

    /**
     * When recording: Takes a copy of the given input states so they can be used for playback
     * When playing: Replaces the given input states with the ones stored in the playback file
     */
    void HandleExtraHidResponse(Service::IR::ExtraHIDResponse& extra_hid_response);
    PlayMode GetPlayMode() const;

    u64 GetCurrentInputIndex() const;
    u64 GetTotalInputCount() const;

    /**
     * Saves the movie immediately, in its current state.
     * This is called in Shutdown.
     */
    void SaveMovie();

private:
    void CheckInputEnd();

    template <typename... Targs>
    void Handle(Targs&... Fargs);

    void Play(Service::HID::PadState& pad_state, s16& circle_pad_x, s16& circle_pad_y);
    void Play(Service::HID::TouchDataEntry& touch_data);
    void Play(Service::HID::AccelerometerDataEntry& accelerometer_data);
    void Play(Service::HID::GyroscopeDataEntry& gyroscope_data);
    void Play(Service::IR::PadState& pad_state, s16& c_stick_x, s16& c_stick_y);
    void Play(Service::IR::ExtraHIDResponse& extra_hid_response);

    void Record(const ControllerState& controller_state);
    void Record(const Service::HID::PadState& pad_state, const s16& circle_pad_x,
                const s16& circle_pad_y);
    void Record(const Service::HID::TouchDataEntry& touch_data);
    void Record(const Service::HID::AccelerometerDataEntry& accelerometer_data);
    void Record(const Service::HID::GyroscopeDataEntry& gyroscope_data);
    void Record(const Service::IR::PadState& pad_state, const s16& c_stick_x, const s16& c_stick_y);
    void Record(const Service::IR::ExtraHIDResponse& extra_hid_response);

    ValidationResult ValidateHeader(const CTMHeader& header) const;
    ValidationResult ValidateInput(std::span<const u8> input, u64 expected_count) const;

private:
    const Core::System& system;
    PlayMode play_mode;

    std::string record_movie_file;
    std::string record_movie_author;

    u64 init_time;       // Clock init time override for RNG consistency
    s64 base_ticks = -1; // Core timing base system ticks override for RNG consistency

    std::vector<u8> recorded_input;
    std::size_t current_byte = 0;
    u64 current_input = 0;
    // Total input count of the current movie being played. Not used for recording.
    u64 total_input = 0;

    u64 id = 0; // ID of the current movie loaded
    u64 program_id = 0;
    u32 rerecord_count = 1;
    bool read_only = true;

    std::function<void()> playback_completion_callback = [] {};

    template <class Archive>
    void serialize(Archive& ar, const unsigned int file_version);
    friend class boost::serialization::access;
};
} // namespace Core

BOOST_CLASS_VERSION(Core::Movie, 1)