DDraceNetwork Docs
datafile.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SHARED_DATAFILE_H
4#define ENGINE_SHARED_DATAFILE_H
5
6#include <engine/storage.h>
7
8#include <base/hash.h>
9#include <base/types.h>
10
11#include "uuid_manager.h"
12
13#include <cstdint>
14#include <map>
15#include <vector>
16
17enum
18{
19 ITEMTYPE_EX = 0xffff,
20};
21
22// raw datafile access
24{
26 void *GetDataImpl(int Index, bool Swap);
27 int GetFileDataSize(int Index) const;
28
29 int GetExternalItemType(int InternalType, CUuid *pUuid);
30 int GetInternalItemType(int ExternalType);
31
32public:
34 m_pDataFile(nullptr) {}
36
38 {
39 m_pDataFile = Other.m_pDataFile;
40 Other.m_pDataFile = nullptr;
41 return *this;
42 }
43
44 bool Open(class IStorage *pStorage, const char *pFilename, int StorageType);
45 bool Close();
46 bool IsOpen() const { return m_pDataFile != nullptr; }
47 IOHANDLE File() const;
48
49 int GetDataSize(int Index) const;
50 void *GetData(int Index);
51 void *GetDataSwapped(int Index); // makes sure that the data is 32bit LE ints when saved
52 const char *GetDataString(int Index);
53 void ReplaceData(int Index, char *pData, size_t Size); // memory for data must have been allocated with malloc
54 void UnloadData(int Index);
55 int NumData() const;
56
57 int GetItemSize(int Index) const;
58 void *GetItem(int Index, int *pType = nullptr, int *pId = nullptr, CUuid *pUuid = nullptr);
59 void GetType(int Type, int *pStart, int *pNum);
60 int FindItemIndex(int Type, int Id);
61 void *FindItem(int Type, int Id);
62 int NumItems() const;
63
64 SHA256_DIGEST Sha256() const;
65 unsigned Crc() const;
66 int MapSize() const;
67};
68
69// write access
71{
72public:
74 {
77 };
78
79private:
80 struct CDataInfo
81 {
87 };
88
89 struct CItemInfo
90 {
91 int m_Type;
92 int m_Id;
93 int m_Size;
94 int m_Next;
95 int m_Prev;
96 void *m_pData;
97 };
98
100 {
101 int m_Num = 0;
102 int m_First = -1;
103 int m_Last = -1;
104 };
105
107 {
110 };
111
112 enum
113 {
114 MAX_ITEM_TYPES = 0x10000,
115 };
116
118 std::map<uint16_t, CItemTypeInfo, std::less<>> m_ItemTypes; // item types must be sorted in ascending order
119 std::vector<CItemInfo> m_vItems;
120 std::vector<CDataInfo> m_vDatas;
121 std::vector<CExtendedItemType> m_vExtendedItemTypes;
122
123 int GetTypeFromIndex(int Index) const;
124 int GetExtendedItemTypeIndex(int Type, const CUuid *pUuid);
125
126public:
129 {
130 m_File = Other.m_File;
131 Other.m_File = 0;
132 m_ItemTypes = std::move(Other.m_ItemTypes);
133 m_vItems = std::move(Other.m_vItems);
134 m_vDatas = std::move(Other.m_vDatas);
135 m_vExtendedItemTypes = std::move(Other.m_vExtendedItemTypes);
136 }
138
139 bool Open(class IStorage *pStorage, const char *pFilename, int StorageType = IStorage::TYPE_SAVE);
140 int AddItem(int Type, int Id, size_t Size, const void *pData, const CUuid *pUuid = nullptr);
141 int AddData(size_t Size, const void *pData, ECompressionLevel CompressionLevel = COMPRESSION_DEFAULT);
142 int AddDataSwapped(size_t Size, const void *pData);
143 int AddDataString(const char *pStr);
144 void Finish();
145};
146
147#endif
Definition: datafile.h:24
int GetItemSize(int Index) const
Definition: datafile.cpp:446
void * GetItem(int Index, int *pType=nullptr, int *pId=nullptr, CUuid *pUuid=nullptr)
Definition: datafile.cpp:502
int GetFileDataSize(int Index) const
Definition: datafile.cpp:274
struct CDatafile * m_pDataFile
Definition: datafile.h:25
bool Open(class IStorage *pStorage, const char *pFilename, int StorageType)
Definition: datafile.cpp:103
void GetType(int Type, int *pStart, int *pNum)
Definition: datafile.cpp:521
int FindItemIndex(int Type, int Id)
Definition: datafile.cpp:540
int GetInternalItemType(int ExternalType)
Definition: datafile.cpp:478
void UnloadData(int Index)
Definition: datafile.cpp:434
bool Close()
Definition: datafile.cpp:240
IOHANDLE File() const
Definition: datafile.cpp:259
int GetDataSize(int Index) const
Definition: datafile.cpp:285
bool IsOpen() const
Definition: datafile.h:46
void ReplaceData(int Index, char *pData, size_t Size)
Definition: datafile.cpp:424
SHA256_DIGEST Sha256() const
Definition: datafile.cpp:575
unsigned Crc() const
Definition: datafile.cpp:582
void * FindItem(int Type, int Id)
Definition: datafile.cpp:558
int NumData() const
Definition: datafile.cpp:266
void * GetData(int Index)
Definition: datafile.cpp:399
int GetExternalItemType(int InternalType, CUuid *pUuid)
Definition: datafile.cpp:455
int MapSize() const
Definition: datafile.cpp:589
int NumItems() const
Definition: datafile.cpp:568
CDataFileReader()
Definition: datafile.h:33
~CDataFileReader()
Definition: datafile.h:35
void * GetDataSwapped(int Index)
Definition: datafile.cpp:404
const char * GetDataString(int Index)
Definition: datafile.cpp:409
void * GetDataImpl(int Index, bool Swap)
Definition: datafile.cpp:311
CDataFileReader & operator=(CDataFileReader &&Other)
Definition: datafile.h:37
Definition: datafile.h:71
@ MAX_ITEM_TYPES
Definition: datafile.h:114
~CDataFileWriter()
Definition: datafile.cpp:601
ECompressionLevel
Definition: datafile.h:74
@ COMPRESSION_DEFAULT
Definition: datafile.h:75
@ COMPRESSION_BEST
Definition: datafile.h:76
CDataFileWriter()
Definition: datafile.cpp:596
std::vector< CDataInfo > m_vDatas
Definition: datafile.h:120
void Finish()
Definition: datafile.cpp:770
int AddDataSwapped(size_t Size, const void *pData)
Definition: datafile.cpp:729
int GetTypeFromIndex(int Index) const
Definition: datafile.cpp:628
std::map< uint16_t, CItemTypeInfo, std::less<> > m_ItemTypes
Definition: datafile.h:118
CDataFileWriter(CDataFileWriter &&Other)
Definition: datafile.h:128
bool Open(class IStorage *pStorage, const char *pFilename, int StorageType=IStorage::TYPE_SAVE)
Definition: datafile.cpp:621
int AddDataString(const char *pStr)
Definition: datafile.cpp:747
int GetExtendedItemTypeIndex(int Type, const CUuid *pUuid)
Definition: datafile.cpp:633
int AddData(size_t Size, const void *pData, ECompressionLevel CompressionLevel=COMPRESSION_DEFAULT)
Definition: datafile.cpp:712
std::vector< CItemInfo > m_vItems
Definition: datafile.h:119
int AddItem(int Type, int Id, size_t Size, const void *pData, const CUuid *pUuid=nullptr)
Definition: datafile.cpp:667
IOHANDLE m_File
Definition: datafile.h:117
std::vector< CExtendedItemType > m_vExtendedItemTypes
Definition: datafile.h:121
Definition: storage.h:20
@ TYPE_SAVE
Definition: storage.h:25
@ ITEMTYPE_EX
Definition: datafile.h:19
void * IOHANDLE
Definition: logger.h:11
Definition: datafile.h:81
int m_CompressedSize
Definition: datafile.h:85
int m_UncompressedSize
Definition: datafile.h:83
void * m_pCompressedData
Definition: datafile.h:84
void * m_pUncompressedData
Definition: datafile.h:82
ECompressionLevel m_CompressionLevel
Definition: datafile.h:86
Definition: datafile.h:107
int m_Type
Definition: datafile.h:108
CUuid m_Uuid
Definition: datafile.h:109
Definition: datafile.h:90
int m_Prev
Definition: datafile.h:95
int m_Next
Definition: datafile.h:94
int m_Id
Definition: datafile.h:92
int m_Size
Definition: datafile.h:93
void * m_pData
Definition: datafile.h:96
int m_Type
Definition: datafile.h:91
Definition: datafile.h:100
int m_Last
Definition: datafile.h:103
int m_Num
Definition: datafile.h:101
int m_First
Definition: datafile.h:102
Definition: datafile.cpp:91
Definition: uuid_manager.h:17
Definition: hash.h:15