MetadataLoader.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "il2cpp-config.h"
  2. #include "MetadataLoader.h"
  3. #include "os/File.h"
  4. #include "os/Mutex.h"
  5. #include "utils/MemoryMappedFile.h"
  6. #include "utils/PathUtils.h"
  7. #include "utils/Runtime.h"
  8. #include "utils/Logging.h"
  9. #if IL2CPP_TARGET_ANDROID && IL2CPP_TINY_DEBUGGER && !IL2CPP_TINY_FROM_IL2CPP_BUILDER
  10. #include <stdlib.h>
  11. extern "C"
  12. {
  13. void* loadAsset(const char* path, int *size, void* (*alloc)(size_t));
  14. }
  15. #elif IL2CPP_TARGET_JAVASCRIPT && IL2CPP_TINY_DEBUGGER && !IL2CPP_TINY_FROM_IL2CPP_BUILDER
  16. extern void* g_MetadataForWebTinyDebugger;
  17. #endif
  18. void* il2cpp::vm::MetadataLoader::LoadMetadataFile(const char* fileName)
  19. {
  20. #if IL2CPP_TARGET_ANDROID && IL2CPP_TINY_DEBUGGER && !IL2CPP_TINY_FROM_IL2CPP_BUILDER
  21. std::string resourcesDirectory = utils::PathUtils::Combine(utils::StringView<char>("Data"), utils::StringView<char>("Metadata"));
  22. std::string resourceFilePath = utils::PathUtils::Combine(resourcesDirectory, utils::StringView<char>(fileName, strlen(fileName)));
  23. int size = 0;
  24. return loadAsset(resourceFilePath.c_str(), &size, malloc);
  25. #elif IL2CPP_TARGET_JAVASCRIPT && IL2CPP_TINY_DEBUGGER && !IL2CPP_TINY_FROM_IL2CPP_BUILDER
  26. return g_MetadataForWebTinyDebugger;
  27. #else
  28. std::string resourcesDirectory = utils::PathUtils::Combine(utils::Runtime::GetDataDir(), utils::StringView<char>("Metadata"));
  29. std::string resourceFilePath = utils::PathUtils::Combine(resourcesDirectory, utils::StringView<char>(fileName, strlen(fileName)));
  30. int error = 0;
  31. os::FileHandle* handle = os::File::Open(resourceFilePath, kFileModeOpen, kFileAccessRead, kFileShareRead, kFileOptionsNone, &error);
  32. if (error != 0)
  33. {
  34. utils::Logging::Write("ERROR: Could not open %s", resourceFilePath.c_str());
  35. return NULL;
  36. }
  37. void* fileBuffer = utils::MemoryMappedFile::Map(handle);
  38. os::File::Close(handle, &error);
  39. if (error != 0)
  40. {
  41. utils::MemoryMappedFile::Unmap(fileBuffer);
  42. fileBuffer = NULL;
  43. return NULL;
  44. }
  45. return fileBuffer;
  46. #endif
  47. }
  48. void il2cpp::vm::MetadataLoader::UnloadMetadataFile(void* fileBuffer)
  49. {
  50. #if IL2CPP_TARGET_ANDROID && IL2CPP_TINY_DEBUGGER && !IL2CPP_DEBUGGER_TESTS
  51. free(fileBuffer);
  52. #else
  53. bool success = il2cpp::utils::MemoryMappedFile::Unmap(fileBuffer);
  54. NO_UNUSED_WARNING(success);
  55. IL2CPP_ASSERT(success);
  56. #endif
  57. }