File.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Container/ArrayPtr.h"
  6. #include "../Core/Object.h"
  7. #include "../IO/AbstractFile.h"
  8. #ifdef __ANDROID__
  9. struct SDL_RWops;
  10. #endif
  11. namespace Urho3D
  12. {
  13. #ifdef __ANDROID__
  14. extern const char* APK;
  15. // Macro for checking if a given pathname is inside APK's assets directory
  16. #define URHO3D_IS_ASSET(p) p.StartsWith(APK)
  17. // Macro for truncating the APK prefix string from the asset pathname and at the same time patching the directory name components (see custom_rules.xml)
  18. #ifdef ASSET_DIR_INDICATOR
  19. #define URHO3D_ASSET(p) p.Substring(5).Replaced("/", ASSET_DIR_INDICATOR "/").CString()
  20. #else
  21. #define URHO3D_ASSET(p) p.Substring(5).CString()
  22. #endif
  23. #endif
  24. /// File open mode.
  25. enum FileMode
  26. {
  27. FILE_READ = 0,
  28. FILE_WRITE,
  29. FILE_READWRITE
  30. };
  31. class PackageFile;
  32. /// %File opened either through the filesystem or from within a package file.
  33. class URHO3D_API File : public Object, public AbstractFile
  34. {
  35. URHO3D_OBJECT(File, Object);
  36. public:
  37. /// Construct.
  38. explicit File(Context* context);
  39. /// Construct and open a filesystem file.
  40. File(Context* context, const String& fileName, FileMode mode = FILE_READ);
  41. /// Construct and open from a package file.
  42. File(Context* context, PackageFile* package, const String& fileName);
  43. /// Destruct. Close the file if open.
  44. ~File() override;
  45. /// Read bytes from the file. Return number of bytes actually read.
  46. i32 Read(void* dest, i32 size) override;
  47. /// Set position from the beginning of the file.
  48. i64 Seek(i64 position) override;
  49. /// Write bytes to the file. Return number of bytes actually written.
  50. i32 Write(const void* data, i32 size) override;
  51. /// Return a checksum of the file contents using the SDBM hash algorithm.
  52. hash32 GetChecksum() override;
  53. /// Open a filesystem file. Return true if successful.
  54. bool Open(const String& fileName, FileMode mode = FILE_READ);
  55. /// Open from within a package file. Return true if successful.
  56. bool Open(PackageFile* package, const String& fileName);
  57. /// Close the file.
  58. void Close();
  59. /// Flush any buffered output to the file.
  60. void Flush();
  61. /// Return the open mode.
  62. /// @property
  63. FileMode GetMode() const { return mode_; }
  64. /// Return whether is open.
  65. /// @property
  66. bool IsOpen() const;
  67. /// Return the file handle.
  68. void* GetHandle() const { return handle_; }
  69. /// Return whether the file originates from a package.
  70. /// @property
  71. bool IsPackaged() const { return offset_ != 0; }
  72. private:
  73. /// Open file internally using either C standard IO functions or SDL RWops for Android asset files. Return true if successful.
  74. bool OpenInternal(const String& fileName, FileMode mode, bool fromPackage = false);
  75. /// Perform the file read internally using either C standard IO functions or SDL RWops for Android asset files. Return true if successful. This does not handle compressed package file reading.
  76. bool ReadInternal(void* dest, i32 size);
  77. /// Seek in file internally using either C standard IO functions or SDL RWops for Android asset files.
  78. void SeekInternal(i64 newPosition);
  79. /// Open mode.
  80. FileMode mode_;
  81. /// File handle.
  82. void* handle_;
  83. #ifdef __ANDROID__
  84. /// SDL RWops context for Android asset loading.
  85. SDL_RWops* assetHandle_;
  86. #endif
  87. /// Read buffer for Android asset or compressed file loading.
  88. SharedArrayPtr<u8> readBuffer_;
  89. /// Decompression input buffer for compressed file loading.
  90. SharedArrayPtr<u8> inputBuffer_;
  91. /// Read buffer position.
  92. i32 readBufferOffset_;
  93. /// Bytes in the current read buffer.
  94. i32 readBufferSize_;
  95. /// Start position within a package file, 0 for regular files.
  96. i64 offset_;
  97. /// Content checksum.
  98. hash32 checksum_;
  99. /// Compression flag.
  100. bool compressed_;
  101. /// Synchronization needed before read -flag.
  102. bool readSyncNeeded_;
  103. /// Synchronization needed before write -flag.
  104. bool writeSyncNeeded_;
  105. };
  106. }