Horizon Official Technical Documentation
Horizon::Zone::StorageDatabase Class Reference

#include <StorageDB.hpp>

+ Collaboration diagram for Horizon::Zone::StorageDatabase:

Public Member Functions

 StorageDatabase ()
 
 ~StorageDatabase ()
 
bool load ()
 
std::shared_ptr< const storage_config_dataget_storage_by_id (int32_t id)
 
int32_t get_storage_db_size ()
 
std::map< uint32_t, std::shared_ptr< const storage_config_data > > const get_storage_db () const
 

Static Public Member Functions

static StorageDatabaseget_instance ()
 

Protected Member Functions

bool load_storage (sol::table const &storage_tbl)
 
bool load_internal (sol::object const &key, sol::object const &value)
 

Private Attributes

LockedLookupTable< uint32_t, std::shared_ptr< const storage_config_data > > _storage_db
 

Constructor & Destructor Documentation

◆ StorageDatabase()

StorageDatabase::StorageDatabase ( )
36{
37}

◆ ~StorageDatabase()

StorageDatabase::~StorageDatabase ( )
40{
41}

Member Function Documentation

◆ get_instance()

static StorageDatabase * Horizon::Zone::StorageDatabase::get_instance ( )
inlinestatic
56 {
57 static StorageDatabase instance;
58 return &instance;
59 }
StorageDatabase()
Definition: StorageDB.cpp:35

◆ get_storage_by_id()

std::shared_ptr< const storage_config_data > Horizon::Zone::StorageDatabase::get_storage_by_id ( int32_t  id)
inline
68{ return _storage_db.at(id); }
LockedLookupTable< uint32_t, std::shared_ptr< const storage_config_data > > _storage_db
Definition: StorageDB.hpp:74

References _storage_db.

◆ get_storage_db()

std::map< uint32_t, std::shared_ptr< const storage_config_data > > const Horizon::Zone::StorageDatabase::get_storage_db ( ) const
inline
71{ return _storage_db.get_map(); }

References _storage_db.

◆ get_storage_db_size()

int32_t Horizon::Zone::StorageDatabase::get_storage_db_size ( )
inline
69{ return _storage_db.size(); }

References _storage_db.

◆ load()

bool StorageDatabase::load ( )
44{
45 sol::state lua;
46
47 int total_entries = 0;
48 std::string tmp_string;
49 std::string file_path = sZone->config().get_static_db_path().string() + "storage_db.lua";
50
51 // Read the file. If there is an error, report it and exit.
52 try {
53 lua.script_file(file_path);
54 sol::table storage_tbl = lua.get<sol::table>("storage_db");
55 total_entries = load_storage(storage_tbl);
56 HLog(info) << "Loaded " << total_entries << " entries from '" << file_path << "'";
57 } catch(const std::exception &e) {
58 HLog(error) << "StorageDB::load: " << e.what();
59 return false;
60 }
61
62 return true;
63}
#define HLog(type)
Definition: Logger.hpp:122
#define sZone
Definition: Zone.hpp:247
bool load_storage(sol::table const &storage_tbl)
Definition: StorageDB.cpp:65

References HLog, load_storage(), and sZone.

+ Here is the call graph for this function:

◆ load_internal()

bool StorageDatabase::load_internal ( sol::object const &  key,
sol::object const &  value 
)
protected
77{
79
80 if (key.is<int>()) {
81 config.storage_id = key.as<int>();
82 } else {
83 HLog(error) << "StorageDB::load_internal: Invalid storage id '" << key.as<std::string>() << "', key must be numeric.";
84 return false;
85 }
86
87 if (!value.is<sol::table>()) {
88 HLog(error) << "StorageDB::load_internal: Invalid value type for storage id '" << config.storage_id << "', value must be a table.";
89 return false;
90 }
91
92 sol::table storage_tbl = value.as<sol::table>();
93 config.name = storage_tbl.get_or<std::string>("Name", "Storage");
94 config.capacity = storage_tbl.get_or("Capacity", 600);
95
96 _storage_db.insert(config.storage_id, std::make_shared<const storage_config_data>(config));
97
98 return true;
99}
Definition: StorageDB.hpp:44
std::string name
Definition: StorageDB.hpp:46
int32_t storage_id
Definition: StorageDB.hpp:45
int32_t capacity
Definition: StorageDB.hpp:47

References _storage_db, Horizon::Zone::storage_config_data::capacity, HLog, Horizon::Zone::storage_config_data::name, and Horizon::Zone::storage_config_data::storage_id.

Referenced by load_storage().

+ Here is the caller graph for this function:

◆ load_storage()

bool StorageDatabase::load_storage ( sol::table const &  storage_tbl)
protected
66{
67 int total_entries = 0;
68 for (auto &kv : storage_tbl) {
69 if (load_internal(kv.first, kv.second)) {
70 total_entries++;
71 }
72 }
73 return total_entries > 0;
74}
bool load_internal(sol::object const &key, sol::object const &value)
Definition: StorageDB.cpp:76

References load_internal().

Referenced by load().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ _storage_db

LockedLookupTable<uint32_t, std::shared_ptr<const storage_config_data> > Horizon::Zone::StorageDatabase::_storage_db
private

The documentation for this class was generated from the following files: