yuzu: Add ui files for multiplayer rooms

This commit is contained in:
FearlessTobi 2021-12-25 20:27:52 +01:00
commit 60008b680e
67 changed files with 4524 additions and 49 deletions

View file

@ -41,6 +41,7 @@ add_custom_command(OUTPUT scm_rev.cpp
add_library(common STATIC
algorithm.h
alignment.h
announce_multiplayer_room.h
assert.cpp
assert.h
atomic_helpers.h

View file

@ -0,0 +1,138 @@
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <array>
#include <functional>
#include <string>
#include <vector>
#include "common/common_types.h"
#include "web_service/web_result.h"
namespace AnnounceMultiplayerRoom {
using MacAddress = std::array<u8, 6>;
struct Room {
struct Member {
std::string username;
std::string nickname;
std::string avatar_url;
MacAddress mac_address;
std::string game_name;
u64 game_id;
};
std::string id;
std::string verify_UID; ///< UID used for verification
std::string name;
std::string description;
std::string owner;
std::string ip;
u16 port;
u32 max_player;
u32 net_version;
bool has_password;
std::string preferred_game;
u64 preferred_game_id;
std::vector<Member> members;
};
using RoomList = std::vector<Room>;
/**
* A AnnounceMultiplayerRoom interface class. A backend to submit/get to/from a web service should
* implement this interface.
*/
class Backend {
public:
virtual ~Backend() = default;
/**
* Sets the Information that gets used for the announce
* @param uid The Id of the room
* @param name The name of the room
* @param description The room description
* @param port The port of the room
* @param net_version The version of the libNetwork that gets used
* @param has_password True if the room is passowrd protected
* @param preferred_game The preferred game of the room
* @param preferred_game_id The title id of the preferred game
*/
virtual void SetRoomInformation(const std::string& name, const std::string& description,
const u16 port, const u32 max_player, const u32 net_version,
const bool has_password, const std::string& preferred_game,
const u64 preferred_game_id) = 0;
/**
* Adds a player information to the data that gets announced
* @param nickname The nickname of the player
* @param mac_address The MAC Address of the player
* @param game_id The title id of the game the player plays
* @param game_name The name of the game the player plays
*/
virtual void AddPlayer(const std::string& username, const std::string& nickname,
const std::string& avatar_url, const MacAddress& mac_address,
const u64 game_id, const std::string& game_name) = 0;
/**
* Updates the data in the announce service. Re-register the room when required.
* @result The result of the update attempt
*/
virtual WebService::WebResult Update() = 0;
/**
* Registers the data in the announce service
* @result The result of the register attempt. When the result code is Success, A global Guid of
* the room which may be used for verification will be in the result's returned_data.
*/
virtual WebService::WebResult Register() = 0;
/**
* Empties the stored players
*/
virtual void ClearPlayers() = 0;
/**
* Get the room information from the announce service
* @result A list of all rooms the announce service has
*/
virtual RoomList GetRoomList() = 0;
/**
* Sends a delete message to the announce service
*/
virtual void Delete() = 0;
};
/**
* Empty implementation of AnnounceMultiplayerRoom interface that drops all data. Used when a
* functional backend implementation is not available.
*/
class NullBackend : public Backend {
public:
~NullBackend() = default;
void SetRoomInformation(const std::string& /*name*/, const std::string& /*description*/,
const u16 /*port*/, const u32 /*max_player*/, const u32 /*net_version*/,
const bool /*has_password*/, const std::string& /*preferred_game*/,
const u64 /*preferred_game_id*/) override {}
void AddPlayer(const std::string& /*username*/, const std::string& /*nickname*/,
const std::string& /*avatar_url*/, const MacAddress& /*mac_address*/,
const u64 /*game_id*/, const std::string& /*game_name*/) override {}
WebService::WebResult Update() override {
return WebService::WebResult{WebService::WebResult::Code::NoWebservice,
"WebService is missing"};
}
WebService::WebResult Register() override {
return WebService::WebResult{WebService::WebResult::Code::NoWebservice,
"WebService is missing"};
}
void ClearPlayers() override {}
RoomList GetRoomList() override {
return RoomList{};
}
void Delete() override {}
};
} // namespace AnnounceMultiplayerRoom

View file

@ -1,4 +1,6 @@
add_library(core STATIC
announce_multiplayer_session.cpp
announce_multiplayer_session.h
arm/arm_interface.h
arm/arm_interface.cpp
arm/cpu_interrupt_handler.cpp
@ -741,11 +743,11 @@ add_library(core STATIC
memory/dmnt_cheat_vm.h
memory.cpp
memory.h
network/network.cpp
network/network.h
network/network_interface.cpp
network/network_interface.h
network/sockets.h
internal_network/network.cpp
internal_network/network.h
internal_network/network_interface.cpp
internal_network/network_interface.h
internal_network/sockets.h
perf_stats.cpp
perf_stats.h
reporter.cpp
@ -780,7 +782,7 @@ endif()
create_target_directory_groups(core)
target_link_libraries(core PUBLIC common PRIVATE audio_core video_core)
target_link_libraries(core PUBLIC common PRIVATE audio_core network video_core)
target_link_libraries(core PUBLIC Boost::boost PRIVATE fmt::fmt nlohmann_json::nlohmann_json mbedtls Opus::Opus)
if (MINGW)
target_link_libraries(core PRIVATE ${MSWSOCK_LIBRARY})

View file

@ -0,0 +1,165 @@
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <chrono>
#include <future>
#include <vector>
#include "announce_multiplayer_session.h"
#include "common/announce_multiplayer_room.h"
#include "common/assert.h"
#include "common/settings.h"
#include "network/network.h"
#ifdef ENABLE_WEB_SERVICE
#include "web_service/announce_room_json.h"
#endif
namespace Core {
// Time between room is announced to web_service
static constexpr std::chrono::seconds announce_time_interval(15);
AnnounceMultiplayerSession::AnnounceMultiplayerSession() {
#ifdef ENABLE_WEB_SERVICE
backend = std::make_unique<WebService::RoomJson>(Settings::values.web_api_url.GetValue(),
Settings::values.yuzu_username.GetValue(),
Settings::values.yuzu_token.GetValue());
#else
backend = std::make_unique<AnnounceMultiplayerRoom::NullBackend>();
#endif
}
WebService::WebResult AnnounceMultiplayerSession::Register() {
std::shared_ptr<Network::Room> room = Network::GetRoom().lock();
if (!room) {
return WebService::WebResult{WebService::WebResult::Code::LibError,
"Network is not initialized"};
}
if (room->GetState() != Network::Room::State::Open) {
return WebService::WebResult{WebService::WebResult::Code::LibError, "Room is not open"};
}
UpdateBackendData(room);
WebService::WebResult result = backend->Register();
if (result.result_code != WebService::WebResult::Code::Success) {
return result;
}
LOG_INFO(WebService, "Room has been registered");
room->SetVerifyUID(result.returned_data);
registered = true;
return WebService::WebResult{WebService::WebResult::Code::Success};
}
void AnnounceMultiplayerSession::Start() {
if (announce_multiplayer_thread) {
Stop();
}
shutdown_event.Reset();
announce_multiplayer_thread =
std::make_unique<std::thread>(&AnnounceMultiplayerSession::AnnounceMultiplayerLoop, this);
}
void AnnounceMultiplayerSession::Stop() {
if (announce_multiplayer_thread) {
shutdown_event.Set();
announce_multiplayer_thread->join();
announce_multiplayer_thread.reset();
backend->Delete();
registered = false;
}
}
AnnounceMultiplayerSession::CallbackHandle AnnounceMultiplayerSession::BindErrorCallback(
std::function<void(const WebService::WebResult&)> function) {
std::lock_guard lock(callback_mutex);
auto handle = std::make_shared<std::function<void(const WebService::WebResult&)>>(function);
error_callbacks.insert(handle);
return handle;
}
void AnnounceMultiplayerSession::UnbindErrorCallback(CallbackHandle handle) {
std::lock_guard lock(callback_mutex);
error_callbacks.erase(handle);
}
AnnounceMultiplayerSession::~AnnounceMultiplayerSession() {
Stop();
}
void AnnounceMultiplayerSession::UpdateBackendData(std::shared_ptr<Network::Room> room) {
Network::RoomInformation room_information = room->GetRoomInformation();
std::vector<Network::Room::Member> memberlist = room->GetRoomMemberList();
backend->SetRoomInformation(
room_information.name, room_information.description, room_information.port,
room_information.member_slots, Network::network_version, room->HasPassword(),
room_information.preferred_game, room_information.preferred_game_id);
backend->ClearPlayers();
for (const auto& member : memberlist) {
backend->AddPlayer(member.username, member.nickname, member.avatar_url, member.mac_address,
member.game_info.id, member.game_info.name);
}
}
void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
// Invokes all current bound error callbacks.
const auto ErrorCallback = [this](WebService::WebResult result) {
std::lock_guard<std::mutex> lock(callback_mutex);
for (auto callback : error_callbacks) {
(*callback)(result);
}
};
if (!registered) {
WebService::WebResult result = Register();
if (result.result_code != WebService::WebResult::Code::Success) {
ErrorCallback(result);
return;
}
}
auto update_time = std::chrono::steady_clock::now();
std::future<WebService::WebResult> future;
while (!shutdown_event.WaitUntil(update_time)) {
update_time += announce_time_interval;
std::shared_ptr<Network::Room> room = Network::GetRoom().lock();
if (!room) {
break;
}
if (room->GetState() != Network::Room::State::Open) {
break;
}
UpdateBackendData(room);
WebService::WebResult result = backend->Update();
if (result.result_code != WebService::WebResult::Code::Success) {
ErrorCallback(result);
}
if (result.result_string == "404") {
registered = false;
// Needs to register the room again
WebService::WebResult register_result = Register();
if (register_result.result_code != WebService::WebResult::Code::Success) {
ErrorCallback(register_result);
}
}
}
}
AnnounceMultiplayerRoom::RoomList AnnounceMultiplayerSession::GetRoomList() {
return backend->GetRoomList();
}
bool AnnounceMultiplayerSession::IsRunning() const {
return announce_multiplayer_thread != nullptr;
}
void AnnounceMultiplayerSession::UpdateCredentials() {
ASSERT_MSG(!IsRunning(), "Credentials can only be updated when session is not running");
#ifdef ENABLE_WEB_SERVICE
backend = std::make_unique<WebService::RoomJson>(Settings::values.web_api_url.GetValue(),
Settings::values.yuzu_username.GetValue(),
Settings::values.yuzu_token.GetValue());
#endif
}
} // namespace Core

View file

@ -0,0 +1,96 @@
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <atomic>
#include <functional>
#include <memory>
#include <mutex>
#include <set>
#include <thread>
#include "common/announce_multiplayer_room.h"
#include "common/common_types.h"
#include "common/thread.h"
namespace Network {
class Room;
}
namespace Core {
/**
* Instruments AnnounceMultiplayerRoom::Backend.
* Creates a thread that regularly updates the room information and submits them
* An async get of room information is also possible
*/
class AnnounceMultiplayerSession {
public:
using CallbackHandle = std::shared_ptr<std::function<void(const WebService::WebResult&)>>;
AnnounceMultiplayerSession();
~AnnounceMultiplayerSession();
/**
* Allows to bind a function that will get called if the announce encounters an error
* @param function The function that gets called
* @return A handle that can be used the unbind the function
*/
CallbackHandle BindErrorCallback(std::function<void(const WebService::WebResult&)> function);
/**
* Unbind a function from the error callbacks
* @param handle The handle for the function that should get unbind
*/
void UnbindErrorCallback(CallbackHandle handle);
/**
* Registers a room to web services
* @return The result of the registration attempt.
*/
WebService::WebResult Register();
/**
* Starts the announce of a room to web services
*/
void Start();
/**
* Stops the announce to web services
*/
void Stop();
/**
* Returns a list of all room information the backend got
* @param func A function that gets executed when the async get finished, e.g. a signal
* @return a list of rooms received from the web service
*/
AnnounceMultiplayerRoom::RoomList GetRoomList();
/**
* Whether the announce session is still running
*/
bool IsRunning() const;
/**
* Recreates the backend, updating the credentials.
* This can only be used when the announce session is not running.
*/
void UpdateCredentials();
private:
Common::Event shutdown_event;
std::mutex callback_mutex;
std::set<CallbackHandle> error_callbacks;
std::unique_ptr<std::thread> announce_multiplayer_thread;
/// Backend interface that logs fields
std::unique_ptr<AnnounceMultiplayerRoom::Backend> backend;
std::atomic_bool registered = false; ///< Whether the room has been registered
void UpdateBackendData(std::shared_ptr<Network::Room> room);
void AnnounceMultiplayerLoop();
};
} // namespace Core

View file

@ -43,14 +43,15 @@
#include "core/hle/service/service.h"
#include "core/hle/service/sm/sm.h"
#include "core/hle/service/time/time_manager.h"
#include "core/internal_network/network.h"
#include "core/loader/loader.h"
#include "core/memory.h"
#include "core/memory/cheat_engine.h"
#include "core/network/network.h"
#include "core/perf_stats.h"
#include "core/reporter.h"
#include "core/telemetry_session.h"
#include "core/tools/freezer.h"
#include "network/network.h"
#include "video_core/renderer_base.h"
#include "video_core/video_core.h"
@ -315,6 +316,15 @@ struct System::Impl {
GetAndResetPerfStats();
perf_stats->BeginSystemFrame();
std::string name = "Unknown Game";
const Loader::ResultStatus res{app_loader->ReadTitle(name)};
if (auto room_member = Network::GetRoomMember().lock()) {
Network::GameInfo game_info;
game_info.name = name;
game_info.id = program_id;
room_member->SendGameInfo(game_info);
}
status = SystemResultStatus::Success;
return status;
}
@ -362,6 +372,11 @@ struct System::Impl {
memory.Reset();
applet_manager.ClearAll();
if (auto room_member = Network::GetRoomMember().lock()) {
Network::GameInfo game_info{};
room_member->SendGameInfo(game_info);
}
LOG_DEBUG(Core, "Shutdown OK");
}

View file

@ -18,8 +18,8 @@ namespace {
} // Anonymous namespace
#include "core/network/network.h"
#include "core/network/network_interface.h"
#include "core/internal_network/network.h"
#include "core/internal_network/network_interface.h"
namespace Service::NIFM {

View file

@ -13,8 +13,8 @@
#include "core/hle/kernel/k_thread.h"
#include "core/hle/service/sockets/bsd.h"
#include "core/hle/service/sockets/sockets_translate.h"
#include "core/network/network.h"
#include "core/network/sockets.h"
#include "core/internal_network/network.h"
#include "core/internal_network/sockets.h"
namespace Service::Sockets {

View file

@ -16,7 +16,7 @@ class System;
namespace Network {
class Socket;
}
} // namespace Network
namespace Service::Sockets {

View file

@ -7,7 +7,7 @@
#include "common/common_types.h"
#include "core/hle/service/sockets/sockets.h"
#include "core/hle/service/sockets/sockets_translate.h"
#include "core/network/network.h"
#include "core/internal_network/network.h"
namespace Service::Sockets {

View file

@ -7,7 +7,7 @@
#include "common/common_types.h"
#include "core/hle/service/sockets/sockets.h"
#include "core/network/network.h"
#include "core/internal_network/network.h"
namespace Service::Sockets {

View file

@ -29,9 +29,9 @@
#include "common/common_types.h"
#include "common/logging/log.h"
#include "common/settings.h"
#include "core/network/network.h"
#include "core/network/network_interface.h"
#include "core/network/sockets.h"
#include "core/internal_network/network.h"
#include "core/internal_network/network_interface.h"
#include "core/internal_network/sockets.h"
namespace Network {

View file

@ -11,7 +11,7 @@
#include "common/logging/log.h"
#include "common/settings.h"
#include "common/string_util.h"
#include "core/network/network_interface.h"
#include "core/internal_network/network_interface.h"
#ifdef _WIN32
#include <iphlpapi.h>

View file

@ -3,6 +3,7 @@
#pragma once
#include <map>
#include <memory>
#include <utility>
@ -12,7 +13,7 @@
#endif
#include "common/common_types.h"
#include "core/network/network.h"
#include "core/internal_network/network.h"
// TODO: C++20 Replace std::vector usages with std::span

View file

@ -251,7 +251,7 @@ public:
void Room::RoomImpl::ServerLoop() {
while (state != State::Closed) {
ENetEvent event;
if (enet_host_service(server, &event, 50) > 0) {
if (enet_host_service(server, &event, 16) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE:
switch (event.packet->data[0]) {
@ -599,7 +599,7 @@ bool Room::RoomImpl::HasModPermission(const ENetPeer* client) const {
if (sending_member == members.end()) {
return false;
}
if (room_information.enable_citra_mods &&
if (room_information.enable_yuzu_mods &&
sending_member->user_data.moderator) { // Community moderator
return true;
@ -1014,7 +1014,7 @@ bool Room::Create(const std::string& name, const std::string& description,
const u32 max_connections, const std::string& host_username,
const std::string& preferred_game, u64 preferred_game_id,
std::unique_ptr<VerifyUser::Backend> verify_backend,
const Room::BanList& ban_list, bool enable_citra_mods) {
const Room::BanList& ban_list, bool enable_yuzu_mods) {
ENetAddress address;
address.host = ENET_HOST_ANY;
if (!server_address.empty()) {
@ -1037,7 +1037,7 @@ bool Room::Create(const std::string& name, const std::string& description,
room_impl->room_information.preferred_game = preferred_game;
room_impl->room_information.preferred_game_id = preferred_game_id;
room_impl->room_information.host_username = host_username;
room_impl->room_information.enable_citra_mods = enable_citra_mods;
room_impl->room_information.enable_yuzu_mods = enable_yuzu_mods;
room_impl->password = password;
room_impl->verify_backend = std::move(verify_backend);
room_impl->username_ban_list = ban_list.first;

View file

@ -32,7 +32,7 @@ struct RoomInformation {
std::string preferred_game; ///< Game to advertise that you want to play
u64 preferred_game_id; ///< Title ID for the advertised game
std::string host_username; ///< Forum username of the host
bool enable_citra_mods; ///< Allow Citra Moderators to moderate on this room
bool enable_yuzu_mods; ///< Allow yuzu Moderators to moderate on this room
};
struct GameInfo {
@ -148,7 +148,7 @@ public:
const std::string& host_username = "", const std::string& preferred_game = "",
u64 preferred_game_id = 0,
std::unique_ptr<VerifyUser::Backend> verify_backend = nullptr,
const BanList& ban_list = {}, bool enable_citra_mods = false);
const BanList& ban_list = {}, bool enable_yuzu_mods = false);
/**
* Sets the verification GUID of the room.

View file

@ -86,7 +86,7 @@ public:
* @params password The password for the room
* the server to assign one for us.
*/
void SendJoinRequest(const std::string& nickname, const std::string& console_id_hash,
void SendJoinRequest(const std::string& nickname_, const std::string& console_id_hash,
const MacAddress& preferred_mac = NoPreferredMac,
const std::string& password = "", const std::string& token = "");
@ -159,7 +159,7 @@ void RoomMember::RoomMemberImpl::MemberLoop() {
while (IsConnected()) {
std::lock_guard lock(network_mutex);
ENetEvent event;
if (enet_host_service(client, &event, 100) > 0) {
if (enet_host_service(client, &event, 16) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE:
switch (event.packet->data[0]) {
@ -251,16 +251,17 @@ void RoomMember::RoomMemberImpl::MemberLoop() {
break;
}
}
std::list<Packet> packets;
{
std::lock_guard lock(send_list_mutex);
for (const auto& packet : send_list) {
ENetPacket* enetPacket = enet_packet_create(packet.GetData(), packet.GetDataSize(),
ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(server, 0, enetPacket);
}
enet_host_flush(client);
send_list.clear();
std::lock_guard send_lock(send_list_mutex);
packets.swap(send_list);
}
for (const auto& packet : packets) {
ENetPacket* enetPacket = enet_packet_create(packet.GetData(), packet.GetDataSize(),
ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(server, 0, enetPacket);
}
enet_host_flush(client);
}
Disconnect();
};
@ -274,14 +275,14 @@ void RoomMember::RoomMemberImpl::Send(Packet&& packet) {
send_list.push_back(std::move(packet));
}
void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname,
void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname_,
const std::string& console_id_hash,
const MacAddress& preferred_mac,
const std::string& password,
const std::string& token) {
Packet packet;
packet << static_cast<u8>(IdJoinRequest);
packet << nickname;
packet << nickname_;
packet << console_id_hash;
packet << preferred_mac;
packet << network_version;

View file

@ -13,7 +13,7 @@ struct UserData {
std::string username;
std::string display_name;
std::string avatar_url;
bool moderator = false; ///< Whether the user is a Citra Moderator.
bool moderator = false; ///< Whether the user is a yuzu Moderator.
};
/**

Some files were not shown because too many files have changed in this diff Show more