-
Notifications
You must be signed in to change notification settings - Fork 4
/
script_data.cpp
229 lines (203 loc) · 8.38 KB
/
script_data.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
#include "script_data.h"
#include <cmath>
#include <exception>
#include <limits>
#include <stdexcept>
#include <fmt/core.h>
#include <nlohmann/json.hpp>
#include "logging_policy.h"
#include "unix_system.h"
using json = nlohmann::json;
namespace pivid {
template <typename T>
static void from_json(json const& j, XY<T>& xy) {
if (j.empty()) {
xy = {};
} else if (j.is_object()) {
j.at("x").get_to(xy.x);
j.at("y").get_to(xy.y);
} else {
CHECK_ARG(j.is_array(), "Bad XY: {}", j.dump());
j.at(0).get_to(xy.x);
j.at(1).get_to(xy.y);
}
}
static double parse_json_time(json const& j) {
if (j.is_number()) return j;
CHECK_ARG(j.is_string(), "Bad JSON time: {}", j.dump());
return parse_realtime(j);
}
static void from_json(json const& j, BezierSegment& seg) {
if (j.is_number()) {
seg = constant_segment({0, 1e12}, j.get<double>());
return;
}
CHECK_ARG(j.is_object(), "Bad Bezier segment: {}", j.dump());
auto const t_j = j.value("t", json());
if (t_j.empty()) {
seg.t = {0.0, j.value("len", 1e12)};
} else if (t_j.is_number() || (t_j.is_array() && t_j.size() == 1)) {
seg.t.begin = parse_json_time(t_j.is_array() ? t_j.at(0) : t_j);
seg.t.end = seg.t.begin + j.value("len", 1e12 - seg.t.begin);
} else {
CHECK_ARG(t_j.is_array(), "Bad Bezier \"t\": {}", j.dump());
CHECK_ARG(t_j.size() == 2, "Bad Bezier \"t\" length: {}", j.dump());
seg.t = {parse_json_time(t_j.at(0)), parse_json_time(t_j.at(1))};
}
double const dt = seg.t.end - seg.t.begin;
CHECK_ARG(dt >= 0.0, "Bad Bezier segment times: {}", j.dump());
auto const v_j = j.value("v", json());
if (v_j.empty()) {
seg.begin_v = 0.0;
seg.end_v = seg.begin_v + j.value("rate", 0.0) * dt;
seg.p1_v = seg.begin_v + (seg.end_v - seg.begin_v) / 3.0;
seg.p2_v = seg.end_v - (seg.end_v - seg.begin_v) / 3.0;
} else if (v_j.is_number() || (v_j.is_array() && v_j.size() == 1)) {
(v_j.is_array() ? v_j.at(0) : v_j).get_to(seg.begin_v);
seg.end_v = seg.begin_v + j.value("rate", 0.0) * dt;
seg.p1_v = seg.begin_v + (seg.end_v - seg.begin_v) / 3.0;
seg.p2_v = seg.end_v - (seg.end_v - seg.begin_v) / 3.0;
} else if (v_j.is_array() && v_j.size() == 2) {
v_j.at(0).get_to(seg.begin_v);
v_j.at(1).get_to(seg.end_v);
auto const rate_j = j.value("rate", json());
if (rate_j.empty()) {
seg.p1_v = seg.begin_v + (seg.end_v - seg.begin_v) / 3.0;
seg.p2_v = seg.end_v - (seg.end_v - seg.begin_v) / 3.0;
} else {
CHECK_ARG(rate_j.size() == 2, "Bad Bezier \"rate\": {}", j.dump());
seg.p1_v = seg.begin_v + rate_j.at(0).get<double>() * dt / 3.0;
seg.p2_v = seg.end_v - rate_j.at(1).get<double>() * dt / 3.0;
}
} else if (v_j.is_array() && v_j.size() == 4) {
CHECK_ARG(v_j.is_array(), "Bad Bezier \"v\": {}", j.dump());
CHECK_ARG(v_j.size() == 4, "Bad Bezier \"v\" length: {}", j.dump());
v_j.at(0).get_to(seg.begin_v);
v_j.at(1).get_to(seg.p1_v);
v_j.at(2).get_to(seg.p2_v);
v_j.at(3).get_to(seg.end_v);
CHECK_ARG(!j.count("rate"), "Redundant Bezier \"rate\": {}", j.dump());
}
}
static void from_json(json const& j, BezierSpline& bezier) {
if (j.is_object() && j.count("segments")) {
j.at("segments").get_to(bezier.segments);
} else if (j.is_array() && !j.at(0).is_number()) {
j.get_to(bezier.segments);
} else if (!j.empty()) {
bezier.segments.resize(1);
j.get_to(bezier.segments[0]);
}
for (size_t si = 0; si + 1 < bezier.segments.size(); ++si) {
CHECK_ARG(
bezier.segments[si].t.end <= bezier.segments[si + 1].t.begin,
"Bad Bezier time sequence: {}", j.dump()
);
}
if (j.is_object()) {
auto const rep_j = j.value("repeat", json());
if (rep_j.is_number()) {
bezier.repeat = rep_j.get<double>();
} else if (rep_j.is_boolean()) {
auto const& segs = bezier.segments;
if (rep_j.get<bool>() && !bezier.segments.empty())
bezier.repeat = segs.rbegin()->t.end - segs.begin()->t.begin;
} else {
CHECK_ARG(rep_j.is_null(), "Bad Bezier \"repeat\": {}", j.dump());
}
}
CHECK_ARG(bezier.repeat >= 0, "Bad Bezier repeat period: {}", j.dump());
}
static void from_json(json const& j, ScriptBufferTuning& bt) {
auto const pin_j = j.value("pin", json());
if (pin_j.is_number()) {
auto* pin = &bt.pin.emplace_back();
from_json(json(0.0), pin->begin);
from_json(pin_j, pin->end);
} else if (
pin_j.is_array() && pin_j.size() == 2 &&
!pin_j.at(0).is_array()
) {
auto* pin = &bt.pin.emplace_back();
from_json(pin_j.at(0), pin->begin);
from_json(pin_j.at(1), pin->end);
} else {
CHECK_ARG(pin_j.is_array(), "Bad JSON pin: {}", j.dump());
for (auto const& item_j : pin_j) {
CHECK_ARG(
item_j.is_array() && item_j.size() == 2,
"Bad JSON pin range: {}", j.dump()
);
auto* pin = &bt.pin.emplace_back();
from_json(item_j.at(0), pin->begin);
from_json(item_j.at(1), pin->end);
}
}
bt.seek_scan_time = j.value("seek_scan_time", bt.seek_scan_time);
CHECK_ARG(bt.seek_scan_time >= 0, "Bad seek_scan_time: {}", j.dump());
bt.decoder_idle_time = j.value("decoder_idle_time", bt.decoder_idle_time);
CHECK_ARG(bt.decoder_idle_time >= 0, "Bad decoder_idle_time: {}", j.dump());
}
static void from_json(json const& j, ScriptMode& mode) {
if (j.is_null()) {
mode = {};
} else {
CHECK_ARG(j.is_array(), "Bad JSON mode: {}", j.dump());
CHECK_ARG(j.size() == 3, "Bad JSON mode length: {}", j.dump());
CHECK_ARG(
j[0].is_number() && j[1].is_number() && j[2].is_number(),
"Bad JSON mode format: {}", j.dump()
);
mode.size = XY<int>{j[0], j[1]};
mode.hz = j[2];
CHECK_ARG(mode.size.x > 0, "Bad JSON mode X: {}", j.dump());
CHECK_ARG(mode.size.y > 0, "Bad JSON mode Y: {}", j.dump());
CHECK_ARG(mode.hz > 0, "Bad JSON mode Hz: {}", j.dump());
}
}
static void from_json(json const& j, ScriptLayer& layer) {
CHECK_ARG(j.is_object(), "Bad JSON layer: {}", j.dump());
layer.media = j.value("media", "");
CHECK_ARG(!layer.media.empty(), "No \"media\" in JSON layer: {}", j.dump());
j.value("play", json(0)).get_to(layer.play);
j.value("buffer", json(layer.buffer)).get_to(layer.buffer);
j.value("from_xy", json()).get_to(layer.from_xy);
j.value("from_size", json()).get_to(layer.from_size);
j.value("to_xy", json()).get_to(layer.to_xy);
j.value("to_size", json()).get_to(layer.to_size);
j.value("opacity", json()).get_to(layer.opacity);
j.value("reflect", json(false)).get_to(layer.reflect);
j.value("rotate", json(0)).get_to(layer.rotate);
}
static void from_json(json const& j, ScriptScreen& screen) {
CHECK_ARG(j.is_object(), "Bad JSON screen: {}", j.dump());
CHECK_ARG(j.count("mode"), "No \"mode\" in JSON screen: {}", j.dump());
j.at("mode").get_to(screen.mode);
screen.update_hz = j.value("update_hz", screen.update_hz);
CHECK_ARG(screen.update_hz >= 0.0, "Bad JSON update_hz: {}", j.dump());
CHECK_ARG(
screen.update_hz <= screen.mode.hz,
"JSON update_hz > mode hz: {}", j.dump()
);
j.value("layers", json::array()).get_to(screen.layers);
CHECK_ARG(
screen.layers.empty() || screen.mode.hz,
"Layers without mode in JSON screen: {}", j.dump()
);
}
Script parse_script(std::string_view text, double default_zero_time) {
try {
Script s = {};
auto const j = nlohmann::json::parse(text);
CHECK_ARG(j.is_object(), "Bad JSON script: {}", j.dump());
j.value("buffer_tuning", json::object()).get_to(s.buffer_tuning);
j.value("screens", json::object()).get_to(s.screens);
s.zero_time = j.value("zero_time", default_zero_time);
s.main_loop_hz = j.value("main_loop_hz", s.main_loop_hz);
CHECK_ARG(s.main_loop_hz > 0.0, "Bad main_loop_hz: {}", j.dump());
return s;
} catch (nlohmann::json::exception const& e) {
std::throw_with_nested(std::invalid_argument(e.what()));
}
}
} // namespace pivid