-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathConfigXML.cpp
More file actions
270 lines (211 loc) · 8.09 KB
/
ConfigXML.cpp
File metadata and controls
270 lines (211 loc) · 8.09 KB
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
/*
* halfmapper, a renderer for GoldSrc maps and chapters.
*
* Copyright(C) 2014 Gonzalo Ávila "gzalo" Alterach
* Copyright(C) 2015 Anthony "birkett" Birkett
*
* This file is part of halfmapper.
*
* This program is free software; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
*/
#include <iostream>
#include "ConfigXML.h"
/**
* Constructor to set some default values.
*
* These values are used as fallbacks when the user config.xml can't be loaded.
* WriteDefaultProgramConfig() will use these.
*/
ConfigXML::ConfigXML()
{
this->m_iWidth = 800;
this->m_iHeight = 600;
this->m_fFov = 60.0f;
this->m_bIsometric = false;
this->m_bFullscreen = false;
this->m_bMultisampling = false;
this->m_bVsync = true;
this->m_szGamePaths.push_back(HALFLIFE_DEFAULT_GAMEPATH);
this->m_szGamePaths.push_back(CSTRIKE_DEFAULT_GAMEPATH);
}//end ConfigXML::ConfigXML()
/**
* Destructor to clean up the internal data storage.
*/
ConfigXML::~ConfigXML()
{
this->m_xmlProgramConfig.Clear();
this->m_xmlMapConfig.Clear();
this->m_vWads.clear();
this->m_vChapterEntries.clear();
this->m_szGamePaths.clear();
}//end ConfigXML::~ConfigXML()
/**
* Load the user (program) configuraion.
*/
XMLError ConfigXML::LoadProgramConfig()
{
XMLError eRetCode = this->m_xmlProgramConfig.LoadFile("config.xml");
if (eRetCode != XML_SUCCESS) {
if (eRetCode == XML_ERROR_FILE_NOT_FOUND) {
this->WriteDefaultProgramConfig();
}
else {
std::cout << "Error loading config.xml. Code " << eRetCode << std::endl;
return eRetCode;
}
}
XMLNode *rootNode = this->m_xmlProgramConfig.FirstChild();
if (rootNode == nullptr) {
std::cout << "Malformed XML. No root element." << std::endl;
return XML_ERROR_FILE_READ_ERROR;
}
XMLElement *window = rootNode->FirstChildElement("window");
if (window == nullptr) {
std::cout << "Malformed XML. No window element." << std::endl;
return XML_ERROR_FILE_READ_ERROR;
}
window->QueryUnsignedAttribute("width", &this->m_iWidth );
window->QueryUnsignedAttribute("height", &this->m_iHeight );
window->QueryFloatAttribute ("fov", &this->m_fFov );
window->QueryBoolAttribute ("isometric", &this->m_bIsometric );
window->QueryBoolAttribute ("fullscreen", &this->m_bFullscreen );
window->QueryBoolAttribute ("multisampling", &this->m_bMultisampling);
window->QueryBoolAttribute ("vsync", &this->m_bVsync );
XMLElement *gamepaths = rootNode->FirstChildElement("gamepaths");
if (gamepaths != nullptr) {
XMLElement *gamepath = gamepaths->FirstChildElement("gamepath");
while (gamepath != nullptr) {
std::string path = gamepath->GetText();
// Add a trailing slash if none.
if (!path.empty() && *path.rbegin() != PATH_DELIM)
path += PATH_DELIM;
this->m_szGamePaths.push_back(path);
gamepath = gamepath->NextSiblingElement("gamepath");
}
}
return XML_SUCCESS;
}//end ConfigXML::LoadProgramConfig()
/**
* Load the map configuration from a given file name.
* \param szFilename File to load from.
*/
XMLError ConfigXML::LoadMapConfig(const char *szFilename)
{
XMLError eRetCode = this->m_xmlMapConfig.LoadFile(szFilename);
if (eRetCode != XML_SUCCESS) {
std::cout << "Error loading map config. Code " << eRetCode << std::endl;
return eRetCode;
}
XMLNode *rootNode = this->m_xmlMapConfig.FirstChild();
if (rootNode == nullptr) {
std::cout << "Malformed XML. No root element." << std::endl;
return XML_ERROR_FILE_READ_ERROR;
}
XMLElement *wadsElement = rootNode->FirstChildElement("wads");
if (wadsElement == nullptr) {
std::cout << "Malformed XML. No wad container element." << std::endl;
return XML_ERROR_FILE_READ_ERROR;
}
XMLElement *wad = wadsElement->FirstChildElement("wad");
if (wad == nullptr) {
std::cout << "Malformed XML. No wads element." << std::endl;
return XML_ERROR_FILE_READ_ERROR;
}
while (wad != nullptr) {
this->m_vWads.push_back(wad->GetText());
wad = wad->NextSiblingElement("wad");
}
XMLElement *chapter = rootNode->FirstChildElement("chapter");
if (chapter == nullptr) {
std::cout << "Malformed XML. No chapters found." << std::endl;
return XML_ERROR_FILE_READ_ERROR;
}
while (chapter != nullptr) {
ChapterEntry sChapterEntry;
sChapterEntry.m_szName = chapter->Attribute("name");
sChapterEntry.m_bRender = chapter->BoolAttribute("render");
if (sChapterEntry.m_szName == "") {
std::cout << "Malformed XML. Chapter found without name attribute." << std::endl;
return XML_ERROR_FILE_READ_ERROR;
}
XMLElement *chapteroffset = chapter->FirstChildElement("offset");
if (chapteroffset != nullptr) {
chapteroffset->QueryFloatAttribute("x", &sChapterEntry.m_fOffsetX);
chapteroffset->QueryFloatAttribute("y", &sChapterEntry.m_fOffsetY);
chapteroffset->QueryFloatAttribute("z", &sChapterEntry.m_fOffsetZ);
}
XMLElement *map = chapter->FirstChildElement("map");
while (map != nullptr) {
MapEntry sMapEntry;
sMapEntry.m_szName = map->Attribute("name");
sMapEntry.m_bRender = map->BoolAttribute("render");
if (sMapEntry.m_szName == "") {
std::cout << "Malformed XML. Map found without name attribute." << std::endl;
return XML_ERROR_FILE_READ_ERROR;
}
XMLElement *offset = map->FirstChildElement("offset");
if (offset != nullptr) {
sMapEntry.m_szOffsetTargetName = offset->Attribute("targetname");
offset->QueryFloatAttribute("x", &sMapEntry.m_fOffsetX);
offset->QueryFloatAttribute("y", &sMapEntry.m_fOffsetY);
offset->QueryFloatAttribute("z", &sMapEntry.m_fOffsetZ);
}
sChapterEntry.m_vMapEntries.push_back(sMapEntry);
map = map->NextSiblingElement("map");
}
chapter = chapter->NextSiblingElement("chapter");
this->m_vChapterEntries.push_back(sChapterEntry);
}
return XML_SUCCESS;
}//end LoadMapConfig()
/**
* Write the default program config when not found.
*/
XMLError ConfigXML::WriteDefaultProgramConfig()
{
// Document root node.
XMLNode *rootNode = this->m_xmlProgramConfig.NewElement("config");
// Window settings.
XMLElement *window = this->m_xmlProgramConfig.NewElement("window");
window->SetAttribute("width", this->m_iWidth );
window->SetAttribute("height", this->m_iHeight );
window->SetAttribute("fov", this->m_fFov );
window->SetAttribute("isometric", this->m_bIsometric );
window->SetAttribute("fullscreen", this->m_bFullscreen );
window->SetAttribute("multisampling", this->m_bMultisampling);
window->SetAttribute("vsync", this->m_bVsync );
// Collection of game paths.
XMLElement *gamepaths = this->m_xmlProgramConfig.NewElement("gamepaths");
// Half Life game path.
XMLElement *hlgamepath = this->m_xmlProgramConfig.NewElement("gamepath");
hlgamepath->SetAttribute("name", "halflife");
hlgamepath->SetText(HALFLIFE_DEFAULT_GAMEPATH);
// Counter Strike game path.
XMLElement *csgamepath = this->m_xmlProgramConfig.NewElement("gamepath");
csgamepath->SetAttribute("name", "cstrike");
csgamepath->SetText(CSTRIKE_DEFAULT_GAMEPATH);
// Add elements to the document.
this->m_xmlProgramConfig.InsertFirstChild(rootNode);
rootNode->InsertFirstChild(window);
rootNode->InsertEndChild(gamepaths);
gamepaths->InsertFirstChild(hlgamepath);
gamepaths->InsertEndChild(csgamepath);
// Save.
XMLError eRetCode = this->m_xmlProgramConfig.SaveFile("config.xml");
if (eRetCode != XML_SUCCESS) {
std::cout << "Error writing config.xml. Code " << eRetCode << std::endl;
return XML_ERROR_FILE_READ_ERROR;
}
return XML_SUCCESS;
}//end ConfigXML::WriteDefaultProgramConfig()