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. /*!
@param[in] autoInit \brief
Default constructor that immediately initializes the CoreCLR.
\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.
@throws SystemExitException \brief
Loads the CoreCLR and grabs pointers to bootstrapping functions and kickstarts the
CoreCLR.
\throws std::runtime_error
Thrown if there is a failure in loading the CLR and related functions. Thrown if there is a failure in loading the CLR and related functions.
*//*****************************************************************************/
void Init();
/****************************************************************************//*!
@brief Unloads the CoreCLR.
@throws SystemExitException */
/***********************************************************************************/
void Init();
/***********************************************************************************/
/*!
\brief
Unloads the CoreCLR.
\throws std::runtime_error
Thrown if there is a failure in unloading the CLR. 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. /*!
@return True if this DotNetRuntime has been initialised. \brief
*//*****************************************************************************/ Checks if the DotNetRuntime has successfully 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 \return
Type of the function pointer that the specified function name will True if this DotNetRuntime has been initialised.
provide.
@params[in] assemblyName */
/***********************************************************************************/
inline bool IsLoaded() const noexcept { return initialised; }
/***********************************************************************************/
/*!
\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. Name of the CoreCLR assembly that contains the function.
@params[in] typeName \param typeName
Name of the CoreCLR type in the assembly that contains the function. Name of the CoreCLR type in the assembly that contains the function. Nested types
Nested types are separated by a period(.). are separated by a period(.).
@params[in] functionName \param functionName
Name of the CoreCLR function to get a pointer to. Name of the CoreCLR function to get a pointer to.
\return
Pointer to the function in the assembly that was specified.
@returns 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 /*!
\brief
Retrieves a function pointer from the CoreCLR based on the specified
function name. function name.
@tparam FunctionType \tparam FunctionType
Type of the function pointer that the specified function name will Type of the function pointer that the specified function name will provide.
provide. \param functionName
@params[in] functionName
Name of the CoreCLR function to get a pointer to. Name of the CoreCLR function to get a pointer to.
\return
Pointer to the function in the CoreCLR that was specified.
@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 /*!
\brief
Compiles a semicolon separated string of trusted platform assemblies by
searching the specified directory. searching the specified directory.
@params[in] directory \param directory
Path to the directory where the trusted platform assemblies reside. Path to the directory where the trusted platform assemblies reside.
\return
Semicolon separated string of trusted platform assemblies.
@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