fork/slippc

git clone https://git.julia.blue/fork/slippc

Aaron Batilofeat: Export Slippi 3.14 data to jsonf0a1f51

master
16.7 KiB366 linesraw
1#include "replay.h"
2
3//JSON Output shortcuts
4#define JFLT(i,k,n) SPACE[ILEV*(i)] << "\"" << (k) << "\" : " << float(n)
5#define JINT(i,k,n) SPACE[ILEV*(i)] << "\"" << (k) << "\" : " << int32_t(n)
6#define JUIN(i,k,n) SPACE[ILEV*(i)] << "\"" << (k) << "\" : " << uint32_t(n)
7#define JSTR(i,k,s) SPACE[ILEV*(i)] << "\"" << (k) << "\" : \"" << (s) << "\""
8//Logic for outputting a line only if it changed since last frame (or if we're in full output mode)
9#define CHANGED(field) (not delta) || (f == 0) || (s.player[p].frame[f].field != s.player[p].frame[f-1].field)
10#define ICHANGED(field) (not delta) || (f == 0) || (s.item[i].frame[f].field != s.item[i].frame[f-1].field)
11//Logic for outputting a comma or not depending on whether we're the first element in a JSON object
12#define JEND(a) ((a++ == 0) ? "\n" : ",\n")
13
14namespace slip {
15
16void SlippiReplay::setFrames(int32_t max_frames) {
17  this->last_frame  = max_frames;
18  this->frame_count = max_frames-this->first_frame;
19  for(unsigned i = 0; i < 4; ++i) {
20    if (this->player[i].player_type != 3) {
21      this->player[i].frame = new SlippiFrame[this->frame_count];
22      if (this->player[i].ext_char_id == CharExt::CLIMBER) { //Extra player for Ice Climbers
23        this->player[i+4].frame = new SlippiFrame[this->frame_count];
24      }
25    }
26  }
27}
28
29void SlippiReplay::cleanup() {
30  for(unsigned i = 0; i < 4; ++i) {
31    if (this->player[i].player_type != 3) {
32      delete [] this->player[i].frame;
33      if (this->player[i].ext_char_id == CharExt::CLIMBER) { //Extra player for Ice Climbers
34        delete [] this->player[i+4].frame;
35      }
36    }
37  }
38  for(unsigned i = 0; i < MAX_ITEMS; ++i) {
39    if (this->item[i].frame != nullptr) {
40      delete [] this->item[i].frame;
41    }
42  }
43}
44
45std::string SlippiReplay::replayAsJson(bool delta) {
46  SlippiReplay s = (*this);
47
48  uint8_t _slippi_maj = (s.slippi_version_raw >> 24) & 0xff;
49  uint8_t _slippi_min = (s.slippi_version_raw >> 16) & 0xff;
50  uint8_t _slippi_rev = (s.slippi_version_raw >>  8) & 0xff;
51
52  std::stringstream ss;
53  ss << "{" << std::endl;
54
55  ss << JSTR(0,"original_file" , escape_json(s.original_file))  << ",\n";
56  ss << JSTR(0,"slippi_version", s.slippi_version)              << ",\n";
57  ss << JSTR(0,"parser_version", s.parser_version)              << ",\n";
58  ss << JUIN(0,"errors",         s.errors)                      << ",\n";
59  ss << JSTR(0,"game_start_raw", s.game_start_raw)              << ",\n";
60  ss << JSTR(0,"start_time"    , s.start_time)                  << ",\n";
61  ss << JINT(0,"frame_count"   , s.frame_count)                 << ",\n";
62  ss << JSTR(0,"played_on"     , s.played_on)                   << ",\n";
63  ss << JINT(0,"winner_id"     , s.winner_id)                   << ",\n";
64  ss << JUIN(0,"timer"         , s.timer)                       << ",\n";
65  ss << JUIN(0,"teams"         , s.teams)                       << ",\n";
66  ss << JUIN(0,"stage"         , s.stage)                       << ",\n";
67  ss << JUIN(0,"seed"          , s.seed)                        << ",\n";
68  ss << JINT(0,"items_on"      , s.items_on)                    << ",\n";
69  ss << JUIN(0,"end_type"      , s.end_type)                    << ",\n";
70  ss << JINT(0,"lras"          , s.lras)                        << ",\n";
71  if(MIN_VERSION(1,5,0)) {
72    ss << JUIN(0,"pal"           , s.pal)            << ",\n";
73  }
74  if(MIN_VERSION(2,0,0)) {
75    ss << JUIN(0,"frozen_stadium", s.frozen_stadium) << ",\n";
76  }
77  if(MIN_VERSION(3,7,0)) {
78    ss << JUIN(0,"scene_min"     , s.scene_min)      << ",\n";
79    ss << JUIN(0,"scene_maj"     , s.scene_maj)      << ",\n";
80  }
81  if(MIN_VERSION(3,12,0)) {
82    ss << JUIN(0,"language"      , s.language)       << ",\n";
83  }
84  if(MIN_VERSION(3,14,0)) {
85    ss << JSTR(0,"match_id"          , s.match_id)          << ",\n";
86    ss << JUIN(0,"game_number"       , s.game_number)       << ",\n";
87    ss << JUIN(0,"tiebreaker_number" , s.tiebreaker_number) << ",\n";
88  }
89  ss << JINT(0,"first_frame"   , s.first_frame)    << ",\n";
90  ss << JINT(0,"last_frame"    , s.last_frame)     << ",\n";
91  ss << JUIN(0,"sudden_death"  , s.sudden_death)   << ",\n";
92  ss << JINT(0,"sd_score"      , s.sd_score)       << ",\n";
93  ss << JUIN(0,"timer_behav"   , s.timer_behav)   << ",\n";
94  ss << JUIN(0,"ui_chars"      , s.ui_chars)      << ",\n";
95  ss << JUIN(0,"game_mode"     , s.game_mode)     << ",\n";
96  ss << JUIN(0,"friendly_fire" , s.friendly_fire) << ",\n";
97  ss << JUIN(0,"demo_mode"     , s.demo_mode)     << ",\n";
98  ss << JUIN(0,"classic_adv"   , s.classic_adv)   << ",\n";
99  ss << JUIN(0,"hrc_event"     , s.hrc_event)     << ",\n";
100  ss << JUIN(0,"allstar_wait1" , s.allstar_wait1) << ",\n";
101  ss << JUIN(0,"allstar_wait2" , s.allstar_wait2) << ",\n";
102  ss << JUIN(0,"allstar_game1" , s.allstar_game1) << ",\n";
103  ss << JUIN(0,"allstar_game2" , s.allstar_game2) << ",\n";
104  ss << JUIN(0,"single_button" , s.single_button) << ",\n";
105  ss << JUIN(0,"pause_timer"   , s.pause_timer)   << ",\n";
106  ss << JUIN(0,"pause_nohud"   , s.pause_nohud)   << ",\n";
107  ss << JUIN(0,"pause_lras"    , s.pause_lras)    << ",\n";
108  ss << JUIN(0,"pause_off"     , s.pause_off)     << ",\n";
109  ss << JUIN(0,"pause_zretry"  , s.pause_zretry)  << ",\n";
110  ss << JUIN(0,"pause_analog"  , s.pause_analog)  << ",\n";
111  ss << JUIN(0,"pause_score"   , s.pause_score)   << ",\n";
112  ss << JUIN(0,"items1"        , s.items1)        << ",\n";
113  ss << JUIN(0,"items2"        , s.items2)        << ",\n";
114  ss << JUIN(0,"items3"        , s.items3)        << ",\n";
115  ss << JUIN(0,"items4"        , s.items4)        << ",\n";
116  ss << JUIN(0,"items5"        , s.items5)        << ",\n";
117  ss << "\"metadata\" : " << s.metadata << "\n},\n";
118
119  ss << "\"players\" : [\n";
120  for(unsigned p = 0; p < 8; ++p) {
121    unsigned pp = (p % 4);
122    if(p > 3 && s.player[pp].ext_char_id != CharExt::CLIMBER) { //If we're not Ice climbers
123      if (p == 7) {
124        ss << SPACE[ILEV] << "{}\n";
125      } else {
126        ss << SPACE[ILEV] << "{},\n";
127      }
128      continue;
129    }
130
131    ss << SPACE[ILEV] << "{\n";
132    ss << JUIN(1,"player_id"   ,pp)                                   << ",\n";
133    ss << JUIN(1,"is_follower" ,p > 3)                                << ",\n";
134    ss << JUIN(1,"ext_char_id" ,s.player[pp].ext_char_id)             << ",\n";
135    ss << JUIN(1,"player_type" ,s.player[pp].player_type)             << ",\n";
136    ss << JUIN(1,"start_stocks",s.player[pp].start_stocks)            << ",\n";
137    ss << JUIN(1,"end_stocks"  ,s.player[pp].end_stocks)              << ",\n";
138    ss << JUIN(1,"color"       ,s.player[pp].color)                   << ",\n";
139    ss << JUIN(1,"team_id"     ,s.player[pp].team_id)                 << ",\n";
140    ss << JUIN(1,"cpu_level"   ,s.player[pp].cpu_level)               << ",\n";
141    ss << JUIN(1,"dash_back"   ,s.player[pp].dash_back)               << ",\n";
142    ss << JUIN(1,"shield_drop" ,s.player[pp].shield_drop)             << ",\n";
143    ss << JUIN(1,"shade"       ,s.player[pp].shade)                   << ",\n";
144    ss << JUIN(1,"handicap"    ,s.player[pp].handicap)                << ",\n";
145    ss << JUIN(1,"offense"     ,s.player[pp].offense)                 << ",\n";
146    ss << JUIN(1,"defense"     ,s.player[pp].defense)                 << ",\n";
147    ss << JUIN(1,"scale"       ,s.player[pp].scale)                   << ",\n";
148    ss << JUIN(1,"stamina"     ,s.player[pp].stamina)                 << ",\n";
149    ss << JUIN(1,"silent"      ,s.player[pp].silent)                  << ",\n";
150    ss << JUIN(1,"low_gravity" ,s.player[pp].low_gravity)             << ",\n";
151    ss << JUIN(1,"invisible"   ,s.player[pp].invisible)               << ",\n";
152    ss << JUIN(1,"black_stock" ,s.player[pp].black_stock)             << ",\n";
153    ss << JUIN(1,"metal"       ,s.player[pp].metal)                   << ",\n";
154    ss << JUIN(1,"warp_in"     ,s.player[pp].warp_in)                 << ",\n";
155    ss << JUIN(1,"rumble"      ,s.player[pp].rumble)                  << ",\n";
156    ss << JSTR(1,"tag_css"     ,escape_json(s.player[pp].tag_css))    << ",\n";
157    ss << JSTR(1,"tag_code"    ,escape_json(s.player[pp].tag_code))   << ",\n";
158    ss << JSTR(1,"tag_player"  ,escape_json(s.player[pp].tag))        << ",\n";
159    ss << JSTR(1,"disp_name"   ,escape_json(s.player[pp].disp_name))  << ",\n";
160    ss << JSTR(1,"slippi_uid"  ,escape_json(s.player[pp].slippi_uid)) << ",\n";
161
162    if (s.player[p].player_type == 3) {
163      ss << SPACE[ILEV] << "\"frames\" : []\n";
164    } else {
165      ss << SPACE[ILEV] << "\"frames\" : [\n";
166      for(unsigned f = 0; f < s.frame_count; ++f) {
167        ss << SPACE[ILEV*2] << "{";
168
169        int a = 0; //True for only the first thing output per line
170        if (CHANGED(follower))
171          ss << JEND(a) << JUIN(2,"follower"      ,s.player[p].frame[f].follower);
172        if (CHANGED(seed))
173          ss << JEND(a) << JUIN(2,"seed"          ,s.player[p].frame[f].seed);
174        if (CHANGED(action_pre))
175          ss << JEND(a) << JUIN(2,"action_pre"    ,s.player[p].frame[f].action_pre);
176        if (CHANGED(pos_x_pre))
177          ss << JEND(a) << JFLT(2,"pos_x_pre"     ,s.player[p].frame[f].pos_x_pre);
178        if (CHANGED(pos_y_pre))
179          ss << JEND(a) << JFLT(2,"pos_y_pre"     ,s.player[p].frame[f].pos_y_pre);
180        if (CHANGED(face_dir_pre))
181          ss << JEND(a) << JFLT(2,"face_dir_pre"  ,s.player[p].frame[f].face_dir_pre);
182        if (CHANGED(joy_x))
183          ss << JEND(a) << JFLT(2,"joy_x"         ,s.player[p].frame[f].joy_x);
184        if (CHANGED(joy_y))
185          ss << JEND(a) << JFLT(2,"joy_y"         ,s.player[p].frame[f].joy_y);
186        if (CHANGED(c_x))
187          ss << JEND(a) << JFLT(2,"c_x"           ,s.player[p].frame[f].c_x);
188        if (CHANGED(c_y))
189          ss << JEND(a) << JFLT(2,"c_y"           ,s.player[p].frame[f].c_y);
190        if (CHANGED(trigger))
191          ss << JEND(a) << JFLT(2,"trigger"       ,s.player[p].frame[f].trigger);
192        if (CHANGED(buttons))
193          ss << JEND(a) << JUIN(2,"buttons"       ,s.player[p].frame[f].buttons);
194        if (CHANGED(phys_l))
195          ss << JEND(a) << JFLT(2,"phys_l"        ,s.player[p].frame[f].phys_l);
196        if (CHANGED(phys_r))
197          ss << JEND(a) << JFLT(2,"phys_r"        ,s.player[p].frame[f].phys_r);
198        if (CHANGED(ucf_x))
199          ss << JEND(a) << JUIN(2,"ucf_x"         ,s.player[p].frame[f].ucf_x);
200        if (CHANGED(percent_pre))
201          ss << JEND(a) << JFLT(2,"percent_pre"   ,s.player[p].frame[f].percent_pre);
202        if (CHANGED(char_id))
203          ss << JEND(a) << JUIN(2,"char_id"       ,s.player[p].frame[f].char_id);
204        if (CHANGED(action_post))
205          ss << JEND(a) << JUIN(2,"action_post"   ,s.player[p].frame[f].action_post);
206        if (CHANGED(pos_x_post))
207          ss << JEND(a) << JFLT(2,"pos_x_post"    ,s.player[p].frame[f].pos_x_post);
208        if (CHANGED(pos_y_post))
209          ss << JEND(a) << JFLT(2,"pos_y_post"    ,s.player[p].frame[f].pos_y_post);
210        if (CHANGED(face_dir_post))
211          ss << JEND(a) << JFLT(2,"face_dir_post" ,s.player[p].frame[f].face_dir_post);
212        if (CHANGED(percent_post))
213          ss << JEND(a) << JFLT(2,"percent_post"  ,s.player[p].frame[f].percent_post);
214        if (CHANGED(shield))
215          ss << JEND(a) << JFLT(2,"shield"        ,s.player[p].frame[f].shield);
216        if (CHANGED(hit_with))
217          ss << JEND(a) << JUIN(2,"hit_with"      ,s.player[p].frame[f].hit_with);
218        if (CHANGED(combo))
219          ss << JEND(a) << JUIN(2,"combo"         ,s.player[p].frame[f].combo);
220        if (CHANGED(hurt_by))
221          ss << JEND(a) << JUIN(2,"hurt_by"       ,s.player[p].frame[f].hurt_by);
222        if (CHANGED(stocks))
223          ss << JEND(a) << JUIN(2,"stocks"        ,s.player[p].frame[f].stocks);
224        if (CHANGED(action_fc))
225          ss << JEND(a) << JFLT(2,"action_fc"     ,s.player[p].frame[f].action_fc);
226
227        if(MIN_VERSION(2,0,0)) {
228          if (CHANGED(flags_1))
229            ss << JEND(a) << JUIN(2,"flags_1"       ,s.player[p].frame[f].flags_1);
230          if (CHANGED(flags_2))
231            ss << JEND(a) << JUIN(2,"flags_2"       ,s.player[p].frame[f].flags_2);
232          if (CHANGED(flags_3))
233            ss << JEND(a) << JUIN(2,"flags_3"       ,s.player[p].frame[f].flags_3);
234          if (CHANGED(flags_4))
235            ss << JEND(a) << JUIN(2,"flags_4"       ,s.player[p].frame[f].flags_4);
236          if (CHANGED(flags_5))
237            ss << JEND(a) << JUIN(2,"flags_5"       ,s.player[p].frame[f].flags_5);
238          if (CHANGED(hitstun))
239            ss << JEND(a) << JUIN(2,"hitstun"       ,s.player[p].frame[f].hitstun);
240          if (CHANGED(airborne))
241            ss << JEND(a) << JUIN(2,"airborne"      ,s.player[p].frame[f].airborne);
242          if (CHANGED(ground_id))
243            ss << JEND(a) << JUIN(2,"ground_id"     ,s.player[p].frame[f].ground_id);
244          if (CHANGED(jumps))
245            ss << JEND(a) << JUIN(2,"jumps"         ,s.player[p].frame[f].jumps);
246          if (CHANGED(l_cancel))
247            ss << JEND(a) << JUIN(2,"l_cancel"      ,s.player[p].frame[f].l_cancel);
248          if (CHANGED(alive))
249            ss << JEND(a) << JINT(2,"alive"         ,s.player[p].frame[f].alive);
250        }
251
252        if(MIN_VERSION(2,1,0)) {
253          if (CHANGED(hurtbox))
254            ss << JEND(a) << JUIN(2,"hurtbox"       ,s.player[p].frame[f].hurtbox);
255        }
256
257        if(MIN_VERSION(3,5,0)) {
258          if (CHANGED(self_air_x))
259            ss << JEND(a) << JFLT(2,"self_air_x"    ,s.player[p].frame[f].self_air_x);
260          if (CHANGED(self_air_y))
261            ss << JEND(a) << JFLT(2,"self_air_y"    ,s.player[p].frame[f].self_air_y);
262          if (CHANGED(attack_x))
263            ss << JEND(a) << JFLT(2,"attack_x"      ,s.player[p].frame[f].attack_x);
264          if (CHANGED(attack_y))
265            ss << JEND(a) << JFLT(2,"attack_y"      ,s.player[p].frame[f].attack_y);
266          if (CHANGED(self_grd_x))
267            ss << JEND(a) << JFLT(2,"self_grd_x"    ,s.player[p].frame[f].self_grd_x);
268        }
269
270        if(MIN_VERSION(3,8,0)) {
271          if (CHANGED(hitlag))
272            ss << JEND(a) << JFLT(2,"hitlag"        ,s.player[p].frame[f].hitlag);
273        }
274
275        if(MIN_VERSION(3,11,0)) {
276          if (CHANGED(anim_index))
277            ss << JEND(a) << JUIN(2,"anim_index"    ,s.player[p].frame[f].anim_index);
278        }
279
280        if (f < s.frame_count-1) {
281          ss << "\n" << SPACE[ILEV*2] << "},\n";
282        } else {
283          ss << "\n" << SPACE[ILEV*2] << "}\n";
284        }
285      }
286      ss << SPACE[ILEV*2] << "]\n";
287    }
288    if (p == 7) {
289      ss << SPACE[ILEV] << "}\n";
290    } else {
291      ss << SPACE[ILEV] << "},\n";
292    }
293  }
294  if (MAX_VERSION(3,0,0)) {
295    ss << "]\n";
296  } else {
297    ss << "],\n";
298    ss << "\"items\" : [\n";
299    for(unsigned i = 0; i < MAX_ITEMS; ++i) {
300      if (s.item[i].spawn_id > MAX_ITEMS) {
301        break;
302      }
303      ss << SPACE[ILEV] << "{\n";
304      ss << JUIN(1,"spawn_id" ,s.item[i].spawn_id)           << ",\n";
305      ss << JUIN(1,"item_type",s.item[i].type)               << ",\n";
306      ss << SPACE[ILEV] << "\"frames\" : [\n";
307
308      for(unsigned f = 0; f < s.item[i].num_frames; ++f) {
309        ss << SPACE[ILEV*2] << "{";
310        int a = 0; //True for only the first thing output per line
311
312        ss << JEND(a) << JUIN(2,"frame"      ,s.item[i].frame[f].frame);
313        if (ICHANGED(state))
314          ss << JEND(a) << JUIN(2,"state"      ,s.item[i].frame[f].state);
315        if (ICHANGED(face_dir))
316          ss << JEND(a) << JFLT(2,"face_dir"   ,s.item[i].frame[f].face_dir);
317        if (ICHANGED(xvel))
318          ss << JEND(a) << JFLT(2,"xvel"       ,s.item[i].frame[f].xvel);
319        if (ICHANGED(yvel))
320          ss << JEND(a) << JFLT(2,"yvel"       ,s.item[i].frame[f].yvel);
321        if (ICHANGED(xpos))
322          ss << JEND(a) << JFLT(2,"xpos"       ,s.item[i].frame[f].xpos);
323        if (ICHANGED(ypos))
324          ss << JEND(a) << JFLT(2,"ypos"       ,s.item[i].frame[f].ypos);
325        if (ICHANGED(damage))
326          ss << JEND(a) << JUIN(2,"damage"     ,s.item[i].frame[f].damage);
327        if (ICHANGED(expire))
328          ss << JEND(a) << JFLT(2,"expire"     ,s.item[i].frame[f].expire);
329
330        if(MIN_VERSION(3,2,0)) {
331          if (ICHANGED(flags_1))
332            ss << JEND(a) << JUIN(2,"flags_1"     ,s.item[i].frame[f].flags_1);
333          if (ICHANGED(flags_2))
334            ss << JEND(a) << JUIN(2,"flags_2"     ,s.item[i].frame[f].flags_2);
335          if (ICHANGED(flags_3))
336            ss << JEND(a) << JUIN(2,"flags_3"     ,s.item[i].frame[f].flags_3);
337          if (ICHANGED(flags_4))
338            ss << JEND(a) << JUIN(2,"flags_4"     ,s.item[i].frame[f].flags_4);
339          if(MIN_VERSION(3,6,0)) {
340            if (ICHANGED(owner))
341              ss << JEND(a) << JINT(2,"owner"      ,s.item[i].frame[f].owner);
342          }
343        }
344
345        if (f+1 == s.item[i].num_frames) {
346          ss << "\n" << SPACE[ILEV*2] << "}\n";
347        } else {
348          ss << "\n" << SPACE[ILEV*2] << "},\n";
349        }
350
351      }
352
353      if (s.item[i+1].spawn_id > MAX_ITEMS) {
354        ss << SPACE[ILEV] << "]}\n";
355      } else {
356        ss << SPACE[ILEV] << "]},\n";
357      }
358    }
359    ss << "]\n";
360  }
361
362  ss << "}" << std::endl;
363  return ss.str();
364}
365
366}