Skip to content

Commit

Permalink
Convert settings and string map to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
cjhoward committed Dec 22, 2023
1 parent f53913c commit 08f6cd1
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/engine/input/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void keyboard::input_text(const std::string& text)
{
m_text_input_publisher.publish({this, text});
}

void keyboard::edit_text(const std::string& text, std::size_t position, std::size_t length)
{
m_text_edit_publisher.publish({this, text, position, length});
Expand Down
15 changes: 13 additions & 2 deletions src/engine/utility/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@
#include <engine/utility/json.hpp>
#include <engine/resources/resource-loader.hpp>
#include <engine/resources/deserializer.hpp>
#include <engine/resources/serializer.hpp>

template <>
void deserializer<json>::deserialize(::json& json, deserialize_context& ctx)
void serializer<json>::serialize(const json& element, serialize_context& ctx)
{
// Dump JSON to string
auto dump = element.dump(1, '\t', false);

// Write string
ctx.write8(reinterpret_cast<std::byte*>(dump.data()), dump.length());
}

template <>
void deserializer<json>::deserialize(json& element, deserialize_context& ctx)
{
// Read file into buffer
std::string file_buffer(ctx.size(), '\0');
ctx.read8(reinterpret_cast<std::byte*>(file_buffer.data()), ctx.size());

// Parse JSON from file buffer
json = ::json::parse(file_buffer, nullptr, true, true);
element = nlohmann::json::parse(file_buffer, nullptr, true, true);
}

template <>
Expand Down
2 changes: 1 addition & 1 deletion src/engine/utility/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <nlohmann/json.hpp>

/// JSON data.
/** JSON element. */
using json = nlohmann::json;

#endif // ANTKEEPER_UTILITY_JSON_HPP
4 changes: 2 additions & 2 deletions src/game/debug/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ namespace {
return 1;
}

if (auto i = ctx->string_map->find(hash::fnv1a32<char>(arguments[1])); i != ctx->string_map->end())
if (auto it = ctx->string_map->find(arguments[1]); it != ctx->string_map->end())
{
cout << i->second << '\n';
cout << it->get_ref<const std::string&>() << '\n';
return 0;
}

Expand Down
12 changes: 6 additions & 6 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ game::~game()

// Save settings
resource_manager->set_write_path(shared_config_path);
resource_manager->save(*settings, "settings.cfg");
resource_manager->save(*settings, "settings.json");

// Destruct input and window managers
input_manager.reset();
Expand Down Expand Up @@ -347,18 +347,18 @@ void game::load_settings()
if (option_reset)
{
// Command-line reset option found, reset settings
settings = std::make_shared<dict<hash::fnv1a32_t>>();
settings = std::make_shared<json>();
resource_manager->set_write_path(shared_config_path);
resource_manager->save(*settings, "settings.cfg");
resource_manager->save(*settings, "settings.json");
debug::log_info("Settings reset");
}
else
{
settings = resource_manager->load<dict<hash::fnv1a32_t>>("settings.cfg");
settings = resource_manager->load<json>("settings.json");
if (!settings)
{
debug::log_info("Settings not found");
settings = std::make_shared<dict<hash::fnv1a32_t>>();
settings = std::make_shared<json>();
}
}
}
Expand Down Expand Up @@ -625,7 +625,7 @@ void game::load_language()
languages = resource_manager->load<json>("localization/languages.json");

// Load language string map
string_map = resource_manager->load<i18n::string_map>(std::format("localization/strings.{}.json", language_tag));
string_map = resource_manager->load<json>(std::format("localization/strings.{}.json", language_tag));

// Change window title
const std::string window_title = get_string(*this, "window_title");
Expand Down
5 changes: 2 additions & 3 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <engine/render/material.hpp>
#include <engine/type/font.hpp>
#include <engine/type/typeface.hpp>
#include <engine/utility/dict.hpp>
#include <engine/utility/json.hpp>
#include <engine/math/vector.hpp>
#include <engine/utility/state-machine.hpp>
Expand Down Expand Up @@ -142,7 +141,7 @@ class game
std::filesystem::path controls_path;

// Persistent settings
std::shared_ptr<dict<hash::fnv1a32_t>> settings;
std::shared_ptr<json> settings;

