feat(Core/DBC): rework load DBC (#3002)

* Move DBC load system from TC
* Add db tables for all dbc
* Support override data from db
* Support override spells from db (#2994)
This commit is contained in:
Kargatum
2020-07-22 13:43:16 +07:00
committed by GitHub
parent 3cce44469a
commit 833611f1c5
19 changed files with 13497 additions and 843 deletions

View File

@@ -1,25 +1,24 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-GPL2
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2008-2020 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "DBCFileLoader.h"
#include "Errors.h"
DBCFileLoader::DBCFileLoader() : recordSize(0), recordCount(0), fieldCount(0), stringSize(0), fieldsOffset(NULL), data(NULL), stringTable(NULL) { }
DBCFileLoader::DBCFileLoader() : recordSize(0), recordCount(0), fieldCount(0), stringSize(0), fieldsOffset(nullptr), data(nullptr), stringTable(nullptr) { }
bool DBCFileLoader::Load(const char* filename, const char* fmt)
bool DBCFileLoader::Load(char const* filename, char const* fmt)
{
uint32 header;
if (data)
{
delete [] data;
data = NULL;
data = nullptr;
}
FILE* f = fopen(filename, "rb");
@@ -32,7 +31,6 @@ bool DBCFileLoader::Load(const char* filename, const char* fmt)
return false;
}
EndianConvert(header);
if (header != 0x43424457) //'WDBC'
@@ -75,6 +73,7 @@ bool DBCFileLoader::Load(const char* filename, const char* fmt)
fieldsOffset = new uint32[fieldCount];
fieldsOffset[0] = 0;
for (uint32 i = 1; i < fieldCount; ++i)
{
fieldsOffset[i] = fieldsOffset[i - 1];
@@ -100,23 +99,22 @@ bool DBCFileLoader::Load(const char* filename, const char* fmt)
DBCFileLoader::~DBCFileLoader()
{
if (data)
delete [] data;
delete[] data;
if (fieldsOffset)
delete [] fieldsOffset;
delete[] fieldsOffset;
}
DBCFileLoader::Record DBCFileLoader::getRecord(size_t id)
{
assert(data);
ASSERT(data);
return Record(*this, data + id * recordSize);
}
uint32 DBCFileLoader::GetFormatRecordSize(const char* format, int32* index_pos)
uint32 DBCFileLoader::GetFormatRecordSize(char const* format, int32* index_pos)
{
uint32 recordsize = 0;
int32 i = -1;
for (uint32 x = 0; format[x]; ++x)
{
switch (format[x])
@@ -158,7 +156,7 @@ uint32 DBCFileLoader::GetFormatRecordSize(const char* format, int32* index_pos)
return recordsize;
}
char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**& indexTable, uint32 sqlRecordCount, uint32 sqlHighestIndex, char*& sqlDataTable)
char* DBCFileLoader::AutoProduceData(char const* format, uint32& records, char**& indexTable)
{
/*
format STRING, NA, FLOAT, NA, INT <=>
@@ -173,7 +171,7 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
typedef char* ptr;
if (strlen(format) != fieldCount)
return NULL;
return nullptr;
//get struct size and index pos
int32 i;
@@ -190,10 +188,6 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
maxi = ind;
}
// If higher index avalible from sql - use it instead of dbcs
if (sqlHighestIndex > maxi)
maxi = sqlHighestIndex;
++maxi;
records = maxi;
indexTable = new ptr[maxi];
@@ -201,11 +195,11 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
}
else
{
records = recordCount + sqlRecordCount;
indexTable = new ptr[recordCount + sqlRecordCount];
records = recordCount;
indexTable = new ptr[recordCount];
}
char* dataTable = new char[(recordCount + sqlRecordCount) * recordsize];
char* dataTable = new char[recordCount * recordsize];
uint32 offset = 0;
@@ -234,7 +228,7 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
offset += sizeof(uint8);
break;
case FT_STRING:
*((char**)(&dataTable[offset])) = NULL; // will replace non-empty or "" strings in AutoProduceStrings
*((char**)(&dataTable[offset])) = nullptr; // will replace non-empty or "" strings in AutoProduceStrings
offset += sizeof(char*);
break;
case FT_LOGIC:
@@ -251,15 +245,13 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**
}
}
sqlDataTable = dataTable + offset;
return dataTable;
}
char* DBCFileLoader::AutoProduceStrings(const char* format, char* dataTable)
char* DBCFileLoader::AutoProduceStrings(char const* format, char* dataTable)
{
if (strlen(format) != fieldCount)
return NULL;
return nullptr;
char* stringPool = new char[stringSize];
memcpy(stringPool, stringTable, stringSize);
@@ -289,21 +281,21 @@ char* DBCFileLoader::AutoProduceStrings(const char* format, char* dataTable)
if (!*slot || !**slot)
{
const char * st = getRecord(y).getString(x);
*slot=stringPool+(st-(const char*)stringTable);
*slot = stringPool + (st - (char const*)stringTable);
}
offset += sizeof(char*);
break;
}
case FT_LOGIC:
ASSERT(false && "Attempted to load DBC files that does not have field types that match what is in the core. Check DBCfmt.h or your DBC files.");
break;
case FT_NA:
case FT_NA_BYTE:
case FT_SORT:
break;
default:
ASSERT(false && "Unknown field format character in DBCfmt.h");
break;
}
case FT_LOGIC:
ASSERT(false && "Attempted to load DBC files that does not have field types that match what is in the core. Check DBCfmt.h or your DBC files.");
break;
case FT_NA:
case FT_NA_BYTE:
case FT_SORT:
break;
default:
ASSERT(false && "Unknown field format character in DBCfmt.h");
break;
}
}
}

View File

@@ -1,98 +1,102 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-GPL2
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2008-2020 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#ifndef DBC_FILE_LOADER_H
#define DBC_FILE_LOADER_H
#include "Define.h"
#include "Errors.h"
#include "Utilities/ByteConverter.h"
#include <cassert>
enum DbcFieldFormat
{
FT_NA='x', //not used or unknown, 4 byte size
FT_NA_BYTE='X', //not used or unknown, byte
FT_STRING='s', //char*
FT_FLOAT='f', //float
FT_INT='i', //uint32
FT_BYTE='b', //uint8
FT_SORT='d', //sorted by this field, field is not included
FT_IND='n', //the same, but parsed to data
FT_LOGIC='l', //Logical (boolean)
FT_SQL_PRESENT='p', //Used in sql format to mark column present in sql dbc
FT_SQL_ABSENT='a' //Used in sql format to mark column absent in sql dbc
FT_NA = 'x', //not used or unknown, 4 byte size
FT_NA_BYTE = 'X', //not used or unknown, byte
FT_STRING = 's', //char*
FT_FLOAT = 'f', //float
FT_INT = 'i', //uint32
FT_BYTE = 'b', //uint8
FT_SORT = 'd', //sorted by this field, field is not included
FT_IND = 'n', //the same, but parsed to data
FT_LOGIC = 'l' //Logical (boolean)
};
class DBCFileLoader
{
public:
DBCFileLoader();
~DBCFileLoader();
bool Load(const char *filename, const char *fmt);
class Record
{
public:
DBCFileLoader();
~DBCFileLoader();
bool Load(const char *filename, const char *fmt);
class Record
float getFloat(size_t field) const
{
public:
float getFloat(size_t field) const
{
assert(field < file.fieldCount);
float val = *reinterpret_cast<float*>(offset+file.GetOffset(field));
EndianConvert(val);
return val;
}
uint32 getUInt(size_t field) const
{
assert(field < file.fieldCount);
uint32 val = *reinterpret_cast<uint32*>(offset+file.GetOffset(field));
EndianConvert(val);
return val;
}
uint8 getUInt8(size_t field) const
{
assert(field < file.fieldCount);
return *reinterpret_cast<uint8*>(offset+file.GetOffset(field));
}
ASSERT(field < file.fieldCount);
float val = *reinterpret_cast<float*>(offset+file.GetOffset(field));
EndianConvert(val);
return val;
}
const char *getString(size_t field) const
{
assert(field < file.fieldCount);
size_t stringOffset = getUInt(field);
assert(stringOffset < file.stringSize);
return reinterpret_cast<char*>(file.stringTable + stringOffset);
}
uint32 getUInt(size_t field) const
{
ASSERT(field < file.fieldCount);
uint32 val = *reinterpret_cast<uint32*>(offset+file.GetOffset(field));
EndianConvert(val);
return val;
}
private:
Record(DBCFileLoader &file_, unsigned char *offset_): offset(offset_), file(file_) { }
unsigned char *offset;
DBCFileLoader &file;
uint8 getUInt8(size_t field) const
{
ASSERT(field < file.fieldCount);
return *reinterpret_cast<uint8*>(offset+file.GetOffset(field));
}
friend class DBCFileLoader;
const char *getString(size_t field) const
{
ASSERT(field < file.fieldCount);
size_t stringOffset = getUInt(field);
ASSERT(stringOffset < file.stringSize);
return reinterpret_cast<char*>(file.stringTable + stringOffset);
}
};
// Get record by id
Record getRecord(size_t id);
/// Get begin iterator over records
uint32 GetNumRows() const { return recordCount; }
uint32 GetRowSize() const { return recordSize; }
uint32 GetCols() const { return fieldCount; }
uint32 GetOffset(size_t id) const { return (fieldsOffset != NULL && id < fieldCount) ? fieldsOffset[id] : 0; }
bool IsLoaded() const { return data != NULL; }
char* AutoProduceData(const char* fmt, uint32& count, char**& indexTable, uint32 sqlRecordCount, uint32 sqlHighestIndex, char *& sqlDataTable);
char* AutoProduceStrings(const char* fmt, char* dataTable);
static uint32 GetFormatRecordSize(const char * format, int32 * index_pos = NULL);
private:
Record(DBCFileLoader &file_, unsigned char *offset_): offset(offset_), file(file_) { }
unsigned char *offset;
DBCFileLoader &file;
uint32 recordSize;
uint32 recordCount;
uint32 fieldCount;
uint32 stringSize;
uint32 *fieldsOffset;
unsigned char *data;
unsigned char *stringTable;
friend class DBCFileLoader;
};
// Get record by id
Record getRecord(size_t id);
uint32 GetNumRows() const { return recordCount; }
uint32 GetRowSize() const { return recordSize; }
uint32 GetCols() const { return fieldCount; }
uint32 GetOffset(size_t id) const { return (fieldsOffset != nullptr && id < fieldCount) ? fieldsOffset[id] : 0; }
bool IsLoaded() const { return data != nullptr; }
char* AutoProduceData(char const* fmt, uint32& count, char**& indexTable);
char* AutoProduceStrings(char const* fmt, char* dataTable);
static uint32 GetFormatRecordSize(const char * format, int32 * index_pos = nullptr);
private:
uint32 recordSize;
uint32 recordCount;
uint32 fieldCount;
uint32 stringSize;
uint32 *fieldsOffset;
unsigned char *data;
unsigned char *stringTable;
DBCFileLoader(DBCFileLoader const& right) = delete;
DBCFileLoader& operator=(DBCFileLoader const& right) = delete;
};
#endif

View File

@@ -1,322 +0,0 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-GPL2
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#ifndef DBCSTORE_H
#define DBCSTORE_H
#include "DBCFileLoader.h"
#include "Logging/Log.h"
#include "Field.h"
#include "DatabaseWorkerPool.h"
#include "Implementation/WorldDatabase.h"
#include "DatabaseEnv.h"
#include <unordered_map>
struct SqlDbc
{
std::string const* formatString;
std::string const* indexName;
std::string sqlTableName;
int32 indexPos;
int32 sqlIndexPos;
SqlDbc(std::string const* _filename, std::string const* _format, std::string const* _idname, char const* fmt)
: formatString(_format), indexName (_idname), sqlIndexPos(0)
{
// Convert dbc file name to sql table name
sqlTableName = *_filename;
for (uint32 i = 0; i< sqlTableName.size(); ++i)
{
if (isalpha(sqlTableName[i]))
sqlTableName[i] = char(tolower(sqlTableName[i]));
else if (sqlTableName[i] == '.')
sqlTableName[i] = '_';
}
// Get sql index position
DBCFileLoader::GetFormatRecordSize(fmt, &indexPos);
if (indexPos >= 0)
{
uint32 uindexPos = uint32(indexPos);
for (uint32 x = 0; x < formatString->size(); ++x)
{
// Count only fields present in sql
if ((*formatString)[x] == FT_SQL_PRESENT)
{
if (x == uindexPos)
break;
++sqlIndexPos;
}
}
}
}
};
template<class T>
class DBCStorage
{
typedef std::list<char*> StringPoolList;
public:
explicit DBCStorage(char const* f)
#ifndef ELUNA
: fmt(f), nCount(0), fieldCount(0), dataTable(NULL)
#else
: fmt(f), nCount(0), fieldCount(0), dataTable(NULL), maxdatacount(0), mindatacount(std::numeric_limits<uint32>::max())
#endif
{
indexTable.asT = NULL;
}
~DBCStorage() { Clear(); }
T const* LookupEntry(uint32 id) const
{
#ifdef ELUNA
if (id <= maxdatacount && id >= mindatacount)
{
typename std::unordered_map<uint32, T const*>::const_iterator it = data.find(id);
if (it != data.end())
return it->second;
}
#endif
return (id >= nCount) ? NULL : indexTable.asT[id];
}
#ifdef ELUNA
void SetEntry(uint32 id, T* t)
{
delete data[id];
data[id] = t;
maxdatacount = std::max(maxdatacount, id);
mindatacount = std::min(mindatacount, id);
}
#endif
#ifndef ELUNA
uint32 GetNumRows() const { return nCount; }
#else
uint32 GetNumRows() const { return std::max(maxdatacount + 1, nCount); }
#endif
char const* GetFormat() const { return fmt; }
uint32 GetFieldCount() const { return fieldCount; }
bool Load(char const* fn, SqlDbc* sql)
{
DBCFileLoader dbc;
// Check if load was sucessful, only then continue
if (!dbc.Load(fn, fmt))
return false;
uint32 sqlRecordCount = 0;
uint32 sqlHighestIndex = 0;
Field* fields = NULL;
QueryResult result = QueryResult(NULL);
// Load data from sql
if (sql)
{
std::string query = "SELECT * FROM " + sql->sqlTableName;
if (sql->indexPos >= 0)
query +=" ORDER BY " + *sql->indexName + " DESC";
query += ';';
result = WorldDatabase.Query(query.c_str());
if (result)
{
sqlRecordCount = uint32(result->GetRowCount());
if (sql->indexPos >= 0)
{
fields = result->Fetch();
sqlHighestIndex = fields[sql->sqlIndexPos].GetUInt32();
}
// Check if sql index pos is valid
if (int32(result->GetFieldCount() - 1) < sql->sqlIndexPos)
{
sLog->outError("Invalid index pos for dbc:'%s'", sql->sqlTableName.c_str());
return false;
}
}
}
char* sqlDataTable = NULL;
fieldCount = dbc.GetCols();
dataTable = reinterpret_cast<T*>(dbc.AutoProduceData(fmt, nCount, indexTable.asChar,
sqlRecordCount, sqlHighestIndex, sqlDataTable));
stringPoolList.push_back(dbc.AutoProduceStrings(fmt, reinterpret_cast<char*>(dataTable)));
// Insert sql data into arrays
if (result)
{
if (indexTable.asT)
{
uint32 offset = 0;
uint32 rowIndex = dbc.GetNumRows();
do
{
if (!fields)
fields = result->Fetch();
if (sql->indexPos >= 0)
{
uint32 id = fields[sql->sqlIndexPos].GetUInt32();
if (indexTable.asT[id])
{
sLog->outError("Index %d already exists in dbc:'%s'", id, sql->sqlTableName.c_str());
return false;
}
indexTable.asT[id] = reinterpret_cast<T*>(&sqlDataTable[offset]);
}
else
indexTable.asT[rowIndex]= reinterpret_cast<T*>(&sqlDataTable[offset]);
uint32 columnNumber = 0;
uint32 sqlColumnNumber = 0;
for (; columnNumber < sql->formatString->size(); ++columnNumber)
{
if ((*sql->formatString)[columnNumber] == FT_SQL_ABSENT)
{
switch (fmt[columnNumber])
{
case FT_FLOAT:
*reinterpret_cast<float*>(&sqlDataTable[offset]) = 0.0f;
offset += 4;
break;
case FT_IND:
case FT_INT:
*reinterpret_cast<uint32*>(&sqlDataTable[offset]) = uint32(0);
offset += 4;
break;
case FT_BYTE:
*reinterpret_cast<uint8*>(&sqlDataTable[offset]) = uint8(0);
offset += 1;
break;
case FT_STRING:
// Beginning of the pool - empty string
*reinterpret_cast<char**>(&sqlDataTable[offset]) = stringPoolList.back();
offset += sizeof(char*);
break;
}
}
else if ((*sql->formatString)[columnNumber] == FT_SQL_PRESENT)
{
bool validSqlColumn = true;
switch (fmt[columnNumber])
{
case FT_FLOAT:
*reinterpret_cast<float*>(&sqlDataTable[offset]) = fields[sqlColumnNumber].GetFloat();
offset += 4;
break;
case FT_IND:
case FT_INT:
*reinterpret_cast<uint32*>(&sqlDataTable[offset]) = fields[sqlColumnNumber].GetUInt32();
offset += 4;
break;
case FT_BYTE:
*reinterpret_cast<uint8*>(&sqlDataTable[offset]) = fields[sqlColumnNumber].GetUInt8();
offset += 1;
break;
case FT_STRING:
sLog->outError("Unsupported data type in table '%s' at char %d", sql->sqlTableName.c_str(), columnNumber);
return false;
case FT_SORT:
break;
default:
validSqlColumn = false;
break;
}
if (validSqlColumn && (columnNumber != (sql->formatString->size()-1)))
sqlColumnNumber++;
}
else
{
sLog->outError("Incorrect sql format string '%s' at char %d", sql->sqlTableName.c_str(), columnNumber);
return false;
}
}
if (sqlColumnNumber != (result->GetFieldCount() - 1))
{
sLog->outError("SQL and DBC format strings are not matching for table: '%s'", sql->sqlTableName.c_str());
return false;
}
fields = NULL;
++rowIndex;
} while (result->NextRow());
}
}
// error in dbc file at loading if NULL
return indexTable.asT != NULL;
}
bool LoadStringsFrom(char const* fn)
{
// DBC must be already loaded using Load
if (!indexTable.asT)
return false;
DBCFileLoader dbc;
// Check if load was successful, only then continue
if (!dbc.Load(fn, fmt))
return false;
stringPoolList.push_back(dbc.AutoProduceStrings(fmt, reinterpret_cast<char*>(dataTable)));
return true;
}
void Clear()
{
#ifdef ELUNA
data.clear();
maxdatacount = 0;
mindatacount = std::numeric_limits<uint32>::max();
#endif
if (!indexTable.asT)
return;
delete[] reinterpret_cast<char*>(indexTable.asT);
indexTable.asT = NULL;
delete[] reinterpret_cast<char*>(dataTable);
dataTable = NULL;
while (!stringPoolList.empty())
{
delete[] stringPoolList.front();
stringPoolList.pop_front();
}
nCount = 0;
}
private:
char const* fmt;
uint32 nCount;
uint32 fieldCount;
union
{
T** asT;
char** asChar;
}
indexTable;
T* dataTable;
StringPoolList stringPoolList;
#ifdef ELUNA
uint32 maxdatacount;
uint32 mindatacount;
std::unordered_map<uint32, T const*> data;
#endif
};
#endif