fork/slippc

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

Patrick CrainFixed escape_json() breaking parsing of original_filename on Windows; bumped version to 0.7.162d6bb0

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