1 #include "lua/internal.hpp"
2 #include "lua/unsaferewind.hpp"
3 #include "core/movie.hpp"
4 #include "core/rrdata.hpp"
5 #include "core/moviedata.hpp"
6 #include "core/mainloop.hpp"
10 function_ptr_luafun
mcurframe(lua_func_misc
, "movie.currentframe", [](lua_state
& L
, const std::string
& fname
)
12 auto& m
= get_movie();
13 L
.pushnumber(m
.get_current_frame());
17 function_ptr_luafun
mfc(lua_func_misc
, "movie.framecount", [](lua_state
& L
, const std::string
& fname
) -> int {
18 auto& m
= get_movie();
19 L
.pushnumber(m
.get_frame_count());
23 function_ptr_luafun
mrrs(lua_func_misc
, "movie.rerecords", [](lua_state
& L
, const std::string
& fname
) -> int {
24 L
.pushnumber(rrdata
.count());
28 function_ptr_luafun
mro(lua_func_misc
, "movie.readonly", [](lua_state
& L
, const std::string
& fname
) -> int {
29 auto& m
= get_movie();
30 L
.pushboolean(m
.readonly_mode() ? 1 : 0);
34 function_ptr_luafun
mrw(lua_func_misc
, "movie.readwrite", [](lua_state
& L
, const std::string
& fname
) -> int {
35 auto& m
= get_movie();
36 m
.readonly_mode(false);
40 function_ptr_luafun
mfs(lua_func_misc
, "movie.frame_subframes", [](lua_state
& L
, const std::string
& fname
)
42 uint64_t frame
= L
.get_numeric_argument
<uint64_t>(1, "movie.frame_subframes");
43 auto& m
= get_movie();
44 L
.pushnumber(m
.frame_subframes(frame
));
48 function_ptr_luafun
mrs(lua_func_misc
, "movie.read_subframes", [](lua_state
& L
, const std::string
& fname
)
50 uint64_t frame
= L
.get_numeric_argument
<uint64_t>(1, "movie.frame_subframes");
51 uint64_t subframe
= L
.get_numeric_argument
<uint64_t>(2, "movie.frame_subframes");
52 auto& m
= get_movie();
53 controller_frame r
= m
.read_subframe(frame
, subframe
);
56 for(size_t i
= 0; i
< r
.get_index_count(); i
++) {
58 L
.pushnumber(r
.axis2(i
));
64 function_ptr_luafun
rrc(lua_func_misc
, "movie.read_rtc", [](lua_state
& L
, const std::string
& fname
) -> int {
65 L
.pushnumber(our_movie
.rtc_second
);
66 L
.pushnumber(our_movie
.rtc_subsecond
);
70 function_ptr_luafun
musv(lua_func_misc
, "movie.unsafe_rewind", [](lua_state
& L
, const std::string
& fname
)
72 if(L
.isnoneornil(1)) {
73 //Start process to mark save.
74 mainloop_signal_need_rewind(NULL
);
75 } else if(lua_class
<lua_unsaferewind
>::is(L
, 1)) {
77 lua_obj_pin
<lua_unsaferewind
>* u
= new lua_obj_pin
<lua_unsaferewind
>(
78 lua_class
<lua_unsaferewind
>::pin(L
, 1, fname
.c_str()));
79 mainloop_signal_need_rewind(u
);
81 throw std::runtime_error("movie.unsafe_rewind: Expected nil or UNSAFEREWIND as 1st argument");
85 function_ptr_luafun
movie_to_rewind(lua_func_misc
, "movie.to_rewind", [](lua_state
& L
,
86 const std::string
& fname
) -> int {
87 std::string filename
= L
.get_string(1, fname
.c_str());
88 moviefile
mfile(filename
, *our_rom
.rtype
);
89 if(!mfile
.is_savestate
)
90 throw std::runtime_error("movie.to_rewind only allows savestates");
91 lua_unsaferewind
* u2
= lua_class
<lua_unsaferewind
>::create(L
);
92 u2
->state
= mfile
.savestate
;
93 if(u2
->state
.size() >= 32)
94 u2
->state
.resize(u2
->state
.size() - 32);
95 u2
->secs
= mfile
.rtc_second
;
96 u2
->ssecs
= mfile
.rtc_subsecond
;
97 u2
->pollcounters
= mfile
.pollcounters
;
98 u2
->lag
= mfile
.lagged_frames
;
99 u2
->frame
= mfile
.save_frame
;
100 u2
->hostmemory
= mfile
.host_memory
;
101 //Now the remaining field ptr is somewhat nastier.
103 uint64_t s
= mfile
.input
.size();
105 while(++f
< u2
->frame
) {
108 while(u2
->ptr
< s
&& !mfile
.input
[u2
->ptr
].sync())