CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Debugger/WebSocket/ReplaySubscriber.cpp
Views: 1401
1
// Copyright (c) 2021- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <cstdint>
19
#include "Common/Data/Encoding/Base64.h"
20
#include "Common/Swap.h"
21
#include "Core/HLE/sceRtc.h"
22
#include "Core/Replay.h"
23
#include "Core/System.h"
24
#include "Core/Debugger/WebSocket/ReplaySubscriber.h"
25
26
DebuggerSubscriber *WebSocketReplayInit(DebuggerEventHandlerMap &map) {
27
// No need to bind or alloc state, these are all global.
28
map["replay.begin"] = &WebSocketReplayBegin;
29
map["replay.abort"] = &WebSocketReplayAbort;
30
map["replay.flush"] = &WebSocketReplayFlush;
31
map["replay.execute"] = &WebSocketReplayExecute;
32
map["replay.status"] = &WebSocketReplayStatus;
33
map["replay.time.get"] = &WebSocketReplayTimeGet;
34
map["replay.time.set"] = &WebSocketReplayTimeSet;
35
36
return nullptr;
37
}
38
39
// Begin or resume recording of replay data (replay.begin)
40
//
41
// If a replay was previously being played back, this will keep any executed replay data up to
42
// this point for the next flush. To discard, break the CPU, abort, and then begin.
43
//
44
// No parameters.
45
//
46
// Response (same event name) with no extra data.
47
void WebSocketReplayBegin(DebuggerRequest &req) {
48
ReplayBeginSave();
49
req.Respond();
50
}
51
52
// Abort any replay execution or recording (replay.abort)
53
//
54
// This stops executing any replay and discards any in progress recording.
55
//
56
// No parameters.
57
//
58
// Response (same event name) with no extra data.
59
void WebSocketReplayAbort(DebuggerRequest &req) {
60
ReplayAbort();
61
req.Respond();
62
}
63
64
// Flush current recording data (replay.flush)
65
//
66
// Flushes event data and returns it. Note when combining, you must decode first.
67
//
68
// No parameters.
69
//
70
// Response (same event name):
71
// - version: unsigned integer, version number of data.
72
// - base64: base64 encode of binary data.
73
void WebSocketReplayFlush(DebuggerRequest &req) {
74
if (!PSP_IsInited())
75
return req.Fail("Game not running");
76
77
std::vector<uint8_t> data;
78
ReplayFlushBlob(&data);
79
80
JsonWriter &json = req.Respond();
81
json.writeInt("version", ReplayVersion());
82
json.writeString("base64", Base64Encode(data.data(), data.size()));
83
}
84
85
// Begin executing a replay (replay.execute)
86
//
87
// Parameters:
88
// - version: unsigned integer, same version from replay.flush.
89
// - base64: base64 encoded replay data.
90
//
91
// Response (same event name) with no extra data.
92
void WebSocketReplayExecute(DebuggerRequest &req) {
93
if (!PSP_IsInited())
94
return req.Fail("Game not running");
95
96
uint32_t version = -1;
97
if (!req.ParamU32("version", &version))
98
return;
99
std::string encoded;
100
if (!req.ParamString("base64", &encoded))
101
return;
102
103
std::vector<uint8_t> data = Base64Decode(encoded.data(), encoded.size());
104
if (!ReplayExecuteBlob(version, data))
105
return req.Fail("Invalid replay data or version");
106
107
req.Respond();
108
}
109
110
// Get replay status (replay.status)
111
//
112
// No parameters.
113
//
114
// Response (same event name):
115
// - executing: boolean if a replay is being executed.
116
// - saving: boolean if a replay is being recorded.
117
void WebSocketReplayStatus(DebuggerRequest &req) {
118
JsonWriter &json = req.Respond();
119
json.writeBool("executing", ReplayIsExecuting());
120
json.writeBool("saving", ReplayIsSaving());
121
}
122
123
// Get the base RTC (real time clock) time for replay data (replay.time.get)
124
//
125
// The base time is constant during a game session, and represents the "power on" time of the
126
// emulated PSP.
127
//
128
// No parameters.
129
//
130
// Response (same event name):
131
// - value: unsigned integer, may have more than 32 integer bits.
132
void WebSocketReplayTimeGet(DebuggerRequest &req) {
133
if (!PSP_IsInited())
134
return req.Fail("Game not running");
135
136
JsonWriter &json = req.Respond();
137
json.writeUint("value", RtcBaseTime());
138
}
139
140
// Overwrite the base RTC time (replay.time.set)
141
//
142
// Parameters:
143
// - value: unsigned integer.
144
//
145
// Response (same event name) with no extra data.
146
void WebSocketReplayTimeSet(DebuggerRequest &req) {
147
if (!PSP_IsInited())
148
return req.Fail("Game not running");
149
150
uint32_t value;
151
if (!req.ParamU32("value", &value, false)) {
152
return;
153
}
154
155
RtcSetBaseTime((int32_t)value);
156
req.Respond();
157
}
158
159