Upload UI
[lsnes.git] / src / lua / movie.cpp
blob497024c0da9efaaf666266616e57b881ff136e66
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"
8 namespace
10 function_ptr_luafun mcurframe(lua_func_misc, "movie.currentframe", [](lua_state& L, const std::string& fname)
11 -> int {
12 auto& m = get_movie();
13 L.pushnumber(m.get_current_frame());
14 return 1;
15 });
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());
20 return 1;
21 });
23 function_ptr_luafun mrrs(lua_func_misc, "movie.rerecords", [](lua_state& L, const std::string& fname) -> int {
24 L.pushnumber(rrdata.count());
25 return 1;
26 });
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);
31 return 1;
32 });
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);
37 return 0;
38 });
40 function_ptr_luafun mfs(lua_func_misc, "movie.frame_subframes", [](lua_state& L, const std::string& fname)
41 -> int {
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));
45 return 1;
46 });
48 function_ptr_luafun mrs(lua_func_misc, "movie.read_subframes", [](lua_state& L, const std::string& fname)
49 -> int {
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);
54 L.newtable();
56 for(size_t i = 0; i < r.get_index_count(); i++) {
57 L.pushnumber(i);
58 L.pushnumber(r.axis2(i));
59 L.settable(-3);
61 return 1;
62 });
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);
67 return 2;
68 });
70 function_ptr_luafun musv(lua_func_misc, "movie.unsafe_rewind", [](lua_state& L, const std::string& fname)
71 -> int {
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)) {
76 //Load the save.
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);
80 } else
81 throw std::runtime_error("movie.unsafe_rewind: Expected nil or UNSAFEREWIND as 1st argument");
82 return 0;
83 });
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.
102 uint64_t f = 0;
103 uint64_t s = mfile.input.size();
104 u2->ptr = 0;
105 while(++f < u2->frame) {
106 if(u2->ptr < s)
107 u2->ptr++;
108 while(u2->ptr < s && !mfile.input[u2->ptr].sync())
109 u2->ptr++;
111 return 1;