-
Notifications
You must be signed in to change notification settings - Fork 56
/
AddonReader.h
190 lines (161 loc) · 3.92 KB
/
AddonReader.h
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef ADDONREADER_H
#define ADDONREADER_H
#include <stdint.h>
#include "Bootil/Bootil.h"
#include "AddonFormat.h"
//
//
//
//
namespace Addon
{
class Reader
{
public:
Reader()
{
Clear();
}
//
// Load an addon (call Parse after this succeeds)
//
bool ReadFromFile( Bootil::BString strName )
{
m_buffer.Clear();
return Bootil::File::Read( strName, m_buffer );
}
//
// Parse the addon into this class
//
bool Parse()
{
m_buffer.SetPos( 0 );
// Ident
if ( m_buffer.ReadType<char>() != 'G' ||
m_buffer.ReadType<char>() != 'M' ||
m_buffer.ReadType<char>() != 'A' ||
m_buffer.ReadType<char>() != 'D' )
{
return false;
}
//
// Format Version
//
m_fmtversion = m_buffer.ReadType<char>();
if ( m_fmtversion > Addon::Version )
return false;
m_buffer.ReadType<uint64_t>(); // steamid
m_buffer.ReadType<uint64_t>(); // timestamp
//
// Required content (not used at the moment, just read out)
//
if ( m_fmtversion > 1 )
{
Bootil::BString strContent = m_buffer.ReadString();
while ( !strContent.empty() )
{
strContent = m_buffer.ReadString();
}
}
m_name = m_buffer.ReadString();
m_desc = m_buffer.ReadString();
m_author = m_buffer.ReadString();
//
// Addon version - unused
//
m_buffer.ReadType<int32_t>();
//
// File index
//
int32_t iFileNumber = 1;
int64_t iOffset = 0;
while ( m_buffer.ReadType<uint32_t>() != 0 )
{
Addon::FileEntry entry;
entry.strName = m_buffer.ReadString();
entry.iSize = m_buffer.ReadType<int64_t>();
entry.iCRC = m_buffer.ReadType<uint32_t>();
entry.iOffset = iOffset;
entry.iFileNumber = iFileNumber;
m_index.push_back( entry );
iOffset += entry.iSize;
iFileNumber++;
}
m_fileblock = m_buffer.GetPos();
//
// Try to parse the description
//
Bootil::Data::Tree json;
if ( Bootil::Data::Json::Import( json, m_desc.c_str() ) )
{
m_desc = json.ChildValue( "description" );
m_type = json.ChildValue( "type" );
Bootil::Data::Tree& tags = json.GetChild( "tags" );
BOOTIL_FOREACH( tag, tags.Children(), Bootil::Data::Tree::List )
{
m_tags.push_back( tag->Value() );
}
}
return true;
}
//
// Return the FileEntry for a FileID
//
bool GetFile( uint32_t iFileID, Addon::FileEntry& outfile )
{
BOOTIL_FOREACH( file, m_index, Addon::FileEntry::List )
{
if ( file->iFileNumber == iFileID )
{
outfile = *file;
return true;
}
}
return false;
}
//
// Read a file id from the addon into the buffer
//
bool ReadFile( uint32_t iFileID, Bootil::Buffer& buffer )
{
Addon::FileEntry file;
if ( !GetFile( iFileID, file ) ) return false;
buffer.Write( m_buffer.GetBase( m_fileblock + file.iOffset ), file.iSize );
return true;
}
//
// Clears the addon's variables, frees all memory
//
void Clear()
{
m_buffer.Clear();
m_fmtversion = 0;
m_name.clear();
m_author.clear();
m_desc.clear();
m_index.clear();
m_type.clear();
m_fileblock = 0;
m_tags.clear();
}
const Addon::FileEntry::List& GetList() { return m_index; }
uint32_t GetFormatVersion() { return m_fmtversion; }
const Bootil::Buffer& GetBuffer() { return m_buffer; }
Bootil::BString Title() { return m_name; }
Bootil::BString Description() { return m_desc; }
Bootil::BString Author() { return m_author; }
const Bootil::BString& Type() { return m_type; }
Bootil::String::List& Tags() { return m_tags; }
protected:
Bootil::AutoBuffer m_buffer;
char m_fmtversion;
Bootil::BString m_name;
Bootil::BString m_author;
Bootil::BString m_desc;
Bootil::BString m_type;
Addon::FileEntry::List m_index;
uint32_t m_fileblock;
Bootil::String::List m_tags;
};
}
#endif