fork/slippc

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

Patrick CrainFixed heap allocation for SlippiItemFrame taking an inordinate amount of memory (prev. ~500MB, now about ~100MB); bumped parser version to 0.6.1210d608

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