summaryrefslogtreecommitdiffstats
path: root/Field.cpp
blob: c95c32da055f1042eafc7df50215ed6a8e77c0ad (plain) (blame)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <cmath>
#include "Field.hpp"

Field::Field() {
}

Field::Field(const Field &other) : m_dataLength(other.m_dataLength), m_type(other.m_type) {

    m_data = new byte[m_dataLength];
    std::copy(other.m_data,other.m_data+m_dataLength,m_data);
}

void Field::swap(Field &other) {
    std::swap(other.m_dataLength, m_dataLength);
    std::swap(other.m_data, m_data);
    std::swap(other.m_type, m_type);
}

Field &Field::operator=(Field other) {
    other.swap(*this);
    return *this;
}

Field::~Field() {
    Clear();
}

size_t Field::GetLength() {
    if (m_data != nullptr && m_dataLength == 0)
        throw 102;
    return m_dataLength;
}

void Field::Clear() {
    m_dataLength = 0;
    delete[] m_data;
    m_data = nullptr;
}

void Field::CopyToBuff(byte *ptr) {
    if (m_dataLength > 0)
        std::copy(m_data,m_data+m_dataLength,ptr);
}

void Field::SetRaw(byte *ptr, size_t len, FieldType type) {
    Clear();
    m_dataLength = len;
    m_type = type;
    m_data = new byte[m_dataLength];
    std::copy(ptr,ptr+m_dataLength,m_data);
}

int Field::GetVarInt() {

    size_t readed;
    return VarIntRead(m_data, readed);

}

void Field::SetVarInt(int value) {
    Clear();
    m_type = VarInt;
    m_data = new byte[5];
    m_dataLength = VarIntWrite(value, m_data);
}

int Field::GetInt() {
    int value = *(reinterpret_cast<int *>(m_data));
    endswap(&value);
    return value;
}

void Field::SetInt(int value) {
    Clear();
    m_type = Int;
    m_data = new byte[4];
    m_dataLength = 4;
    int *p = reinterpret_cast<int *>(m_data);
    *p = value;
    endswap(p);
}

bool Field::GetBool() {
    return *m_data != 0x00;
}

void Field::SetBool(bool value) {
    Clear();
    m_type = Boolean;
    m_data = new byte[1];
    m_dataLength = 1;
    *m_data = value ? 0x01 : 0x00;
}

unsigned short Field::GetUShort() {
    unsigned short *p = reinterpret_cast<unsigned short *>(m_data);
    unsigned short t = *p;
    endswap(&t);
    return t;
}

void Field::SetUShort(unsigned short value) {
    Clear();
    m_type = UnsignedShort;
    m_dataLength = 2;
    m_data = new byte[2];
    unsigned short *p = reinterpret_cast<unsigned short *>(m_data);
    *p = value;
    endswap(p);
}

std::string Field::GetString() {
    Field fLen;
    byte *ptr = m_data;
    size_t l;
    int val = VarIntRead(ptr, l);
    ptr += l;
    std::string s((char *) ptr, val);
    return s;
}

void Field::SetString(std::string value) {
    Clear();
    m_type = String;
    Field fLen;
    fLen.SetVarInt(value.size());
    m_dataLength = value.size() + fLen.GetLength();
    m_data = new byte[m_dataLength];
    byte *p = m_data;
    fLen.CopyToBuff(p);
    p += fLen.GetLength();
    std::copy(value.begin(),value.end(),p);
}

long long Field::GetLong() {
    long long t = *reinterpret_cast<long long *>(m_data);
    endswap(&t);
    return t;
}

void Field::SetLong(long long value) {
    Clear();
    m_type = Long;
    m_dataLength = 8;
    m_data = new byte[m_dataLength];
    long long *p = reinterpret_cast<long long *>(m_data);
    *p = value;
    endswap(p);
}

FieldType Field::GetType() {
    return m_type;
}

byte Field::GetUByte() {
    byte t = *reinterpret_cast<byte *>(m_data);
    endswap(&t);
    return t;
}

void Field::SetUByte(byte value) {
    Clear();
    m_type = UnsignedByte;
    endswap(&value);
    m_dataLength = 1;
    m_data = new byte[m_dataLength];
    byte *p = reinterpret_cast<byte *>(m_data);
    *p = value;
}

sbyte Field::GetByte() {
    sbyte t = *reinterpret_cast<sbyte *>(m_data);
    endswap(&t);
    return t;
}

void Field::SetByte(sbyte value) {
    Clear();
    m_type = Byte8_t;
    endswap(&value);
    m_dataLength = 1;
    m_data = new byte[m_dataLength];
    sbyte *p = reinterpret_cast<sbyte *>(m_data);
    *p = value;
}

float Field::GetFloat() {
    float t = *reinterpret_cast<float *>(m_data);
    endswap(&t);
    return t;
}

void Field::SetFloat(float value) {
    Clear();
    m_type = Float;
    endswap(&value);
    m_dataLength = 4;
    m_data = new byte[m_dataLength];
    float *p = reinterpret_cast<float *>(m_data);
    *p = value;
}

PositionI Field::GetPosition() {
    unsigned long long t = *reinterpret_cast<unsigned long long *>(m_data);
    endswap(&t);
    int x = t >> 38;
    int y = (t >> 26) & 0xFFF;
    int z = t << 38 >> 38;
    if (x >= pow(2, 25)) {
        x -= pow(2, 26);
    }
    if (y >= pow(2, 11)) {
        y -= pow(2, 12);
    }
    if (z >= pow(2, 25)) {
        z -= pow(2, 26);
    }
    PositionI val;
    val.SetX(x);
    val.setZ(z);
    val.SetY(y);
    return val;
}

void Field::SetPosition(PositionI value) {
    Clear();
    m_type = Position;
    m_dataLength = 8;
    m_data = new byte[m_dataLength];
    unsigned long long *t = reinterpret_cast<unsigned long long *>(m_data);
    unsigned long long x = ((unsigned long long) value.GetX()) << 38;
    unsigned long long y = ((unsigned long long) value.GetY()) << 26;
    unsigned long long z = value.GetZ();
    endswap(&x);
    endswap(&z);
    endswap(&y);
    *t = x | y | z;
}

double Field::GetDouble() {
    double t = *reinterpret_cast<double *>(m_data);
    endswap(&t);
    return t;
}

void Field::SetDouble(double value) {
    Clear();
    m_type = Double;
    endswap(&value);
    m_dataLength = 8;
    m_data = new byte[m_dataLength];
    double *p = reinterpret_cast<double *>(m_data);
    *p = value;
}

size_t Field::GetFieldLength(FieldType type) {
    switch (type) {
        case Unknown:
            return 0;
        case Boolean:
            return 1;
        case Byte8_t:
            return 1;
        case UnsignedByte:
            return 1;
        case Short:
            return 2;
        case UnsignedShort:
            return 2;
        case Int:
            return 4;
        case Long:
            return 8;
        case Float:
            return 4;
        case Double:
            return 8;
        case Position:
            return 8;
        case Angle:
            return 4;
        case Uuid:
            return 16;
        default:
            return 0;
    }
}

std::vector<Field> Field::GetArray() {
    /*std::vector<Field> vec;
    if (m_type<20){
        size_t fieldLen=GetFieldLength(m_type);
        byte* ptr = m_data;
        for (int i=0;i<m_dataLength/fieldLen;i++){
            Field f;
            f.SetRaw(ptr,fieldLen,m_type);
            vec.push_back(f);
            ptr+=fieldLen;
        }
        return vec;
    }*/
    return m_childs;
}

void Field::Attach(Field field) {
    m_childs.push_back(field);
}