From a78b3c0123e291a7a7b597fe509f110deef412da Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 22 Nov 2022 15:37:01 +0800 Subject: [PATCH] Reworked NativeAsset system --- SHADE_Engine/src/Resource/SHResourceManager.h | 3 +- .../src/Resource/SHResourceManagerWrapper.cpp | 34 ++++++++++ .../src/Resource/SHResourceManagerWrapper.h | 57 ++++++++++++++++ SHADE_Managed/src/Assets/Font.cxx | 45 ++++++++++++- SHADE_Managed/src/Assets/Font.hxx | 59 ++++++++++++++++- SHADE_Managed/src/Assets/Material.cxx | 43 +++++++++++- SHADE_Managed/src/Assets/Material.hxx | 57 +++++++++++++++- SHADE_Managed/src/Assets/Mesh.cxx | 45 +++++++++++-- SHADE_Managed/src/Assets/Mesh.hxx | 65 +++++++++++++++++-- SHADE_Managed/src/Assets/NativeAsset.cxx | 14 ++++ SHADE_Managed/src/Assets/NativeAsset.h++ | 44 ------------- SHADE_Managed/src/Assets/NativeAsset.hxx | 57 +++++++--------- 12 files changed, 425 insertions(+), 98 deletions(-) create mode 100644 SHADE_Engine/src/Resource/SHResourceManagerWrapper.cpp create mode 100644 SHADE_Engine/src/Resource/SHResourceManagerWrapper.h delete mode 100644 SHADE_Managed/src/Assets/NativeAsset.h++ diff --git a/SHADE_Engine/src/Resource/SHResourceManager.h b/SHADE_Engine/src/Resource/SHResourceManager.h index 6afdf2b0..5b98ffc3 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.h +++ b/SHADE_Engine/src/Resource/SHResourceManager.h @@ -49,7 +49,8 @@ namespace SHADE template<> struct SHResourceLoader { using AssetType = SHMaterialAsset; }; template<> struct SHResourceLoader { using AssetType = SHMaterialSpec; }; template<> struct SHResourceLoader { using AssetType = SHFontAsset; }; -/// + + /// /// Static class responsible for loading and caching runtime resources from their /// serialised Asset IDs. /// diff --git a/SHADE_Engine/src/Resource/SHResourceManagerWrapper.cpp b/SHADE_Engine/src/Resource/SHResourceManagerWrapper.cpp new file mode 100644 index 00000000..f99e2dc6 --- /dev/null +++ b/SHADE_Engine/src/Resource/SHResourceManagerWrapper.cpp @@ -0,0 +1,34 @@ +/************************************************************************************//*! +\file SHResourceManagerWrapper.cpp +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 22, 2022 +\brief Contains the definition of the functions of the SHResourceManagerWraper + static class. + +Copyright (C) 2022 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 Header +#include "SHpch.h" +// Primary Include +#include "SHResourceManagerWrapper.h" +// Project Includes +#include "SHResourceManager.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Query Functions */ + /*-----------------------------------------------------------------------------------*/ + std::optional SHResourceManagerWrapper::GetAssetID(Handle handle) + { + return SHResourceManager::GetAssetID(handle); + } + + std::optional SHResourceManagerWrapper::GetAssetName(Handle handle) + { + return SHResourceManager::GetAssetName(handle); + } +} diff --git a/SHADE_Engine/src/Resource/SHResourceManagerWrapper.h b/SHADE_Engine/src/Resource/SHResourceManagerWrapper.h new file mode 100644 index 00000000..9f34f74e --- /dev/null +++ b/SHADE_Engine/src/Resource/SHResourceManagerWrapper.h @@ -0,0 +1,57 @@ +/************************************************************************************//*! +\file SHResourceManagerWrapper.h +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 22, 2022 +\brief Contains the definition of the SHResourceManagerWrapper static class. + +Copyright (C) 2022 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 + +// STL Includes +#include +// Project Includes +#include "SH_API.h" +#include "Resource/SHHandle.h" +#include "Assets/SHAssetMacros.h" + +namespace SHADE +{ + /// + /// Static class providing access to non-templated functions of SHResourceManager for + /// SHADE_Managed. + /// + class SH_API SHResourceManagerWrapper + { + public: + /*---------------------------------------------------------------------------------*/ + /* Query Functions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Retrieves the AssetID associated with a specified Handle. + /// Compared to the templated version, this function is slower as it requires + /// searching through the storage of all resource types. + /// + /// Handle to get the AssetID of. + /// + /// AssetID for the specified Handle. If the Handle is invalid, there will be no + /// value. + /// + static std::optional GetAssetID(Handle handle); + /// + /// Retrieves the name associated with the AssetID that is associated with the + /// specified Handle. + /// Compared to the templated version, this function is slower as it requires + /// searching through the storage of all resource types. + /// + /// Handle to get the name of. + /// + /// Name for the specified Handle. If the Handle is invalid, there will be no + /// value. + /// + static std::optional GetAssetName(Handle handle); + }; +} \ No newline at end of file diff --git a/SHADE_Managed/src/Assets/Font.cxx b/SHADE_Managed/src/Assets/Font.cxx index 782b0688..b21c7a02 100644 --- a/SHADE_Managed/src/Assets/Font.cxx +++ b/SHADE_Managed/src/Assets/Font.cxx @@ -15,18 +15,57 @@ of DigiPen Institute of Technology is prohibited. #include "SHpch.h" // Primary Header #include "Font.hxx" +// Project Headers +#include "Utility/Convert.hxx" namespace SHADE { /*---------------------------------------------------------------------------------*/ - /* Explicit Template Instantiation */ + /* Properties */ /*---------------------------------------------------------------------------------*/ - template ref class NativeAsset; + Handle Font::NativeObject::get() + try + { + return Handle(Convert::ToNative(asset.NativeObjectHandle)); + } + catch (const BadHandleCastException&) + { + return Handle(); + } + GenericHandle Font::NativeObjectHandle::get() + { + return asset.NativeObjectHandle; + } + AssetID Font::NativeAssetID::get() + { + return asset.NativeAssetID; + } /*---------------------------------------------------------------------------------*/ /* Constructors/Destructor */ /*---------------------------------------------------------------------------------*/ Font::Font(Handle font) - : NativeAsset { font } + : asset { Handle(font) } {} + + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + Font::operator bool(Font asset) + { + return asset; + } + + /*---------------------------------------------------------------------------------*/ + /* Conversion Operators */ + /*---------------------------------------------------------------------------------*/ + Font::operator Asset(Font nativeAsset) + { + return nativeAsset.asset; + } + + Font::operator Font(Asset asset) + { + return Font(Handle(Convert::ToNative(asset.NativeObjectHandle))); + } } diff --git a/SHADE_Managed/src/Assets/Font.hxx b/SHADE_Managed/src/Assets/Font.hxx index fd194d1a..4acc49e6 100644 --- a/SHADE_Managed/src/Assets/Font.hxx +++ b/SHADE_Managed/src/Assets/Font.hxx @@ -24,11 +24,36 @@ namespace SHADE { /// /// Managed counterpart of the native Font object that can be fed to TextRenderables - /// for rendering. + /// for rendering. /// - public ref class Font : public NativeAsset + public value struct Font { internal: + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// Copy of the Handle to the native object. + /// + property Handle NativeObject + { + Handle get(); + } + /// + /// Generic handle for the native object + /// + property GenericHandle NativeObjectHandle + { + GenericHandle get(); + } + /// + /// The raw asset ID of the asset. + /// + property AssetID NativeAssetID + { + AssetID get(); + } + /*-----------------------------------------------------------------------------*/ /* Constructors/Destructor */ /*-----------------------------------------------------------------------------*/ @@ -37,5 +62,35 @@ namespace SHADE /// /// Handle to the font object. Font(Handle font); + + /*-----------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*-----------------------------------------------------------------------------*/ + /// + /// Implicit conversion operator to enable checking if a Font is valid. + /// + /// Asset to check. + /// True if the Asset is valid. + static operator bool(Font asset); + + /*-----------------------------------------------------------------------------*/ + /* Conversion Operators */ + /*-----------------------------------------------------------------------------*/ + /// + /// Conversion operator to enable casting from a Font to an Asset. + /// + /// Vector3 to convert from. + static explicit operator Asset(Font nativeAsset); + /// + /// Conversion operator to enable casting from a Asset to a Font. + /// + /// Vector2 to convert from. + static explicit operator Font(Asset vec); + + protected: + /*-----------------------------------------------------------------------------*/ + /* Data Members */ + /*-----------------------------------------------------------------------------*/ + Asset asset; }; } diff --git a/SHADE_Managed/src/Assets/Material.cxx b/SHADE_Managed/src/Assets/Material.cxx index f4262c2a..3caa5b22 100644 --- a/SHADE_Managed/src/Assets/Material.cxx +++ b/SHADE_Managed/src/Assets/Material.cxx @@ -53,15 +53,31 @@ namespace SHADE } /*---------------------------------------------------------------------------------*/ - /* Explicit Template Instantiation */ + /* Properties */ /*---------------------------------------------------------------------------------*/ - template ref class NativeAsset; + Handle Material::NativeObject::get() + try + { + return Handle(Convert::ToNative(asset.NativeObjectHandle)); + } + catch (const BadHandleCastException&) + { + return Handle(); + } + GenericHandle Material::NativeObjectHandle::get() + { + return asset.NativeObjectHandle; + } + AssetID Material::NativeAssetID::get() + { + return asset.NativeAssetID; + } /*---------------------------------------------------------------------------------*/ /* Constructors/Destructor */ /*---------------------------------------------------------------------------------*/ Material::Material(Handle material) - : NativeAsset{ material } + : asset { Handle(material) } {} /*---------------------------------------------------------------------------------*/ @@ -116,4 +132,25 @@ namespace SHADE throw gcnew System::ArgumentException("Attempted to retrieve an invalid property on a material."); } + + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + Material::operator bool(Material asset) + { + return asset; + } + + /*---------------------------------------------------------------------------------*/ + /* Conversion Operators */ + /*---------------------------------------------------------------------------------*/ + Material::operator Asset(Material nativeAsset) + { + return nativeAsset.asset; + } + + Material::operator Material(Asset asset) + { + return Material(Handle(Convert::ToNative(asset.NativeObjectHandle))); + } } diff --git a/SHADE_Managed/src/Assets/Material.hxx b/SHADE_Managed/src/Assets/Material.hxx index 25cc96a6..14cf557e 100644 --- a/SHADE_Managed/src/Assets/Material.hxx +++ b/SHADE_Managed/src/Assets/Material.hxx @@ -26,9 +26,34 @@ namespace SHADE /// Managed counterpart of the native MaterialInstance object containing material /// data that can be fed to Renderables for rendering. /// - public ref class Material : public NativeAsset + public value struct Material { internal: + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// Copy of the Handle to the native object. + /// + property Handle NativeObject + { + Handle get(); + } + /// + /// Generic handle for the native object + /// + property GenericHandle NativeObjectHandle + { + GenericHandle get(); + } + /// + /// The raw asset ID of the asset. + /// + property AssetID NativeAssetID + { + AssetID get(); + } + /*-----------------------------------------------------------------------------*/ /* Constructors/Destructor */ /*-----------------------------------------------------------------------------*/ @@ -77,5 +102,35 @@ namespace SHADE /// generic T GetProperty(System::String^ name); + + /*-----------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*-----------------------------------------------------------------------------*/ + /// + /// Implicit conversion operator to enable checking if a Material is valid. + /// + /// Asset to check. + /// True if the Asset is valid. + static operator bool(Material asset); + + /*-----------------------------------------------------------------------------*/ + /* Conversion Operators */ + /*-----------------------------------------------------------------------------*/ + /// + /// Conversion operator to enable casting from a Material to an Asset. + /// + /// Vector3 to convert from. + static explicit operator Asset(Material nativeAsset); + /// + /// Conversion operator to enable casting from a Asset to a Material. + /// + /// Vector2 to convert from. + static explicit operator Material(Asset vec); + + protected: + /*-----------------------------------------------------------------------------*/ + /* Data Members */ + /*-----------------------------------------------------------------------------*/ + Asset asset; }; } diff --git a/SHADE_Managed/src/Assets/Mesh.cxx b/SHADE_Managed/src/Assets/Mesh.cxx index 95a61ff6..bcfeac36 100644 --- a/SHADE_Managed/src/Assets/Mesh.cxx +++ b/SHADE_Managed/src/Assets/Mesh.cxx @@ -21,14 +21,51 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { /*---------------------------------------------------------------------------------*/ - /* Explicit Template Instantiation */ + /* Properties */ /*---------------------------------------------------------------------------------*/ - template ref class NativeAsset; + Handle Mesh::NativeObject::get() + try + { + return Handle(Convert::ToNative(asset.NativeObjectHandle)); + } + catch (const BadHandleCastException&) + { + return Handle(); + } + GenericHandle Mesh::NativeObjectHandle::get() + { + return asset.NativeObjectHandle; + } + AssetID Mesh::NativeAssetID::get() + { + return asset.NativeAssetID; + } /*---------------------------------------------------------------------------------*/ /* Constructors/Destructor */ /*---------------------------------------------------------------------------------*/ - Mesh::Mesh(Handle mesh) - : NativeAsset { mesh } + Mesh::Mesh(Handle Mesh) + : asset{ Handle(Mesh) } {} + + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + Mesh::operator bool(Mesh asset) + { + return asset; + } + + /*---------------------------------------------------------------------------------*/ + /* Conversion Operators */ + /*---------------------------------------------------------------------------------*/ + Mesh::operator Asset(Mesh nativeAsset) + { + return nativeAsset.asset; + } + + Mesh::operator Mesh(Asset asset) + { + return Mesh(Handle(Convert::ToNative(asset.NativeObjectHandle))); + } } diff --git a/SHADE_Managed/src/Assets/Mesh.hxx b/SHADE_Managed/src/Assets/Mesh.hxx index 8cd356ba..7403ae83 100644 --- a/SHADE_Managed/src/Assets/Mesh.hxx +++ b/SHADE_Managed/src/Assets/Mesh.hxx @@ -26,16 +26,71 @@ namespace SHADE /// Managed counterpart of the native Mesh object containing vertex data that can /// be fed to Renderables for rendering. /// - public ref class Mesh : public NativeAsset + public value struct Mesh { - internal: + internal: + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// Copy of the Handle to the native object. + /// + property Handle NativeObject + { + Handle get(); + } + /// + /// Generic handle for the native object + /// + property GenericHandle NativeObjectHandle + { + GenericHandle get(); + } + /// + /// The raw asset ID of the asset. + /// + property AssetID NativeAssetID + { + AssetID get(); + } + /*-----------------------------------------------------------------------------*/ /* Constructors/Destructor */ /*-----------------------------------------------------------------------------*/ /// - /// Constructor for the Mesh + /// Constructor for the Mesh. /// - /// Handle to the mesh object. - Mesh(Handle mesh); + /// Handle to the Mesh object. + Mesh(Handle Mesh); + + /*-----------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*-----------------------------------------------------------------------------*/ + /// + /// Implicit conversion operator to enable checking if a Mesh is valid. + /// + /// Asset to check. + /// True if the Asset is valid. + static operator bool(Mesh asset); + + /*-----------------------------------------------------------------------------*/ + /* Conversion Operators */ + /*-----------------------------------------------------------------------------*/ + /// + /// Conversion operator to enable casting from a Mesh to an Asset. + /// + /// Vector3 to convert from. + static explicit operator Asset(Mesh nativeAsset); + /// + /// Conversion operator to enable casting from a Asset to a Mesh. + /// + /// Vector2 to convert from. + static explicit operator Mesh(Asset vec); + + protected: + /*-----------------------------------------------------------------------------*/ + /* Data Members */ + /*-----------------------------------------------------------------------------*/ + Asset asset; }; } diff --git a/SHADE_Managed/src/Assets/NativeAsset.cxx b/SHADE_Managed/src/Assets/NativeAsset.cxx index 9cdb1840..3f827fea 100644 --- a/SHADE_Managed/src/Assets/NativeAsset.cxx +++ b/SHADE_Managed/src/Assets/NativeAsset.cxx @@ -17,6 +17,8 @@ of DigiPen Institute of Technology is prohibited. #include "NativeAsset.hxx" // Project Includes #include "Engine/GenericHandle.hxx" +#include "Utility/Convert.hxx" +#include "Resource/SHResourceManagerWrapper.h" namespace SHADE { @@ -27,6 +29,10 @@ namespace SHADE { return nativeObjHandle; } + AssetID Asset::NativeAssetID::get() + { + return SHResourceManagerWrapper::GetAssetID(Convert::ToNative(nativeObjHandle)).value_or(INVALID_ASSET_ID); + } /*---------------------------------------------------------------------------------*/ /* Constructors */ @@ -34,4 +40,12 @@ namespace SHADE Asset::Asset(Handle nativeHandle) : nativeObjHandle { Convert::ToCLI(Handle(nativeHandle)) } {} + + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + Asset::operator bool(Asset asset) + { + return asset.nativeObjHandle && asset.NativeAssetID != INVALID_ASSET_ID; + } } \ No newline at end of file diff --git a/SHADE_Managed/src/Assets/NativeAsset.h++ b/SHADE_Managed/src/Assets/NativeAsset.h++ deleted file mode 100644 index 05be83b4..00000000 --- a/SHADE_Managed/src/Assets/NativeAsset.h++ +++ /dev/null @@ -1,44 +0,0 @@ -/************************************************************************************//*! -\file NativeAsset.h++ -\author Tng Kah Wei, kahwei.tng, 390009620 -\par email: kahwei.tng\@digipen.edu -\date Oct 28, 2022 -\brief Contains the definition of templated functions for the managed - NativeAsset classes. - - Note: This file is written in C++17/CLI. - -Copyright (C) 2022 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 - -// Primary Include -#include "NativeAsset.hxx" -#include "Utility/Convert.hxx" - -namespace SHADE -{ - /*---------------------------------------------------------------------------------*/ - /* Properties */ - /*---------------------------------------------------------------------------------*/ - template - Handle NativeAsset::NativeObject::get() - try - { - return Handle(Convert::ToNative(nativeObjHandle)); - } - catch (const BadHandleCastException&) - { - return Handle(); // Null handle - } - - /*---------------------------------------------------------------------------------*/ - /* Constructors */ - /*---------------------------------------------------------------------------------*/ - template - NativeAsset::NativeAsset(Handle nativeObj) - : Asset { Handle(nativeObj) } - {} -} diff --git a/SHADE_Managed/src/Assets/NativeAsset.hxx b/SHADE_Managed/src/Assets/NativeAsset.hxx index 4d53ce6b..7ce9c6ed 100644 --- a/SHADE_Managed/src/Assets/NativeAsset.hxx +++ b/SHADE_Managed/src/Assets/NativeAsset.hxx @@ -14,14 +14,17 @@ of DigiPen Institute of Technology is prohibited. *//*************************************************************************************/ #pragma once +// External Dependencies +#include "Assets/SHAssetMacros.h" +// Project Includes #include "Engine/GenericHandle.hxx" namespace SHADE { /// - /// Abstract base class that all Native Assets will inherit from. + /// Struct that contains native asset information. /// - public ref class Asset abstract + public value struct Asset { internal: /*-----------------------------------------------------------------------------*/ @@ -34,6 +37,13 @@ namespace SHADE { GenericHandle get(); } + /// + /// The raw asset ID of the asset. + /// + property AssetID NativeAssetID + { + AssetID get(); + } /*-----------------------------------------------------------------------------*/ /* Constructors/Destructor */ @@ -44,43 +54,20 @@ namespace SHADE /// Native asset object handle. Asset(Handle nativeHandle); + /*-----------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*-----------------------------------------------------------------------------*/ + /// + /// Implicit conversion operator to enable checking if a Asset is valid. + /// + /// Asset to check. + /// True if the Asset is valid. + static operator bool(Asset asset); + protected: /*-----------------------------------------------------------------------------*/ /* Data Members */ /*-----------------------------------------------------------------------------*/ GenericHandle nativeObjHandle; }; - - /// - /// Generalised template class for a managed representation of a native asset - /// - /// - /// The type of the asset's native representation. - /// - template - public ref class NativeAsset abstract : Asset - { - internal: - /*-----------------------------------------------------------------------------*/ - /* Properties */ - /*-----------------------------------------------------------------------------*/ - /// - /// Copy of the Handle to the native object. - /// - property Handle NativeObject - { - Handle get(); - } - - /*-----------------------------------------------------------------------------*/ - /* Constructors/Destructor */ - /*-----------------------------------------------------------------------------*/ - /// - /// Constructor for the native asset - /// - /// Native asset object handle. - NativeAsset(Handle ptr); - }; } - -#include "NativeAsset.h++"