Skip to content

Commit 664f5ce

Browse files
authoredDec 19, 2020
Add open user data button to main menu (#10579)
1 parent 025035d commit 664f5ce

File tree

9 files changed

+100
-19
lines changed

9 files changed

+100
-19
lines changed
 

‎build/android/app/src/main/java/net/minetest/minetest/GameActivity.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ public int getDisplayWidth() {
142142
return getResources().getDisplayMetrics().widthPixels;
143143
}
144144

145-
public void openURL(String url) {
146-
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
145+
public void openURI(String uri) {
146+
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
147147
startActivity(browserIntent);
148148
}
149149
}

‎builtin/mainmenu/tab_credits.lua

+17-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ return {
100100
cbf_formspec = function(tabview, name, tabdata)
101101
local logofile = defaulttexturedir .. "logo.png"
102102
local version = core.get_version()
103-
return "image[0.5,1;" .. core.formspec_escape(logofile) .. "]" ..
104-
"label[0.5,2.8;" .. version.project .. " " .. version.string .. "]" ..
105-
"button[0.5,3;2,2;homepage;minetest.net]" ..
103+
local fs = "image[0.75,0.5;2.2,2.2;" .. core.formspec_escape(logofile) .. "]" ..
104+
"style[label_button;border=false]" ..
105+
"button[0.5,2;2.5,2;label_button;" .. version.project .. " " .. version.string .. "]" ..
106+
"button[0.75,2.75;2,2;homepage;minetest.net]" ..
106107
"tablecolumns[color;text]" ..
107108
"tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
108109
"table[3.5,-0.25;8.5,6.05;list_credits;" ..
@@ -115,10 +116,23 @@ return {
115116
"#FFFF00," .. fgettext("Previous Contributors") .. ",," ..
116117
buildCreditList(previous_contributors) .. "," ..
117118
";1]"
119+
120+
if PLATFORM ~= "Android" then
121+
fs = fs .. "tooltip[userdata;" ..
122+
fgettext("Opens the directory that contains user-provided worlds, games, mods,\n" ..
123+
"and texture packs in a file manager / explorer.") .. "]"
124+
fs = fs .. "button[0,4.75;3.5,1;userdata;" .. fgettext("Open User Data Directory") .. "]"
125+
end
126+
127+
return fs
118128
end,
119129
cbf_button_handler = function(this, fields, name, tabdata)
120130
if fields.homepage then
121131
core.open_url("https://www.minetest.net")
122132
end
133+
134+
if fields.userdata then
135+
core.open_dir(core.get_user_path())
136+
end
123137
end,
124138
}

‎doc/menu_lua_api.txt

+7
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ core.get_max_supp_proto()
4343
core.open_url(url)
4444
^ opens the URL in a web browser, returns false on failure.
4545
^ Must begin with http:// or https://
46+
core.open_dir(path)
47+
^ opens the path in the system file browser/explorer, returns false on failure.
48+
^ Must be an existing directory.
4649
core.get_version() (possible in async calls)
4750
^ returns current core version
4851

4952

53+
5054
Filesystem
5155
----------
5256

@@ -207,6 +211,9 @@ Content and Packages
207211
Content - an installed mod, modpack, game, or texture pack (txt)
208212
Package - content which is downloadable from the content db, may or may not be installed.
209213

214+
* core.get_user_path() (possible in async calls)
215+
* returns path to global user data,
216+
the directory that contains user-provided mods, worlds, games, and texture packs.
210217
* core.get_modpath() (possible in async calls)
211218
* returns path to global modpath
212219
* core.get_clientmodpath() (possible in async calls)

‎src/porting.cpp

+27-8
Original file line numberDiff line numberDiff line change
@@ -719,29 +719,48 @@ int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...)
719719
return c;
720720
}
721721

722-
bool openURL(const std::string &url)
722+
static bool open_uri(const std::string &uri)
723723
{
724-
if ((url.substr(0, 7) != "http://" && url.substr(0, 8) != "https://") ||
725-
url.find_first_of("\r\n") != std::string::npos) {
726-
errorstream << "Invalid url: " << url << std::endl;
724+
if (uri.find_first_of("\r\n") != std::string::npos) {
725+
errorstream << "Unable to open URI as it is invalid, contains new line: " << uri << std::endl;
727726
return false;
728727
}
729728

730729
#if defined(_WIN32)
731-
return (intptr_t)ShellExecuteA(NULL, NULL, url.c_str(), NULL, NULL, SW_SHOWNORMAL) > 32;
730+
return (intptr_t)ShellExecuteA(NULL, NULL, uri.c_str(), NULL, NULL, SW_SHOWNORMAL) > 32;
732731
#elif defined(__ANDROID__)
733-
openURLAndroid(url);
732+
openURIAndroid(uri);
734733
return true;
735734
#elif defined(__APPLE__)
736-
const char *argv[] = {"open", url.c_str(), NULL};
735+
const char *argv[] = {"open", uri.c_str(), NULL};
737736
return posix_spawnp(NULL, "open", NULL, NULL, (char**)argv,
738737
(*_NSGetEnviron())) == 0;
739738
#else
740-
const char *argv[] = {"xdg-open", url.c_str(), NULL};
739+
const char *argv[] = {"xdg-open", uri.c_str(), NULL};
741740
return posix_spawnp(NULL, "xdg-open", NULL, NULL, (char**)argv, environ) == 0;
742741
#endif
743742
}
744743

744+
bool open_url(const std::string &url)
745+
{
746+
if (url.substr(0, 7) != "http://" && url.substr(0, 8) != "https://") {
747+
errorstream << "Unable to open browser as URL is missing schema: " << url << std::endl;
748+
return false;
749+
}
750+
751+
return open_uri(url);
752+
}
753+
754+
bool open_directory(const std::string &path)
755+
{
756+
if (!fs::IsDir(path)) {
757+
errorstream << "Unable to open directory as it does not exist: " << path << std::endl;
758+
return false;
759+
}
760+
761+
return open_uri(path);
762+
}
763+
745764
// Load performance counter frequency only once at startup
746765
#ifdef _WIN32
747766

‎src/porting.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,25 @@ void attachOrCreateConsole();
332332

333333
int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...);
334334

