/
Movie.cpp
1603 lines (1390 loc) · 43.9 KB
/
Movie.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2009 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <algorithm>
#include <array>
#include <cctype>
#include <iterator>
#include <mbedtls/config.h>
#include <mbedtls/md.h>
#include <mutex>
#include "Common/ChunkFile.h"
#include "Common/CommonPaths.h"
#include "Common/FileUtil.h"
#include "Common/Hash.h"
#include "Common/NandPaths.h"
#include "Common/StringUtil.h"
#include "Common/Timer.h"
#include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "Core/CoreTiming.h"
#include "Core/DSP/DSPCore.h"
#include "Core/HW/CPU.h"
#include "Core/HW/DVDInterface.h"
#include "Core/HW/EXI_DeviceIPL.h"
#include "Core/HW/ProcessorInterface.h"
#include "Core/HW/SI.h"
#include "Core/HW/Wiimote.h"
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"
#include "Core/HW/WiimoteEmu/WiimoteHid.h"
#include "Core/IPC_HLE/WII_IPC_HLE_Device_usb.h"
#include "Core/IPC_HLE/WII_IPC_HLE_WiiMote.h"
#include "Core/Movie.h"
#include "Core/NetPlayProto.h"
#include "Core/PowerPC/PowerPC.h"
#include "Core/State.h"
#include "DiscIO/Enums.h"
#include "InputCommon/GCPadStatus.h"
#include "VideoCommon/Fifo.h"
#include "VideoCommon/VideoBackendBase.h"
#include "VideoCommon/VideoConfig.h"
// The chunk to allocate movie data in multiples of.
#define DTM_BASE_LENGTH (1024)
static std::mutex cs_frameSkip;
namespace Movie
{
static bool s_bFrameStep = false;
static bool s_bReadOnly = true;
static u32 s_rerecords = 0;
static PlayMode s_playMode = MODE_NONE;
static u32 s_framesToSkip = 0, s_frameSkipCounter = 0;
static u8 s_numPads = 0;
static ControllerState s_padState;
static DTMHeader tmpHeader;
static u8* tmpInput = nullptr;
static size_t tmpInputAllocated = 0;
static u64 s_currentByte = 0, s_totalBytes = 0;
static u64 s_currentFrame = 0, s_totalFrames = 0; // VI
static u64 s_currentLagCount = 0;
static u64 s_totalLagCount = 0; // just stats
static u64 s_currentInputCount = 0, s_totalInputCount = 0; // just stats
static u64 s_totalTickCount = 0, s_tickCountAtLastInput = 0; // just stats
static u64 s_recordingStartTime; // seconds since 1970 that recording started
static bool s_bSaveConfig = false, s_bSkipIdle = false, s_bDualCore = false;
static bool s_bProgressive = false, s_bPAL60 = false;
static bool s_bDSPHLE = false, s_bFastDiscSpeed = false;
static bool s_bSyncGPU = false, s_bNetPlay = false;
static std::string s_videoBackend = "unknown";
static int s_iCPUCore = 1;
static bool s_bClearSave = false;
static bool s_bDiscChange = false;
static bool s_bReset = false;
static std::string s_author = "";
static std::string s_discChange = "";
static u64 s_titleID = 0;
static u8 s_MD5[16];
static u8 s_bongos, s_memcards;
static u8 s_revision[20];
static u32 s_DSPiromHash = 0;
static u32 s_DSPcoefHash = 0;
static u8 s_language = static_cast<u8>(DiscIO::Language::LANGUAGE_UNKNOWN);
static bool s_bRecordingFromSaveState = false;
static bool s_bPolled = false;
// s_InputDisplay is used by both CPU and GPU (is mutable).
static std::mutex s_input_display_lock;
static std::string s_InputDisplay[8];
static GCManipFunction gcmfunc = nullptr;
static WiiManipFunction wiimfunc = nullptr;
// NOTE: Host / CPU Thread
static void EnsureTmpInputSize(size_t bound)
{
if (tmpInputAllocated >= bound)
return;
// The buffer expands in powers of two of DTM_BASE_LENGTH
// (standard exponential buffer growth).
size_t newAlloc = DTM_BASE_LENGTH;
while (newAlloc < bound)
newAlloc *= 2;
u8* newTmpInput = new u8[newAlloc];
tmpInputAllocated = newAlloc;
if (tmpInput != nullptr)
{
if (s_totalBytes > 0)
memcpy(newTmpInput, tmpInput, (size_t)s_totalBytes);
delete[] tmpInput;
}
tmpInput = newTmpInput;
}
static bool IsMovieHeader(u8 magic[4])
{
return magic[0] == 'D' && magic[1] == 'T' && magic[2] == 'M' && magic[3] == 0x1A;
}
static std::array<u8, 20> ConvertGitRevisionToBytes(const std::string& revision)
{
std::array<u8, 20> revision_bytes{};
if (revision.size() % 2 == 0 && std::all_of(revision.begin(), revision.end(), ::isxdigit))
{
// The revision string normally contains a git commit hash,
// which is 40 hexadecimal digits long. In DTM files, each pair of
// hexadecimal digits is stored as one byte, for a total of 20 bytes.
size_t bytes_to_write = std::min(revision.size() / 2, revision_bytes.size());
unsigned int temp;
for (size_t i = 0; i < bytes_to_write; ++i)
{
sscanf(&revision[2 * i], "%02x", &temp);
revision_bytes[i] = temp;
}
}
else
{
// If the revision string for some reason doesn't only contain hexadecimal digit
// pairs, we instead copy the string with no conversion. This probably doesn't match
// the intended design of the DTM format, but it's the most sensible fallback.
size_t bytes_to_write = std::min(revision.size(), revision_bytes.size());
std::copy_n(std::begin(revision), bytes_to_write, std::begin(revision_bytes));
}
return revision_bytes;
}
// NOTE: GPU Thread
std::string GetInputDisplay()
{
if (!IsMovieActive())
{
s_numPads = 0;
for (int i = 0; i < 4; ++i)
{
if (SerialInterface::GetDeviceType(i) != SIDEVICE_NONE)
s_numPads |= (1 << i);
if (g_wiimote_sources[i] != WIIMOTE_SRC_NONE)
s_numPads |= (1 << (i + 4));
}
}
std::string input_display;
{
std::lock_guard<std::mutex> guard(s_input_display_lock);
for (int i = 0; i < 8; ++i)
{
if ((s_numPads & (1 << i)) != 0)
input_display += s_InputDisplay[i];
}
}
return input_display;
}
// NOTE: GPU Thread
std::string GetRTCDisplay()
{
time_t current_time = CEXIIPL::GetGCTimeJan1970();
tm* gm_time = gmtime(¤t_time);
char buffer[256];
strftime(buffer, sizeof(buffer), "Date/Time: %c", gm_time);
std::stringstream format_time;
format_time << buffer;
return format_time.str();
}
// NOTE: GPU Thread
void FrameUpdate()
{
// TODO[comex]: This runs on the GPU thread, yet it messes with the CPU
// state directly. That's super sketchy.
s_currentFrame++;
if (!s_bPolled)
s_currentLagCount++;
if (IsRecordingInput())
{
s_totalFrames = s_currentFrame;
s_totalLagCount = s_currentLagCount;
}
if (s_bFrameStep)
{
s_bFrameStep = false;
CPU::Break();
}
if (s_framesToSkip)
FrameSkipping();
s_bPolled = false;
}
// called when game is booting up, even if no movie is active,
// but potentially after BeginRecordingInput or PlayInput has been called.
// NOTE: EmuThread
void Init()
{
s_bPolled = false;
s_bFrameStep = false;
s_bSaveConfig = false;
s_iCPUCore = SConfig::GetInstance().iCPUCore;
if (IsPlayingInput())
{
ReadHeader();
std::thread md5thread(CheckMD5);
md5thread.detach();
if (strncmp(tmpHeader.gameID, SConfig::GetInstance().GetUniqueID().c_str(), 6))
{
PanicAlertT("The recorded game (%s) is not the same as the selected game (%s)",
tmpHeader.gameID, SConfig::GetInstance().GetUniqueID().c_str());
EndPlayInput(false);
}
}
if (IsRecordingInput())
{
GetSettings();
std::thread md5thread(GetMD5);
md5thread.detach();
s_tickCountAtLastInput = 0;
}
s_frameSkipCounter = s_framesToSkip;
memset(&s_padState, 0, sizeof(s_padState));
if (!tmpHeader.bFromSaveState || !IsPlayingInput())
Core::SetStateFileName("");
for (auto& disp : s_InputDisplay)
disp.clear();
if (!IsMovieActive())
{
s_bRecordingFromSaveState = false;
s_rerecords = 0;
s_currentByte = 0;
s_currentFrame = 0;
s_currentLagCount = 0;
s_currentInputCount = 0;
}
}
// NOTE: CPU Thread
void InputUpdate()
{
s_currentInputCount++;
if (IsRecordingInput())
{
s_totalInputCount = s_currentInputCount;
s_totalTickCount += CoreTiming::GetTicks() - s_tickCountAtLastInput;
s_tickCountAtLastInput = CoreTiming::GetTicks();
}
}
// NOTE: Host Thread
void SetFrameSkipping(unsigned int framesToSkip)
{
std::lock_guard<std::mutex> lk(cs_frameSkip);
s_framesToSkip = framesToSkip;
s_frameSkipCounter = 0;
// Don't forget to re-enable rendering in case it wasn't...
// as this won't be changed anymore when frameskip is turned off
if (framesToSkip == 0)
Fifo::SetRendering(true);
}
// NOTE: CPU Thread
void SetPolledDevice()
{
s_bPolled = true;
}
// NOTE: Host Thread
void DoFrameStep()
{
if (Core::GetState() == Core::CORE_PAUSE)
{
// if already paused, frame advance for 1 frame
s_bFrameStep = true;
Core::RequestRefreshInfo();
Core::SetState(Core::CORE_RUN);
}
else if (!s_bFrameStep)
{
// if not paused yet, pause immediately instead
Core::SetState(Core::CORE_PAUSE);
}
}
// NOTE: Host Thread
void SetReadOnly(bool bEnabled)
{
if (s_bReadOnly != bEnabled)
Core::DisplayMessage(bEnabled ? "Read-only mode." : "Read+Write mode.", 1000);
s_bReadOnly = bEnabled;
}
// NOTE: GPU Thread
void FrameSkipping()
{
// Frameskipping will desync movie playback
if (!Core::g_want_determinism)
{
std::lock_guard<std::mutex> lk(cs_frameSkip);
s_frameSkipCounter++;
if (s_frameSkipCounter > s_framesToSkip || Core::ShouldSkipFrame(s_frameSkipCounter) == false)
s_frameSkipCounter = 0;
Fifo::SetRendering(!s_frameSkipCounter);
}
}
bool IsRecordingInput()
{
return (s_playMode == MODE_RECORDING);
}
bool IsRecordingInputFromSaveState()
{
return s_bRecordingFromSaveState;
}
bool IsJustStartingRecordingInputFromSaveState()
{
return IsRecordingInputFromSaveState() && s_currentFrame == 0;
}
bool IsJustStartingPlayingInputFromSaveState()
{
return IsRecordingInputFromSaveState() && s_currentFrame == 1 && IsPlayingInput();
}
bool IsPlayingInput()
{
return (s_playMode == MODE_PLAYING);
}
bool IsMovieActive()
{
return s_playMode != MODE_NONE;
}
bool IsReadOnly()
{
return s_bReadOnly;
}
u64 GetRecordingStartTime()
{
return s_recordingStartTime;
}
u64 GetCurrentFrame()
{
return s_currentFrame;
}
u64 GetTotalFrames()
{
return s_totalFrames;
}
u64 GetCurrentInputCount()
{
return s_currentInputCount;
}
u64 GetTotalInputCount()
{
return s_totalInputCount;
}
u64 GetCurrentLagCount()
{
return s_currentLagCount;
}
u64 GetTotalLagCount()
{
return s_totalLagCount;
}
void SetClearSave(bool enabled)
{
s_bClearSave = enabled;
}
void SignalDiscChange(const std::string& new_disc_filename)
{
s_discChange = new_disc_filename;
s_bDiscChange = true;
}
void SetReset(bool reset)
{
s_bReset = reset;
}
void SetTitleId(u64 title_id)
{
s_titleID = title_id;
}
bool IsUsingPad(int controller)
{
return ((s_numPads & (1 << controller)) != 0);
}
bool IsUsingBongo(int controller)
{
return ((s_bongos & (1 << controller)) != 0);
}
bool IsUsingWiimote(int wiimote)
{
return ((s_numPads & (1 << (wiimote + 4))) != 0);
}
bool IsConfigSaved()
{
return s_bSaveConfig;
}
bool IsDualCore()
{
return s_bDualCore;
}
bool IsProgressive()
{
return s_bProgressive;
}
bool IsPAL60()
{
return s_bPAL60;
}
bool IsSkipIdle()
{
return s_bSkipIdle;
}
bool IsDSPHLE()
{
return s_bDSPHLE;
}
bool IsFastDiscSpeed()
{
return s_bFastDiscSpeed;
}
int GetCPUMode()
{
return s_iCPUCore;
}
u8 GetLanguage()
{
return s_language;
}
bool IsStartingFromClearSave()
{
return s_bClearSave;
}
bool IsUsingMemcard(int memcard)
{
return (s_memcards & (1 << memcard)) != 0;
}
bool IsSyncGPU()
{
return s_bSyncGPU;
}
bool IsNetPlayRecording()
{
return s_bNetPlay;
}
// NOTE: Host Thread
void ChangePads(bool instantly)
{
if (!Core::IsRunning())
return;
int controllers = 0;
for (int i = 0; i < MAX_SI_CHANNELS; ++i)
if (SIDevice_IsGCController(SConfig::GetInstance().m_SIDevice[i]))
controllers |= (1 << i);
if (instantly && (s_numPads & 0x0F) == controllers)
return;
for (int i = 0; i < MAX_SI_CHANNELS; ++i)
{
SIDevices device = SIDEVICE_NONE;
if (IsUsingPad(i))
{
if (SIDevice_IsGCController(SConfig::GetInstance().m_SIDevice[i]))
device = SConfig::GetInstance().m_SIDevice[i];
else
device = IsUsingBongo(i) ? SIDEVICE_GC_TARUKONGA : SIDEVICE_GC_CONTROLLER;
}
if (instantly) // Changes from savestates need to be instantaneous
SerialInterface::AddDevice(device, i);
else
SerialInterface::ChangeDevice(device, i);
}
}
// NOTE: Host / Emu Threads
void ChangeWiiPads(bool instantly)
{
int controllers = 0;
for (int i = 0; i < MAX_WIIMOTES; ++i)
if (g_wiimote_sources[i] != WIIMOTE_SRC_NONE)
controllers |= (1 << i);
// This is important for Wiimotes, because they can desync easily if they get re-activated
if (instantly && (s_numPads >> 4) == controllers)
return;
for (int i = 0; i < MAX_WIIMOTES; ++i)
{
g_wiimote_sources[i] = IsUsingWiimote(i) ? WIIMOTE_SRC_EMU : WIIMOTE_SRC_NONE;
GetUsbPointer()->AccessWiiMote(i | 0x100)->Activate(IsUsingWiimote(i));
}
}
// NOTE: Host Thread
bool BeginRecordingInput(int controllers)
{
if (s_playMode != MODE_NONE || controllers == 0)
return false;
bool was_unpaused = Core::PauseAndLock(true);
s_numPads = controllers;
s_currentFrame = s_totalFrames = 0;
s_currentLagCount = s_totalLagCount = 0;
s_currentInputCount = s_totalInputCount = 0;
s_totalTickCount = s_tickCountAtLastInput = 0;
s_bongos = 0;
s_memcards = 0;
if (NetPlay::IsNetPlayRunning())
{
s_bNetPlay = true;
s_recordingStartTime = CEXIIPL::NetPlay_GetGCTime();
}
else if (SConfig::GetInstance().bEnableCustomRTC)
{
s_recordingStartTime = SConfig::GetInstance().m_customRTCValue;
}
else
{
s_recordingStartTime = Common::Timer::GetLocalTimeSinceJan1970();
}
s_rerecords = 0;
for (int i = 0; i < MAX_SI_CHANNELS; ++i)
if (SConfig::GetInstance().m_SIDevice[i] == SIDEVICE_GC_TARUKONGA)
s_bongos |= (1 << i);
if (Core::IsRunningAndStarted())
{
const std::string save_path = File::GetUserPath(D_STATESAVES_IDX) + "dtm.sav";
if (File::Exists(save_path))
File::Delete(save_path);
State::SaveAs(save_path);
s_bRecordingFromSaveState = true;
// This is only done here if starting from save state because otherwise we won't have the
// titleid. Otherwise it's set in WII_IPC_HLE_Device_es.cpp.
// TODO: find a way to GetTitleDataPath() from Movie::Init()
if (SConfig::GetInstance().bWii)
{
if (File::Exists(Common::GetTitleDataPath(s_titleID, Common::FROM_SESSION_ROOT) +
"banner.bin"))
Movie::s_bClearSave = false;
else
Movie::s_bClearSave = true;
}
std::thread md5thread(GetMD5);
md5thread.detach();
GetSettings();
}
// Wiimotes cause desync issues if they're not reset before launching the game
if (!Core::IsRunningAndStarted())
{
// This will also reset the wiimotes for gamecube games, but that shouldn't do anything
Wiimote::ResetAllWiimotes();
}
s_playMode = MODE_RECORDING;
s_author = SConfig::GetInstance().m_strMovieAuthor;
EnsureTmpInputSize(1);
s_currentByte = s_totalBytes = 0;
Core::UpdateWantDeterminism();
Core::PauseAndLock(false, was_unpaused);
Core::DisplayMessage("Starting movie recording", 2000);
return true;
}
static std::string Analog2DToString(u8 x, u8 y, const std::string& prefix, u8 range = 255)
{
u8 center = range / 2 + 1;
if ((x <= 1 || x == center || x >= range) && (y <= 1 || y == center || y >= range))
{
if (x != center || y != center)
{
if (x != center && y != center)
{
return StringFromFormat("%s:%s,%s", prefix.c_str(), x < center ? "LEFT" : "RIGHT",
y < center ? "DOWN" : "UP");
}
else if (x != center)
{
return StringFromFormat("%s:%s", prefix.c_str(), x < center ? "LEFT" : "RIGHT");
}
else
{
return StringFromFormat("%s:%s", prefix.c_str(), y < center ? "DOWN" : "UP");
}
}
else
{
return "";
}
}
else
{
return StringFromFormat("%s:%d,%d", prefix.c_str(), x, y);
}
}
static std::string Analog1DToString(u8 v, const std::string& prefix, u8 range = 255)
{
if (v > 0)
{
if (v == range)
{
return prefix;
}
else
{
return StringFromFormat("%s:%d", prefix.c_str(), v);
}
}
else
{
return "";
}
}
// NOTE: CPU Thread
static void SetInputDisplayString(ControllerState padState, int controllerID)
{
std::string display_str = StringFromFormat("P%d:", controllerID + 1);
if (padState.A)
display_str += " A";
if (padState.B)
display_str += " B";
if (padState.X)
display_str += " X";
if (padState.Y)
display_str += " Y";
if (padState.Z)
display_str += " Z";
if (padState.Start)
display_str += " START";
if (padState.DPadUp)
display_str += " UP";
if (padState.DPadDown)
display_str += " DOWN";
if (padState.DPadLeft)
display_str += " LEFT";
if (padState.DPadRight)
display_str += " RIGHT";
if (padState.reset)
display_str += " RESET";
display_str += Analog1DToString(padState.TriggerL, " L");
display_str += Analog1DToString(padState.TriggerR, " R");
display_str += Analog2DToString(padState.AnalogStickX, padState.AnalogStickY, " ANA");
display_str += Analog2DToString(padState.CStickX, padState.CStickY, " C");
display_str += '\n';
std::lock_guard<std::mutex> guard(s_input_display_lock);
s_InputDisplay[controllerID] = std::move(display_str);
}
// NOTE: CPU Thread
static void SetWiiInputDisplayString(int remoteID, u8* const data,
const WiimoteEmu::ReportFeatures& rptf, int ext,
const wiimote_key key)
{
int controllerID = remoteID + 4;
std::string display_str = StringFromFormat("R%d:", remoteID + 1);
u8* const coreData = rptf.core ? (data + rptf.core) : nullptr;
u8* const accelData = rptf.accel ? (data + rptf.accel) : nullptr;
u8* const irData = rptf.ir ? (data + rptf.ir) : nullptr;
u8* const extData = rptf.ext ? (data + rptf.ext) : nullptr;
if (coreData)
{
wm_buttons buttons = *(wm_buttons*)coreData;
if (buttons.left)
display_str += " LEFT";
if (buttons.right)
display_str += " RIGHT";
if (buttons.down)
display_str += " DOWN";
if (buttons.up)
display_str += " UP";
if (buttons.a)
display_str += " A";
if (buttons.b)
display_str += " B";
if (buttons.plus)
display_str += " +";
if (buttons.minus)
display_str += " -";
if (buttons.one)
display_str += " 1";
if (buttons.two)
display_str += " 2";
if (buttons.home)
display_str += " HOME";
}
if (accelData)
{
wm_accel* dt = (wm_accel*)accelData;
display_str +=
StringFromFormat(" ACC:%d,%d,%d", dt->x << 2 | ((wm_buttons*)coreData)->acc_x_lsb,
dt->y << 2 | ((wm_buttons*)coreData)->acc_y_lsb << 1,
dt->z << 2 | ((wm_buttons*)coreData)->acc_z_lsb << 1);
}
if (irData)
{
u16 x = irData[0] | ((irData[2] >> 4 & 0x3) << 8);
u16 y = irData[1] | ((irData[2] >> 6 & 0x3) << 8);
display_str += StringFromFormat(" IR:%d,%d", x, y);
}
// Nunchuk
if (extData && ext == 1)
{
wm_nc nunchuk;
memcpy(&nunchuk, extData, sizeof(wm_nc));
WiimoteDecrypt(&key, (u8*)&nunchuk, 0, sizeof(wm_nc));
nunchuk.bt.hex = nunchuk.bt.hex ^ 0x3;
std::string accel = StringFromFormat(
" N-ACC:%d,%d,%d", (nunchuk.ax << 2) | nunchuk.bt.acc_x_lsb,
(nunchuk.ay << 2) | nunchuk.bt.acc_y_lsb, (nunchuk.az << 2) | nunchuk.bt.acc_z_lsb);
if (nunchuk.bt.c)
display_str += " C";
if (nunchuk.bt.z)
display_str += " Z";
display_str += accel;
display_str += Analog2DToString(nunchuk.jx, nunchuk.jy, " ANA");
}
// Classic controller
if (extData && ext == 2)
{
wm_classic_extension cc;
memcpy(&cc, extData, sizeof(wm_classic_extension));
WiimoteDecrypt(&key, (u8*)&cc, 0, sizeof(wm_classic_extension));
cc.bt.hex = cc.bt.hex ^ 0xFFFF;
if (cc.bt.regular_data.dpad_left)
display_str += " LEFT";
if (cc.bt.dpad_right)
display_str += " RIGHT";
if (cc.bt.dpad_down)
display_str += " DOWN";
if (cc.bt.regular_data.dpad_up)
display_str += " UP";
if (cc.bt.a)
display_str += " A";
if (cc.bt.b)
display_str += " B";
if (cc.bt.x)
display_str += " X";
if (cc.bt.y)
display_str += " Y";
if (cc.bt.zl)
display_str += " ZL";
if (cc.bt.zr)
display_str += " ZR";
if (cc.bt.plus)
display_str += " +";
if (cc.bt.minus)
display_str += " -";
if (cc.bt.home)
display_str += " HOME";
display_str += Analog1DToString(cc.lt1 | (cc.lt2 << 3), " L", 31);
display_str += Analog1DToString(cc.rt, " R", 31);
display_str += Analog2DToString(cc.regular_data.lx, cc.regular_data.ly, " ANA", 63);
display_str += Analog2DToString(cc.rx1 | (cc.rx2 << 1) | (cc.rx3 << 3), cc.ry, " R-ANA", 31);
}
display_str += '\n';
std::lock_guard<std::mutex> guard(s_input_display_lock);
s_InputDisplay[controllerID] = std::move(display_str);
}
// NOTE: CPU Thread
void CheckPadStatus(GCPadStatus* PadStatus, int controllerID)
{
s_padState.A = ((PadStatus->button & PAD_BUTTON_A) != 0);
s_padState.B = ((PadStatus->button & PAD_BUTTON_B) != 0);
s_padState.X = ((PadStatus->button & PAD_BUTTON_X) != 0);
s_padState.Y = ((PadStatus->button & PAD_BUTTON_Y) != 0);
s_padState.Z = ((PadStatus->button & PAD_TRIGGER_Z) != 0);
s_padState.Start = ((PadStatus->button & PAD_BUTTON_START) != 0);
s_padState.DPadUp = ((PadStatus->button & PAD_BUTTON_UP) != 0);
s_padState.DPadDown = ((PadStatus->button & PAD_BUTTON_DOWN) != 0);
s_padState.DPadLeft = ((PadStatus->button & PAD_BUTTON_LEFT) != 0);
s_padState.DPadRight = ((PadStatus->button & PAD_BUTTON_RIGHT) != 0);
s_padState.L = ((PadStatus->button & PAD_TRIGGER_L) != 0);
s_padState.R = ((PadStatus->button & PAD_TRIGGER_R) != 0);
s_padState.TriggerL = PadStatus->triggerLeft;
s_padState.TriggerR = PadStatus->triggerRight;
s_padState.AnalogStickX = PadStatus->stickX;
s_padState.AnalogStickY = PadStatus->stickY;
s_padState.CStickX = PadStatus->substickX;
s_padState.CStickY = PadStatus->substickY;
s_padState.disc = s_bDiscChange;
s_bDiscChange = false;
s_padState.reset = s_bReset;
s_bReset = false;
SetInputDisplayString(s_padState, controllerID);
}
// NOTE: CPU Thread
void RecordInput(GCPadStatus* PadStatus, int controllerID)
{
if (!IsRecordingInput() || !IsUsingPad(controllerID))
return;
CheckPadStatus(PadStatus, controllerID);
EnsureTmpInputSize((size_t)(s_currentByte + 8));
memcpy(&(tmpInput[s_currentByte]), &s_padState, 8);
s_currentByte += 8;
s_totalBytes = s_currentByte;
}
// NOTE: CPU Thread
void CheckWiimoteStatus(int wiimote, u8* data, const WiimoteEmu::ReportFeatures& rptf, int ext,
const wiimote_key key)
{
SetWiiInputDisplayString(wiimote, data, rptf, ext, key);
if (IsRecordingInput())
RecordWiimote(wiimote, data, rptf.size);
}
void RecordWiimote(int wiimote, u8* data, u8 size)
{
if (!IsRecordingInput() || !IsUsingWiimote(wiimote))
return;
InputUpdate();
EnsureTmpInputSize((size_t)(s_currentByte + size + 1));
tmpInput[s_currentByte++] = size;
memcpy(&(tmpInput[s_currentByte]), data, size);
s_currentByte += size;
s_totalBytes = s_currentByte;
}
// NOTE: EmuThread / Host Thread
void ReadHeader()
{
s_numPads = tmpHeader.numControllers;
s_recordingStartTime = tmpHeader.recordingStartTime;
if (s_rerecords < tmpHeader.numRerecords)
s_rerecords = tmpHeader.numRerecords;
if (tmpHeader.bSaveConfig)
{
s_bSaveConfig = true;
s_bSkipIdle = tmpHeader.bSkipIdle;
s_bDualCore = tmpHeader.bDualCore;
s_bProgressive = tmpHeader.bProgressive;
s_bPAL60 = tmpHeader.bPAL60;
s_bDSPHLE = tmpHeader.bDSPHLE;
s_bFastDiscSpeed = tmpHeader.bFastDiscSpeed;
s_iCPUCore = tmpHeader.CPUCore;
s_bClearSave = tmpHeader.bClearSave;
s_memcards = tmpHeader.memcards;
s_bongos = tmpHeader.bongos;
s_bSyncGPU = tmpHeader.bSyncGPU;
s_bNetPlay = tmpHeader.bNetPlay;
s_language = tmpHeader.language;
memcpy(s_revision, tmpHeader.revision, ArraySize(s_revision));
}
else
{
GetSettings();
}
s_videoBackend = (char*)tmpHeader.videoBackend;
s_discChange = (char*)tmpHeader.discChange;
s_author = (char*)tmpHeader.author;
memcpy(s_MD5, tmpHeader.md5, 16);
s_DSPiromHash = tmpHeader.DSPiromHash;
s_DSPcoefHash = tmpHeader.DSPcoefHash;
}
// NOTE: Host Thread
bool PlayInput(const std::string& filename)
{
if (s_playMode != MODE_NONE)
return false;
if (!File::Exists(filename))
return false;
File::IOFile g_recordfd;
if (!g_recordfd.Open(filename, "rb"))
return false;
g_recordfd.ReadArray(&tmpHeader, 1);
if (!IsMovieHeader(tmpHeader.filetype))
{
PanicAlertT("Invalid recording file");
g_recordfd.Close();
return false;
}
ReadHeader();
s_totalFrames = tmpHeader.frameCount;
s_totalLagCount = tmpHeader.lagCount;
s_totalInputCount = tmpHeader.inputCount;
s_totalTickCount = tmpHeader.tickCount;
s_currentFrame = 0;
s_currentLagCount = 0;
s_currentInputCount = 0;