mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
Compare commits
2 Commits
f8aac019bd
...
9a510d2319
| Author | SHA1 | Date | |
|---|---|---|---|
| 9a510d2319 | |||
| 176df4ee04 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -41,6 +41,7 @@ CMakeError.log
|
||||
*~
|
||||
.DS_Store
|
||||
assets/config.toml
|
||||
assets/server-config.toml
|
||||
.venv/
|
||||
pyout/
|
||||
vcpkg_installed/
|
||||
@@ -55,7 +55,7 @@ public:
|
||||
AudioEngine& audio();
|
||||
|
||||
private:
|
||||
Config m_config;
|
||||
Config m_game_config;
|
||||
Camera m_camera;
|
||||
TextureManager m_texture_manager;
|
||||
NetworkServer m_server;
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Cubed {
|
||||
|
||||
class Config {
|
||||
public:
|
||||
Config();
|
||||
explicit Config(std::string_view path);
|
||||
Config(const Config&) = delete;
|
||||
Config(Config&&) = delete;
|
||||
Config& operator=(const Config&) = delete;
|
||||
@@ -54,8 +54,7 @@ public:
|
||||
|
||||
private:
|
||||
toml::table m_tbl;
|
||||
constexpr static inline std::string_view CONGIF_PATH =
|
||||
ASSETS_PATH "config.toml";
|
||||
const std::string CONGIF_PATH;
|
||||
const toml::node* find_node(const toml::table& root,
|
||||
std::string_view path) const;
|
||||
// Follow the path to find the last-level toml::table, creating it if it
|
||||
|
||||
@@ -105,6 +105,12 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
std::atomic<bool> m_is_pending_delete_queue_free{false};
|
||||
std::mutex m_delete_vbo_mutex;
|
||||
std::mutex m_delete_vao_mutex;
|
||||
std::vector<std::unique_ptr<VertexBuffer>> m_pending_delete_vbo;
|
||||
std::vector<std::unique_ptr<VertexArray>> m_pending_delete_vao;
|
||||
|
||||
enum class ChunkLoadStyle { RANDOM, CENTER };
|
||||
using ChunkHashMap =
|
||||
tbb::concurrent_hash_map<ChunkPos, std::shared_ptr<ClientChunk>,
|
||||
@@ -130,15 +136,11 @@ private:
|
||||
std::vector<glm::vec4> m_planes;
|
||||
std::jthread m_client_thread;
|
||||
|
||||
std::mutex m_delete_vbo_mutex;
|
||||
std::mutex m_delete_vao_mutex;
|
||||
mutable std::shared_mutex m_player_info_mutex;
|
||||
|
||||
tbb::concurrent_queue<std::unique_ptr<ClientChunk>> m_pending_upload_queue;
|
||||
tbb::concurrent_queue<ChunkPos> m_dirty_chunk_queue;
|
||||
tbb::concurrent_queue<PendingSound> m_pending_sound;
|
||||
std::vector<std::unique_ptr<VertexBuffer>> m_pending_delete_vbo;
|
||||
std::vector<std::unique_ptr<VertexArray>> m_pending_delete_vao;
|
||||
|
||||
std::deque<ChunkPos> m_dirty_queue;
|
||||
std::vector<const ChunkRenderSnapshot*> m_render_snapshots;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/gameplay/server_world.hpp"
|
||||
#include "Cubed/gameplay/session.hpp"
|
||||
|
||||
@@ -8,17 +9,19 @@ namespace Cubed {
|
||||
|
||||
class NetworkServer {
|
||||
public:
|
||||
NetworkServer(int port = 25530);
|
||||
explicit NetworkServer();
|
||||
~NetworkServer();
|
||||
void stop();
|
||||
|
||||
// Run in another thread after initialization is complete
|
||||
void start_server(int port = 25530);
|
||||
|
||||
void start_server(int port);
|
||||
void start_server();
|
||||
int port() const;
|
||||
ServerWorld& server_world();
|
||||
|
||||
private:
|
||||
Config m_config;
|
||||
|
||||
asio::io_context m_io;
|
||||
std::thread m_net_thread;
|
||||
int m_port = 25530;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/gameplay/cave_carver.hpp"
|
||||
#include "Cubed/gameplay/chunk_pos.hpp"
|
||||
#include "Cubed/gameplay/game_time.hpp"
|
||||
@@ -25,7 +26,7 @@ class Session;
|
||||
class ServerWorld {
|
||||
public:
|
||||
enum class ThreadPoolKind { NET, GEN };
|
||||
ServerWorld();
|
||||
ServerWorld(Config& config);
|
||||
~ServerWorld();
|
||||
void stop();
|
||||
void handle_player_exit(const std::string& uuid);
|
||||
@@ -118,6 +119,9 @@ private:
|
||||
|
||||
using uuid_acc = PlayerUUIDMap::accessor;
|
||||
using uuid_cacc = PlayerUUIDMap::const_accessor;
|
||||
|
||||
Config& m_config;
|
||||
|
||||
// key = uuid
|
||||
PlayerHashMap m_players;
|
||||
ChunkHashMap m_chunks;
|
||||
|
||||
14
src/app.cpp
14
src/app.cpp
@@ -13,11 +13,13 @@
|
||||
namespace Cubed {
|
||||
|
||||
App::App()
|
||||
: m_texture_manager(m_config), m_audio(m_config),
|
||||
m_client_world(m_audio, m_config), m_dev_panel(*this),
|
||||
|
||||
: m_game_config(ASSETS_PATH "config.toml"),
|
||||
m_texture_manager(m_game_config), m_audio(m_game_config),
|
||||
m_client_world(m_audio, m_game_config), m_dev_panel(*this),
|
||||
m_renderer(m_camera, m_client_world, m_texture_manager, m_dev_panel,
|
||||
m_config),
|
||||
m_window(m_renderer, m_config) {}
|
||||
m_game_config),
|
||||
m_window(m_renderer, m_game_config) {}
|
||||
|
||||
App::~App() {
|
||||
if (m_client) {
|
||||
@@ -362,7 +364,7 @@ void App::update() {
|
||||
const auto& player = m_client_world.get_player();
|
||||
if (player_gait != player.get_gait()) {
|
||||
player_gait = player.get_gait();
|
||||
float fov = m_config.get("player.fov", 70.0f);
|
||||
float fov = m_game_config.get("player.fov", 70.0f);
|
||||
if (player_gait == Gait::WALK) {
|
||||
m_renderer.update_fov(fov);
|
||||
}
|
||||
@@ -406,7 +408,7 @@ TextureManager& App::texture_manager() { return m_texture_manager; }
|
||||
Window& App::window() { return m_window; }
|
||||
ClientWorld& App::client_world() { return m_client_world; }
|
||||
ServerWorld& App::server_world() { return m_server.server_world(); }
|
||||
Config& App::config() { return m_config; }
|
||||
Config& App::config() { return m_game_config; }
|
||||
const App::Argument& App::argument() const { return m_argument; }
|
||||
AudioEngine& App::audio() { return m_audio; }
|
||||
} // namespace Cubed
|
||||
@@ -10,7 +10,7 @@ using namespace std::string_view_literals;
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
Config::Config() { load_config(); }
|
||||
Config::Config(std::string_view path) : CONGIF_PATH(path) { load_config(); }
|
||||
|
||||
Config::~Config() { save_to_file(); }
|
||||
|
||||
|
||||
@@ -25,11 +25,22 @@ ClientWorld::ClientWorld(AudioEngine& auido, Config& config)
|
||||
: m_player(*this), m_audio(auido), m_config(config) {}
|
||||
|
||||
ClientWorld::~ClientWorld() {
|
||||
m_client->close();
|
||||
|
||||
stop_client_thread();
|
||||
stop_thread_pool();
|
||||
// Must first clean up and push the generated chunk data into
|
||||
// m_pending_delete_vbo and m_pending_delete_vao; cannot delete them in the
|
||||
// destructor, otherwise it will cause leaks and use-after-free.
|
||||
m_dirty_chunk_queue.clear();
|
||||
m_pending_upload_queue.clear();
|
||||
|
||||
m_chunks.clear();
|
||||
|
||||
if (m_is_pending_delete_queue_free.exchange(true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard lk(m_delete_vbo_mutex);
|
||||
m_pending_delete_vbo.clear();
|
||||
@@ -262,10 +273,18 @@ void ClientWorld::set_block(const glm::ivec3& block_pos, unsigned id) {
|
||||
}
|
||||
}
|
||||
void ClientWorld::push_delete_vbo(std::unique_ptr<VertexBuffer>& vbo) {
|
||||
if (m_is_pending_delete_queue_free) {
|
||||
Logger::error("Push delete vbo Use After Free");
|
||||
return;
|
||||
}
|
||||
std::lock_guard lk(m_delete_vbo_mutex);
|
||||
m_pending_delete_vbo.push_back(std::move(vbo));
|
||||
}
|
||||
void ClientWorld::push_delete_vao(std::unique_ptr<VertexArray>& vao) {
|
||||
if (m_is_pending_delete_queue_free) {
|
||||
Logger::error("Push delete vao Use After Free");
|
||||
return;
|
||||
}
|
||||
std::lock_guard lk(m_delete_vao_mutex);
|
||||
m_pending_delete_vao.push_back(std::move(vao));
|
||||
}
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
using asio::ip::tcp;
|
||||
namespace Cubed {
|
||||
|
||||
NetworkServer::NetworkServer(int port) : m_port(port) {}
|
||||
NetworkServer::NetworkServer()
|
||||
: m_config(ASSETS_PATH "server-config.toml"), m_world(m_config) {
|
||||
m_port = m_config.get("port", 25530);
|
||||
}
|
||||
|
||||
NetworkServer::~NetworkServer() { stop(); }
|
||||
|
||||
@@ -84,6 +87,11 @@ void NetworkServer::net_run() {
|
||||
|
||||
void NetworkServer::start_server(int port) {
|
||||
m_port = port;
|
||||
m_config.set("port", m_port);
|
||||
start_server();
|
||||
}
|
||||
|
||||
void NetworkServer::start_server() {
|
||||
m_world.init_world();
|
||||
net_run();
|
||||
m_started = true;
|
||||
|
||||
@@ -13,7 +13,7 @@ using namespace std::chrono_literals;
|
||||
using namespace google::protobuf;
|
||||
|
||||
namespace Cubed {
|
||||
ServerWorld::ServerWorld() {}
|
||||
ServerWorld::ServerWorld(Config& config) : m_config(config) {}
|
||||
|
||||
ServerWorld::~ServerWorld() { stop(); }
|
||||
|
||||
@@ -500,8 +500,7 @@ bool ServerWorld::set_block(const glm::ivec3& block_pos, unsigned id) {
|
||||
}
|
||||
|
||||
void ServerWorld::hot_reload() {
|
||||
|
||||
int dist = 24;
|
||||
int dist = m_config.get("server_distance", 24);
|
||||
m_rendering_distance = dist <= MAX_DISTANCE ? dist : MAX_DISTANCE;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user