SHADE_Y3/SHADE_Engine/src/Scripting/SHDotNetRuntime.h

150 lines
7.4 KiB
C
Raw Normal View History

/*************************************************************************************//*!
\file SHDotNetRuntime.h
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 2, 2021
\brief Contains the interface of a wrapper class for interfacing with the
.NET 5 Runtime.
Copyright (C) 2021 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//**************************************************************************************/
#pragma once
// Standard Libraries
#include <iomanip> // std::setfill, std::setw
#include <stdexcept> // std::runtime_error
#include <string> // std::string
#include <sstream> // std::ostringstream
// External Dependencies
#include <Windows.h> // HMODULE
#include <coreclrhost.h> // coreclr_*
namespace SHADE
{
/********************************************************************************//*!
@brief Class that encapsulates the state of the .NET Core Runtime lifecycle.
*//*********************************************************************************/
class SHDotNetRuntime
{
public:
/*----------------------------------------------------------------------------------*/
/* Constructors/Destructor */
/*----------------------------------------------------------------------------------*/
/****************************************************************************//*!
@brief Default constructor that immediately initializes the CoreCLR.
@param[in] autoInit
If true, loads the CoreCLR by calling Init().
*//*****************************************************************************/
SHDotNetRuntime(bool autoInit = true);
/****************************************************************************//*!
@brief Destructor that unloads the CoreCLR if it has not been unloaded
yet.
*//*****************************************************************************/
~SHDotNetRuntime();
// Disallow copy and moving
SHDotNetRuntime(const SHDotNetRuntime&) = delete;
SHDotNetRuntime(SHDotNetRuntime&&) = delete;
/*----------------------------------------------------------------------------------*/
/* Lifecycle Functions */
/*----------------------------------------------------------------------------------*/
/****************************************************************************//*!
@brief Loads the CoreCLR and grabs pointers to bootstrapping functions and
kickstarts the CoreCLR.
@throws SystemExitException
Thrown if there is a failure in loading the CLR and related functions.
*//*****************************************************************************/
void Init();
/****************************************************************************//*!
@brief Unloads the CoreCLR.
@throws SystemExitException
Thrown if there is a failure in unloading the CLR.
*//*****************************************************************************/
void Exit();
/*----------------------------------------------------------------------------------*/
/* Usage Functions */
/*----------------------------------------------------------------------------------*/
/****************************************************************************//*!
@brief Checks if the DotNetRuntime has successfully 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.
@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.
*//*****************************************************************************/
template<typename FunctionType>
FunctionType GetFunctionPtr(const std::string_view& assemblyName,
const std::string_view& typeName,
const std::string_view& functionName);
private:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
bool initialised = false;
// References to CoreCLR key components
HMODULE coreClr = nullptr;
void* hostHandle = nullptr;
unsigned int domainId = 0;
// Function Pointers to CoreCLR functions
coreclr_initialize_ptr initializeCoreClr = nullptr;
coreclr_create_delegate_ptr createManagedDelegate = nullptr;
coreclr_shutdown_ptr shutdownCoreClr = nullptr;
/*-----------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------*/
/****************************************************************************//*!
@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.
@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>
FunctionType getCoreClrFunctionPtr(const std::string& functionName);
/****************************************************************************//*!
@brief Compiles a semicolon separated string of trusted platform assemblies by
searching the specified directory.
@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 void throwIfFailed(const std::string& errMsg, int resultCode);
};
} // namespace PlushieEngine::Scripts
#include "SHDotNetRuntime.hpp"