/
MapPropertyInfoFileModule.cpp
136 lines (107 loc) · 2.94 KB
/
MapPropertyInfoFileModule.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
#include "MapPropertyInfoFileModule.h"
#include "itextstream.h"
#include "parser/DefTokeniser.h"
#include "string/replace.h"
namespace map
{
namespace
{
const char* const MAP_PROPERTIES = "MapProperties";
const char* const KEY_VALUE = "KeyValue";
}
std::string MapPropertyInfoFileModule::getName()
{
return "Map Properties";
}
void MapPropertyInfoFileModule::clear()
{
_store.clearProperties();
}
void MapPropertyInfoFileModule::onInfoFileSaveStart()
{
clear();
}
void MapPropertyInfoFileModule::onBeginSaveMap(const scene::IMapRootNodePtr& root)
{
// Load all the properties from the map root into a local store
root->foreachProperty([this](const std::string& key, const std::string& value)
{
_store.setProperty(key, value);
});
}
void MapPropertyInfoFileModule::onFinishSaveMap(const scene::IMapRootNodePtr& root)
{}
void MapPropertyInfoFileModule::onSavePrimitive(const scene::INodePtr& node, std::size_t entityNum, std::size_t primitiveNum)
{}
void MapPropertyInfoFileModule::onSaveEntity(const scene::INodePtr& node, std::size_t entityNum)
{}
void MapPropertyInfoFileModule::writeBlocks(std::ostream& stream)
{
// Block output
stream << "\t" << MAP_PROPERTIES << std::endl;
stream << "\t{" << std::endl;
_store.foreachProperty([&](const std::string& key, const std::string& value)
{
stream << "\t\t" << KEY_VALUE << " { " <<
"\"" << string::replace_all_copy(key, "\"", """) << "\"" <<
" " <<
"\"" << string::replace_all_copy(value, "\"", """) << "\"" <<
" } " << std::endl;
});
stream << "\t}" << std::endl;
rMessage() << "Map Properties written." << std::endl;
}
void MapPropertyInfoFileModule::onInfoFileSaveFinished()
{
clear();
}
void MapPropertyInfoFileModule::onInfoFileLoadStart()
{
clear();
}
bool MapPropertyInfoFileModule::canParseBlock(const std::string& blockName)
{
return blockName == MAP_PROPERTIES;
}
void MapPropertyInfoFileModule::parseBlock(const std::string& blockName, parser::DefTokeniser& tok)
{
// The opening brace
tok.assertNextToken("{");
int blockLevel = 1;
while (tok.hasMoreTokens() && blockLevel > 0)
{
std::string token = tok.nextToken();
if (token == KEY_VALUE)
{
// KeyValue { "Key" "Value" }
tok.assertNextToken("{");
std::string key = tok.nextToken();
std::string value = tok.nextToken();
string::replace_all(key, """, "\"");
string::replace_all(value, """, "\"");
_store.setProperty(key, value);
tok.assertNextToken("}");
}
else if (token == "{")
{
blockLevel++;
}
else if (token == "}")
{
blockLevel--;
}
}
}
void MapPropertyInfoFileModule::applyInfoToScene(const scene::IMapRootNodePtr& root, const NodeIndexMap& nodeMap)
{
_store.foreachProperty([&](const std::string& key, const std::string& value)
{
root->setProperty(key, value);
});
}
void MapPropertyInfoFileModule::onInfoFileLoadFinished()
{
rMessage() << "[InfoFile]: Parsed " << _store.getPropertyCount() << " map properties." << std::endl;
clear();
}
}