fork/slippc

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

Patrick Crainadded ability to parse and analyze partially corrupt files; added punish stock info to analysis json output7fcd71c

0.8.0
16.5 KiB361 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  ss << JINT(0,"first_frame"   , s.first_frame)    << ",\n";
85  ss << JINT(0,"last_frame"    , s.last_frame)     << ",\n";
86  ss << JUIN(0,"sudden_death"  , s.sudden_death)   << ",\n";
87  ss << JINT(0,"sd_score"      , s.sd_score)       << ",\n";
88  ss << JUIN(0,"timer_behav"   , s.timer_behav)   << ",\n";
89  ss << JUIN(0,"ui_chars"      , s.ui_chars)      << ",\n";
90  ss << JUIN(0,"game_mode"     , s.game_mode)     << ",\n";
91  ss << JUIN(0,"friendly_fire" , s.friendly_fire) << ",\n";
92  ss << JUIN(0,"demo_mode"     , s.demo_mode)     << ",\n";
93  ss << JUIN(0,"classic_adv"   , s.classic_adv)   << ",\n";
94  ss << JUIN(0,"hrc_event"     , s.hrc_event)     << ",\n";
95  ss << JUIN(0,"allstar_wait1" , s.allstar_wait1) << ",\n";
96  ss << JUIN(0,"allstar_wait2" , s.allstar_wait2) << ",\n";
97  ss << JUIN(0,"allstar_game1" , s.allstar_game1) << ",\n";
98  ss << JUIN(0,"allstar_game2" , s.allstar_game2) << ",\n";
99  ss << JUIN(0,"single_button" , s.single_button) << ",\n";
100  ss << JUIN(0,"pause_timer"   , s.pause_timer)   << ",\n";
101  ss << JUIN(0,"pause_nohud"   , s.pause_nohud)   << ",\n";
102  ss << JUIN(0,"pause_lras"    , s.pause_lras)    << ",\n";
103  ss << JUIN(0,"pause_off"     , s.pause_off)     << ",\n";
104  ss << JUIN(0,"pause_zretry"  , s.pause_zretry)  << ",\n";
105  ss << JUIN(0,"pause_analog"  , s.pause_analog)  << ",\n";
106  ss << JUIN(0,"pause_score"   , s.pause_score)   << ",\n";
107  ss << JUIN(0,"items1"        , s.items1)        << ",\n";
108  ss << JUIN(0,"items2"        , s.items2)        << ",\n";
109  ss << JUIN(0,"items3"        , s.items3)        << ",\n";
110  ss << JUIN(0,"items4"        , s.items4)        << ",\n";
111  ss << JUIN(0,"items5"        , s.items5)        << ",\n";
112  ss << "\"metadata\" : " << s.metadata << "\n},\n";
113
114  ss << "\"players\" : [\n";
115  for(unsigned p = 0; p < 8; ++p) {
116    unsigned pp = (p % 4);
117    if(p > 3 && s.player[pp].ext_char_id != CharExt::CLIMBER) { //If we're not Ice climbers
118      if (p == 7) {
119        ss << SPACE[ILEV] << "{}\n";
120      } else {
121        ss << SPACE[ILEV] << "{},\n";
122      }
123      continue;
124    }
125
126    ss << SPACE[ILEV] << "{\n";
127    ss << JUIN(1,"player_id"   ,pp)                                   << ",\n";
128    ss << JUIN(1,"is_follower" ,p > 3)                                << ",\n";
129    ss << JUIN(1,"ext_char_id" ,s.player[pp].ext_char_id)             << ",\n";
130    ss << JUIN(1,"player_type" ,s.player[pp].player_type)             << ",\n";
131    ss << JUIN(1,"start_stocks",s.player[pp].start_stocks)            << ",\n";
132    ss << JUIN(1,"end_stocks"  ,s.player[pp].end_stocks)              << ",\n";
133    ss << JUIN(1,"color"       ,s.player[pp].color)                   << ",\n";
134    ss << JUIN(1,"team_id"     ,s.player[pp].team_id)                 << ",\n";
135    ss << JUIN(1,"cpu_level"   ,s.player[pp].cpu_level)               << ",\n";
136    ss << JUIN(1,"dash_back"   ,s.player[pp].dash_back)               << ",\n";
137    ss << JUIN(1,"shield_drop" ,s.player[pp].shield_drop)             << ",\n";
138    ss << JUIN(1,"shade"       ,s.player[pp].shade)                   << ",\n";
139    ss << JUIN(1,"handicap"    ,s.player[pp].handicap)                << ",\n";
140    ss << JUIN(1,"offense"     ,s.player[pp].offense)                 << ",\n";
141    ss << JUIN(1,"defense"     ,s.player[pp].defense)                 << ",\n";
142    ss << JUIN(1,"scale"       ,s.player[pp].scale)                   << ",\n";
143    ss << JUIN(1,"stamina"     ,s.player[pp].stamina)                 << ",\n";
144    ss << JUIN(1,"silent"      ,s.player[pp].silent)                  << ",\n";
145    ss << JUIN(1,"low_gravity" ,s.player[pp].low_gravity)             << ",\n";
146    ss << JUIN(1,"invisible"   ,s.player[pp].invisible)               << ",\n";
147    ss << JUIN(1,"black_stock" ,s.player[pp].black_stock)             << ",\n";
148    ss << JUIN(1,"metal"       ,s.player[pp].metal)                   << ",\n";
149    ss << JUIN(1,"warp_in"     ,s.player[pp].warp_in)                 << ",\n";
150    ss << JUIN(1,"rumble"      ,s.player[pp].rumble)                  << ",\n";
151    ss << JSTR(1,"tag_css"     ,escape_json(s.player[pp].tag_css))    << ",\n";
152    ss << JSTR(1,"tag_code"    ,escape_json(s.player[pp].tag_code))   << ",\n";
153    ss << JSTR(1,"tag_player"  ,escape_json(s.player[pp].tag))        << ",\n";
154    ss << JSTR(1,"disp_name"   ,escape_json(s.player[pp].disp_name))  << ",\n";
155    ss << JSTR(1,"slippi_uid"  ,escape_json(s.player[pp].slippi_uid)) << ",\n";
156
157    if (s.player[p].player_type == 3) {
158      ss << SPACE[ILEV] << "\"frames\" : []\n";
159    } else {
160      ss << SPACE[ILEV] << "\"frames\" : [\n";
161      for(unsigned f = 0; f < s.frame_count; ++f) {
162        ss << SPACE[ILEV*2] << "{";
163
164        int a = 0; //True for only the first thing output per line
165        if (CHANGED(follower))
166          ss << JEND(a) << JUIN(2,"follower"      ,s.player[p].frame[f].follower);
167        if (CHANGED(seed))
168          ss << JEND(a) << JUIN(2,"seed"          ,s.player[p].frame[f].seed);
169        if (CHANGED(action_pre))
170          ss << JEND(a) << JUIN(2,"action_pre"    ,s.player[p].frame[f].action_pre);
171        if (CHANGED(pos_x_pre))
172          ss << JEND(a) << JFLT(2,"pos_x_pre"     ,s.player[p].frame[f].pos_x_pre);
173        if (CHANGED(pos_y_pre))
174          ss << JEND(a) << JFLT(2,"pos_y_pre"     ,s.player[p].frame[f].pos_y_pre);
175        if (CHANGED(face_dir_pre))
176          ss << JEND(a) << JFLT(2,"face_dir_pre"  ,s.player[p].frame[f].face_dir_pre);
177        if (CHANGED(joy_x))
178          ss << JEND(a) << JFLT(2,"joy_x"         ,s.player[p].frame[f].joy_x);
179        if (CHANGED(joy_y))
180          ss << JEND(a) << JFLT(2,"joy_y"         ,s.player[p].frame[f].joy_y);
181        if (CHANGED(c_x))
182          ss << JEND(a) << JFLT(2,"c_x"           ,s.player[p].frame[f].c_x);
183        if (CHANGED(c_y))
184          ss << JEND(a) << JFLT(2,"c_y"           ,s.player[p].frame[f].c_y);
185        if (CHANGED(trigger))
186          ss << JEND(a) << JFLT(2,"trigger"       ,s.player[p].frame[f].trigger);
187        if (CHANGED(buttons))
188          ss << JEND(a) << JUIN(2,"buttons"       ,s.player[p].frame[f].buttons);
189        if (CHANGED(phys_l))
190          ss << JEND(a) << JFLT(2,"phys_l"        ,s.player[p].frame[f].phys_l);
191        if (CHANGED(phys_r))
192          ss << JEND(a) << JFLT(2,"phys_r"        ,s.player[p].frame[f].phys_r);
193        if (CHANGED(ucf_x))
194          ss << JEND(a) << JUIN(2,"ucf_x"         ,s.player[p].frame[f].ucf_x);
195        if (CHANGED(percent_pre))
196          ss << JEND(a) << JFLT(2,"percent_pre"   ,s.player[p].frame[f].percent_pre);
197        if (CHANGED(char_id))
198          ss << JEND(a) << JUIN(2,"char_id"       ,s.player[p].frame[f].char_id);
199        if (CHANGED(action_post))
200          ss << JEND(a) << JUIN(2,"action_post"   ,s.player[p].frame[f].action_post);
201        if (CHANGED(pos_x_post))
202          ss << JEND(a) << JFLT(2,"pos_x_post"    ,s.player[p].frame[f].pos_x_post);
203        if (CHANGED(pos_y_post))
204          ss << JEND(a) << JFLT(2,"pos_y_post"    ,s.player[p].frame[f].pos_y_post);
205        if (CHANGED(face_dir_post))
206          ss << JEND(a) << JFLT(2,"face_dir_post" ,s.player[p].frame[f].face_dir_post);
207        if (CHANGED(percent_post))
208          ss << JEND(a) << JFLT(2,"percent_post"  ,s.player[p].frame[f].percent_post);
209        if (CHANGED(shield))
210          ss << JEND(a) << JFLT(2,"shield"        ,s.player[p].frame[f].shield);
211        if (CHANGED(hit_with))
212          ss << JEND(a) << JUIN(2,"hit_with"      ,s.player[p].frame[f].hit_with);
213        if (CHANGED(combo))
214          ss << JEND(a) << JUIN(2,"combo"         ,s.player[p].frame[f].combo);
215        if (CHANGED(hurt_by))
216          ss << JEND(a) << JUIN(2,"hurt_by"       ,s.player[p].frame[f].hurt_by);
217        if (CHANGED(stocks))
218          ss << JEND(a) << JUIN(2,"stocks"        ,s.player[p].frame[f].stocks);
219        if (CHANGED(action_fc))
220          ss << JEND(a) << JFLT(2,"action_fc"     ,s.player[p].frame[f].action_fc);
221
222        if(MIN_VERSION(2,0,0)) {
223          if (CHANGED(flags_1))
224            ss << JEND(a) << JUIN(2,"flags_1"       ,s.player[p].frame[f].flags_1);
225          if (CHANGED(flags_2))
226            ss << JEND(a) << JUIN(2,"flags_2"       ,s.player[p].frame[f].flags_2);
227          if (CHANGED(flags_3))
228            ss << JEND(a) << JUIN(2,"flags_3"       ,s.player[p].frame[f].flags_3);
229          if (CHANGED(flags_4))
230            ss << JEND(a) << JUIN(2,"flags_4"       ,s.player[p].frame[f].flags_4);
231          if (CHANGED(flags_5))
232            ss << JEND(a) << JUIN(2,"flags_5"       ,s.player[p].frame[f].flags_5);
233          if (CHANGED(hitstun))
234            ss << JEND(a) << JUIN(2,"hitstun"       ,s.player[p].frame[f].hitstun);
235          if (CHANGED(airborne))
236            ss << JEND(a) << JUIN(2,"airborne"      ,s.player[p].frame[f].airborne);
237          if (CHANGED(ground_id))
238            ss << JEND(a) << JUIN(2,"ground_id"     ,s.player[p].frame[f].ground_id);
239          if (CHANGED(jumps))
240            ss << JEND(a) << JUIN(2,"jumps"         ,s.player[p].frame[f].jumps);
241          if (CHANGED(l_cancel))
242            ss << JEND(a) << JUIN(2,"l_cancel"      ,s.player[p].frame[f].l_cancel);
243          if (CHANGED(alive))
244            ss << JEND(a) << JINT(2,"alive"         ,s.player[p].frame[f].alive);
245        }
246
247        if(MIN_VERSION(2,1,0)) {
248          if (CHANGED(hurtbox))
249            ss << JEND(a) << JUIN(2,"hurtbox"       ,s.player[p].frame[f].hurtbox);
250        }
251
252        if(MIN_VERSION(3,5,0)) {
253          if (CHANGED(self_air_x))
254            ss << JEND(a) << JFLT(2,"self_air_x"    ,s.player[p].frame[f].self_air_x);
255          if (CHANGED(self_air_y))
256            ss << JEND(a) << JFLT(2,"self_air_y"    ,s.player[p].frame[f].self_air_y);
257          if (CHANGED(attack_x))
258            ss << JEND(a) << JFLT(2,"attack_x"      ,s.player[p].frame[f].attack_x);
259          if (CHANGED(attack_y))
260            ss << JEND(a) << JFLT(2,"attack_y"      ,s.player[p].frame[f].attack_y);
261          if (CHANGED(self_grd_x))
262            ss << JEND(a) << JFLT(2,"self_grd_x"    ,s.player[p].frame[f].self_grd_x);
263        }
264
265        if(MIN_VERSION(3,8,0)) {
266          if (CHANGED(hitlag))
267            ss << JEND(a) << JFLT(2,"hitlag"        ,s.player[p].frame[f].hitlag);
268        }
269
270        if(MIN_VERSION(3,11,0)) {
271          if (CHANGED(anim_index))
272            ss << JEND(a) << JUIN(2,"anim_index"    ,s.player[p].frame[f].anim_index);
273        }
274
275        if (f < s.frame_count-1) {
276          ss << "\n" << SPACE[ILEV*2] << "},\n";
277        } else {
278          ss << "\n" << SPACE[ILEV*2] << "}\n";
279        }
280      }
281      ss << SPACE[ILEV*2] << "]\n";
282    }
283    if (p == 7) {
284      ss << SPACE[ILEV] << "}\n";
285    } else {
286      ss << SPACE[ILEV] << "},\n";
287    }
288  }
289  if (MAX_VERSION(3,0,0)) {
290    ss << "]\n";
291  } else {
292    ss << "],\n";
293    ss << "\"items\" : [\n";
294    for(unsigned i = 0; i < MAX_ITEMS; ++i) {
295      if (s.item[i].spawn_id > MAX_ITEMS) {
296        break;
297      }
298      ss << SPACE[ILEV] << "{\n";
299      ss << JUIN(1,"spawn_id" ,s.item[i].spawn_id)           << ",\n";
300      ss << JUIN(1,"item_type",s.item[i].type)               << ",\n";
301      ss << SPACE[ILEV] << "\"frames\" : [\n";
302
303      for(unsigned f = 0; f < s.item[i].num_frames; ++f) {
304        ss << SPACE[ILEV*2] << "{";
305        int a = 0; //True for only the first thing output per line
306
307        ss << JEND(a) << JUIN(2,"frame"      ,s.item[i].frame[f].frame);
308        if (ICHANGED(state))
309          ss << JEND(a) << JUIN(2,"state"      ,s.item[i].frame[f].state);
310        if (ICHANGED(face_dir))
311          ss << JEND(a) << JFLT(2,"face_dir"   ,s.item[i].frame[f].face_dir);
312        if (ICHANGED(xvel))
313          ss << JEND(a) << JFLT(2,"xvel"       ,s.item[i].frame[f].xvel);
314        if (ICHANGED(yvel))
315          ss << JEND(a) << JFLT(2,"yvel"       ,s.item[i].frame[f].yvel);
316        if (ICHANGED(xpos))
317          ss << JEND(a) << JFLT(2,"xpos"       ,s.item[i].frame[f].xpos);
318        if (ICHANGED(ypos))
319          ss << JEND(a) << JFLT(2,"ypos"       ,s.item[i].frame[f].ypos);
320        if (ICHANGED(damage))
321          ss << JEND(a) << JUIN(2,"damage"     ,s.item[i].frame[f].damage);
322        if (ICHANGED(expire))
323          ss << JEND(a) << JFLT(2,"expire"     ,s.item[i].frame[f].expire);
324
325        if(MIN_VERSION(3,2,0)) {
326          if (ICHANGED(flags_1))
327            ss << JEND(a) << JUIN(2,"flags_1"     ,s.item[i].frame[f].flags_1);
328          if (ICHANGED(flags_2))
329            ss << JEND(a) << JUIN(2,"flags_2"     ,s.item[i].frame[f].flags_2);
330          if (ICHANGED(flags_3))
331            ss << JEND(a) << JUIN(2,"flags_3"     ,s.item[i].frame[f].flags_3);
332          if (ICHANGED(flags_4))
333            ss << JEND(a) << JUIN(2,"flags_4"     ,s.item[i].frame[f].flags_4);
334          if(MIN_VERSION(3,6,0)) {
335            if (ICHANGED(owner))
336              ss << JEND(a) << JINT(2,"owner"      ,s.item[i].frame[f].owner);
337          }
338        }
339
340        if (f+1 == s.item[i].num_frames) {
341          ss << "\n" << SPACE[ILEV*2] << "}\n";
342        } else {
343          ss << "\n" << SPACE[ILEV*2] << "},\n";
344        }
345
346      }
347
348      if (s.item[i+1].spawn_id > MAX_ITEMS) {
349        ss << SPACE[ILEV] << "]}\n";
350      } else {
351        ss << SPACE[ILEV] << "]},\n";
352      }
353    }
354    ss << "]\n";
355  }
356
357  ss << "}" << std::endl;
358  return ss.str();
359}
360
361}