2017-12-17 09:13:09 +05:30
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2018-09-26 18:08:57 +05:30
|
|
|
#include <boost/optional.hpp>
|
2017-12-17 09:13:09 +05:30
|
|
|
#include <cryptopp/hex.h>
|
|
|
|
#include "common/bit_field.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/file_util.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/scm_rev.h"
|
|
|
|
#include "common/string_util.h"
|
|
|
|
#include "common/swap.h"
|
2018-09-26 18:08:57 +05:30
|
|
|
#include "common/timer.h"
|
2017-12-17 09:13:09 +05:30
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/hle/service/hid/hid.h"
|
|
|
|
#include "core/hle/service/ir/extra_hid.h"
|
|
|
|
#include "core/hle/service/ir/ir_rst.h"
|
|
|
|
#include "core/movie.h"
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
/*static*/ Movie Movie::s_instance;
|
2017-12-17 09:13:09 +05:30
|
|
|
|
|
|
|
enum class PlayMode { None, Recording, Playing };
|
|
|
|
|
|
|
|
enum class ControllerStateType : u8 {
|
|
|
|
PadAndCircle,
|
|
|
|
Touch,
|
|
|
|
Accelerometer,
|
|
|
|
Gyroscope,
|
|
|
|
IrRst,
|
|
|
|
ExtraHidResponse
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
struct ControllerState {
|
|
|
|
ControllerStateType type;
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
union {
|
|
|
|
u16_le hex;
|
|
|
|
|
|
|
|
BitField<0, 1, u16_le> a;
|
|
|
|
BitField<1, 1, u16_le> b;
|
|
|
|
BitField<2, 1, u16_le> select;
|
|
|
|
BitField<3, 1, u16_le> start;
|
|
|
|
BitField<4, 1, u16_le> right;
|
|
|
|
BitField<5, 1, u16_le> left;
|
|
|
|
BitField<6, 1, u16_le> up;
|
|
|
|
BitField<7, 1, u16_le> down;
|
|
|
|
BitField<8, 1, u16_le> r;
|
|
|
|
BitField<9, 1, u16_le> l;
|
|
|
|
BitField<10, 1, u16_le> x;
|
|
|
|
BitField<11, 1, u16_le> y;
|
|
|
|
// Bits 12-15 are currently unused
|
|
|
|
};
|
|
|
|
s16_le circle_pad_x;
|
|
|
|
s16_le circle_pad_y;
|
|
|
|
} pad_and_circle;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
u16_le x;
|
|
|
|
u16_le y;
|
|
|
|
// This is a bool, u8 for platform compatibility
|
|
|
|
u8 valid;
|
|
|
|
} touch;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
s16_le x;
|
|
|
|
s16_le y;
|
|
|
|
s16_le z;
|
|
|
|
} accelerometer;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
s16_le x;
|
|
|
|
s16_le y;
|
|
|
|
s16_le z;
|
|
|
|
} gyroscope;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
s16_le x;
|
|
|
|
s16_le y;
|
|
|
|
// These are bool, u8 for platform compatibility
|
|
|
|
u8 zl;
|
|
|
|
u8 zr;
|
|
|
|
} ir_rst;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
union {
|
|
|
|
u32_le hex;
|
|
|
|
|
|
|
|
BitField<0, 5, u32_le> battery_level;
|
|
|
|
BitField<5, 1, u32_le> zl_not_held;
|
|
|
|
BitField<6, 1, u32_le> zr_not_held;
|
|
|
|
BitField<7, 1, u32_le> r_not_held;
|
|
|
|
BitField<8, 12, u32_le> c_stick_x;
|
|
|
|
BitField<20, 12, u32_le> c_stick_y;
|
|
|
|
};
|
|
|
|
} extra_hid_response;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
static_assert(sizeof(ControllerState) == 7, "ControllerState should be 7 bytes");
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
constexpr std::array<u8, 4> header_magic_bytes{{'C', 'T', 'M', 0x1B}};
|
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
struct CTMHeader {
|
|
|
|
std::array<u8, 4> filetype; /// Unique Identifier to check the file type (always "CTM"0x1B)
|
|
|
|
u64_le program_id; /// ID of the ROM being executed. Also called title_id
|
|
|
|
std::array<u8, 20> revision; /// Git hash of the revision this movie was created with
|
2018-09-26 18:08:57 +05:30
|
|
|
u64_le clock_init_time; /// The init time of the system clock
|
2017-12-17 09:13:09 +05:30
|
|
|
|
2018-09-26 18:08:57 +05:30
|
|
|
std::array<u8, 216> reserved; /// Make heading 256 bytes so it has consistent size
|
2017-12-17 09:13:09 +05:30
|
|
|
};
|
|
|
|
static_assert(sizeof(CTMHeader) == 256, "CTMHeader should be 256 bytes");
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
2018-07-07 20:00:37 +05:30
|
|
|
bool Movie::IsPlayingInput() const {
|
2017-12-17 09:13:09 +05:30
|
|
|
return play_mode == PlayMode::Playing;
|
|
|
|
}
|
2018-07-07 20:00:37 +05:30
|
|
|
bool Movie::IsRecordingInput() const {
|
2017-12-17 09:13:09 +05:30
|
|
|
return play_mode == PlayMode::Recording;
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::CheckInputEnd() {
|
2017-12-17 09:13:09 +05:30
|
|
|
if (current_byte + sizeof(ControllerState) > recorded_input.size()) {
|
2018-06-29 16:48:07 +05:30
|
|
|
LOG_INFO(Movie, "Playback finished");
|
2017-12-17 09:13:09 +05:30
|
|
|
play_mode = PlayMode::None;
|
2018-09-26 18:08:57 +05:30
|
|
|
init_time = 0;
|
2018-07-07 20:00:37 +05:30
|
|
|
playback_completion_callback();
|
2017-12-17 09:13:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Play(Service::HID::PadState& pad_state, s16& circle_pad_x, s16& circle_pad_y) {
|
2017-12-17 09:13:09 +05:30
|
|
|
ControllerState s;
|
|
|
|
std::memcpy(&s, &recorded_input[current_byte], sizeof(ControllerState));
|
|
|
|
current_byte += sizeof(ControllerState);
|
|
|
|
|
|
|
|
if (s.type != ControllerStateType::PadAndCircle) {
|
2018-06-29 16:48:07 +05:30
|
|
|
LOG_ERROR(Movie,
|
2018-06-29 19:26:12 +05:30
|
|
|
"Expected to read type {}, but found {}. Your playback will be out of sync",
|
|
|
|
static_cast<int>(ControllerStateType::PadAndCircle), static_cast<int>(s.type));
|
2017-12-17 09:13:09 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pad_state.a.Assign(s.pad_and_circle.a);
|
|
|
|
pad_state.b.Assign(s.pad_and_circle.b);
|
|
|
|
pad_state.select.Assign(s.pad_and_circle.select);
|
|
|
|
pad_state.start.Assign(s.pad_and_circle.start);
|
|
|
|
pad_state.right.Assign(s.pad_and_circle.right);
|
|
|
|
pad_state.left.Assign(s.pad_and_circle.left);
|
|
|
|
pad_state.up.Assign(s.pad_and_circle.up);
|
|
|
|
pad_state.down.Assign(s.pad_and_circle.down);
|
|
|
|
pad_state.r.Assign(s.pad_and_circle.r);
|
|
|
|
pad_state.l.Assign(s.pad_and_circle.l);
|
|
|
|
pad_state.x.Assign(s.pad_and_circle.x);
|
|
|
|
pad_state.y.Assign(s.pad_and_circle.y);
|
|
|
|
|
|
|
|
circle_pad_x = s.pad_and_circle.circle_pad_x;
|
|
|
|
circle_pad_y = s.pad_and_circle.circle_pad_y;
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Play(Service::HID::TouchDataEntry& touch_data) {
|
2017-12-17 09:13:09 +05:30
|
|
|
ControllerState s;
|
|
|
|
std::memcpy(&s, &recorded_input[current_byte], sizeof(ControllerState));
|
|
|
|
current_byte += sizeof(ControllerState);
|
|
|
|
|
|
|
|
if (s.type != ControllerStateType::Touch) {
|
2018-06-29 16:48:07 +05:30
|
|
|
LOG_ERROR(Movie,
|
2018-06-29 19:26:12 +05:30
|
|
|
"Expected to read type {}, but found {}. Your playback will be out of sync",
|
|
|
|
static_cast<int>(ControllerStateType::Touch), static_cast<int>(s.type));
|
2017-12-17 09:13:09 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
touch_data.x = s.touch.x;
|
|
|
|
touch_data.y = s.touch.y;
|
|
|
|
touch_data.valid.Assign(s.touch.valid);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Play(Service::HID::AccelerometerDataEntry& accelerometer_data) {
|
2017-12-17 09:13:09 +05:30
|
|
|
ControllerState s;
|
|
|
|
std::memcpy(&s, &recorded_input[current_byte], sizeof(ControllerState));
|
|
|
|
current_byte += sizeof(ControllerState);
|
|
|
|
|
|
|
|
if (s.type != ControllerStateType::Accelerometer) {
|
2018-06-29 16:48:07 +05:30
|
|
|
LOG_ERROR(Movie,
|
2018-06-29 19:26:12 +05:30
|
|
|
"Expected to read type {}, but found {}. Your playback will be out of sync",
|
|
|
|
static_cast<int>(ControllerStateType::Accelerometer), static_cast<int>(s.type));
|
2017-12-17 09:13:09 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
accelerometer_data.x = s.accelerometer.x;
|
|
|
|
accelerometer_data.y = s.accelerometer.y;
|
|
|
|
accelerometer_data.z = s.accelerometer.z;
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Play(Service::HID::GyroscopeDataEntry& gyroscope_data) {
|
2017-12-17 09:13:09 +05:30
|
|
|
ControllerState s;
|
|
|
|
std::memcpy(&s, &recorded_input[current_byte], sizeof(ControllerState));
|
|
|
|
current_byte += sizeof(ControllerState);
|
|
|
|
|
|
|
|
if (s.type != ControllerStateType::Gyroscope) {
|
2018-06-29 16:48:07 +05:30
|
|
|
LOG_ERROR(Movie,
|
2018-06-29 19:26:12 +05:30
|
|
|
"Expected to read type {}, but found {}. Your playback will be out of sync",
|
|
|
|
static_cast<int>(ControllerStateType::Gyroscope), static_cast<int>(s.type));
|
2017-12-17 09:13:09 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gyroscope_data.x = s.gyroscope.x;
|
|
|
|
gyroscope_data.y = s.gyroscope.y;
|
|
|
|
gyroscope_data.z = s.gyroscope.z;
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Play(Service::IR::PadState& pad_state, s16& c_stick_x, s16& c_stick_y) {
|
2017-12-17 09:13:09 +05:30
|
|
|
ControllerState s;
|
|
|
|
std::memcpy(&s, &recorded_input[current_byte], sizeof(ControllerState));
|
|
|
|
current_byte += sizeof(ControllerState);
|
|
|
|
|
|
|
|
if (s.type != ControllerStateType::IrRst) {
|
2018-06-29 16:48:07 +05:30
|
|
|
LOG_ERROR(Movie,
|
2018-06-29 19:26:12 +05:30
|
|
|
"Expected to read type {}, but found {}. Your playback will be out of sync",
|
|
|
|
static_cast<int>(ControllerStateType::IrRst), static_cast<int>(s.type));
|
2017-12-17 09:13:09 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
c_stick_x = s.ir_rst.x;
|
|
|
|
c_stick_y = s.ir_rst.y;
|
|
|
|
pad_state.zl.Assign(s.ir_rst.zl);
|
|
|
|
pad_state.zr.Assign(s.ir_rst.zr);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Play(Service::IR::ExtraHIDResponse& extra_hid_response) {
|
2017-12-17 09:13:09 +05:30
|
|
|
ControllerState s;
|
|
|
|
std::memcpy(&s, &recorded_input[current_byte], sizeof(ControllerState));
|
|
|
|
current_byte += sizeof(ControllerState);
|
|
|
|
|
|
|
|
if (s.type != ControllerStateType::ExtraHidResponse) {
|
2018-06-29 16:48:07 +05:30
|
|
|
LOG_ERROR(
|
2018-03-31 11:54:21 +05:30
|
|
|
Movie, "Expected to read type {}, but found {}. Your playback will be out of sync",
|
|
|
|
static_cast<int>(ControllerStateType::ExtraHidResponse), static_cast<int>(s.type));
|
2017-12-17 09:13:09 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-24 02:38:14 +05:30
|
|
|
extra_hid_response.buttons.battery_level.Assign(
|
|
|
|
static_cast<u8>(s.extra_hid_response.battery_level));
|
2017-12-17 09:13:09 +05:30
|
|
|
extra_hid_response.c_stick.c_stick_x.Assign(s.extra_hid_response.c_stick_x);
|
|
|
|
extra_hid_response.c_stick.c_stick_y.Assign(s.extra_hid_response.c_stick_y);
|
2018-07-24 02:38:14 +05:30
|
|
|
extra_hid_response.buttons.r_not_held.Assign(static_cast<u8>(s.extra_hid_response.r_not_held));
|
|
|
|
extra_hid_response.buttons.zl_not_held.Assign(
|
|
|
|
static_cast<u8>(s.extra_hid_response.zl_not_held));
|
|
|
|
extra_hid_response.buttons.zr_not_held.Assign(
|
|
|
|
static_cast<u8>(s.extra_hid_response.zr_not_held));
|
2017-12-17 09:13:09 +05:30
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Record(const ControllerState& controller_state) {
|
2017-12-17 09:13:09 +05:30
|
|
|
recorded_input.resize(current_byte + sizeof(ControllerState));
|
|
|
|
std::memcpy(&recorded_input[current_byte], &controller_state, sizeof(ControllerState));
|
|
|
|
current_byte += sizeof(ControllerState);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Record(const Service::HID::PadState& pad_state, const s16& circle_pad_x,
|
2017-12-17 09:13:09 +05:30
|
|
|
const s16& circle_pad_y) {
|
|
|
|
ControllerState s;
|
|
|
|
s.type = ControllerStateType::PadAndCircle;
|
|
|
|
|
|
|
|
s.pad_and_circle.a.Assign(static_cast<u16>(pad_state.a));
|
|
|
|
s.pad_and_circle.b.Assign(static_cast<u16>(pad_state.b));
|
|
|
|
s.pad_and_circle.select.Assign(static_cast<u16>(pad_state.select));
|
|
|
|
s.pad_and_circle.start.Assign(static_cast<u16>(pad_state.start));
|
|
|
|
s.pad_and_circle.right.Assign(static_cast<u16>(pad_state.right));
|
|
|
|
s.pad_and_circle.left.Assign(static_cast<u16>(pad_state.left));
|
|
|
|
s.pad_and_circle.up.Assign(static_cast<u16>(pad_state.up));
|
|
|
|
s.pad_and_circle.down.Assign(static_cast<u16>(pad_state.down));
|
|
|
|
s.pad_and_circle.r.Assign(static_cast<u16>(pad_state.r));
|
|
|
|
s.pad_and_circle.l.Assign(static_cast<u16>(pad_state.l));
|
|
|
|
s.pad_and_circle.x.Assign(static_cast<u16>(pad_state.x));
|
|
|
|
s.pad_and_circle.y.Assign(static_cast<u16>(pad_state.y));
|
|
|
|
|
|
|
|
s.pad_and_circle.circle_pad_x = circle_pad_x;
|
|
|
|
s.pad_and_circle.circle_pad_y = circle_pad_y;
|
|
|
|
|
|
|
|
Record(s);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Record(const Service::HID::TouchDataEntry& touch_data) {
|
2017-12-17 09:13:09 +05:30
|
|
|
ControllerState s;
|
|
|
|
s.type = ControllerStateType::Touch;
|
|
|
|
|
|
|
|
s.touch.x = touch_data.x;
|
|
|
|
s.touch.y = touch_data.y;
|
|
|
|
s.touch.valid = static_cast<u8>(touch_data.valid);
|
|
|
|
|
|
|
|
Record(s);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Record(const Service::HID::AccelerometerDataEntry& accelerometer_data) {
|
2017-12-17 09:13:09 +05:30
|
|
|
ControllerState s;
|
|
|
|
s.type = ControllerStateType::Accelerometer;
|
|
|
|
|
|
|
|
s.accelerometer.x = accelerometer_data.x;
|
|
|
|
s.accelerometer.y = accelerometer_data.y;
|
|
|
|
s.accelerometer.z = accelerometer_data.z;
|
|
|
|
|
|
|
|
Record(s);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Record(const Service::HID::GyroscopeDataEntry& gyroscope_data) {
|
2017-12-17 09:13:09 +05:30
|
|
|
ControllerState s;
|
|
|
|
s.type = ControllerStateType::Gyroscope;
|
|
|
|
|
|
|
|
s.gyroscope.x = gyroscope_data.x;
|
|
|
|
s.gyroscope.y = gyroscope_data.y;
|
|
|
|
s.gyroscope.z = gyroscope_data.z;
|
|
|
|
|
|
|
|
Record(s);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Record(const Service::IR::PadState& pad_state, const s16& c_stick_x,
|
2017-12-17 09:13:09 +05:30
|
|
|
const s16& c_stick_y) {
|
|
|
|
ControllerState s;
|
|
|
|
s.type = ControllerStateType::IrRst;
|
|
|
|
|
|
|
|
s.ir_rst.x = c_stick_x;
|
|
|
|
s.ir_rst.y = c_stick_y;
|
|
|
|
s.ir_rst.zl = static_cast<u8>(pad_state.zl);
|
|
|
|
s.ir_rst.zr = static_cast<u8>(pad_state.zr);
|
|
|
|
|
|
|
|
Record(s);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Record(const Service::IR::ExtraHIDResponse& extra_hid_response) {
|
2017-12-17 09:13:09 +05:30
|
|
|
ControllerState s;
|
|
|
|
s.type = ControllerStateType::ExtraHidResponse;
|
|
|
|
|
|
|
|
s.extra_hid_response.battery_level.Assign(extra_hid_response.buttons.battery_level);
|
|
|
|
s.extra_hid_response.c_stick_x.Assign(extra_hid_response.c_stick.c_stick_x);
|
|
|
|
s.extra_hid_response.c_stick_y.Assign(extra_hid_response.c_stick.c_stick_y);
|
|
|
|
s.extra_hid_response.r_not_held.Assign(extra_hid_response.buttons.r_not_held);
|
|
|
|
s.extra_hid_response.zl_not_held.Assign(extra_hid_response.buttons.zl_not_held);
|
|
|
|
s.extra_hid_response.zr_not_held.Assign(extra_hid_response.buttons.zr_not_held);
|
|
|
|
|
|
|
|
Record(s);
|
|
|
|
}
|
|
|
|
|
2018-09-26 18:08:57 +05:30
|
|
|
u64 Movie::GetOverrideInitTime() const {
|
|
|
|
return init_time;
|
|
|
|
}
|
|
|
|
|
2018-08-08 21:12:23 +05:30
|
|
|
Movie::ValidationResult Movie::ValidateHeader(const CTMHeader& header, u64 program_id) const {
|
2017-12-17 09:13:09 +05:30
|
|
|
if (header_magic_bytes != header.filetype) {
|
2018-06-29 16:48:07 +05:30
|
|
|
LOG_ERROR(Movie, "Playback file does not have valid header");
|
2018-07-07 20:00:37 +05:30
|
|
|
return ValidationResult::Invalid;
|
2017-12-17 09:13:09 +05:30
|
|
|
}
|
|
|
|
|
2018-11-27 00:34:18 +05:30
|
|
|
std::string revision = fmt::format("{:02x}", fmt::join(header.revision, ""));
|
2017-12-17 09:13:09 +05:30
|
|
|
|
2018-08-08 21:12:23 +05:30
|
|
|
if (!program_id)
|
|
|
|
Core::System::GetInstance().GetAppLoader().ReadProgramId(program_id);
|
2017-12-17 09:13:09 +05:30
|
|
|
if (program_id != header.program_id) {
|
2018-06-29 16:48:07 +05:30
|
|
|
LOG_WARNING(Movie, "This movie was recorded using a ROM with a different program id");
|
2018-07-07 20:00:37 +05:30
|
|
|
return ValidationResult::GameDismatch;
|
2017-12-17 09:13:09 +05:30
|
|
|
}
|
|
|
|
|
2018-07-07 20:00:37 +05:30
|
|
|
if (revision != Common::g_scm_rev) {
|
|
|
|
LOG_WARNING(Movie,
|
|
|
|
"This movie was created on a different version of Citra, playback may desync");
|
|
|
|
return ValidationResult::RevisionDismatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValidationResult::OK;
|
2017-12-17 09:13:09 +05:30
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::SaveMovie() {
|
2018-07-07 20:00:37 +05:30
|
|
|
LOG_INFO(Movie, "Saving recorded movie to '{}'", record_movie_file);
|
|
|
|
FileUtil::IOFile save_record(record_movie_file, "wb");
|
2017-12-17 09:13:09 +05:30
|
|
|
|
|
|
|
if (!save_record.IsGood()) {
|
2018-06-29 16:48:07 +05:30
|
|
|
LOG_ERROR(Movie, "Unable to open file to save movie");
|
2017-12-17 09:13:09 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CTMHeader header = {};
|
|
|
|
header.filetype = header_magic_bytes;
|
2018-09-26 18:08:57 +05:30
|
|
|
header.clock_init_time = init_time;
|
2017-12-17 09:13:09 +05:30
|
|
|
|
|
|
|
Core::System::GetInstance().GetAppLoader().ReadProgramId(header.program_id);
|
|
|
|
|
|
|
|
std::string rev_bytes;
|
|
|
|
CryptoPP::StringSource(Common::g_scm_rev, true,
|
|
|
|
new CryptoPP::HexDecoder(new CryptoPP::StringSink(rev_bytes)));
|
|
|
|
std::memcpy(header.revision.data(), rev_bytes.data(), sizeof(CTMHeader::revision));
|
|
|
|
|
|
|
|
save_record.WriteBytes(&header, sizeof(CTMHeader));
|
|
|
|
save_record.WriteBytes(recorded_input.data(), recorded_input.size());
|
|
|
|
|
|
|
|
if (!save_record.IsGood()) {
|
2018-06-29 16:48:07 +05:30
|
|
|
LOG_ERROR(Movie, "Error saving movie");
|
2017-12-17 09:13:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-07 20:00:37 +05:30
|
|
|
void Movie::StartPlayback(const std::string& movie_file,
|
|
|
|
std::function<void()> completion_callback) {
|
|
|
|
LOG_INFO(Movie, "Loading Movie for playback");
|
|
|
|
FileUtil::IOFile save_record(movie_file, "rb");
|
|
|
|
const u64 size = save_record.GetSize();
|
|
|
|
|
|
|
|
if (save_record.IsGood() && size > sizeof(CTMHeader)) {
|
|
|
|
CTMHeader header;
|
|
|
|
save_record.ReadArray(&header, 1);
|
|
|
|
if (ValidateHeader(header) != ValidationResult::Invalid) {
|
|
|
|
play_mode = PlayMode::Playing;
|
|
|
|
recorded_input.resize(size - sizeof(CTMHeader));
|
|
|
|
save_record.ReadArray(recorded_input.data(), recorded_input.size());
|
|
|
|
current_byte = 0;
|
|
|
|
playback_completion_callback = completion_callback;
|
2017-12-17 09:13:09 +05:30
|
|
|
}
|
2018-07-07 20:00:37 +05:30
|
|
|
} else {
|
|
|
|
LOG_ERROR(Movie, "Failed to playback movie: Unable to open '{}'", movie_file);
|
2017-12-17 09:13:09 +05:30
|
|
|
}
|
2018-07-07 20:00:37 +05:30
|
|
|
}
|
2017-12-17 09:13:09 +05:30
|
|
|
|
2018-07-07 20:00:37 +05:30
|
|
|
void Movie::StartRecording(const std::string& movie_file) {
|
|
|
|
LOG_INFO(Movie, "Enabling Movie recording");
|
|
|
|
play_mode = PlayMode::Recording;
|
|
|
|
record_movie_file = movie_file;
|
|
|
|
}
|
|
|
|
|
2018-09-26 18:08:57 +05:30
|
|
|
static boost::optional<CTMHeader> ReadHeader(const std::string& movie_file) {
|
2018-07-07 20:00:37 +05:30
|
|
|
FileUtil::IOFile save_record(movie_file, "rb");
|
|
|
|
const u64 size = save_record.GetSize();
|
|
|
|
|
|
|
|
if (!save_record || size <= sizeof(CTMHeader)) {
|
2018-09-26 18:08:57 +05:30
|
|
|
return boost::none;
|
2017-12-17 09:13:09 +05:30
|
|
|
}
|
2018-07-07 20:00:37 +05:30
|
|
|
|
|
|
|
CTMHeader header;
|
|
|
|
save_record.ReadArray(&header, 1);
|
2018-09-26 18:08:57 +05:30
|
|
|
|
|
|
|
if (header_magic_bytes != header.filetype) {
|
|
|
|
return boost::none;
|
|
|
|
}
|
|
|
|
|
|
|
|
return header;
|
2018-08-08 21:12:23 +05:30
|
|
|
}
|
|
|
|
|
2018-09-26 18:08:57 +05:30
|
|
|
void Movie::PrepareForPlayback(const std::string& movie_file) {
|
|
|
|
auto header = ReadHeader(movie_file);
|
2018-10-06 03:50:09 +05:30
|
|
|
if (header == boost::none)
|
2018-09-26 18:08:57 +05:30
|
|
|
return;
|
2018-08-08 21:12:23 +05:30
|
|
|
|
2018-09-26 18:08:57 +05:30
|
|
|
init_time = header.value().clock_init_time;
|
|
|
|
}
|
2018-08-08 21:12:23 +05:30
|
|
|
|
2018-09-26 18:08:57 +05:30
|
|
|
void Movie::PrepareForRecording() {
|
|
|
|
init_time = (Settings::values.init_clock == Settings::InitClock::SystemTime
|
|
|
|
? Common::Timer::GetTimeSinceJan1970().count()
|
|
|
|
: Settings::values.init_time);
|
|
|
|
}
|
2018-08-08 21:12:23 +05:30
|
|
|
|
2018-09-26 18:08:57 +05:30
|
|
|
Movie::ValidationResult Movie::ValidateMovie(const std::string& movie_file, u64 program_id) const {
|
|
|
|
LOG_INFO(Movie, "Validating Movie file '{}'", movie_file);
|
|
|
|
auto header = ReadHeader(movie_file);
|
2018-10-06 03:50:09 +05:30
|
|
|
if (header == boost::none)
|
2018-09-26 18:08:57 +05:30
|
|
|
return ValidationResult::Invalid;
|
|
|
|
|
|
|
|
return ValidateHeader(header.value(), program_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 Movie::GetMovieProgramID(const std::string& movie_file) const {
|
|
|
|
auto header = ReadHeader(movie_file);
|
2018-10-06 03:50:09 +05:30
|
|
|
if (header == boost::none)
|
2018-08-08 21:12:23 +05:30
|
|
|
return 0;
|
|
|
|
|
2018-09-26 18:08:57 +05:30
|
|
|
return static_cast<u64>(header.value().program_id);
|
2017-12-17 09:13:09 +05:30
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Shutdown() {
|
|
|
|
if (IsRecordingInput()) {
|
|
|
|
SaveMovie();
|
2017-12-17 09:13:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
play_mode = PlayMode::None;
|
|
|
|
recorded_input.resize(0);
|
2018-07-07 20:00:37 +05:30
|
|
|
record_movie_file.clear();
|
2017-12-17 09:13:09 +05:30
|
|
|
current_byte = 0;
|
2018-09-26 18:08:57 +05:30
|
|
|
init_time = 0;
|
2017-12-17 09:13:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Targs>
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::Handle(Targs&... Fargs) {
|
2017-12-17 09:13:09 +05:30
|
|
|
if (IsPlayingInput()) {
|
2017-12-17 10:25:56 +05:30
|
|
|
ASSERT(current_byte + sizeof(ControllerState) <= recorded_input.size());
|
2017-12-17 09:13:09 +05:30
|
|
|
Play(Fargs...);
|
|
|
|
CheckInputEnd();
|
|
|
|
} else if (IsRecordingInput()) {
|
|
|
|
Record(Fargs...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::HandlePadAndCircleStatus(Service::HID::PadState& pad_state, s16& circle_pad_x,
|
|
|
|
s16& circle_pad_y) {
|
2017-12-17 09:13:09 +05:30
|
|
|
Handle(pad_state, circle_pad_x, circle_pad_y);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::HandleTouchStatus(Service::HID::TouchDataEntry& touch_data) {
|
2017-12-17 09:13:09 +05:30
|
|
|
Handle(touch_data);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::HandleAccelerometerStatus(Service::HID::AccelerometerDataEntry& accelerometer_data) {
|
2017-12-17 09:13:09 +05:30
|
|
|
Handle(accelerometer_data);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::HandleGyroscopeStatus(Service::HID::GyroscopeDataEntry& gyroscope_data) {
|
2017-12-17 09:13:09 +05:30
|
|
|
Handle(gyroscope_data);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::HandleIrRst(Service::IR::PadState& pad_state, s16& c_stick_x, s16& c_stick_y) {
|
2017-12-17 09:13:09 +05:30
|
|
|
Handle(pad_state, c_stick_x, c_stick_y);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:25:56 +05:30
|
|
|
void Movie::HandleExtraHidResponse(Service::IR::ExtraHIDResponse& extra_hid_response) {
|
2017-12-17 09:13:09 +05:30
|
|
|
Handle(extra_hid_response);
|
|
|
|
}
|
2018-03-31 11:54:21 +05:30
|
|
|
} // namespace Core
|