// Window management and window event handling
std::unique_ptr<app::window_manager> window_manager;
Expand All @@ -164,7 +163,7 @@ class game
// Localization and internationalization
std::shared_ptr<json> languages;
std::string language_tag;
std::shared_ptr<i18n::string_map> string_map;
std::shared_ptr<json> string_map;

// Fonts
std::unordered_map<hash::fnv1a32_t, std::shared_ptr<type::typeface>> typefaces;
Expand Down
2 changes: 1 addition & 1 deletion src/game/loaders/entity-archetype-loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ std::unique_ptr<entity::archetype> resource_loader<entity::archetype>::load(::re
std::unique_ptr<entity::archetype> archetype = std::make_unique<entity::archetype>();

// Load components from table rows
for (json::const_iterator element = json_data->cbegin(); element != json_data->cend(); ++element)
for (auto element = json_data->cbegin(); element != json_data->cend(); ++element)
{
if (!load_component(*archetype, resource_manager, element))
{
Expand Down
15 changes: 7 additions & 8 deletions src/game/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "game/game.hpp"
#include <engine/debug/log.hpp>

#include <string_view>

/**
* Reads a setting if found, inserts a setting if not found, and overwrites a setting if a type mismatch occurs.
Expand All @@ -20,24 +20,23 @@
* @return `true` if the setting was read, `false` if the setting was written.
*/
template <class T>
bool read_or_write_setting(::game& ctx, hash::fnv1a32_t key, T& value)
bool read_or_write_setting(::game& ctx, std::string_view key, T& value)
{
if (auto i = ctx.settings->find(key); i != ctx.settings->end())
if (auto it = ctx.settings->find(key); it != ctx.settings->end())
{
try
{
value = std::any_cast<T>(i->second);
it->get_to<T>(value);
}
catch (const std::bad_any_cast&)
catch (const std::exception& e)
{
debug::log_error("Setting type mismatch ({:x}={})", key.value, value);
i->second = value;
debug::log_error("Setting type mismatch ({}={}): {}", key, value, e.what());
return false;
}
}
else
{
debug::log_trace("Setting key not found ({:x}={})", key.value, value);
debug::log_trace("Setting key not found ({}={})", key, value);
(*ctx.settings)[key] = value;
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/game/states/gamepad-config-menu-state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ std::string gamepad_config_menu_state::get_mapping_string(const input::action_ma
return mapping_string;
}

void gamepad_config_menu_state::add_control_item(input::action_map& action_map, input::action& control, hash::fnv1a32_t control_name_hash)
void gamepad_config_menu_state::add_control_item(input::action_map& action_map, input::action& control, std::string_view control_name)
{
// Construct texts
auto name_text = std::make_unique<scene::text>();
Expand All @@ -280,7 +280,7 @@ void gamepad_config_menu_state::add_control_item(input::action_map& action_map,
ctx.menu_item_texts.push_back({name_text.get(), value_text.get()});

// Set control name and mapping texts
name_text->set_content(get_string(ctx, control_name_hash));
name_text->set_content(get_string(ctx, control_name));
value_text->set_content(get_mapping_string(action_map, control));

// Callback invoked when an input has been mapped to the control
Expand Down
2 changes: 1 addition & 1 deletion src/game/states/gamepad-config-menu-state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class gamepad_config_menu_state: public game_state

private:
std::string get_mapping_string(const input::action_map& action_map, const input::action& control);
void add_control_item(input::action_map& action_map, input::action& control, hash::fnv1a32_t control_name_hash);
void add_control_item(input::action_map& action_map, input::action& control, std::string_view control_name);

std::shared_ptr<event::subscription> gamepad_axis_mapped_subscription;
std::shared_ptr<event::subscription> gamepad_button_mapped_subscription;
Expand Down
6 changes: 3 additions & 3 deletions src/game/states/keyboard-config-menu-state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ std::string keyboard_config_menu_state::get_mapping_string(const input::action_m
std::string scancode_string_name = std::format("scancode_{:02x}", std::to_underlying(key_mapping.scancode));

// Set mapping string to scancode string
mapping_string = get_string(ctx, hash::fnv1a32<char>(scancode_string_name));
mapping_string = get_string(ctx, scancode_string_name);
}
else if (auto mouse_button_mappings = action_map.get_mouse_button_mappings(control); !mouse_button_mappings.empty())
{
Expand Down Expand Up @@ -192,7 +192,7 @@ std::string keyboard_config_menu_state::get_mapping_string(const input::action_m
return mapping_string;
}

void keyboard_config_menu_state::add_control_item(input::action_map& action_map, input::action& control, hash::fnv1a32_t control_name_hash)
void keyboard_config_menu_state::add_control_item(input::action_map& action_map, input::action& control, std::string_view control_name)
{
// Construct texts
auto name_text = std::make_unique<scene::text>();
Expand All @@ -202,7 +202,7 @@ void keyboard_config_menu_state::add_control_item(input::action_map& action_map,
ctx.menu_item_texts.push_back({name_text.get(), value_text.get()});

// Set control name and mapping texts
name_text->set_content(get_string(ctx, control_name_hash));
name_text->set_content(get_string(ctx, control_name));
value_text->set_content(get_mapping_string(action_map, control));

// Callback invoked when an input has been mapped to the control
Expand Down
2 changes: 1 addition & 1 deletion src/game/states/keyboard-config-menu-state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class keyboard_config_menu_state: public game_state

private:
std::string get_mapping_string(const input::action_map& action_map, const input::action& control);
void add_control_item(input::action_map& action_map, input::action& control, hash::fnv1a32_t control_name_hash);
void add_control_item(input::action_map& action_map, input::action& control, std::string_view control_name);

std::shared_ptr<event::subscription> key_mapped_subscription;
std::shared_ptr<event::subscription> mouse_button_mapped_subscription;
Expand Down
4 changes: 3 additions & 1 deletion src/game/states/language-menu-state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "game/fonts.hpp"
#include "game/menu.hpp"
#include "game/strings.hpp"
#include "game/debug/shell.hpp"
#include <engine/hash/fnv1a.hpp>
#include <engine/resources/resource-manager.hpp>
#include <algorithm>
Expand Down Expand Up @@ -54,10 +55,11 @@ language_menu_state::language_menu_state(::game& ctx):
ctx.language_tag = language_it.key();

// Load language strings
ctx.string_map = ctx.resource_manager->load<i18n::string_map>(std::format("localization/strings.{}.json", ctx.language_tag));
ctx.string_map = ctx.resource_manager->load<json>(std::format("localization/strings.{}.json", ctx.language_tag));

// Update language tag settings
(*ctx.settings)["language_tag"] = ctx.language_tag;
ctx.shell->set_variable("language", ctx.language_tag);

// Log language tag
debug::log_info("Language tag: {}", ctx.language_tag);
Expand Down
1 change: 0 additions & 1 deletion src/game/states/language-menu-state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class language_menu_state: public game_state
private:
void update_text_content();

std::shared_ptr<json> language_manifest;
json::const_iterator language_it;

std::unique_ptr<scene::text> language_name_text;
Expand Down
11 changes: 7 additions & 4 deletions src/game/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
#include "game/strings.hpp"
#include <format>

std::string get_string(const ::game& ctx, hash::fnv1a32_t key)
std::string get_string(const ::game& ctx, std::string_view key)
{
if (auto i = ctx.string_map->find(key); i != ctx.string_map->end())
if (auto it = ctx.string_map->find(key); it != ctx.string_map->end())
{
return i->second;
if (it->is_string())
{
return it->get<std::string>();
}
}

return std::format("${:x}", key.value);
return std::format("${}", key);
}
5 changes: 2 additions & 3 deletions src/game/strings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
#define ANTKEEPER_GAME_STRINGS_HPP

#include "game/game.hpp"
#include <engine/hash/fnv1a.hpp>
#include <cstdint>
#include <string>
#include <string_view>

/**
* Returns a localized string.
Expand All @@ -17,6 +16,6 @@
*
* @return String value.
*/
[[nodiscard]] std::string get_string(const ::game& ctx, hash::fnv1a32_t key);
[[nodiscard]] std::string get_string(const ::game& ctx, std::string_view key);

#endif // ANTKEEPER_GAME_STRINGS_HPP

0 comments on commit 08f6cd1

Please sign in to comment.