335-
bool openURL(const std::string &url);
335+
/**
336+
* Opens URL in default web browser
337+
*
338+
* Must begin with http:// or https://, and not contain any new lines
339+
*
340+
* @param url The URL
341+
* @return true on success, false on failure
342+
*/
343+
bool open_url(const std::string &url);
344+
345+
/**
346+
* Opens a directory in the default file manager
347+
*
348+
* The directory must exist.
349+
*
350+
* @param path Path to directory
351+
* @return true on success, false on failure
352+
*/
353+
bool open_directory(const std::string &path);
336354

337355
} // namespace porting
338356

‎src/porting_android.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,13 @@ void showInputDialog(const std::string &acceptButton, const std::string &hint,
213213
jacceptButton, jhint, jcurrent, jeditType);
214214
}
215215

216-
void openURLAndroid(const std::string &url)
216+
void openURIAndroid(const std::string &url)
217217
{
218-
jmethodID url_open = jnienv->GetMethodID(nativeActivity, "openURL",
218+
jmethodID url_open = jnienv->GetMethodID(nativeActivity, "openURI",
219219
"(Ljava/lang/String;)V");
220220

221221
FATAL_ERROR_IF(url_open == nullptr,
222-
"porting::openURLAndroid unable to find java openURL method");
222+
"porting::openURIAndroid unable to find java openURI method");
223223

224224
jstring jurl = jnienv->NewStringUTF(url.c_str());
225225
jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl);

‎src/porting_android.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void initializePathsAndroid();
5858
void showInputDialog(const std::string &acceptButton,
5959
const std::string &hint, const std::string &current, int editType);
6060

61-
void openURLAndroid(const std::string &url);
61+
void openURIAndroid(const std::string &url);
6262

6363
/**
6464
* WORKAROUND for not working callbacks from java -> c++

‎src/script/lua_api/l_mainmenu.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,14 @@ int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
686686
}
687687

688688

689+
/******************************************************************************/
690+
int ModApiMainMenu::l_get_user_path(lua_State *L)
691+
{
692+
std::string path = fs::RemoveRelativePathComponents(porting::path_user);
693+
lua_pushstring(L, path.c_str());
694+
return 1;
695+
}
696+
689697
/******************************************************************************/
690698
int ModApiMainMenu::l_get_modpath(lua_State *L)
691699
{
@@ -1067,7 +1075,15 @@ int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
10671075
int ModApiMainMenu::l_open_url(lua_State *L)
10681076
{
10691077
std::string url = luaL_checkstring(L, 1);
1070-
lua_pushboolean(L, porting::openURL(url));
1078+
lua_pushboolean(L, porting::open_url(url));
1079+
return 1;
1080+
}
1081+
1082+
/******************************************************************************/
1083+
int ModApiMainMenu::l_open_dir(lua_State *L)
1084+
{
1085+
std::string path = luaL_checkstring(L, 1);
1086+
lua_pushboolean(L, porting::open_directory(path));
10711087
return 1;
10721088
}
10731089

@@ -1113,6 +1129,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
11131129
API_FCT(set_background);
11141130
API_FCT(set_topleft_text);
11151131
API_FCT(get_mapgen_names);
1132+
API_FCT(get_user_path);
11161133
API_FCT(get_modpath);
11171134
API_FCT(get_clientmodpath);
11181135
API_FCT(get_gamepath);
@@ -1134,6 +1151,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
11341151
API_FCT(get_min_supp_proto);
11351152
API_FCT(get_max_supp_proto);
11361153
API_FCT(open_url);
1154+
API_FCT(open_dir);
11371155
API_FCT(do_async_callback);
11381156
}
11391157

@@ -1144,6 +1162,7 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
11441162
API_FCT(get_games);
11451163
API_FCT(get_favorites);
11461164
API_FCT(get_mapgen_names);
1165+
API_FCT(get_user_path);
11471166
API_FCT(get_modpath);
11481167
API_FCT(get_clientmodpath);
11491168
API_FCT(get_gamepath);

‎src/script/lua_api/l_mainmenu.h

+4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class ModApiMainMenu: public ModApiBase
112112

113113
static int l_get_mainmenu_path(lua_State *L);
114114

115+
static int l_get_user_path(lua_State *L);
116+
115117
static int l_get_modpath(lua_State *L);
116118

117119
static int l_get_clientmodpath(lua_State *L);
@@ -148,6 +150,8 @@ class ModApiMainMenu: public ModApiBase
148150
// other
149151
static int l_open_url(lua_State *L);
150152

153+
static int l_open_dir(lua_State *L);
154+
151155

152156
// async
153157
static int l_do_async_callback(lua_State *L);

0 commit comments

Comments
 (0)
Please sign in to comment.