Skip to content

Commit

Permalink
Merge pull request #305 from TheMadDodger/runtime
Browse files Browse the repository at this point in the history
Add a basic runtime and application
  • Loading branch information
TheMadDodger committed Feb 22, 2024
2 parents 109aafa + 48901fa commit 5715269
Show file tree
Hide file tree
Showing 65 changed files with 1,184 additions and 82 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ GloryEngine/Build/
GloryEngine/Engine/**/Debug
GloryEngine/Modules/**/Debug
GloryEngine/Editor/**/Debug
GloryEngine/Runtime/**/Debug
GloryEngine/Scripting/**/Debug
GloryEngine/Engine/**/Release
GloryEngine/Modules/**/Release
GloryEngine/Editor/**/Release
GloryEngine/Runtime/**/Release
GloryEngine/Scripting/**/Release

!GloryEngine/Engine/**/Data/Debug
Expand Down
2 changes: 1 addition & 1 deletion GloryEngine/Editor/Applications/Glorious/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ project "Glorious"
"GloryClusteredRenderer",
"GloryOpenGLGraphics",
--"GloryVulkanGraphics",
"GlorySDLImage",
"GlorySDLWindow",
"GlorySDLInput",
"GloryEditorSDL",
Expand All @@ -102,6 +101,7 @@ project "Glorious"
"GloryEngine.Core",
"GloryEngine.Entities",
"CSAPIInstall",
"GloryRuntime",
"WriteVersion",
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ project "GloriousLauncher"
--todo: When asset management is contained in its own lib these links are no more needed
"GloryJobs",
"GloryThreads",
"GloryUtils",
}

dependson
Expand Down
1 change: 1 addition & 0 deletions GloryEngine/Editor/Backend/GloryEditorOGL/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ project "GloryEditorOGL"
--todo: When asset management is contained in its own lib these links are no more needed
"GloryJobs",
"GloryThreads",
"GloryUtils",
}

defines
Expand Down
1 change: 1 addition & 0 deletions GloryEngine/Editor/Backend/GloryEditorSDL/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ project "GloryEditorSDL"
"ImGui",
"GloryEditor",
"GloryUtilsVersion",
"GloryUtils",
}

defines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ project "ASSIMPImporter"
--todo: When asset management is contained in its own lib these links are no more needed
"GloryJobs",
"GloryThreads",
"GloryUtils",
}

defines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ project "EntityPrefabImporter"
--todo: When asset management is contained in its own lib these links are no more needed
"GloryJobs",
"GloryThreads",
"GloryUtils",
}

defines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ project "SDLImageImporter"
--todo: When asset management is contained in its own lib these links are no more needed
"GloryJobs",
"GloryThreads",
"GloryUtils",
}

defines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ project "JoltEditorExtension"
--todo: When asset management is contained in its own lib these links are no more needed
"GloryJobs",
"GloryThreads",
"GloryUtils",
}

defines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ project "MonoEditorExtension"
--todo: When asset management is contained in its own lib these links are no more needed
"GloryJobs",
"GloryThreads",
"GloryUtils",
}

defines
Expand Down
6 changes: 4 additions & 2 deletions GloryEngine/Editor/GloryEditor/AssetCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ namespace Glory::Editor
continue;
}

AssetLocation compiledLocation{GenerateCompiledAssetPath(id).string()};
assetDatabase.SetAsset(compiledLocation, data.Meta);
AssetLocation location = data.Location;
location.Index = 0;
location.Path = GenerateCompiledAssetPath(id).string();
assetDatabase.SetAsset(location, data.Meta);
}
}

Expand Down
91 changes: 88 additions & 3 deletions GloryEngine/Engine/Core/AssetArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,42 @@
#include "BinaryStream.h"
#include "Resource.h"
#include "ResourceType.h"
#include "Debug.h"
#include "Engine.h"

#include <sstream>

