1 | /*
|
---|
2 | *
|
---|
3 | Copyright (C) 2017 Fábio Bento (random-guy)
|
---|
4 |
|
---|
5 | This program is free software: you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation, either version 3 of the License, or
|
---|
8 | (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | *
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "projectfilevago.h"
|
---|
21 |
|
---|
22 | const QString ProjectFileVago::XMLTableName = "XML";
|
---|
23 | const QString ProjectFileVago::TexturesTableName = "Textures";
|
---|
24 | const QString ProjectFileVago::CharactersTableName = "Characters";
|
---|
25 | const QString ProjectFileVago::ObjectsTableName = "Objects";
|
---|
26 | const QString ProjectFileVago::LevelsTableName = "Levels";
|
---|
27 | const QString ProjectFileVago::MiscTableName = "Misc";
|
---|
28 |
|
---|
29 | ProjectFileVago::ProjectData ProjectFileVago::readProjectDataFromFile(const QString &fileFullPath){
|
---|
30 |
|
---|
31 | ProjectFileVago::ProjectData currentProjectData;
|
---|
32 |
|
---|
33 | upgradeProjectFileIfNecessary(fileFullPath);
|
---|
34 |
|
---|
35 | pugi::xml_document doc;
|
---|
36 |
|
---|
37 | pugi::xml_parse_result result = doc.load_file(QSTR_TO_CSTR(fileFullPath));
|
---|
38 |
|
---|
39 | if(result.status!=pugi::status_ok){
|
---|
40 | throw std::runtime_error(QSTR_TO_CSTR(QString("An error ocurred while loading project file.\n") + result.description()));
|
---|
41 | }
|
---|
42 |
|
---|
43 | if(QString(doc.root().first_child().name()) != "VagoProject"){
|
---|
44 | throw std::runtime_error(QSTR_TO_CSTR(QString(doc.root().name()) + "The file opened is not a valid Vago project file. Load aborted."));
|
---|
45 | }
|
---|
46 |
|
---|
47 | QString projVagoVersion;
|
---|
48 |
|
---|
49 | try{
|
---|
50 | projVagoVersion = QString(doc.select_node("/VagoProject/@vagoVersion").attribute().value());
|
---|
51 | }
|
---|
52 | catch (const pugi::xpath_exception& e)
|
---|
53 | {
|
---|
54 | throw std::runtime_error(QSTR_TO_CSTR(QString("Couldn't find the vagoVersion of the current project. Load aborted.\n") + e.what()));
|
---|
55 | }
|
---|
56 |
|
---|
57 | if(!projVagoVersion.startsWith(GlobalVars::LastCompatibleVersion)){
|
---|
58 | throw std::runtime_error("The project that you are trying to load seems it is not compatible with your Vago Version. Please update Vago and try again.");
|
---|
59 | }
|
---|
60 |
|
---|
61 | // After the initial validations begin loading the project data
|
---|
62 |
|
---|
63 | auto fFetchFromToForTab = [](pugi::xml_document &doc, const QString &tableName, ProjectTable &tableToInjectData) -> void{
|
---|
64 | tableToInjectData.from = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+tableName+"/@from")).attribute().value();
|
---|
65 | tableToInjectData.to = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+tableName+"/@to")).attribute().value();
|
---|
66 | };
|
---|
67 |
|
---|
68 | auto fFetchTabTableRows = [](pugi::xml_document &doc, const QString &tableName) -> QVector<ProjectTableRow>{
|
---|
69 |
|
---|
70 | QVector<ProjectTableRow> rows;
|
---|
71 |
|
---|
72 | for(const pugi::xpath_node &xPathNode : doc.select_nodes(QSTR_TO_CSTR("/VagoProject/"+tableName+"/Row"))){
|
---|
73 |
|
---|
74 | ProjectTableRow currentRow;
|
---|
75 |
|
---|
76 | pugi::xml_node currNode = xPathNode.node();
|
---|
77 |
|
---|
78 | currentRow.fileFolder = currNode.attribute("fileFolder").value();
|
---|
79 | currentRow.fromTo = currNode.attribute("fromTo").value();
|
---|
80 | currentRow.command = currNode.attribute("command").value();
|
---|
81 |
|
---|
82 | pugi::xml_attribute disabledAttr = currNode.attribute("disabled");
|
---|
83 | currentRow.isDisabled = disabledAttr.empty() ? false : disabledAttr.as_bool();
|
---|
84 |
|
---|
85 | rows.append(currentRow);
|
---|
86 | }
|
---|
87 |
|
---|
88 | return rows;
|
---|
89 | };
|
---|
90 |
|
---|
91 | QString currentTableName = XMLTableName;
|
---|
92 |
|
---|
93 | // XML tab
|
---|
94 |
|
---|
95 | fFetchFromToForTab(doc, currentTableName, currentProjectData.xmlTable);
|
---|
96 | currentProjectData.xmlTable.rows = fFetchTabTableRows(doc, currentTableName);
|
---|
97 |
|
---|
98 | // Textures tab
|
---|
99 |
|
---|
100 | currentTableName = TexturesTableName;
|
---|
101 |
|
---|
102 | fFetchFromToForTab(doc, currentTableName, currentProjectData.texturesTable);
|
---|
103 |
|
---|
104 | currentProjectData.texturesTable.rbTexturesType = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@type")).attribute().value();
|
---|
105 | currentProjectData.texturesTable.cbGenMipMaps = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@genMipMaps")).attribute().as_bool();
|
---|
106 | currentProjectData.texturesTable.cbNoUwrap = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@noUwrap")).attribute().as_bool();
|
---|
107 | currentProjectData.texturesTable.cbNoUwrap = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@noVwrap")).attribute().as_bool();
|
---|
108 | currentProjectData.texturesTable.cbLarge = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@large")).attribute().as_bool();
|
---|
109 | currentProjectData.texturesTable.cbEnvMap = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@envMap")).attribute().as_bool();
|
---|
110 | currentProjectData.texturesTable.leEnvMapTexture = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@envMapValue")).attribute().value();
|
---|
111 |
|
---|
112 | currentProjectData.texturesTable.rows = fFetchTabTableRows(doc, currentTableName);
|
---|
113 |
|
---|
114 | // Characters tab
|
---|
115 |
|
---|
116 | currentTableName = CharactersTableName;
|
---|
117 |
|
---|
118 | fFetchFromToForTab(doc, currentTableName, currentProjectData.charactersTable);
|
---|
119 |
|
---|
120 | currentProjectData.charactersTable.cbCellShading = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@cellShading")).attribute().as_bool();
|
---|
121 | currentProjectData.charactersTable.cbNormals = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@normals")).attribute().as_bool();
|
---|
122 | currentProjectData.charactersTable.cbStandingPose = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@standingPose")).attribute().as_bool();
|
---|
123 | currentProjectData.charactersTable.cbWithTRBS_ONCC = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@extractTRBSONCC")).attribute().as_bool();
|
---|
124 | currentProjectData.charactersTable.leTRBS_ONCC = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@extractTRBSONCCValue")).attribute().value();
|
---|
125 |
|
---|
126 | currentProjectData.charactersTable.rows = fFetchTabTableRows(doc, currentTableName);
|
---|
127 |
|
---|
128 | // Objects tab
|
---|
129 |
|
---|
130 | currentTableName = ObjectsTableName;
|
---|
131 |
|
---|
132 | fFetchFromToForTab(doc, currentTableName, currentProjectData.objectsTable);
|
---|
133 |
|
---|
134 | currentProjectData.objectsTable.cbTexture = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@texture")).attribute().as_bool();
|
---|
135 | currentProjectData.objectsTable.leTextureName = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@textureValue")).attribute().value();
|
---|
136 | currentProjectData.objectsTable.cbWithAnimation = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@withAnimation")).attribute().as_bool();
|
---|
137 | currentProjectData.objectsTable.leAnimationName = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@withAnimationValue")).attribute().value();
|
---|
138 |
|
---|
139 | currentProjectData.objectsTable.rows = fFetchTabTableRows(doc, currentTableName);
|
---|
140 |
|
---|
141 | // Levels tab
|
---|
142 |
|
---|
143 | currentTableName = LevelsTableName;
|
---|
144 |
|
---|
145 | fFetchFromToForTab(doc, currentTableName, currentProjectData.levelsTable);
|
---|
146 |
|
---|
147 | currentProjectData.levelsTable.cbSpecificFilesLevels = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@extractWithFiles")).attribute().as_bool();
|
---|
148 | currentProjectData.levelsTable.leSpecificFilesLevels = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@extractWithFilesValue")).attribute().value();
|
---|
149 | currentProjectData.levelsTable.cbDatLevels = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@datFilename")).attribute().as_bool();
|
---|
150 | currentProjectData.levelsTable.leTargetDatLevels = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@datFilenameValue")).attribute().value();
|
---|
151 | currentProjectData.levelsTable.cbBnvLevels = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@bnvSource")).attribute().as_bool();
|
---|
152 | currentProjectData.levelsTable.leBnvLevels = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@bnvSourceValue")).attribute().value();
|
---|
153 | currentProjectData.levelsTable.cbGridsLevels = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@generateGrids")).attribute().as_bool();
|
---|
154 | currentProjectData.levelsTable.cbAdditionalSourcesLevels = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@additionalSources")).attribute().as_bool();
|
---|
155 | currentProjectData.levelsTable.leAdditSourcesLevels = doc.select_node(QSTR_TO_CSTR("/VagoProject/"+currentTableName+"/Options/@additionalSourcesValue")).attribute().value();
|
---|
156 |
|
---|
157 | currentProjectData.levelsTable.rows = fFetchTabTableRows(doc, currentTableName);
|
---|
158 |
|
---|
159 | // Misc tab
|
---|
160 |
|
---|
161 | currentTableName = MiscTableName;
|
---|
162 |
|
---|
163 | fFetchFromToForTab(doc, currentTableName, currentProjectData.miscTable);
|
---|
164 |
|
---|
165 | currentProjectData.miscTable.rows = fFetchTabTableRows(doc, currentTableName);
|
---|
166 |
|
---|
167 | return currentProjectData;
|
---|
168 | }
|
---|
169 |
|
---|
170 | void ProjectFileVago::upgradeProjectFileIfNecessary(const QString &filePath){
|
---|
171 |
|
---|
172 | pugi::xml_document doc;
|
---|
173 |
|
---|
174 | pugi::xml_parse_result result = doc.load_file(QSTR_TO_CSTR(filePath));
|
---|
175 |
|
---|
176 | if(result.status!=pugi::status_ok){
|
---|
177 | throw std::runtime_error(QSTR_TO_CSTR(QString("An error ocurred while loading project file.\n") + result.description()));
|
---|
178 | }
|
---|
179 |
|
---|
180 | QString projectVersion = QString(doc.select_single_node("/VagoProject").node().attribute("vagoVersion").as_string());
|
---|
181 |
|
---|
182 | // 1.4 added standing pose in characters tab (we need to add it in 1.0 projects)
|
---|
183 | if(projectVersion == "1.0"){
|
---|
184 |
|
---|
185 | if(!Util::FileSystem::backupFile(filePath, filePath + UtilVago::getDateTimeFormatForFilename(QDateTime::currentDateTime()))){
|
---|
186 | QString errorMessage = "Couldn't backup the existing project file for version upgrade, program can't proceed.";
|
---|
187 | Util::Dialogs::showError(errorMessage);
|
---|
188 | LOG_FATAL << errorMessage;
|
---|
189 | exit(1);
|
---|
190 | }
|
---|
191 |
|
---|
192 | // Update version
|
---|
193 | doc.select_single_node("/VagoProject").node().attribute("vagoVersion").set_value(QSTR_TO_CSTR(GlobalVars::LastCompatibleVersion));
|
---|
194 |
|
---|
195 | // Add standing pose to characters options
|
---|
196 | doc.select_node("/VagoProject/Characters/Options").node().append_attribute("standingPose").set_value("false");
|
---|
197 |
|
---|
198 | if(!doc.save_file(QSTR_TO_CSTR(filePath), PUGIXML_TEXT("\t"), pugi::format_default | pugi::format_write_bom, pugi::xml_encoding::encoding_utf8)){
|
---|
199 | throw std::runtime_error(QSTR_TO_CSTR("Error while saving: '" + filePath + "'. After file version upgrade."));
|
---|
200 | }
|
---|
201 | }
|
---|
202 | else if(projectVersion != GlobalVars::LastCompatibleVersion){
|
---|
203 | throw std::runtime_error("Can't load the project file, it is from an incompatible version. Probably newer?");
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | // Right now it always replaces totally the old file with new content (old content is not modified but replaced)
|
---|
208 | void ProjectFileVago::saveProjectDataToFile(const QString &fileFullPath, const ProjectFileVago::ProjectData &newProjectData){
|
---|
209 | pugi::xml_document doc;
|
---|
210 | pugi::xml_node rootNode;
|
---|
211 |
|
---|
212 | rootNode = doc.append_child("VagoProject"); // create
|
---|
213 |
|
---|
214 | rootNode.append_attribute("vagoVersion").set_value(QSTR_TO_CSTR(GlobalVars::LastCompatibleVersion));
|
---|
215 |
|
---|
216 | // Let's starting writting the GUI data
|
---|
217 |
|
---|
218 | QString currentTableName;
|
---|
219 |
|
---|
220 | // Returns node of the table
|
---|
221 | auto fWriteTabGenericData = [](pugi::xml_node &rootNode, const ProjectTable &table, const QString &tableName) -> pugi::xml_node{
|
---|
222 |
|
---|
223 | pugi::xml_node rowsNode = rootNode.append_child(QSTR_TO_CSTR(tableName));
|
---|
224 |
|
---|
225 | rowsNode.append_attribute("from").set_value(QSTR_TO_CSTR(table.from));
|
---|
226 | rowsNode.append_attribute("to").set_value(QSTR_TO_CSTR(table.to));
|
---|
227 |
|
---|
228 | for(const ProjectTableRow &currRow : table.rows){
|
---|
229 |
|
---|
230 | pugi::xml_node currentRow = rowsNode.append_child("Row");
|
---|
231 |
|
---|
232 | currentRow.append_attribute("fileFolder").set_value(QSTR_TO_CSTR(currRow.fileFolder));
|
---|
233 | currentRow.append_attribute("fromTo").set_value(QSTR_TO_CSTR(currRow.fromTo));
|
---|
234 | currentRow.append_attribute("command").set_value(QSTR_TO_CSTR(currRow.command));
|
---|
235 |
|
---|
236 | // Only necessary to add if it is infact disabled
|
---|
237 | if(currRow.isDisabled){
|
---|
238 | currentRow.append_attribute("disabled").set_value(currRow.isDisabled);
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | return rowsNode;
|
---|
243 | };
|
---|
244 |
|
---|
245 | // XML tab
|
---|
246 |
|
---|
247 | currentTableName = XMLTableName;
|
---|
248 |
|
---|
249 | fWriteTabGenericData(rootNode, newProjectData.xmlTable, currentTableName);
|
---|
250 |
|
---|
251 | // Textures tab
|
---|
252 |
|
---|
253 | currentTableName = TexturesTableName;
|
---|
254 |
|
---|
255 | pugi::xml_node currentNodeTable = fWriteTabGenericData(rootNode, newProjectData.texturesTable, currentTableName);
|
---|
256 |
|
---|
257 | pugi::xml_node options = currentNodeTable.append_child("Options");
|
---|
258 | options.append_attribute("type").set_value(QSTR_TO_CSTR(newProjectData.texturesTable.rbTexturesType));
|
---|
259 | options.append_attribute("genMipMaps").set_value(Util::String::boolToCstr(newProjectData.texturesTable.cbGenMipMaps));
|
---|
260 | options.append_attribute("noUwrap").set_value(Util::String::boolToCstr(newProjectData.texturesTable.cbNoUwrap));
|
---|
261 | options.append_attribute("noVwrap").set_value(Util::String::boolToCstr(newProjectData.texturesTable.cbNoVwrap));
|
---|
262 | options.append_attribute("large").set_value(Util::String::boolToCstr(newProjectData.texturesTable.cbLarge));
|
---|
263 | options.append_attribute("envMap").set_value(Util::String::boolToCstr(newProjectData.texturesTable.cbEnvMap));
|
---|
264 | options.append_attribute("envMapValue").set_value(QSTR_TO_CSTR(newProjectData.texturesTable.leEnvMapTexture));
|
---|
265 |
|
---|
266 | // Characters tab
|
---|
267 |
|
---|
268 | currentTableName = CharactersTableName;
|
---|
269 |
|
---|
270 | currentNodeTable = fWriteTabGenericData(rootNode, newProjectData.charactersTable, currentTableName);
|
---|
271 |
|
---|
272 | options = currentNodeTable.append_child("Options");
|
---|
273 | options.append_attribute("cellShading").set_value(Util::String::boolToCstr(newProjectData.charactersTable.cbCellShading));
|
---|
274 | options.append_attribute("normals").set_value(Util::String::boolToCstr(newProjectData.charactersTable.cbNormals));
|
---|
275 | options.append_attribute("standingPose").set_value(Util::String::boolToCstr(newProjectData.charactersTable.cbStandingPose));
|
---|
276 | options.append_attribute("extractTRBSONCC").set_value(Util::String::boolToCstr(newProjectData.charactersTable.cbWithTRBS_ONCC));
|
---|
277 | options.append_attribute("extractTRBSONCCValue").set_value(QSTR_TO_CSTR(newProjectData.charactersTable.leTRBS_ONCC));
|
---|
278 |
|
---|
279 | // Objects tab
|
---|
280 |
|
---|
281 | currentTableName = ObjectsTableName;
|
---|
282 |
|
---|
283 | currentNodeTable = fWriteTabGenericData(rootNode, newProjectData.objectsTable, currentTableName);
|
---|
284 |
|
---|
285 | options = currentNodeTable.append_child("Options");
|
---|
286 | options.append_attribute("texture").set_value(Util::String::boolToCstr(newProjectData.objectsTable.cbTexture));
|
---|
287 | options.append_attribute("textureValue").set_value(QSTR_TO_CSTR(newProjectData.objectsTable.leTextureName));
|
---|
288 | options.append_attribute("withAnimation").set_value(Util::String::boolToCstr(newProjectData.objectsTable.cbWithAnimation));
|
---|
289 | options.append_attribute("withAnimationValue").set_value(QSTR_TO_CSTR(newProjectData.objectsTable.leAnimationName));
|
---|
290 |
|
---|
291 | // Levels tab
|
---|
292 |
|
---|
293 | currentTableName = LevelsTableName;
|
---|
294 |
|
---|
295 | currentNodeTable = fWriteTabGenericData(rootNode, newProjectData.levelsTable, currentTableName);
|
---|
296 |
|
---|
297 | options = currentNodeTable.append_child("Options");
|
---|
298 | options.append_attribute("extractWithFiles").set_value(Util::String::boolToCstr(newProjectData.levelsTable.cbSpecificFilesLevels));
|
---|
299 | options.append_attribute("extractWithFilesValue").set_value(QSTR_TO_CSTR(newProjectData.levelsTable.leSpecificFilesLevels));
|
---|
300 | options.append_attribute("datFilename").set_value(Util::String::boolToCstr(newProjectData.levelsTable.cbDatLevels));
|
---|
301 | options.append_attribute("datFilenameValue").set_value(QSTR_TO_CSTR(newProjectData.levelsTable.leTargetDatLevels));
|
---|
302 | options.append_attribute("bnvSource").set_value(Util::String::boolToCstr(newProjectData.levelsTable.cbBnvLevels));
|
---|
303 | options.append_attribute("bnvSourceValue").set_value(QSTR_TO_CSTR(newProjectData.levelsTable.leBnvLevels));
|
---|
304 | options.append_attribute("generateGrids").set_value(Util::String::boolToCstr(newProjectData.levelsTable.cbGridsLevels));
|
---|
305 | options.append_attribute("additionalSources").set_value(Util::String::boolToCstr(newProjectData.levelsTable.cbAdditionalSourcesLevels));
|
---|
306 | options.append_attribute("additionalSourcesValue").set_value(QSTR_TO_CSTR(newProjectData.levelsTable.leAdditSourcesLevels));
|
---|
307 |
|
---|
308 | // Misc tab
|
---|
309 |
|
---|
310 | currentTableName = MiscTableName;
|
---|
311 |
|
---|
312 | fWriteTabGenericData(rootNode, newProjectData.miscTable, currentTableName);
|
---|
313 |
|
---|
314 | if(!doc.save_file(fileFullPath.toUtf8().constData(), PUGIXML_TEXT("\t"), pugi::format_default | pugi::format_write_bom, pugi::xml_encoding::encoding_utf8)){
|
---|
315 | throw std::runtime_error("An error ocurred while trying to save the project file. Please try another path.");
|
---|
316 | }
|
---|
317 |
|
---|
318 | }
|
---|