Added C# Assets System and Serialization of Script Enabled State #247

Merged
Pycorax merged 11 commits from SP3-6-c-scripting into main 2022-11-22 18:59:32 +08:00
12 changed files with 425 additions and 98 deletions
Showing only changes of commit a78b3c0123 - Show all commits

View File

@ -49,7 +49,8 @@ namespace SHADE
template<> struct SHResourceLoader<SHMaterialSpec> { using AssetType = SHMaterialAsset; };
template<> struct SHResourceLoader<SHMaterial> { using AssetType = SHMaterialSpec; };
template<> struct SHResourceLoader<SHFont> { using AssetType = SHFontAsset; };
/// <summary>
/// <summary>
/// Static class responsible for loading and caching runtime resources from their
/// serialised Asset IDs.
/// </summary>

View File

@ -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<AssetID> SHResourceManagerWrapper::GetAssetID(Handle<void> handle)
{
return SHResourceManager::GetAssetID(handle);
}
std::optional<std::string> SHResourceManagerWrapper::GetAssetName(Handle<void> handle)
{
return SHResourceManager::GetAssetName(handle);
}
}

View File

@ -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 <optional>
// Project Includes
#include "SH_API.h"
#include "Resource/SHHandle.h"
#include "Assets/SHAssetMacros.h"
namespace SHADE
{
/// <summary>
/// Static class providing access to non-templated functions of SHResourceManager for
/// SHADE_Managed.
/// </summary>
class SH_API SHResourceManagerWrapper
{
public:
/*---------------------------------------------------------------------------------*/
/* Query Functions */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// 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.
/// </summary>
/// <param name="handle">Handle to get the AssetID of.</param>
/// <return>
/// AssetID for the specified Handle. If the Handle is invalid, there will be no
/// value.
/// </return>
static std::optional<AssetID> GetAssetID(Handle<void> handle);
/// <summary>
/// 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.
/// </summary>
/// <param name="handle">Handle to get the name of.</param>
/// <return>
/// Name for the specified Handle. If the Handle is invalid, there will be no
/// value.
/// </return>
static std::optional<std::string> GetAssetName(Handle<void> handle);
};
}

View File

@ -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<SHFont>;
Handle<SHFont> Font::NativeObject::get()
try
{
return Handle<SHFont>(Convert::ToNative(asset.NativeObjectHandle));
}
catch (const BadHandleCastException&)
{
return Handle<SHFont>();
}
GenericHandle Font::NativeObjectHandle::get()
{
return asset.NativeObjectHandle;
}
AssetID Font::NativeAssetID::get()
{
return asset.NativeAssetID;
}
/*---------------------------------------------------------------------------------*/
/* Constructors/Destructor */
/*---------------------------------------------------------------------------------*/
Font::Font(Handle<SHFont> font)
: NativeAsset<SHFont> { font }
: asset { Handle<void>(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<SHFont>(Convert::ToNative(asset.NativeObjectHandle)));
}
}

View File

@ -24,11 +24,36 @@ namespace SHADE
{
/// <summary>
/// Managed counterpart of the native Font object that can be fed to TextRenderables
/// for rendering.
/// for rendering.
/// </summary>
public ref class Font : public NativeAsset<SHFont>
public value struct Font
{
internal:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Copy of the Handle to the native object.
/// </summary>
property Handle<SHFont> NativeObject
{
Handle<SHFont> get();
}
/// <summary>
/// Generic handle for the native object
/// </summary>
property GenericHandle NativeObjectHandle
{
GenericHandle get();
}
/// <summary>
/// The raw asset ID of the asset.
/// </summary>
property AssetID NativeAssetID
{
AssetID get();
}
/*-----------------------------------------------------------------------------*/
/* Constructors/Destructor */
/*-----------------------------------------------------------------------------*/
@ -37,5 +62,35 @@ namespace SHADE
/// </summary>
/// <param name="font">Handle to the font object.</param>
Font(Handle<SHFont> font);
/*-----------------------------------------------------------------------------*/
/* Operator Overloads */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Implicit conversion operator to enable checking if a Font is valid.
/// </summary>
/// <param name="gameObj">Asset to check.</param>
/// <returns>True if the Asset is valid.</returns>
static operator bool(Font asset);
/*-----------------------------------------------------------------------------*/
/* Conversion Operators */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Conversion operator to enable casting from a Font to an Asset.
/// </summary>
/// <param name="vec">Vector3 to convert from.</param>
static explicit operator Asset(Font nativeAsset);
/// <summary>
/// Conversion operator to enable casting from a Asset to a Font.
/// </summary>
/// <param name="vec">Vector2 to convert from.</param>
static explicit operator Font(Asset vec);
protected:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
Asset asset;
};
}

