summaryrefslogtreecommitdiffstats
path: root/game/shared
diff options
context:
space:
mode:
authoruntodesu <kirill@untode.su>2025-09-11 13:48:31 +0500
committeruntodesu <kirill@untode.su>2025-09-11 13:48:31 +0500
commitaaed751bf4430bf4b9b30cef532b8753b9f639ce (patch)
tree16bc751c272ba27ad53ec48dbdd3a6d9e6a8d4c2 /game/shared
parent96bd73ae020ecca1f94698744c77498a89ad19f7 (diff)
downloadvoxelius-aaed751bf4430bf4b9b30cef532b8753b9f639ce.tar.bz2
voxelius-aaed751bf4430bf4b9b30cef532b8753b9f639ce.zip
Replace most of C strings with string_view
Diffstat (limited to 'game/shared')
-rw-r--r--game/shared/game.cc4
-rw-r--r--game/shared/protocol.cc8
-rw-r--r--game/shared/protocol.hh4
-rw-r--r--game/shared/splash.cc12
-rw-r--r--game/shared/splash.hh2
-rw-r--r--game/shared/threading.cc22
-rw-r--r--game/shared/world/dimension.cc6
-rw-r--r--game/shared/world/dimension.hh4
-rw-r--r--game/shared/world/item_registry.cc16
-rw-r--r--game/shared/world/item_registry.hh10
-rw-r--r--game/shared/world/voxel_registry.cc22
-rw-r--r--game/shared/world/voxel_registry.hh12
12 files changed, 68 insertions, 54 deletions
diff --git a/game/shared/game.cc b/game/shared/game.cc
index 62eaf13..94c2c1f 100644
--- a/game/shared/game.cc
+++ b/game/shared/game.cc
@@ -6,7 +6,7 @@
static std::filesystem::path get_gamepath(void)
{
- if(auto gamepath = io::cmdline::get("gamepath")) {
+ if(auto gamepath = io::cmdline::get_cstr("gamepath")) {
// Allow users and third-party launchers to override
// content location. Perhaps this would work to allow
// for a Minecraft-like versioning approach?
@@ -18,7 +18,7 @@ static std::filesystem::path get_gamepath(void)
static std::filesystem::path get_userpath(void)
{
- if(auto userpath = io::cmdline::get("userpath")) {
+ if(auto userpath = io::cmdline::get_cstr("userpath")) {
// Allow users and third-party launchers to override
// user data location. Perhaps this would work to allow
// for a Minecraft-like versioning approach?
diff --git a/game/shared/protocol.cc b/game/shared/protocol.cc
index f8a9ba8..34b7034 100644
--- a/game/shared/protocol.cc
+++ b/game/shared/protocol.cc
@@ -424,18 +424,18 @@ void protocol::decode(entt::dispatcher& dispatcher, const ENetPacket* packet, EN
}
}
-ENetPacket* protocol::utils::make_disconnect(const char* reason, enet_uint32 flags)
+ENetPacket* protocol::utils::make_disconnect(std::string_view reason, enet_uint32 flags)
{
protocol::Disconnect packet;
- packet.reason = std::string(reason);
+ packet.reason = reason;
return protocol::encode(packet, flags);
}
-ENetPacket* protocol::utils::make_chat_message(const char* message, enet_uint32 flags)
+ENetPacket* protocol::utils::make_chat_message(std::string_view message, enet_uint32 flags)
{
protocol::ChatMessage packet;
packet.type = protocol::ChatMessage::TEXT_MESSAGE;
- packet.message = std::string(message);
+ packet.message = message;
return protocol::encode(packet, flags);
}
diff --git a/game/shared/protocol.hh b/game/shared/protocol.hh
index 9141797..0b6038b 100644
--- a/game/shared/protocol.hh
+++ b/game/shared/protocol.hh
@@ -90,8 +90,8 @@ void decode(entt::dispatcher& dispatcher, const ENetPacket* packet, ENetPeer* pe
namespace protocol::utils
{
-ENetPacket* make_disconnect(const char* reason, enet_uint32 flags = ENET_PACKET_FLAG_RELIABLE);
-ENetPacket* make_chat_message(const char* message, enet_uint32 flags = ENET_PACKET_FLAG_RELIABLE);
+ENetPacket* make_disconnect(std::string_view reason, enet_uint32 flags = ENET_PACKET_FLAG_RELIABLE);
+ENetPacket* make_chat_message(std::string_view message, enet_uint32 flags = ENET_PACKET_FLAG_RELIABLE);
} // namespace protocol::utils
namespace protocol::utils
diff --git a/game/shared/splash.cc b/game/shared/splash.cc
index 1ff3e8f..d5b0a6e 100644
--- a/game/shared/splash.cc
+++ b/game/shared/splash.cc
@@ -2,8 +2,8 @@
#include "shared/splash.hh"
-constexpr static const char* SPLASHES_FILENAME_CLIENT = "misc/splashes_client.txt";
-constexpr static const char* SPLASHES_FILENAME_SERVER = "misc/splashes_server.txt";
+constexpr static std::string_view SPLASHES_FILENAME_CLIENT = "misc/splashes_client.txt";
+constexpr static std::string_view SPLASHES_FILENAME_SERVER = "misc/splashes_server.txt";
constexpr static std::size_t SPLASH_SERVER_MAX_LENGTH = 32;
static std::mt19937_64 splash_random;
@@ -22,9 +22,9 @@ static std::string sanitize_line(const std::string& line)
return result;
}
-static void splash_init_filename(const char* filename)
+static void splash_init_filename(std::string_view filename)
{
- if(auto file = PHYSFS_openRead(filename)) {
+ if(auto file = PHYSFS_openRead(std::string(filename).c_str())) {
auto source = std::string(PHYSFS_fileLength(file), char(0x00));
PHYSFS_readBytes(file, source.data(), source.size());
PHYSFS_close(file);
@@ -58,8 +58,8 @@ void splash::init_server(void)
}
}
-const char* splash::get(void)
+std::string_view splash::get(void)
{
std::uniform_int_distribution<std::size_t> dist(0, splash_lines.size() - 1);
- return splash_lines.at(dist(splash_random)).c_str();
+ return splash_lines.at(dist(splash_random));
}
diff --git a/game/shared/splash.hh b/game/shared/splash.hh
index f494a5b..2857621 100644
--- a/game/shared/splash.hh
+++ b/game/shared/splash.hh
@@ -6,7 +6,7 @@ namespace splash
{
void init_client(void);
void init_server(void);
-const char* get(void);
+std::string_view get(void);
} // namespace splash
#endif // SHARED_SPLASH_HH
diff --git a/game/shared/threading.cc b/game/shared/threading.cc
index ae3b3ea..209bd3c 100644
--- a/game/shared/threading.cc
+++ b/game/shared/threading.cc
@@ -5,7 +5,7 @@
#include "core/io/cmdline.hh"
#include "core/math/constexpr.hh"
-constexpr static const char* DEFAULT_POOL_SIZE_ARG = "4";
+constexpr static std::string_view DEFAULT_POOL_SIZE_ARG = "4";
static BS::light_thread_pool* thread_pool;
static std::deque<Task*> task_deque;
@@ -38,17 +38,31 @@ void threading::init(void)
auto num_concurrent_threads = std::thread::hardware_concurrency();
unsigned int thread_pool_size;
- if(num_concurrent_threads && !std::strcmp(argument, "max")) {
+ if(num_concurrent_threads && 0 == argument.compare("max")) {
// Use the maximum available number of concurrent
// hardware threads provided by the implementation
thread_pool_size = num_concurrent_threads;
}
else {
if(num_concurrent_threads) {
- thread_pool_size = math::clamp<unsigned int>(std::strtoul(argument, nullptr, 10), 1U, num_concurrent_threads);
+ auto result = std::from_chars(argument.data(), argument.data() + argument.size(), thread_pool_size);
+
+ if(result.ec == std::errc()) {
+ thread_pool_size = math::clamp<unsigned int>(thread_pool_size, 1U, num_concurrent_threads);
+ }
+ else {
+ thread_pool_size = 4U;
+ }
}
else {
- thread_pool_size = math::max<unsigned int>(std::strtoul(argument, nullptr, 10), 1U);
+ auto result = std::from_chars(argument.data(), argument.data() + argument.size(), thread_pool_size);
+
+ if(result.ec == std::errc()) {
+ thread_pool_size = math::max<unsigned int>(thread_pool_size, 1U);
+ }
+ else {
+ thread_pool_size = 4U;
+ }
}
}
diff --git a/game/shared/world/dimension.cc b/game/shared/world/dimension.cc
index 3389917..2d193fc 100644
--- a/game/shared/world/dimension.cc
+++ b/game/shared/world/dimension.cc
@@ -7,7 +7,7 @@
#include "shared/coord.hh"
#include "shared/globals.hh"
-world::Dimension::Dimension(const char* name, float gravity)
+world::Dimension::Dimension(std::string_view name, float gravity)
{
m_name = name;
m_gravity = gravity;
@@ -21,9 +21,9 @@ world::Dimension::~Dimension(void)
chunks.clear();
}
-const char* world::Dimension::get_name(void) const
+std::string_view world::Dimension::get_name(void) const
{
- return m_name.c_str();
+ return m_name;
}
float world::Dimension::get_gravity(void) const
diff --git a/game/shared/world/dimension.hh b/game/shared/world/dimension.hh
index 5b06895..3a383ac 100644
--- a/game/shared/world/dimension.hh
+++ b/game/shared/world/dimension.hh
@@ -26,10 +26,10 @@ namespace world
{
class Dimension {
public:
- explicit Dimension(const char* name, float gravity);
+ explicit Dimension(std::string_view name, float gravity);
virtual ~Dimension(void);
- const char* get_name(void) const;
+ std::string_view get_name(void) const;
float get_gravity(void) const;
public:
diff --git a/game/shared/world/item_registry.cc b/game/shared/world/item_registry.cc
index 378f0f1..e0d30cc 100644
--- a/game/shared/world/item_registry.cc
+++ b/game/shared/world/item_registry.cc
@@ -10,7 +10,7 @@ std::unordered_map<std::string, world::ItemInfoBuilder> world::item_registry::bu
std::unordered_map<std::string, item_id> world::item_registry::names = {};
std::vector<std::shared_ptr<world::ItemInfo>> world::item_registry::items = {};
-world::ItemInfoBuilder::ItemInfoBuilder(const char* name)
+world::ItemInfoBuilder::ItemInfoBuilder(std::string_view name)
{
prototype.name = name;
prototype.texture = std::string();
@@ -18,7 +18,7 @@ world::ItemInfoBuilder::ItemInfoBuilder(const char* name)
prototype.cached_texture = nullptr;
}
-world::ItemInfoBuilder& world::ItemInfoBuilder::set_texture(const char* texture)
+world::ItemInfoBuilder& world::ItemInfoBuilder::set_texture(std::string_view texture)
{
prototype.texture = texture;
prototype.cached_texture = nullptr;
@@ -52,21 +52,21 @@ item_id world::ItemInfoBuilder::build(void) const
return static_cast<item_id>(world::item_registry::items.size());
}
-world::ItemInfoBuilder& world::item_registry::construct(const char* name)
+world::ItemInfoBuilder& world::item_registry::construct(std::string_view name)
{
- const auto it = world::item_registry::builders.find(name);
+ const auto it = world::item_registry::builders.find(std::string(name));
if(it != world::item_registry::builders.cend()) {
return it->second;
}
else {
- return world::item_registry::builders.emplace(name, ItemInfoBuilder(name)).first->second;
+ return world::item_registry::builders.emplace(std::string(name), ItemInfoBuilder(name)).first->second;
}
}
-world::ItemInfo* world::item_registry::find(const char* name)
+world::ItemInfo* world::item_registry::find(std::string_view name)
{
- const auto it = world::item_registry::names.find(name);
+ const auto it = world::item_registry::names.find(std::string(name));
if(it != world::item_registry::names.cend()) {
return world::item_registry::find(it->second);
@@ -93,7 +93,7 @@ void world::item_registry::purge(void)
world::item_registry::items.clear();
}
-std::uint64_t world::item_registry::calcualte_checksum(void)
+std::uint64_t world::item_registry::calculate_checksum(void)
{
std::uint64_t result = 0;
diff --git a/game/shared/world/item_registry.hh b/game/shared/world/item_registry.hh
index 7cf3bd8..7508f2a 100644
--- a/game/shared/world/item_registry.hh
+++ b/game/shared/world/item_registry.hh
@@ -26,11 +26,11 @@ namespace world
{
class ItemInfoBuilder final {
public:
- explicit ItemInfoBuilder(const char* name);
+ explicit ItemInfoBuilder(std::string_view name);
virtual ~ItemInfoBuilder(void) = default;
public:
- ItemInfoBuilder& set_texture(const char* texture);
+ ItemInfoBuilder& set_texture(std::string_view texture);
ItemInfoBuilder& set_place_voxel(voxel_id place_voxel);
public:
@@ -50,8 +50,8 @@ extern std::vector<std::shared_ptr<ItemInfo>> items;
namespace world::item_registry
{
-ItemInfoBuilder& construct(const char* name);
-ItemInfo* find(const char* name);
+ItemInfoBuilder& construct(std::string_view name);
+ItemInfo* find(std::string_view name);
ItemInfo* find(const item_id item);
} // namespace world::item_registry
@@ -62,7 +62,7 @@ void purge(void);
namespace world::item_registry
{
-std::uint64_t calcualte_checksum(void);
+std::uint64_t calculate_checksum(void);
} // namespace world::item_registry
#endif // SHARED_ITEM_REGISTRY_HH
diff --git a/game/shared/world/voxel_registry.cc b/game/shared/world/voxel_registry.cc
index 8deab30..46737d6 100644
--- a/game/shared/world/voxel_registry.cc
+++ b/game/shared/world/voxel_registry.cc
@@ -8,7 +8,7 @@ std::unordered_map<std::string, world::VoxelInfoBuilder> world::voxel_registry::
std::unordered_map<std::string, voxel_id> world::voxel_registry::names = {};
std::vector<std::shared_ptr<world::VoxelInfo>> world::voxel_registry::voxels = {};
-world::VoxelInfoBuilder::VoxelInfoBuilder(const char* name, voxel_type type, bool animated, bool blending)
+world::VoxelInfoBuilder::VoxelInfoBuilder(std::string_view name, voxel_type type, bool animated, bool blending)
{
prototype.name = name;
prototype.type = type;
@@ -45,16 +45,16 @@ world::VoxelInfoBuilder::VoxelInfoBuilder(const char* name, voxel_type type, boo
prototype.item_pick = NULL_ITEM_ID;
}
-world::VoxelInfoBuilder& world::VoxelInfoBuilder::add_texture_default(const char* texture)
+world::VoxelInfoBuilder& world::VoxelInfoBuilder::add_texture_default(std::string_view texture)
{
- default_texture.paths.push_back(texture);
+ default_texture.paths.push_back(std::string(texture));
return *this;
}
-world::VoxelInfoBuilder& world::VoxelInfoBuilder::add_texture(voxel_face face, const char* texture)
+world::VoxelInfoBuilder& world::VoxelInfoBuilder::add_texture(voxel_face face, std::string_view texture)
{
const auto index = static_cast<std::size_t>(face);
- prototype.textures[index].paths.push_back(texture);
+ prototype.textures[index].paths.push_back(std::string(texture));
return *this;
}
@@ -140,21 +140,21 @@ voxel_id world::VoxelInfoBuilder::build(void) const
return new_info->base_voxel;
}
-world::VoxelInfoBuilder& world::voxel_registry::construct(const char* name, voxel_type type, bool animated, bool blending)
+world::VoxelInfoBuilder& world::voxel_registry::construct(std::string_view name, voxel_type type, bool animated, bool blending)
{
- const auto it = world::voxel_registry::builders.find(name);
+ const auto it = world::voxel_registry::builders.find(std::string(name));
if(it != world::voxel_registry::builders.cend()) {
return it->second;
}
else {
- return world::voxel_registry::builders.emplace(name, VoxelInfoBuilder(name, type, animated, blending)).first->second;
+ return world::voxel_registry::builders.emplace(std::string(name), VoxelInfoBuilder(name, type, animated, blending)).first->second;
}
}
-world::VoxelInfo* world::voxel_registry::find(const char* name)
+world::VoxelInfo* world::voxel_registry::find(std::string_view name)
{
- const auto it = world::voxel_registry::names.find(name);
+ const auto it = world::voxel_registry::names.find(std::string(name));
if(it != world::voxel_registry::names.cend()) {
return world::voxel_registry::find(it->second);
@@ -181,7 +181,7 @@ void world::voxel_registry::purge(void)
world::voxel_registry::voxels.clear();
}
-std::uint64_t world::voxel_registry::calcualte_checksum(void)
+std::uint64_t world::voxel_registry::calculate_checksum(void)
{
std::uint64_t result = 0;
diff --git a/game/shared/world/voxel_registry.hh b/game/shared/world/voxel_registry.hh
index 653c56e..9a7e206 100644
--- a/game/shared/world/voxel_registry.hh
+++ b/game/shared/world/voxel_registry.hh
@@ -108,12 +108,12 @@ namespace world
{
class VoxelInfoBuilder final {
public:
- explicit VoxelInfoBuilder(const char* name, voxel_type type, bool animated, bool blending);
+ explicit VoxelInfoBuilder(std::string_view name, voxel_type type, bool animated, bool blending);
virtual ~VoxelInfoBuilder(void) = default;
public:
- VoxelInfoBuilder& add_texture_default(const char* texture);
- VoxelInfoBuilder& add_texture(voxel_face face, const char* texture);
+ VoxelInfoBuilder& add_texture_default(std::string_view texture);
+ VoxelInfoBuilder& add_texture(voxel_face face, std::string_view texture);
VoxelInfoBuilder& set_touch(voxel_touch type, const glm::fvec3& values);
VoxelInfoBuilder& set_surface(voxel_surface surface);
@@ -135,8 +135,8 @@ extern std::vector<std::shared_ptr<VoxelInfo>> voxels;
namespace world::voxel_registry
{
-VoxelInfoBuilder& construct(const char* name, voxel_type type, bool animated, bool blending);
-VoxelInfo* find(const char* name);
+VoxelInfoBuilder& construct(std::string_view name, voxel_type type, bool animated, bool blending);
+VoxelInfo* find(std::string_view name);
VoxelInfo* find(const voxel_id voxel);
} // namespace world::voxel_registry
@@ -147,7 +147,7 @@ void purge(void);
namespace world::voxel_registry
{
-std::uint64_t calcualte_checksum(void);
+std::uint64_t calculate_checksum(void);
} // namespace world::voxel_registry
#endif // SHARED_VOXEL_REGISTRY_HH