File: esmwriter.hpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (214 lines) | stat: -rw-r--r-- 6,406 bytes parent folder | download
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
#ifndef OPENMW_ESM_WRITER_H
#define OPENMW_ESM_WRITER_H

#include <iosfwd>
#include <list>
#include <type_traits>

#include "components/esm/decompose.hpp"
#include "components/esm/esmcommon.hpp"
#include "components/esm/refid.hpp"

#include "loadtes3.hpp"

namespace ToUTF8
{
    class Utf8Encoder;
}

namespace ESM
{

    class ESMWriter
    {
        struct RecordData
        {
            NAME name;
            std::streampos position;
            uint32_t size;
        };

    public:
        ESMWriter();

        unsigned int getVersion() const;

        // Set various header data (Header::Data). All of the below functions must be called before writing,
        // otherwise this data will be left uninitialized.
        void setVersion(unsigned int ver = 0x3fa66666);
        void setType(int type);
        void setEncoder(ToUTF8::Utf8Encoder* encoding);
        void setAuthor(std::string_view author);
        void setDescription(std::string_view desc);
        void setHeader(const Header& value) { mHeader = value; }

        // Set the record count for writing it in the file header
        void setRecordCount(int count);
        // Counts how many records we have actually written.
        // It is a good idea to compare this with the value you wrote into the header (setRecordCount)
        // It should be the record count you set + 1 (1 additional record for the TES3 header)
        int getRecordCount() const { return mRecordCount; }

        FormatVersion getFormatVersion() const { return mHeader.mFormatVersion; }
        void setFormatVersion(FormatVersion value);

        void clearMaster();

        void addMaster(std::string_view name, uint64_t size);

        void save(std::ostream& file);
        ///< Start saving a file by writing the TES3 header.

        void close();
        ///< \note Does not close the stream.

        void writeHNString(NAME name, std::string_view data);
        void writeHNString(NAME name, std::string_view data, size_t size);
        void writeHNCString(NAME name, std::string_view data)
        {
            startSubRecord(name);
            writeHCString(data);
            endRecord(name);
        }
        void writeHNOString(NAME name, std::string_view data)
        {
            if (!data.empty())
                writeHNString(name, data);
        }
        void writeHNOCString(NAME name, std::string_view data)
        {
            if (!data.empty())
                writeHNCString(name, data);
        }

        void writeHNRefId(NAME name, RefId value);

        void writeHNRefId(NAME name, RefId value, std::size_t size);

        void writeHNCRefId(NAME name, RefId value)
        {
            startSubRecord(name);
            writeHCRefId(value);
            endRecord(name);
        }

        void writeHNORefId(NAME name, RefId value)
        {
            if (!value.empty())
                writeHNRefId(name, value);
        }

        void writeHNOCRefId(NAME name, RefId value)
        {
            if (!value.empty())
                writeHNCRefId(name, value);
        }

        void writeCellId(const ESM::RefId& cellId);

        template <typename T>
        void writeHNT(NAME name, const T& data)
        {
            startSubRecord(name);
            writeT(data);
            endRecord(name);
        }

        template <typename T, std::size_t size>
        void writeHNT(NAME name, const T (&data)[size])
        {
            startSubRecord(name);
            writeT(data);
            endRecord(name);
        }

        void writeNamedComposite(NAME name, const auto& value)
        {
            decompose(value, [&](const auto&... args) {
                startSubRecord(name);
                (writeT(args), ...);
                endRecord(name);
            });
        }

        void writeComposite(const auto& value)
        {
            decompose(value, [&](const auto&... args) { (writeT(args), ...); });
        }

        // Prevent using writeHNT with strings. This already happened by accident and results in
        // state being discarded without any error on writing or reading it. :(
        // writeHNString and friends must be used instead.
        void writeHNT(NAME name, const std::string& data) = delete;
        void writeHNT(NAME name, std::string_view data) = delete;

        void writeT(NAME data) = delete;

        template <typename T, std::size_t size>
        void writeHNT(NAME name, const T (&data)[size], int) = delete;

        template <typename T>
        void writeHNT(NAME name, const T& data, std::size_t size)
        {
            startSubRecord(name);
            writeT(data, size);
            endRecord(name);
        }

        template <typename T>
        void writeT(const T& data)
        {
            static_assert(!std::is_pointer_v<T>);
            write(reinterpret_cast<const char*>(&data), sizeof(T));
        }

        template <typename T, std::size_t size>
        void writeT(const T (&data)[size])
        {
            write(reinterpret_cast<const char*>(data), size * sizeof(T));
        }

        template <typename T>
        void writeT(const T& data, size_t size)
        {
            static_assert(!std::is_pointer_v<T>);
            write((char*)&data, size);
        }

        void startRecord(NAME name, uint32_t flags = 0);
        void startRecord(uint32_t name, uint32_t flags = 0);
        /// @note Sub-record hierarchies are not properly supported in ESMReader. This should be fixed later.
        void startSubRecord(NAME name);
        void endRecord(NAME name);
        void endRecord(uint32_t name);
        void writeMaybeFixedSizeString(const std::string& data, std::size_t size);
        void writeHString(std::string_view data);
        void writeHCString(std::string_view data);

        void writeMaybeFixedSizeRefId(RefId value, std::size_t size);

        void writeHRefId(RefId refId);

        void writeHCRefId(RefId refId);

        void writeName(NAME data);

        void write(const char* data, size_t size);

        void writeFormId(const ESM::FormId&, bool wide = false, NAME tag = "FRMR");

    private:
        std::list<RecordData> mRecords;
        std::ostream* mStream;
        std::streampos mHeaderPos;
        ToUTF8::Utf8Encoder* mEncoder;
        int mRecordCount;
        bool mCounting;

        Header mHeader;

        void writeRefId(RefId value);
    };
}

#endif