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 = 1024;
  50. File::File(Context* context) :
  51. Object(context),
  52. mode_(FILE_READ),
  53. handle_(0),
  54. #ifdef ANDROID
  55. assetHandle_(0),
  56. #endif
  57. readBufferOffset_(0),
  58. readBufferSize_(0),
  59. offset_(0),
  60. checksum_(0)
  61. {
  62. }
  63. File::File(Context* context, const String& fileName, FileMode mode) :
  64. Object(context),
  65. mode_(FILE_READ),
  66. handle_(0),
  67. #ifdef ANDROID
  68. assetHandle_(0),
  69. #endif
  70. readBufferOffset_(0),
  71. readBufferSize_(0),
  72. offset_(0),
  73. checksum_(0)
  74. {
  75. Open(fileName, mode);
  76. }
  77. File::File(Context* context, PackageFile* package, const String& fileName) :
  78. Object(context),
  79. mode_(FILE_READ),
  80. handle_(0),
  81. #ifdef ANDROID
  82. assetHandle_(0),
  83. #endif
  84. readBufferOffset_(0),
  85. readBufferSize_(0),
  86. offset_(0),
  87. checksum_(0)
  88. {
  89. Open(package, fileName);
  90. }
  91. File::~File()
  92. {
  93. Close();
  94. }
  95. bool File::Open(const String& fileName, FileMode mode)
  96. {
  97. Close();
  98. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  99. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  100. {
  101. LOGERROR("Access denied to " + fileName);
  102. return false;
  103. }
  104. #ifdef ANDROID
  105. if (fileName.StartsWith("/apk/"))
  106. {
  107. if (mode != FILE_READ)
  108. {
  109. LOGERROR("Only read mode is supported for asset files");
  110. return false;
  111. }
  112. assetHandle_ = SDL_RWFromFile(fileName.Substring(5).CString(), "rb");
  113. if (!assetHandle_)
  114. {
  115. LOGERROR("Could not open asset file " + fileName);
  116. return false;
  117. }
  118. else
  119. {
  120. fileName_ = fileName;
  121. mode_ = mode;
  122. position_ = 0;
  123. offset_ = 0;
  124. checksum_ = 0;
  125. size_ = assetHandle_->hidden.androidio.size;
  126. readBuffer_ = new unsigned char[READ_BUFFER_SIZE];
  127. readBufferOffset_ = 0;
  128. readBufferSize_ = 0;
  129. return true;
  130. }
  131. }
  132. #endif
  133. #ifdef WIN32
  134. handle_ = _wfopen(GetWideNativePath(fileName).CString(), openMode[mode]);
  135. #else
  136. handle_ = fopen(GetNativePath(fileName).CString(), openMode[mode]);
  137. #endif
  138. if (!handle_)
  139. {
  140. LOGERROR("Could not open file " + fileName);
  141. return false;
  142. }
  143. fileName_ = fileName;
  144. mode_ = mode;
  145. position_ = 0;
  146. offset_ = 0;
  147. checksum_ = 0;
  148. fseek((FILE*)handle_, 0, SEEK_END);
  149. size_ = ftell((FILE*)handle_);
  150. fseek((FILE*)handle_, 0, SEEK_SET);
  151. return true;
  152. }
  153. bool File::Open(PackageFile* package, const String& fileName)
  154. {
  155. Close();
  156. if (!package)
  157. return false;
  158. const PackageEntry* entry = package->GetEntry(fileName);
  159. if (!entry)
  160. return false;
  161. #ifdef WIN32
  162. handle_ = _wfopen(GetWideNativePath(package->GetName()).CString(), L"rb");
  163. #else
  164. handle_ = fopen(GetNativePath(package->GetName()).CString(), "rb");
  165. #endif
  166. if (!handle_)
  167. {
  168. LOGERROR("Could not open package file " + fileName);
  169. return false;
  170. }
  171. fileName_ = fileName;
  172. mode_ = FILE_READ;
  173. offset_ = entry->offset_;
  174. checksum_ = entry->checksum_;
  175. position_ = 0;
  176. size_ = entry->size_;
  177. if (package->IsCompressed())
  178. {
  179. readBuffer_ = new unsigned char[package->GetBlockSize()];
  180. readBufferOffset_ = 0;
  181. readBufferSize_ = 0;
  182. }
  183. fseek((FILE*)handle_, offset_, SEEK_SET);
  184. return true;
  185. }
  186. unsigned File::Read(void* dest, unsigned size)
  187. {
  188. if (mode_ == FILE_WRITE)
  189. {
  190. LOGERROR("File not opened for reading");
  191. return 0;
  192. }
  193. if (size + position_ > size_)
  194. size = size_ - position_;
  195. if (!size)
  196. return 0;
  197. #ifdef ANDROID
  198. if (assetHandle_)
  199. {
  200. unsigned sizeLeft = size;
  201. unsigned char* destPtr = (unsigned char*)dest;
  202. while (sizeLeft)
  203. {
  204. if (readBufferOffset_ >= readBufferSize_)
  205. {
  206. readBufferSize_ = Min((int)size_ - position_, (int)READ_BUFFER_SIZE);
  207. readBufferOffset_ = 0;
  208. SDL_RWread(assetHandle_, readBuffer_.Get(), readBufferSize_, 1);
  209. }
  210. unsigned copySize = Min((int)(readBufferSize_ - readBufferOffset_), (int)sizeLeft);
  211. memcpy(destPtr, readBuffer_.Get() + readBufferOffset_, copySize);
  212. destPtr += copySize;
  213. sizeLeft -= copySize;
  214. readBufferOffset_ += copySize;
  215. position_ += copySize;
  216. }
  217. return size;
  218. }
  219. #else
  220. // Package compressed mode
  221. if (readBuffer_)
  222. {
  223. unsigned sizeLeft = size;
  224. unsigned char* destPtr = (unsigned char*)dest;
  225. while (sizeLeft)
  226. {
  227. if (readBufferOffset_ >= readBufferSize_)
  228. {
  229. unsigned char blockHeaderBytes[4];
  230. fread(blockHeaderBytes, sizeof blockHeaderBytes, 1, (FILE*)handle_);
  231. MemoryBuffer blockHeader(&blockHeaderBytes[0], sizeof blockHeaderBytes);
  232. unsigned unpackedSize = blockHeader.ReadUShort();
  233. unsigned packedSize = blockHeader.ReadUShort();
  234. /// \todo Should reuse a block to hold the packed data
  235. /// \todo Handle errors
  236. SharedArrayPtr<unsigned char> packedBytes(new unsigned char[packedSize]);
  237. fread(packedBytes.Get(), packedSize, 1, (FILE*)handle_);
  238. LZ4_decompress_fast((const char*)packedBytes.Get(), (char *)readBuffer_.Get(), unpackedSize);
  239. readBufferSize_ = unpackedSize;
  240. readBufferOffset_ = 0;
  241. }
  242. unsigned copySize = Min((int)(readBufferSize_ - readBufferOffset_), (int)sizeLeft);
  243. memcpy(destPtr, readBuffer_.Get() + readBufferOffset_, copySize);
  244. destPtr += copySize;
  245. sizeLeft -= copySize;
  246. readBufferOffset_ += copySize;
  247. position_ += copySize;
  248. }
  249. return size;
  250. }
  251. #endif
  252. if (!handle_)
  253. {
  254. LOGERROR("File not open");
  255. return 0;
  256. }
  257. size_t ret = fread(dest, size, 1, (FILE*)handle_);
  258. if (ret != 1)
  259. {
  260. // Return to the position where the read began
  261. fseek((FILE*)handle_, position_ + offset_, SEEK_SET);
  262. LOGERROR("Error while reading from file " + GetName());
  263. return 0;
  264. }
  265. position_ += size;
  266. return size;
  267. }
  268. unsigned File::Seek(unsigned position)
  269. {
  270. // Allow sparse seeks if writing
  271. if (mode_ == FILE_READ && position > size_)
  272. position = size_;
  273. #ifdef ANDROID
  274. if (assetHandle_)
  275. {
  276. SDL_RWseek(assetHandle_, position, SEEK_SET);
  277. position_ = position;
  278. readBufferOffset_ = 0;
  279. readBufferSize_ = 0;
  280. return position_;
  281. }
  282. #else
  283. // Package compressed mode
  284. if (readBuffer_)
  285. {
  286. // Start over from the beginning
  287. if (position == 0)
  288. {
  289. position_ = 0;
  290. readBufferOffset_ = 0;
  291. readBufferSize_ = 0;
  292. fseek((FILE*)handle_, offset_, SEEK_SET);
  293. }
  294. // Skip bytes
  295. else if (position >= position_)
  296. {
  297. unsigned char skipBuffer[READ_BUFFER_SIZE];
  298. while (position > position_)
  299. Read(skipBuffer, Min((int)position - position_, (int)READ_BUFFER_SIZE));
  300. }
  301. else
  302. LOGERROR("Seeking backward in a compressed file is not supported");
  303. return position_;
  304. }
  305. #endif
  306. if (!handle_)
  307. {
  308. LOGERROR("File not open");
  309. return 0;
  310. }
  311. fseek((FILE*)handle_, position + offset_, SEEK_SET);
  312. position_ = position;
  313. return position_;
  314. }
  315. unsigned File::Write(const void* data, unsigned size)
  316. {
  317. if (mode_ == FILE_READ)
  318. {
  319. LOGERROR("File not opened for writing");
  320. return 0;
  321. }
  322. if (!handle_)
  323. {
  324. LOGERROR("File not open");
  325. return 0;
  326. }
  327. if (!size)
  328. return 0;
  329. if (fwrite(data, size, 1, (FILE*)handle_) != 1)
  330. {
  331. // Return to the position where the write began
  332. fseek((FILE*)handle_, position_ + offset_, SEEK_SET);
  333. LOGERROR("Error while writing to file " + GetName());
  334. return 0;
  335. }
  336. position_ += size;
  337. if (position_ > size_)
  338. size_ = position_;
  339. return size;
  340. }
  341. unsigned File::GetChecksum()
  342. {
  343. if (offset_ || checksum_)
  344. return checksum_;
  345. if (!handle_ || mode_ == FILE_WRITE)
  346. return 0;
  347. PROFILE(CalculateFileChecksum);
  348. unsigned oldPos = position_;
  349. checksum_ = 0;
  350. Seek(0);
  351. while (!IsEof())
  352. {
  353. unsigned char block[1024];
  354. unsigned readBytes = Read(block, 1024);
  355. for (unsigned i = 0; i < readBytes; ++i)
  356. checksum_ = SDBMHash(checksum_, block[i]);
  357. }
  358. Seek(oldPos);
  359. return checksum_;
  360. }
  361. void File::Close()
  362. {
  363. #ifdef ANDROID
  364. if (assetHandle_)
  365. {
  366. SDL_RWclose(assetHandle_);
  367. assetHandle_ = 0;
  368. }
  369. #endif
  370. readBuffer_.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. }