1 #include "lua/internal.hpp"
2 #include "lua/unsaferewind.hpp"
3 #include "core/instance.hpp"
4 #include "core/moviedata.hpp"
5 #include "core/mainloop.hpp"
9 int currentframe(lua::state
& L
, lua::parameters
& P
)
11 auto& m
= CORE().mlogic
->get_movie();
12 L
.pushnumber(m
.get_current_frame());
16 int lagcounter(lua::state
& L
, lua::parameters
& P
)
18 auto& m
= CORE().mlogic
->get_movie();
19 L
.pushnumber(m
.get_lag_frames());
23 int framecount(lua::state
& L
, lua::parameters
& P
)
25 auto& m
= CORE().mlogic
->get_movie();
26 L
.pushnumber(m
.get_frame_count());
30 int rerecords(lua::state
& L
, lua::parameters
& P
)
32 L
.pushnumber(CORE().mlogic
->get_rrdata().count());
36 int readonly(lua::state
& L
, lua::parameters
& P
)
38 auto& m
= CORE().mlogic
->get_movie();
39 L
.pushboolean(m
.readonly_mode() ? 1 : 0);
43 int readwrite(lua::state
& L
, lua::parameters
& P
)
45 auto& m
= CORE().mlogic
->get_movie();
46 m
.readonly_mode(false);
50 int frame_subframes(lua::state
& L
, lua::parameters
& P
)
56 auto& m
= CORE().mlogic
->get_movie();
57 L
.pushnumber(m
.frame_subframes(frame
));
61 int read_subframes(lua::state
& L
, lua::parameters
& P
)
63 uint64_t frame
, subframe
;
67 auto& m
= CORE().mlogic
->get_movie();
68 portctrl::frame r
= m
.read_subframe(frame
, subframe
);
71 for(size_t i
= 0; i
< r
.get_index_count(); i
++) {
73 L
.pushnumber(r
.axis2(i
));
79 int read_rtc(lua::state
& L
, lua::parameters
& P
)
82 L
.pushnumber(core
.mlogic
->get_mfile().dyn
.rtc_second
);
83 L
.pushnumber(core
.mlogic
->get_mfile().dyn
.rtc_subsecond
);
87 int unsafe_rewind(lua::state
& L
, lua::parameters
& P
)
90 //Start process to mark save.
91 mainloop_signal_need_rewind(NULL
);
92 } else if(P
.is
<lua_unsaferewind
>()) {
94 lua::objpin
<lua_unsaferewind
> pin
;
98 mainloop_signal_need_rewind(new lua::objpin
<lua_unsaferewind
>(pin
));
100 P
.expected("UNSAFEREWIND or nil");
104 int to_rewind(lua::state
& L
, lua::parameters
& P
)
107 std::string filename
;
111 moviefile
mfile(filename
, core
.rom
->get_internal_rom_type());
112 if(!mfile
.dyn
.save_frame
)
113 throw std::runtime_error("movie.to_rewind only allows savestates");
114 lua_unsaferewind
* u2
= lua::_class
<lua_unsaferewind
>::create(L
);
115 u2
->console_state
= mfile
.dyn
;
117 if(u2
->console_state
.savestate
.size() >= 32)
118 u2
->console_state
.savestate
.resize(u2
->console_state
.savestate
.size() - 32);
119 //Now the remaining field ptr is somewhat nastier.
121 uint64_t s
= mfile
.input
->size();
123 while(++f
< u2
->console_state
.save_frame
) {
126 while(u2
->ptr
< s
&& !(*mfile
.input
)[u2
->ptr
].sync())
132 int rom_loaded(lua::state
& L
, lua::parameters
& P
)
135 L
.pushboolean(!core
.rom
->get_internal_rom_type().isnull());
139 int get_rom_info(lua::state
& L
, lua::parameters
& P
)
142 auto& rom
= *(core
.rom
);
143 bool any_loaded
= false;
146 for(unsigned i
= 0; i
< ROM_SLOT_COUNT
; i
++) {
147 auto img
= &rom
.get_rom(i
);
148 auto xml
= &rom
.get_markup(i
);
149 if(img
->sha_256
.read() == "") img
= NULL
; //Trivial image.
150 if(xml
->sha_256
.read() == "") xml
= NULL
; //Trivial image.
151 if(!img
&& !xml
) continue;
156 L
.pushstring("filename");
157 L
.pushlstring(img
->filename
);
159 L
.pushstring("hint");
160 L
.pushlstring(img
->namehint
);
162 L
.pushstring("sha256");
163 L
.pushlstring(img
->sha_256
.read());
167 L
.pushstring("xml_filename");
168 L
.pushlstring(xml
->filename
);
170 L
.pushstring("xml_hint");
171 L
.pushlstring(xml
->namehint
);
173 L
.pushstring("xml_sha256");
174 L
.pushlstring(xml
->sha_256
.read());
186 int get_game_info(lua::state
& L
, lua::parameters
& P
)
188 uint64_t framemagic
[4];
190 auto& rom
= core
.rom
->get_internal_rom_type();
191 auto& sysreg
= rom
.combine_region(rom
.get_region());
193 sysreg
.fill_framerate_magic(framemagic
);
197 L
.pushstring("core");
198 L
.pushlstring(rom
.get_core_identifier());
200 L
.pushstring("core_short");
201 L
.pushlstring(rom
.get_core_shortname());
203 L
.pushstring("type");
204 L
.pushlstring(rom
.get_iname());
206 L
.pushstring("type_long");
207 L
.pushlstring(rom
.get_hname());
209 L
.pushstring("region");
210 L
.pushlstring(rom
.get_region().get_iname());
212 L
.pushstring("region_long");
213 L
.pushlstring(rom
.get_region().get_hname());
215 L
.pushstring("gametype");
216 L
.pushlstring(sysreg
.get_name());
218 L
.pushstring("fps_n");
219 L
.pushnumber(framemagic
[1]);
221 L
.pushstring("fps_d");
222 L
.pushnumber(framemagic
[0]);
225 L
.pushnumber(1.0 * framemagic
[1] / framemagic
[0]);
231 int get_last_movie(lua::state
& L
, lua::parameters
& P
)
233 auto m
= CORE().mlogic
->get_mfile().get_filename();
241 lua::functions
LUA_movie_fns(lua_func_misc
, "movie", {
242 {"currentframe", currentframe
},
243 {"lagcount", lagcounter
},
244 {"framecount", framecount
},
245 {"rerecords", rerecords
},
246 {"readonly", readonly
},
247 {"readwrite", readwrite
},
248 {"frame_subframes", frame_subframes
},
249 {"read_subframes", read_subframes
},
250 {"read_rtc", read_rtc
},
251 {"unsafe_rewind", unsafe_rewind
},
252 {"to_rewind", to_rewind
},
253 {"rom_loaded", rom_loaded
},
254 {"get_rom_info", get_rom_info
},
255 {"get_game_info", get_game_info
},
256 {"get_last_movie", get_last_movie
},