00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "stdmisc.h"
00031
00032 #include "nel/misc/file.h"
00033 #include "nel/misc/path.h"
00034
00035 #include "nel/misc/sheet_id.h"
00036 #include "nel/misc/common.h"
00037
00038 using namespace std;
00039
00040 namespace NLMISC {
00041
00042 map<uint32,std::string> CSheetId::_SheetIdToName;
00043 map<std::string,uint32> CSheetId::_SheetNameToId;
00044 vector<std::string> CSheetId::_FileExtensions;
00045 bool CSheetId::_Initialised=false;
00046 bool CSheetId::_RemoveUnknownSheet=true;
00047
00048 const CSheetId CSheetId::Unknown(0);
00049
00050 void CSheetId::cbFileChange (const std::string &filename)
00051 {
00052 nlinfo ("%s changed, reload it", filename.c_str());
00053
00054 loadSheetId();
00055 }
00056
00057
00058
00059
00060
00061 CSheetId::CSheetId( const string& sheetName )
00062 {
00063 if (!build(sheetName))
00064 {
00065 nlwarning("<CSheetId::CSheetId> The sheet %s is not in sheet_id.bin, setting it to Unknown",sheetName.c_str());
00066 *this = Unknown;
00067 }
00068
00069 }
00070
00071
00072
00073
00074
00075
00076 bool CSheetId::build(const std::string& sheetName)
00077 {
00078 nlassert(_Initialised);
00079
00080 map<string,uint32>::const_iterator itId = _SheetNameToId.find( strlwr(sheetName) );
00081 if( itId != _SheetNameToId.end() )
00082 {
00083 _Id.Id = (*itId).second;
00084 return true;
00085 }
00086 else
00087 {
00088 return false;
00089 }
00090 }
00091
00092 void CSheetId::loadSheetId ()
00093 {
00094
00095 CIFile file;
00096 if(file.open(CPath::lookup("sheet_id.bin", false, false)))
00097 {
00098
00099 _FileExtensions.clear ();
00100 _SheetIdToName.clear ();
00101 _SheetNameToId.clear ();
00102
00103
00104 _FileExtensions.resize(256);
00105
00106
00107 file.serialCont(_SheetIdToName);
00108
00109
00110 file.close();
00111
00112 if (_RemoveUnknownSheet)
00113 {
00114 uint32 removednbfiles = 0;
00115 uint32 nbfiles = _SheetIdToName.size();
00116
00117
00118 map<uint32,string>::iterator itStr2;
00119 for( itStr2 = _SheetIdToName.begin(); itStr2 != _SheetIdToName.end(); )
00120 {
00121 if (CPath::exists ((*itStr2).second))
00122 {
00123 ++itStr2;
00124 }
00125 else
00126 {
00127 map<uint32,string>::iterator olditStr = itStr2;
00128
00129 itStr2++;
00130 _SheetIdToName.erase (olditStr);
00131 removednbfiles++;
00132 }
00133 }
00134
00135 nlinfo ("Removed %d files on %d from CSheetId because these files doesn't exists", removednbfiles, nbfiles);
00136 }
00137
00138
00139 map<uint32,string>::iterator itStr;
00140 for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
00141 {
00142
00143 _SheetNameToId.insert( make_pair(strlwr((*itStr).second),(*itStr).first) );
00144
00145
00146 TSheetId sheetId;
00147 sheetId.Id=(*itStr).first;
00148 uint8 type= sheetId.IdInfos.Type;
00149
00150
00151 if (_FileExtensions[type].empty())
00152 {
00153
00154 _FileExtensions[type]=strlwr(CFile::getExtension((*itStr).second));
00155 }
00156 }
00157 }
00158 else
00159 {
00160 nlerror("<CSheetId::init> Can't open the file sheet_id.bin");
00161 }
00162 }
00163
00164
00165
00166
00167
00168 void CSheetId::init(bool removeUnknownSheet)
00169 {
00170
00171 if (_Initialised)
00172 return;
00173
00174 CFile::addFileChangeCallback ("sheet_id.bin", cbFileChange);
00175
00176 _RemoveUnknownSheet = removeUnknownSheet;
00177
00178 loadSheetId ();
00179
00180 _Initialised=true;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190 CSheetId& CSheetId::operator=( const CSheetId& sheetId )
00191 {
00192 nlassert(_Initialised);
00193
00194 if(this == &sheetId)
00195 {
00196 return *this;
00197 }
00198
00199 _Id.Id = sheetId.asInt();
00200
00201 return *this;
00202
00203
00204 }
00205
00206
00207
00208
00209
00210
00211 CSheetId& CSheetId::operator=( const string& sheetName )
00212 {
00213 nlassert(_Initialised);
00214
00215 map<string,uint32>::const_iterator itId = _SheetNameToId.find( strlwr(sheetName) );
00216 if( itId != _SheetNameToId.end() )
00217 {
00218 _Id.Id = (*itId).second;
00219 }
00220 else
00221 {
00222 nlwarning("<CSheetId::operator=> The sheet %s is not in sheet_id.bin, setting it to Unknown",sheetName.c_str());
00223 *this = Unknown;
00224 }
00225
00226 return *this;
00227
00228 }
00229
00230
00231
00232
00233
00234
00235 CSheetId& CSheetId::operator=( uint32 sheetRef )
00236 {
00237 nlassert(_Initialised);
00238
00239 _Id.Id = sheetRef;
00240
00241 return *this;
00242
00243 }
00244
00245
00246
00247
00248
00249
00250
00251 bool CSheetId::operator < (const CSheetId& sheetRef ) const
00252 {
00253 nlassert(_Initialised);
00254
00255 if (_Id.Id < sheetRef.asInt())
00256 {
00257 return true;
00258 }
00259
00260 return false;
00261
00262 }
00263
00264
00265
00266
00267
00268
00269
00270 string CSheetId::toString() const
00271 {
00272 nlassert(_Initialised);
00273
00274 map<uint32,string>::const_iterator itStr = _SheetIdToName.find( _Id.Id );
00275 if( itStr != _SheetIdToName.end() )
00276 {
00277 return (*itStr).second;
00278 }
00279 else
00280 {
00281
00282
00283
00284 return NLMISC::toString( "<Sheet %d not found in sheet_id.bin>", _Id.Id );
00285 }
00286
00287 }
00288
00289
00290
00291
00292
00293
00294
00295 void CSheetId::display()
00296 {
00297 nlassert(_Initialised);
00298
00299 map<uint32,string>::const_iterator itStr;
00300 for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
00301 {
00302
00303 nlinfo("(%08x %d) %s",(*itStr).first,(*itStr).first,(*itStr).second.c_str());
00304 }
00305
00306 }
00307
00308
00309
00310
00311
00312
00313
00314 void CSheetId::display(uint8 type)
00315 {
00316 nlassert(_Initialised);
00317
00318 map<uint32,string>::const_iterator itStr;
00319 for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
00320 {
00321
00322 TSheetId sheetId;
00323 sheetId.Id=(*itStr).first;
00324
00325
00326 if (type==sheetId.IdInfos.Type)
00327 {
00328
00329 nlinfo("(%08x %d) %s",(*itStr).first,(*itStr).first,(*itStr).second.c_str());
00330 }
00331 }
00332
00333 }
00334
00335
00336
00337
00338
00339
00340
00341 void CSheetId::buildIdVector(std::vector <CSheetId> &result)
00342 {
00343 nlassert(_Initialised);
00344
00345 map<uint32,string>::const_iterator itStr;
00346 for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
00347 {
00348 result.push_back( (CSheetId)(*itStr).first );
00349 }
00350
00351 }
00352
00353
00354
00355
00356
00357
00358 void CSheetId::buildIdVector(std::vector <CSheetId> &result,uint8 type)
00359 {
00360 nlassert(_Initialised);
00361
00362 map<uint32,string>::const_iterator itStr;
00363 for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
00364 {
00365
00366 TSheetId sheetId;
00367 sheetId.Id=(*itStr).first;
00368
00369
00370 if (type==sheetId.IdInfos.Type)
00371 {
00372 result.push_back( (CSheetId)sheetId.Id );
00373 }
00374 }
00375
00376 }
00377
00378
00379
00380
00381
00382 void CSheetId::buildIdVector(std::vector <CSheetId> &result, std::vector <std::string> &resultFilenames,uint8 type)
00383 {
00384 nlassert(_Initialised);
00385
00386 map<uint32,string>::const_iterator itStr;
00387 for( itStr = _SheetIdToName.begin(); itStr != _SheetIdToName.end(); ++itStr )
00388 {
00389
00390 TSheetId sheetId;
00391 sheetId.Id=(*itStr).first;
00392
00393
00394 if (type==sheetId.IdInfos.Type)
00395 {
00396 result.push_back( (CSheetId)sheetId.Id );
00397 resultFilenames.push_back( (*itStr).second );
00398 }
00399 }
00400
00401 }
00402
00403
00404
00405
00406
00407 void CSheetId::buildIdVector(std::vector <CSheetId> &result,const std::string &fileExtension)
00408 {
00409 uint32 type=typeFromFileExtension(fileExtension);
00410 if (type!=(uint32)~0)
00411 buildIdVector(result,(uint8)type);
00412
00413 }
00414
00415
00416
00417
00418
00419 void CSheetId::buildIdVector(std::vector <CSheetId> &result, std::vector <std::string> &resultFilenames,const std::string &fileExtension)
00420 {
00421 uint32 type=typeFromFileExtension(fileExtension);
00422 if (type!=(uint32)~0)
00423 buildIdVector(result,resultFilenames, (uint8)type);
00424
00425 }
00426
00427
00428
00429
00430
00431
00432 uint32 CSheetId::typeFromFileExtension(const std::string &fileExtension)
00433 {
00434 nlassert(_Initialised);
00435
00436 unsigned i;
00437 for (i=0;i<_FileExtensions.size();i++)
00438 if (strlwr(fileExtension)==_FileExtensions[i])
00439 return i;
00440
00441 return ~0;
00442 }
00443
00444
00445
00446
00447
00448
00449 const std::string &CSheetId::fileExtensionFromType(uint8 type)
00450 {
00451 nlassert(_Initialised);
00452 nlassert(type<256);
00453
00454 return _FileExtensions[type];
00455
00456 }
00457
00458 }