Fixed SHDotNetRuntime::IsLoaded() giving incorrect data and updated qualifiers and comment formats

This commit is contained in:
Kah Wei 2022-09-15 11:25:58 +08:00
parent 9a97d12d96
commit 12cfbb9952
2 changed files with 135 additions and 77 deletions

View File

@ -23,26 +23,43 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE namespace SHADE
{ {
/********************************************************************************//*! /*************************************************************************************/
@brief Class that encapsulates the state of the .NET Core Runtime lifecycle. /*!
*//*********************************************************************************/
class SHDotNetRuntime
\brief
Class that encapsulates the state of the .NET Core Runtime lifecycle.
*/
/*************************************************************************************/
class SHDotNetRuntime class SHDotNetRuntime
{ {
public: public:
/*----------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Constructors/Destructor */ /* Constructors/Destructor */
/*----------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/****************************************************************************//*! /***********************************************************************************/
@brief Default constructor that immediately initializes the CoreCLR. /*!
\brief
Default constructor that immediately initializes the CoreCLR.
@param[in] autoInit \param autoInit
If true, loads the CoreCLR by calling Init(). If true, loads the CoreCLR by calling Init().
*//*****************************************************************************/
*/
/***********************************************************************************/
SHDotNetRuntime(bool autoInit = true); SHDotNetRuntime(bool autoInit = true);
/****************************************************************************//*! /***********************************************************************************/
@brief Destructor that unloads the CoreCLR if it has not been unloaded /*!
yet.
*//*****************************************************************************/ \brief
Destructor that unloads the CoreCLR if it has not been unloaded yet.
*/
/***********************************************************************************/
~SHDotNetRuntime(); ~SHDotNetRuntime();
// Disallow copy and moving // Disallow copy and moving
@ -52,58 +69,78 @@ namespace SHADE
/*----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------------*/
/* Lifecycle Functions */ /* Lifecycle Functions */
/*----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------------*/
/****************************************************************************//*! /***********************************************************************************/
@brief Loads the CoreCLR and grabs pointers to bootstrapping functions and /*!
kickstarts the CoreCLR.
\brief
@throws SystemExitException Loads the CoreCLR and grabs pointers to bootstrapping functions and kickstarts the
Thrown if there is a failure in loading the CLR and related functions. CoreCLR.
*//*****************************************************************************/
void Init();
/****************************************************************************//*!
@brief Unloads the CoreCLR.
@throws SystemExitException \throws std::runtime_error
Thrown if there is a failure in unloading the CLR. Thrown if there is a failure in loading the CLR and related functions.
*//*****************************************************************************/
*/
/***********************************************************************************/
void Init();
/***********************************************************************************/
/*!
\brief
Unloads the CoreCLR.
\throws std::runtime_error
Thrown if there is a failure in unloading the CLR.
*/
/***********************************************************************************/
void Exit(); void Exit();
/*----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------------*/
/* Usage Functions */ /* Usage Functions */
/*----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------------*/
/****************************************************************************//*! /***********************************************************************************/
@brief Checks if the DotNetRuntime has successfully been initialised. /*!
\brief
Checks if the DotNetRuntime has successfully been initialised.
@return True if this DotNetRuntime has been initialised. \return
*//*****************************************************************************/ True if this DotNetRuntime has been initialised.
inline bool IsLoaded() { return coreClr != nullptr; }
/****************************************************************************//*!
@brief Retrieves a function pointer from the a CLR assembly based on the
specified assembly, type and function names.
@tparam FunctionType */
Type of the function pointer that the specified function name will /***********************************************************************************/
provide. inline bool IsLoaded() const noexcept { return initialised; }
@params[in] assemblyName
Name of the CoreCLR assembly that contains the function.
@params[in] typeName
Name of the CoreCLR type in the assembly that contains the function.
Nested types are separated by a period(.).
@params[in] functionName
Name of the CoreCLR function to get a pointer to.
@returns Pointer to the function in the assembly that was specified. /***********************************************************************************/
*//*****************************************************************************/ /*!
\brief
Retrieves a function pointer from the a CLR assembly based on the specified
assembly, type and function names.
\tparam FunctionType
Type of the function pointer that the specified function name will provide.
\param assemblyName
Name of the CoreCLR assembly that contains the function.
\param typeName
Name of the CoreCLR type in the assembly that contains the function. Nested types
are separated by a period(.).
\param functionName
Name of the CoreCLR function to get a pointer to.
\return
Pointer to the function in the assembly that was specified.
*/
/***********************************************************************************/
template<typename FunctionType> template<typename FunctionType>
FunctionType GetFunctionPtr(const std::string_view& assemblyName, FunctionType GetFunctionPtr(const std::string_view& assemblyName,
const std::string_view& typeName, const std::string_view& typeName,
const std::string_view& functionName); const std::string_view& functionName) const;
private: private:
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
bool initialised = false; bool initialised = false;
// References to CoreCLR key components // References to CoreCLR key components
HMODULE coreClr = nullptr; HMODULE coreClr = nullptr;
@ -114,34 +151,55 @@ namespace SHADE
coreclr_create_delegate_ptr createManagedDelegate = nullptr; coreclr_create_delegate_ptr createManagedDelegate = nullptr;
coreclr_shutdown_ptr shutdownCoreClr = nullptr; coreclr_shutdown_ptr shutdownCoreClr = nullptr;
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/****************************************************************************//*! /***********************************************************************************/
@brief Retrieves a function pointer from the CoreCLR based on the specified /*!
function name.
\brief
Retrieves a function pointer from the CoreCLR based on the specified
function name.
\tparam FunctionType
Type of the function pointer that the specified function name will provide.
\param functionName
Name of the CoreCLR function to get a pointer to.
\return
Pointer to the function in the CoreCLR that was specified.
@tparam FunctionType */
Type of the function pointer that the specified function name will /***********************************************************************************/
provide.
@params[in] functionName
Name of the CoreCLR function to get a pointer to.
@returns Pointer to the function in the CoreCLR that was specified.
*//*****************************************************************************/
template<typename FunctionType> template<typename FunctionType>
FunctionType getCoreClrFunctionPtr(const std::string& functionName); FunctionType getCoreClrFunctionPtr(const std::string& functionName);
/****************************************************************************//*! /***********************************************************************************/
@brief Compiles a semicolon separated string of trusted platform assemblies by /*!
searching the specified directory.
\brief
Compiles a semicolon separated string of trusted platform assemblies by
searching the specified directory.
\param directory
Path to the directory where the trusted platform assemblies reside.
\return
Semicolon separated string of trusted platform assemblies.
@params[in] directory */
Path to the directory where the trusted platform assemblies reside. /***********************************************************************************/
@returns Semicolon separated string of trusted platform assemblies.
*//*****************************************************************************/
static std::string buildTpaList(const std::string& directory); static std::string buildTpaList(const std::string& directory);
/***********************************************************************************/
/*!
\brief
Takes in a Win32 result code and throws an exception it if there is an error.
\param errMsg
Error message to display if the resultCode is a failure code.
\param resultCode
Result code of the function to check.
*/
/***********************************************************************************/
static void throwIfFailed(const std::string& errMsg, int resultCode); static void throwIfFailed(const std::string& errMsg, int resultCode);
}; };
} }

View File

@ -20,7 +20,7 @@ namespace SHADE
template<typename FunctionType> template<typename FunctionType>
FunctionType SHDotNetRuntime::GetFunctionPtr(const std::string_view & assemblyName, FunctionType SHDotNetRuntime::GetFunctionPtr(const std::string_view & assemblyName,
const std::string_view & typeName, const std::string_view & typeName,
const std::string_view & functionName) const std::string_view & functionName) const
{ {
FunctionType managedDelegate = nullptr; FunctionType managedDelegate = nullptr;
int result = createManagedDelegate int result = createManagedDelegate