File.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_WINRT || IL2CPP_TARGET_XBOXONE
  3. #include <io.h>
  4. #include "os/File.h"
  5. #include "os/Win32/WindowsHeaders.h"
  6. #include "utils/Expected.h"
  7. #include "utils/Il2CppError.h"
  8. namespace il2cpp
  9. {
  10. namespace os
  11. {
  12. static inline std::wstring GetDirectoryForStandardOutput()
  13. {
  14. #if IL2CPP_TARGET_XBOXONE
  15. return L"D:\\";
  16. #elif IL2CPP_TARGET_WINRT
  17. wchar_t buffer[MAX_PATH + 2];
  18. uint32_t tempPathLength = GetTempPathW(MAX_PATH + 2, buffer);
  19. return std::wstring(buffer, tempPathLength);
  20. #else
  21. #error Unknown platform
  22. #endif
  23. }
  24. static FileHandle* GetOrCreateRedirectedHandle(FILE* stdFile, const wchar_t* fileNameOnDisk)
  25. {
  26. #if IL2CPP_DEBUG || IL2CPP_DEVELOPMENT
  27. auto stdFd = _fileno(stdFile);
  28. auto stdHandle = reinterpret_cast<FileHandle*>(_get_osfhandle(stdFd));
  29. if (stdHandle != INVALID_HANDLE_VALUE && !_isatty(stdFd))
  30. return stdHandle;
  31. std::wstring pathOnDisk = GetDirectoryForStandardOutput() + fileNameOnDisk;
  32. auto redirectedFile = _wfreopen(pathOnDisk.c_str(), L"w+", stdFile);
  33. return reinterpret_cast<FileHandle*>(_get_osfhandle(_fileno(redirectedFile)));
  34. #else
  35. return NULL;
  36. #endif
  37. }
  38. utils::Expected<bool> File::Isatty(FileHandle* fileHandle)
  39. {
  40. return utils::Il2CppError(utils::NotSupported, "File::Isatty is not supported on WinRT");
  41. }
  42. FileHandle* File::GetStdInput()
  43. {
  44. return GetOrCreateRedirectedHandle(stdin, L"stdin.txt");
  45. }
  46. FileHandle* File::GetStdError()
  47. {
  48. return GetOrCreateRedirectedHandle(stderr, L"stderr.txt");
  49. }
  50. FileHandle* File::GetStdOutput()
  51. {
  52. return GetOrCreateRedirectedHandle(stdout, L"stdout.txt");
  53. }
  54. } //os
  55. } //il2cpp
  56. #endif