File.cpp 8.9 KB

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