mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feature: chat (#36)
* fix(config): load config on demand in get/set instead of asserting * feat(chat): implement in-game chat box with UTF-8 support - Add ChatMessage struct and ChatBox widget for displaying and sending messages - Introduce split_utf8() in text_tools for splitting UTF-8 strings by character count - Add TimeTools utility (get_time_ticks) for message timestamps - Update TextField with start_typing/stop_typing methods and set_typing() - Integrate text input handling in WorldScene and WorldUIManager for chat - Add utf8cpp library for robust UTF-8 validation and processing - Add ChatBox rendering and input logic (src/ui/chat_box.cpp) - Register ChatBox source in CMakeLists.txt - Fix Renderer::render_lable to skip rendering if VAO is null - Update credits to include utf8cpp library * fix(ui): correct chat box layout with dynamic height and bottom anchor * feat(chat): implement client-server chat messaging Add protobuf definition for ChatMsg, new packet ID CHAT_MSG (4001), and handle sending/receiving chat messages on both client and server side. Wire up UI components (ChatBox, TextField) to allow message input and display, including a message queue in ClientWorld for thread-safe processing. Also add default initialization for ChatMessage::time. * feat(chat): distinguish between send and cancel in chat input * feat(ui): add background to Label widget and refactor debug overlay * refactor(ui): decouple ChatBox from image dependencies, allow custom TextField backgrounds TextField's background is now a generic `Widget` pointer, enabling use of any widget (e.g., `Rect`, `Image`) as background. `ChatBox` no longer creates its own `TextField`; it accepts one via `set_text_field`. Removed `set_d_image` and `set_default_image` in favor of `set_background`. Added a shared default image constant in `default_image.hpp`. Updated `host_game_ui.cpp`, `join_game_ui.cpp`, and `world_ui_manager.cpp` to construct backgrounds accordingly. * refactor(ui): move width, height, and fill properties to base Widget Consolidate width, height, and fill (fill_parent, fill_width, fill_height) into the base Widget class. Replace per-widget set_fill(bool) with set_fill_parent(bool), set_fill_width(bool), and set_fill_height(bool). * refactor(ui): generalize Label background, add ChatBox width method * feat(chat): implement message expiration after 5 seconds * feat(ui): add word wrap for chat messages with font width measurement * feat(audio): implement voice chat using Opus codec Add voice chat support with Opus encoding/decoding, push-to-talk (V key), and new audio recording infrastructure. * feat(audio): implement voice chat with Opus codec and streaming audio source Add AudioStreamSource for continuous PCM playback. Refactor voice send/receive to encode with Opus and stream to a dedicated source instead of per-frame sources. Enable voice transmission over network. * feat(audio): add spatial voice source with underwater effects Update voice source to support spatial positioning, low-pass filter, and reverb effect for underwater immersion. Adjust source pool size and low-pass filter parameter. Correct receive_voice parameter type. * feat(settings): add player voice volume and voice input type options - Add player voice volume slider and voice input type combo in settings UI. - Add localization strings for player voice and voice input types. - Add config overloads for const char* default values. - Implement voice input types (PTT, always, off) in world scene. - Refactor WorldScene member order and add load_config method. - Update audio engine to use player voice volume. - Downgrade some logging to debug. * fix(event): delegate error UI events to each handler Move error UI event check from top of handle_event into each individual event handler (mouse_move, mouse_button, mouse_wheel, key, text_input). Also flatten window resize dispatch: all sub-handlers are called regardless of previous handler return values, and the method always returns false. * feat(chat): add colored system messages for player join/exit * chore(logging): reduce verbosity for audio loading and input events * fix(settings): fix typo in config key "vloume" * feat(ui): add microphone icon when recording voice * feat(voice): limit voice message range to 48 units * chore(logging): downgrade text input log level to debug * feat(credits): add license identifiers and reorganize license files * feat: add sensitive word filter for chat messages Implement AC automaton-based filtering using a sensitive lexicon loaded from a JSON asset file. The filter is applied to both player names and chat messages before broadcasting, and can be toggled via the `sensitive_filter` config option. * feat(server): add config option to disable voice chat * fix: correct utf8 validation, add missing include, remove debug log
This commit is contained in:
@@ -653,7 +653,6 @@ void ClientPlayer::init(std::string_view name) {
|
||||
glm::ivec3 block = glm::floor(m_player_pos);
|
||||
block.y -= 1;
|
||||
BlockType id = m_world.get_block_tpye(block);
|
||||
Logger::info("player Block {} Walk Sound", id);
|
||||
if (id == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -661,7 +660,7 @@ void ClientPlayer::init(std::string_view name) {
|
||||
std::string sound = "block/" + name + "/walk.ogg";
|
||||
auto& audio = m_world.get_audio();
|
||||
audio.play_3d(sound, m_player_pos);
|
||||
Logger::info("Player block {} walk sound", name);
|
||||
Logger::debug("Player block {} walk sound", name);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "Cubed/gameplay/packet.hpp"
|
||||
#include "Cubed/scene/world_scene.hpp"
|
||||
#include "Cubed/tools/math_tools.hpp"
|
||||
#include "Cubed/tools/time_tools.hpp"
|
||||
|
||||
#include <absl/container/inlined_vector.h>
|
||||
#include <numbers>
|
||||
@@ -474,6 +475,11 @@ void ClientWorld::init(std::string_view player_name,
|
||||
m_audio.play_bgm();
|
||||
}
|
||||
|
||||
void ClientWorld::receive_login_rsp(LoginRsp& rsp) {
|
||||
m_voice_chat = rsp.voice_chat();
|
||||
start_client_thread(rsp.uuid());
|
||||
}
|
||||
|
||||
void ClientWorld::start_client_thread(std::string_view uuid) {
|
||||
if (m_game_running) {
|
||||
Logger::error("Game Already Running");
|
||||
@@ -481,6 +487,7 @@ void ClientWorld::start_client_thread(std::string_view uuid) {
|
||||
}
|
||||
// response
|
||||
m_player.set_uuid(uuid);
|
||||
|
||||
m_client_thread = std::jthread([this](std::stop_token token) {
|
||||
m_game_running = true;
|
||||
client_run(token);
|
||||
@@ -725,6 +732,7 @@ AABB ClientWorld::get_block_aabb(const glm::ivec3& pos) {
|
||||
}
|
||||
|
||||
AudioEngine& ClientWorld::get_audio() { return m_audio; }
|
||||
const AudioEngine& ClientWorld::get_audio() const { return m_audio; }
|
||||
Config& ClientWorld::get_config() { return m_config; }
|
||||
WorldScene& ClientWorld::world_scene() { return m_world_scene; }
|
||||
void ClientWorld::set_direct_exit() { m_exit_direct = true; }
|
||||
@@ -750,6 +758,29 @@ void ClientWorld::request_exit() {
|
||||
}
|
||||
}
|
||||
|
||||
void ClientWorld::receive_chat_message(ChatMsg& msg) {
|
||||
Color color = color_from_int(msg.color());
|
||||
|
||||
m_message_queue.emplace(msg.name(), msg.msg(), color, msg.system_msg(),
|
||||
Tools::get_time_ticks());
|
||||
}
|
||||
|
||||
void ClientWorld::receive_voice_message(VoiceMsg& msg) {
|
||||
if (!m_voice_chat) {
|
||||
return;
|
||||
}
|
||||
glm::vec3 pos{msg.pos().x(), msg.pos().y(), msg.pos().z()};
|
||||
m_voice_queue.emplace(msg.opus_data(), pos);
|
||||
}
|
||||
bool ClientWorld::enable_voice_chat() const { return m_voice_chat.load(); }
|
||||
void ClientWorld::send_chat_message(ChatMessage& message) {
|
||||
Arena arena;
|
||||
auto msg = Arena::Create<ChatMsg>(&arena);
|
||||
msg->set_name(message.player);
|
||||
msg->set_msg(message.text);
|
||||
m_client->send(make_packet(*msg));
|
||||
}
|
||||
|
||||
void ClientWorld::update(float delta_time) {
|
||||
|
||||
m_player.update(delta_time);
|
||||
@@ -916,6 +947,15 @@ void ClientWorld::update(float delta_time) {
|
||||
for (auto& [pos, timer] : m_timers) {
|
||||
timer.update(delta_time);
|
||||
}
|
||||
ChatMessage message;
|
||||
while (m_message_queue.try_pop(message)) {
|
||||
m_world_scene.handle_chat_message(message);
|
||||
}
|
||||
|
||||
VoiceMessage vm;
|
||||
while (m_voice_queue.try_pop(vm)) {
|
||||
m_audio.receive_voice(vm.data, vm.pos);
|
||||
}
|
||||
}
|
||||
|
||||
bool ClientWorld::handle_event(const Event& e) {
|
||||
|
||||
@@ -83,11 +83,7 @@ asio::awaitable<void> NetworkClient::read_loop() {
|
||||
auto* rsp = Arena::Create<LoginRsp>(&arena);
|
||||
Logger::info("Client: Receive Login rsp");
|
||||
if (decode_packet(*rsp, body_data, header)) {
|
||||
if (rsp->success()) {
|
||||
m_world.start_client_thread(rsp->uuid());
|
||||
} else {
|
||||
Logger::error("Connected Server Fail");
|
||||
}
|
||||
m_world.receive_login_rsp(*rsp);
|
||||
}
|
||||
} break;
|
||||
case std::to_underlying(PacketEnum::CHUNK_DATA_RSP): {
|
||||
@@ -135,6 +131,18 @@ asio::awaitable<void> NetworkClient::read_loop() {
|
||||
m_world.receive_player_water_sound(*rsp);
|
||||
}
|
||||
} break;
|
||||
case std::to_underlying(PacketEnum::CHAT_MSG): {
|
||||
auto* msg = Arena::Create<ChatMsg>(&arena);
|
||||
if (decode_packet(*msg, body_data, header)) {
|
||||
m_world.receive_chat_message(*msg);
|
||||
}
|
||||
} break;
|
||||
case std::to_underlying(PacketEnum::VOICE_MSG): {
|
||||
auto* msg = Arena::Create<VoiceMsg>(&arena);
|
||||
if (decode_packet(*msg, body_data, header)) {
|
||||
m_world.receive_voice_message(*msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (const asio::system_error& e) {
|
||||
@@ -222,5 +230,5 @@ void NetworkClient::set_error(std::string_view error) {
|
||||
m_error_string = error;
|
||||
m_connect_error = true;
|
||||
}
|
||||
|
||||
ClientWorld& NetworkClient::world() { return m_world; }
|
||||
} // namespace Cubed
|
||||
@@ -4,14 +4,17 @@
|
||||
#include "Cubed/gameplay/session.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
#include "Cubed/tools/math_tools.hpp"
|
||||
#include "Cubed/tools/uuid.hpp"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <ranges>
|
||||
#include <utility>
|
||||
using namespace std::chrono;
|
||||
using namespace std::chrono_literals;
|
||||
using namespace google::protobuf;
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
using nlohmann::json;
|
||||
namespace Cubed {
|
||||
ServerWorld::ServerWorld(Config& config) : m_config(config) {}
|
||||
|
||||
@@ -197,6 +200,21 @@ void ServerWorld::init_world() {
|
||||
|
||||
m_cave_carcer.init(ChunkGenerator::seed());
|
||||
m_river_worm.init(ChunkGenerator::seed());
|
||||
|
||||
m_enable_filter = m_config.get("sensitive_filter", false);
|
||||
Logger::info("sensitive filter {}", m_enable_filter.load());
|
||||
m_voice_chat = m_config.get("voice_chat", true);
|
||||
Logger::info("voice chat {}", m_voice_chat.load());
|
||||
try {
|
||||
fs::path path = std::format("{}SensitiveLexicon.json", ASSETS_PATH);
|
||||
std::ifstream s{path};
|
||||
json j = json::parse(s);
|
||||
m_filter.load(j);
|
||||
} catch (const std::exception& e) {
|
||||
Logger::error("Load SensitiveLexicon.json Fail");
|
||||
m_enable_filter = false;
|
||||
}
|
||||
|
||||
// m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE * 4);
|
||||
start_thread_pool();
|
||||
|
||||
@@ -680,20 +698,27 @@ void ServerWorld::handle_player_login(const std::string& name,
|
||||
auto* rsp = Arena::Create<LoginRsp>(&arena);
|
||||
rsp->set_success(true);
|
||||
rsp->set_uuid(uuid);
|
||||
rsp->set_voice_chat(m_voice_chat);
|
||||
session->send(make_packet(*rsp), 0);
|
||||
|
||||
boardcast_message("Server", std::format("Player {} Join Game", name),
|
||||
Color::YELLOW, true);
|
||||
}
|
||||
|
||||
void ServerWorld::handle_player_exit(const std::string& uuid) {
|
||||
std::shared_ptr<Session> exit_session;
|
||||
ChunkPosSet old_set;
|
||||
std::string name;
|
||||
{
|
||||
std::lock_guard lock(m_player_mutex);
|
||||
auto it = m_players.find(uuid);
|
||||
if (it != m_players.end()) {
|
||||
Logger::info("Player {} Exit the Server", it->second.get_name());
|
||||
name = it->second.get_name();
|
||||
Logger::info("Player {} Exit the Server", name);
|
||||
exit_session = it->second.get_session();
|
||||
old_set = std::move(it->second.get_chunk_pos_set());
|
||||
m_players.erase(it);
|
||||
|
||||
} else {
|
||||
Logger::error("Player {} isn't in Server", uuid);
|
||||
return;
|
||||
@@ -723,6 +748,9 @@ void ServerWorld::handle_player_exit(const std::string& uuid) {
|
||||
s->send(make_packet(*rsp), 0);
|
||||
}
|
||||
}
|
||||
|
||||
boardcast_message("Server", std::format("Player {} Exit Game", name),
|
||||
Color::YELLOW, true);
|
||||
}
|
||||
|
||||
glm::vec3 ServerWorld::get_player_pos(const std::string& uuid) const {
|
||||
@@ -753,6 +781,58 @@ void ServerWorld::handle_chunk_req(int task_id, const std::string& uuid,
|
||||
[task_id, uuid, pos, this]() { send_chunk(task_id, uuid, pos); });
|
||||
}
|
||||
|
||||
void ServerWorld::handle_chat_message(ChatMsg& msg) {
|
||||
|
||||
std::string name = msg.name();
|
||||
std::string message = msg.msg();
|
||||
auto pool = m_net_thread_pool.load();
|
||||
pool->enqueue([this, player = std::move(name), m = std::move(message)]() {
|
||||
boardcast_message(player, m);
|
||||
});
|
||||
}
|
||||
|
||||
void ServerWorld::handle_voice_message(VoiceMsg& msg) {
|
||||
if (!m_voice_chat) {
|
||||
return;
|
||||
}
|
||||
auto pool = m_net_thread_pool.load();
|
||||
std::string uuid = msg.uuid();
|
||||
std::string data = msg.opus_data();
|
||||
auto pos = msg.pos();
|
||||
glm::vec3 p{pos.x(), pos.y(), pos.z()};
|
||||
pool->enqueue([this, uuid = std::move(uuid), data = std::move(data), p]() {
|
||||
std::vector<std::shared_ptr<Session>> session;
|
||||
|
||||
{
|
||||
std::shared_lock lock(m_player_mutex);
|
||||
for (auto& [key, player] : m_players) {
|
||||
if (key == uuid) {
|
||||
continue;
|
||||
}
|
||||
if (Math::distance2(p, player.get_pos()) > 48.0f * 48.0f) {
|
||||
continue;
|
||||
}
|
||||
session.emplace_back(player.get_session());
|
||||
}
|
||||
}
|
||||
|
||||
Arena arena;
|
||||
auto msg = Arena::Create<VoiceMsg>(&arena);
|
||||
msg->set_uuid(uuid);
|
||||
|
||||
msg->set_opus_data(data);
|
||||
|
||||
auto pos = msg->mutable_pos();
|
||||
pos->set_x(p.x);
|
||||
pos->set_y(p.y);
|
||||
pos->set_z(p.z);
|
||||
|
||||
for (auto& s : session) {
|
||||
s->send(make_packet(*msg), 5);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ServerWorld::handle_block_change(const BlockChangeReq& req) {
|
||||
float x = std::floor(req.pos().x());
|
||||
float y = std::floor(req.pos().y());
|
||||
@@ -861,6 +941,36 @@ void ServerWorld::send_server_stop() {
|
||||
Logger::info("Send Server Mesaage Success");
|
||||
}
|
||||
|
||||
void ServerWorld::boardcast_message(const std::string& name,
|
||||
const std::string& message, Color color,
|
||||
bool system_msg) {
|
||||
|
||||
std::vector<std::shared_ptr<Session>> m_session;
|
||||
{
|
||||
std::shared_lock lock(m_player_mutex);
|
||||
for (auto& [_, p] : m_players) {
|
||||
m_session.emplace_back(p.get_session());
|
||||
}
|
||||
}
|
||||
|
||||
Arena arena;
|
||||
auto msg = Arena::Create<ChatMsg>(&arena);
|
||||
if (m_enable_filter) {
|
||||
msg->set_msg(m_filter.filter(message));
|
||||
msg->set_name(m_filter.filter(name));
|
||||
} else {
|
||||
msg->set_msg(message);
|
||||
msg->set_name(name);
|
||||
}
|
||||
|
||||
msg->set_color(std::to_underlying(color));
|
||||
msg->set_system_msg(system_msg);
|
||||
|
||||
for (auto& s : m_session) {
|
||||
s->send(make_packet(*msg));
|
||||
}
|
||||
}
|
||||
|
||||
int ServerWorld::chunk_load_style() const {
|
||||
return std::to_underlying(m_chunk_load_style.load());
|
||||
}
|
||||
|
||||
@@ -99,6 +99,18 @@ asio::awaitable<void> Session::read_loop() {
|
||||
m_server_world.sync_player_water_sound(*req);
|
||||
}
|
||||
}
|
||||
if (cmd_id == std::to_underlying(PacketEnum::CHAT_MSG)) {
|
||||
auto* msg = Arena::Create<ChatMsg>(&arena);
|
||||
if (decode_packet(*msg, body_data, header)) {
|
||||
m_server_world.handle_chat_message(*msg);
|
||||
}
|
||||
}
|
||||
if (cmd_id == std::to_underlying(PacketEnum::VOICE_MSG)) {
|
||||
auto* msg = Arena::Create<VoiceMsg>(&arena);
|
||||
if (decode_packet(*msg, body_data, header)) {
|
||||
m_server_world.handle_voice_message(*msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (const asio::system_error& e) {
|
||||
auto ec = e.code();
|
||||
|
||||
Reference in New Issue
Block a user