View File

@ -53,15 +53,31 @@ namespace SHADE
}
/*---------------------------------------------------------------------------------*/
/* Explicit Template Instantiation */
/* Properties */
/*---------------------------------------------------------------------------------*/
template ref class NativeAsset<SHMaterialInstance>;
Handle<SHMaterialInstance> Material::NativeObject::get()
try
{
return Handle<SHMaterialInstance>(Convert::ToNative(asset.NativeObjectHandle));
}
catch (const BadHandleCastException&)
{
return Handle<SHMaterialInstance>();
}
GenericHandle Material::NativeObjectHandle::get()
{
return asset.NativeObjectHandle;
}
AssetID Material::NativeAssetID::get()
{
return asset.NativeAssetID;
}
/*---------------------------------------------------------------------------------*/
/* Constructors/Destructor */
/*---------------------------------------------------------------------------------*/
Material::Material(Handle<SHMaterialInstance> material)
: NativeAsset<SHMaterialInstance>{ material }
: asset { Handle<void>(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<SHMaterialInstance>(Convert::ToNative(asset.NativeObjectHandle)));
}
}

View File

@ -26,9 +26,34 @@ namespace SHADE
/// Managed counterpart of the native MaterialInstance object containing material
/// data that can be fed to Renderables for rendering.
/// </summary>
public ref class Material : public NativeAsset<SHMaterialInstance>
public value struct Material
{
internal:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Copy of the Handle to the native object.
/// </summary>
property Handle<SHMaterialInstance> NativeObject
{
Handle<SHMaterialInstance> get();
}
/// <summary>
/// Generic handle for the native object
/// </summary>
property GenericHandle NativeObjectHandle
{
GenericHandle get();
}
/// <summary>
/// The raw asset ID of the asset.
/// </summary>
property AssetID NativeAssetID
{
AssetID get();
}
/*-----------------------------------------------------------------------------*/
/* Constructors/Destructor */
/*-----------------------------------------------------------------------------*/
@ -77,5 +102,35 @@ namespace SHADE
/// </exception>
generic<typename T>
T GetProperty(System::String^ name);
/*-----------------------------------------------------------------------------*/
/* Operator Overloads */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Implicit conversion operator to enable checking if a Material is valid.
/// </summary>
/// <param name="gameObj">Asset to check.</param>
/// <returns>True if the Asset is valid.</returns>
static operator bool(Material asset);
/*-----------------------------------------------------------------------------*/
/* Conversion Operators */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Conversion operator to enable casting from a Material to an Asset.
/// </summary>
/// <param name="vec">Vector3 to convert from.</param>
static explicit operator Asset(Material nativeAsset);
/// <summary>
/// Conversion operator to enable casting from a Asset to a Material.
/// </summary>
/// <param name="vec">Vector2 to convert from.</param>
static explicit operator Material(Asset vec);
protected:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
Asset asset;
};
}

View File

@ -21,14 +21,51 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Explicit Template Instantiation */
/* Properties */
/*---------------------------------------------------------------------------------*/
template ref class NativeAsset<SHMesh>;
Handle<SHMesh> Mesh::NativeObject::get()
try
{
return Handle<SHMesh>(Convert::ToNative(asset.NativeObjectHandle));
}
catch (const BadHandleCastException&)
{
return Handle<SHMesh>();
}
GenericHandle Mesh::NativeObjectHandle::get()
{
return asset.NativeObjectHandle;
}
AssetID Mesh::NativeAssetID::get()
{
return asset.NativeAssetID;
}
/*---------------------------------------------------------------------------------*/
/* Constructors/Destructor */
/*---------------------------------------------------------------------------------*/
Mesh::Mesh(Handle<SHMesh> mesh)
: NativeAsset<SHMesh> { mesh }
Mesh::Mesh(Handle<SHMesh> Mesh)
: asset{ Handle<void>(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<SHMesh>(Convert::ToNative(asset.NativeObjectHandle)));
}
}

View File

