Skip to content

Commit

Permalink
netplay: Add password feature to Android
Browse files Browse the repository at this point in the history
  • Loading branch information
kleidis committed Oct 29, 2024
1 parent fc0466f commit 989c9ea
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class NetPlayDialog(context: Context) : BaseSheetDialog(context) {
val ipAddress = binding.ipAddress.text.toString()
val username = binding.username.text.toString()
val portStr = binding.ipPort.text.toString()
val password = binding.password.text.toString()
val port = try {
portStr.toInt()
} catch (e: Exception) {
Expand All @@ -212,13 +213,13 @@ class NetPlayDialog(context: Context) : BaseSheetDialog(context) {
binding.btnConfirm.text = activity.getString(R.string.original_button_text)
} else {
Handler(Looper.getMainLooper()).post {
val operation: (String, Int, String) -> Int = if (isCreateRoom) {
{ ip, port, username -> NetPlayManager.netPlayCreateRoom(ip, port, username) }
val operation: (String, Int, String, String) -> Int = if (isCreateRoom) {
{ ip, port, username, pass -> NetPlayManager.netPlayCreateRoom(ip, port, username, pass) }
} else {
{ ip, port, username -> NetPlayManager.netPlayJoinRoom(ip, port, username) }
{ ip, port, username, pass -> NetPlayManager.netPlayJoinRoom(ip, port, username, pass) }
}

val result = operation(ipAddress, port, username)
val result = operation(ipAddress, port, username, password)
if (result == 0) {
if (isCreateRoom) {
NetPlayManager.setUsername(activity, username)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import io.github.mandarine3ds.mandarine.databinding.DialogMultiplayerRoomBinding
import io.github.mandarine3ds.mandarine.dialogs.NetPlayDialog

object NetPlayManager {
external fun netPlayCreateRoom(ipAddress: String, port: Int, username: String): Int
external fun netPlayJoinRoom(ipAddress: String, port: Int, username: String): Int
external fun netPlayCreateRoom(ipAddress: String, port: Int, username: String, password: String): Int
external fun netPlayJoinRoom(ipAddress: String, port: Int, username: String, password: String): Int
external fun netPlayRoomInfo(): Array<String>
external fun netPlayIsJoined(): Boolean
external fun netPlayIsHostedRoom(): Boolean
Expand Down
33 changes: 21 additions & 12 deletions src/android/app/src/main/jni/multiplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include "core/hle/service/cfg/cfg.h"
#include "network/network.h"

#include <thread>
#include <chrono>

void AddNetPlayMessage(jint type, jstring msg) {
IDCache::GetEnvForThread()->CallStaticVoidMethod(IDCache::GetNativeLibraryClass(),
IDCache::GetAddNetPlayMessage(), type, msg);
Expand Down Expand Up @@ -137,7 +140,7 @@ bool NetworkInit() {
}

NetPlayStatus NetPlayCreateRoom(const std::string& ipaddress, int port,
const std::string& username) {
const std::string& username, const std::string& password) {
auto member = Network::GetRoomMember().lock();
if (!member) {
return NetPlayStatus::NETWORK_ERROR;
Expand All @@ -152,19 +155,20 @@ NetPlayStatus NetPlayCreateRoom(const std::string& ipaddress, int port,
return NetPlayStatus::NETWORK_ERROR;
}

if (!room->Create(ipaddress, "", "", port, "", 31, username)) {
if (!room->Create(ipaddress, "", "", port, password, 31, username)) {
return NetPlayStatus::CREATE_ROOM_ERROR;
}

std::string console = Service::CFG::GetConsoleIdHash(Core::System::GetInstance());
member->Join(username, console, "127.0.0.1", port);
member->Join(username, console, "127.0.0.1", port, 0, Network::NoPreferredMac, password);
if (member->GetState() == Network::RoomMember::State::Joined) {
Network::SetInRoom(true);
}
return NetPlayStatus::NO_ERROR;
}

NetPlayStatus NetPlayJoinRoom(const std::string& ipaddress, int port, const std::string& username) {
NetPlayStatus NetPlayJoinRoom(const std::string& ipaddress, int port,
const std::string& username, const std::string& password) {
auto member = Network::GetRoomMember().lock();
if (!member) {
return NetPlayStatus::NETWORK_ERROR;
Expand All @@ -175,14 +179,22 @@ NetPlayStatus NetPlayJoinRoom(const std::string& ipaddress, int port, const std:
}

std::string console = Service::CFG::GetConsoleIdHash(Core::System::GetInstance());
member->Join(username, console, ipaddress.c_str(), port);
if (member->GetState() == Network::RoomMember::State::Joined) {
member->Join(username, console, ipaddress.c_str(), port, 0, Network::NoPreferredMac, password);

// Wait a bit for the connection and join process to complete
std::this_thread::sleep_for(std::chrono::milliseconds(500));

if (member->GetState() == Network::RoomMember::State::Joined ||
member->GetState() == Network::RoomMember::State::Moderator) {
Network::SetInRoom(true);
return NetPlayStatus::NO_ERROR;
}

if (!member->IsConnected()) {
return NetPlayStatus::COULD_NOT_CONNECT;
}
return NetPlayStatus::NO_ERROR;

return NetPlayStatus::WRONG_PASSWORD;
}

void NetPlaySendMessage(const std::string& msg) {
Expand Down Expand Up @@ -231,11 +243,8 @@ bool NetPlayIsJoined() {
return false;
}

if (member->GetState() == Network::RoomMember::State::Joining || member->IsConnected()) {
return true;
}

return false;
return (member->GetState() == Network::RoomMember::State::Joined ||
member->GetState() == Network::RoomMember::State::Moderator);
}

bool NetPlayIsHostedRoom() {
Expand Down
4 changes: 2 additions & 2 deletions src/android/app/src/main/jni/multiplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ enum class NetPlayStatus : s32 {

bool NetworkInit();
NetPlayStatus NetPlayCreateRoom(const std::string& ipaddress, int port,
const std::string& username);
NetPlayStatus NetPlayJoinRoom(const std::string& ipaddress, int port, const std::string& username);
const std::string& username, const std::string& password);
NetPlayStatus NetPlayJoinRoom(const std::string& ipaddress, int port, const std::string& username, const std::string& password);
std::vector<std::string> NetPlayRoomInfo();
bool NetPlayIsJoined();
bool NetPlayIsHostedRoom();
Expand Down
12 changes: 8 additions & 4 deletions src/android/app/src/main/jni/native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,19 @@ void Java_io_github_mandarine3ds_mandarine_NativeLibrary_removeAmiibo(
}

JNIEXPORT jint JNICALL Java_io_github_mandarine3ds_mandarine_utils_NetPlayManager_netPlayCreateRoom(
JNIEnv* env, [[maybe_unused]] jobject obj, jstring ipaddress, jint port, jstring username) {
JNIEnv* env, [[maybe_unused]] jobject obj, jstring ipaddress, jint port,
jstring username, jstring password) {
return static_cast<jint>(
NetPlayCreateRoom(GetJString(env, ipaddress), port, GetJString(env, username)));
NetPlayCreateRoom(GetJString(env, ipaddress), port,
GetJString(env, username), GetJString(env, password)));
}

JNIEXPORT jint JNICALL Java_io_github_mandarine3ds_mandarine_utils_NetPlayManager_netPlayJoinRoom(
JNIEnv* env, [[maybe_unused]] jobject obj, jstring ipaddress, jint port, jstring username) {
JNIEnv* env, [[maybe_unused]] jobject obj, jstring ipaddress, jint port,
jstring username, jstring password) {
return static_cast<jint>(
NetPlayJoinRoom(GetJString(env, ipaddress), port, GetJString(env, username)));
NetPlayJoinRoom(GetJString(env, ipaddress), port,
GetJString(env, username), GetJString(env, password)));
}

JNIEXPORT jobjectArray JNICALL
Expand Down
13 changes: 13 additions & 0 deletions src/android/app/src/main/res/layout/dialog_multiplayer_room.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/multiplayer_password"
android:padding="8dp">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/btn_confirm"
android:layout_width="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions src/android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@
<string name="multiplayer_address_unbanned">address unbanned</string>
<string name="multiplayer_kick_member">Kick Out</string>
<string name="multiplayer_chat_input_hint">Send messages……</string>
<string name="multiplayer_password">Password</string>
<string name="original_button_text">Join</string>
<string name="disabled_button_text">Joining...</string>

Expand Down

0 comments on commit 989c9ea

Please sign in to comment.