暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Environment.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "il2cpp-config.h"
  2. #if !IL2CPP_USE_GENERIC_ENVIRONMENT && IL2CPP_TARGET_POSIX && !IL2CPP_USE_PLATFORM_SPECIFIC_ENVIRON
  3. #include "il2cpp-class-internals.h"
  4. #include "os/Environment.h"
  5. #include "il2cpp-api.h"
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sys/utsname.h>
  9. #if defined(__APPLE__) && !defined(__arm__)
  10. // Apple defines this in crt_externs.h but doesn't provide that header for
  11. // arm-apple-darwin9. We'll manually define the symbol on Apple as it does
  12. // in fact exist on all implementations (so far)
  13. extern "C" char*** _NSGetEnviron(void);
  14. #define environ (*_NSGetEnviron())
  15. #else
  16. extern char **environ; // GNU C library
  17. #endif
  18. namespace il2cpp
  19. {
  20. namespace os
  21. {
  22. int32_t Environment::GetProcessorCount()
  23. {
  24. int count = 1;
  25. #ifdef _SC_NPROCESSORS_ONLN
  26. count = (int)sysconf(_SC_NPROCESSORS_ONLN);
  27. if (count > 0)
  28. return count;
  29. #endif
  30. #ifdef USE_SYSCTL
  31. {
  32. int mib[2];
  33. size_t len = sizeof(int);
  34. mib[0] = CTL_HW;
  35. mib[1] = HW_NCPU;
  36. if (sysctl(mib, 2, &count, &len, NULL, 0) == 0)
  37. return count;
  38. }
  39. #endif
  40. return count;
  41. }
  42. std::string Environment::GetMachineName()
  43. {
  44. const int n = 512;
  45. char buf[n];
  46. if (gethostname(buf, sizeof(buf)) == 0)
  47. {
  48. buf[n - 1] = 0;
  49. int i;
  50. // try truncating the string at the first dot
  51. for (i = 0; i < n; i++)
  52. {
  53. if (buf[i] == '.')
  54. {
  55. buf[i] = 0;
  56. break;
  57. }
  58. }
  59. return buf;
  60. }
  61. return NULL;
  62. }
  63. std::string Environment::GetOsVersionString()
  64. {
  65. struct utsname name;
  66. if (uname(&name) >= 0)
  67. return name.release;
  68. return "0.0.0.0";
  69. }
  70. std::string Environment::GetOsUserName()
  71. {
  72. const std::string username(GetEnvironmentVariable("USER"));
  73. return username.empty() ? "Unknown" : username;
  74. }
  75. std::string Environment::GetEnvironmentVariable(const std::string& name)
  76. {
  77. const char* variable = getenv(name.c_str());
  78. return variable ? std::string(variable) : std::string();
  79. }
  80. void Environment::SetEnvironmentVariable(const std::string& name, const std::string& value)
  81. {
  82. if (value.empty())
  83. {
  84. unsetenv(name.c_str());
  85. }
  86. else
  87. {
  88. setenv(name.c_str(), value.c_str(), 1); // 1 means overwrite
  89. }
  90. }
  91. std::vector<std::string> Environment::GetEnvironmentVariableNames()
  92. {
  93. std::vector<std::string> result;
  94. for (char **envvar = environ; *envvar != NULL; ++envvar)
  95. {
  96. const char* equalAddress = strchr(*envvar, '=');
  97. if (equalAddress != NULL)
  98. result.push_back(std::string(*envvar, size_t(equalAddress - *envvar)));
  99. }
  100. return result;
  101. }
  102. std::string Environment::GetHomeDirectory()
  103. {
  104. static std::string homeDirectory;
  105. if (!homeDirectory.empty())
  106. return homeDirectory;
  107. homeDirectory = GetEnvironmentVariable("HOME");
  108. return homeDirectory.empty() ? "/" : homeDirectory;
  109. }
  110. std::vector<std::string> Environment::GetLogicalDrives()
  111. {
  112. std::vector<std::string> result;
  113. // This implementation is not correct according to the definition of this icall, but this is
  114. // the only "logical drive" that the Mono version in Unity returns for OSX.
  115. result.push_back("/");
  116. // TODO: Implement additional logic for Linux
  117. return result;
  118. }
  119. void Environment::Exit(int result)
  120. {
  121. exit(result);
  122. }
  123. NORETURN void Environment::Abort()
  124. {
  125. abort();
  126. }
  127. utils::Expected<std::string> Environment::GetWindowsFolderPath(int folder)
  128. {
  129. // This should only be called on Windows.
  130. return std::string();
  131. }
  132. utils::Expected<bool> Environment::Is64BitOs()
  133. {
  134. struct utsname name;
  135. if (uname(&name) >= 0)
  136. {
  137. return strcmp(name.machine, "x86_64") == 0 || strncmp(name.machine, "aarch64", 7) == 0 || strncmp(name.machine, "ppc64", 5) == 0;
  138. }
  139. return false;
  140. }
  141. }
  142. }
  143. #endif