1/*
2 * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#ifndef BINARYTREE_H
24#define BINARYTREE_H
25
26#include "declarations.h"
27#include <framework/util/databuffer.h>
28
29enum {
30 BINARYTREE_ESCAPE_CHAR = 0xFD,
31 BINARYTREE_NODE_START = 0xFE,
32 BINARYTREE_NODE_END = 0xFF
33};
34
35class BinaryTree : public stdext::shared_object
36{
37public:
38 BinaryTree(const FileStreamPtr& fin);
39 ~BinaryTree();
40
41 void seek(uint pos);
42 void skip(uint len);
43 uint tell() { return m_pos; }
44 uint size() { unserialize(); return m_buffer.size(); }
45
46 uint8 getU8();
47 uint16 getU16();
48 uint32 getU32();
49 uint64 getU64();
50 std::string getString(uint16 len = 0);
51 Point getPoint();
52
53 BinaryTreeVec getChildren();
54 bool canRead() { unserialize(); return m_pos < m_buffer.size(); }
55
56private:
57 void unserialize();
58 void skipNodes();
59
60 FileStreamPtr m_fin;
61 DataBuffer<uint8> m_buffer;
62 uint m_pos;
63 uint m_startPos;
64};
65
66class OutputBinaryTree : public stdext::shared_object
67{
68public:
69 OutputBinaryTree(const FileStreamPtr& finish);
70
71 void addU8(uint8 v);
72 void addU16(uint16 v);
73 void addU32(uint32 v);
74 void addString(const std::string& v);
75 void addPos(uint16 x, uint16 y, uint8 z);
76 void addPoint(const Point& point);
77
78 void startNode(uint8 node);
79 void endNode();
80
81private:
82 FileStreamPtr m_fin;
83
84protected:
85 void write(const uint8* data, size_t size);
86};
87
88#endif
89
90