Lua: Fix type confusion between signed and unsigned
[lsnes.git] / include / core / moviefile.hpp
blobe6beb0b380f7870f6096b4046b8ecac96823df26
1 #ifndef _moviefile__hpp__included__
2 #define _moviefile__hpp__included__
4 #include <string>
5 #include <vector>
6 #include <stdexcept>
7 #include <map>
8 #include "core/controllerframe.hpp"
9 #include "core/rom-small.hpp"
10 #include "core/subtitles.hpp"
11 #include "interface/romtype.hpp"
12 #include "library/rrdata.hpp"
13 #include "library/zip.hpp"
15 class loaded_rom;
17 /**
18 * Dynamic state parts of movie file.
20 struct dynamic_state
22 /**
23 * Ctor.
25 dynamic_state();
26 /**
27 * Contents of SRAM on time of savestate (if is_savestate is true).
29 std::map<std::string, std::vector<char>> sram;
30 /**
31 * Core savestate (if is_savestate is true).
33 std::vector<char> savestate; //Savestate to load (if is_savestate is true).
34 /**
35 * Host memory (if is_savestate is true).
37 std::vector<char> host_memory;
38 /**
39 * Screenshot (if is_savestate is true).
41 std::vector<char> screenshot;
42 /**
43 * Current frame (if is_savestate is true).
45 uint64_t save_frame;
46 /**
47 * Number of lagged frames (if is_savestate is true).
49 uint64_t lagged_frames;
50 /**
51 * Poll counters (if is_savestate is true).
53 std::vector<uint32_t> pollcounters;
54 /**
55 * Poll flag.
57 unsigned poll_flag;
58 /**
59 * Current RTC second.
61 int64_t rtc_second;
62 /**
63 * Current RTC subsecond.
65 int64_t rtc_subsecond;
66 /**
67 * Active macros at savestate.
69 std::map<std::string, uint64_t> active_macros;
70 /**
71 * Clear the state to power-on defaults.
73 void clear(int64_t sec, int64_t ssec, const std::map<std::string, std::vector<char>>& initsram);
74 /**
75 * Swap the dynamic state with another.
77 void swap(dynamic_state& s) throw();
80 /**
81 * This structure gives parsed representationg of movie file, as result of decoding or for encoding.
83 struct moviefile
85 /**
86 * Brief information
88 struct brief_info
90 brief_info() { current_frame = 0; rerecords = 0; }
91 brief_info(const std::string& filename);
92 std::string sysregion;
93 std::string corename;
94 std::string projectid;
95 std::string hash[ROM_SLOT_COUNT];
96 std::string hashxml[ROM_SLOT_COUNT];
97 std::string hint[ROM_SLOT_COUNT];
98 uint64_t current_frame;
99 uint64_t rerecords;
100 private:
101 void load(zip::reader& r);
102 void binary_io(int s);
105 * Extract branches.
107 struct branch_extractor
109 branch_extractor(const std::string& filename);
110 virtual ~branch_extractor();
111 virtual std::set<std::string> enumerate() { return real->enumerate(); }
112 virtual void read(const std::string& name, portctrl::frame_vector& v) { real->read(name, v); }
113 protected:
114 branch_extractor() { real = NULL; }
115 private:
116 branch_extractor* real;
119 * Extract SRAMs.
121 struct sram_extractor
123 sram_extractor(const std::string& filename);
124 virtual ~sram_extractor();
125 virtual std::set<std::string> enumerate() { return real->enumerate(); }
126 virtual void read(const std::string& name, std::vector<char>& v) { real->read(name, v); }
127 protected:
128 sram_extractor() { real = NULL; }
129 private:
130 sram_extractor* real;
133 * Identify if file is movie/savestate file or not.
135 static bool is_movie_or_savestate(const std::string& filename);
137 * This constructor construct movie structure with default settings.
139 * throws std::bad_alloc: Not enough memory.
141 moviefile() throw(std::bad_alloc);
144 * This constructor loads a movie/savestate file and fills structure accordingly.
146 * parameter filename: The file to load.
147 * parameter romtype: Type of ROM.
148 * throws std::bad_alloc: Not enough memory.
149 * throws std::runtime_error: Can't load the movie file
151 moviefile(const std::string& filename, core_type& romtype) throw(std::bad_alloc, std::runtime_error);
154 * Fill a stub movie with specified loaded ROM.
156 * Parameter rom: The rom.
157 * Parameter settings: The settings.
158 * Parameter rtc_sec: The RTC seconds value.
159 * Parameter rtc_subsec: The RTC subseconds value.
161 moviefile(loaded_rom& rom, std::map<std::string, std::string>& c_settings, uint64_t rtc_sec,
162 uint64_t rtc_subsec);
165 * Reads this movie structure and saves it into file.
167 * parameter filename: The file to save to.
168 * parameter compression: The compression level 0-9. 0 is uncompressed.
169 * parameter binary: Save in binary form if true.
170 * parameter rrd: The rerecords data.
171 * throws std::bad_alloc: Not enough memory.
172 * throws std::runtime_error: Can't save the movie file.
174 void save(const std::string& filename, unsigned compression, bool binary, rrdata_set& rrd, bool as_state)
175 throw(std::bad_alloc, std::runtime_error);
177 * Reads this movie structure and saves it to stream (uncompressed ZIP).
179 void save(std::ostream& outstream, rrdata_set& rrd, bool as_state) throw(std::bad_alloc, std::runtime_error);
181 * Force loading as corrupt.
183 bool force_corrupt;
185 * What is the ROM type and region?
187 core_sysregion* gametype;
189 * Settings.
191 std::map<std::string, std::string> settings;
193 * Emulator Core version string.
195 std::string coreversion;
197 * Name of the game
199 std::string gamename;
201 * Project ID (used to identify if two movies are from the same project).
203 std::string projectid;
205 * Rerecord count (only saved).
207 std::string rerecords;
209 * Rerecord count (memory saves only).
211 uint64_t rerecords_mem;
213 * SHA-256 of ROM (empty string if none).
215 std::string romimg_sha256[ROM_SLOT_COUNT];
217 * SHA-256 of ROM XML (empty string if none).
219 std::string romxml_sha256[ROM_SLOT_COUNT];
221 * ROM name hint (empty string if none).
223 std::string namehint[ROM_SLOT_COUNT];
225 * Authors of the run, first in each pair is full name, second is nickname.
227 std::vector<std::pair<std::string, std::string>> authors;
229 * Contents of SRAM on time of initial powerup.
231 std::map<std::string, std::vector<char>> movie_sram;
233 * Contents of RAM on time of initial powerup.
235 std::map<std::string, std::vector<char>> ramcontent;
237 * Anchoring core savestate (if not empty).
239 std::vector<char> anchor_savestate;
241 * Compressed rrdata.
243 std::vector<char> c_rrdata;
245 * Input for each (sub)frame (points to active branch).
247 portctrl::frame_vector* input;
249 * Branches.
251 std::map<std::string, portctrl::frame_vector> branches;
253 * Movie starting RTC second.
255 int64_t movie_rtc_second;
257 * Movie starting RTC subsecond.
259 int64_t movie_rtc_subsecond;
261 * Start paused flag.
263 bool start_paused;
265 * Lazy project create flag.
267 bool lazy_project_create;
269 * Subtitles.
271 std::map<moviefile_subtiming, std::string> subtitles;
273 * Dynamic state.
275 dynamic_state dyn;
277 * Get number of frames in movie.
279 * returns: Number of frames.
281 uint64_t get_frame_count() throw();
283 * Get length of the movie
285 * returns: Length of the movie in milliseconds.
287 uint64_t get_movie_length() throw();
289 * Return reference to memory slot.
291 static moviefile*& memref(const std::string& slot);
294 * Copy data.
296 void copy_fields(const moviefile& mv);
299 * Create a default branch.
301 void create_default_branch(portctrl::type_set& ports);
303 * Get name of current branch.
305 const std::string& current_branch();
307 * Fork a branch.
309 void fork_branch(const std::string& oldname, const std::string& newname);
311 * Fixup input pointer post-copy.
313 void fixup_current_branch(const moviefile& mv);
315 * Clear the dynamic state to power-on defaults.
317 void clear_dynstate();
318 private:
319 moviefile(const moviefile&);
320 moviefile& operator=(const moviefile&);
321 void binary_io(int stream, rrdata_set& rrd, bool as_state) throw(std::bad_alloc, std::runtime_error);
322 void binary_io(int stream, struct core_type& romtype) throw(std::bad_alloc, std::runtime_error);
323 void save(zip::writer& w, rrdata_set& rrd, bool as_state) throw(std::bad_alloc, std::runtime_error);
324 void load(zip::reader& r, core_type& romtype) throw(std::bad_alloc, std::runtime_error);
325 memtracker::autorelease tracker;
328 void emerg_save_movie(const moviefile& mv, rrdata_set& rrd);
330 #endif