namespace Glory
{
AssetArchive::AssetArchive(BinaryStream* pStream) : m_pStream(pStream)
AssetArchive::AssetArchive(BinaryStream* pStream, bool isNew) : m_pStream(pStream), m_Version(), m_Owned()
{
if (isNew)
{
m_Version = Version::Parse(GloryCoreVersion);
WriteVersion();
}
else
ReadVersion();
}

AssetArchive::~AssetArchive()
{
if (!m_pStream) return;
m_pStream->Close();
m_pStream = nullptr;

for (size_t i = 0; i < m_pResources.size(); ++i)
{
if (!m_Owned.IsSet(i)) continue;
delete m_pResources[i];
}
m_pResources.clear();
}

bool AssetArchive::VerifyVersion()
{
const Version currentVersion = Version::Parse(GloryCoreVersion);
return Version::Compare(m_Version, currentVersion) == 0;
}

void AssetArchive::Serialize(Resource* pResource)
Expand All @@ -23,14 +47,75 @@ namespace Glory
const uint32_t typeHash = ResourceTypes::GetHash(type);

/* Write name, type and sub resource count */
m_pStream->Write(pResource->Name()).Write(typeHash);
m_pStream->Write(pResource->GetUUID()).Write(pResource->Name()).Write(typeHash);

/* Write the resource */
pResource->Serialize(*m_pStream);
}

void AssetArchive::Deserialize(Resource* pResource)
void AssetArchive::Deserialize(Engine* pEngine)
{
if (!VerifyVersion())
{
std::string versionStr;
m_Version.GetVersionString(versionStr);
std::stringstream str;
str << "Compiled asset archive was built with a different core/runtime version (" << versionStr << ") than the current version " << GloryCoreVersion;
pEngine->GetDebug().LogFatalError(str.str());
return;
}

while (!m_pStream->Eof())
{
ReadResource(pEngine);
}

m_Owned.Reserve(m_pResources.size());
m_Owned.SetAll();
}

size_t AssetArchive::Size() const
{
return m_pResources.size();
}

Resource* AssetArchive::Get(Engine* pEngine, size_t index) const
{
if (!m_Owned.IsSet(index))
{
pEngine->GetDebug().LogError("Resource already claimed!");
return nullptr;
}
m_Owned.UnSet(index);
return m_pResources[index];
}

void AssetArchive::WriteVersion()
{
m_pStream->Write(m_Version);
}

void AssetArchive::ReadVersion()
{
m_pStream->Read(m_Version);
}

Resource* AssetArchive::ReadResource(Engine* pEngine)
{
std::string name;
uint32_t typeHash = 0;
UUID uuid = 0;
m_pStream->Read(uuid).Read(name).Read(typeHash);

const ResourceType* pType = pEngine->GetResourceTypes().GetResourceType(typeHash);
Resource* pResource = pType->Create();

pResource->SetName(name);
pResource->SetResourceUUID(uuid);

pResource->Deserialize(*m_pStream);
m_pResources.push_back(pResource);

return pResource;
}
}
22 changes: 20 additions & 2 deletions GloryEngine/Engine/Core/AssetArchive.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
#pragma once
#include "Version.h"

#include <vector>
#include <BitSet.h>

namespace Glory
{
class Resource;
class BinaryStream;
class Engine;

class AssetArchive
{
public:
AssetArchive(BinaryStream* pStream);
AssetArchive(BinaryStream* pStream, bool isNew=false);
virtual ~AssetArchive();

bool VerifyVersion();

void Serialize(Resource* pResource);
void Deserialize(Resource* pResource);
void Deserialize(Engine* pEngine);

size_t Size() const;
Resource* Get(Engine* pEngine, size_t index) const;

private:
void WriteVersion();
void ReadVersion();

Resource* ReadResource(Engine* pEngine);

BinaryStream* m_pStream;
Version m_Version;
std::vector<Resource*> m_pResources;
mutable Utils::BitSet m_Owned;
};
}
70 changes: 59 additions & 11 deletions GloryEngine/Engine/Core/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include "AssetDatabase.h"
#include "Engine.h"
#include "Debug.h"
#include "BinaryStream.h"
#include "AssetArchive.h"

