Added Script class

This commit is contained in:
Kah Wei 2022-09-12 22:42:46 +08:00
parent b0e9469e4e
commit 216a9ac7cb
14 changed files with 958 additions and 9 deletions

View File

@ -10,9 +10,15 @@
<Filter Include="Scripts"> <Filter Include="Scripts">
<UniqueIdentifier>{4D6F1AE8-B94E-9983-C266-245A2EC5FFE4}</UniqueIdentifier> <UniqueIdentifier>{4D6F1AE8-B94E-9983-C266-245A2EC5FFE4}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Utility">
<UniqueIdentifier>{594615A9-C525-9444-CE3D-1F1B3A9CFAA5}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\Engine\DisposableAssemblyLoadContext.hxx"> <ClInclude Include="src\Engine\Entity.hxx">
<Filter>Engine</Filter>
</ClInclude>
<ClInclude Include="src\Engine\GameObject.hxx">
<Filter>Engine</Filter> <Filter>Engine</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\Math\Math.hxx"> <ClInclude Include="src\Math\Math.hxx">
@ -25,15 +31,24 @@
<Filter>Math</Filter> <Filter>Math</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\SHpch.h" /> <ClInclude Include="src\SHpch.h" />
<ClInclude Include="src\Scripts\Entity.hxx"> <ClInclude Include="src\Scripts\Script.hxx">
<Filter>Scripts</Filter> <Filter>Scripts</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\Scripts\GameObject.hxx"> <ClInclude Include="src\Utility\Convert.hxx">
<Filter>Scripts</Filter> <Filter>Utility</Filter>
</ClInclude>
<ClInclude Include="src\Utility\Debug.hxx">
<Filter>Utility</Filter>
</ClInclude>
<ClInclude Include="src\Utility\DisposableAssemblyLoadContext.hxx">
<Filter>Utility</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\Engine\DisposableAssemblyLoadContext.cxx"> <ClCompile Include="src\Engine\Entity.cxx">
<Filter>Engine</Filter>
</ClCompile>
<ClCompile Include="src\Engine\GameObject.cxx">
<Filter>Engine</Filter> <Filter>Engine</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Math\Math.cxx"> <ClCompile Include="src\Math\Math.cxx">
@ -46,11 +61,17 @@
<Filter>Math</Filter> <Filter>Math</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\SHpch.cpp" /> <ClCompile Include="src\SHpch.cpp" />
<ClCompile Include="src\Scripts\Entity.cxx"> <ClCompile Include="src\Scripts\Script.cxx">
<Filter>Scripts</Filter> <Filter>Scripts</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Scripts\GameObject.cxx"> <ClCompile Include="src\Utility\Convert.cxx">
<Filter>Scripts</Filter> <Filter>Utility</Filter>
</ClCompile>
<ClCompile Include="src\Utility\Debug.cxx">
<Filter>Utility</Filter>
</ClCompile>
<ClCompile Include="src\Utility\DisposableAssemblyLoadContext.cxx">
<Filter>Utility</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -25,7 +25,7 @@ project "SHADE_Managed"
includedirs includedirs
{ {
"%{prj.location}/src", "%{prj.location}/src",
"%{IncludeDir.assimp}/include", "%{IncludeDir.spdlog}/include",
"%{IncludeDir.imgui}", "%{IncludeDir.imgui}",
"%{IncludeDir.imguizmo}", "%{IncludeDir.imguizmo}",
"%{IncludeDir.imnodes}", "%{IncludeDir.imnodes}",
@ -34,11 +34,25 @@ project "SHADE_Managed"
"%{wks.location}/SHADE_Engine/src" "%{wks.location}/SHADE_Engine/src"
} }
links
{
"yaml-cpp",
"imgui",
"spdlog",
}
flags flags
{ {
"MultiProcessorCompile" "MultiProcessorCompile"
} }
dependson
{
"yaml-cpp",
"imgui",
"spdlog",
}
warnings 'Extra' warnings 'Extra'
filter "configurations:Debug" filter "configurations:Debug"

View File

