File.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. if (!handle_)
  189. {
  190. // Do not log the error further here to prevent spamming the stderr stream
  191. return 0;
  192. }
  193. if (mode_ == FILE_WRITE)
  194. {
  195. LOGERROR("File not opened for reading");
  196. return 0;
  197. }
  198. if (size + position_ > size_)
  199. size = size_ - position_;
  200. if (!size)
  201. return 0;
  202. #ifdef ANDROID
  203. if (assetHandle_)
  204. {
  205. unsigned sizeLeft = size;
  206. unsigned char* destPtr = (unsigned char*)dest;
  207. while (sizeLeft)
  208. {
  209. if (readBufferOffset_ >= readBufferSize_)
  210. {
  211. readBufferSize_ = Min((int)size_ - position_, (int)READ_BUFFER_SIZE);
  212. readBufferOffset_ = 0;
  213. SDL_RWread(assetHandle_, readBuffer_.Get(), readBufferSize_, 1);
  214. }
  215. unsigned copySize = Min((int)(readBufferSize_ - readBufferOffset_), (int)sizeLeft);
  216. memcpy(destPtr, readBuffer_.Get() + readBufferOffset_, copySize);
  217. destPtr += copySize;
  218. sizeLeft -= copySize;
  219. readBufferOffset_ += copySize;
  220. position_ += copySize;
  221. }
  222. return size;
  223. }
  224. #endif
  225. if (compressed_)
  226. {
  227. unsigned sizeLeft = size;
  228. unsigned char* destPtr = (unsigned char*)dest;
  229. while (sizeLeft)
  230. {
  231. if (!readBuffer_ || readBufferOffset_ >= readBufferSize_)
  232. {
  233. unsigned char blockHeaderBytes[4];
  234. fread(blockHeaderBytes, sizeof blockHeaderBytes, 1, (FILE*)handle_);
  235. MemoryBuffer blockHeader(&blockHeaderBytes[0], sizeof blockHeaderBytes);
  236. unsigned unpackedSize = blockHeader.ReadUShort();
  237. unsigned packedSize = blockHeader.ReadUShort();
  238. if (!readBuffer_)
  239. {
  240. readBuffer_ = new unsigned char[unpackedSize];
  241. inputBuffer_ = new unsigned char[LZ4_compressBound(unpackedSize)];
  242. }
  243. /// \todo Handle errors
  244. fread(inputBuffer_.Get(), packedSize, 1, (FILE*)handle_);
  245. LZ4_decompress_fast((const char*)inputBuffer_.Get(), (char *)readBuffer_.Get(), unpackedSize);
  246. readBufferSize_ = unpackedSize;
  247. readBufferOffset_ = 0;
  248. }
  249. unsigned copySize = Min((int)(readBufferSize_ - readBufferOffset_), (int)sizeLeft);
  250. memcpy(destPtr, readBuffer_.Get() + readBufferOffset_, copySize);
  251. destPtr += copySize;
  252. sizeLeft -= copySize;
  253. readBufferOffset_ += copySize;
  254. position_ += copySize;
  255. }
  256. return size;
  257. }
  258. size_t ret = fread(dest, size, 1, (FILE*)handle_);
  259. if (ret != 1)
  260. {
  261. // Return to the position where the read began
  262. fseek((FILE*)handle_, position_ + offset_, SEEK_SET);
  263. LOGERROR("Error while reading from file " + GetName());
  264. return 0;
  265. }
  266. position_ += size;
  267. return size;
  268. }
  269. unsigned File::Seek(unsigned position)
  270. {
  271. if (!handle_)
  272. {
  273. // Do not log the error further here to prevent spamming the stderr stream
  274. return 0;
  275. }
  276. // Allow sparse seeks if writing
  277. if (mode_ == FILE_READ && position > size_)
  278. position = size_;
  279. #ifdef ANDROID
  280. if (assetHandle_)
  281. {
  282. SDL_RWseek(assetHandle_, position, SEEK_SET);
  283. position_ = position;
  284. readBufferOffset_ = 0;
  285. readBufferSize_ = 0;
  286. return position_;
  287. }
  288. #endif
  289. if (compressed_)
  290. {
  291. // Start over from the beginning
  292. if (position == 0)
  293. {
  294. position_ = 0;
  295. readBufferOffset_ = 0;
  296. readBufferSize_ = 0;
  297. fseek((FILE*)handle_, offset_, SEEK_SET);
  298. }
  299. // Skip bytes
  300. else if (position >= position_)
  301. {
  302. unsigned char skipBuffer[SKIP_BUFFER_SIZE];
  303. while (position > position_)
  304. Read(skipBuffer, Min((int)position - position_, (int)SKIP_BUFFER_SIZE));
  305. }
  306. else
  307. LOGERROR("Seeking backward in a compressed file is not supported");
  308. return position_;
  309. }
  310. fseek((FILE*)handle_, position + offset_, SEEK_SET);
  311. position_ = position;
  312. return position_;
  313. }
  314. unsigned File::Write(const void* data, unsigned size)
  315. {
  316. if (!handle_)
  317. {
  318. // Do not log the error further here to prevent spamming the stderr stream
  319. return 0;
  320. }
  321. if (mode_ == FILE_READ)
  322. {
  323. LOGERROR("File not opened for writing");
  324. return 0;
  325. }
  326. if (!size)
  327. return 0;
  328. if (fwrite(data, size, 1, (FILE*)handle_) != 1)
  329. {
  330. // Return to the position where the write began
  331. fseek((FILE*)handle_, position_ + offset_, SEEK_SET);
  332. LOGERROR("Error while writing to file " + GetName());
  333. return 0;
  334. }
  335. position_ += size;
  336. if (position_ > size_)
  337. size_ = position_;
  338. return size;
  339. }
  340. unsigned File::GetChecksum()
  341. {
  342. if (offset_ || checksum_)
  343. return checksum_;
  344. if (!handle_ || mode_ == FILE_WRITE)
  345. return 0;
  346. PROFILE(CalculateFileChecksum);
  347. unsigned oldPos = position_;
  348. checksum_ = 0;
  349. Seek(0);
  350. while (!IsEof())
  351. {
  352. unsigned char block[1024];
  353. unsigned readBytes = Read(block, 1024);
  354. for (unsigned i = 0; i < readBytes; ++i)
  355. checksum_ = SDBMHash(checksum_, block[i]);
  356. }
  357. Seek(oldPos);
  358. return checksum_;
  359. }
  360. void File::Close()
  361. {
  362. #ifdef ANDROID
  363. if (assetHandle_)
  364. {
  365. SDL_RWclose(assetHandle_);
  366. assetHandle_ = 0;
  367. }
  368. #endif
  369. readBuffer_.Reset();
  370. inputBuffer_.Reset();
  371. if (handle_)
  372. {
  373. fclose((FILE*)handle_);
  374. handle_ = 0;
  375. position_ = 0;
  376. size_ = 0;
  377. offset_ = 0;
  378. checksum_ = 0;
  379. }
  380. }
  381. void File::Flush()
  382. {
  383. if (handle_)
  384. fflush((FILE*)handle_);
  385. }
  386. void File::SetName(const String& name)
  387. {
  388. fileName_ = name;
  389. }
  390. }