2021-12-05 17:54:36 +00:00
|
|
|
#include "CDBehaviorParameterTable.h"
|
|
|
|
#include "GeneralUtils.h"
|
|
|
|
|
2022-07-09 20:59:36 +00:00
|
|
|
#include "Game.h"
|
|
|
|
#include "dLogger.h"
|
|
|
|
#include "dServer.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-09 20:59:36 +00:00
|
|
|
#include <boost/interprocess/managed_shared_memory.hpp>
|
|
|
|
#include <boost/interprocess/containers/map.hpp>
|
|
|
|
#include <boost/interprocess/allocators/allocator.hpp>
|
|
|
|
#include <functional>
|
|
|
|
#include <utility>
|
|
|
|
#include <chrono>
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-09 20:59:36 +00:00
|
|
|
typedef size_t KeyType;
|
|
|
|
typedef float MappedType;
|
|
|
|
typedef std::pair<const KeyType, MappedType> ValueType;
|
|
|
|
|
|
|
|
typedef boost::interprocess::allocator<ValueType, boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
|
|
|
|
|
|
|
|
typedef boost::interprocess::map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> CDBehaviorParameterMap;
|
|
|
|
|
|
|
|
CDBehaviorParameterMap *table = nullptr;
|
|
|
|
|
2022-07-09 22:40:13 +00:00
|
|
|
boost::interprocess::managed_shared_memory segment;
|
|
|
|
ShmemAllocator* alloc_inst;
|
|
|
|
boost::interprocess::offset_ptr<CDBehaviorParameterMap> m;
|
2022-07-09 20:59:36 +00:00
|
|
|
|
|
|
|
//! Constructor
|
|
|
|
CDBehaviorParameterTable::CDBehaviorParameterTable(void) {
|
2022-07-09 22:40:13 +00:00
|
|
|
try {
|
|
|
|
segment = boost::interprocess::managed_shared_memory(boost::interprocess::open_read_only, "CDBehaviorParameterTable");
|
|
|
|
|
|
|
|
alloc_inst = new ShmemAllocator(segment.get_segment_manager());
|
|
|
|
|
|
|
|
m = segment.find<CDBehaviorParameterMap>("CDBehaviorParameter").first;
|
|
|
|
|
|
|
|
if (m == nullptr) {
|
|
|
|
Game::logger->Log("CDBehaviorParameterTable", "CDBehaviorParameter segment is nullptr!\n");
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-09 22:40:13 +00:00
|
|
|
// Load the entire table into m_Entries
|
|
|
|
/*for (auto it = m->begin(); it != m->end(); ++it) {
|
|
|
|
m_Entries.insert(std::make_pair(it->first, it->second));
|
|
|
|
}*/
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
// Not open, we are master
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Destructor
|
|
|
|
CDBehaviorParameterTable::~CDBehaviorParameterTable(void) { }
|
|
|
|
|
|
|
|
//! Returns the table's name
|
|
|
|
std::string CDBehaviorParameterTable::GetName(void) const {
|
2022-01-15 18:37:43 +00:00
|
|
|
return "BehaviorParameter";
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 20:59:36 +00:00
|
|
|
float CDBehaviorParameterTable::GetEntry(const uint32_t behaviorID, const std::string& name, const float defaultValue)
|
2021-12-05 17:54:36 +00:00
|
|
|
{
|
|
|
|
size_t hash = 0;
|
|
|
|
GeneralUtils::hash_combine(hash, behaviorID);
|
|
|
|
GeneralUtils::hash_combine(hash, name);
|
|
|
|
|
2022-01-24 22:02:56 +00:00
|
|
|
// Search for specific parameter
|
2022-07-09 20:59:36 +00:00
|
|
|
try {
|
2022-07-09 22:40:13 +00:00
|
|
|
/*boost::interprocess::managed_shared_memory segment(boost::interprocess::open_read_only, "CDBehaviorParameterTable");
|
2022-07-09 20:59:36 +00:00
|
|
|
|
|
|
|
// If the segment manager is not null, then the segment exists
|
|
|
|
// Get the table from the segment
|
|
|
|
ShmemAllocator alloc_inst (segment.get_segment_manager());
|
|
|
|
|
|
|
|
boost::interprocess::offset_ptr<CDBehaviorParameterMap> m = segment.find<CDBehaviorParameterMap>("CDBehaviorParameter").first;
|
|
|
|
|
|
|
|
if (m == nullptr) {
|
|
|
|
Game::logger->Log("CDBehaviorParameterTable", "CDBehaviorParameter segment is nullptr!\n");
|
|
|
|
|
|
|
|
return defaultValue;
|
2022-07-09 22:40:13 +00:00
|
|
|
}*/
|
|
|
|
|
|
|
|
// Start a timer
|
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
2022-07-09 20:59:36 +00:00
|
|
|
|
|
|
|
auto it = m->find(hash);
|
|
|
|
|
2022-07-09 22:40:13 +00:00
|
|
|
bool found = it != m->end();
|
|
|
|
|
|
|
|
// End the timer
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
// Get duraction
|
|
|
|
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
|
|
|
|
|
|
|
|
// Reset the timer
|
|
|
|
/*start = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
auto nativeIt = m_Entries.find(hash);
|
|
|
|
|
|
|
|
bool nativeFound = nativeIt != m_Entries.end();
|
|
|
|
|
|
|
|
// End the timer
|
|
|
|
end = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
// Get duraction
|
|
|
|
auto nativeDuration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);*/
|
|
|
|
|
|
|
|
//Game::logger->Log("CDBehaviorParameterTable", "Duration: (sm) %fms / (m) %fms -> %f\n", duration.count() / 1000000.0f, nativeDuration.count() / 1000000.0f, (double) duration.count() / (double) nativeDuration.count());
|
|
|
|
|
|
|
|
if (found) {
|
2022-07-09 20:59:36 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
catch (std::exception &e) {
|
|
|
|
Game::logger->Log("CDBehaviorParameterTable", "Failed to find entry for behaviorID: %d, parameterID: %s\n%e\n", behaviorID, name.c_str(), e.what());
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2022-07-09 06:07:52 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-09 20:59:36 +00:00
|
|
|
void CDBehaviorParameterTable::CreateSharedMap()
|
|
|
|
{
|
|
|
|
boost::interprocess::shared_memory_object::remove("CDBehaviorParameterTable");
|
|
|
|
|
2022-07-09 22:40:13 +00:00
|
|
|
Game::logger->Log("CDBehaviorParameterTable", "Create shared memory segment.\n");
|
2022-07-09 20:59:36 +00:00
|
|
|
|
2022-07-09 22:40:13 +00:00
|
|
|
// Create the segment, TODO: calculate size
|
2022-07-09 20:59:36 +00:00
|
|
|
boost::interprocess::managed_shared_memory segment(boost::interprocess::create_only, "CDBehaviorParameterTable", 40 * 1000 * 1000);
|
|
|
|
|
|
|
|
ShmemAllocator alloc_inst (segment.get_segment_manager());
|
|
|
|
|
2022-07-09 22:40:13 +00:00
|
|
|
table = segment.construct<CDBehaviorParameterMap>("CDBehaviorParameter") (std::less<KeyType>(), alloc_inst);
|
2022-07-09 20:59:36 +00:00
|
|
|
|
|
|
|
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM BehaviorParameter");
|
|
|
|
|
|
|
|
size_t hash = 0;
|
|
|
|
|
|
|
|
while (!tableData.eof()) {
|
2022-07-09 06:07:52 +00:00
|
|
|
hash = 0;
|
2022-07-09 20:59:36 +00:00
|
|
|
|
|
|
|
int32_t behaviorID = tableData.getIntField(0, -1);
|
|
|
|
std::string parameterID = tableData.getStringField(1, "");
|
|
|
|
|
2022-07-09 06:07:52 +00:00
|
|
|
GeneralUtils::hash_combine(hash, behaviorID);
|
2022-07-09 20:59:36 +00:00
|
|
|
GeneralUtils::hash_combine(hash, parameterID);
|
|
|
|
|
|
|
|
float value = tableData.getFloatField(2, -1.0f);
|
|
|
|
|
|
|
|
table->insert(ValueType(hash, value));
|
|
|
|
|
|
|
|
tableData.nextRow();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-09 20:59:36 +00:00
|
|
|
|
|
|
|
tableData.finalize();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|