#include <Hash.h>

namespace Glory
{
Expand Down Expand Up @@ -100,6 +104,32 @@ namespace Glory
});
}

const AssetArchive* AssetManager::GetOrLoadArchive(const std::filesystem::path& path)
{
if (!std::filesystem::exists(path))
{
std::stringstream str;
str << "Failed to load asset archive at path " << path << " file not found!";
m_pEngine->GetDebug().LogError(str.str());
return nullptr;
}

/* Load as archive */
BinaryFileStream stream{ path };
AssetArchive archive{ &stream };
archive.Deserialize(m_pEngine);
const std::string& str = path.string();
const uint32_t hash = Hashing::Hash(str.data());
m_LoadedArchives.Emplace(hash, std::move(archive));
stream.Close();
return &m_LoadedArchives.at(hash);
}

void AssetManager::AddAssetArchive(uint32_t hash, AssetArchive&& archive)
{
m_LoadedArchives.Emplace(hash, std::move(archive));
}

bool AssetManager::LoadResourceJob(UUID uuid)
{
Resource* pResource = LoadAsset(uuid);
Expand Down Expand Up @@ -138,22 +168,40 @@ namespace Glory
// return pSubResource;
//}

LoaderModule* pModule = m_pEngine->GetLoaderModule(meta.Hash());

if (pModule == nullptr) return nullptr;

//if (assetLocation.IsSubAsset)
//{
// throw new std::exception("Not implemented yet");
//}
Resource* pResource = nullptr;

std::filesystem::path path = m_pEngine->GetAssetDatabase().m_AssetPath;
path.append(assetLocation.Path);
if (path.extension().compare(".gcag") == 0)
{
const AssetArchive* archive = GetOrLoadArchive(path);
if (archive)
{
for (size_t i = 0; i < archive->Size(); ++i)
{
Resource* pSubResource = archive->Get(m_pEngine, i);
AddLoadedResource(pResource);
}

if (!std::filesystem::exists(path))
path = assetLocation.Path;
pResource = FindResource(uuid);
}
}

/*if (!pResource)
{
std::stringstream str;
str << "Using legacy loading for file: " << path;
m_pEngine->GetDebug().LogWarning(str.str());
LoaderModule* pModule = m_pEngine->GetLoaderModule(meta.Hash());
if (pModule == nullptr) return nullptr;
if (!std::filesystem::exists(path))
path = assetLocation.Path;
pResource = pModule->Load(path.string());
}*/

Resource* pResource = pModule->Load(path.string());
if (pResource == nullptr)
{
m_pEngine->GetDebug().LogError("Failed to load asset: " + std::to_string(uuid) + " at path: " + path.string());
Expand Down
6 changes: 6 additions & 0 deletions GloryEngine/Engine/Core/AssetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Glory
{
class Engine;

class AssetArchive;

struct CallbackData
{
CallbackData();
Expand Down Expand Up @@ -56,6 +58,9 @@ namespace Glory
bool IsLoading(UUID uuid);
void GetAllLoading(std::vector<UUID>& out);

const AssetArchive* GetOrLoadArchive(const std::filesystem::path& path);
void AddAssetArchive(uint32_t hash, AssetArchive&& archive);

private:
bool LoadResourceJob(UUID uuid);
Resource* LoadAsset(UUID uuid);
Expand All @@ -82,6 +87,7 @@ namespace Glory
};

Engine* m_pEngine;
ThreadedUMap<uint32_t, AssetArchive> m_LoadedArchives;
ThreadedUMap<UUID, Resource*> m_pLoadedAssets;
ThreadedVector<UUID> m_pLoadingAssets;
ThreadedUMap<std::string, size_t> m_PathToGroupIndex;
Expand Down
Loading

0 comments on commit 5715269

Please sign in to comment.