@ -0,0 +1,171 @@
/************************************************************************************//*!
\file Script.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 28, 2021
\brief Contains the definition of the functions for the PlushieScript managed
class.
Note: This file is written in C++17/CLI.
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.
*//*************************************************************************************/
// Precompiled Headers
#include "SHpch.h"
// Primary Header
#include "Script.hxx"
// Project Headers
#include "Utility/Debug.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Component Access Functions */
/*---------------------------------------------------------------------------------*/
generic <typename T>
T Script::AddComponent()
{
return owner.AddComponent<T>();
}
generic <typename T>
T Script::GetComponent()
{
return owner.GetComponent<T>();
}
generic <typename T>
T Script::GetComponentInChildren()
{
return owner.GetComponentInChildren<T>();
}
generic <typename T>
T Script::EnsureComponent()
{
return owner.EnsureComponent<T>();
}
generic <typename T>
void Script::RemoveComponent()
{
throw gcnew System::NotImplementedException;
//ECS::RemoveComponent<T>(owner.GetNativeEntity());
}
/*---------------------------------------------------------------------------------*/
/* Script Access Functions */
/*---------------------------------------------------------------------------------*/
generic <typename T>
T Script::AddScript()
{
throw gcnew System::NotImplementedException;
//return ScriptStore::AddScript<T>(owner.GetEntity());
}
generic <typename T>
T Script::GetScript()
{
throw gcnew System::NotImplementedException;
//return ScriptStore::GetScript<T>(owner.GetEntity());
}
generic <typename T>
T Script::GetScriptInChildren()
{
throw gcnew System::NotImplementedException;
//return ScriptStore::GetScriptInChildren<T>(owner.GetEntity());
}
generic <typename T>
System::Collections::Generic::IEnumerable<T>^ Script::GetScripts()
{
throw gcnew System::NotImplementedException;
//return ScriptStore::GetScripts<T>(owner.GetEntity());
}
generic <typename T>
void Script::RemoveScript()
{
throw gcnew System::NotImplementedException;
//ScriptStore::RemoveScript<T>(owner.GetEntity());
}
/*---------------------------------------------------------------------------------*/
/* "All-time" Lifecycle Functions */
/*---------------------------------------------------------------------------------*/
void Script::OnAttached()
{
SAFE_NATIVE_CALL_BEGIN
onAttached();
SAFE_NATIVE_CALL_END(this)
}
void Script::OnDetached()
{
SAFE_NATIVE_CALL_BEGIN
onDetatched();
SAFE_NATIVE_CALL_END(this)
}
/*---------------------------------------------------------------------------------*/
/* Lifecycle Functions */
/*---------------------------------------------------------------------------------*/
void Script::Awake()
{
SAFE_NATIVE_CALL_BEGIN
awake();
SAFE_NATIVE_CALL_END(this)
}
void Script::Start()
{
SAFE_NATIVE_CALL_BEGIN
start();
SAFE_NATIVE_CALL_END(this)
}
void Script::FixedUpdate()
{
SAFE_NATIVE_CALL_BEGIN
fixedUpdate();
SAFE_NATIVE_CALL_END(this)
}
void Script::Update()
{
SAFE_NATIVE_CALL_BEGIN
update();
SAFE_NATIVE_CALL_END(this)
}
void Script::LateUpdate()
{
SAFE_NATIVE_CALL_BEGIN
lateUpdate();
SAFE_NATIVE_CALL_END(this)
}
void Script::OnDestroy()
{
SAFE_NATIVE_CALL_BEGIN
onDestroy();
SAFE_NATIVE_CALL_END(this)
}
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/
Script::Script(GameObject gameObj)
: owner { gameObj }
{}
/*---------------------------------------------------------------------------------*/
/* Virtual "All-Time" Lifecycle Functions */
/*---------------------------------------------------------------------------------*/
void Script::onAttached() {}
void Script::onDetatched() {}
/*---------------------------------------------------------------------------------*/
/* Virtual Lifecycle Functions */
/*---------------------------------------------------------------------------------*/
void Script::awake() {}
void Script::start() {}
void Script::fixedUpdate() {}
void Script::update() {}
void Script::lateUpdate() {}
void Script::onDestroy() {}
}// namespace PlushieAPI

