TrinityCore
DB2FileLoader.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef DB2_FILE_LOADER_H
19#define DB2_FILE_LOADER_H
20
21#include "Common.h"
22#include <exception>
23#include <string>
24
26struct DB2FieldMeta;
27struct DB2Meta;
28
29#pragma pack(push, 1)
30
32{
35 std::array<char, 128> Schema;
54};
55
57{
67};
68
69#pragma pack(pop)
70
71inline constinit uint64 DUMMY_KNOWN_TACT_ID = 0x5452494E49545900; // TRINITY
72
74{
77 char const* Name;
78};
79
81{
82 constexpr explicit DB2FileLoadInfo(DB2FieldMeta const* fields, std::size_t fieldCount, DB2Meta const* meta)
83 : Fields(fields), FieldCount(fieldCount), Meta(meta) { }
84
85 uint32 GetStringFieldCount(bool localizedOnly) const;
86 std::pair<int32/*fieldIndex*/, int32/*arrayIndex*/> GetFieldIndexByName(char const* fieldName) const;
87 int32 GetFieldIndexByMetaIndex(uint32 metaIndex) const;
88
90 std::size_t FieldCount;
91 DB2Meta const* Meta;
92};
93
95{
96 Skip,
98};
99
101{
103 DB2FileSource(DB2FileSource const& other) = delete;
104 DB2FileSource(DB2FileSource&& other) noexcept = delete;
105 DB2FileSource& operator=(DB2FileSource const& other) = delete;
106 DB2FileSource& operator=(DB2FileSource&& other) noexcept = delete;
107
108 virtual ~DB2FileSource();
109
110 // Returns true when the source is open for reading
111 virtual bool IsOpen() const = 0;
112
113 // Reads numBytes bytes from source and places them into buffer
114 // Returns true if numBytes was read successfully
115 virtual bool Read(void* buffer, std::size_t numBytes) = 0;
116
117 // Returns current read position in file
118 virtual int64 GetPosition() const = 0;
119
120 virtual bool SetPosition(int64 position) = 0;
121
122 virtual int64 GetFileSize() const = 0;
123
124 virtual char const* GetFileName() const = 0;
125
127};
128
130{
131public:
132 DB2Record(DB2FileLoaderImpl const& db2, uint32 recordIndex, std::size_t* fieldOffsets);
133 DB2Record(DB2Record const& other);
134 DB2Record(DB2Record&& other) noexcept;
135 DB2Record& operator=(DB2Record const& other) = delete;
136 DB2Record& operator=(DB2Record&& other) noexcept = delete;
137 ~DB2Record();
138
139 explicit operator bool() const;
140
141 uint32 GetId() const;
142
143 uint8 GetUInt8(uint32 field, uint32 arrayIndex) const;
144 uint8 GetUInt8(char const* fieldName) const;
145 uint16 GetUInt16(uint32 field, uint32 arrayIndex) const;
146 uint16 GetUInt16(char const* fieldName) const;
147 uint32 GetUInt32(uint32 field, uint32 arrayIndex) const;
148 uint32 GetUInt32(char const* fieldName) const;
149 int32 GetInt32(uint32 field, uint32 arrayIndex) const;
150 int32 GetInt32(char const* fieldName) const;
151 uint64 GetUInt64(uint32 field, uint32 arrayIndex) const;
152 uint64 GetUInt64(char const* fieldName) const;
153 float GetFloat(uint32 field, uint32 arrayIndex) const;
154 float GetFloat(char const* fieldName) const;
155 char const* GetString(uint32 field, uint32 arrayIndex) const;
156 char const* GetString(char const* fieldName) const;
157
158 // Creates its own heap allocated copy of _fieldOffsets
159 // by default _fieldOffets point to a shared array inside Loader to avoid heap allocations
160 // meaning that only one instance of DB2Record has valid offsets if the file is sparse
161 void MakePersistent();
162
163private:
166 unsigned char const* _recordData;
167 std::size_t* _fieldOffsets;
168};
169
170#pragma pack(push, 1)
172{
175};
176#pragma pack(pop)
177
178class TC_COMMON_API DB2FileLoadException : public std::exception
179{
180public:
181 DB2FileLoadException(std::string msg) : _msg(std::move(msg)) { }
182
183 char const* what() const noexcept override { return _msg.c_str(); }
184
185private:
186 std::string _msg;
187};
188
190{
191public:
193 DB2FileLoader(DB2FileLoader const& other) = delete;
194 DB2FileLoader(DB2FileLoader&& other) noexcept = delete;
195 DB2FileLoader& operator=(DB2FileLoader const& other) = delete;
196 DB2FileLoader& operator=(DB2FileLoader&& other) noexcept = delete;
198
199 // loadInfo argument is required when trying to read data from the file
200 void LoadHeaders(DB2FileSource* source, DB2FileLoadInfo const* loadInfo);
201 void Load(DB2FileSource* source, DB2FileLoadInfo const* loadInfo);
202 char* AutoProduceData(uint32& indexTableSize, char**& indexTable);
203 char* AutoProduceStrings(char** indexTable, uint32 indexTableSize, LocaleConstant locale);
204 void AutoProduceRecordCopies(uint32 records, char** indexTable, char* dataTable);
205
206 uint32 GetCols() const { return _header.TotalFieldCount; }
207 uint32 GetRecordCount() const;
208 uint32 GetRecordCopyCount() const;
209 uint32 GetTableHash() const { return _header.TableHash; }
210 uint32 GetLayoutHash() const { return _header.LayoutHash; }
211 uint32 GetMinId() const;
212 uint32 GetMaxId() const;
213
214 DB2Header const& GetHeader() const { return _header; }
215 DB2SectionHeader const& GetSectionHeader(uint32 section) const;
216 DB2Record GetRecord(uint32 recordNumber) const;
217 DB2RecordCopy GetRecordCopy(uint32 copyNumber) const;
218
219private:
222};
223
224#endif
LocaleConstant
Definition: Common.h:48
DB2EncryptedSectionHandling
Definition: DB2FileLoader.h:95
constinit uint64 DUMMY_KNOWN_TACT_ID
Definition: DB2FileLoader.h:71
uint8_t uint8
Definition: Define.h:144
int64_t int64
Definition: Define.h:137
#define TC_COMMON_API
Definition: Define.h:99
int16_t int16
Definition: Define.h:139
int32_t int32
Definition: Define.h:138
uint64_t uint64
Definition: Define.h:141
uint16_t uint16
Definition: Define.h:143
DBCFormer
Definition: Define.h:147
uint32_t uint32
Definition: Define.h:142
DB2FileLoadException(std::string msg)
char const * what() const noexcept override
DB2Header _header
DB2Header const & GetHeader() const
DB2FileLoaderImpl * _impl
uint32 GetTableHash() const
DB2FileLoader & operator=(DB2FileLoader &&other) noexcept=delete
DB2FileLoader(DB2FileLoader &&other) noexcept=delete
uint32 GetCols() const
uint32 GetLayoutHash() const
DB2FileLoader(DB2FileLoader const &other)=delete
DB2FileLoader & operator=(DB2FileLoader const &other)=delete
DB2Record & operator=(DB2Record &&other) noexcept=delete
DB2FileLoaderImpl const & _db2
std::size_t * _fieldOffsets
unsigned char const * _recordData
uint32 _recordIndex
DB2Record & operator=(DB2Record const &other)=delete
TC_GAME_API uint32 GetId(std::string_view username)
STL namespace.
char const * Name
Definition: DB2FileLoader.h:77
DBCFormer Type
Definition: DB2FileLoader.h:76
std::size_t FieldCount
Definition: DB2FileLoader.h:90
DB2Meta const * Meta
Definition: DB2FileLoader.h:91
constexpr DB2FileLoadInfo(DB2FieldMeta const *fields, std::size_t fieldCount, DB2Meta const *meta)
Definition: DB2FileLoader.h:82
DB2FieldMeta const * Fields
Definition: DB2FileLoader.h:89
DB2FileSource & operator=(DB2FileSource const &other)=delete
virtual int64 GetFileSize() const =0
virtual DB2EncryptedSectionHandling HandleEncryptedSection(DB2SectionHeader const &sectionHeader) const =0
DB2FileSource & operator=(DB2FileSource &&other) noexcept=delete
virtual char const * GetFileName() const =0
virtual bool IsOpen() const =0
DB2FileSource(DB2FileSource &&other) noexcept=delete
virtual bool SetPosition(int64 position)=0
virtual ~DB2FileSource()
virtual bool Read(void *buffer, std::size_t numBytes)=0
DB2FileSource(DB2FileSource const &other)=delete
virtual int64 GetPosition() const =0
uint32 TotalFieldCount
Definition: DB2FileLoader.h:47
std::array< char, 128 > Schema
Definition: DB2FileLoader.h:35
uint32 SectionCount
Definition: DB2FileLoader.h:53
uint32 RecordCount
Definition: DB2FileLoader.h:36
uint32 PackedDataOffset
Definition: DB2FileLoader.h:48
uint32 CommonDataSize
Definition: DB2FileLoader.h:51
uint32 StringTableSize
Definition: DB2FileLoader.h:39
uint32 MaxId
Definition: DB2FileLoader.h:43
uint32 PalletDataSize
Definition: DB2FileLoader.h:52
uint32 ColumnMetaSize
Definition: DB2FileLoader.h:50
uint32 Signature
Definition: DB2FileLoader.h:33
uint16 Flags
Definition: DB2FileLoader.h:45
uint32 TableHash
Definition: DB2FileLoader.h:40
uint32 FieldCount
Definition: DB2FileLoader.h:37
uint32 LayoutHash
Definition: DB2FileLoader.h:41
uint32 RecordSize
Definition: DB2FileLoader.h:38
int16 IndexField
Definition: DB2FileLoader.h:46
uint32 ParentLookupCount
Definition: DB2FileLoader.h:49
uint32 MinId
Definition: DB2FileLoader.h:42
uint32 Version
Definition: DB2FileLoader.h:34
uint32 Locale
Definition: DB2FileLoader.h:44
uint32 SourceRowId
uint32 StringTableSize
Definition: DB2FileLoader.h:61
uint32 CatalogDataCount
Definition: DB2FileLoader.h:65
uint32 ParentLookupDataSize
Definition: DB2FileLoader.h:64
uint32 CatalogDataOffset
Definition: DB2FileLoader.h:62