Skip to content

Commit

Permalink
[core] add: config.hpp
Browse files Browse the repository at this point in the history
This will be the point of parameterizing the types of the entire library.
  • Loading branch information
jd28 committed Oct 1, 2024
1 parent 6d985eb commit 85d5a1f
Show file tree
Hide file tree
Showing 141 changed files with 885 additions and 851 deletions.
20 changes: 20 additions & 0 deletions lib/nw/config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

// This file is inspired by RmlUI config header. The goal is to have a file that can parameterize
// the library by a set types.

#ifdef ROLLNW_CUSTOM_CONFIGURATION_FILE
#include ROLLNW_CUSTOM_CONFIGURATION_FILE
#else

#include <string>

namespace nw {

// Strings.
using String = std::string;
using StringView = std::string_view;

} // namespace nw

#endif
36 changes: 18 additions & 18 deletions lib/nw/formats/Dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ DialogPtr* DialogPtr::add_ptr(DialogPtr* ptr, bool is_link)
}
}

DialogPtr* DialogPtr::add_string(std::string value, nw::LanguageID lang, bool feminine)
DialogPtr* DialogPtr::add_string(String value, nw::LanguageID lang, bool feminine)
{
auto ptr = parent->create_ptr();
ptr->type = (type == DialogNodeType::entry) ? DialogNodeType::reply : DialogNodeType::entry;
Expand Down Expand Up @@ -83,7 +83,7 @@ DialogNode* DialogNode::copy() const
}

