forked from kenygia/MooseEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLsxWriter.cpp
148 lines (143 loc) · 4.91 KB
/
LsxWriter.cpp
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
#include "LsxWriter.h"
#include <stack>
#include <sstream>
#include <cstring>
#include <ctime>
long LsxWriter::writeSingleObject(LsbObject *object, std::ostream& output) {
std::ostringstream ss;
long idx = object->getIndex();
long type = object->getType();
bool isStr = false;
bool isUnicodeStr = false;
bool isLocalizedStr = false;
if (!object->isDirectory()) {
if (type == 0x16 || type == 0x17 || type == 0x19 || type == 0x1E)
isStr = true;
if (type == 0x1C) {
isLocalizedStr = true;
}
if (type == 0x1E) {
isUnicodeStr = true;
}
}
else {
std::vector<LsbObject *> items = object->getItemsOnly();
std::vector<LsbObject *> dirs = object->getDirectoriesOnly();
type = items.size();
long dirCount = dirs.size();
object->setData((char *)&dirCount, sizeof(long));
}
ss.write((char *)&idx, sizeof(long));\
ss.write((char *)&type, sizeof(long));
if (isStr) {
long dataSize = object->getDataSize();
if (isUnicodeStr) {
dataSize /= 2;
}
ss.write((char *)&dataSize, sizeof(long));
}
if (isLocalizedStr) {
char *firstString = object->getData();
long firstStringLength = strlen(firstString);
long firstStringBytes = firstStringLength + 2;
ss.write((char *)&(firstStringBytes), sizeof(long));
ss.write(firstString, firstStringBytes);
char *secondString = object->getData() + firstStringLength + 2;
long secondStringBytes = object->getDataSize() - firstStringBytes;
ss.write((char *)&(secondStringBytes), sizeof(long));
ss.write(secondString, secondStringBytes);
}
else {
ss.write(object->getData(), object->getDataSize());
}
std::string result = ss.str();
long size = result.size();
//output.write(result.data(), result.size());
if (!object->isDirectory()) {
output<<"\t<attribute id=\""<<object->getName()<<" value=\""<<object->toString()<<"\" type=\""<<type<<"\" />";
}
return size;
}
bool LsxWriter::writeFile(std::vector<LsbObject *>& objects, std::vector<TAG_LSB *>& tagList, std::ostream& output) {
long bytesWritten = 0;
HEADER_LSB header;
memset(&header, 0, sizeof(HEADER_LSB));
header.magic = 0x40000000;
header.fileLength = 0x0;
header.fileTimestamp = time(0);
header.verMajor = 2;
header.verMinor = 0;
header.verBuild = 9;
header.verRevision = 0;
header.tagCount = tagList.size();
output<<"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
output<<"<save>\n";
output<<"\t<header version=\"2\" time=\""<<header.fileTimestamp<<"\" />\n";
output<<"\t<version major=\""<<header.verMajor<<"\" minor=\""<<header.verMinor<<"\" revision=\""<<header.verRevision<<"\" build=\""<<header.verBuild<<"\" />\n";
bytesWritten += sizeof(HEADER_LSB);
// for (int i=0; i<tagList.size(); ++i) {
// output.write((char *)&tagList[i]->tagLength, sizeof(tagList[i]->tagLength));
// bytesWritten += sizeof(tagList[i]->tagLength);
// output.write(tagList[i]->tag, tagList[i]->tagLength);
// bytesWritten += tagList[i]->tagLength;
// output.write((char *)&tagList[i]->index, sizeof(tagList[i]->index));
// bytesWritten += sizeof(tagList[i]->index);
// }
long dirCount = objects.size();
if (writerProgressCallback != 0) {
writerProgressCallback->onSaveBegin(dirCount);
}
//output.write((char *)&dirCount, sizeof(dirCount));
bytesWritten += sizeof(dirCount);
long dirHeaderOffset = output.tellp();
for (int i=0; i<objects.size(); ++i) {
ENTITY_HEADER_LSB dirHeader;
dirHeader.id = objects[i]->getIndex();
dirHeader.dataOffset = 0x0;
//output.write((char *)&dirHeader, sizeof(ENTITY_HEADER_LSB));
bytesWritten += sizeof(ENTITY_HEADER_LSB);
}
std::vector<long> headerOffsets;
for (int i=0; i<objects.size(); ++i) {
headerOffsets.push_back(output.tellp());
std::stack<LsbObject *> dirStack;
dirStack.push(objects[i]);
while (!dirStack.empty()) {
LsbObject *currentDirectory = dirStack.top();
dirStack.pop();
if (!currentDirectory->isEntity())
bytesWritten += writeSingleObject(currentDirectory, output);
else {
output<<"\t<region id=\""<<currentDirectory->getName()<<"\">";
}
std::vector<LsbObject *> items = currentDirectory->getItemsOnly();
std::vector<LsbObject *> dirs = currentDirectory->getDirectoriesOnly();
for (int j=0; j<items.size(); ++j) {
bytesWritten += writeSingleObject(items[j], output);
}
for (int j=dirs.size() - 1; j>=0; --j) {
dirStack.push(dirs[j]);
}
}
if (writerProgressCallback != 0) {
writerProgressCallback->onSaveUpdate(objects.size() - (i + 1));
}
}
output.seekp((long)&header.fileLength - (long)&header, std::ios_base::beg);
header.fileLength = bytesWritten;
output.write((char *)&header.fileLength, sizeof(header.fileLength));
output.seekp(dirHeaderOffset, std::ios_base::beg);
for (int i=0; i<objects.size(); ++i) {
ENTITY_HEADER_LSB dirHeader;
dirHeader.id = objects[i]->getIndex();
dirHeader.dataOffset = headerOffsets[i];
output.write((char *)&dirHeader, sizeof(ENTITY_HEADER_LSB));
}
if (writerProgressCallback != 0) {
writerProgressCallback->onSaveEnd();
}
return true;
}
LsxWriter::LsxWriter()
{
}