Merge pull request #4041 from lioncash/mismatch

Movie: use std::mismatch for byte mismatch checking
This commit is contained in:
Pierre Bourdon 2016-07-21 10:59:59 +02:00 committed by GitHub
commit c7ead90a09
1 changed files with 56 additions and 53 deletions

View File

@ -5,6 +5,7 @@
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <cctype> #include <cctype>
#include <iterator>
#include <mbedtls/config.h> #include <mbedtls/config.h>
#include <mbedtls/md.h> #include <mbedtls/md.h>
#include <mutex> #include <mutex>
@ -1047,63 +1048,65 @@ void LoadInput(const std::string& filename)
else if (s_currentByte > 0 && s_totalBytes > 0) else if (s_currentByte > 0 && s_totalBytes > 0)
{ {
// verify identical from movie start to the save's current frame // verify identical from movie start to the save's current frame
u32 len = (u32)s_currentByte; std::vector<u8> movInput(s_currentByte);
u8* movInput = new u8[len]; t_record.ReadArray(movInput.data(), movInput.size());
t_record.ReadArray(movInput, (size_t)len);
for (u32 i = 0; i < len; ++i) const auto result = std::mismatch(movInput.begin(), movInput.end(), tmpInput);
if (result.first != movInput.end())
{ {
if (movInput[i] != tmpInput[i]) const ptrdiff_t mismatch_index = std::distance(movInput.begin(), result.first);
// this is a "you did something wrong" alert for the user's benefit.
// we'll try to say what's going on in excruciating detail, otherwise the user might not
// believe us.
if (IsUsingWiimote(0))
{ {
// this is a "you did something wrong" alert for the user's benefit. const size_t byte_offset = static_cast<size_t>(mismatch_index) + sizeof(DTMHeader);
// we'll try to say what's going on in excruciating detail, otherwise the user might not
// believe us. // TODO: more detail
if (IsUsingWiimote(0)) PanicAlertT("Warning: You loaded a save whose movie mismatches on byte %zu (0x%zX). "
{ "You should load another save before continuing, or load this state with "
// TODO: more detail "read-only mode off. Otherwise you'll probably get a desync.",
PanicAlertT("Warning: You loaded a save whose movie mismatches on byte %d (0x%X). You " byte_offset, byte_offset);
"should load another save before continuing, or load this state with "
"read-only mode off. Otherwise you'll probably get a desync.", std::copy(movInput.begin(), movInput.end(), tmpInput);
i + 256, i + 256); }
memcpy(tmpInput, movInput, s_currentByte); else
} {
else const ptrdiff_t frame = mismatch_index / 8;
{ ControllerState curPadState;
int frame = i / 8; memcpy(&curPadState, &tmpInput[frame * 8], 8);
ControllerState curPadState; ControllerState movPadState;
memcpy(&curPadState, &(tmpInput[frame * 8]), 8); memcpy(&movPadState, &movInput[frame * 8], 8);
ControllerState movPadState; PanicAlertT(
memcpy(&movPadState, &(movInput[frame * 8]), 8); "Warning: You loaded a save whose movie mismatches on frame %td. You should load "
PanicAlertT( "another save before continuing, or load this state with read-only mode off. "
"Warning: You loaded a save whose movie mismatches on frame %d. You should load " "Otherwise you'll probably get a desync.\n\n"
"another save before continuing, or load this state with read-only mode off. " "More information: The current movie is %d frames long and the savestate's movie "
"Otherwise you'll probably get a desync.\n\n" "is %d frames long.\n\n"
"More information: The current movie is %d frames long and the savestate's movie " "On frame %td, the current movie presses:\n"
"is %d frames long.\n\n" "Start=%d, A=%d, B=%d, X=%d, Y=%d, Z=%d, DUp=%d, DDown=%d, DLeft=%d, DRight=%d, "
"On frame %d, the current movie presses:\n" "L=%d, R=%d, LT=%d, RT=%d, AnalogX=%d, AnalogY=%d, CX=%d, CY=%d"
"Start=%d, A=%d, B=%d, X=%d, Y=%d, Z=%d, DUp=%d, DDown=%d, DLeft=%d, DRight=%d, " "\n\n"
"L=%d, R=%d, LT=%d, RT=%d, AnalogX=%d, AnalogY=%d, CX=%d, CY=%d" "On frame %td, the savestate's movie presses:\n"
"\n\n" "Start=%d, A=%d, B=%d, X=%d, Y=%d, Z=%d, DUp=%d, DDown=%d, DLeft=%d, DRight=%d, "
"On frame %d, the savestate's movie presses:\n" "L=%d, R=%d, LT=%d, RT=%d, AnalogX=%d, AnalogY=%d, CX=%d, CY=%d",
"Start=%d, A=%d, B=%d, X=%d, Y=%d, Z=%d, DUp=%d, DDown=%d, DLeft=%d, DRight=%d, " frame, (int)g_totalFrames, (int)tmpHeader.frameCount, frame, (int)curPadState.Start,
"L=%d, R=%d, LT=%d, RT=%d, AnalogX=%d, AnalogY=%d, CX=%d, CY=%d", (int)curPadState.A, (int)curPadState.B, (int)curPadState.X, (int)curPadState.Y,
(int)frame, (int)g_totalFrames, (int)tmpHeader.frameCount, (int)frame, (int)curPadState.Z, (int)curPadState.DPadUp, (int)curPadState.DPadDown,
(int)curPadState.Start, (int)curPadState.A, (int)curPadState.B, (int)curPadState.X, (int)curPadState.DPadLeft, (int)curPadState.DPadRight, (int)curPadState.L,
(int)curPadState.Y, (int)curPadState.Z, (int)curPadState.DPadUp, (int)curPadState.R, (int)curPadState.TriggerL, (int)curPadState.TriggerR,
(int)curPadState.DPadDown, (int)curPadState.DPadLeft, (int)curPadState.DPadRight, (int)curPadState.AnalogStickX, (int)curPadState.AnalogStickY,
(int)curPadState.L, (int)curPadState.R, (int)curPadState.TriggerL, (int)curPadState.CStickX, (int)curPadState.CStickY, frame, (int)movPadState.Start,
(int)curPadState.TriggerR, (int)curPadState.AnalogStickX, (int)movPadState.A, (int)movPadState.B, (int)movPadState.X, (int)movPadState.Y,
(int)curPadState.AnalogStickY, (int)curPadState.CStickX, (int)curPadState.CStickY, (int)movPadState.Z, (int)movPadState.DPadUp, (int)movPadState.DPadDown,
(int)frame, (int)movPadState.Start, (int)movPadState.A, (int)movPadState.B, (int)movPadState.DPadLeft, (int)movPadState.DPadRight, (int)movPadState.L,
(int)movPadState.X, (int)movPadState.Y, (int)movPadState.Z, (int)movPadState.DPadUp, (int)movPadState.R, (int)movPadState.TriggerL, (int)movPadState.TriggerR,
(int)movPadState.DPadDown, (int)movPadState.DPadLeft, (int)movPadState.DPadRight, (int)movPadState.AnalogStickX, (int)movPadState.AnalogStickY,
(int)movPadState.L, (int)movPadState.R, (int)movPadState.TriggerL, (int)movPadState.CStickX, (int)movPadState.CStickY);
(int)movPadState.TriggerR, (int)movPadState.AnalogStickX,
(int)movPadState.AnalogStickY, (int)movPadState.CStickX, (int)movPadState.CStickY);
}
break;
} }
} }
delete[] movInput;
} }
} }
t_record.Close(); t_record.Close();