/// Gets a condition parameter if it exists
std::optional<std::string> DialogPtr::get_condition_param(const std::string& key)
std::optional<String> DialogPtr::get_condition_param(const String& key)
{
for (auto& it : condition_params) {
if (it.first == key) {
Expand All @@ -104,10 +104,10 @@ void DialogPtr::get_all_subnodes(std::vector<DialogNode*>& subnodes)
}

/// Removes condition parameter by key
void DialogPtr::remove_condition_param(const std::string& key)
void DialogPtr::remove_condition_param(const String& key)
{
condition_params.erase(std::remove_if(std::begin(condition_params), std::end(condition_params),
[&key](const std::pair<std::string, std::string>& p) {
[&key](const std::pair<String, String>& p) {
return p.first == key;
}),
std::end(condition_params));
Expand All @@ -120,7 +120,7 @@ void DialogPtr::remove_condition_param(size_t index)
}

/// Sets condition parameter, if key does not exist key and value are appended
void DialogPtr::set_condition_param(const std::string& key, const std::string& value)
void DialogPtr::set_condition_param(const String& key, const String& value)
{
for (auto& it : condition_params) {
if (it.first == key) {
Expand All @@ -131,7 +131,7 @@ void DialogPtr::set_condition_param(const std::string& key, const std::string& v
condition_params.emplace_back(key, value);
}

std::optional<std::string> DialogNode::get_action_param(const std::string& key)
std::optional<String> DialogNode::get_action_param(const String& key)
{
for (auto& it : action_params) {
if (it.first == key) {
Expand All @@ -142,10 +142,10 @@ std::optional<std::string> DialogNode::get_action_param(const std::string& key)
}

/// Removes action parameter by key
void DialogNode::remove_action_param(const std::string& key)
void DialogNode::remove_action_param(const String& key)
{
action_params.erase(std::remove_if(std::begin(action_params), std::end(action_params),
[&key](const std::pair<std::string, std::string>& p) {
[&key](const std::pair<String, String>& p) {
return p.first == key;
}),
std::end(action_params));
Expand All @@ -158,7 +158,7 @@ void DialogNode::remove_action_param(size_t index)
}

/// Sets action parameter, if key does not exist key and value are appended
void DialogNode::set_action_param(const std::string& key, const std::string& value)
void DialogNode::set_action_param(const String& key, const String& value)
{
for (auto& it : action_params) {
if (it.first == key) {
Expand Down Expand Up @@ -222,7 +222,7 @@ DialogPtr* Dialog::add_ptr(DialogPtr* ptr, bool is_link)
}
}

DialogPtr* Dialog::add_string(std::string value, nw::LanguageID lang, bool feminine)
DialogPtr* Dialog::add_string(String value, nw::LanguageID lang, bool feminine)
{
auto ptr = create_ptr();
ptr->type = DialogNodeType::entry;
Expand Down Expand Up @@ -362,8 +362,8 @@ void Dialog::remove_node_internal(DialogNode* node, DialogNodeType type)

bool Dialog::read_nodes(const GffStruct gff, DialogNodeType node_type)
{
std::string_view node_list;
std::string_view ptr_list;
StringView node_list;
StringView ptr_list;
std::vector<DialogNode*>* holder;

if (node_type == DialogNodeType::entry) {
Expand Down Expand Up @@ -401,7 +401,7 @@ bool Dialog::read_nodes(const GffStruct gff, DialogNodeType node_type)
size_t num_action_params = s["ActionParams"].size();
for (size_t j = 0; j < num_action_params; ++j) {
GffStruct p = s["ActionParams"][j];
std::string key, value;
String key, value;
p.get_to("Key", key);
p.get_to("Value", value);
node->action_params.emplace_back(key, value);
Expand Down Expand Up @@ -432,7 +432,7 @@ bool Dialog::read_nodes(const GffStruct gff, DialogNodeType node_type)
size_t num_params = p["ConditionParams"].size();
for (size_t h = 0; h < num_params; ++h) {
GffStruct cp = p["ConditionParams"][h];
std::string key, value;
String key, value;
cp.get_to("Key", key);
cp.get_to("Value", value);
ptr->condition_params.emplace_back(key, value);
Expand Down Expand Up @@ -483,7 +483,7 @@ bool Dialog::load(const GffStruct gff)
size_t num_params = s["ConditionParams"].size();
for (size_t h = 0; h < num_params; ++h) {
GffStruct cp = s["ConditionParams"][h];
std::string key, value;
String key, value;
cp.get_to("Key", key);
cp.get_to("Value", value);
ptr->condition_params.emplace_back(key, value);
Expand Down Expand Up @@ -528,8 +528,8 @@ GffBuilder serialize(const Dialog* obj)

DialogNodeType node_type = DialogNodeType::reply;
const std::vector<DialogNode*>* holder = &obj->entries;
std::string_view node_list = "EntryList";
std::string_view ptr_list = "RepliesList";
StringView node_list = "EntryList";
StringView ptr_list = "RepliesList";

auto& entry_list = gff.top.add_list(node_list);
for (size_t i = 0; i < holder->size(); ++i) {
Expand Down Expand Up @@ -728,7 +728,7 @@ void serialize(nlohmann::json& archive, const DialogNode& node)

void deserialize(const nlohmann::json& archive, Dialog& node)
{
if (archive["$type"].get<std::string>() != "DLG") {
if (archive["$type"].get<String>() != "DLG") {
LOG_F(ERROR, "invalid dlg json");
return;
}
Expand Down
28 changes: 14 additions & 14 deletions lib/nw/formats/Dialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ struct DialogPtr {
DialogNode* node = nullptr;

Resref script_appears;
std::vector<std::pair<std::string, std::string>> condition_params;
std::vector<std::pair<String, String>> condition_params;

bool is_start = false;
bool is_link = false;
std::string comment;
String comment;

/// Adds Dialog Pointer, if `is_link` is false no new pointer or node is created.
/// if `is_link` is true a new pointer will created with the node copied from input pointer.
DialogPtr* add_ptr(DialogPtr* ptr, bool is_link = false);

/// Adds Dialog Pointer and Node with string value set
DialogPtr* add_string(std::string value, nw::LanguageID lang = nw::LanguageID::english, bool feminine = false);
DialogPtr* add_string(String value, nw::LanguageID lang = nw::LanguageID::english, bool feminine = false);

/// Adds empty Dialog Pointer and Node
DialogPtr* add();
Expand All @@ -76,10 +76,10 @@ struct DialogPtr {
void get_all_subnodes(std::vector<DialogNode*>& subnodes);

/// Gets a condition parameter if it exists
std::optional<std::string> get_condition_param(const std::string& key);
std::optional<String> get_condition_param(const String& key);

/// Removes condition parameter by key
void remove_condition_param(const std::string& key);
void remove_condition_param(const String& key);

/// Removes condition parameter by index
void remove_condition_param(size_t index);
Expand All @@ -88,7 +88,7 @@ struct DialogPtr {
void remove_ptr(DialogPtr* ptr);

/// Sets condition parameter, if key does not exist key and value are appended
void set_condition_param(const std::string& key, const std::string& value);
void set_condition_param(const String& key, const String& value);
};

struct DialogNode {
Expand All @@ -98,9 +98,9 @@ struct DialogNode {
Dialog* parent = nullptr;
DialogNodeType type;

std::string comment;
std::string quest;
std::string speaker;
String comment;
String quest;
String speaker;
uint32_t quest_entry = std::numeric_limits<uint32_t>::max();
Resref script_action;
Resref sound;
Expand All @@ -110,22 +110,22 @@ struct DialogNode {
uint32_t delay = std::numeric_limits<uint32_t>::max();

std::vector<DialogPtr*> pointers;
std::vector<std::pair<std::string, std::string>> action_params;
std::vector<std::pair<String, String>> action_params;

/// Copies a Node
DialogNode* copy() const;

/// Gets action parameter if it exists
std::optional<std::string> get_action_param(const std::string& key);
std::optional<String> get_action_param(const String& key);

/// Removes action parameter by key
void remove_action_param(const std::string& key);
void remove_action_param(const String& key);

/// Removes action parameter by index
void remove_action_param(size_t index);

/// Sets action parameter, if key does not exist key and value are appended
void set_action_param(const std::string& key, const std::string& value);
void set_action_param(const String& key, const String& value);
};

struct Dialog {
Expand All @@ -151,7 +151,7 @@ struct Dialog {
DialogPtr* add_ptr(DialogPtr* ptr, bool is_link = false);

/// Adds Dialog Pointer and Node with string value set
DialogPtr* add_string(std::string value, nw::LanguageID lang = nw::LanguageID::english, bool feminine = false);
DialogPtr* add_string(String value, nw::LanguageID lang = nw::LanguageID::english, bool feminine = false);

/// Creates a new Dialog Node.
DialogNode* create_node(DialogNodeType type);
Expand Down
2 changes: 1 addition & 1 deletion lib/nw/formats/Faction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace nw {

struct FactionInfo {
std::string name;
String name;
uint32_t parent = std::numeric_limits<uint32_t>::max();
uint16_t global = 0;
};
Expand Down
4 changes: 2 additions & 2 deletions lib/nw/formats/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ bool Image::write_to(const std::filesystem::path& filename) const
if (!bytes_) return false;

fs::path temp = fs::temp_directory_path() / filename.filename();
std::string ext = filename.extension().string();
std::string temp_path = path_to_string(temp);
String ext = filename.extension().string();
String temp_path = path_to_string(temp);

int width = static_cast<int>(width_);
int height = static_cast<int>(height_);
Expand Down
4 changes: 2 additions & 2 deletions lib/nw/formats/Ini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Ini::Ini(ResourceData data)
loaded_ = parse();
}

bool Ini::get_to(std::string key, std::string& out) const
bool Ini::get_to(String key, String& out) const
{
string::tolower(&key);
auto it = map_.find(key);
Expand Down Expand Up @@ -51,7 +51,7 @@ int Ini::parse_ini(void* user, const char* section, const char* name, const char
if (!name) // Happens when INI_CALL_HANDLER_ON_NEW_SECTION enabled
return 1;
Ini* reader = static_cast<Ini*>(user);
std::string key = std::string(section) + "/" + name;
String key = String(section) + "/" + name;
string::tolower(&key);

reader->map_[key] = value ? value : "";
Expand Down
16 changes: 8 additions & 8 deletions lib/nw/formats/Ini.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,36 @@ struct Ini {
/**
* @brief Gets a value
*
* @tparam T `int32_t`, `float`, or `std::string`
* @tparam T `int32_t`, `float`, or `String`
* @param key
* @return std::optional<T>
*/
template <typename T>
std::optional<T> get(std::string key) const;
std::optional<T> get(String key) const;

/// Gets string value
bool get_to(std::string key, std::string& out) const;
bool get_to(String key, String& out) const;

/// Gets numeric value
template <typename T>
bool get_to(std::string key, T& out) const;
bool get_to(String key, T& out) const;

/// Deterimes if Ini file was successfully parsed
bool valid() const noexcept;

private:
ResourceData data_;
absl::flat_hash_map<std::string, std::string> map_;
absl::flat_hash_map<String, String> map_;
bool loaded_ = false;

static int parse_ini(void* user, const char* section, const char* name, const char* value);
bool parse();
};

template <typename T>
std::optional<T> Ini::get(std::string key) const
std::optional<T> Ini::get(String key) const
{
std::string val;
String val;
if (!get_to(std::move(key), val))
return {};

Expand All @@ -69,7 +69,7 @@ std::optional<T> Ini::get(std::string key) const
}

template <typename T>
bool Ini::get_to(std::string key, T& out) const
bool Ini::get_to(String key, T& out) const
{
static_assert(std::is_arithmetic_v<T>, "[ini] only numeric types are allowed");
string::tolower(&key);
Expand Down
4 changes: 2 additions & 2 deletions lib/nw/formats/Journal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ struct JournalEntry {
};

struct JournalCategory {
std::string comment;
String comment;
std::vector<JournalEntry> entries;
LocString name;
std::string tag;
String tag;
uint32_t priority;
uint32_t xp;
uint16_t picture;
Expand Down
4 changes: 2 additions & 2 deletions lib/nw/formats/Palette.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct PaletteTreeNode {

// Every node has a name or a strref present, if both are, strref wins,
// but both are retained.
std::string name;
String name;
uint32_t strref = std::numeric_limits<uint32_t>::max();

// Extra data dependent on palette resource type,
Expand All @@ -42,7 +42,7 @@ struct PaletteTreeNode {

// Only if restype == ResourceType::utc
float cr = 0.0;
std::string faction;
String faction;

std::vector<PaletteTreeNode> children;
};
Expand Down
6 changes: 4 additions & 2 deletions lib/nw/formats/Tileset.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "../config.hpp"

#include <limits>
#include <stdint.h>
#include <string>
Expand All @@ -8,13 +10,13 @@
namespace nw {

struct Tile {
std::string model;
String model;
};

/// Abstraction of the SET tileset file.
struct Tileset {
uint32_t strref = std::numeric_limits<uint32_t>::max();
std::string name;
String name;
std::vector<Tile> tiles;
float tile_height = 5.0;
};
Expand Down
Loading

0 comments on commit 85d5a1f

Please sign in to comment.