7#include <Poco/JSON/Array.h>
8#include <Poco/JSON/Object.h>
9#include <Poco/JSON/Parser.h>
21void ScenarioRecorder::setObserver() {
23 eventModule_ = eve::ModuleManager::requireInstance<eve::platform_event::PlatformEvent>(
"PlatformEvent");
24 if (!eventModule_)
return;
30 if (!eventModule_)
return;
32 eventModule_ =
nullptr;
38 if (err) *err =
"null VM";
43 if (base.empty())
return false;
56 if (!recording_)
return;
58 if (frames_.empty() || frames_.back().frame != currentFrame_)
64 if (err) *err =
"not recording";
69 const bool ok =
save(baseline_, frames_, errorReport_, errorSite_,
path, err);
88 if (!recording_)
return;
89 if (!report.empty()) errorReport_ = report;
90 if (!site.empty()) errorSite_ = site;
94 if (!recording_ || !vm_)
return;
97 for (
const auto&
a : msg.
args) {
101 ev.
args.push_back(std::to_string(
static_cast<long long>(
a.i)));
109 if (frames_.empty() || frames_.back().frame != currentFrame_)
111 frames_.back().events.push_back(std::move(ev));
116 if (err) *err =
"null VM";
119 std::string base, er, es;
120 std::vector<ScenarioFrame> frames;
121 if (!load(
path, &base, &frames, &er, &es, err))
return false;
122 if (!Snapshot::instance().restore(
vm, base, err))
return false;
126 frames_ = std::move(frames);
132 eventModule_ = eve::ModuleManager::requireInstance<eve::platform_event::PlatformEvent>(
"PlatformEvent");
136bool ScenarioRecorder::stageFrame() {
137 if (!replay_ || !eventModule_)
return false;
138 if (replayIndex_ >=
static_cast<int>(frames_.size())) {
143 for (
const auto& ev :
frame.events) {
144 std::vector<eve::platform_event::Variant> args;
145 args.reserve(ev.args.size());
152bool ScenarioRecorder::save(
const std::string& baseline,
153 const std::vector<ScenarioFrame>& frames,
154 const std::string& errorReport,
const std::string& errorSite,
155 const std::string&
path, std::string* err) {
157 Poco::JSON::Object
root;
158 root.set(
"version",
"1");
159 root.set(
"baseline", baseline);
160 Poco::JSON::Array framesArr;
161 for (
const auto&
f : frames) {
162 Poco::JSON::Object fo;
163 fo.set(
"frame",
f.frame);
164 Poco::JSON::Array evs;
165 for (
const auto& e :
f.events) {
166 Poco::JSON::Object eo;
167 eo.set(
"name", e.name);
168 Poco::JSON::Array args;
169 for (
const auto&
a : e.args)
171 eo.set(
"args", args);
174 fo.set(
"events", evs);
177 root.set(
"frames", framesArr);
178 root.set(
"errorReport", errorReport);
179 root.set(
"errorSite", errorSite);
180 std::ostringstream oss;
182 std::ofstream ofs(
path, std::ios::binary);
184 if (err) *err =
"cannot write " +
path;
188 return static_cast<bool>(ofs);
189 }
catch (
const std::exception& e) {
190 if (err) *err = e.what();
195bool ScenarioRecorder::load(
const std::string&
path, std::string* baseline,
196 std::vector<ScenarioFrame>* frames, std::string* errorReport,
197 std::string* errorSite, std::string* err) {
199 std::ifstream ifs(
path, std::ios::binary);
201 if (err) *err =
"cannot read " +
path;
204 std::ostringstream oss;
206 Poco::JSON::Parser parser;
207 Poco::Dynamic::Var
result = parser.parse(oss.str());
208 Poco::JSON::Object::Ptr
root =
result.extract<Poco::JSON::Object::Ptr>();
209 if (baseline) *baseline =
root->getValue<std::string>(
"baseline");
210 if (errorReport) *errorReport =
root->optValue<std::string>(
"errorReport",
"");
211 if (errorSite) *errorSite =
root->optValue<std::string>(
"errorSite",
"");
214 Poco::JSON::Array::Ptr fa =
root->getArray(
"frames");
216 for (
size_t i = 0; i < fa->size(); ++i) {
217 Poco::JSON::Object::Ptr fo = fa->getObject(
static_cast<unsigned int>(i));
219 f.
frame = fo->optValue<
int>(
"frame",
static_cast<int>(i));
220 Poco::JSON::Array::Ptr evs = fo->getArray(
"events");
222 for (
size_t j = 0; j < evs->size(); ++j) {
223 Poco::JSON::Object::Ptr eo =
224 evs->getObject(
static_cast<unsigned int>(j));
226 e.
name = eo->getValue<std::string>(
"name");
227 Poco::JSON::Array::Ptr args = eo->getArray(
"args");
229 for (
size_t k = 0; k < args->size(); ++k)
230 e.
args.push_back(args->getElement<std::string>(
231 static_cast<unsigned int>(k)));
233 f.events.push_back(std::move(e));
236 frames->push_back(std::move(
f));
241 }
catch (
const std::exception& e) {
242 if (err) *err = e.what();
struct SQVM * HSQUIRRELVM
A state-driven bug scenario: baseline snapshot + ordered per-frame inputs.
void setErrorInfo(const std::string &report, const std::string &site)
Called by DevTool when a script error is dumped during recording.
bool end(const std::string &path, std::string *err=nullptr)
Stop recording and persist the scenario to a JSON file.
void markFrame()
Begin a new frame bucket; no-op unless recording.
void cancel()
Abort recording and restore the previous event observer.
bool begin(HSQUIRRELVM vm, std::string *err=nullptr)
Start recording: capture the baseline snapshot and observe the event queue.
void detachObserver()
Ensure the event observer is detached (restore previous); safe to call repeatedly.
void onEventConsumed(const eve::platform_event::Message &msg)
Invoked by the event poll observer for every consumed message.
static ScenarioRecorder & instance()
Instance.
static bool save(const std::string &baseline, const std::vector< ScenarioFrame > &frames, const std::string &errorReport, const std::string &errorSite, const std::string &path, std::string *err=nullptr)
Persist a scenario to a JSON file (used for both record and replay paths).
static Snapshot & instance()
One serializable event payload recorded from the event queue.
std::vector< std::string > args
String payloads (non-string payloads dropped).
std::string name
Event/message name ("keypressed", ...).
All events consumed during one game frame.
int frame
Frame index within the scenario.