Universal AssetHandles. Serialize AssetHandle instead of AssetIdentifier

A bit unstable
This commit is contained in:
Simon Lübeß
2023-08-01 21:38:54 +02:00
parent 6d7f26b1f4
commit 60725c3588
45 changed files with 520 additions and 127 deletions
+21 -4
View File
@@ -5,6 +5,7 @@ using System;
using System.Collections;
using System.IO;
using System.Linq;
using GlitchyEngine.Content;
namespace GlitchyEditor.Assets;
@@ -96,6 +97,7 @@ class AssetHierarchy
internal TreeNode<AssetNode> _assetsDirectoryNode = null;
private append Dictionary<StringView, TreeNode<AssetNode>> _pathToAssetNode = .();
private append Dictionary<StringView, TreeNode<AssetNode>> _identifierToAssetNode = .();
private append Dictionary<AssetHandle, TreeNode<AssetNode>> _handleToAssetNode = .();
private append String _resourcesDirectory = .();
private append String _assetsDirectory = .();
@@ -166,7 +168,8 @@ class AssetHierarchy
}
}
}
/// Sets path to the directory that contains the engine assets.
public void SetResourcesDirectory(StringView fileName)
{
ResourcesDirectory = fileName;
@@ -206,6 +209,7 @@ class AssetHierarchy
Update();
}
/// Sets path to the directory that contains the game assets.
public void SetAssetsDirectory(StringView fileName)
{
AssetsDirectory = fileName;
@@ -225,7 +229,6 @@ class AssetHierarchy
return;
}
AssetNode assetNode = new AssetNode();
assetNode.Path = new String(AssetsDirectory);
assetNode.Name = new String();
@@ -239,7 +242,6 @@ class AssetHierarchy
Log.EngineLogger.Trace($"Created directory node for: \"{AssetsDirectory}\"");
_fileSystemDirty = true;
SetupFileSystemWatcher();
@@ -297,7 +299,7 @@ class AssetHierarchy
return .Err;
}
/// Gets the tree node for the given filePath or .Err, if the file/directory doesn't exist.
/// Gets the tree node for the given asset identifier or .Err, if the file/directory doesn't exist.
/// @param filePath the path for which to return the tree node.
/// @remarks Do not hold a reference to the TreeNode because it can become invalid when the file hierarchy changes.
public Result<TreeNode<AssetNode>> GetNodeFromIdentifier(StringView identifier)
@@ -310,6 +312,19 @@ class AssetHierarchy
return .Err;
}
/// Gets the tree node for the given asset handle; or .Err, if no file exists with the given handle.
/// @param filePath the path for which to return the tree node.
/// @remarks Do not hold a reference to the TreeNode because it can become invalid when the file hierarchy changes.
public Result<TreeNode<AssetNode>> GetNodeFromAssetHandle(AssetHandle assetHandle)
{
if (_handleToAssetNode.TryGetValue(assetHandle, let treeNode))
{
return treeNode;
}
return .Err;
}
public bool FileExists(StringView filePath)
{
return _pathToAssetNode.ContainsKey(filePath);
@@ -399,6 +414,8 @@ class AssetHierarchy
//GrabSubAssets(node);
HandleFile(treeNode.Value);
_handleToAssetNode.Add(assetNode.AssetFile.AssetConfig.AssetHandle, treeNode);
Log.EngineLogger.Trace($"Created file node for: \"{assetNode.Path}\"");
}
}
@@ -103,7 +103,10 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
{
for (let texture in effect.Textures)
{
ImGui.Button(texture.key);
ImGui.TextUnformatted(texture.key);
ImGui.SameLine();
ImGui.Button("Texture");
if (ImGui.BeginDragDropTarget())
{
@@ -315,7 +318,8 @@ class MaterialFile
{
public String Effect ~ delete _;
public Dictionary<String, String> Textures ~ DeleteDictionaryAndKeysAndValues!(_);
//public Dictionary<String, String> Textures ~ DeleteDictionaryAndKeysAndValues!(_);
public Dictionary<String, AssetHandle<Texture>> Textures ~ DeleteDictionaryAndKeys!(_);
public Dictionary<String, VariableValue> Variables ~
{
if (_ != null)
@@ -417,14 +421,13 @@ class MaterialAssetLoader : IAssetLoader, IAssetSaver //, IReloadingAssetLoader
Material material = new Material(fx);
for (let (slotName, textureIdentifier) in materialFile.Textures)
for (let (slotName, textureHandle) in materialFile.Textures)
{
AssetHandle<Texture> texture = contentManager.LoadAsset(textureIdentifier);
AssetHandle<Texture> texture = contentManager.LoadAsset(textureHandle);
if (texture.IsInvalid)
{
Log.EngineLogger.Error($"Failed to load texture \"{textureIdentifier}\".");
Log.EngineLogger.Error($"Failed to load texture \"{textureHandle}\".");
}
material.SetTexture(slotName, texture);
@@ -475,9 +478,7 @@ class MaterialAssetLoader : IAssetLoader, IAssetSaver //, IReloadingAssetLoader
for (let (slotName, texture) in material.[Friend]_textures)
{
Texture textureAsset = texture.Handle.Get();
materialFile.Textures.Add(new String(slotName), new String(textureAsset?.Identifier ?? ""));
materialFile.Textures.Add(new String(slotName), texture.Handle);
}
Effect effect = material.Effect;