Files
TCPlayerbotCore/src/server/shared/DataStores/DBCFileLoader.cpp
T

329 lines
8.9 KiB
C++
Raw Normal View History

/*
2011-01-01 15:01:13 +01:00
* Copyright (C) 2008-2011 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2009-06-07 15:29:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
#include "DBCFileLoader.h"
2011-03-19 12:50:17 +01:00
#include "Errors.h"
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
DBCFileLoader::DBCFileLoader()
{
data = NULL;
fieldsOffset = NULL;
}
2009-10-17 15:51:44 -07:00
bool DBCFileLoader::Load(const char* filename, const char* fmt)
2009-06-07 15:29:55 -05:00
{
uint32 header;
if (data)
2009-06-07 15:29:55 -05:00
{
delete [] data;
data = NULL;
2009-06-07 15:29:55 -05:00
}
2009-10-17 15:51:44 -07:00
FILE* f = fopen(filename,"rb");
if (!f)
return false;
2009-10-17 15:51:44 -07:00
if (fread(&header, 4, 1, f) != 1) // Number of records
2010-12-06 02:07:53 +01:00
{
fclose(f);
2009-06-07 15:29:55 -05:00
return false;
2010-12-06 02:07:53 +01:00
}
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
EndianConvert(header);
2009-10-17 15:51:44 -07:00
2010-12-06 02:07:53 +01:00
if (header != 0x43424457) //'WDBC'
{
fclose(f);
return false;
}
if (fread(&recordCount, 4, 1, f) != 1) // Number of records
2010-12-06 02:07:53 +01:00
{
fclose(f);
2009-06-07 15:29:55 -05:00
return false;
2010-12-06 02:07:53 +01:00
}
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
EndianConvert(recordCount);
2009-10-17 15:51:44 -07:00
if (fread(&fieldCount, 4, 1, f) != 1) // Number of fields
2010-12-06 02:07:53 +01:00
{
fclose(f);
2009-06-07 15:29:55 -05:00
return false;
2010-12-06 02:07:53 +01:00
}
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
EndianConvert(fieldCount);
2009-10-17 15:51:44 -07:00
if (fread(&recordSize, 4, 1, f) != 1) // Size of a record
2010-12-06 02:07:53 +01:00
{
fclose(f);
2009-06-07 15:29:55 -05:00
return false;
2010-12-06 02:07:53 +01:00
}
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
EndianConvert(recordSize);
2009-10-17 15:51:44 -07:00
if (fread(&stringSize, 4, 1, f) != 1) // String size
2010-12-06 02:07:53 +01:00
{
fclose(f);
2009-06-07 15:29:55 -05:00
return false;
2010-12-06 02:07:53 +01:00
}
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
EndianConvert(stringSize);
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
fieldsOffset = new uint32[fieldCount];
fieldsOffset[0] = 0;
for (uint32 i = 1; i < fieldCount; ++i)
2009-06-07 15:29:55 -05:00
{
fieldsOffset[i] = fieldsOffset[i - 1];
if (fmt[i - 1] == 'b' || fmt[i - 1] == 'X') // byte fields
fieldsOffset[i] += sizeof(uint8);
2009-06-07 15:29:55 -05:00
else // 4 byte fields (int32/float/strings)
fieldsOffset[i] += sizeof(uint32);
2009-06-07 15:29:55 -05:00
}
2009-10-17 15:51:44 -07:00
data = new unsigned char[recordSize * recordCount + stringSize];
2009-06-07 15:29:55 -05:00
stringTable = data + recordSize*recordCount;
2009-10-17 15:51:44 -07:00
if (fread(data, recordSize * recordCount + stringSize, 1, f) != 1)
2010-12-06 02:07:53 +01:00
{
fclose(f);
2009-06-07 15:29:55 -05:00
return false;
2010-12-06 02:07:53 +01:00
}
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
fclose(f);
2009-06-07 15:29:55 -05:00
return true;
}
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
DBCFileLoader::~DBCFileLoader()
{
if (data)
2009-06-07 15:29:55 -05:00
delete [] data;
if (fieldsOffset)
2009-06-07 15:29:55 -05:00
delete [] fieldsOffset;
}
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
DBCFileLoader::Record DBCFileLoader::getRecord(size_t id)
{
assert(data);
return Record(*this, data + id * recordSize);
2009-06-07 15:29:55 -05:00
}
2009-10-17 15:51:44 -07:00
uint32 DBCFileLoader::GetFormatRecordSize(const char* format, int32* index_pos)
2009-06-07 15:29:55 -05:00
{
uint32 recordsize = 0;
int32 i = -1;
for (uint32 x = 0; format[x]; ++x)
{
switch (format[x])
2009-06-07 15:29:55 -05:00
{
case FT_FLOAT:
recordsize += sizeof(float);
break;
2009-06-07 15:29:55 -05:00
case FT_INT:
recordsize += sizeof(uint32);
2009-06-07 15:29:55 -05:00
break;
case FT_STRING:
recordsize += sizeof(char*);
2009-06-07 15:29:55 -05:00
break;
case FT_SORT:
i = x;
2009-06-07 15:29:55 -05:00
break;
case FT_IND:
i = x;
recordsize += sizeof(uint32);
2009-06-07 15:29:55 -05:00
break;
case FT_BYTE:
recordsize += sizeof(uint8);
break;
case FT_NA:
case FT_NA_BYTE:
break;
case FT_LOGIC:
ASSERT(false && "Attempted to load DBC files that do not have field types that match what is in the core. Check DBCfmt.h or your DBC files.");
break;
default:
ASSERT(false && "Unknown field format character in DBCfmt.h");
2009-06-07 15:29:55 -05:00
break;
}
}
2009-10-17 15:51:44 -07:00
if (index_pos)
2009-06-07 15:29:55 -05:00
*index_pos = i;
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
return recordsize;
}
2009-10-17 15:51:44 -07:00
char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char**& indexTable, uint32 sqlRecordCount, uint32 sqlHighestIndex, char*& sqlDataTable)
2009-06-07 15:29:55 -05:00
{
/*
format STRING, NA, FLOAT,NA,INT <=>
struct{
char* field0,
float field1,
int field2
}entry;
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
this func will generate entry[rows] data;
*/
2009-10-17 15:51:44 -07:00
typedef char* ptr;
if (strlen(format) != fieldCount)
2009-06-07 15:29:55 -05:00
return NULL;
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
//get struct size and index pos
int32 i;
uint32 recordsize = GetFormatRecordSize(format, &i);
2009-10-17 15:51:44 -07:00
if (i >= 0)
2009-06-07 15:29:55 -05:00
{
uint32 maxi = 0;
2009-06-07 15:29:55 -05:00
//find max index
for (uint32 y = 0; y < recordCount; ++y)
2009-06-07 15:29:55 -05:00
{
uint32 ind = getRecord(y).getUInt(i);
if (ind > maxi)
maxi = ind;
2009-06-07 15:29:55 -05:00
}
2009-10-17 15:51:44 -07:00
// If higher index avalible from sql - use it instead of dbcs
if (sqlHighestIndex > maxi)
maxi = sqlHighestIndex;
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
++maxi;
records = maxi;
indexTable = new ptr[maxi];
memset(indexTable, 0, maxi * sizeof(ptr));
2009-06-07 15:29:55 -05:00
}
else
{
records = recordCount + sqlRecordCount;
indexTable = new ptr[recordCount + sqlRecordCount];
2009-06-07 15:29:55 -05:00
}
2009-10-17 15:51:44 -07:00
char* dataTable = new char[(recordCount + sqlRecordCount) * recordsize];
2009-10-17 15:51:44 -07:00
uint32 offset = 0;
2009-10-17 15:51:44 -07:00
for (uint32 y = 0; y < recordCount; ++y)
2009-06-07 15:29:55 -05:00
{
if (i >= 0)
indexTable[getRecord(y).getUInt(i)] = &dataTable[offset];
2009-06-07 15:29:55 -05:00
else
indexTable[y] = &dataTable[offset];
2009-10-17 15:51:44 -07:00
for (uint32 x=0; x < fieldCount; ++x)
2009-06-07 15:29:55 -05:00
{
switch (format[x])
2009-06-07 15:29:55 -05:00
{
case FT_FLOAT:
*((float*)(&dataTable[offset])) = getRecord(y).getFloat(x);
offset += sizeof(float);
2009-06-07 15:29:55 -05:00
break;
case FT_IND:
case FT_INT:
*((uint32*)(&dataTable[offset])) = getRecord(y).getUInt(x);
offset += sizeof(uint32);
2009-06-07 15:29:55 -05:00
break;
case FT_BYTE:
*((uint8*)(&dataTable[offset])) = getRecord(y).getUInt8(x);
offset += sizeof(uint8);
2009-06-07 15:29:55 -05:00
break;
case FT_STRING:
*((char**)(&dataTable[offset])) = NULL; // will replace non-empty or "" strings in AutoProduceStrings
offset += sizeof(char*);
break;
case FT_LOGIC:
ASSERT(false && "Attempted to load DBC files that do 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");
2009-06-07 15:29:55 -05:00
break;
}
}
}
sqlDataTable = dataTable + offset;
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
return dataTable;
}
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
char* DBCFileLoader::AutoProduceStrings(const char* format, char* dataTable)
{
if (strlen(format) != fieldCount)
2009-06-07 15:29:55 -05:00
return NULL;
2009-10-17 15:51:44 -07:00
char* stringPool = new char[stringSize];
memcpy(stringPool, stringTable, stringSize);
2009-10-17 15:51:44 -07:00
uint32 offset = 0;
2009-10-17 15:51:44 -07:00
for (uint32 y = 0; y < recordCount; ++y)
2009-06-07 15:29:55 -05:00
{
for (uint32 x = 0; x < fieldCount; ++x)
2009-06-07 15:29:55 -05:00
{
switch (format[x])
{
case FT_FLOAT:
offset += sizeof(float);
break;
case FT_IND:
case FT_INT:
offset += sizeof(uint32);
break;
case FT_BYTE:
offset += sizeof(uint8);
break;
case FT_STRING:
2009-06-07 15:29:55 -05:00
{
// fill only not filled entries
char** slot = (char**)(&dataTable[offset]);
if (!*slot || !**slot)
{
const char * st = getRecord(y).getString(x);
*slot=stringPool+(st-(const char*)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;
}
2009-06-07 15:29:55 -05:00
}
}
2009-10-17 15:51:44 -07:00
2009-06-07 15:29:55 -05:00
return stringPool;
}