@ -26,16 +26,71 @@ namespace SHADE
/// Managed counterpart of the native Mesh object containing vertex data that can
/// be fed to Renderables for rendering.
/// </summary>
public ref class Mesh : public NativeAsset<SHMesh>
public value struct Mesh
{
internal:
internal:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Copy of the Handle to the native object.
/// </summary>
property Handle<SHMesh> NativeObject
{
Handle<SHMesh> get();
}
/// <summary>
/// Generic handle for the native object
/// </summary>
property GenericHandle NativeObjectHandle
{
GenericHandle get();
}
/// <summary>
/// The raw asset ID of the asset.
/// </summary>
property AssetID NativeAssetID
{
AssetID get();
}
/*-----------------------------------------------------------------------------*/
/* Constructors/Destructor */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructor for the Mesh
/// Constructor for the Mesh.
/// </summary>
/// <param name="mesh">Handle to the mesh object.</param>
Mesh(Handle<SHMesh> mesh);
/// <param name="Mesh">Handle to the Mesh object.</param>
Mesh(Handle<SHMesh> Mesh);
/*-----------------------------------------------------------------------------*/
/* Operator Overloads */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Implicit conversion operator to enable checking if a Mesh is valid.
/// </summary>
/// <param name="gameObj">Asset to check.</param>
/// <returns>True if the Asset is valid.</returns>
static operator bool(Mesh asset);
/*-----------------------------------------------------------------------------*/
/* Conversion Operators */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Conversion operator to enable casting from a Mesh to an Asset.
/// </summary>
/// <param name="vec">Vector3 to convert from.</param>
static explicit operator Asset(Mesh nativeAsset);
/// <summary>
/// Conversion operator to enable casting from a Asset to a Mesh.
/// </summary>
/// <param name="vec">Vector2 to convert from.</param>
static explicit operator Mesh(Asset vec);
protected:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
Asset asset;
};
}

View File

@ -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<void> nativeHandle)
: nativeObjHandle { Convert::ToCLI(Handle<void>(nativeHandle)) }
{}
/*---------------------------------------------------------------------------------*/
/* Operator Overloads */
/*---------------------------------------------------------------------------------*/
Asset::operator bool(Asset asset)
{
return asset.nativeObjHandle && asset.NativeAssetID != INVALID_ASSET_ID;
}
}

View File

@ -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 <typename NativeAssetType>
Handle<NativeAssetType> NativeAsset<NativeAssetType>::NativeObject::get()
try
{
return Handle<NativeAssetType>(Convert::ToNative(nativeObjHandle));
}
catch (const BadHandleCastException&)
{
return Handle<NativeAssetType>(); // Null handle
}
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/
template <typename NativeAssetType>
NativeAsset<NativeAssetType>::NativeAsset(Handle<NativeAssetType> nativeObj)
: Asset { Handle<void>(nativeObj) }
{}
}

View File

@ -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
{
/// <summary>
/// Abstract base class that all Native Assets will inherit from.
/// Struct that contains native asset information.
/// </summary>
public ref class Asset abstract
public value struct Asset
{
internal:
/*-----------------------------------------------------------------------------*/
@ -34,6 +37,13 @@ namespace SHADE
{
GenericHandle get();
}
/// <summary>
/// The raw asset ID of the asset.
/// </summary>
property AssetID NativeAssetID
{
AssetID get();
}
/*-----------------------------------------------------------------------------*/
/* Constructors/Destructor */
@ -44,43 +54,20 @@ namespace SHADE
/// <param name="ptr">Native asset object handle.</param>
Asset(Handle<void> nativeHandle);
/*-----------------------------------------------------------------------------*/
/* Operator Overloads */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Implicit conversion operator to enable checking if a Asset is valid.
/// </summary>
/// <param name="gameObj">Asset to check.</param>
/// <returns>True if the Asset is valid.</returns>
static operator bool(Asset asset);
protected:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
GenericHandle nativeObjHandle;
};
/// <summary>
/// Generalised template class for a managed representation of a native asset
/// </summary>
/// <typeparam name="NativeAssetType">
/// The type of the asset's native representation.
/// </typeparam>
template<typename NativeAssetType>
public ref class NativeAsset abstract : Asset
{
internal:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Copy of the Handle to the native object.
/// </summary>
property Handle<NativeAssetType> NativeObject
{
Handle<NativeAssetType> get();
}
/*-----------------------------------------------------------------------------*/
/* Constructors/Destructor */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructor for the native asset
/// </summary>
/// <param name="ptr">Native asset object handle.</param>
NativeAsset(Handle<NativeAssetType> ptr);
};
}
#include "NativeAsset.h++"