platform_impl.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <platform.h>
  9. #include <ISystem.h>
  10. #include <Random.h>
  11. #include <IConsole.h>
  12. #include <AzCore/Debug/Profiler.h>
  13. #include <AzCore/Memory/AllocatorManager.h>
  14. #include <AzCore/Module/Environment.h>
  15. #include <AzCore/std/string/conversions.h>
  16. #include <AzCore/Utils/Utils.h>
  17. // Section dictionary
  18. #if defined(AZ_RESTRICTED_PLATFORM)
  19. #define PLATFORM_IMPL_H_SECTION_TRAITS 1
  20. #define PLATFORM_IMPL_H_SECTION_CRYLOWLATENCYSLEEP 2
  21. #define PLATFORM_IMPL_H_SECTION_CRYGETFILEATTRIBUTES 3
  22. #define PLATFORM_IMPL_H_SECTION_CRY_FILE_ATTRIBUTE_STUBS 5
  23. #define PLATFORM_IMPL_H_SECTION_CRY_SYSTEM_FUNCTIONS 6
  24. #define PLATFORM_IMPL_H_SECTION_VIRTUAL_ALLOCATORS 7
  25. #endif
  26. struct SSystemGlobalEnvironment* gEnv = nullptr;
  27. // Traits
  28. #if defined(AZ_RESTRICTED_PLATFORM)
  29. #define AZ_RESTRICTED_SECTION PLATFORM_IMPL_H_SECTION_TRAITS
  30. #include AZ_RESTRICTED_FILE(platform_impl_h)
  31. #endif
  32. #if defined(WIN32) || defined(WIN64)
  33. void CryPureCallHandler()
  34. {
  35. CryFatalError("Pure function call");
  36. }
  37. void CryInvalidParameterHandler(
  38. [[maybe_unused]] const wchar_t* expression,
  39. [[maybe_unused]] const wchar_t* function,
  40. [[maybe_unused]] const wchar_t* file,
  41. [[maybe_unused]] unsigned int line,
  42. [[maybe_unused]] uintptr_t pReserved)
  43. {
  44. //size_t i;
  45. //char sFunc[128];
  46. //char sExpression[128];
  47. //char sFile[128];
  48. //wcstombs_s( &i,sFunc,sizeof(sFunc),function,_TRUNCATE );
  49. //wcstombs_s( &i,sExpression,sizeof(sExpression),expression,_TRUNCATE );
  50. //wcstombs_s( &i,sFile,sizeof(sFile),file,_TRUNCATE );
  51. //CryFatalError( "Invalid parameter detected in function %s. File: %s Line: %d, Expression: %s",sFunc,sFile,line,sExpression );
  52. CryFatalError("Invalid parameter detected in CRT function\n");
  53. }
  54. void InitCRTHandlers()
  55. {
  56. _set_purecall_handler(CryPureCallHandler);
  57. _set_invalid_parameter_handler(CryInvalidParameterHandler);
  58. }
  59. #else
  60. void InitCRTHandlers() {}
  61. #endif
  62. //////////////////////////////////////////////////////////////////////////
  63. // This is an entry to DLL initialization function that must be called for each loaded module
  64. //////////////////////////////////////////////////////////////////////////
  65. void ModuleInitISystem(ISystem* pSystem, [[maybe_unused]] const char* moduleName)
  66. {
  67. if (gEnv) // Already registered.
  68. {
  69. return;
  70. }
  71. InitCRTHandlers();
  72. if (pSystem) // DONT REMOVE THIS. ITS FOR RESOURCE COMPILER!!!!
  73. {
  74. gEnv = pSystem->GetGlobalEnvironment();
  75. assert(gEnv);
  76. } // if pSystem
  77. }
  78. void ModuleShutdownISystem([[maybe_unused]] ISystem* pSystem)
  79. {
  80. }
  81. void* GetModuleInitISystemSymbol()
  82. {
  83. return reinterpret_cast<void*>(&ModuleInitISystem);
  84. }
  85. void* GetModuleShutdownISystemSymbol()
  86. {
  87. return reinterpret_cast<void*>(&ModuleShutdownISystem);
  88. }
  89. //////////////////////////////////////////////////////////////////////////
  90. // global random number generator used by cry_random functions
  91. CRndGen CryRandom_Internal::g_random_generator;
  92. //////////////////////////////////////////////////////////////////////////
  93. // when using STL Port _STLP_DEBUG and _STLP_DEBUG_TERMINATE - avoid actually
  94. // crashing (default terminator seems to kill the thread, which isn't nice).
  95. #ifdef _STLP_DEBUG_TERMINATE
  96. # ifdef __stl_debug_terminate
  97. # undef __stl_debug_terminate
  98. # endif
  99. void __stl_debug_terminate(void)
  100. {
  101. assert(0 && "STL Debug Error");
  102. }
  103. #endif
  104. #ifdef _STLP_DEBUG_MESSAGE
  105. # ifdef __stl_debug_message
  106. # undef __stl_debug_message
  107. # endif
  108. void __stl_debug_message(const char* format_str, ...)
  109. {
  110. va_list __args;
  111. va_start(__args, format_str);
  112. #ifdef WIN32
  113. char __buffer [4096];
  114. azvsnprintf(__buffer, sizeof(__buffer) / sizeof(char), format_str, __args);
  115. OutputDebugStringA(__buffer);
  116. #endif
  117. gEnv->pLog->LogV(ILog::eErrorAlways, format_str, __args);
  118. va_end(__args);
  119. }
  120. #endif //_STLP_DEBUG_MESSAGE
  121. #if defined(WIN32) || defined(WIN64)
  122. #include <intrin.h>
  123. #endif
  124. #if defined(AZ_RESTRICTED_PLATFORM)
  125. #define AZ_RESTRICTED_SECTION PLATFORM_IMPL_H_SECTION_CRY_SYSTEM_FUNCTIONS
  126. #include AZ_RESTRICTED_FILE(platform_impl_h)
  127. #endif
  128. #if defined (_WIN32)
  129. //////////////////////////////////////////////////////////////////////////
  130. void CrySleep(unsigned int dwMilliseconds)
  131. {
  132. AZ_PROFILE_FUNCTION(System);
  133. Sleep(dwMilliseconds);
  134. }
  135. //////////////////////////////////////////////////////////////////////////
  136. void CryMessageBox([[maybe_unused]] const char* lpText, [[maybe_unused]] const char* lpCaption, [[maybe_unused]] unsigned int uType)
  137. {
  138. #ifdef WIN32
  139. ICVar* const pCVar = gEnv && gEnv->pConsole ? gEnv->pConsole->GetCVar("sys_no_crash_dialog") : NULL;
  140. if ((pCVar && pCVar->GetIVal() != 0) || (gEnv && gEnv->bNoAssertDialog))
  141. {
  142. return;
  143. }
  144. AZStd::wstring lpTextW;
  145. AZStd::to_wstring(lpTextW, lpText);
  146. AZStd::wstring lpCaptionW;
  147. AZStd::to_wstring(lpCaptionW, lpCaption);
  148. MessageBoxW(NULL, lpTextW.c_str(), lpCaptionW.c_str(), uType);
  149. #else
  150. return;
  151. #endif
  152. }
  153. // Initializes root folder of the game, optionally returns exe and path name.
  154. void InitRootDir(char szExeFileName[], uint nExeSize, char szExeRootName[], uint nRootSize)
  155. {
  156. char szPath[_MAX_PATH];
  157. [[maybe_unused]] AZ::Utils::GetExecutablePathReturnType ret = AZ::Utils::GetExecutablePath(szPath, _MAX_PATH);
  158. AZ_Assert(ret.m_pathStored == AZ::Utils::ExecutablePathResult::Success, "The path to the current executable exceeds the expected length");
  159. const size_t nLen = strnlen(szPath, _MAX_PATH);
  160. // Find path above exe name and deepest folder.
  161. bool firstIteration = true;
  162. for (size_t n = nLen - 1; n > 0; n--)
  163. {
  164. if (szPath[n] == '\\')
  165. {
  166. szPath[n] = 0;
  167. if (firstIteration)
  168. {
  169. // Return exe path
  170. if (szExeRootName)
  171. {
  172. azstrncpy(szExeRootName, nRootSize, szPath + n + 1, nLen - n - 1);
  173. }
  174. // Return exe name
  175. if (szExeFileName)
  176. {
  177. azstrncpy(szExeFileName, nExeSize, szPath + n, nLen - n);
  178. }
  179. firstIteration = false;
  180. }
  181. // Check if the engineroot exists
  182. azstrcat(szPath, AZ_ARRAY_SIZE(szPath), "\\engine.json");
  183. WIN32_FILE_ATTRIBUTE_DATA data;
  184. wchar_t szPathW[_MAX_PATH];
  185. AZStd::to_wstring(szPathW, _MAX_PATH, szPath);
  186. BOOL res = GetFileAttributesExW(szPathW, GetFileExInfoStandard, &data);
  187. if (res != 0 && data.dwFileAttributes != INVALID_FILE_ATTRIBUTES)
  188. {
  189. // Found file
  190. szPath[n] = 0;
  191. SetCurrentDirectoryW(szPathW);
  192. break;
  193. }
  194. }
  195. }
  196. }
  197. //////////////////////////////////////////////////////////////////////////
  198. threadID CryGetCurrentThreadId()
  199. {
  200. return GetCurrentThreadId();
  201. }
  202. #endif // _WIN32
  203. #if defined(AZ_RESTRICTED_PLATFORM)
  204. #define AZ_RESTRICTED_SECTION PLATFORM_IMPL_H_SECTION_CRY_FILE_ATTRIBUTE_STUBS
  205. #include AZ_RESTRICTED_FILE(platform_impl_h)
  206. #endif
  207. #if defined(AZ_PLATFORM_WINDOWS)
  208. int64 CryGetTicks()
  209. {
  210. LARGE_INTEGER li;
  211. QueryPerformanceCounter(&li);
  212. return li.QuadPart;
  213. }
  214. #endif
  215. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  216. // Threads implementation. For static linking it must be declared inline otherwise creating multiple symbols
  217. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  218. #define THR_INLINE inline
  219. //////////////////////////////////////////////////////////////////////////
  220. inline void CryDebugStr([[maybe_unused]] const char* format, ...)
  221. {
  222. /*
  223. #ifdef CRYSYSTEM_EXPORTS
  224. va_list ArgList;
  225. char szBuffer[65535];
  226. va_start(ArgList, format);
  227. azvsnprintf(szBuffer,sizeof(szBuffer)-1, format, ArgList);
  228. va_end(ArgList);
  229. azstrcat(szBuffer,"\n");
  230. OutputDebugString(szBuffer);
  231. #endif
  232. */
  233. }
  234. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  235. #if defined(AZ_RESTRICTED_PLATFORM)
  236. #define AZ_RESTRICTED_SECTION PLATFORM_IMPL_H_SECTION_VIRTUAL_ALLOCATORS
  237. #include AZ_RESTRICTED_FILE(platform_impl_h)
  238. #endif
  239. #if defined(AZ_RESTRICTED_SECTION_IMPLEMENTED)
  240. #undef AZ_RESTRICTED_SECTION_IMPLEMENTED
  241. #endif