Skip to content

Commit

Permalink
Merge branch 'main' into somethingElse
Browse files Browse the repository at this point in the history
  • Loading branch information
EmosewaMC committed Oct 10, 2023
2 parents 9b0833f + c6087ce commit 26d0951
Show file tree
Hide file tree
Showing 46 changed files with 433 additions and 546 deletions.
2 changes: 1 addition & 1 deletion dCommon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ set(DCOMMON_SOURCES
"NiPoint3.cpp"
"NiQuaternion.cpp"
"SHA512.cpp"
"Type.cpp"
"Demangler.cpp"
"ZCompression.cpp"
"BrickByBrickFix.cpp"
"BinaryPathFinder.cpp"
Expand Down
29 changes: 29 additions & 0 deletions dCommon/Demangler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "Demangler.h"
#ifdef __GNUG__
#include <cstdlib>
#include <cxxabi.h>
#include <memory>
#include <typeinfo>

std::string Demangler::Demangle(const char* name) {
// some arbitrary value to eliminate the compiler warning
// -4 is not a valid return value for __cxa_demangle so we'll use that.
int status = -4;

// __cxa_demangle requires that we free the returned char*
std::unique_ptr<char, void (*)(void*)> res{
abi::__cxa_demangle(name, NULL, NULL, &status),
std::free
};

return (status == 0) ? res.get() : "";
}

#else // __GNUG__

// does nothing if not g++
std::string Demangler::Demangle(const char* name) {
return name;
}

#endif // __GNUG__
9 changes: 9 additions & 0 deletions dCommon/Demangler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <string>

namespace Demangler {
// Given a char* containing a mangled name, return a std::string containing the demangled name.
// If the function fails for any reason, it returns an empty string.
std::string Demangle(const char* name);
}
36 changes: 19 additions & 17 deletions dCommon/Diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static void ErrorCallback(void* data, const char* msg, int errnum) {
}
#endif

#include "Type.h"
#include "Demangler.h"

void GenerateDump() {
std::string cmd = "sudo gcore " + std::to_string(getpid());
Expand All @@ -122,41 +122,43 @@ void CatchUnhandled(int sig) {
if (Diagnostics::GetProduceMemoryDump()) {
GenerateDump();
}

void* array[10];
constexpr uint8_t MaxStackTrace = 32;
void* array[MaxStackTrace];
size_t size;

// get void*'s for all entries on the stack
size = backtrace(array, 10);
size = backtrace(array, MaxStackTrace);

#if defined(__GNUG__) and defined(__dynamic)
# if defined(__GNUG__)

// Loop through the returned addresses, and get the symbols to be demangled
char** strings = backtrace_symbols(array, size);

// Print the stack trace
for (size_t i = 0; i < size; i++) {
// Take a string like './WorldServer(_ZN19SlashCommandHandler17HandleChatCommandERKSbIDsSt11char_traitsIDsESaIDsEEP6EntityRK13SystemAddress+0x6187) [0x55869c44ecf7]' and extract the function name
// Take a string like './WorldServer(_ZN19SlashCommandHandler17HandleChatCommandERKSbIDsSt11char_traitsIDsESaIDsEEP6EntityRK13SystemAddress+0x6187) [0x55869c44ecf7]'
// and extract '_ZN19SlashCommandHandler17HandleChatCommandERKSbIDsSt11char_traitsIDsESaIDsEEP6EntityRK13SystemAddress' from it to be demangled into a proper name
std::string functionName = strings[i];
std::string::size_type start = functionName.find('(');
std::string::size_type end = functionName.find('+');
if (start != std::string::npos && end != std::string::npos) {
std::string demangled = functionName.substr(start + 1, end - start - 1);

demangled = demangle(functionName.c_str());
demangled = Demangler::Demangle(demangled.c_str());

if (demangled.empty()) {
Game::logger->Log("Diagnostics", "[%02zu] %s", i, demangled.c_str());
} else {
Game::logger->Log("Diagnostics", "[%02zu] %s", i, functionName.c_str());
// If the demangled string is not empty, then we can replace the mangled string with the demangled one
if (!demangled.empty()) {
demangled.push_back('(');
demangled += functionName.substr(end);
functionName = demangled;
}
} else {
Game::logger->Log("Diagnostics", "[%02zu] %s", i, functionName.c_str());
}

Game::logger->Log("Diagnostics", "[%02zu] %s", i, functionName.c_str());
}
#else
# else // defined(__GNUG__)
backtrace_symbols_fd(array, size, STDOUT_FILENO);
#endif
# endif // defined(__GNUG__)

FILE* file = fopen(fileName.c_str(), "w+");
if (file != NULL) {
Expand All @@ -166,7 +168,7 @@ void CatchUnhandled(int sig) {
fclose(file);
}

#else
#else // __include_backtrace__

struct backtrace_state* state = backtrace_create_state(
Diagnostics::GetProcessFileName().c_str(),
Expand All @@ -177,7 +179,7 @@ void CatchUnhandled(int sig) {
struct bt_ctx ctx = { state, 0 };
Bt(state);

#endif
#endif // __include_backtrace__

exit(EXIT_FAILURE);
}
Expand Down
27 changes: 0 additions & 27 deletions dCommon/Type.cpp

This file was deleted.

12 changes: 0 additions & 12 deletions dCommon/Type.h

This file was deleted.

6 changes: 3 additions & 3 deletions dDatabase/CDClientManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ CDClientManager::CDClientManager() {
CDBehaviorParameterTable::Instance().LoadValuesFromDatabase();
CDBehaviorTemplateTable::Instance().LoadValuesFromDatabase();
CDBrickIDTableTable::Instance().LoadValuesFromDatabase();
CDComponentsRegistryTable::Instance().LoadValuesFromDatabase();
CDCLIENT_DONT_CACHE_TABLE(CDComponentsRegistryTable::Instance().LoadValuesFromDatabase());
CDCurrencyTableTable::Instance().LoadValuesFromDatabase();
CDDestructibleComponentTable::Instance().LoadValuesFromDatabase();
CDEmoteTableTable::Instance().LoadValuesFromDatabase();
Expand All @@ -65,8 +65,8 @@ CDClientManager::CDClientManager() {
CDItemSetSkillsTable::Instance().LoadValuesFromDatabase();
CDItemSetsTable::Instance().LoadValuesFromDatabase();
CDLevelProgressionLookupTable::Instance().LoadValuesFromDatabase();
CDLootMatrixTable::Instance().LoadValuesFromDatabase();
CDLootTableTable::Instance().LoadValuesFromDatabase();
CDCLIENT_DONT_CACHE_TABLE(CDLootMatrixTable::Instance().LoadValuesFromDatabase());
CDCLIENT_DONT_CACHE_TABLE(CDLootTableTable::Instance().LoadValuesFromDatabase());
CDMissionEmailTable::Instance().LoadValuesFromDatabase();
CDMissionNPCComponentTable::Instance().LoadValuesFromDatabase();
CDMissionTasksTable::Instance().LoadValuesFromDatabase();
Expand Down
52 changes: 29 additions & 23 deletions dDatabase/Tables/CDLootMatrixTable.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
#include "CDLootMatrixTable.h"

CDLootMatrix CDLootMatrixTable::ReadRow(CppSQLite3Query& tableData) const {
CDLootMatrix entry{};
if (tableData.eof()) return entry;
entry.LootTableIndex = tableData.getIntField("LootTableIndex", -1);
entry.RarityTableIndex = tableData.getIntField("RarityTableIndex", -1);
entry.percent = tableData.getFloatField("percent", -1.0f);
entry.minToDrop = tableData.getIntField("minToDrop", -1);
entry.maxToDrop = tableData.getIntField("maxToDrop", -1);
entry.flagID = tableData.getIntField("flagID", -1);
UNUSED(entry.gate_version = tableData.getStringField("gate_version", ""));
return entry;
}

void CDLootMatrixTable::LoadValuesFromDatabase() {

// First, get the size of the table
Expand All @@ -11,42 +24,35 @@ void CDLootMatrixTable::LoadValuesFromDatabase() {
tableSize.nextRow();
}

tableSize.finalize();

// Reserve the size
this->entries.reserve(size);

// Now get the data
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM LootMatrix");
while (!tableData.eof()) {
CDLootMatrix entry;
entry.LootMatrixIndex = tableData.getIntField("LootMatrixIndex", -1);
entry.LootTableIndex = tableData.getIntField("LootTableIndex", -1);
entry.RarityTableIndex = tableData.getIntField("RarityTableIndex", -1);
entry.percent = tableData.getFloatField("percent", -1.0f);
entry.minToDrop = tableData.getIntField("minToDrop", -1);
entry.maxToDrop = tableData.getIntField("maxToDrop", -1);
entry.id = tableData.getIntField("id", -1);
entry.flagID = tableData.getIntField("flagID", -1);
UNUSED(entry.gate_version = tableData.getStringField("gate_version", ""));

this->entries.push_back(entry);
uint32_t lootMatrixIndex = tableData.getIntField("LootMatrixIndex", -1);

this->entries[lootMatrixIndex].push_back(ReadRow(tableData));
tableData.nextRow();
}

tableData.finalize();
}

std::vector<CDLootMatrix> CDLootMatrixTable::Query(std::function<bool(CDLootMatrix)> predicate) {
const LootMatrixEntries& CDLootMatrixTable::GetMatrix(uint32_t matrixId) {
auto itr = this->entries.find(matrixId);
if (itr != this->entries.end()) {
return itr->second;
}

std::vector<CDLootMatrix> data = cpplinq::from(this->entries)
>> cpplinq::where(predicate)
>> cpplinq::to_vector();
auto query = CDClientDatabase::CreatePreppedStmt("SELECT * FROM LootMatrix where LootMatrixIndex = ?;");
query.bind(1, static_cast<int32_t>(matrixId));

return data;
}
auto tableData = query.execQuery();
while (!tableData.eof()) {
this->entries[matrixId].push_back(ReadRow(tableData));
tableData.nextRow();
}

const std::vector<CDLootMatrix>& CDLootMatrixTable::GetEntries() const {
return this->entries;
return this->entries[matrixId];
}

16 changes: 8 additions & 8 deletions dDatabase/Tables/CDLootMatrixTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
#include "CDTable.h"

struct CDLootMatrix {
unsigned int LootMatrixIndex; //!< The Loot Matrix Index
unsigned int LootTableIndex; //!< The Loot Table Index
unsigned int RarityTableIndex; //!< The Rarity Table Index
float percent; //!< The percent that this matrix is used?
unsigned int minToDrop; //!< The minimum amount of loot from this matrix to drop
unsigned int maxToDrop; //!< The maximum amount of loot from this matrix to drop
unsigned int id; //!< The ID of the Loot Matrix
unsigned int flagID; //!< ???
UNUSED(std::string gate_version); //!< The Gate Version
};

class CDLootMatrixTable : public CDTable<CDLootMatrixTable> {
private:
std::vector<CDLootMatrix> entries;
typedef uint32_t LootMatrixIndex;
typedef std::vector<CDLootMatrix> LootMatrixEntries;

class CDLootMatrixTable : public CDTable<CDLootMatrixTable> {
public:
void LoadValuesFromDatabase();
// Queries the table with a custom "where" clause
std::vector<CDLootMatrix> Query(std::function<bool(CDLootMatrix)> predicate);

const std::vector<CDLootMatrix>& GetEntries() const;
// Gets a matrix by ID or inserts a blank one if none existed.
const LootMatrixEntries& GetMatrix(uint32_t matrixId);
private:
CDLootMatrix ReadRow(CppSQLite3Query& tableData) const;
std::unordered_map<LootMatrixIndex, LootMatrixEntries> entries;
};

49 changes: 26 additions & 23 deletions dDatabase/Tables/CDLootTableTable.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#include "CDLootTableTable.h"

CDLootTable CDLootTableTable::ReadRow(CppSQLite3Query& tableData) const {
CDLootTable entry{};
if (tableData.eof()) return entry;
entry.itemid = tableData.getIntField("itemid", -1);
entry.MissionDrop = tableData.getIntField("MissionDrop", -1) == 1 ? true : false;
entry.sortPriority = tableData.getIntField("sortPriority", -1);
return entry;
}

void CDLootTableTable::LoadValuesFromDatabase() {

// First, get the size of the table
Expand All @@ -11,41 +20,35 @@ void CDLootTableTable::LoadValuesFromDatabase() {
tableSize.nextRow();
}

tableSize.finalize();

// Reserve the size
this->entries.reserve(size);

// Now get the data
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM LootTable");
while (!tableData.eof()) {
CDLootTable entry;
entry.id = tableData.getIntField("id", -1);
entry.itemid = tableData.getIntField("itemid", -1);
entry.LootTableIndex = tableData.getIntField("LootTableIndex", -1);
entry.id = tableData.getIntField("id", -1);
entry.MissionDrop = tableData.getIntField("MissionDrop", -1) == 1 ? true : false;
entry.sortPriority = tableData.getIntField("sortPriority", -1);

this->entries.push_back(entry);
uint32_t lootTableIndex = tableData.getIntField("LootTableIndex", -1);

this->entries[lootTableIndex].push_back(ReadRow(tableData));
tableData.nextRow();
}

tableData.finalize();
}

//! Queries the table with a custom "where" clause
std::vector<CDLootTable> CDLootTableTable::Query(std::function<bool(CDLootTable)> predicate) {
const LootTableEntries& CDLootTableTable::GetTable(uint32_t tableId) {
auto itr = this->entries.find(tableId);
if (itr != this->entries.end()) {
return itr->second;
}

std::vector<CDLootTable> data = cpplinq::from(this->entries)
>> cpplinq::where(predicate)
>> cpplinq::to_vector();
auto query = CDClientDatabase::CreatePreppedStmt("SELECT * FROM LootTable WHERE LootTableIndex = ?;");
query.bind(1, static_cast<int32_t>(tableId));
auto tableData = query.execQuery();

return data;
}
while (!tableData.eof()) {
CDLootTable entry;
this->entries[tableId].push_back(ReadRow(tableData));
tableData.nextRow();
}

//! Gets all the entries in the table
const std::vector<CDLootTable>& CDLootTableTable::GetEntries() const {
return this->entries;
return this->entries[tableId];
}

Loading

0 comments on commit 26d0951

Please sign in to comment.