Skip to content

Commit

Permalink
Allow sending URIs to the game (#98)
Browse files Browse the repository at this point in the history
* rpc send uri and cli opt
* add senduri to options
  • Loading branch information
GameParrot authored Jul 6, 2024
1 parent d2c5735 commit 92eda37
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
34 changes: 33 additions & 1 deletion src/jni/jni_support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <regex>
#if !defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE > 8
#include <filesystem>
#endif
Expand Down Expand Up @@ -249,11 +250,16 @@ void JniSupport::startGame(ANativeActivity_createFunc *activityOnCreate,
if (!options.importFilePath.empty()) {
importFile(options.importFilePath);
}
if (!options.sendUri.empty()) {
sendUri(options.sendUri);
}
if (options.useStdinImport) {
std::thread([=]() {
for (std::string line; std::getline(std::cin, line);) {
struct stat buffer;
if ((stat (line.c_str(), &buffer) == 0)) {
if (line.rfind("minecraft://", 0) == 0) {
sendUri(line);
} else if ((stat (line.c_str(), &buffer) == 0)) {
importFile(line);
}
}
Expand All @@ -262,6 +268,32 @@ void JniSupport::startGame(ANativeActivity_createFunc *activityOnCreate,

}

void JniSupport::sendUri(std::string uri) {
if (uri.find("minecraft://") != std::string::npos) {
FakeJni::LocalFrame frame(vm);

std::string host = "";
std::string query = "";

std::regex host_regex(R"(minecraft?:\/\/([^\/?#:]+))");
std::smatch host_match;
if (std::regex_search(uri, host_match, host_regex)) {
host = host_match[1].str();
}

std::regex query_regex(R"(\?([^#]+))");
std::smatch query_match;
if (std::regex_search(uri, query_match, query_regex)) {
query = query_match[1].str();
}

auto urlLaunch = activity->getClass().getMethod("(Ljava/lang/String;Ljava/lang/String;)V", "nativeProcessIntentUriQuery");
urlLaunch->invoke(frame.getJniEnv(), activity.get(), std::make_shared<FakeJni::JString>(host.c_str()), std::make_shared<FakeJni::JString>(query.c_str())); // The game expects it to be parsed using the java getHost() and getQuery() methods
} else {
Log::warn("JniSupport", "Not sending URI %s, not a valid Minecraft URI", uri.c_str());
}
}

void JniSupport::importFile(std::string path) {
#if !defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE > 8
std::string tmpDir = std::filesystem::temp_directory_path().generic_string();
Expand Down
2 changes: 2 additions & 0 deletions src/jni/jni_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ struct JniSupport {

void importFile(std::string path);

void sendUri(std::string uri);

void stopGame();

void waitForGameExit();
Expand Down
8 changes: 7 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ struct RpcCallbackServer : daemon_utils::auto_shutdown_service {
add_handler_async("mcpelauncher-client/sendfile", [this, &support](simpleipc::connection& conn, std::string const& method, nlohmann::json const& data, result_handler const& cb) {
std::vector<std::string> files = data;
for(auto&& file : files) {
support.importFile(file);
if (file.rfind("minecraft://", 0) == 0) {
support.sendUri(file);
} else {
support.importFile(file);
}
}
cb(simpleipc::rpc_json_result::response({}));
});
Expand Down Expand Up @@ -89,6 +93,7 @@ int main(int argc, char* argv[]) {
argparser::arg<std::string> dataDir(p, "--data-dir", "-dd", "Directory to use for the data");
argparser::arg<std::string> cacheDir(p, "--cache-dir", "-dc", "Directory to use for cache");
argparser::arg<std::string> importFilePath(p, "--import-file-path", "-ifp", "File to import to the game");
argparser::arg<std::string> sendUri(p, "--uri", "-u", "URI to send to the game");
argparser::arg<std::string> v8Flags(p, "--v8-flags", "-v8f", "Flags to pass to the v8 engine of the game",
#if defined(__APPLE__) && defined(__aarch64__)
// Due to problems with running JIT compiled code without using apple specfic workarounds, we just run javascript via jitless
Expand All @@ -114,6 +119,7 @@ int main(int argc, char* argv[]) {
return 0;
}
options.importFilePath = importFilePath;
options.sendUri = sendUri;
options.windowWidth = windowWidth;
options.windowHeight = windowHeight;
options.graphicsApi = forceEgl.get() ? GraphicsApi::OPENGL_ES2 : GraphicsApi::OPENGL;
Expand Down
1 change: 1 addition & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ struct LauncherOptions {
bool fullscreen;
GraphicsApi graphicsApi;
std::string importFilePath;
std::string sendUri;
};
extern LauncherOptions options;

0 comments on commit 92eda37

Please sign in to comment.