View File

@ -0,0 +1,274 @@
/************************************************************************************//*!
\file Script.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 28, 2021
\brief Contains the definition of the Script class.
Note: This file is written in C++17/CLI.
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
// Project Includes
#include "Engine/GameObject.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------------------*/
ref class BaseComponent;
/*---------------------------------------------------------------------------------*/
/* Class Definitions */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// Class that forms the basis of all "script"-objects that can be attached to
/// Entities to update each Entity's Components via C# code.
/// </summary>
public ref class Script
{
public:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// GameObject that this Script belongs to.
/// </summary>
property GameObject Owner
{
GameObject get() { return owner; }
}
/*-----------------------------------------------------------------------------*/
/* Component Access Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Adds a Component to the GameObject that this Script belongs to.
/// </summary>
/// <typeparam name="T">
/// Type of the Component to add. Must be derived from BaseComponent.
/// </typeparam>
/// <returns>Reference to the Component that was added.</returns>
generic<typename T> where T : BaseComponent
T AddComponent();
/// <summary>
/// Gets a Component from the GameObject that this Script belongs to.
/// </summary>
/// <typeparam name="T">
/// Type of the Component to get. Must be derived from BaseComponent.
/// </typeparam>
/// <returns>Reference to the Component that was retrieved.</returns>
generic<typename T> where T : BaseComponent
T GetComponent();
/// <summary>
/// Retrieves the first Component from this GameObject's children that matches
/// the specified type.
/// </summary>
/// <typeparam name="T">
/// Type of the Component to get. Must be derived from BaseComponent.
/// </typeparam>
/// <returns>Reference to the Component that was retrieved.</returns>
generic<typename T> where T : BaseComponent
T GetComponentInChildren();
/// <summary>
/// Ensures a Component on the GameObject that this Script belongs to.
/// </summary>
/// <typeparam name="T">
/// Type of the Component to ensure. Must be derived from BaseComponent.
/// </typeparam>
/// <returns>Reference to the Component.</returns>
generic<typename T> where T : BaseComponent
T EnsureComponent();
/// <summary>
/// Removes a Component from the GameObject that this Script belongs to.
/// </summary>
/// <typeparam name="T">
/// Type of the Component to remove. Must be derived from BaseComponent.
/// </typeparam>
generic<typename T> where T : BaseComponent
void RemoveComponent();
/*-----------------------------------------------------------------------------*/
/* Script Access Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Adds a Script to this GameObject.
/// </summary>
/// <typeparam name="T">
/// Type of script to add.
/// This needs to be a default constructable Script.
/// </typeparam>
/// <returns>Reference to the script added</returns>
generic<typename T> where T : ref class, Script
T AddScript();
/// <summary>
/// Retrieves the first Script from this GameObject that matches the specified
/// type.
/// </summary>
/// <typeparam name="T">
/// Type of script to get.
/// This needs to be a default constructable Script.
/// </typeparam>
/// <returns>Reference to the script added</returns>
generic<typename T> where T : ref class, Script
T GetScript();
/// <summary>
/// Retrieves the first Script from this GameObject's children that matches the
/// specified type.
/// </summary>
/// <typeparam name="T">
/// Type of script to get.
/// This needs to be a default constructable Script.
/// </typeparam>
/// <returns>Reference to the script added</returns>
generic<typename T> where T : ref class, Script
T GetScriptInChildren();
/// <summary>
/// Retrieves a immutable list of scripts from the specified Entity that
/// matches the specified type.
/// <br/>
/// Note that this function allocates. It should be used sparingly.
/// </summary>
/// <typeparam name="T">
/// Type of scripts to get.
/// This needs to be a default constructable Script.
/// </typeparam>
/// <returns>
/// Immutable list of references to scripts of the specified type.
/// </returns>
generic<typename T> where T : ref class, Script
System::Collections::Generic::IEnumerable<T>^ GetScripts();
/// <summary>
/// Removes all Scripts of the specified type from this GameObject.
/// </summary>
/// <typeparam name="T">
/// Type of script to remove.
/// This needs to be a default constructable Script.
/// </typeparam>
generic<typename T> where T : ref class, Script
void RemoveScript();
internal:
/*-----------------------------------------------------------------------------*/
/* "All-Time" Lifecycle Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Used to call onAttached(). This is called immediately when this script is
/// attached to a GameObject.
/// </summary>
void OnAttached();
/// <summary>
/// Used to call onDetached(). This is called immediately when this script is
/// detached from a GameObject.
/// </summary>
void OnDetached();
/*-----------------------------------------------------------------------------*/
/* Lifecycle Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Used to call awake(). This should be called on the first frame that the
/// attached GameObject is active if they are a part of the scene.
/// </summary>
void Awake();
/// <summary>
/// Used to call start(). This should be called on the first frame that the
/// attached GameObject is active but always after Awake().
/// </summary>
void Start();
/// <summary>
/// Used to call fixedUpdate(). This should be called in sync with Physics
/// update steps and thus in most cases will execute more than Update() will.
/// This will be called immediately before a Physics update step.
/// </summary>
void FixedUpdate();
/// <summary>
/// Used to call update(). This should be called every frame before physics and
/// collision resolution.
/// </summary>
void Update();
/// <summary>
/// Used to call lateUpdate(). This should be called every frame after physics
/// and collision resolution but before rendering.
/// </summary>
void LateUpdate();
/// <summary>
/// Used to call onDestroy(). This should be called at the end of the frame
/// where the attached GameObject or this script is destroyed directly or
/// indirectly due to destruction of the owner.
/// </summary>
void OnDestroy();
protected:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructor for Script to tie it to a specific GameObject.
/// Constructors of derived Scripts should call this Constructor.
/// </summary>
/// <param name="gameObj">
/// GameObject that this Script will be tied to.
/// </param>
Script(GameObject gameObj);
/*-----------------------------------------------------------------------------*/
/* Virtual "All-Time" Lifecycle Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Called immediately once this script is attached to a GameObject.
/// </summary>
virtual void onAttached();
/// <summary>
/// Called immediately once this script is detached from a GameObject.
/// </summary>
virtual void onDetatched();
/*-----------------------------------------------------------------------------*/
/* Virtual Lifecycle Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Called on the first frame that the attached GameObject is active if they are
/// a part of the scene.
/// </summary>
virtual void awake();
/// <summary>
/// Called on the first frame that the attached GameObject is active but always
/// after Awake().
/// </summary>
virtual void start();
/// <summary>
/// Called every frame in sync with Physics update steps and thus in most cases
/// will execute more than update() will. This will be called immediately before
/// a Physics update step.
/// </summary>
virtual void fixedUpdate();
/// <summary>
/// Called every frame before physics and collision resolution.
/// </summary>
virtual void update();
/// <summary>
/// Called every frame after physics and collision resolution but before
/// rendering.
/// </summary>
virtual void lateUpdate();
/// <summary>
/// Called just before the end of the frame where the attached GameObject or
/// this script is destroyed directly or indirectly due to destruction of the
/// owner.
/// </summary>
virtual void onDestroy();
private:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
GameObject owner;
};
} // namespace PlushieAPI

View File

@ -0,0 +1,49 @@
/************************************************************************************//*!
\file Convert.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 28, 2021
\brief Contains the definition of the functions for the Convert managed static
class.
Note: This file is written in C++17/CLI.
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.
*//*************************************************************************************/
// Precompiled Headers
#include "SHpch.h"
// Primary Header
#include "Convert.hxx"
// External Dependencies
#include "Engine/ECS_Base/System//SHEntityManager.h"
#include <msclr/marshal_cppstd.h>
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* ECS Conversions */
/*---------------------------------------------------------------------------------*/
SHEntity Convert::ToNative(Entity rawId)
{
return *SHEntityManager::GetEntityByID(rawId);
}
Entity Convert::ToCLI(SHEntity entity)
{
return static_cast<Entity>(entity.GetEID());
}
/*---------------------------------------------------------------------------------*/
/* String Conversions */
/*---------------------------------------------------------------------------------*/
std::string Convert::ToNative(System::String^ str)
{
return msclr::interop::marshal_as<std::string>(str);
}
System::String^ Convert::ToCLI(const std::string& str)
{
return msclr::interop::marshal_as<System::String^>(str);
}
} // namespace PlushieAPI

