ApkFile.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <android/asset_manager_jni.h>
  24. #include "ApkFile.h"
  25. #include "Assert.h"
  26. static AAssetManager* g_android_asset_manager = NULL;
  27. //-----------------------------------------------------------------------------
  28. AAssetManager* get_android_asset_manager()
  29. {
  30. return g_android_asset_manager;
  31. }
  32. //-----------------------------------------------------------------------------
  33. extern "C" JNIEXPORT void JNICALL Java_crown_android_CrownLib_initAssetManager(JNIEnv* env, jobject obj, jobject assetManager)
  34. {
  35. g_android_asset_manager = AAssetManager_fromJava(env, assetManager);
  36. }
  37. namespace crown
  38. {
  39. //-----------------------------------------------------------------------------
  40. ApkFile::ApkFile(const char* path)
  41. : File(FOM_READ), m_asset(NULL)
  42. {
  43. m_asset = AAssetManager_open(get_android_asset_manager(), path, AASSET_MODE_RANDOM);
  44. CE_ASSERT(m_asset != NULL, "Unable to open file: %s", path);
  45. }
  46. //-----------------------------------------------------------------------------
  47. ApkFile::~ApkFile()
  48. {
  49. if (m_asset != NULL)
  50. {
  51. AAsset_close(m_asset);
  52. m_asset = NULL;
  53. }
  54. }
  55. //-----------------------------------------------------------------------------
  56. void ApkFile::seek(size_t position)
  57. {
  58. off_t seek_result = AAsset_seek(m_asset, (off_t)position, SEEK_SET);
  59. CE_ASSERT(seek_result != (off_t) -1, "Failed to seek");
  60. }
  61. //-----------------------------------------------------------------------------
  62. void ApkFile::seek_to_end()
  63. {
  64. off_t seek_result = AAsset_seek(m_asset, 0, SEEK_END);
  65. CE_ASSERT(seek_result != (off_t) -1, "Failed to seek to end");
  66. }
  67. //-----------------------------------------------------------------------------
  68. void ApkFile::skip(size_t bytes)
  69. {
  70. off_t seek_result = AAsset_seek(m_asset, (off_t) bytes, SEEK_CUR);
  71. CE_ASSERT(seek_result != (off_t) -1, "Failed to skip");
  72. }
  73. //-----------------------------------------------------------------------------
  74. void ApkFile::read(void* buffer, size_t size)
  75. {
  76. CE_ASSERT_NOT_NULL(buffer);
  77. size_t bytes_read = (size_t) AAsset_read(m_asset, buffer, size);
  78. CE_ASSERT(bytes_read == size, "Failed to read from file: requested: %lu, read: %lu", size, bytes_read);
  79. }
  80. //-----------------------------------------------------------------------------
  81. void ApkFile::write(const void* /*buffer*/, size_t /*size*/)
  82. {
  83. CE_ASSERT(false, "Attempt to write to android assets folder!");
  84. }
  85. //-----------------------------------------------------------------------------
  86. bool ApkFile::copy_to(File& /*file*/, size_t /*size = 0*/)
  87. {
  88. CE_ASSERT(false, "Not implemented yet :(");
  89. return false;
  90. }
  91. //-----------------------------------------------------------------------------
  92. void ApkFile::flush()
  93. {
  94. // Not needed
  95. }
  96. //-----------------------------------------------------------------------------
  97. bool ApkFile::is_valid() const
  98. {
  99. return m_asset != NULL;
  100. }
  101. //-----------------------------------------------------------------------------
  102. bool ApkFile::end_of_file() const
  103. {
  104. return AAsset_getRemainingLength(m_asset) == 0;
  105. }
  106. //-----------------------------------------------------------------------------
  107. size_t ApkFile::size() const
  108. {
  109. return AAsset_getLength(m_asset);
  110. }
  111. //-----------------------------------------------------------------------------
  112. size_t ApkFile::position() const
  113. {
  114. return (size_t) (AAsset_getLength(m_asset) - AAsset_getRemainingLength(m_asset));
  115. }
  116. //-----------------------------------------------------------------------------
  117. bool ApkFile::can_read() const
  118. {
  119. return true;
  120. }
  121. //-----------------------------------------------------------------------------
  122. bool ApkFile::can_write() const
  123. {
  124. return false;
  125. }
  126. //-----------------------------------------------------------------------------
  127. bool ApkFile::can_seek() const
  128. {
  129. return true;
  130. }
  131. } // namespace crown