1 | #include "xmlpatch.h"
|
---|
2 |
|
---|
3 | XmlPatch::XmlPatch(QString patchFilesWildcard, QString forceTargetFilesWildcard, bool noBackups)
|
---|
4 | {
|
---|
5 | this->patchFilesToProcess=UtilXmlTools::getAllPatchFilesByWildcard(patchFilesWildcard);
|
---|
6 | this->forceTargetFilesWildcard=forceTargetFilesWildcard;
|
---|
7 | this->backupsEnabled=!noBackups;
|
---|
8 |
|
---|
9 | if(forceTargetFilesWildcard!=""){
|
---|
10 | std::cout << "User forced patch in the target file(s): " << forceTargetFilesWildcard.toLatin1().constData() << std::endl;
|
---|
11 | }
|
---|
12 |
|
---|
13 | if(this->patchFilesToProcess.isEmpty()){
|
---|
14 | UtilXmlTools::displayErrorMessage("Loading patch files","No .patch or .oni-patch files were found for the wildcard: "+patchFilesWildcard);
|
---|
15 | }
|
---|
16 |
|
---|
17 | }
|
---|
18 |
|
---|
19 | void XmlPatch::readAndProcessPatchFile(){
|
---|
20 |
|
---|
21 | // Process all PatchFiles
|
---|
22 | for(int i=0; i<this->patchFilesToProcess.size(); i++){
|
---|
23 |
|
---|
24 | QFile inputFile(this->patchFilesToProcess[i]);
|
---|
25 |
|
---|
26 | if (inputFile.open(QIODevice::ReadOnly))
|
---|
27 | {
|
---|
28 |
|
---|
29 | QTextStream fileStream(&inputFile);
|
---|
30 |
|
---|
31 | checkPatchVersion(this->patchFilesToProcess[i], fileStream);
|
---|
32 | checkAndProcessValidCommands(fileStream);
|
---|
33 |
|
---|
34 | inputFile.close();
|
---|
35 | }
|
---|
36 | else{
|
---|
37 | UtilXmlTools::displayErrorMessage("Read patch file", "Error opening patch file: '" + this->patchFilesToProcess[i] + "'.\n" + inputFile.errorString());
|
---|
38 | }
|
---|
39 |
|
---|
40 | }
|
---|
41 |
|
---|
42 | UtilXmlTools::displaySuccessMessage(this->patchFilesToProcess.size(),"Patch File(s)");
|
---|
43 |
|
---|
44 | }
|
---|
45 |
|
---|
46 | void XmlPatch::addToOperation(const QString &xmlString, XmlFilter &filters, const QString &filesWildcard){
|
---|
47 |
|
---|
48 | QStringList filesToProcess;
|
---|
49 | pugi::xml_node nodeToInsertion;
|
---|
50 | pugi::xml_document newNode;
|
---|
51 | pugi::xml_parse_result result;
|
---|
52 |
|
---|
53 | filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
|
---|
54 |
|
---|
55 | if(filesToProcess.isEmpty()){
|
---|
56 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODE","No XML files were found for the wildcard: "+filesWildcard);
|
---|
57 | }
|
---|
58 |
|
---|
59 | result=newNode.load(xmlString.toLatin1().constData()); // load xml to insert
|
---|
60 |
|
---|
61 | if(result.status!=pugi::status_ok){
|
---|
62 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODE", "The xml node to insert is invalid.\n" + Util::toQString(result.description()));
|
---|
63 | }
|
---|
64 |
|
---|
65 | // Process all XmlFiles
|
---|
66 | for(int i=0; i<filesToProcess.size(); i++){
|
---|
67 |
|
---|
68 | UtilXmlTools::loadXmlFile(filesToProcess[i],this->document,this->rootNode,this->backupsEnabled,"@ADD_INSIDE_NODE");
|
---|
69 |
|
---|
70 | nodeToInsertion=UtilXmlTools::getFirstNamedElements(this->rootNode,filters);
|
---|
71 |
|
---|
72 | if(nodeToInsertion.type()==pugi::node_null){
|
---|
73 |
|
---|
74 | QString errMessage;
|
---|
75 |
|
---|
76 | errMessage = "It wasn't found a node with a ElementName: '" + filters.getElementName() + "'";
|
---|
77 | if(filters.getParentElementName()!=""){
|
---|
78 | errMessage += " and a ParentElementName: '" + filters.getParentElementName() + "'";
|
---|
79 | }
|
---|
80 | if(filters.getAttributeName()!=""){
|
---|
81 | errMessage += " and an AttributeName: '" + filters.getAttributeName() + "' and an AttributeValue: '" + filters.getAttributeValue() + "'";
|
---|
82 | }
|
---|
83 |
|
---|
84 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODE",errMessage);
|
---|
85 | }
|
---|
86 |
|
---|
87 | nodeToInsertion.prepend_copy(newNode.first_child()); // append the new node
|
---|
88 |
|
---|
89 |
|
---|
90 | UtilXmlTools::saveXmlFile(filesToProcess[i],this->document, "@ADD_INSIDE_NODE");
|
---|
91 | }
|
---|
92 |
|
---|
93 | UtilXmlTools::displaySuccessMessage(filesToProcess.size(),"@ADD_INSIDE_NODE");
|
---|
94 | }
|
---|
95 |
|
---|
96 | void XmlPatch::removeNodeOperation(XmlFilter &filters, const QString &filesWildcard){
|
---|
97 |
|
---|
98 | QStringList filesToProcess;
|
---|
99 |
|
---|
100 | pugi::xml_node nodeToDeletion;
|
---|
101 |
|
---|
102 | filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
|
---|
103 |
|
---|
104 | if(filesToProcess.isEmpty()){
|
---|
105 | UtilXmlTools::displayErrorMessage("@REMOVE_NODE","No XML files were found for the wildcard: "+filesWildcard);
|
---|
106 | }
|
---|
107 |
|
---|
108 | // Process all XmlFiles
|
---|
109 | for(int i=0; i<filesToProcess.size(); i++){
|
---|
110 |
|
---|
111 | UtilXmlTools::loadXmlFile(filesToProcess[i],this->document,this->rootNode,this->backupsEnabled,"@REMOVE_NODE");
|
---|
112 |
|
---|
113 | nodeToDeletion=UtilXmlTools::getFirstNamedElements(this->rootNode,filters);
|
---|
114 |
|
---|
115 | if(nodeToDeletion.type()==pugi::node_null){
|
---|
116 | QString errMessage;
|
---|
117 |
|
---|
118 | errMessage = "It wasn't found a node with a ElementName: '" + filters.getElementName() + "'";
|
---|
119 | if(filters.getParentElementName()!=""){
|
---|
120 | errMessage += " and a ParentElementName: '" + filters.getParentElementName() + "'";
|
---|
121 | }
|
---|
122 | if(filters.getAttributeName()!=""){
|
---|
123 | errMessage += " and an AttributeName: '" + filters.getAttributeName() + "' and an AttributeValue: '" + filters.getAttributeValue() + "'";
|
---|
124 | }
|
---|
125 |
|
---|
126 | UtilXmlTools::displayErrorMessage("@REMOVE_NODE",errMessage);
|
---|
127 | }
|
---|
128 |
|
---|
129 | if(!nodeToDeletion.parent().remove_child(nodeToDeletion)){ // remove the node
|
---|
130 |
|
---|
131 | QString errMessage;
|
---|
132 |
|
---|
133 | errMessage = "Couldn't remove the node with Element '" + filters.getElementName() + "'";
|
---|
134 |
|
---|
135 | if(filters.getParentElementName()!=""){
|
---|
136 | errMessage += " and a ParentElement: '" + filters.getParentElementName() + "'";
|
---|
137 | }
|
---|
138 |
|
---|
139 | UtilXmlTools::displayErrorMessage("@REMOVE_NODE",errMessage);
|
---|
140 | }
|
---|
141 |
|
---|
142 | UtilXmlTools::saveXmlFile(filesToProcess[i],this->document, "@REMOVE_NODE");
|
---|
143 | }
|
---|
144 |
|
---|
145 | UtilXmlTools::displaySuccessMessage(filesToProcess.size(), "@REMOVE_NODE");
|
---|
146 | }
|
---|
147 |
|
---|
148 | void XmlPatch::executeCommandOperation(const QString &commandString){
|
---|
149 |
|
---|
150 | QProcess newXmlToolsInstance;
|
---|
151 | QString resultOutput;
|
---|
152 |
|
---|
153 | // Avoid infinite fork loops
|
---|
154 | if(commandString.contains("-p") || commandString.contains("--patch-files")){
|
---|
155 | UtilXmlTools::displayErrorMessage("@COMMAND","Use of --patch-files option is not allowed inside a patch file");
|
---|
156 | }
|
---|
157 |
|
---|
158 | newXmlToolsInstance.start(GlobalVars::AppName + " " + commandString);
|
---|
159 | newXmlToolsInstance.waitForFinished(-1); // wait for new instance to finish
|
---|
160 |
|
---|
161 | resultOutput=newXmlToolsInstance.readAll();
|
---|
162 |
|
---|
163 | if(newXmlToolsInstance.exitCode()!=0){
|
---|
164 | UtilXmlTools::displayErrorMessage("@COMMAND", "An error ocurred:\n" + resultOutput);
|
---|
165 | exit(1);
|
---|
166 | }
|
---|
167 |
|
---|
168 | std::cout << "@COMMAND patch operation output:\n" << resultOutput.toLatin1().constData() << std::endl;
|
---|
169 |
|
---|
170 | UtilXmlTools::displaySuccessMessage(1,"@COMMAND");
|
---|
171 | }
|
---|
172 |
|
---|
173 | void XmlPatch::executeCustomCommandOperation(const QString &jsString, const QString &filesWildcard){
|
---|
174 |
|
---|
175 | QStringList filesToProcess;
|
---|
176 | #ifdef _USE_OLD_JS_ENGINE
|
---|
177 | QScriptEngine engine;
|
---|
178 | QScriptValue engineResult; // variable to check for js_errors
|
---|
179 | #else
|
---|
180 | QJSEngine engine;
|
---|
181 | QJSValue engineResult; // variable to check for js_errors
|
---|
182 | #endif
|
---|
183 |
|
---|
184 | QString rexmlString, jsxmlString, currXmlFileString;
|
---|
185 |
|
---|
186 |
|
---|
187 | QFile rexmlfile(":/resources/libs/rexml.js");
|
---|
188 | QFile jsxmlfile(":/resources/libs/jsxml.js");
|
---|
189 |
|
---|
190 | filesToProcess=UtilXmlTools::getAllXmlFilesByWildcard(filesWildcard);
|
---|
191 |
|
---|
192 | if(filesToProcess.isEmpty()){
|
---|
193 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","No XML files were found for the wildcard: "+filesWildcard);
|
---|
194 | }
|
---|
195 |
|
---|
196 | rexmlfile.open(QFile::ReadOnly | QFile::Text);
|
---|
197 | jsxmlfile.open(QFile::ReadOnly | QFile::Text);
|
---|
198 |
|
---|
199 | rexmlString=QTextStream(&rexmlfile).readAll();
|
---|
200 | jsxmlString=QTextStream(&jsxmlfile).readAll();
|
---|
201 |
|
---|
202 | engine.evaluate(rexmlString); // load js libraries
|
---|
203 | engine.evaluate(jsxmlString);
|
---|
204 |
|
---|
205 | // Process all XmlFiles
|
---|
206 | for(int i=0; i<filesToProcess.size(); i++){
|
---|
207 |
|
---|
208 | if(this->backupsEnabled){
|
---|
209 | UtilXmlTools::backupFile(filesToProcess[i]);
|
---|
210 | }
|
---|
211 |
|
---|
212 | QFile currXmlFile(filesToProcess[i]);
|
---|
213 |
|
---|
214 | if(!currXmlFile.open(QFile::ReadOnly | QFile::Text)){
|
---|
215 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + filesToProcess[i] + "' file for read operation.");
|
---|
216 | }
|
---|
217 |
|
---|
218 | currXmlFileString=QTextStream(&currXmlFile).readAll();
|
---|
219 |
|
---|
220 | currXmlFile.close(); // close reading
|
---|
221 |
|
---|
222 | engine.globalObject().setProperty("$xmlData",currXmlFileString);
|
---|
223 |
|
---|
224 | engineResult=engine.evaluate(jsString);
|
---|
225 |
|
---|
226 | #ifdef _USE_OLD_JS_ENGINE
|
---|
227 | if (engine.hasUncaughtException()) {
|
---|
228 | displayJsException(engine,engineResult);
|
---|
229 | }
|
---|
230 | #else
|
---|
231 | checkForJsException(engineResult);
|
---|
232 | #endif
|
---|
233 |
|
---|
234 | if(!currXmlFile.open(QFile::WriteOnly | QFile::Text | QIODevice::Truncate)){
|
---|
235 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + filesToProcess[i] + "' file for @CUSTOM_CODE write operation.");
|
---|
236 | }
|
---|
237 |
|
---|
238 | engineResult=engine.globalObject().property("$xmlData");
|
---|
239 |
|
---|
240 | #ifdef _USE_OLD_JS_ENGINE
|
---|
241 | if (engine.hasUncaughtException()) {
|
---|
242 | displayJsException(engine,engineResult);
|
---|
243 | }
|
---|
244 | #else
|
---|
245 | checkForJsException(engineResult);
|
---|
246 | #endif
|
---|
247 |
|
---|
248 | QTextStream(&currXmlFile) << engineResult.toString(); // retreive the modified xml by javascript and save it to the file
|
---|
249 | }
|
---|
250 |
|
---|
251 | UtilXmlTools::displaySuccessMessage(filesToProcess.size(), "@CUSTOM_CODE");
|
---|
252 | }
|
---|
253 |
|
---|
254 | void XmlPatch::checkPatchVersion(const QString &file, QTextStream &fileStream){
|
---|
255 |
|
---|
256 | QString line, patchVersion="";
|
---|
257 |
|
---|
258 | // First get the patch version and check it validity
|
---|
259 | while ( !fileStream.atEnd() ){
|
---|
260 | line = fileStream.readLine();
|
---|
261 |
|
---|
262 | if(line.startsWith('#')){ // Ignore comments
|
---|
263 | continue;
|
---|
264 | }
|
---|
265 | else if(line.startsWith("@XML_TOOLS")){
|
---|
266 |
|
---|
267 | patchVersion=getPatchParameterValue(line,"Version");
|
---|
268 |
|
---|
269 | if(!patchVersion.startsWith("2.0")){
|
---|
270 | QString errMessage;
|
---|
271 |
|
---|
272 | errMessage = "The current patch version is incompatible with this XmlTools version:\n";
|
---|
273 | errMessage += "Patch file name: '" + file + "'\n";
|
---|
274 | errMessage += "XmlTools version: " + GlobalVars::AppVersion + "\n" + "CurrPatch version: " + patchVersion + "";
|
---|
275 | UtilXmlTools::displayErrorMessage("@XML_TOOLS",errMessage);
|
---|
276 | }
|
---|
277 | break; // We have got what we wanted
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | if(patchVersion==""){
|
---|
282 | UtilXmlTools::displayErrorMessage("@XML_TOOLS","Patch version not found.");
|
---|
283 | }
|
---|
284 |
|
---|
285 | }
|
---|
286 |
|
---|
287 | void XmlPatch::checkAndProcessValidCommands(QTextStream &fileStream){
|
---|
288 |
|
---|
289 | QString line, filesWildcard;
|
---|
290 | QString xmlToInsert, command, jsCode;
|
---|
291 | XmlFilter filters;
|
---|
292 |
|
---|
293 | // Process the rest of the commands in patch file
|
---|
294 | while ( !fileStream.atEnd() ){
|
---|
295 | line = fileStream.readLine();
|
---|
296 |
|
---|
297 | if(line.startsWith('#')){ // Ignore comments
|
---|
298 | continue;
|
---|
299 | }
|
---|
300 | else if(line.startsWith("@ADD_INSIDE_NODE")){
|
---|
301 | filters.setElementName(getPatchParameterValue(line,"ElementName"));
|
---|
302 | filters.setParentElementName(getPatchParameterValue(line,"ParentElementName"));
|
---|
303 | filters.setAttributeName(getPatchParameterValue(line,"AttributeName"));
|
---|
304 | filters.setAttributeValue(getPatchParameterValue(line,"AttributeValue"));
|
---|
305 |
|
---|
306 | if(this->forceTargetFilesWildcard!=""){
|
---|
307 | filesWildcard=this->forceTargetFilesWildcard;
|
---|
308 | }
|
---|
309 | else{
|
---|
310 | filesWildcard=getPatchParameterValue(line,"Files");
|
---|
311 | }
|
---|
312 |
|
---|
313 | // Check attribute filters
|
---|
314 | if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){
|
---|
315 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODE","attribute-value option is required if using attribute-name option.");
|
---|
316 | }
|
---|
317 |
|
---|
318 | if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){
|
---|
319 | UtilXmlTools::displayErrorMessage("@ADD_INSIDE_NODE","attribute-name option is required if using attribute-value option.");
|
---|
320 | }
|
---|
321 |
|
---|
322 | while ( !fileStream.atEnd() && !line.startsWith("</xml>")){
|
---|
323 | line = fileStream.readLine();
|
---|
324 |
|
---|
325 | if(!line.startsWith("<xml>") && !line.startsWith("</xml>")){
|
---|
326 | xmlToInsert += line + "\n";
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | addToOperation(xmlToInsert,filters,filesWildcard);
|
---|
331 |
|
---|
332 | }
|
---|
333 | else if(line.startsWith("@REMOVE_NODE")){
|
---|
334 |
|
---|
335 | filters.setElementName(getPatchParameterValue(line,"ElementName"));
|
---|
336 | filters.setParentElementName(getPatchParameterValue(line,"ParentElementName"));
|
---|
337 | filters.setAttributeName(getPatchParameterValue(line,"AttributeName"));
|
---|
338 | filters.setAttributeValue(getPatchParameterValue(line,"AttributeValue"));
|
---|
339 |
|
---|
340 | if(this->forceTargetFilesWildcard!=""){
|
---|
341 | filesWildcard=this->forceTargetFilesWildcard;
|
---|
342 | }
|
---|
343 | else{
|
---|
344 | filesWildcard=getPatchParameterValue(line,"Files");
|
---|
345 | }
|
---|
346 |
|
---|
347 | // Check attribute filters
|
---|
348 | if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){
|
---|
349 | UtilXmlTools::displayErrorMessage("@REMOVE_NODE","attribute-value option is required if using attribute-name option.");
|
---|
350 | }
|
---|
351 |
|
---|
352 | if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){
|
---|
353 | UtilXmlTools::displayErrorMessage("@REMOVE_NODE","attribute-name option is required if using attribute-value option.");
|
---|
354 | }
|
---|
355 |
|
---|
356 | removeNodeOperation(filters,filesWildcard);
|
---|
357 |
|
---|
358 | }
|
---|
359 | else if(line.startsWith("@COMMAND")){
|
---|
360 |
|
---|
361 | command=getPatchParameterValue(line,"");
|
---|
362 |
|
---|
363 | executeCommandOperation(command);
|
---|
364 | }
|
---|
365 | else if(line.startsWith("@CUSTOM_CODE")){
|
---|
366 |
|
---|
367 | if(this->forceTargetFilesWildcard!=""){
|
---|
368 | filesWildcard=this->forceTargetFilesWildcard;
|
---|
369 | }
|
---|
370 | else{
|
---|
371 | filesWildcard=getPatchParameterValue(line,"Files");
|
---|
372 | }
|
---|
373 |
|
---|
374 | while ( !fileStream.atEnd() && !line.startsWith("</code>")){
|
---|
375 |
|
---|
376 | line = fileStream.readLine();
|
---|
377 |
|
---|
378 | if(!line.startsWith("<code>") && !line.startsWith("</code>")){
|
---|
379 | jsCode += line + "\n";
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | executeCustomCommandOperation(jsCode,filesWildcard);
|
---|
384 | }
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | QString XmlPatch::getPatchParameterValue(const QString& line, QString parameter){
|
---|
389 |
|
---|
390 | int startValueIdx, endValueIdx;
|
---|
391 |
|
---|
392 | parameter+=" "; // Parameters include a space before it's value.
|
---|
393 |
|
---|
394 | if(!line.contains(parameter)){
|
---|
395 | if(parameter=="ParentElementName " || parameter=="AttributeName " || parameter=="AttributeValue "){
|
---|
396 | return ""; // not mandatory
|
---|
397 | }
|
---|
398 | parameter.remove(" "); // just remove the space added so it doesn't look weird when outputted to the user
|
---|
399 |
|
---|
400 | UtilXmlTools::displayErrorMessage("Read patch file parameter","Couldn't retreive '" + parameter + "' parameter.");
|
---|
401 | }
|
---|
402 |
|
---|
403 | startValueIdx=line.indexOf(parameter); // get the position where parameter is defined
|
---|
404 | endValueIdx=line.indexOf("\"",startValueIdx)+1; // get the position where parameter value begins (+1 to ignore mandotory quote)
|
---|
405 |
|
---|
406 | startValueIdx=endValueIdx;
|
---|
407 | endValueIdx=line.indexOf("\"",startValueIdx); // get the last mandatory quote of the value
|
---|
408 |
|
---|
409 | return line.mid(startValueIdx,endValueIdx-startValueIdx); // return the value of the parameter (is in the middle of the mandatory quotes)
|
---|
410 | }
|
---|
411 |
|
---|
412 | #ifdef _USE_OLD_JS_ENGINE
|
---|
413 | void XmlPatch::displayJsException(QScriptEngine &engine, QScriptValue &engineResult){
|
---|
414 | if (engine.hasUncaughtException()) {
|
---|
415 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Uncaught js exception (user code) at line " +QString::number(engine.uncaughtExceptionLineNumber()) + ":\n" + engineResult.toString());
|
---|
416 | }
|
---|
417 | }
|
---|
418 | #else
|
---|
419 | void XmlPatch::checkForJsException(QJSValue &engineResult){
|
---|
420 | if (engineResult.isError()) {
|
---|
421 | UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Uncaught js exception (user code):\n" + engineResult.toString());
|
---|
422 | }
|
---|
423 | }
|
---|
424 | #endif
|
---|