1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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"
22 #include "game_share/persistent_data.h"
25 using namespace NLMISC
;
27 int main(int argc
, char *argv
[])
29 NLMISC::CApplicationContext context
;
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");
58 TConvMode mode
= cm_undefined
;
59 TSourceFormat sourceFormat
= sf_undefined
;
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);
76 sheetIdPath
= paramValue
;
88 outputFileName
= paramValue
;
91 fprintf(stderr
, "Unknown parameter '%s'", args
[i
].c_str());
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());
113 if (args[i].empty() || args[i][0] != '-' || args[i].size() < 2)
115 fprintf(stderr, "Invalid param '%s'\n", args[i].c_str());
119 string paramValue = args[i].substr(2);
124 sheetIdPath = paramValue;
136 outputFileName = paramValue;
138 fprintf(stderr, "Unknown parameter '%s'", args[i].c_str());
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());
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");
172 else if (fileName
.find(".txt") == (fileName
.size() -4))
174 printf("Choosing TXT->BINARY conversion mode");
177 else if (fileName
.find(".bin") == (fileName
.size() -4))
179 printf("Choosing BINARY->XML conversion mode");
184 fprintf(stderr
, "Missing conversion mode flag (-x|-b|-t) and can't deduce mode from filename extension");
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;
205 fprintf(stderr
, "Invalid source format, only support '.xml' files");
211 if (outputFileName
.empty() || filenames
.size() > 1)
213 // build a output name
214 outputFileName
= fileName
;
218 if (mode
== cm_to_binary
)
220 if (sourceFormat
== sf_txt
)
226 else if (mode
== cm_to_xml
)
231 else if (mode
== cm_to_txt
)
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
;
252 if (sourceFormat
== sf_txt
)
254 printf("Converting from txt to bin is currently unpossible ! use xml format");
257 // printf("Converting '%s' (TXT) to '%s' (BINARY)\n", fileName.c_str(), outputFileName.c_str() );
260 printf("Converting '%s' (XML) to '%s' (BINARY)\n", fileName
.c_str(), outputFileName
.c_str() );
261 if (!pdr
.readFromTxtFile(fileName
))
263 if (!pdr
.writeToBinFile(outputFileName
))
268 printf("Converting '%s' (BINARY) to '%s' (XML)\n", fileName
.c_str(), outputFileName
.c_str() );
269 if (!pdr
.readFromBinFile(fileName
))
271 if (!pdr
.writeToTxtFile(outputFileName
, CPersistentDataRecord::XML_STRING
))
276 printf("Converting '%s' (BINARY) to '%s' (TXT)\n", fileName
.c_str(), outputFileName
.c_str() );
277 if (!pdr
.readFromBinFile(fileName
))
279 if (!pdr
.writeToTxtFile(outputFileName
, CPersistentDataRecord::LINES_STRING
))
290 fprintf(stderr
, "Error while reading '%s', conversion aborted", fileName
.c_str());
294 fprintf(stderr
, "Error while writing '%s', conversion aborted", outputFileName
.c_str());