Lua Add getters for various paths

This commit is contained in:
Ilari Liusvaara 2016-04-15 09:18:48 +03:00
parent d287f64c14
commit a3f1d7c8a4
3 changed files with 203 additions and 0 deletions

99
lua.lyx
View file

@ -8261,6 +8261,105 @@ zip.writer: Class ZIPWRITER
See class ZIPWRITER.
\end_layout
\begin_layout Section
Table paths
\end_layout
\begin_layout Subsection
paths.get_executable_file: Get the name of lsnes executable file
\end_layout
\begin_layout Itemize
Syntax: String paths.get_executable_file()
\end_layout
\begin_layout Standard
Returns the filename of the lsnes executable (together with path), if known.
If unknown, returns nil.
\end_layout
\begin_layout Subsection
paths.get_executable_path: Get the path of lsnes executable file
\end_layout
\begin_layout Itemize
Syntax: String paths.get_executable_path()
\end_layout
\begin_layout Standard
Returns the directory of the lsnes executable path, if known.
If unknown, returns nil.
\end_layout
\begin_layout Subsection
paths.get_config_path: Get the path of lsnes config file
\end_layout
\begin_layout Itemize
Syntax: String paths.get_config_path()
\end_layout
\begin_layout Standard
Returns the directory of the lsnes configuration.
\end_layout
\begin_layout Subsection
paths.get_rompath: Get the path of ROM files
\end_layout
\begin_layout Itemize
Syntax: String paths.get_rompath()
\end_layout
\begin_layout Standard
Returns the default directory for various ROMs.
\end_layout
\begin_layout Subsection
paths.get_firmwarepath: Get the path of firmware files
\end_layout
\begin_layout Itemize
Syntax: String paths.get_firmwarepath()
\end_layout
\begin_layout Standard
Returns the default directory for various firmwares.
\end_layout
\begin_layout Subsection
paths.get_slotpath: Get the path of save slot files
\end_layout
\begin_layout Itemize
Syntax: String paths.get_slotpath()
\end_layout
\begin_layout Standard
Returns the default directory for various save slots.
If in project context, returns the project directory.
\end_layout
\begin_layout Subsection
paths.get_save_slot_file: Get save slot filename
\end_layout
\begin_layout Itemize
Syntax: String paths.get_slotpath(string name)
\end_layout
\begin_layout Itemize
Syntax: String paths.get_slotpath(integer slotnum, bool for_save)
\end_layout
\begin_layout Standard
Resolve the filename of specified save slot <name> or <slotnum>.
Projects and save branches are taken into account.
If <for_save> is true, the filename is always for current branch.
If <for_save> is false, branch tree is walked upwards until root is reached
or desired file is located.
\end_layout
\begin_layout Standard
\begin_inset Newpage pagebreak
\end_inset

BIN
lua.pdf

Binary file not shown.

104
src/lua/paths.cpp Normal file
View file

@ -0,0 +1,104 @@
#include "lua/internal.hpp"
#include "interface/romtype.hpp"
#include "library/running-executable.hpp"
#include "library/string.hpp"
#include "core/misc.hpp"
#include "core/settings.hpp"
#include "core/moviedata.hpp"
#include "core/instance.hpp"
#include "core/project.hpp"
namespace
{
class value_unknown {};
template<std::string(*value)(lua::state& L, lua::parameters& P)>
int lua_push_string_fn(lua::state& L, lua::parameters& P)
{
try {
L.pushlstring(value(L, P));
} catch(value_unknown) {
L.pushnil();
}
return 1;
}
std::string get_executable_file(lua::state& L, lua::parameters& P)
{
try {
return running_executable();
} catch(...) {
throw value_unknown();
}
}
std::string get_executable_path(lua::state& L, lua::parameters& P)
{
auto fname = get_executable_file(L, P);
#if defined(__WIN32__) || defined(__WIN64__)
const char* pathsep = "/\\";
#else
const char* pathsep = "/";
#endif
size_t split = fname.find_last_of(pathsep);
if(split >= fname.length()) throw value_unknown();
return fname.substr(0, split);
}
std::string l_get_config_path(lua::state& L, lua::parameters& P)
{
return get_config_path();
}
std::string get_rompath(lua::state& L, lua::parameters& P)
{
return SET_rompath(*CORE().settings);
}
std::string get_firmwarepath(lua::state& L, lua::parameters& P)
{
return SET_firmwarepath(*CORE().settings);
}
std::string get_slotpath(lua::state& L, lua::parameters& P)
{
auto& core = CORE();
auto p = core.project->get();
if(p) {
return p->directory;
} else {
return SET_slotpath(*core.settings);
}
}
std::string get_save_slot_file(lua::state& L, lua::parameters& P)
{
uint32_t num;
std::string name;
bool for_save;
int binary_flag;
std::string ret;
if(P.is_number()) {
P(num, for_save);
name = (stringfmt() << "$SLOT:" << num).str();
} else if(P.is_string()) {
P(name);
for_save = false;
} else {
(stringfmt() << "Expected string or number as argument #1 to " << P.get_fname()).throwex();
}
ret = translate_name_mprefix(name, binary_flag, for_save ? 1 : -1);
return ret;
}
lua::functions LUA_paths_fns(lua_func_misc, "paths", {
{"get_executable_file", lua_push_string_fn<get_executable_file>},
{"get_executable_path", lua_push_string_fn<get_executable_path>},
{"get_config_path", lua_push_string_fn<l_get_config_path>},
{"get_rompath", lua_push_string_fn<get_rompath>},
{"get_firmwarepath", lua_push_string_fn<get_firmwarepath>},
{"get_slotpath", lua_push_string_fn<get_slotpath>},
{"get_save_slot_file", lua_push_string_fn<get_save_slot_file>},
});
}