View File

@ -0,0 +1,68 @@
/************************************************************************************//*!
\file Convert.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 28, 2021
\brief Contains the definition of the Convert static class and the
declaration of its functions.
Note: This file is written in C++17/CLI.
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
// External Dependencies
#include "Engine/ECS_Base/Entity/SHEntity.h"
// Project Includes
#include "Engine/Entity.hxx"
namespace SHADE
{
/// <summary>
/// Provides functions easy and consistent syntax for converting between custom
/// managed and native types that are aligned.
/// </summary>
class Convert
{
public:
/*-----------------------------------------------------------------------------*/
/* Deleted Destructors (Static Class) */
/*-----------------------------------------------------------------------------*/
Convert() = delete;
/*-----------------------------------------------------------------------------*/
/* ECS Conversions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Converts from a managed Entity (UInt32) to a native Entity.
/// </summary>
/// <param name="rawId">Raw ID of an Entity.</param>
/// <returns>Native representation of the specified Entity.</returns>
static SHEntity ToNative(Entity rawId);
/// <summary>
/// Converts from a native Entity to a managed Entity (UInt32).
/// </summary>
/// <param name="entity">Native Entity to convert from.</param>
/// <returns>Managed representation of the specified Entity.</returns>
static Entity ToCLI(SHEntity entity);
/*-----------------------------------------------------------------------------*/
/* String Conversions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Converts from a managed String to a native std::string.
/// </summary>
/// <param name="str">The managed String to convert from.</param>
/// <returns>Native copy of a managed String.</returns>
static std::string ToNative(System::String^ str);
/// <summary>
/// Converts from a native std::Stringto a managed String.
/// </summary>
/// <param name="str">The native std::string to convert from.</param>
/// <returns>Managed copy of a native std::string.</returns>
static System::String^ ToCLI(const std::string& str);
};
}

View File

@ -0,0 +1,112 @@
/************************************************************************************//*!
\file Debug.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 28, 2021
\brief Contains the definition of the functions for the Debug managed static
class.
Note: This file is written in C++17/CLI.
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.
*//*************************************************************************************/
// Precompiled Headers
#include "SHpch.h"
// Primary Header
#include "Debug.hxx"
// Standard Libraries
#include <sstream>
// External Dependencies
#include "Tools/SHLogger.h"
// Project Headers
#include "Convert.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Logging Functions */
/*---------------------------------------------------------------------------------*/
void Debug::Log(System::String^ str)
{
SHLOG_INFO(Convert::ToNative(str));
}
void Debug::Log(System::String^ str, Object^ owner)
{
Log(str, owner->GetType()->Name);
}
void Debug::Log(System::String^ str, System::String^ throwerName)
{
Log("[" + throwerName + "] " + str);
}
void Debug::Log(System::String^ str, const std::string& throwerName)
{
std::ostringstream oss;
oss << "[" << throwerName << "] " << Convert::ToNative(str);
SHLOG_INFO(oss.str());
}
void Debug::LogWarning(System::String^ str)
{
SHLOG_WARNING(Convert::ToNative(str));
}
void Debug::LogWarning(System::String^ str, Object^ thrower)
{
LogWarning(str, thrower->GetType()->Name);
}
void Debug::LogWarning(System::String^ str, System::String^ throwerName)
{
LogWarning("[" + throwerName + "] " + str);
}
void Debug::LogWarning(System::String^ str, const std::string& throwerName)
{
std::ostringstream oss;
oss << "[" << throwerName << "] " << Convert::ToNative(str);
SHLOG_WARNING(oss.str());
}
void Debug::LogError(System::String^ str)
{
SHLOG_ERROR(Convert::ToNative(str));
}
void Debug::LogError(System::String^ str, Object^ thrower)
{
LogError(str, thrower->GetType()->Name);
}
void Debug::LogErrorNative(System::String^ str, const std::string& throwerName)
{
std::ostringstream oss;
oss << "[" << throwerName << "] -> " << Convert::ToNative(str);
SHLOG_ERROR(oss.str());
}
void Debug::LogError(System::String^ str, System::String^ throwerName)
{
LogError("[" + throwerName + "] " + str);
}
void Debug::LogException(System::Exception^ exception)
{
LogError("Unhandled exception: " + exception->ToString(), exception->Source);
}
void Debug::LogException(System::Exception^ exception, Object^ thrower)
{
LogError("Unhandled exception: " + exception->ToString(), thrower->GetType()->Name);
}
void Debug::LogException(const std::exception& exception, Object^ thrower)
{
LogExceptionNative(exception, Convert::ToNative(thrower->GetType()->Name));
}
void Debug::LogExceptionNative(System::Exception^ exception, const std::string& throwerName)
{
std::ostringstream oss;
oss << "[" << throwerName << "] Unhandled exception: " << Convert::ToNative(exception->ToString());
SHLOG_ERROR(oss.str());
}
void Debug::LogExceptionNative(const std::exception& exception, const std::string& throwerName)
{
std::ostringstream oss;
oss << "[" << throwerName << "] Unhandled exception: " << exception.what();
SHLOG_ERROR(oss.str());
}
}

