summaryrefslogtreecommitdiff
path: root/src/common/DataStores/DBCFileLoader.h
blob: c6781d40fa4c4406f8027cdba06e397c43b2c751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation; either version 3 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 Affero 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/>.
 */

#ifndef DBC_FILE_LOADER_H
#define DBC_FILE_LOADER_H

#include "Define.h"
#include "Errors.h"
#include "Utilities/ByteConverter.h"

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)
};

class DBCFileLoader
{
public:
    DBCFileLoader();
    ~DBCFileLoader();

    bool Load(const char* filename, const char* fmt);

    class Record
    {
    public:
        [[nodiscard]] float getFloat(size_t field) const
        {
            ASSERT(field < file.fieldCount);
            float val = *reinterpret_cast<float*>(offset + file.GetOffset(field));
            EndianConvert(val);
            return val;
        }

        [[nodiscard]] uint32 getUInt(size_t field) const
        {
            ASSERT(field < file.fieldCount);
            uint32 val = *reinterpret_cast<uint32*>(offset + file.GetOffset(field));
            EndianConvert(val);
            return val;
        }

        [[nodiscard]] uint8 getUInt8(size_t field) const
        {
            ASSERT(field < file.fieldCount);
            return *reinterpret_cast<uint8*>(offset + file.GetOffset(field));
        }

        [[nodiscard]] 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);
        }

    private:
        Record(DBCFileLoader& file_, unsigned char* offset_): offset(offset_), file(file_) { }
        unsigned char* offset;
        DBCFileLoader& file;

        friend class DBCFileLoader;
    };

    // Get record by id
    Record getRecord(size_t id);

    [[nodiscard]] uint32 GetNumRows() const { return recordCount; }
    [[nodiscard]] uint32 GetRowSize() const { return recordSize; }
    [[nodiscard]] uint32 GetCols() const { return fieldCount; }
    [[nodiscard]] uint32 GetOffset(size_t id) const { return (fieldsOffset != nullptr && id < fieldCount) ? fieldsOffset[id] : 0; }
    [[nodiscard]] 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