lsnes rr2-β24
[lsnes.git] / src / lua / settings.cpp
blob06e340ffeaed8534eb15341ecbd82011a6e3b64e
1 #include "lua/internal.hpp"
2 #include "core/instance.hpp"
3 #include "core/framerate.hpp"
4 #include "library/settingvar.hpp"
5 #include <limits>
7 namespace
9 int ss_set(lua::state& L, lua::parameters& P)
11 std::string name, value;
13 P(name, value);
15 try {
16 CORE().setcache->set(name, value);
17 } catch(std::exception& e) {
18 L.pushnil();
19 L.pushstring(e.what());
20 return 2;
22 L.pushboolean(1);
23 return 1;
26 int ss_get(lua::state& L, lua::parameters& P)
28 std::string name;
30 P(name);
32 try {
33 std::string value = CORE().setcache->get(name);
34 L.pushlstring(value.c_str(), value.length());
35 return 1;
36 } catch(std::exception& e) {
37 L.pushnil();
38 L.pushstring(e.what());
39 return 2;
43 int ss_getlist(lua::state& L, lua::parameters& P)
45 L.newtable();
46 auto& settings = *CORE().settings;
47 auto set = settings.get_settings_set();
48 for(auto i : set) {
49 auto& setting = settings[i];
50 L.pushlstring(setting.get_iname());
51 L.pushlstring(setting.str());
52 L.settable(-3);
54 return 1;
57 int ss_getspeed(lua::state& L, lua::parameters& P)
59 double spd = CORE().framerate->get_speed_multiplier();
60 if(spd == std::numeric_limits<double>::infinity())
61 L.pushstring("turbo");
62 else
63 L.pushnumber(spd);
64 return 1;
67 int ss_setspeed(lua::state& L, lua::parameters& P)
69 double spd = 0;
70 std::string special;
71 bool is_string = false;
73 if(P.is_string()) {
74 P(special);
75 is_string = true;
76 } else {
77 P(spd);
79 if(special == "turbo")
80 CORE().framerate->set_speed_multiplier(std::numeric_limits<double>::infinity());
81 else if(!is_string && spd > 0)
82 CORE().framerate->set_speed_multiplier(spd);
83 else
84 throw std::runtime_error("Unknown special speed");
85 return 0;
88 lua::functions LUA_settings_fns(lua_func_misc, "settings", {
89 {"set", ss_set},
90 {"get", ss_get},
91 {"get_all", ss_getlist},
92 {"get_speed", ss_getspeed},
93 {"set_speed", ss_setspeed},
94 });