Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / ryzom / tools / pdr_util / pdr_util.cpp
blob7f1c13600096a9773222e9a3f2bce0acf258acce
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "nel/misc/types_nl.h"
18 #include "nel/misc/path.h"
19 #include "nel/misc/sheet_id.h"
20 #include <stdio.h>
21 #include <vector>
22 #include "game_share/persistent_data.h"
24 using namespace std;
25 using namespace NLMISC;
27 int main(int argc, char *argv[])
29 NLMISC::CApplicationContext context;
31 if (argc == 1)
33 printf("Usage : %s [-s<sheet_id_path>] [-x|-b|-t] [-o<outputFileName>] inputFileName\n", argv[0]);
34 printf(" -s : must contains the path where the sheet_id.bin can be found\n");
35 printf(" You could ignore this param is the sheet_id.bin can be found in the local directory\n");
36 printf(" -x : convert a binary pdr to XML format (exclude -b -t)\n");
37 printf(" -t : convert a binary pdr to text line format (exclude -b -x)\n");
38 printf(" -b : convert a XML or text pdr to binary format (exclude -x -t)\n");
39 printf(" -o : output filename\n");
40 return -1;
43 enum TConvMode
45 cm_undefined,
46 cm_to_xml,
47 cm_to_binary,
48 cm_to_txt,
51 enum TSourceFormat
53 sf_undefined,
54 sf_xml,
55 sf_txt
58 TConvMode mode = cm_undefined;
59 TSourceFormat sourceFormat = sf_undefined;
60 string sheetIdPath;
61 string fileName;
62 vector<string> filenames;
63 string outputFileName;
65 vector<string> args(argv, argv+argc);
67 for (uint i=1; i<args.size(); ++i)
69 if (args[i].size() >= 2 && args[i][0] == '-')
71 string paramValue = args[i].substr(2);
73 switch (args[i][1])
75 case 's':
76 sheetIdPath = paramValue;
77 break;
78 case 'x':
79 mode = cm_to_xml;
80 break;
81 case 't':
82 mode = cm_to_txt;
83 break;
84 case 'b':
85 mode = cm_to_binary;
86 break;
87 case 'o':
88 outputFileName = paramValue;
89 break;
90 default:
91 fprintf(stderr, "Unknown parameter '%s'", args[i].c_str());
92 return -1;
95 else if(!args[i].empty())
97 filenames.push_back(args[i]);
100 if (i == args.size()-1)
102 // last param, must be the filename
103 if (args[i].empty() || args[i][0] == '-')
105 fprintf(stderr, "Invalid or missing filename '%s'\n", args[i].c_str());
106 return -1;
109 fileName = args[i];
111 else
113 if (args[i].empty() || args[i][0] != '-' || args[i].size() < 2)
115 fprintf(stderr, "Invalid param '%s'\n", args[i].c_str());
116 return -1;
119 string paramValue = args[i].substr(2);
121 switch (args[i][1])
123 case 's':
124 sheetIdPath = paramValue;
125 break;
126 case 'x':
127 mode = cm_to_xml;
128 break;
129 case 't':
130 mode = cm_to_txt;
131 break;
132 case 'b':
133 mode = cm_to_binary;
134 break;
135 case 'o':
136 outputFileName = paramValue;
137 default:
138 fprintf(stderr, "Unknown parameter '%s'", args[i].c_str());
139 return -1;
145 // init the sheet id
146 if (!sheetIdPath.empty())
148 CPath::addSearchPath(sheetIdPath, false, false);
151 CSheetId::init(false);
153 for(uint f = 0; f < filenames.size(); f++)
155 fileName = filenames[f];
157 if (!CFile::isExists(fileName))
159 fprintf(stderr, "Couldn't find file '%s'", fileName.c_str());
160 // return -1;
161 continue;
164 if (mode == cm_undefined)
166 // try to automatically determine conversion mode
167 if (fileName.find(".xml") == (fileName.size() - 4))
169 printf("Choosing XML->BINARY conversion mode");
170 mode = cm_to_binary;
172 else if (fileName.find(".txt") == (fileName.size() -4))
174 printf("Choosing TXT->BINARY conversion mode");
175 mode = cm_to_binary;
177 else if (fileName.find(".bin") == (fileName.size() -4))
179 printf("Choosing BINARY->XML conversion mode");
180 mode = cm_to_xml;
182 else
184 fprintf(stderr, "Missing conversion mode flag (-x|-b|-t) and can't deduce mode from filename extension");
185 // return -1;
186 continue;
190 // determine source format when concerting to binary
191 if (mode == cm_to_binary)
193 if (fileName.find(".xml") == (fileName.size() - 4))
195 printf("Source file is in XML format");
196 sourceFormat = sf_xml;
198 /* else if (fileName.find(".txt") == (fileName.size() -4))
200 printf("Source file is in TXT format");
201 sourceFormat = sf_txt;
203 */ else
205 fprintf(stderr, "Invalid source format, only support '.xml' files");
206 // return -1;
207 continue;
211 if (outputFileName.empty() || filenames.size() > 1)
213 // build a output name
214 outputFileName = fileName;
215 string inExt;
216 string outExt;
218 if (mode == cm_to_binary)
220 if (sourceFormat == sf_txt)
221 inExt = ".txt";
222 else
223 inExt = ".xml";
224 outExt = ".bin";
226 else if (mode == cm_to_xml)
228 outExt = ".xml";
229 inExt = ".bin";
231 else if (mode == cm_to_txt)
233 outExt = ".txt";
234 inExt = ".bin";
237 if (outputFileName.find(inExt) == (outputFileName.size()-inExt.size()))
239 // remove input ext from output filename
240 outputFileName = outputFileName.substr(0, outputFileName.size()-inExt.size());
242 // append output extension
243 outputFileName += outExt;
246 static CPersistentDataRecord pdr;
247 pdr.clear();
249 switch(mode)
251 case cm_to_binary:
252 if (sourceFormat == sf_txt)
254 printf("Converting from txt to bin is currently unpossible ! use xml format");
255 // return -1;
256 continue;
257 // printf("Converting '%s' (TXT) to '%s' (BINARY)\n", fileName.c_str(), outputFileName.c_str() );
259 else
260 printf("Converting '%s' (XML) to '%s' (BINARY)\n", fileName.c_str(), outputFileName.c_str() );
261 if (!pdr.readFromTxtFile(fileName))
262 goto failureRead;
263 if (!pdr.writeToBinFile(outputFileName))
264 goto failureWrite;
265 break;
267 case cm_to_xml:
268 printf("Converting '%s' (BINARY) to '%s' (XML)\n", fileName.c_str(), outputFileName.c_str() );
269 if (!pdr.readFromBinFile(fileName))
270 goto failureRead;
271 if (!pdr.writeToTxtFile(outputFileName, CPersistentDataRecord::XML_STRING))
272 goto failureWrite;
273 break;
275 case cm_to_txt:
276 printf("Converting '%s' (BINARY) to '%s' (TXT)\n", fileName.c_str(), outputFileName.c_str() );
277 if (!pdr.readFromBinFile(fileName))
278 goto failureRead;
279 if (!pdr.writeToTxtFile(outputFileName, CPersistentDataRecord::LINES_STRING))
280 goto failureWrite;
281 break;
282 default:
283 break;
286 // return 0;
287 continue;
289 failureRead:
290 fprintf(stderr, "Error while reading '%s', conversion aborted", fileName.c_str());
291 // return -1;
292 continue;
293 failureWrite:
294 fprintf(stderr, "Error while writing '%s', conversion aborted", outputFileName.c_str());
295 // return -1;
296 continue;