diff options
| author | untodesu <kirill@untode.su> | 2025-09-11 13:48:31 +0500 |
|---|---|---|
| committer | untodesu <kirill@untode.su> | 2025-09-11 13:48:31 +0500 |
| commit | aaed751bf4430bf4b9b30cef532b8753b9f639ce (patch) | |
| tree | 16bc751c272ba27ad53ec48dbdd3a6d9e6a8d4c2 /game | |
| parent | 96bd73ae020ecca1f94698744c77498a89ad19f7 (diff) | |
| download | voxelius-aaed751bf4430bf4b9b30cef532b8753b9f639ce.tar.bz2 voxelius-aaed751bf4430bf4b9b30cef532b8753b9f639ce.zip | |
Replace most of C strings with string_view
Diffstat (limited to 'game')
56 files changed, 263 insertions, 248 deletions
diff --git a/game/client/config/gamepad_axis.cc b/game/client/config/gamepad_axis.cc index a82de81..8ae74be 100644 --- a/game/client/config/gamepad_axis.cc +++ b/game/client/config/gamepad_axis.cc @@ -6,9 +6,9 @@ #include "client/io/gamepad.hh" -constexpr static const char* UNKNOWN_AXIS_NAME = "UNKNOWN"; +constexpr static std::string_view UNKNOWN_AXIS_NAME = "UNKNOWN"; -static const std::pair<int, const char*> axis_names[] = { +static const std::pair<int, std::string_view> axis_names[] = { { GLFW_GAMEPAD_AXIS_LEFT_X, "LEFT_X" }, { GLFW_GAMEPAD_AXIS_LEFT_Y, "LEFT_Y" }, { GLFW_GAMEPAD_AXIS_RIGHT_X, "RIGHT_X" }, @@ -17,7 +17,7 @@ static const std::pair<int, const char*> axis_names[] = { { GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER, "RIGHT_TRIG" }, }; -static const char* get_axis_name(int axis) +static std::string_view get_axis_name(int axis) { for(const auto& it : axis_names) { if(it.first != axis) { @@ -42,19 +42,20 @@ config::GamepadAxis::GamepadAxis(int axis, bool inverted) m_full_string = std::format("{}:{}", m_name, m_inverted ? 1U : 0U); } -const char* config::GamepadAxis::get(void) const +std::string_view config::GamepadAxis::get(void) const { - return m_full_string.c_str(); + return m_full_string; } -void config::GamepadAxis::set(const char* value) +void config::GamepadAxis::set(std::string_view value) { char new_name[64]; unsigned int new_invert; + std::string value_str(value); - if(2 == std::sscanf(value, "%63[^:]:%u", new_name, &new_invert)) { + if(2 == std::sscanf(value_str.c_str(), "%63[^:]:%u", new_name, &new_invert)) { for(const auto& it : axis_names) { - if(!std::strcmp(it.second, new_name)) { + if(0 == it.second.compare(new_name)) { m_inverted = new_invert; m_gamepad_axis = it.first; m_name = get_axis_name(m_gamepad_axis); @@ -108,7 +109,7 @@ float config::GamepadAxis::get_value(const GLFWgamepadstate& state, float deadzo return 0.0f; } -const char* config::GamepadAxis::get_name(void) const +std::string_view config::GamepadAxis::get_name(void) const { return m_name; } diff --git a/game/client/config/gamepad_axis.hh b/game/client/config/gamepad_axis.hh index 0308ce6..86a2990 100644 --- a/game/client/config/gamepad_axis.hh +++ b/game/client/config/gamepad_axis.hh @@ -14,8 +14,8 @@ public: explicit GamepadAxis(int axis, bool inverted); virtual ~GamepadAxis(void) = default; - virtual const char* get(void) const override; - virtual void set(const char* value) override; + virtual std::string_view get(void) const override; + virtual void set(std::string_view value) override; int get_axis(void) const; void set_axis(int axis); @@ -29,13 +29,13 @@ public: // this configuration value actually contain the // inversion flag. Since we're updating that flag // in the UI by means of a separate checkbox, we only need the name here - const char* get_name(void) const; + std::string_view get_name(void) const; private: bool m_inverted; int m_gamepad_axis; std::string m_full_string; - const char* m_name; + std::string_view m_name; }; } // namespace config diff --git a/game/client/config/gamepad_button.cc b/game/client/config/gamepad_button.cc index b983baa..07e4457 100644 --- a/game/client/config/gamepad_button.cc +++ b/game/client/config/gamepad_button.cc @@ -6,9 +6,9 @@ #include "client/io/gamepad.hh" -constexpr static const char* UNKNOWN_BUTTON_NAME = "UNKNOWN"; +constexpr static std::string_view UNKNOWN_BUTTON_NAME = "UNKNOWN"; -static const std::pair<int, const char*> button_names[] = { +static const std::pair<int, std::string_view> button_names[] = { { GLFW_GAMEPAD_BUTTON_A, "A" }, { GLFW_GAMEPAD_BUTTON_B, "B" }, { GLFW_GAMEPAD_BUTTON_X, "X" }, @@ -26,7 +26,7 @@ static const std::pair<int, const char*> button_names[] = { { GLFW_GAMEPAD_BUTTON_DPAD_LEFT, "DPAD_LEFT" }, }; -static const char* get_button_name(int button) +static std::string_view get_button_name(int button) { for(const auto& it : button_names) { if(it.first == button) { @@ -49,15 +49,15 @@ config::GamepadButton::GamepadButton(int button) m_name = get_button_name(button); } -const char* config::GamepadButton::get(void) const +std::string_view config::GamepadButton::get(void) const { return m_name; } -void config::GamepadButton::set(const char* value) +void config::GamepadButton::set(std::string_view value) { for(const auto& it : button_names) { - if(!std::strcmp(it.second, value)) { + if(0 == it.second.compare(value)) { m_gamepad_button = it.first; m_name = it.second; return; diff --git a/game/client/config/gamepad_button.hh b/game/client/config/gamepad_button.hh index 11566c1..e1d1224 100644 --- a/game/client/config/gamepad_button.hh +++ b/game/client/config/gamepad_button.hh @@ -14,8 +14,8 @@ public: explicit GamepadButton(int button); virtual ~GamepadButton(void) = default; - virtual const char* get(void) const override; - virtual void set(const char* value) override; + virtual std::string_view get(void) const override; + virtual void set(std::string_view value) override; int get_button(void) const; void set_button(int button); @@ -25,7 +25,7 @@ public: private: int m_gamepad_button; - const char* m_name; + std::string_view m_name; }; } // namespace config diff --git a/game/client/config/keybind.cc b/game/client/config/keybind.cc index 6bd8ef0..e254f7b 100644 --- a/game/client/config/keybind.cc +++ b/game/client/config/keybind.cc @@ -6,9 +6,9 @@ #include "client/const.hh" -constexpr static const char* UNKNOWN_KEY_NAME = "UNKNOWN"; +constexpr static std::string_view UNKNOWN_KEY_NAME = "UNKNOWN"; -static const std::pair<int, const char*> key_names[] = { +static const std::pair<int, std::string_view> key_names[] = { { GLFW_KEY_SPACE, "SPACE" }, { GLFW_KEY_APOSTROPHE, "'" }, { GLFW_KEY_COMMA, "," }, @@ -131,7 +131,7 @@ static const std::pair<int, const char*> key_names[] = { { GLFW_KEY_MENU, "MENU" }, }; -static const char* get_key_name(int keycode) +static std::string_view get_key_name(int keycode) { for(const auto& it : key_names) { if(it.first == keycode) { @@ -160,10 +160,10 @@ config::KeyBind::KeyBind(int default_value) } } -void config::KeyBind::set(const char* value) +void config::KeyBind::set(std::string_view value) { for(const auto& it : key_names) { - if((it.first != DEBUG_KEY) && !std::strcmp(it.second, value)) { + if((it.first != DEBUG_KEY) && 0 == it.second.compare(value)) { m_glfw_keycode = it.first; m_name = it.second; return; @@ -174,7 +174,7 @@ void config::KeyBind::set(const char* value) m_name = UNKNOWN_KEY_NAME; } -const char* config::KeyBind::get(void) const +std::string_view config::KeyBind::get(void) const { return m_name; } diff --git a/game/client/config/keybind.hh b/game/client/config/keybind.hh index abfb97a..0005fe4 100644 --- a/game/client/config/keybind.hh +++ b/game/client/config/keybind.hh @@ -12,8 +12,8 @@ public: explicit KeyBind(int default_value); virtual ~KeyBind(void) = default; - virtual void set(const char* value) override; - virtual const char* get(void) const override; + virtual void set(std::string_view value) override; + virtual std::string_view get(void) const override; void set_key(int keycode); int get_key(void) const; @@ -21,7 +21,7 @@ public: bool equals(int keycode) const; private: - const char* m_name; + std::string_view m_name; int m_glfw_keycode; }; } // namespace config diff --git a/game/client/gui/bother.cc b/game/client/gui/bother.cc index 3a35438..d87f220 100644 --- a/game/client/gui/bother.cc +++ b/game/client/gui/bother.cc @@ -117,7 +117,7 @@ void gui::bother::update_late(void) } } -void gui::bother::ping(unsigned int identity, const char* host, std::uint16_t port) +void gui::bother::ping(unsigned int identity, std::string_view host, std::uint16_t port) { if(bother_set.count(identity)) { // Already in the process @@ -133,7 +133,7 @@ void gui::bother::ping(unsigned int identity, const char* host, std::uint16_t po BotherQueueItem item; item.identity = identity; - item.hostname = std::string(host); + item.hostname = host; item.port = port; bother_queue.push_back(item); diff --git a/game/client/gui/bother.hh b/game/client/gui/bother.hh index c10bf8a..f555d74 100644 --- a/game/client/gui/bother.hh +++ b/game/client/gui/bother.hh @@ -19,7 +19,7 @@ namespace gui::bother void init(void); void shutdown(void); void update_late(void); -void ping(unsigned int identity, const char* host, std::uint16_t port); +void ping(unsigned int identity, std::string_view host, std::uint16_t port); void cancel(unsigned int identity); } // namespace gui::bother diff --git a/game/client/gui/language.cc b/game/client/gui/language.cc index b5bdc7d..3c700a2 100644 --- a/game/client/gui/language.cc +++ b/game/client/gui/language.cc @@ -9,13 +9,13 @@ #include "client/globals.hh" -constexpr static const char* DEFAULT_LANGUAGE = "en_US"; +constexpr static std::string_view DEFAULT_LANGUAGE = "en_US"; // Available languages are kept in a special manifest file which // is essentially a key-value map of semi-IETF-compliant language tags // and the language's endonym; after reading the manifest, the translation // system knows what language it can load and will act accordingly -constexpr static const char* MANIFEST_PATH = "lang/manifest.json"; +constexpr static std::string_view MANIFEST_PATH = "lang/manifest.json"; static gui::LanguageManifest manifest; static gui::LanguageIterator current_language; @@ -36,7 +36,7 @@ void gui::language::init(void) settings::add_language_select(0, settings_location::GENERAL, "language"); - auto file = PHYSFS_openRead(MANIFEST_PATH); + auto file = PHYSFS_openRead(std::string(MANIFEST_PATH).c_str()); if(file == nullptr) { spdlog::critical("language: {}: {}", MANIFEST_PATH, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); @@ -85,14 +85,14 @@ void gui::language::init(void) void gui::language::init_late(void) { - auto user_language = ietf_map.find(config_language.get()); + auto user_language = ietf_map.find(config_language.get_value()); if(user_language != ietf_map.cend()) { gui::language::set(user_language->second); return; } - auto fallback = ietf_map.find(DEFAULT_LANGUAGE); + auto fallback = ietf_map.find(std::string(DEFAULT_LANGUAGE)); if(fallback != ietf_map.cend()) { gui::language::set(fallback->second); @@ -159,9 +159,9 @@ gui::LanguageIterator gui::language::get_current(void) return current_language; } -gui::LanguageIterator gui::language::find(const char* ietf) +gui::LanguageIterator gui::language::find(std::string_view ietf) { - const auto it = ietf_map.find(ietf); + const auto it = ietf_map.find(std::string(ietf)); if(it != ietf_map.cend()) { return it->second; } @@ -180,18 +180,18 @@ gui::LanguageIterator gui::language::cend(void) return manifest.cend(); } -const char* gui::language::resolve(const char* key) +std::string_view gui::language::resolve(std::string_view key) { - const auto it = language_map.find(key); + const auto it = language_map.find(std::string(key)); + if(it != language_map.cend()) { - return it->second.c_str(); - } - else { - return key; + return it->second; } + + return key; } -std::string gui::language::resolve_gui(const char* key) +std::string gui::language::resolve_gui(std::string_view key) { // We need window tags to retain their hierarchy when a language // dynamically changes; ImGui allows to provide hidden unique identifiers diff --git a/game/client/gui/language.hh b/game/client/gui/language.hh index d54208a..37cc790 100644 --- a/game/client/gui/language.hh +++ b/game/client/gui/language.hh @@ -32,15 +32,15 @@ void set(LanguageIterator new_language); namespace gui::language { LanguageIterator get_current(void); -LanguageIterator find(const char* ietf); +LanguageIterator find(std::string_view ietf); LanguageIterator cbegin(void); LanguageIterator cend(void); } // namespace gui::language namespace gui::language { -const char* resolve(const char* key); -std::string resolve_gui(const char* key); +std::string_view resolve(std::string_view key); +std::string resolve_gui(std::string_view key); } // namespace gui::language #endif // CLIENT_LANGUAGE_HH diff --git a/game/client/gui/main_menu.cc b/game/client/gui/main_menu.cc index 37ee398..62802a8 100644 --- a/game/client/gui/main_menu.cc +++ b/game/client/gui/main_menu.cc @@ -158,7 +158,7 @@ void gui::main_menu::layout(void) ImGui::PushFont(globals::font_debug); ImGui::SetCursorScreenPos(ImVec2(padding.x + spacing.x, window_size.y - globals::font_debug->FontSize - padding.y - spacing.y)); - ImGui::Text("Voxelius %s", project_version_string); + ImGui::Text("Voxelius %*s", version::semver.size(), version::semver.data()); // string_view is not always null-terminated ImGui::PopFont(); } diff --git a/game/client/gui/message_box.cc b/game/client/gui/message_box.cc index 615281b..59e2d33 100644 --- a/game/client/gui/message_box.cc +++ b/game/client/gui/message_box.cc @@ -75,17 +75,17 @@ void gui::message_box::reset(void) buttons.clear(); } -void gui::message_box::set_title(const char* title) +void gui::message_box::set_title(std::string_view title) { str_title = gui::language::resolve(title); } -void gui::message_box::set_subtitle(const char* subtitle) +void gui::message_box::set_subtitle(std::string_view subtitle) { str_subtitle = gui::language::resolve(subtitle); } -void gui::message_box::add_button(const char* text, const message_box_action& action) +void gui::message_box::add_button(std::string_view text, const message_box_action& action) { Button button = {}; button.str_title = std::format("{}###MessageBox_Button{}", gui::language::resolve(text), buttons.size()); diff --git a/game/client/gui/message_box.hh b/game/client/gui/message_box.hh index c5545fc..a906744 100644 --- a/game/client/gui/message_box.hh +++ b/game/client/gui/message_box.hh @@ -16,9 +16,9 @@ void reset(void); namespace gui::message_box { -void set_title(const char* title); -void set_subtitle(const char* subtitle); -void add_button(const char* text, const message_box_action& action); +void set_title(std::string_view title); +void set_subtitle(std::string_view subtitle); +void add_button(std::string_view text, const message_box_action& action); } // namespace gui::message_box #endif // CLIENT_MESSAGE_BOX_HH diff --git a/game/client/gui/metrics.cc b/game/client/gui/metrics.cc index 350208c..f439fd0 100644 --- a/game/client/gui/metrics.cc +++ b/game/client/gui/metrics.cc @@ -50,7 +50,7 @@ void gui::metrics::layout(void) auto y_step = 1.5f * globals::font_debug->FontSize; // Draw version - auto version_line = std::format("Voxelius {}", project_version_string); + auto version_line = std::format("Voxelius {} [{}]", version::semver, version::commit); gui::imdraw_ext::text_shadow(version_line, position, text_color, shadow_color, globals::font_debug, draw_list); position.y += 1.5f * y_step; diff --git a/game/client/gui/play_menu.cc b/game/client/gui/play_menu.cc index f2a8887..dc4ffed 100644 --- a/game/client/gui/play_menu.cc +++ b/game/client/gui/play_menu.cc @@ -19,9 +19,9 @@ #include "client/session.hh" constexpr static ImGuiWindowFlags WINDOW_FLAGS = ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration; -constexpr static const char* DEFAULT_SERVER_NAME = "Voxelius Server"; -constexpr static const char* SERVERS_TXT = "servers.txt"; -constexpr static const char* WARNING_TOAST = "[!]"; +constexpr static std::string_view DEFAULT_SERVER_NAME = "Voxelius Server"; +constexpr static std::string_view SERVERS_TXT = "servers.txt"; +constexpr static std::string_view WARNING_TOAST = "[!]"; constexpr static std::size_t MAX_SERVER_ITEM_NAME = 24; @@ -265,10 +265,11 @@ static void layout_server_item(ServerStatusItem* item) draw_list->AddText(stats_pos, ImGui::GetColorU32(ImGuiCol_TextDisabled), stats.c_str(), stats.c_str() + stats.size()); if(item->protocol_version != protocol::VERSION) { - auto warning_size = ImGui::CalcTextSize(WARNING_TOAST); + auto warning_size = ImGui::CalcTextSize(WARNING_TOAST.data(), WARNING_TOAST.data() + WARNING_TOAST.size()); auto warning_pos = ImVec2(stats_pos.x - warning_size.x - padding.x - 4.0f * globals::gui_scale, cursor.y + padding.y); auto warning_end = ImVec2(warning_pos.x + warning_size.x, warning_pos.y + warning_size.y); - draw_list->AddText(warning_pos, ImGui::GetColorU32(ImGuiCol_DragDropTarget), WARNING_TOAST); + draw_list->AddText(warning_pos, ImGui::GetColorU32(ImGuiCol_DragDropTarget), WARNING_TOAST.data(), + WARNING_TOAST.data() + WARNING_TOAST.size()); if(ImGui::IsMouseHoveringRect(warning_pos, warning_end)) { ImGui::BeginTooltip(); @@ -442,7 +443,7 @@ static void layout_servers_buttons(void) void gui::play_menu::init(void) { - if(auto file = PHYSFS_openRead(SERVERS_TXT)) { + if(auto file = PHYSFS_openRead(std::string(SERVERS_TXT).c_str())) { auto source = std::string(PHYSFS_fileLength(file), char(0x00)); PHYSFS_readBytes(file, source.data(), source.size()); PHYSFS_close(file); @@ -496,7 +497,7 @@ void gui::play_menu::shutdown(void) stream << std::format("{}:{}%{}%{}", item->hostname, item->port, item->password, item->name) << std::endl; } - if(auto file = PHYSFS_openWrite(SERVERS_TXT)) { + if(auto file = PHYSFS_openWrite(std::string(SERVERS_TXT).c_str())) { auto source = stream.str(); PHYSFS_writeBytes(file, source.data(), source.size()); PHYSFS_close(file); diff --git a/game/client/gui/progress_bar.cc b/game/client/gui/progress_bar.cc index 2bfb69e..8be2f8c 100644 --- a/game/client/gui/progress_bar.cc +++ b/game/client/gui/progress_bar.cc @@ -99,12 +99,12 @@ void gui::progress_bar::reset(void) button_action = nullptr; } -void gui::progress_bar::set_title(const char* title) +void gui::progress_bar::set_title(std::string_view title) { str_title = gui::language::resolve(title); } -void gui::progress_bar::set_button(const char* text, const progress_bar_action& action) +void gui::progress_bar::set_button(std::string_view text, const progress_bar_action& action) { str_button = std::format("{}###ProgressBar_Button", gui::language::resolve(text)); button_action = action; diff --git a/game/client/gui/progress_bar.hh b/game/client/gui/progress_bar.hh index 3765543..79fdcb5 100644 --- a/game/client/gui/progress_bar.hh +++ b/game/client/gui/progress_bar.hh @@ -16,8 +16,8 @@ void layout(void); namespace gui::progress_bar { void reset(void); -void set_title(const char* title); -void set_button(const char* text, const progress_bar_action& action); +void set_title(std::string_view title); +void set_button(std::string_view text, const progress_bar_action& action); } // namespace gui::progress_bar #endif // CLIENT_PROGRESS_BAR_HH diff --git a/game/client/gui/settings.cc b/game/client/gui/settings.cc index 034db4c..e8f9bca 100644 --- a/game/client/gui/settings.cc +++ b/game/client/gui/settings.cc @@ -311,7 +311,7 @@ void SettingValue_InputUnsigned::layout(void) const void SettingValue_InputString::layout(void) const { ImGuiInputTextFlags flags; - std::string current_value = value->get(); + std::string current_value(value->get_value()); if(allow_whitespace) { flags = ImGuiInputTextFlags_AllowTabInput; @@ -321,7 +321,7 @@ void SettingValue_InputString::layout(void) const } if(ImGui::InputText(wid.c_str(), ¤t_value, flags)) { - value->set(current_value.c_str()); + value->set(current_value); } layout_label(); @@ -847,7 +847,7 @@ void settings::layout(void) ImGui::End(); } -void settings::add_checkbox(int priority, config::Boolean& value, settings_location location, const char* name, bool tooltip) +void settings::add_checkbox(int priority, config::Boolean& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_CheckBox; setting_value->type = setting_type::CHECKBOX; @@ -862,7 +862,7 @@ void settings::add_checkbox(int priority, config::Boolean& value, settings_locat values_all.push_back(setting_value); } -void settings::add_input(int priority, config::Int& value, settings_location location, const char* name, bool tooltip) +void settings::add_input(int priority, config::Int& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_InputInt; setting_value->type = setting_type::INPUT_INT; @@ -877,13 +877,15 @@ void settings::add_input(int priority, config::Int& value, settings_location loc values_all.push_back(setting_value); } -void settings::add_input(int priority, config::Float& value, settings_location location, const char* name, bool tooltip, const char* format) +void settings::add_input( + int priority, config::Float& value, settings_location location, std::string_view name, bool tooltip, std::string_view fmt) { auto setting_value = new SettingValue_InputFloat; setting_value->type = setting_type::INPUT_FLOAT; setting_value->priority = priority; setting_value->has_tooltip = tooltip; setting_value->value = &value; + setting_value->format = fmt; setting_value->name = name; setting_value->wid = std::format("###{}", static_cast<const void*>(setting_value->value)); @@ -892,7 +894,7 @@ void settings::add_input(int priority, config::Float& value, settings_location l values_all.push_back(setting_value); } -void settings::add_input(int priority, config::Unsigned& value, settings_location location, const char* name, bool tooltip) +void settings::add_input(int priority, config::Unsigned& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_InputUnsigned; setting_value->type = setting_type::INPUT_UINT; @@ -908,7 +910,7 @@ void settings::add_input(int priority, config::Unsigned& value, settings_locatio } void settings::add_input( - int priority, config::String& value, settings_location location, const char* name, bool tooltip, bool allow_whitespace) + int priority, config::String& value, settings_location location, std::string_view name, bool tooltip, bool allow_whitespace) { auto setting_value = new SettingValue_InputString; setting_value->type = setting_type::INPUT_STRING; @@ -924,7 +926,7 @@ void settings::add_input( values_all.push_back(setting_value); } -void settings::add_slider(int priority, config::Int& value, settings_location location, const char* name, bool tooltip) +void settings::add_slider(int priority, config::Int& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_SliderInt; setting_value->type = setting_type::SLIDER_INT; @@ -940,7 +942,7 @@ void settings::add_slider(int priority, config::Int& value, settings_location lo } void settings::add_slider( - int priority, config::Float& value, settings_location location, const char* name, bool tooltip, const char* format) + int priority, config::Float& value, settings_location location, std::string_view name, bool tooltip, std::string_view fmt) { auto setting_value = new SettingValue_SliderFloat; setting_value->type = setting_type::SLIDER_FLOAT; @@ -949,14 +951,14 @@ void settings::add_slider( setting_value->value = &value; setting_value->name = name; - setting_value->format = format; + setting_value->format = fmt; setting_value->wid = std::format("###{}", static_cast<const void*>(setting_value->value)); values[static_cast<unsigned int>(location)].push_back(setting_value); values_all.push_back(setting_value); } -void settings::add_slider(int priority, config::Unsigned& value, settings_location location, const char* name, bool tooltip) +void settings::add_slider(int priority, config::Unsigned& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_SliderUnsigned; setting_value->type = setting_type::SLIDER_UINT; @@ -971,7 +973,7 @@ void settings::add_slider(int priority, config::Unsigned& value, settings_locati values_all.push_back(setting_value); } -void settings::add_stepper(int priority, config::Int& value, settings_location location, const char* name, bool tooltip) +void settings::add_stepper(int priority, config::Int& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_StepperInt; setting_value->type = setting_type::STEPPER_INT; @@ -987,7 +989,7 @@ void settings::add_stepper(int priority, config::Int& value, settings_location l values_all.push_back(setting_value); } -void settings::add_stepper(int priority, config::Unsigned& value, settings_location location, const char* name, bool tooltip) +void settings::add_stepper(int priority, config::Unsigned& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_StepperUnsigned; setting_value->type = setting_type::STEPPER_UINT; @@ -1003,7 +1005,7 @@ void settings::add_stepper(int priority, config::Unsigned& value, settings_locat values_all.push_back(setting_value); } -void settings::add_keybind(int priority, config::KeyBind& value, settings_location location, const char* name) +void settings::add_keybind(int priority, config::KeyBind& value, settings_location location, std::string_view name) { auto setting_value = new SettingValue_KeyBind; setting_value->type = setting_type::KEYBIND; @@ -1018,7 +1020,7 @@ void settings::add_keybind(int priority, config::KeyBind& value, settings_locati values_all.push_back(setting_value); } -void settings::add_gamepad_axis(int priority, config::GamepadAxis& value, settings_location location, const char* name) +void settings::add_gamepad_axis(int priority, config::GamepadAxis& value, settings_location location, std::string_view name) { auto setting_value = new SettingValue_GamepadAxis; setting_value->type = setting_type::GAMEPAD_AXIS; @@ -1033,7 +1035,7 @@ void settings::add_gamepad_axis(int priority, config::GamepadAxis& value, settin values_all.push_back(setting_value); } -void settings::add_gamepad_button(int priority, config::GamepadButton& value, settings_location location, const char* name) +void settings::add_gamepad_button(int priority, config::GamepadButton& value, settings_location location, std::string_view name) { auto setting_value = new SettingValue_GamepadButton; setting_value->type = setting_type::GAMEPAD_BUTTON; @@ -1048,7 +1050,7 @@ void settings::add_gamepad_button(int priority, config::GamepadButton& value, se values_all.push_back(setting_value); } -void settings::add_language_select(int priority, settings_location location, const char* name) +void settings::add_language_select(int priority, settings_location location, std::string_view name) { auto setting_value = new SettingValue_Language; setting_value->type = setting_type::LANGUAGE_SELECT; diff --git a/game/client/gui/settings.hh b/game/client/gui/settings.hh index 15aa6a7..d807bc4 100644 --- a/game/client/gui/settings.hh +++ b/game/client/gui/settings.hh @@ -49,45 +49,46 @@ void layout(void); namespace settings { -void add_checkbox(int priority, config::Boolean& value, settings_location location, const char* name, bool tooltip); +void add_checkbox(int priority, config::Boolean& value, settings_location location, std::string_view name, bool tooltip); } // namespace settings namespace settings { -void add_input(int priority, config::Int& value, settings_location location, const char* name, bool tooltip); -void add_input(int priority, config::Float& value, settings_location location, const char* name, bool tooltip, const char* format = "%.3f"); -void add_input(int priority, config::Unsigned& value, settings_location location, const char* name, bool tooltip); -void add_input(int priority, config::String& value, settings_location location, const char* name, bool tooltip, bool allow_whitespace); +void add_input(int priority, config::Int& value, settings_location location, std::string_view name, bool tooltip); +void add_input( + int priority, config::Float& value, settings_location location, std::string_view name, bool tooltip, std::string_view fmt = "%.3f"); +void add_input(int priority, config::Unsigned& value, settings_location location, std::string_view name, bool tooltip); +void add_input(int priority, config::String& value, settings_location location, std::string_view name, bool tooltip, bool allow_whitespace); } // namespace settings namespace settings { -void add_slider(int priority, config::Int& value, settings_location location, const char* name, bool tooltip); +void add_slider(int priority, config::Int& value, settings_location location, std::string_view name, bool tooltip); void add_slider( - int priority, config::Float& value, settings_location location, const char* name, bool tooltip, const char* format = "%.3f"); -void add_slider(int priority, config::Unsigned& value, settings_location location, const char* name, bool tooltip); + int priority, config::Float& value, settings_location location, std::string_view name, bool tooltip, std::string_view format = "%.3f"); +void add_slider(int priority, config::Unsigned& value, settings_location location, std::string_view name, bool tooltip); } // namespace settings namespace settings { -void add_stepper(int priority, config::Int& value, settings_location location, const char* name, bool tooltip); -void add_stepper(int priority, config::Unsigned& value, settings_location location, const char* name, bool tooltip); +void add_stepper(int priority, config::Int& value, settings_location location, std::string_view name, bool tooltip); +void add_stepper(int priority, config::Unsigned& value, settings_location location, std::string_view name, bool tooltip); } // namespace settings namespace settings { -void add_keybind(int priority, config::KeyBind& value, settings_location location, const char* name); +void add_keybind(int priority, config::KeyBind& value, settings_location location, std::string_view name); } // namespace settings namespace settings { -void add_gamepad_axis(int priority, config::GamepadAxis& value, settings_location location, const char* name); -void add_gamepad_button(int priority, config::GamepadButton& value, settings_location location, const char* name); +void add_gamepad_axis(int priority, config::GamepadAxis& value, settings_location location, std::string_view name); +void add_gamepad_button(int priority, config::GamepadButton& value, settings_location location, std::string_view name); } // namespace settings namespace settings { -void add_language_select(int priority, settings_location location, const char* name); +void add_language_select(int priority, settings_location location, std::string_view name); } // namespace settings #endif // CLIENT_SETTINGS_HH diff --git a/game/client/gui/splash.cc b/game/client/gui/splash.cc index a144f97..9eed8d3 100644 --- a/game/client/gui/splash.cc +++ b/game/client/gui/splash.cc @@ -18,7 +18,7 @@ constexpr static ImGuiWindowFlags WINDOW_FLAGS = ImGuiWindowFlags_NoBackground | constexpr static int SPLASH_COUNT = 4; constexpr static std::size_t DELAY_MICROSECONDS = 2000000; -constexpr static const char* SPLASH_PATH = "textures/gui/client_splash.png"; +constexpr static std::string_view SPLASH_PATH = "textures/gui/client_splash.png"; static resource_ptr<TextureGUI> texture; static float texture_aspect; diff --git a/game/client/gui/window_title.cc b/game/client/gui/window_title.cc index 91e308f..6f46668 100644 --- a/game/client/gui/window_title.cc +++ b/game/client/gui/window_title.cc @@ -13,10 +13,10 @@ void gui::window_title::update(void) std::string title; if(globals::sound_ctx && globals::sound_dev) { - title = std::format("Voxelius {}: {}", project_version_string, splash::get()); + title = std::format("Voxelius {}: {}", version::semver, splash::get()); } else { - title = std::format("Voxelius {}: {} [NOSOUND]", project_version_string, splash::get()); + title = std::format("Voxelius {}: {} [NOSOUND]", version::semver, splash::get()); } glfwSetWindowTitle(globals::window, title.c_str()); diff --git a/game/client/io/gamepad.cc b/game/client/io/gamepad.cc index 9910950..d07ee86 100644 --- a/game/client/io/gamepad.cc +++ b/game/client/io/gamepad.cc @@ -91,7 +91,7 @@ void io::gamepad::init(void) settings::add_checkbox(0, io::gamepad::active, settings_location::GAMEPAD, "gamepad.active", true); settings::add_slider(1, io::gamepad::deadzone, settings_location::GAMEPAD, "gamepad.deadzone", true, "%.03f"); - auto mappings_path = io::cmdline::get("gpmap", "misc/gamecontrollerdb.txt"); + auto mappings_path = io::cmdline::get_cstr("gpmap", "misc/gamecontrollerdb.txt"); auto mappings_file = PHYSFS_openRead(mappings_path); if(mappings_file) { diff --git a/game/client/main.cc b/game/client/main.cc index cb3e209..06a0da2 100644 --- a/game/client/main.cc +++ b/game/client/main.cc @@ -157,7 +157,7 @@ int main(int argc, char** argv) shared_game::init(argc, argv); - spdlog::info("Voxelius Client {}", project_version_string); + spdlog::info("Voxelius Client {}", version::semver); glfwSetErrorCallback(&on_glfw_error); @@ -312,7 +312,7 @@ int main(int argc, char** argv) int vmode_width = DEFAULT_WIDTH; int vmode_height = DEFAULT_HEIGHT; - if(auto vmode = io::cmdline::get("mode")) { + if(auto vmode = io::cmdline::get_cstr("mode")) { std::sscanf(vmode, "%dx%d", &vmode_width, &vmode_height); vmode_height = math::max(vmode_height, MIN_HEIGHT); vmode_width = math::max(vmode_width, MIN_WIDTH); diff --git a/game/client/program.cc b/game/client/program.cc index 17441e6..e35716d 100644 --- a/game/client/program.cc +++ b/game/client/program.cc @@ -6,10 +6,10 @@ // This fills up the array of source lines and figures out // which lines are to be dynamically resolved as variant macros -static void parse_source(const char* source, std::vector<std::string>& out_lines, std::vector<GL_VariedMacro>& out_variants) +static void parse_source(std::string_view source, std::vector<std::string>& out_lines, std::vector<GL_VariedMacro>& out_variants) { std::string line; - std::istringstream stream = std::istringstream(source); + std::istringstream stream = std::istringstream(std::string(source)); unsigned long line_number = 0UL; out_lines.clear(); @@ -38,7 +38,7 @@ static void parse_source(const char* source, std::vector<std::string>& out_lines } } -static GLuint compile_shader(const char* path, const char* source, GLenum shader_stage) +static GLuint compile_shader(std::string_view path, const char* source, GLenum shader_stage) { GLuint shader = glCreateShader(shader_stage); glShaderSource(shader, 1, &source, nullptr); @@ -66,14 +66,14 @@ static GLuint compile_shader(const char* path, const char* source, GLenum shader return shader; } -bool GL_Program::setup(const char* vpath, const char* fpath) +bool GL_Program::setup(std::string_view vpath, std::string_view fpath) { destroy(); vert_path = std::string(vpath); frag_path = std::string(fpath); - auto vfile = PHYSFS_openRead(vpath); + auto vfile = PHYSFS_openRead(vert_path.c_str()); if(vfile == nullptr) { spdlog::warn("gl_program: {}: {}", vpath, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); @@ -84,7 +84,7 @@ bool GL_Program::setup(const char* vpath, const char* fpath) PHYSFS_readBytes(vfile, vsource.data(), vsource.size()); PHYSFS_close(vfile); - auto ffile = PHYSFS_openRead(fpath); + auto ffile = PHYSFS_openRead(frag_path.c_str()); if(ffile == nullptr) { spdlog::warn("gl_program: {}: {}", fpath, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); @@ -188,10 +188,10 @@ void GL_Program::destroy(void) needs_update = false; } -std::size_t GL_Program::add_uniform(const char* name) +std::size_t GL_Program::add_uniform(std::string_view name) { for(std::size_t i = 0; i < uniforms.size(); ++i) { - if(!uniforms[i].name.compare(name)) { + if(0 == uniforms[i].name.compare(name)) { return i; } } diff --git a/game/client/program.hh b/game/client/program.hh index ce2538b..4e796fa 100644 --- a/game/client/program.hh +++ b/game/client/program.hh @@ -15,11 +15,11 @@ struct GL_Uniform final { class GL_Program final { public: - bool setup(const char* vpath, const char* fpath); + bool setup(std::string_view vpath, std::string_view fpath); void destroy(void); bool update(void); - std::size_t add_uniform(const char* name); + std::size_t add_uniform(std::string_view name); void set_variant_vert(unsigned int variant, unsigned int value); void set_variant_frag(unsigned int variant, unsigned int value); diff --git a/game/client/resource/sound_effect.cc b/game/client/resource/sound_effect.cc index 0d987a5..75d5984 100644 --- a/game/client/resource/sound_effect.cc +++ b/game/client/resource/sound_effect.cc @@ -24,9 +24,9 @@ static drwav_bool32 drwav_seek_physfs(void* file, int offset, drwav_seek_origin } template<> -resource_ptr<SoundEffect> resource::load<SoundEffect>(const char* name, unsigned int flags) +resource_ptr<SoundEffect> resource::load<SoundEffect>(std::string_view name, unsigned int flags) { - auto it = resource_map.find(name); + auto it = resource_map.find(std::string(name)); if(it != resource_map.cend()) { // Return an existing resource @@ -38,7 +38,7 @@ resource_ptr<SoundEffect> resource::load<SoundEffect>(const char* name, unsigned return nullptr; } - auto file = PHYSFS_openRead(name); + auto file = PHYSFS_openRead(std::string(name).c_str()); if(file == nullptr) { spdlog::warn("resource: {} [SoundEffect]: {}", name, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); @@ -76,7 +76,7 @@ resource_ptr<SoundEffect> resource::load<SoundEffect>(const char* name, unsigned delete[] samples; - return resource_map.insert_or_assign(name, new_resource).first->second; + return resource_map.insert_or_assign(std::string(name), new_resource).first->second; } template<> diff --git a/game/client/resource/texture_gui.cc b/game/client/resource/texture_gui.cc index 971e201..415845d 100644 --- a/game/client/resource/texture_gui.cc +++ b/game/client/resource/texture_gui.cc @@ -8,9 +8,9 @@ static emhash8::HashMap<std::string, resource_ptr<TextureGUI>> resource_map; template<> -resource_ptr<TextureGUI> resource::load<TextureGUI>(const char* name, unsigned int flags) +resource_ptr<TextureGUI> resource::load<TextureGUI>(std::string_view name, unsigned int flags) { - auto it = resource_map.find(name); + auto it = resource_map.find(std::string(name)); if(it != resource_map.cend()) { // Return an existing resource @@ -67,7 +67,7 @@ resource_ptr<TextureGUI> resource::load<TextureGUI>(const char* name, unsigned i new_resource->size.x = image->size.x; new_resource->size.y = image->size.y; - return resource_map.insert_or_assign(name, new_resource).first->second; + return resource_map.insert_or_assign(std::string(name), new_resource).first->second; } return nullptr; diff --git a/game/client/session.cc b/game/client/session.cc index 521e0c2..5826b02 100644 --- a/game/client/session.cc +++ b/game/client/session.cc @@ -188,10 +188,10 @@ void session::invalidate(void) globals::dimension = nullptr; } -void session::connect(const char* host, std::uint16_t port, const char* password) +void session::connect(std::string_view host, std::uint16_t port, std::string_view password) { ENetAddress address; - enet_address_set_host(&address, host); + enet_address_set_host(&address, std::string(host).c_str()); address.port = port; session::peer = enet_host_connect(globals::client_host, &address, 1, 0); @@ -202,7 +202,7 @@ void session::connect(const char* host, std::uint16_t port, const char* password globals::fixed_frametime = 0.0f; globals::fixed_accumulator = 0; - server_password_hash = math::crc64(password); + server_password_hash = math::crc64(password.data(), password.size()); if(!session::peer) { server_password_hash = UINT64_MAX; @@ -245,7 +245,7 @@ void session::connect(const char* host, std::uint16_t port, const char* password globals::gui_screen = GUI_PROGRESS_BAR; } -void session::disconnect(const char* reason) +void session::disconnect(std::string_view reason) { if(session::peer) { protocol::Disconnect packet; @@ -279,8 +279,8 @@ void session::send_login_request(void) { protocol::LoginRequest packet; packet.version = protocol::VERSION; - packet.voxel_registry_checksum = world::voxel_registry::calcualte_checksum(); - packet.item_registry_checksum = world::item_registry::calcualte_checksum(); + packet.voxel_registry_checksum = world::voxel_registry::calculate_checksum(); + packet.item_registry_checksum = world::item_registry::calculate_checksum(); packet.password_hash = server_password_hash; packet.username = client_game::username.get(); diff --git a/game/client/session.hh b/game/client/session.hh index a763d18..8384a49 100644 --- a/game/client/session.hh +++ b/game/client/session.hh @@ -18,8 +18,8 @@ void invalidate(void); namespace session { -void connect(const char* hostname, std::uint16_t port, const char* password); -void disconnect(const char* reason); +void connect(std::string_view hostname, std::uint16_t port, std::string_view password); +void disconnect(std::string_view reason); void send_login_request(void); } // namespace session diff --git a/game/client/sound/sound.cc b/game/client/sound/sound.cc index f3bd5e2..c77f968 100644 --- a/game/client/sound/sound.cc +++ b/game/client/sound/sound.cc @@ -95,43 +95,43 @@ void sound::update(void) alSourcef(ui_source, AL_GAIN, ui_gain); } -void sound::play_generic(const char* sound, bool looping, float pitch) +void sound::play_generic(std::string_view sound, bool looping, float pitch) { - if(sound) { + if(sound.size()) { sound::play_generic(resource::load<SoundEffect>(sound), looping, pitch); } else { - sound::play_generic(static_cast<const char*>(nullptr), looping, pitch); + sound::play_generic(static_cast<resource_ptr<SoundEffect>>(nullptr), looping, pitch); } } -void sound::play_entity(entt::entity entity, const char* sound, bool looping, float pitch) +void sound::play_entity(entt::entity entity, std::string_view sound, bool looping, float pitch) { - if(sound) { + if(sound.size()) { sound::play_entity(entity, resource::load<SoundEffect>(sound), looping, pitch); } else { - sound::play_entity(entity, static_cast<const char*>(nullptr), looping, pitch); + sound::play_entity(entity, static_cast<resource_ptr<SoundEffect>>(nullptr), looping, pitch); } } -void sound::play_player(const char* sound, bool looping, float pitch) +void sound::play_player(std::string_view sound, bool looping, float pitch) { - if(sound) { + if(sound.size()) { sound::play_player(resource::load<SoundEffect>(sound), looping, pitch); } else { - sound::play_player(static_cast<const char*>(nullptr), looping, pitch); + sound::play_player(static_cast<resource_ptr<SoundEffect>>(nullptr), looping, pitch); } } -void sound::play_ui(const char* sound, bool looping, float pitch) +void sound::play_ui(std::string_view sound, bool looping, float pitch) { - if(sound) { + if(sound.size()) { sound::play_ui(resource::load<SoundEffect>(sound), looping, pitch); } else { - sound::play_ui(static_cast<const char*>(nullptr), looping, pitch); + sound::play_ui(static_cast<resource_ptr<SoundEffect>>(nullptr), looping, pitch); } } @@ -203,4 +203,4 @@ void sound::play_ui(resource_ptr<SoundEffect> sound, bool looping, float pitch) alSourcef(ui_source, AL_PITCH, math::clamp(pitch, MIN_PITCH, MAX_PITCH)); alSourcePlay(ui_source); } -}
\ No newline at end of file +} diff --git a/game/client/sound/sound.hh b/game/client/sound/sound.hh index 5eb3ed7..877c64a 100644 --- a/game/client/sound/sound.hh +++ b/game/client/sound/sound.hh @@ -30,10 +30,10 @@ void update(void); namespace sound { -void play_generic(const char* sound, bool looping, float pitch); -void play_entity(entt::entity entity, const char* sound, bool looping, float pitch); -void play_player(const char* sound, bool looping, float pitch); -void play_ui(const char* sound, bool looping, float pitch); +void play_generic(std::string_view sound, bool looping, float pitch); +void play_entity(entt::entity entity, std::string_view sound, bool looping, float pitch); +void play_player(std::string_view sound, bool looping, float pitch); +void play_ui(std::string_view sound, bool looping, float pitch); } // namespace sound namespace sound diff --git a/game/client/toggles.cc b/game/client/toggles.cc index 96c4ae4..e6ffc26 100644 --- a/game/client/toggles.cc +++ b/game/client/toggles.cc @@ -13,7 +13,7 @@ #include "client/globals.hh" struct ToggleInfo final { - const char* description; + std::string_view description; int glfw_keycode; bool is_enabled; }; @@ -24,7 +24,7 @@ static ToggleInfo toggle_infos[TOGGLE_COUNT]; static void print_toggle_state(const ToggleInfo& info) { - if(info.description) { + if(info.description.size()) { if(info.is_enabled) { gui::client_chat::print(std::format("[toggles] {} ON", info.description)); } @@ -102,7 +102,7 @@ void toggles::init(void) toggle_infos[TOGGLE_CHUNK_AABB].glfw_keycode = GLFW_KEY_G; toggle_infos[TOGGLE_CHUNK_AABB].is_enabled = false; - toggle_infos[TOGGLE_METRICS_UI].description = nullptr; + toggle_infos[TOGGLE_METRICS_UI].description = std::string_view(); toggle_infos[TOGGLE_METRICS_UI].glfw_keycode = GLFW_KEY_V; toggle_infos[TOGGLE_METRICS_UI].is_enabled = false; @@ -114,10 +114,6 @@ void toggles::init(void) toggle_infos[TOGGLE_PM_FLIGHT].glfw_keycode = GLFW_KEY_F; toggle_infos[TOGGLE_PM_FLIGHT].is_enabled = false; -#ifndef NDEBUG - toggle_infos[TOGGLE_WIREFRAME].is_enabled = true; -#endif - globals::dispatcher.sink<io::GlfwKeyEvent>().connect<&on_glfw_key>(); } diff --git a/game/client/world/voxel_sounds.cc b/game/client/world/voxel_sounds.cc index 235a851..71ea1fc 100644 --- a/game/client/world/voxel_sounds.cc +++ b/game/client/world/voxel_sounds.cc @@ -9,7 +9,7 @@ constexpr static std::size_t NUM_SURFACES = static_cast<std::size_t>(world::voxe static std::vector<resource_ptr<SoundEffect>> footsteps_sounds[NUM_SURFACES]; static std::mt19937_64 randomizer; -static void add_footsteps_effect(world::voxel_surface surface, const char* name) +static void add_footsteps_effect(world::voxel_surface surface, std::string_view name) { if(auto effect = resource::load<SoundEffect>(name)) { auto surface_index = static_cast<std::size_t>(surface); diff --git a/game/server/chat.cc b/game/server/chat.cc index b7826b0..a0ceba8 100644 --- a/game/server/chat.cc +++ b/game/server/chat.cc @@ -23,33 +23,33 @@ void server_chat::init(void) globals::dispatcher.sink<protocol::ChatMessage>().connect<&on_chat_message_packet>(); } -void server_chat::broadcast(const char* message) +void server_chat::broadcast(std::string_view message) { server_chat::broadcast(message, "server"); } -void server_chat::broadcast(const char* message, const char* sender) +void server_chat::broadcast(std::string_view message, std::string_view sender) { protocol::ChatMessage packet; packet.type = protocol::ChatMessage::TEXT_MESSAGE; - packet.message = std::string(message); - packet.sender = std::string(sender); + packet.message = message; + packet.sender = sender; protocol::broadcast(globals::server_host, protocol::encode(packet)); spdlog::info("<{}> {}", sender, message); } -void server_chat::send(Session* session, const char* message) +void server_chat::send(Session* session, std::string_view message) { server_chat::send(session, message, "server"); } -void server_chat::send(Session* session, const char* message, const char* sender) +void server_chat::send(Session* session, std::string_view message, std::string_view sender) { protocol::ChatMessage packet; packet.type = protocol::ChatMessage::TEXT_MESSAGE; - packet.message = std::string(message); - packet.sender = std::string(sender); + packet.message = message; + packet.sender = sender; protocol::broadcast(globals::server_host, protocol::encode(packet)); } diff --git a/game/server/chat.hh b/game/server/chat.hh index 524ea78..fdf1a75 100644 --- a/game/server/chat.hh +++ b/game/server/chat.hh @@ -7,10 +7,10 @@ struct Session; namespace server_chat { void init(void); -void broadcast(const char* message); -void broadcast(const char* message, const char* sender); -void send(Session* session, const char* message); -void send(Session* session, const char* message, const char* sender); +void broadcast(std::string_view message); +void broadcast(std::string_view message, std::string_view sender); +void send(Session* session, std::string_view message); +void send(Session* session, std::string_view message, std::string_view sender); } // namespace server_chat #endif // SERVER_CHAT_HH diff --git a/game/server/game.cc b/game/server/game.cc index 231c87b..de9af9c 100644 --- a/game/server/game.cc +++ b/game/server/game.cc @@ -69,7 +69,7 @@ void server_game::init(void) void server_game::init_late(void) { - server_game::password_hash = math::crc64(password_string.get()); + server_game::password_hash = math::crc64(password_string.get_value()); sessions::init_late(); diff --git a/game/server/main.cc b/game/server/main.cc index fd940e2..deb0f97 100644 --- a/game/server/main.cc +++ b/game/server/main.cc @@ -32,7 +32,7 @@ int main(int argc, char** argv) shared_game::init(argc, argv); - spdlog::info("Voxelius Server {}", project_version_string); + spdlog::info("Voxelius Server {}", version::semver); globals::fixed_frametime = 0.0f; globals::fixed_frametime_avg = 0.0f; diff --git a/game/server/sessions.cc b/game/server/sessions.cc index 7ca0d7c..df74e01 100644 --- a/game/server/sessions.cc +++ b/game/server/sessions.cc @@ -61,14 +61,14 @@ static void on_login_request_packet(const protocol::LoginRequest& packet) // FIXME: calculate voxel registry checksum ahead of time // instead of figuring it out every time a new player connects - if(packet.voxel_registry_checksum != world::voxel_registry::calcualte_checksum()) { + if(packet.voxel_registry_checksum != world::voxel_registry::calculate_checksum()) { protocol::Disconnect response; response.reason = "protocol.voxel_registry_checksum"; protocol::send(packet.peer, protocol::encode(response)); return; } - if(packet.item_registry_checksum != world::item_registry::calcualte_checksum()) { + if(packet.item_registry_checksum != world::item_registry::calculate_checksum()) { protocol::Disconnect response; response.reason = "protocol.item_registry_checksum"; protocol::send(packet.peer, protocol::encode(response)); @@ -297,11 +297,11 @@ void sessions::shutdown(void) dimension_listeners.clear(); } -Session* sessions::create(ENetPeer* peer, const char* client_username) +Session* sessions::create(ENetPeer* peer, std::string_view client_username) { for(unsigned int i = 0U; i < sessions::max_players.get_value(); ++i) { if(!sessions_vector[i].peer) { - std::uint64_t client_identity = math::crc64(client_username); + std::uint64_t client_identity = math::crc64(client_username.data(), client_username.size()); sessions_vector[i].client_index = i; sessions_vector[i].client_identity = client_identity; @@ -309,7 +309,7 @@ Session* sessions::create(ENetPeer* peer, const char* client_username) sessions_vector[i].player_entity = entt::null; sessions_vector[i].peer = peer; - username_map[client_username] = &sessions_vector[i]; + username_map[std::string(client_username)] = &sessions_vector[i]; identity_map[client_identity] = &sessions_vector[i]; peer->data = &sessions_vector[i]; @@ -323,9 +323,9 @@ Session* sessions::create(ENetPeer* peer, const char* client_username) return nullptr; } -Session* sessions::find(const char* client_username) +Session* sessions::find(std::string_view client_username) { - const auto it = username_map.find(client_username); + const auto it = username_map.find(std::string(client_username)); if(it != username_map.cend()) { return it->second; } diff --git a/game/server/sessions.hh b/game/server/sessions.hh index ee5b22e..e9ce989 100644 --- a/game/server/sessions.hh +++ b/game/server/sessions.hh @@ -37,8 +37,8 @@ void shutdown(void); namespace sessions { -Session* create(ENetPeer* peer, const char* client_username); -Session* find(const char* client_username); +Session* create(ENetPeer* peer, std::string_view client_username); +Session* find(std::string_view client_username); Session* find(std::uint16_t client_index); Session* find(std::uint64_t client_identity); Session* find(ENetPeer* peer); diff --git a/game/server/whitelist.cc b/game/server/whitelist.cc index 8a858c6..4e53ee8 100644 --- a/game/server/whitelist.cc +++ b/game/server/whitelist.cc @@ -11,7 +11,7 @@ #include "server/game.hh" #include "server/globals.hh" -constexpr static const char* DEFAULT_FILENAME = "whitelist.txt"; +constexpr static std::string_view DEFAULT_FILENAME = "whitelist.txt"; constexpr static char SEPARATOR_CHAR = ':'; config::Boolean whitelist::enabled(false); @@ -36,12 +36,12 @@ void whitelist::init_late(void) return; } - if(utils::is_whitespace(whitelist::filename.get())) { + if(utils::is_whitespace(whitelist::filename.get_value())) { spdlog::warn("whitelist: enabled but filename is empty, using default ({})", DEFAULT_FILENAME); whitelist::filename.set(DEFAULT_FILENAME); } - PHYSFS_File* file = PHYSFS_openRead(whitelist::filename.get()); + PHYSFS_File* file = PHYSFS_openRead(whitelist::filename.c_str()); if(file == nullptr) { spdlog::warn("whitelist: {}: {}", whitelist::filename.get(), PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); @@ -80,14 +80,14 @@ void whitelist::shutdown(void) // UNDONE: implement saving } -bool whitelist::contains(const char* username) +bool whitelist::contains(std::string_view username) { - return whitelist_map.contains(username); + return whitelist_map.contains(std::string(username)); } -bool whitelist::matches(const char* username, std::uint64_t password_hash) +bool whitelist::matches(std::string_view username, std::uint64_t password_hash) { - const auto it = whitelist_map.find(username); + const auto it = whitelist_map.find(std::string(username)); if(it == whitelist_map.cend()) { // Not whitelisted, no match diff --git a/game/server/whitelist.hh b/game/server/whitelist.hh index 8f909b9..b84a34c 100644 --- a/game/server/whitelist.hh +++ b/game/server/whitelist.hh @@ -23,8 +23,8 @@ void shutdown(void); namespace whitelist { -bool contains(const char* username); -bool matches(const char* username, std::uint64_t password_hash); +bool contains(std::string_view username); +bool matches(std::string_view username, std::uint64_t password_hash); } // namespace whitelist #endif // SERVER_WHITELIST_HH diff --git a/game/server/world/overworld.cc b/game/server/world/overworld.cc index ff0b6f7..fca3edf 100644 --- a/game/server/world/overworld.cc +++ b/game/server/world/overworld.cc @@ -70,7 +70,7 @@ static void compute_tree_feature(unsigned int height, world::Feature& feature, v } } -world::Overworld::Overworld(const char* name) : Dimension(name, -30.0f) +world::Overworld::Overworld(std::string_view name) : Dimension(name, -30.0f) { m_bottommost_chunk.set_limits(-64, -4); m_terrain_variation.set_limits(16, 256); diff --git a/game/server/world/overworld.hh b/game/server/world/overworld.hh index 4141105..3da0401 100644 --- a/game/server/world/overworld.hh +++ b/game/server/world/overworld.hh @@ -25,7 +25,7 @@ namespace world { class Overworld final : public Dimension { public: - explicit Overworld(const char* name); + explicit Overworld(std::string_view name); virtual ~Overworld(void) = default; public: diff --git a/game/server/world/universe.cc b/game/server/world/universe.cc index 9b031ca..fe840da 100644 --- a/game/server/world/universe.cc +++ b/game/server/world/universe.cc @@ -41,7 +41,7 @@ static std::string make_chunk_filename(const DimensionMetadata* metadata, const static void add_new_dimension(world::Dimension* dimension) { - if(globals::dimensions.count(dimension->get_name())) { + if(globals::dimensions.count(std::string(dimension->get_name()))) { spdlog::critical("universe: dimension named {} already exists", dimension->get_name()); std::terminate(); } @@ -62,7 +62,7 @@ static void add_new_dimension(world::Dimension* dimension) std::terminate(); } - globals::dimensions.insert_or_assign(dimension->get_name(), dimension); + globals::dimensions.insert_or_assign(std::string(dimension->get_name()), dimension); auto& mapped_metadata = metadata_map.insert_or_assign(dimension, metadata).first->second; @@ -122,7 +122,7 @@ void world::universe::init_late(void) std::terminate(); } - auto spawn_dimension = globals::dimensions.find(universe_spawn_dimension.get()); + auto spawn_dimension = globals::dimensions.find(universe_spawn_dimension.get_value()); if(spawn_dimension == globals::dimensions.cend()) { spdlog::critical("universe: {} is not a valid dimension name", universe_spawn_dimension.get()); 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 |
