File.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "File.h"
  24. #include "FileSystem.h"
  25. #include "Log.h"
  26. #include "MemoryBuffer.h"
  27. #include "PackageFile.h"
  28. #include "Profiler.h"
  29. #include <cstdio>
  30. #include <lz4.h>
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. #ifdef WIN32
  35. static const wchar_t* openMode[] =
  36. {
  37. L"rb",
  38. L"wb",
  39. L"w+b"
  40. };
  41. #else
  42. static const char* openMode[] =
  43. {
  44. "rb",
  45. "wb",
  46. "w+b"
  47. };
  48. #endif
  49. static const unsigned READ_BUFFER_SIZE = 32768;
  50. static const unsigned SKIP_BUFFER_SIZE = 1024;
  51. File::File(Context* context) :
  52. Object(context),
  53. mode_(FILE_READ),
  54. handle_(0),
  55. #ifdef ANDROID
  56. assetHandle_(0),
  57. #endif
  58. readBufferOffset_(0),
  59. readBufferSize_(0),
  60. offset_(0),
  61. checksum_(0),
  62. compressed_(false)
  63. {
  64. }
  65. File::File(Context* context, const String& fileName, FileMode mode) :
  66. Object(context),
  67. mode_(FILE_READ),
  68. handle_(0),
  69. #ifdef ANDROID
  70. assetHandle_(0),
  71. #endif
  72. readBufferOffset_(0),
  73. readBufferSize_(0),
  74. offset_(0),
  75. checksum_(0),
  76. compressed_(false)
  77. {
  78. Open(fileName, mode);
  79. }
  80. File::File(Context* context, PackageFile* package, const String& fileName) :
  81. Object(context),
  82. mode_(FILE_READ),
  83. handle_(0),
  84. #ifdef ANDROID
  85. assetHandle_(0),
  86. #endif
  87. readBufferOffset_(0),
  88. readBufferSize_(0),
  89. offset_(0),
  90. checksum_(0),
  91. compressed_(false)
  92. {
  93. Open(package, fileName);
  94. }
  95. File::~File()
  96. {
  97. Close();
  98. }
  99. bool File::Open(const String& fileName, FileMode mode)
  100. {
  101. Close();
  102. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  103. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  104. {
  105. LOGERROR("Access denied to " + fileName);
  106. return false;
  107. }
  108. #ifdef ANDROID
  109. if (fileName.StartsWith("/apk/"))
  110. {
  111. if (mode != FILE_READ)
  112. {
  113. LOGERROR("Only read mode is supported for asset files");
  114. return false;
  115. }
  116. assetHandle_ = SDL_RWFromFile(fileName.Substring(5).CString(), "rb");
  117. if (!assetHandle_)
  118. {
  119. LOGERROR("Could not open asset file " + fileName);
  120. return false;
  121. }
  122. else
  123. {
  124. fileName_ = fileName;
  125. mode_ = mode;
  126. position_ = 0;
  127. offset_ = 0;
  128. checksum_ = 0;
  129. size_ = assetHandle_->hidden.androidio.size;
  130. readBuffer_ = new unsigned char[READ_BUFFER_SIZE];
  131. readBufferOffset_ = 0;
  132. readBufferSize_ = 0;
  133. return true;
  134. }
  135. }
  136. #endif
  137. #ifdef WIN32
  138. handle_ = _wfopen(GetWideNativePath(fileName).CString(), openMode[mode]);
  139. #else
  140. handle_ = fopen(GetNativePath(fileName).CString(), openMode[mode]);
  141. #endif
  142. if (!handle_)
  143. {
  144. LOGERROR("Could not open file " + fileName);
  145. return false;
  146. }
  147. fileName_ = fileName;
  148. mode_ = mode;
  149. position_ = 0;
  150. offset_ = 0;
  151. checksum_ = 0;
  152. compressed_ = false;
  153. fseek((FILE*)handle_, 0, SEEK_END);
  154. size_ = ftell((FILE*)handle_);
  155. fseek((FILE*)handle_, 0, SEEK_SET);
  156. return true;
  157. }
  158. bool File::Open(PackageFile* package, const String& fileName)
  159. {
  160. Close();
  161. if (!package)
  162. return false;
  163. const PackageEntry* entry = package->GetEntry(fileName);
  164. if (!entry)
  165. return false;
  166. #ifdef WIN32
  167. handle_ = _wfopen(GetWideNativePath(package->GetName()).CString(), L"rb");
  168. #else
  169. handle_ = fopen(GetNativePath(package->GetName()).CString(), "rb");
  170. #endif
  171. if (!handle_)
  172. {
  173. LOGERROR("Could not open package file " + fileName);
  174. return false;
  175. }
  176. fileName_ = fileName;
  177. mode_ = FILE_READ;
  178. offset_ = entry->offset_;
  179. checksum_ = entry->checksum_;
  180. position_ = 0;
  181. size_ = entry->size_;
  182. compressed_ = package->IsCompressed();
  183. fseek((FILE*)handle_, offset_, SEEK_SET);
  184. return true;
  185. }
  186. unsigned File::Read(void* dest, unsigned size)
  187. {
  188. #ifdef ANDROID
  189. if (!handle_ && !assetHandle_)
  190. #else
  191. if (!handle_)
  192. #endif
  193. {
  194. // Do not log the error further here to prevent spamming the stderr stream
  195. return 0;
  196. }
  197. if (mode_ == FILE_WRITE)
  198. {
  199. LOGERROR("File not opened for reading");
  200. return 0;
  201. }
  202. if (size + position_ > size_)
  203. size = size_ - position_;
  204. if (!size)
  205. return 0;
  206. #ifdef ANDROID
  207. if (assetHandle_)
  208. {
  209. unsigned sizeLeft = size;
  210. unsigned char* destPtr = (unsigned char*)dest;
  211. while (sizeLeft)
  212. {
  213. if (readBufferOffset_ >= readBufferSize_)
  214. {
  215. readBufferSize_ = Min((int)size_ - position_, (int)READ_BUFFER_SIZE);
  216. readBufferOffset_ = 0;
  217. SDL_RWread(assetHandle_, readBuffer_.Get(), readBufferSize_, 1);
  218. }
  219. unsigned copySize = Min((int)(readBufferSize_ - readBufferOffset_), (int)sizeLeft);
  220. memcpy(destPtr, readBuffer_.Get() + readBufferOffset_, copySize);
  221. destPtr += copySize;
  222. sizeLeft -= copySize;
  223. readBufferOffset_ += copySize;
  224. position_ += copySize;
  225. }
  226. return size;
  227. }
  228. #endif
  229. if (compressed_)
  230. {
  231. unsigned sizeLeft = size;
  232. unsigned char* destPtr = (unsigned char*)dest;
  233. while (sizeLeft)
  234. {
  235. if (!readBuffer_ || readBufferOffset_ >= readBufferSize_)
  236. {
  237. unsigned char blockHeaderBytes[4];
  238. fread(blockHeaderBytes, sizeof blockHeaderBytes, 1, (FILE*)handle_);
  239. MemoryBuffer blockHeader(&blockHeaderBytes[0], sizeof blockHeaderBytes);
  240. unsigned unpackedSize = blockHeader.ReadUShort();
  241. unsigned packedSize = blockHeader.ReadUShort();
  242. if (!readBuffer_)
  243. {
  244. readBuffer_ = new unsigned char[unpackedSize];
  245. inputBuffer_ = new unsigned char[LZ4_compressBound(unpackedSize)];
  246. }
  247. /// \todo Handle errors
  248. fread(inputBuffer_.Get(), packedSize, 1, (FILE*)handle_);
  249. LZ4_decompress_fast((const char*)inputBuffer_.Get(), (char *)readBuffer_.Get(), unpackedSize);
  250. readBufferSize_ = unpackedSize;
  251. readBufferOffset_ = 0;
  252. }
  253. unsigned copySize = Min((int)(readBufferSize_ - readBufferOffset_), (int)sizeLeft);
  254. memcpy(destPtr, readBuffer_.Get() + readBufferOffset_, copySize);
  255. destPtr += copySize;
  256. sizeLeft -= copySize;
  257. readBufferOffset_ += copySize;
  258. position_ += copySize;
  259. }
  260. return size;
  261. }
  262. size_t ret = fread(dest, size, 1, (FILE*)handle_);
  263. if (ret != 1)
  264. {
  265. // Return to the position where the read began
  266. fseek((FILE*)handle_, position_ + offset_, SEEK_SET);
  267. LOGERROR("Error while reading from file " + GetName());
  268. return 0;
  269. }
  270. position_ += size;
  271. return size;
  272. }
  273. unsigned File::Seek(unsigned position)
  274. {
  275. #ifdef ANDROID
  276. if (!handle_ && !assetHandle_)
  277. #else
  278. if (!handle_)
  279. #endif
  280. {
  281. // Do not log the error further here to prevent spamming the stderr stream
  282. return 0;
  283. }
  284. // Allow sparse seeks if writing
  285. if (mode_ == FILE_READ && position > size_)
  286. position = size_;
  287. #ifdef ANDROID
  288. if (assetHandle_)
  289. {
  290. SDL_RWseek(assetHandle_, position, SEEK_SET);
  291. position_ = position;
  292. readBufferOffset_ = 0;
  293. readBufferSize_ = 0;
  294. return position_;
  295. }
  296. #endif
  297. if (compressed_)
  298. {
  299. // Start over from the beginning
  300. if (position == 0)
  301. {
  302. position_ = 0;
  303. readBufferOffset_ = 0;
  304. readBufferSize_ = 0;
  305. fseek((FILE*)handle_, offset_, SEEK_SET);
  306. }
  307. // Skip bytes
  308. else if (position >= position_)
  309. {
  310. unsigned char skipBuffer[SKIP_BUFFER_SIZE];
  311. while (position > position_)
  312. Read(skipBuffer, Min((int)position - position_, (int)SKIP_BUFFER_SIZE));
  313. }
  314. else
  315. LOGERROR("Seeking backward in a compressed file is not supported");
  316. return position_;
  317. }
  318. fseek((FILE*)handle_, position + offset_, SEEK_SET);
  319. position_ = position;
  320. return position_;
  321. }
  322. unsigned File::Write(const void* data, unsigned size)
  323. {
  324. if (!handle_)
  325. {
  326. // Do not log the error further here to prevent spamming the stderr stream
  327. return 0;
  328. }
  329. if (mode_ == FILE_READ)
  330. {
  331. LOGERROR("File not opened for writing");
  332. return 0;
  333. }
  334. if (!size)
  335. return 0;
  336. if (fwrite(data, size, 1, (FILE*)handle_) != 1)
  337. {
  338. // Return to the position where the write began
  339. fseek((FILE*)handle_, position_ + offset_, SEEK_SET);
  340. LOGERROR("Error while writing to file " + GetName());
  341. return 0;
  342. }
  343. position_ += size;
  344. if (position_ > size_)
  345. size_ = position_;
  346. return size;
  347. }
  348. unsigned File::GetChecksum()
  349. {
  350. if (offset_ || checksum_)
  351. return checksum_;
  352. if (!handle_ || mode_ == FILE_WRITE)
  353. return 0;
  354. PROFILE(CalculateFileChecksum);
  355. unsigned oldPos = position_;
  356. checksum_ = 0;
  357. Seek(0);
  358. while (!IsEof())
  359. {
  360. unsigned char block[1024];
  361. unsigned readBytes = Read(block, 1024);
  362. for (unsigned i = 0; i < readBytes; ++i)
  363. checksum_ = SDBMHash(checksum_, block[i]);
  364. }
  365. Seek(oldPos);
  366. return checksum_;
  367. }
  368. void File::Close()
  369. {
  370. #ifdef ANDROID
  371. if (assetHandle_)
  372. {
  373. SDL_RWclose(assetHandle_);
  374. assetHandle_ = 0;
  375. }
  376. #endif
  377. readBuffer_.Reset();
  378. inputBuffer_.Reset();
  379. if (handle_)
  380. {
  381. fclose((FILE*)handle_);
  382. handle_ = 0;
  383. position_ = 0;
  384. size_ = 0;
  385. offset_ = 0;
  386. checksum_ = 0;
  387. }
  388. }
  389. void File::Flush()
  390. {
  391. if (handle_)
  392. fflush((FILE*)handle_);
  393. }
  394. void File::SetName(const String& name)
  395. {
  396. fileName_ = name;
  397. }
  398. }