View File

@ -0,0 +1,240 @@
/************************************************************************************//*!
\file Debug.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 30, 2021
\brief Contains the definition of the Debug static class and the declaration of
its functions.
Note: This file is written in C++17/CLI.
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 Library
#include <stdexcept>
#include <string>
/*-------------------------------------------------------------------------------------*/
/* Macro Functions */
/*-------------------------------------------------------------------------------------*/
/// <summary>
/// Macro expansion that is used together with SAFE_NATIVE_CALL_END or
/// SAFE_NATIVE_CALL_END_N to wrap the body of a function with a try and catch that
/// catches native and managed exceptions. This is needed to prevent crashes when calling
/// managed code from native code.
/// </summary>
#define SAFE_NATIVE_CALL_BEGIN try {
/// <summary>
/// Macro expansion that is used together with SAFE_NATIVE_CALL_BEGIN or to wrap the body
/// of a function with a try and catch that catches native and managed exceptions. This
/// is needed to prevent crashes when calling managed code from native code.
/// <br/>
/// Use this instead of SAFE_NATIVE_CALL_END_N if passing in managed types as the owner.
/// </summary>
/// <param name="OWNER">
/// The managed object that owns the function that this macro encapsulates.
/// </param>
#define SAFE_NATIVE_CALL_END(OWNER) \
} \
catch (System::Exception^ e) \
{ \
Debug::LogException(e); \
} \
catch (const std::exception& e) \
{ \
Debug::LogException(e, OWNER); \
} \
catch (...) \
{ \
Debug::LogError("Unsupported native exception.", OWNER); \
} \
/// <summary>
/// Macro expansion that is used together with SAFE_NATIVE_CALL_BEGIN or to wrap the body
/// of a function with a try and catch that catches native and managed exceptions. This
/// is needed to prevent crashes when calling managed code from native code.
/// <br/>
/// Use this instead of SAFE_NATIVE_CALL_END if passing in a native string that specifies
/// the owner.
/// </summary>
/// <param name="OWNER">
/// The managed object that owns the function that this macro encapsulates.
/// </param>
#define SAFE_NATIVE_CALL_END_N(OWNER) \
} \
catch (System::Exception^ e) \
{ \
Debug::LogExceptionNative(e, OWNER); \
} \
catch (const std::exception& e) \
{ \
Debug::LogExceptionNative(e, OWNER); \
} \
catch (...) \
{ \
Debug::LogErrorNative("Unsupported native exception.", OWNER); \
} \
/*-------------------------------------------------------------------------------------*/
/* Type Definitions */
/*-------------------------------------------------------------------------------------*/
namespace SHADE
{
/// <summary>
/// Static class that contains the functions for working with time.
/// </summary>
public ref class Debug abstract sealed
{
public:
/*-----------------------------------------------------------------------------*/
/* Logging Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Logs a message to the output.
/// </summary>
/// <param name="str">The string to output.</param>
static void Log(System::String^ str);
/// <summary>
/// Logs a message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="owner">
/// Object that sent the message to label the message.
/// The name of the object will be used.
/// </param>
static void Log(System::String^ str, Object^ owner);
/// <summary>
/// Logs a message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="throwerName">
/// Name of the object that sent the message to label the message.
/// The name of the object will be used.
/// </param>
static void Log(System::String^ str, System::String^ throwerName);
/// <summary>
/// Logs a message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="throwerName">
/// Name of the object that sent the message to label the message.
/// The name of the object will be used.
/// </param>
static void Log(System::String^ str, const std::string& throwerName);
/// <summary>
/// Logs a warning message to the output.
/// </summary>
/// <param name="str">The string to output.</param>
static void LogWarning(System::String^ str);
/// <summary>
/// Logs a warning message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="thrower">
/// Object that threw the warning to label the warning message.
/// The name of the object will be used.
/// </param>
static void LogWarning(System::String^ str, Object^ thrower);
/// <summary>
/// Logs a warning message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="throwerName">
/// Name of the object that threw the warning to label the warning message.
/// The name of the object will be used.
/// </param>
static void LogWarning(System::String^ str, System::String^ throwerName);
/// <summary>
/// Logs a warning message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="throwerName">
/// Name of the object that threw the warning to label the warning message.
/// The name of the object will be used.
/// </param>
static void LogWarning(System::String^ str, const std::string& throwerName);
/// <summary>
/// Logs a error message to the output.
/// </summary>
/// <param name="str">The string to output.</param>
static void LogError(System::String^ str);
/// <summary>
/// Logs a error message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="thrower">
/// Object that threw the error to label the error message.
/// The name of the object will be used.
/// </param>
static void LogError(System::String^ str, Object^ thrower);
/// <summary>
/// Logs a error message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="throwerName">
/// Name of the object that threw the error to label the error message.
/// The name of the object will be used.
/// </param>
static void LogErrorNative(System::String^ str, const std::string& throwerName);
/// <summary>
/// Logs a error message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="throwerName">
/// Name of the object that threw the error to label the error message.
/// The name of the object will be used.
/// </param>
static void LogError(System::String^ str, System::String^ throwerName);
/// <summary>
/// Logs an exception that is formatted nicely to the output.
/// </summary>
/// <param name="exception">Exception to log.</param>
static void LogException(System::Exception^ exception);
/// <summary>
/// Logs an exception that is formatted nicely to the output.
/// </summary>
/// <param name="exception">Exception to log.</param>
/// <param name="thrower">
/// Object that threw the exception to label the exception message.
/// The name of the object will be used.
/// </param>
static void LogException(System::Exception^ exception, Object^ thrower);
/// <summary>
/// Logs a native exception that is formatted nicely to the output.
/// Equivalent to calling
/// LogException(exception, Convert::ToNative(thrower->GetType()->Name));
/// </summary>
/// <param name="exception">Native exception to log.</param>
/// <param name="thrower">
/// Object that threw the exception to label the exception message.
/// The name of the object will be used.
/// </param>
static void LogException(const std::exception& exception, Object^ thrower);
/// <summary>
/// Logs an exception that is formatted nicely to the output.
/// </summary>
/// <param name="throwerName">Name of the one responsible for the exception.</param>
/// <param name="exception">Exception to log.</param>
static void LogExceptionNative(System::Exception^ exception, const std::string& throwerName);
/// <summary>
/// Logs a native exception that is formatted nicely to the output.
/// </summary>
/// <param name="exception">Native exception to log.</param>
/// <param name="throwerName">Name of the one responsible for the exception.</param>
static void LogExceptionNative(const std::exception& exception, const std::string& throwerName);
};
}