Asset.cpp
Go to the documentation of this file.00001 //*** Asset.cpp *** 00002 00003 #include "Asset.h" 00004 #include "Debug.h" 00005 #include "Platform_FileSystem.h" 00006 #include "Platform_FileSystem_File.h" 00007 #include "DynamicBuffer.h" 00008 #include "Archive.h" 00009 #include "ArchiveFile.h" 00010 #include "StaticBuffer.h" 00011 #include "ArchiveManager.h" 00012 #include "StandardLibrary.h" 00013 #include "Filename.h" 00014 00015 00016 //*** Constructor Memory Buffer*** 00017 00018 Asset::Asset(const StaticBuffer* staticBuffer): 00019 file_(0), 00020 archiveFile_(0), 00021 staticBuffer_(staticBuffer), 00022 dynamicBuffer_(0), 00023 fileIsOpen_(false), 00024 readOnly_(false) 00025 { 00026 } 00027 00028 00029 //*** Constructor Memory Buffer*** 00030 00031 Asset::Asset(DynamicBuffer* dynamicBuffer): 00032 file_(0), 00033 archiveFile_(0), 00034 staticBuffer_(0), 00035 dynamicBuffer_(dynamicBuffer), 00036 fileIsOpen_(false), 00037 readOnly_(false) 00038 { 00039 } 00040 00041 00042 //*** Constructor File (Filename) *** 00043 00044 Asset::Asset(const Filename& filename): 00045 filename_(filename), 00046 file_(0), 00047 archiveFile_(0), 00048 staticBuffer_(0), 00049 dynamicBuffer_(0), 00050 fileIsOpen_(false), 00051 readOnly_(false) 00052 { 00053 CreateFromFilename(filename_); 00054 } 00055 00056 00057 //*** Constructor File (StringId) *** 00058 00059 Asset::Asset(const StringId& filenameId): 00060 filename_(filenameId), 00061 file_(0), 00062 archiveFile_(0), 00063 staticBuffer_(0), 00064 dynamicBuffer_(0), 00065 fileIsOpen_(false), 00066 readOnly_(false) 00067 { 00068 CreateFromFilename(filename_); 00069 } 00070 00071 00072 //*** Constructor File (const char*) *** 00073 00074 Asset::Asset(const char* filenameStr): 00075 filename_(filenameStr), 00076 file_(0), 00077 archiveFile_(0), 00078 staticBuffer_(0), 00079 dynamicBuffer_(0), 00080 fileIsOpen_(false), 00081 readOnly_(false) 00082 { 00083 CreateFromFilename(filename_); 00084 } 00085 00086 00087 //*** CreateFromFilename *** 00088 00089 void Asset::CreateFromFilename(const Filename& filename) 00090 { 00091 // First, try and get the file from the archive manager, if it is present 00092 if (ArchiveManager::IsInstanceCreated()) 00093 { 00094 archiveFile_=siArchiveManager->GetFile(filename); 00095 } 00096 00097 // If we couldn't get the directory from the archive manager, try to get it from the file system, if it is present 00098 if (!archiveFile_ && Platform::GetPlatform_FileSystem()) 00099 { 00100 file_=Platform::GetPlatform_FileSystem()->CreateFileObject(filename.GetString()); 00101 } 00102 00103 // If we couldn't get the file from neither the archive manager nor the filesystem, we trigger an assert for 00104 // debug builds, and just reports the file as being empty for release builds. 00105 Assert(archiveFile_ || file_, 00106 "Error creating AssetEnumerator - No filesystem or archive manager present, or file not found."); 00107 } 00108 00109 00110 //*** Destructor *** 00111 00112 Asset::~Asset() 00113 { 00114 if (file_) 00115 { 00116 if (fileIsOpen_) 00117 { 00118 file_->Close(); 00119 } 00120 delete file_; 00121 } 00122 00123 if (archiveFile_) 00124 { 00125 if (fileIsOpen_) 00126 { 00127 archiveFile_->Close(); 00128 } 00129 delete archiveFile_; 00130 } 00131 } 00132 00133 00134 //*** Copy Constructor *** 00135 00136 Asset::Asset(const Asset& asset): 00137 file_(0), 00138 archiveFile_(0), 00139 staticBuffer_(0), 00140 dynamicBuffer_(0), 00141 fileIsOpen_(false), 00142 readOnly_(false) 00143 { 00144 staticBuffer_=asset.staticBuffer_; 00145 dynamicBuffer_=asset.dynamicBuffer_; 00146 fileIsOpen_=asset.fileIsOpen_; 00147 readOnly_=asset.readOnly_; 00148 if (asset.file_) 00149 { 00150 const char* filename=asset.file_->GetPath(); 00151 if (Platform::GetPlatform_FileSystem()) 00152 { 00153 file_=file_=Platform::GetPlatform_FileSystem()->CreateFileObject(filename); 00154 } 00155 if (fileIsOpen_) 00156 { 00157 file_->Open(); 00158 file_->SetPosition(asset.file_->GetPosition()); 00159 } 00160 } 00161 if (asset.archiveFile_) 00162 { 00163 archiveFile_=new ArchiveFile(asset.archiveFile_); 00164 if (fileIsOpen_) 00165 { 00166 archiveFile_->Open(); 00167 archiveFile_->Seek(asset.archiveFile_->Tell()); 00168 } 00169 } 00170 00171 } 00172 00173 00174 //*** Copy Operator *** 00175 00176 const Asset& Asset::operator=(const Asset& asset) 00177 { 00178 if (file_) 00179 { 00180 if (fileIsOpen_) 00181 { 00182 file_->Close(); 00183 } 00184 delete file_; 00185 } 00186 00187 if (archiveFile_) 00188 { 00189 if (fileIsOpen_) 00190 { 00191 archiveFile_->Close(); 00192 } 00193 delete archiveFile_; 00194 } 00195 00196 dynamicBuffer_=asset.dynamicBuffer_; 00197 staticBuffer_=asset.staticBuffer_; 00198 fileIsOpen_=asset.fileIsOpen_; 00199 readOnly_=asset.readOnly_; 00200 file_=0; 00201 if (asset.file_) 00202 { 00203 const char* filename=asset.file_->GetPath(); 00204 if (Platform::GetPlatform_FileSystem()) 00205 { 00206 file_=file_=Platform::GetPlatform_FileSystem()->CreateFileObject(filename); 00207 } 00208 if (fileIsOpen_) 00209 { 00210 file_->Open(); 00211 file_->SetPosition(asset.file_->GetPosition()); 00212 } 00213 } 00214 00215 if (asset.archiveFile_) 00216 { 00217 archiveFile_=new ArchiveFile(asset.archiveFile_); 00218 if (fileIsOpen_) 00219 { 00220 archiveFile_->Open(); 00221 archiveFile_->Seek(asset.archiveFile_->Tell()); 00222 } 00223 } 00224 return *this; 00225 } 00226 00227 00228 //*** GetFilename *** 00229 00230 const Filename& Asset::GetFilename() const 00231 { 00232 return filename_; 00233 } 00234 00235 00236 //*** Open *** 00237 00238 bool Asset::Open() const 00239 { 00240 if (archiveFile_ && !fileIsOpen_) 00241 { 00242 fileIsOpen_=archiveFile_->Open(); 00243 readOnly_=true; 00244 return fileIsOpen_; 00245 } 00246 00247 if (dynamicBuffer_) 00248 { 00249 dynamicBuffer_->SetPosition(0); 00250 readOnly_=true; 00251 fileIsOpen_=true; 00252 return fileIsOpen_; 00253 } 00254 00255 if (staticBuffer_) 00256 { 00257 staticBuffer_->SetPosition(0); 00258 readOnly_=true; 00259 fileIsOpen_=true; 00260 return fileIsOpen_; 00261 } 00262 00263 if (file_ && !fileIsOpen_) 00264 { 00265 fileIsOpen_=file_->Open(); 00266 readOnly_=true; 00267 return fileIsOpen_; 00268 } 00269 00270 return false; 00271 } 00272 00273 00274 //*** Create *** 00275 00276 bool Asset::Create() 00277 { 00278 if (archiveFile_ && !fileIsOpen_) 00279 { 00280 readOnly_=true; 00281 return false; 00282 } 00283 00284 if (dynamicBuffer_) 00285 { 00286 readOnly_=false; 00287 return true; 00288 } 00289 00290 if (staticBuffer_) 00291 { 00292 readOnly_=true; 00293 return false; 00294 } 00295 00296 if (file_ && !fileIsOpen_) 00297 { 00298 fileIsOpen_=file_->Create(); 00299 readOnly_=!fileIsOpen_; 00300 return fileIsOpen_; 00301 } 00302 00303 readOnly_=true; 00304 return false; 00305 } 00306 00307 00308 //*** Close *** 00309 00310 bool Asset::Close() const 00311 { 00312 readOnly_=true; 00313 00314 if (archiveFile_ && fileIsOpen_) 00315 { 00316 fileIsOpen_=false; 00317 return archiveFile_->Close(); 00318 } 00319 00320 if (dynamicBuffer_) 00321 { 00322 return true; 00323 } 00324 00325 if (staticBuffer_) 00326 { 00327 return true; 00328 } 00329 00330 if (file_ && fileIsOpen_) 00331 { 00332 fileIsOpen_=false; 00333 return file_->Close(); 00334 } 00335 00336 return false; 00337 } 00338 00339 00340 //*** GetSize *** 00341 00342 int Asset::GetSize() const 00343 { 00344 if (archiveFile_) 00345 { 00346 return archiveFile_->GetSize(); 00347 } 00348 00349 if (dynamicBuffer_) 00350 { 00351 return dynamicBuffer_->GetSize(); 00352 } 00353 00354 if (staticBuffer_) 00355 { 00356 return staticBuffer_->GetSize(); 00357 } 00358 00359 if (file_) 00360 { 00361 int size=0; 00362 if (fileIsOpen_) 00363 { 00364 int currentPosition=file_->GetPosition(); 00365 file_->SetPosition(0,Platform_FileSystem_File::ORIGIN_END); 00366 size=file_->GetPosition(); 00367 file_->SetPosition(currentPosition,Platform_FileSystem_File::ORIGIN_START); 00368 } 00369 else 00370 { 00371 if (file_->Open()) 00372 { 00373 file_->SetPosition(0,Platform_FileSystem_File::ORIGIN_END); 00374 size=file_->GetPosition(); 00375 file_->Close(); 00376 } 00377 } 00378 return size; 00379 } 00380 00381 return 0; 00382 } 00383 00384 00385 //*** Seek *** 00386 00387 int Asset::Seek(int offset, SeekOrigin origin) const 00388 { 00389 if (archiveFile_) 00390 { 00391 switch (origin) 00392 { 00393 case SEEK_FROM_START: 00394 { 00395 return archiveFile_->Seek(offset,ArchiveFile::SEEK_FROM_START); 00396 } break; 00397 case SEEK_FROM_CURRENT: 00398 { 00399 return archiveFile_->Seek(offset,ArchiveFile::SEEK_FROM_CURRENT); 00400 } break; 00401 case SEEK_FROM_END: 00402 { 00403 return archiveFile_->Seek(offset,ArchiveFile::SEEK_FROM_END); 00404 } break; 00405 } 00406 return -1; 00407 } 00408 00409 if (dynamicBuffer_) 00410 { 00411 switch (origin) 00412 { 00413 case SEEK_FROM_START: 00414 { 00415 dynamicBuffer_->SetPosition(offset); 00416 return dynamicBuffer_->GetPosition(); 00417 } break; 00418 case SEEK_FROM_CURRENT: 00419 { 00420 dynamicBuffer_->SetPosition(dynamicBuffer_->GetPosition()+offset); 00421 return dynamicBuffer_->GetPosition(); 00422 } break; 00423 case SEEK_FROM_END: 00424 { 00425 dynamicBuffer_->SetPosition(dynamicBuffer_->GetSize()-offset); 00426 return dynamicBuffer_->GetPosition(); 00427 } break; 00428 } 00429 return -1; 00430 } 00431 00432 if (staticBuffer_) 00433 { 00434 switch (origin) 00435 { 00436 case SEEK_FROM_START: 00437 { 00438 staticBuffer_->SetPosition(offset); 00439 return staticBuffer_->GetPosition(); 00440 } break; 00441 case SEEK_FROM_CURRENT: 00442 { 00443 staticBuffer_->SetPosition(staticBuffer_->GetPosition()+offset); 00444 return staticBuffer_->GetPosition(); 00445 } break; 00446 case SEEK_FROM_END: 00447 { 00448 staticBuffer_->SetPosition(staticBuffer_->GetSize()-offset); 00449 return staticBuffer_->GetPosition(); 00450 } break; 00451 } 00452 return -1; 00453 } 00454 00455 if (file_) 00456 { 00457 Platform_FileSystem_File::SetPositionOrigin fileOrigin=Platform_FileSystem_File::ORIGIN_START; 00458 switch (origin) 00459 { 00460 case SEEK_FROM_START: 00461 { 00462 fileOrigin=Platform_FileSystem_File::ORIGIN_START; 00463 } break; 00464 case SEEK_FROM_CURRENT: 00465 { 00466 fileOrigin=Platform_FileSystem_File::ORIGIN_CURRENT; 00467 } break; 00468 case SEEK_FROM_END: 00469 { 00470 fileOrigin=Platform_FileSystem_File::ORIGIN_END; 00471 } break; 00472 } 00473 return file_->SetPosition(offset,fileOrigin); 00474 } 00475 00476 return -1; 00477 } 00478 00479 00480 //*** Tell *** 00481 00482 int Asset::Tell() const 00483 { 00484 if (archiveFile_) 00485 { 00486 return archiveFile_->Tell(); 00487 } 00488 00489 00490 if (dynamicBuffer_) 00491 { 00492 return dynamicBuffer_->GetPosition(); 00493 } 00494 00495 if (staticBuffer_) 00496 { 00497 return staticBuffer_->GetPosition(); 00498 } 00499 00500 if (file_) 00501 { 00502 return file_->GetPosition(); 00503 } 00504 00505 return 0; 00506 } 00507 00508 00509 //*** WRITEMACRO *** 00510 00511 // This macro defines the code that is to be executed for all of the overloaded Write methods 00512 // The code is identical for all the different Write methods, only the input parameters change, 00513 // so it makes more sense to use a macro rather than copy-paste. This macro is undefined as 00514 // soon as it is not needed anymore 00515 00516 #define WRITEMACRO() \ 00517 Assert(!readOnly_,"Asset is read only!"); \ 00518 if (readOnly_) \ 00519 { \ 00520 return 0; \ 00521 } \ 00522 if (dynamicBuffer_) \ 00523 return dynamicBuffer_->Write(value,count); \ 00524 if (file_) \ 00525 return file_->Write(value,count); \ 00526 return 0; \ 00527 00528 00529 //** Write methods */ 00530 00531 int Asset::Write(const char* value, int count) 00532 { 00533 WRITEMACRO(); 00534 } 00535 00536 int Asset::Write(const short* value, int count) 00537 { 00538 WRITEMACRO(); 00539 } 00540 00541 int Asset::Write(const int* value, int count) 00542 { 00543 WRITEMACRO(); 00544 } 00545 00546 int Asset::Write(const long* value, int count) 00547 { 00548 WRITEMACRO(); 00549 } 00550 00551 int Asset::Write(const unsigned char* value, int count) 00552 { 00553 WRITEMACRO(); 00554 } 00555 00556 int Asset::Write(const unsigned short* value, int count) 00557 { 00558 WRITEMACRO(); 00559 } 00560 00561 int Asset::Write(const unsigned int* value, int count) 00562 { 00563 WRITEMACRO(); 00564 } 00565 00566 int Asset::Write(const unsigned long* value, int count) 00567 { 00568 WRITEMACRO(); 00569 } 00570 00571 int Asset::Write(const float* value, int count) 00572 { 00573 WRITEMACRO(); 00574 } 00575 00576 int Asset::Write(const double* value, int count) 00577 { 00578 WRITEMACRO(); 00579 } 00580 00581 int Asset::Write(const bool* value, int count) 00582 { 00583 WRITEMACRO(); 00584 } 00585 00586 // No need for the write macro anymore, so undefine it 00587 #undef WRITEMACRO 00588 00589 00590 //*** READMACRO *** 00591 00592 // This macro defines the code that is to be executed for all of the overloaded Read methods 00593 // The code is identical for all the different Read methods, only the input parameters change, 00594 // so it makes more sense to use a macro rather than copy-paste. This macro is undefined as 00595 // soon as it is not needed anymore 00596 00597 #define READMACRO() \ 00598 if (archiveFile_) \ 00599 return archiveFile_->Read(value,count); \ 00600 if (dynamicBuffer_) \ 00601 return dynamicBuffer_->Read(value,count); \ 00602 if (staticBuffer_) \ 00603 return staticBuffer_->Read(value,count); \ 00604 if (file_) \ 00605 return file_->Read(value,count); \ 00606 return 0; \ 00607 00608 00609 //** Read methods */ 00610 00611 00612 int Asset::Read(char* value, int count) const 00613 { 00614 READMACRO(); 00615 } 00616 00617 int Asset::Read(short* value, int count) const 00618 { 00619 READMACRO(); 00620 } 00621 00622 int Asset::Read(int* value, int count) const 00623 { 00624 READMACRO(); 00625 } 00626 00627 int Asset::Read(long* value, int count) const 00628 { 00629 READMACRO(); 00630 } 00631 00632 int Asset::Read(unsigned char* value, int count) const 00633 { 00634 READMACRO(); 00635 } 00636 00637 int Asset::Read(unsigned short* value, int count) const 00638 { 00639 READMACRO(); 00640 } 00641 00642 int Asset::Read(unsigned int* value, int count) const 00643 { 00644 READMACRO(); 00645 } 00646 00647 int Asset::Read(unsigned long* value, int count) const 00648 { 00649 READMACRO(); 00650 } 00651 00652 int Asset::Read(float* value, int count) const 00653 { 00654 READMACRO(); 00655 } 00656 00657 int Asset::Read(double* value, int count) const 00658 { 00659 READMACRO(); 00660 } 00661 00662 int Asset::Read(bool* value, int count) const 00663 { 00664 READMACRO(); 00665 } 00666 00667 // No need for the read macro anymore, so undefine it 00668 #undef READMACRO 00669 00670
Reproduction/republishing of any material on this site without permission is strictly prohibited.
