Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Environment.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_WINRT
  3. #include "os\Environment.h"
  4. #include "os\Win32\WindowsHelpers.h"
  5. #include "utils\Expected.h"
  6. #include "utils\Il2CppError.h"
  7. #include "utils\StringUtils.h"
  8. #include <windows.storage.h>
  9. #include <wrl.h>
  10. using namespace ABI::Windows::Storage;
  11. using namespace Microsoft::WRL;
  12. using namespace Microsoft::WRL::Wrappers;
  13. namespace il2cpp
  14. {
  15. namespace os
  16. {
  17. #define CSIDL_DESKTOP 0x0000 // <desktop>
  18. #define CSIDL_PROGRAMS 0x0002 // Start Menu\Programs
  19. #define CSIDL_PERSONAL 0x0005 // My Documents
  20. #define CSIDL_FAVORITES 0x0006 // <user name>\Favorites
  21. #define CSIDL_STARTUP 0x0007 // Start Menu\Programs\Startup
  22. #define CSIDL_RECENT 0x0008 // <user name>\Recent
  23. #define CSIDL_SENDTO 0x0009 // <user name>\SendTo
  24. #define CSIDL_STARTMENU 0x000b // <user name>\Start Menu
  25. #define CSIDL_MYMUSIC 0x000d // "My Music" folder
  26. #define CSIDL_MYVIDEO 0x000e // "My Videos" folder
  27. #define CSIDL_DESKTOPDIRECTORY 0x0010 // <user name>\Desktop
  28. #define CSIDL_DRIVES 0x0011 // My Computer
  29. #define CSIDL_NETWORK 0x0012 // Network Neighborhood (My Network Places)
  30. #define CSIDL_NETHOOD 0x0013 // <user name>\nethood
  31. #define CSIDL_FONTS 0x0014 // windows\fonts
  32. #define CSIDL_TEMPLATES 0x0015
  33. #define CSIDL_COMMON_STARTMENU 0x0016 // All Users\Start Menu
  34. #define CSIDL_COMMON_PROGRAMS 0X0017 // All Users\Start Menu\Programs
  35. #define CSIDL_COMMON_STARTUP 0x0018 // All Users\Startup
  36. #define CSIDL_COMMON_DESKTOPDIRECTORY 0x0019 // All Users\Desktop
  37. #define CSIDL_APPDATA 0x001a // <user name>\Application Data
  38. #define CSIDL_PRINTHOOD 0x001b // <user name>\PrintHood
  39. #define CSIDL_LOCAL_APPDATA 0x001c // <user name>\Local Settings\Applicaiton Data (non roaming)
  40. #define CSIDL_ALTSTARTUP 0x001d // non localized startup
  41. #define CSIDL_COMMON_ALTSTARTUP 0x001e // non localized common startup
  42. #define CSIDL_COMMON_FAVORITES 0x001f
  43. #define CSIDL_INTERNET_CACHE 0x0020
  44. #define CSIDL_COOKIES 0x0021
  45. #define CSIDL_HISTORY 0x0022
  46. #define CSIDL_COMMON_APPDATA 0x0023 // All Users\Application Data
  47. #define CSIDL_WINDOWS 0x0024 // GetWindowsDirectory()
  48. #define CSIDL_SYSTEM 0x0025 // GetSystemDirectory()
  49. #define CSIDL_PROGRAM_FILES 0x0026 // C:\Program Files
  50. #define CSIDL_MYPICTURES 0x0027 // C:\Program Files\My Pictures
  51. #define CSIDL_PROFILE 0x0028 // USERPROFILE
  52. #define CSIDL_SYSTEMX86 0x0029 // x86 system directory on RISC
  53. #define CSIDL_PROGRAM_FILESX86 0x002a // x86 C:\Program Files on RISC
  54. #define CSIDL_PROGRAM_FILES_COMMON 0x002b // C:\Program Files\Common
  55. #define CSIDL_PROGRAM_FILES_COMMONX86 0x002c // x86 Program Files\Common on RISC
  56. #define CSIDL_COMMON_TEMPLATES 0x002d // All Users\Templates
  57. #define CSIDL_COMMON_DOCUMENTS 0x002e // All Users\Documents
  58. #define CSIDL_COMMON_ADMINTOOLS 0x002f // All Users\Start Menu\Programs\Administrative Tools
  59. #define CSIDL_ADMINTOOLS 0x0030 // <user name>\Start Menu\Programs\Administrative Tools
  60. #define CSIDL_CONNECTIONS 0x0031 // Network and Dial-up Connections
  61. #define CSIDL_COMMON_MUSIC 0x0035 // All Users\My Music
  62. #define CSIDL_COMMON_PICTURES 0x0036 // All Users\My Pictures
  63. #define CSIDL_COMMON_VIDEO 0x0037 // All Users\My Video
  64. #define CSIDL_RESOURCES 0x0038 // Resource Direcotry
  65. #define CSIDL_RESOURCES_LOCALIZED 0x0039 // Localized Resource Direcotry
  66. #define CSIDL_COMMON_OEM_LINKS 0x003a // Links to All Users OEM specific apps
  67. #define CSIDL_CDBURN_AREA 0x003b // USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
  68. #define CSIDL_COMPUTERSNEARME 0x003d // Computers Near Me (computered from Workgroup membership)
  69. template<typename T>
  70. static inline utils::Expected<std::string> GetAppFolder(T appDataToStorageFolder)
  71. {
  72. ComPtr<IApplicationDataStatics> appDataStatics;
  73. ComPtr<IApplicationData> appData;
  74. ComPtr<IStorageFolder> appDataFolder;
  75. ComPtr<IStorageItem> appDataFolderItem;
  76. HString appDataPath;
  77. auto hr = RoGetActivationFactory(HStringReference(RuntimeClass_Windows_Storage_ApplicationData).Get(), __uuidof(IApplicationDataStatics), &appDataStatics);
  78. if (IL2CPP_HR_FAILED(hr))
  79. return utils::Il2CppError(utils::ComError, hr);
  80. hr = appDataStatics->get_Current(&appData);
  81. if (IL2CPP_HR_FAILED(hr))
  82. return utils::Il2CppError(utils::ComError, hr);
  83. hr = appDataToStorageFolder(appData.Get(), &appDataFolder);
  84. if (IL2CPP_HR_FAILED(hr))
  85. return utils::Il2CppError(utils::ComError, hr);
  86. hr = appDataFolder.As(&appDataFolderItem);
  87. if (IL2CPP_HR_FAILED(hr))
  88. return utils::Il2CppError(utils::ComError, hr);
  89. hr = appDataFolderItem->get_Path(appDataPath.GetAddressOf());
  90. if (IL2CPP_HR_FAILED(hr))
  91. return utils::Il2CppError(utils::ComError, hr);
  92. unsigned int dummy;
  93. return utils::StringUtils::Utf16ToUtf8(appDataPath.GetRawBuffer(&dummy));
  94. }
  95. static inline utils::Expected<std::string> GetLocalAppDataFolder()
  96. {
  97. return GetAppFolder([](IApplicationData* appData, IStorageFolder** folder) { return appData->get_LocalFolder(folder); });
  98. }
  99. static inline utils::Expected<std::string> GetRoamingAppDataFolder()
  100. {
  101. return GetAppFolder([](IApplicationData* appData, IStorageFolder** folder) { return appData->get_RoamingFolder(folder); });
  102. }
  103. template<typename T>
  104. static utils::Expected<std::string> GetUserFolderPath(T&& userDataPathToFolderPathConverter)
  105. {
  106. ComPtr<ABI::Windows::Storage::IUserDataPathsStatics> userDataPathsStatics;
  107. auto hr = RoGetActivationFactory(HStringReference(L"Windows.Storage.UserDataPaths").Get(), __uuidof(userDataPathsStatics), &userDataPathsStatics);
  108. if (FAILED(hr))
  109. {
  110. // Before OS version 16299, you weren't allowed to touch these folders.
  111. // In OS version 16299 they added UserDataPaths class for this purpose
  112. // If that class does not exist, we throw UnauthorizedAccessException
  113. return utils::Il2CppError(utils::UnauthorizedAccess, "Failed getting the path of a special folder: Access Denied.");
  114. }
  115. ComPtr<ABI::Windows::Storage::IUserDataPaths> userDataPaths;
  116. hr = userDataPathsStatics->GetDefault(&userDataPaths);
  117. if (IL2CPP_HR_FAILED(hr))
  118. return utils::Il2CppError(utils::ComError, hr);
  119. HString resultHString;
  120. hr = userDataPathToFolderPathConverter(userDataPaths.Get(), resultHString.GetAddressOf());
  121. if (IL2CPP_HR_FAILED(hr))
  122. return utils::Il2CppError(utils::ComError, hr);
  123. unsigned int dummy;
  124. return utils::StringUtils::Utf16ToUtf8(resultHString.GetRawBuffer(&dummy));
  125. }
  126. utils::Expected<std::string> Environment::GetWindowsFolderPath(int32_t folder)
  127. {
  128. switch (folder)
  129. {
  130. case CSIDL_APPDATA:
  131. return GetRoamingAppDataFolder();
  132. case CSIDL_COOKIES:
  133. return GetUserFolderPath([](ABI::Windows::Storage::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Cookies(result); });
  134. case CSIDL_DESKTOP:
  135. return GetUserFolderPath([](ABI::Windows::Storage::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Desktop(result); });
  136. case CSIDL_FAVORITES:
  137. return GetUserFolderPath([](ABI::Windows::Storage::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Favorites(result); });
  138. case CSIDL_HISTORY:
  139. return GetUserFolderPath([](ABI::Windows::Storage::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_History(result); });
  140. case CSIDL_INTERNET_CACHE:
  141. return GetUserFolderPath([](ABI::Windows::Storage::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_InternetCache(result); });
  142. case CSIDL_MYMUSIC:
  143. return GetUserFolderPath([](ABI::Windows::Storage::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Music(result); });
  144. case CSIDL_MYPICTURES:
  145. return GetUserFolderPath([](ABI::Windows::Storage::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Pictures(result); });
  146. case CSIDL_MYVIDEO:
  147. return GetUserFolderPath([](ABI::Windows::Storage::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Videos(result); });
  148. case CSIDL_PERSONAL:
  149. return GetUserFolderPath([](ABI::Windows::Storage::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Documents(result); });
  150. case CSIDL_PROFILE:
  151. return GetUserFolderPath([](ABI::Windows::Storage::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Profile(result); });
  152. case CSIDL_RECENT:
  153. return GetUserFolderPath([](ABI::Windows::Storage::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Recent(result); });
  154. case CSIDL_TEMPLATES:
  155. return GetUserFolderPath([](ABI::Windows::Storage::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Templates(result); });
  156. case CSIDL_LOCAL_APPDATA:
  157. return GetLocalAppDataFolder();
  158. default:
  159. return utils::Il2CppError(utils::UnauthorizedAccess, "Failed getting the path of a special folder: Access Denied.");
  160. }
  161. return std::string();
  162. }
  163. utils::Expected<bool> Environment::Is64BitOs()
  164. {
  165. #if IL2CPP_TARGET_WINRT
  166. BOOL isWow64Process = FALSE;
  167. if (IsWow64Process(GetCurrentProcess(), &isWow64Process))
  168. return isWow64Process == TRUE;
  169. return false;
  170. #endif // IL2CPP_TARGET_WINRT
  171. return true;
  172. }
  173. }
  174. }
  175. #endif