]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_util.cpp
Code modernization: subfolders (#6283)
[dragonfireclient.git] / src / script / lua_api / l_util.cpp
index 26e2b985cd87e554730b1c3bec9f23f130614298..dffbc66d16e2d6644c4b076d543112f1752ec3a6 100644 (file)
@@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "lua_api/l_util.h"
 #include "lua_api/l_internal.h"
+#include "lua_api/l_settings.h"
 #include "common/c_converter.h"
 #include "common/c_content.h"
 #include "cpp_api/s_async.h"
@@ -77,71 +78,6 @@ int ModApiUtil::l_get_us_time(lua_State *L)
        return 1;
 }
 
-#define CHECK_SECURE_SETTING(L, name) \
-       if (ScriptApiSecurity::isSecure(L) && \
-                       name.compare(0, 7, "secure.") == 0) { \
-               throw LuaError("Attempt to set secure setting."); \
-       }
-
-// setting_set(name, value)
-int ModApiUtil::l_setting_set(lua_State *L)
-{
-       NO_MAP_LOCK_REQUIRED;
-       std::string name = luaL_checkstring(L, 1);
-       std::string value = luaL_checkstring(L, 2);
-       CHECK_SECURE_SETTING(L, name);
-       g_settings->set(name, value);
-       return 0;
-}
-
-// setting_get(name)
-int ModApiUtil::l_setting_get(lua_State *L)
-{
-       NO_MAP_LOCK_REQUIRED;
-       const char *name = luaL_checkstring(L, 1);
-       try{
-               std::string value = g_settings->get(name);
-               lua_pushstring(L, value.c_str());
-       } catch(SettingNotFoundException &e){
-               lua_pushnil(L);
-       }
-       return 1;
-}
-
-// setting_setbool(name)
-int ModApiUtil::l_setting_setbool(lua_State *L)
-{
-       NO_MAP_LOCK_REQUIRED;
-       std::string name = luaL_checkstring(L, 1);
-       bool value = lua_toboolean(L, 2);
-       CHECK_SECURE_SETTING(L, name);
-       g_settings->setBool(name, value);
-       return 0;
-}
-
-// setting_getbool(name)
-int ModApiUtil::l_setting_getbool(lua_State *L)
-{
-       NO_MAP_LOCK_REQUIRED;
-       const char *name = luaL_checkstring(L, 1);
-       try{
-               bool value = g_settings->getBool(name);
-               lua_pushboolean(L, value);
-       } catch(SettingNotFoundException &e){
-               lua_pushnil(L);
-       }
-       return 1;
-}
-
-// setting_save()
-int ModApiUtil::l_setting_save(lua_State *L)
-{
-       NO_MAP_LOCK_REQUIRED;
-       if(g_settings_path != "")
-               g_settings->updateConfigFile(g_settings_path.c_str());
-       return 0;
-}
-
 // parse_json(str[, nullvalue])
 int ModApiUtil::l_parse_json(lua_State *L)
 {
@@ -238,7 +174,7 @@ int ModApiUtil::l_get_dig_params(lua_State *L)
 int ModApiUtil::l_get_hit_params(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
-       UNORDERED_MAP<std::string, int> groups;
+       std::unordered_map<std::string, int> groups;
        read_groups(L, 1, groups);
        ToolCapabilities tp = read_tool_capabilities(L, 2);
        if(lua_isnoneornil(L, 3))
@@ -310,7 +246,7 @@ int ModApiUtil::l_get_builtin_path(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
 
-       std::string path = porting::path_share + DIR_DELIM + "builtin";
+       std::string path = porting::path_share + DIR_DELIM + "builtin" + DIR_DELIM;
        lua_pushstring(L, path.c_str());
 
        return 1;
@@ -398,7 +334,8 @@ int ModApiUtil::l_get_dir_list(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        const char *path = luaL_checkstring(L, 1);
-       short is_dir = lua_isboolean(L, 2) ? lua_toboolean(L, 2) : -1;
+       bool list_all = !lua_isboolean(L, 2); // if its not a boolean list all
+       bool list_dirs = lua_toboolean(L, 2); // true: list dirs, false: list files
 
        CHECK_SECURE_PATH(L, path, false);
 
@@ -407,9 +344,9 @@ int ModApiUtil::l_get_dir_list(lua_State *L)
        int index = 0;
        lua_newtable(L);
 
-       for (size_t i = 0; i < list.size(); i++) {
-               if (is_dir == -1 || is_dir == list[i].dir) {
-                       lua_pushstring(L, list[i].name.c_str());
+       for (const fs::DirListNode &dln : list) {
+               if (list_all || list_dirs == dln.dir) {
+                       lua_pushstring(L, dln.name.c_str());
                        lua_rawseti(L, -2, ++index);
                }
        }
@@ -477,7 +414,7 @@ int ModApiUtil::l_get_version(lua_State *L)
        lua_pushstring(L, g_version_string);
        lua_setfield(L, table, "string");
 
-       if (strcmp(g_version_string, g_version_hash)) {
+       if (strcmp(g_version_string, g_version_hash) != 0) {
                lua_pushstring(L, g_version_hash);
                lua_setfield(L, table, "hash");
        }
@@ -492,12 +429,6 @@ void ModApiUtil::Initialize(lua_State *L, int top)
 
        API_FCT(get_us_time);
 
-       API_FCT(setting_set);
-       API_FCT(setting_get);
-       API_FCT(setting_setbool);
-       API_FCT(setting_getbool);
-       API_FCT(setting_save);
-
        API_FCT(parse_json);
        API_FCT(write_json);
 
@@ -523,36 +454,56 @@ void ModApiUtil::Initialize(lua_State *L, int top)
        API_FCT(decode_base64);
 
        API_FCT(get_version);
+
+       LuaSettings::create(L, g_settings, g_settings_path);
+       lua_setfield(L, top, "settings");
 }
 
-void ModApiUtil::InitializeAsync(AsyncEngine& engine)
+void ModApiUtil::InitializeClient(lua_State *L, int top)
 {
-       ASYNC_API_FCT(log);
+       API_FCT(log);
 
-       ASYNC_API_FCT(get_us_time);
+       API_FCT(get_us_time);
 
-       //ASYNC_API_FCT(setting_set);
-       ASYNC_API_FCT(setting_get);
-       //ASYNC_API_FCT(setting_setbool);
-       ASYNC_API_FCT(setting_getbool);
-       //ASYNC_API_FCT(setting_save);
+       API_FCT(parse_json);
+       API_FCT(write_json);
 
-       ASYNC_API_FCT(parse_json);
-       ASYNC_API_FCT(write_json);
+       API_FCT(is_yes);
 
-       ASYNC_API_FCT(is_yes);
+       API_FCT(compress);
+       API_FCT(decompress);
 
-       ASYNC_API_FCT(get_builtin_path);
+       API_FCT(encode_base64);
+       API_FCT(decode_base64);
+
+       API_FCT(get_version);
+}
 
-       ASYNC_API_FCT(compress);
-       ASYNC_API_FCT(decompress);
+void ModApiUtil::InitializeAsync(lua_State *L, int top)
+{
+       API_FCT(log);
 
-       ASYNC_API_FCT(mkdir);
-       ASYNC_API_FCT(get_dir_list);
+       API_FCT(get_us_time);
 
-       ASYNC_API_FCT(encode_base64);
-       ASYNC_API_FCT(decode_base64);
+       API_FCT(parse_json);
+       API_FCT(write_json);
+
+       API_FCT(is_yes);
+
+       API_FCT(get_builtin_path);
+
+       API_FCT(compress);
+       API_FCT(decompress);
+
+       API_FCT(mkdir);
+       API_FCT(get_dir_list);
+
+       API_FCT(encode_base64);
+       API_FCT(decode_base64);
+
+       API_FCT(get_version);
 
-       ASYNC_API_FCT(get_version);
+       LuaSettings::create(L, g_settings, g_settings_path);
+       lua_setfield(L, top, "settings");
 }