Refactor Lua support
[lsnes.git] / src / lua / movie.cpp
blob16b39f9cd2c139c0f9d6cea3c7d8f88a61739ed8
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(LS, "movie.currentframe", [](lua_state& L, const std::string& fname) -> int {
11 auto& m = get_movie();
12 L.pushnumber(m.get_current_frame());
13 return 1;
14 });
16 function_ptr_luafun mfc(LS, "movie.framecount", [](lua_state& L, const std::string& fname) -> int {
17 auto& m = get_movie();
18 L.pushnumber(m.get_frame_count());
19 return 1;
20 });
22 function_ptr_luafun mrrs(LS, "movie.rerecords", [](lua_state& L, const std::string& fname) -> int {
23 L.pushnumber(rrdata::count());
24 return 1;
25 });
27 function_ptr_luafun mro(LS, "movie.readonly", [](lua_state& L, const std::string& fname) -> int {
28 auto& m = get_movie();
29 L.pushboolean(m.readonly_mode() ? 1 : 0);
30 return 1;
31 });
33 function_ptr_luafun mrw(LS, "movie.readwrite", [](lua_state& L, const std::string& fname) -> int {
34 auto& m = get_movie();
35 m.readonly_mode(false);
36 return 0;
37 });
39 function_ptr_luafun mfs(LS, "movie.frame_subframes", [](lua_state& L, const std::string& fname) -> int {
40 uint64_t frame = L.get_numeric_argument<uint64_t>(1, "movie.frame_subframes");
41 auto& m = get_movie();
42 L.pushnumber(m.frame_subframes(frame));
43 return 1;
44 });
46 function_ptr_luafun mrs(LS, "movie.read_subframes", [](lua_state& L, const std::string& fname) -> int {
47 uint64_t frame = L.get_numeric_argument<uint64_t>(1, "movie.frame_subframes");
48 uint64_t subframe = L.get_numeric_argument<uint64_t>(2, "movie.frame_subframes");
49 auto& m = get_movie();
50 controller_frame r = m.read_subframe(frame, subframe);
51 L.newtable();
53 L.pushnumber(0);
54 L.pushnumber(r.sync() ? 1 : 0);
55 L.settable(-3);
56 L.pushnumber(1);
57 L.pushnumber(r.axis3(0, 0, 1) ? 1 : 0);
58 L.settable(-3);
59 L.pushnumber(2);
60 L.pushnumber(r.axis3(0, 0, 2));
61 L.settable(-3);
62 L.pushnumber(3);
63 L.pushnumber(r.axis3(0, 0, 3));
64 L.settable(-3);
66 for(size_t i = 4; i < r.get_index_count(); i++) {
67 L.pushnumber(i);
68 L.pushnumber(r.axis2(i));
69 L.settable(-3);
71 return 1;
72 });
74 function_ptr_luafun rrc(LS, "movie.read_rtc", [](lua_state& L, const std::string& fname) -> int {
75 L.pushnumber(our_movie.rtc_second);
76 L.pushnumber(our_movie.rtc_subsecond);
77 return 2;
78 });
80 function_ptr_luafun musv(LS, "movie.unsafe_rewind", [](lua_state& L, const std::string& fname) -> int {
81 if(L.isnoneornil(1)) {
82 //Start process to mark save.
83 mainloop_signal_need_rewind(NULL);
84 } else if(lua_class<lua_unsaferewind>::is(L, 1)) {
85 //Load the save.
86 lua_obj_pin<lua_unsaferewind>* u = lua_class<lua_unsaferewind>::pin(L, 1, fname.c_str());
87 mainloop_signal_need_rewind(u);
88 } else {
89 L.pushstring("movie.unsafe_rewind: Expected nil or UNSAFEREWIND as 1st argument");
90 L.error();
91 return 0;
93 });