Backport open url feature (#221)

This commit is contained in:
Bektur 2021-08-19 17:38:53 +06:00 committed by MoNTE48
parent 9188d2bbf7
commit 02c91ac585
8 changed files with 88 additions and 5 deletions

View File

@ -28,7 +28,9 @@ import static com.multicraft.game.helpers.Utilities.getTotalMem;
import static com.multicraft.game.helpers.Utilities.makeFullScreen;
import android.app.NativeActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
@ -180,4 +182,9 @@ public class GameActivity extends NativeActivity {
public void notifyExitGame() {
}
public void openURI(String uri) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(browserIntent);
}
}

View File

@ -100,14 +100,15 @@ return {
caption = fgettext("Credits"),
cbf_formspec = function()
local version = core.get_version()
return "label[0.1,-0.1;" ..
return "label[0.1,0;" ..
"MultiCraft Open Source, ver. " .. version.string .. "\n" ..
"Copyright (C) 2014-2021 MultiCraft Development Team\n" ..
"Licence: LGPLv3.0+ and CC-BY-SA 4.0\n" ..
"Home page: http://multicraft.world]" ..
"License: GNU LGPLv3.0+ and CC BY-SA 4.0]" ..
"button[9.5,0;2.5,0.5;homepage;Home Page]" ..
"button[9.5,0.8;2.5,0.5;privacy;Privacy Policy]" ..
"tablecolumns[color;text]" ..
"tableoptions[background=#999999;highlight=#00000000;border=true]" ..
"table[0,1.6;11.8,3.8;list_credits;" ..
"table[0,1.5;11.8,4;list_credits;" ..
"#FFFF00," .. fgettext("MultiCraft Developers") .. ",," ..
buildCreditList(multicraft_developers) .. ",,," ..
"#FFFF00," .. fgettext("Minetest Developers") .. ",," ..
@ -119,5 +120,13 @@ return {
"#FFFF00," .. fgettext("Previous Contributors") .. ",," ..
buildCreditList(previous_contributors) .. "," ..
";1]"
end
end,
cbf_button_handler = function(_, fields)
if fields.homepage then
core.open_url("http://multicraft.world")
end
if fields.privacy then
core.open_url("http://multicraft.world/privacy")
end
end,
}

View File

@ -940,6 +940,38 @@ void attachOrCreateConsole(void)
#endif
}
static bool open_uri(const std::string &uri)
{
if (uri.find_first_of("\r\n") != std::string::npos) {
errorstream << "Unable to open URI as it is invalid, contains new line: " << uri << std::endl;
return false;
}
#if defined(_WIN32)
return (intptr_t)ShellExecuteA(NULL, NULL, uri.c_str(), NULL, NULL, SW_SHOWNORMAL) > 32;
#elif defined(__ANDROID__)
openURIAndroid(uri);
return true;
#elif defined(__APPLE__)
const char *argv[] = {"open", uri.c_str(), NULL};
return posix_spawnp(NULL, "open", NULL, NULL, (char**)argv,
(*_NSGetEnviron())) == 0;
#else
const char *argv[] = {"xdg-open", uri.c_str(), NULL};
return posix_spawnp(NULL, "xdg-open", NULL, NULL, (char**)argv, environ) == 0;
#endif
}
bool open_url(const std::string &url)
{
if (url.substr(0, 7) != "http://" && url.substr(0, 8) != "https://") {
errorstream << "Unable to open browser as URL is missing schema: " << url << std::endl;
return false;
}
return open_uri(url);
}
// Load performance counter frequency only once at startup
#ifdef _WIN32

View File

@ -353,6 +353,18 @@ bool secure_rand_fill_buf(void *buf, size_t len);
// This attaches to the parents process console, or creates a new one if it doesnt exist.
void attachOrCreateConsole(void);
/**
* Opens URL in default web browser
*
* Must begin with http:// or https://, and not contain any new lines
*
* @param url The URL
* @return true on success, false on failure
*/
bool open_url(const std::string &url);
} // namespace porting
#ifdef __ANDROID__

View File

@ -229,6 +229,16 @@ void showInputDialog(const std::string &acceptButton, const std::string &hint,
jacceptButton, jhint, jcurrent, jeditType);
}
void openURIAndroid(const std::string &url)
{
jmethodID url_open = jnienv->GetMethodID(nativeActivity, "openURI",
"(Ljava/lang/String;)V");
FATAL_ERROR_IF(url_open == nullptr,
"porting::openURIAndroid unable to find java openURI method");
jstring jurl = jnienv->NewStringUTF(url.c_str());
jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl);
}
int getInputDialogState()
{
jmethodID dialogstate = jnienv->GetMethodID(nativeActivity,

View File

@ -58,6 +58,8 @@ void initializePathsAndroid();
void showInputDialog(const std::string &acceptButton,
const std::string &hint, const std::string &current, int editType);
void openURIAndroid(const std::string &url);
/**
* WORKAROUND for not working callbacks from java -> c++
* get current state of input dialog

View File

@ -1120,6 +1120,14 @@ int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
return 1;
}
/******************************************************************************/
int ModApiMainMenu::l_open_url(lua_State *L)
{
std::string url = luaL_checkstring(L, 1);
lua_pushboolean(L, porting::open_url(url));
return 1;
}
/******************************************************************************/
int ModApiMainMenu::l_do_async_callback(lua_State *L)
{
@ -1188,6 +1196,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(get_screen_info);
API_FCT(get_min_supp_proto);
API_FCT(get_max_supp_proto);
API_FCT(open_url);
API_FCT(do_async_callback);
}

View File

@ -139,6 +139,8 @@ private:
static int l_get_max_supp_proto(lua_State *L);
// other
static int l_open_url(lua_State *L);
// async
static int l_do_async_callback(lua_State *L);