Skip to content

Commit

Permalink
[v0.2.5] Fix for ResourceAsset
Browse files Browse the repository at this point in the history
[!] Improvements for Database assets
  • Loading branch information
g0dzZz-coder committed Jan 21, 2024
1 parent f842aa5 commit 6894c4e
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Depra.Assets.Delegates;
using Depra.Assets.Exceptions;
using Depra.Assets.Extensions;
using Depra.Assets.Files;
using Depra.Assets.Files.Database;
using Depra.Assets.ValueObjects;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Depra.Assets.Files.Database
namespace Depra.Assets.Editor.Files
{
public sealed class DatabaseAsset<TAsset> : IAssetFile<TAsset>, IDisposable where TAsset : ScriptableObject
public sealed class EditorDatabaseAsset<TAsset> : IAssetFile<TAsset>, IDisposable where TAsset : ScriptableObject
{
private readonly Type _assetType;
private readonly DatabaseAssetUri _uri;
private readonly RuntimeDatabaseAsset<TAsset> _runtimeAsset;

private TAsset _loadedAsset;

public DatabaseAsset(DatabaseAssetUri uri)
public EditorDatabaseAsset(DatabaseAssetUri uri)
{
_uri = uri;
_assetType = typeof(TAsset);
Metadata = new AssetMetadata(_uri = uri, FileSize.Unknown);
Metadata = new AssetMetadata(_uri, FileSize.Unknown);
_runtimeAsset = new RuntimeDatabaseAsset<TAsset>(_uri);
}

public AssetMetadata Metadata { get; }
Expand All @@ -38,17 +44,9 @@ public TAsset Load()
return _loadedAsset;
}

TAsset loadedAsset = null;
#if UNITY_EDITOR
if (_uri.Exists())
{
loadedAsset = AssetDatabase.LoadAssetAtPath<TAsset>(_uri.Relative);
}
#endif
if (loadedAsset == null)
{
loadedAsset = CreateAsset();
}
var loadedAsset = (TAsset) ActivateAsset(_uri.Exists()
? AssetDatabase.LoadAssetAtPath<TAsset>(_uri.Relative)
: _runtimeAsset.Load());

Guard.AgainstNull(loadedAsset, () => new AssetCatNotBeCreated(_assetType, _assetType.Name));

Expand All @@ -65,39 +63,14 @@ public void Unload()
return;
}

#if UNITY_EDITOR
AssetDatabase.DeleteAsset(_uri.Relative);
#endif
_loadedAsset = null;
_runtimeAsset.Unload();
AssetDatabase.DeleteAsset(_uri.Relative);
}

[Obsolete("Not yet supported in Unity. Use DatabaseAsset<TAsset>.Load() instead")]
public Task<TAsset> LoadAsync(DownloadProgressDelegate onProgress = null,
CancellationToken cancellationToken = default)
{
if (IsLoaded)
{
onProgress?.Invoke(DownloadProgress.Full);
return Task.FromResult(_loadedAsset);
}

throw new AssetCanNotBeLoaded("Asynchronous loading is not supported by Unity");
}

private TAsset CreateAsset()
{
var asset = ScriptableObject.CreateInstance<TAsset>();
#if UNITY_EDITOR
asset = (TAsset) ActivateAsset(asset);
#endif

return asset;
}

#if UNITY_EDITOR
private Object ActivateAsset(Object asset)
{
_uri.Directory.CreateIfNotExists();
_uri.Directory.Require();

asset.name = _uri.Name;
AssetDatabase.CreateAsset(asset, _uri.Relative);
Expand All @@ -106,20 +79,19 @@ private Object ActivateAsset(Object asset)

return asset;
}
#endif
IEnumerable<IAssetUri> IAssetFile.Dependencies()
{
#if UNITY_EDITOR
var paths = AssetDatabase.GetDependencies(_uri.Relative);
foreach (var path in paths)
{
yield return new DatabaseAssetUri(path);
}
#else
return Array.Empty<IAssetUri>();
#endif
}

public IEnumerable<IAssetUri> Dependencies() => AssetDatabase
.GetDependencies(_uri.Relative, recursive: false)
.Select(path => new DatabaseAssetUri(path)).Cast<IAssetUri>();

void IDisposable.Dispose() => Unload();

[Obsolete("Not yet supported in Unity. Use DatabaseAsset<TAsset>.Load() instead")]
Task<TAsset> IAssetFile<TAsset>.LoadAsync(DownloadProgressDelegate onProgress,
CancellationToken cancellationToken)
{
onProgress?.Invoke(DownloadProgress.Full);
return Task.FromResult(IsLoaded ? _loadedAsset : Load());
}
}
}
File renamed without changes.
18 changes: 2 additions & 16 deletions Runtime/Extensions/DirectoryInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,16 @@
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using Depra.Assets.Common;

namespace Depra.Assets.Extensions
{
public static class DirectoryInfoExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsEmpty(this DirectoryInfo self) =>
self.EnumerateFileSystemInfos().Any() == false;
public static bool IsEmpty(this DirectoryInfo self) => self.EnumerateFileSystemInfos().Any() == false;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DeleteIfEmpty(this DirectoryInfo self)
{
if (self.Exists == false || self.IsEmpty() == false)
{
return;
}

self.Delete(true);
File.Delete(self.FullName + AssetTypes.META);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DirectoryInfo CreateIfNotExists(this DirectoryInfo self)
public static DirectoryInfo Require(this DirectoryInfo self)
{
if (self.Exists == false)
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Files.Bundles/Files/AssetBundleUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed record AssetBundleUri : IAssetUri
public AssetBundleUri(string path)
{
_fileInfo = new FileInfo(path);
_fileInfo.Directory.CreateIfNotExists();
_fileInfo.Directory.Require();

Name = string.IsNullOrEmpty(Extension)
? _fileInfo.Name
Expand Down
2 changes: 2 additions & 0 deletions Runtime/Files.Database/DatabaseAssetUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ public DatabaseAssetUri(string relativeDirectory, string name, string extension)
internal DirectoryInfo Directory { get; }

public bool Exists() => File.Exists(Absolute);

public override string ToString() => Absolute;
}
}
54 changes: 54 additions & 0 deletions Runtime/Files.Database/RuntimeDatabaseAsset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: Apache-2.0
// © 2023 Nikolay Melnikov <n.melnikov@depra.org>

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Depra.Assets.Delegates;
using Depra.Assets.Exceptions;
using Depra.Assets.ValueObjects;
using UnityEngine;

namespace Depra.Assets.Files.Database
{
public sealed class RuntimeDatabaseAsset<TAsset> : IAssetFile<TAsset>, IDisposable where TAsset : ScriptableObject
{
private TAsset _loadedAsset;

public RuntimeDatabaseAsset(string name) : this((IAssetUri) new AssetName(name)) { }

public RuntimeDatabaseAsset(IAssetUri uri) => Metadata = new AssetMetadata(uri, FileSize.Unknown);

public AssetMetadata Metadata { get; }
public bool IsLoaded => _loadedAsset != null;

public TAsset Load()
{
var loadedAsset = ScriptableObject.CreateInstance<TAsset>();
Guard.AgainstNull(loadedAsset, () => new AssetCatNotBeCreated(typeof(TAsset), nameof(TAsset)));

return _loadedAsset = loadedAsset;
}

public void Unload()
{
if (_loadedAsset)
{
_loadedAsset = null;
}
}

void IDisposable.Dispose() => Unload();

IEnumerable<IAssetUri> IAssetFile.Dependencies() => Array.Empty<IAssetUri>();

[Obsolete("Not yet supported in Unity. Use RuntimeDatabaseAsset<TAsset>.Load() instead")]
Task<TAsset> IAssetFile<TAsset>.LoadAsync(DownloadProgressDelegate onProgress,
CancellationToken cancellationToken)
{
onProgress?.Invoke(DownloadProgress.Full);
return Task.FromResult(IsLoaded ? _loadedAsset : Load());
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Files.Database/RuntimeDatabaseAsset.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions Runtime/Files.Resource/ResourcesAsset.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
// © 2023 Nikolay Melnikov <n.melnikov@depra.org>
// © 2023-2024 Nikolay Melnikov <n.melnikov@depra.org>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Depra.Assets.Delegates;
Expand All @@ -11,17 +12,21 @@
using Depra.Assets.ValueObjects;
using UnityEngine;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Depra.Assets.Files.Resource
{
public sealed class ResourcesAsset<TAsset> : IAssetFile<TAsset>, IDisposable where TAsset : Object
{
private readonly ResourcesPath _path;
private TAsset _loadedAsset;

public ResourcesAsset(ResourcesPath path)
{
Guard.AgainstNull(path, nameof(path));
Metadata = new AssetMetadata(path, FileSize.Unknown);
Metadata = new AssetMetadata(_path = path, FileSize.Unknown);
}

public AssetMetadata Metadata { get; }
Expand Down Expand Up @@ -64,7 +69,7 @@ public async Task<TAsset> LoadAsync(DownloadProgressDelegate onProgress = null,
}

var loadedAsset = await Resources
.LoadAsync(Metadata.Uri.Relative)
.LoadAsync<TAsset>(Metadata.Uri.Relative)
.ToTask(OnProgress, cancellationToken);

Guard.AgainstNull(loadedAsset, () => new ResourceNotLoaded(Metadata.Uri.Relative));
Expand All @@ -78,7 +83,14 @@ public async Task<TAsset> LoadAsync(DownloadProgressDelegate onProgress = null,
void OnProgress(float progress) => onProgress?.Invoke(new DownloadProgress(progress));
}

IEnumerable<IAssetUri> IAssetFile.Dependencies() => Array.Empty<IAssetUri>();
public IEnumerable<IAssetUri> Dependencies() =>
#if UNITY_EDITOR
AssetDatabase
.GetDependencies(_path.Project, recursive: false)
.Select(path => new AssetName(path));
#else
return Array.Empty<IAssetUri>();
#endif

void IDisposable.Dispose() => Unload();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.depra.assets.unity",
"version": "0.2.4",
"version": "0.2.5",
"displayName": "Depra.Assets",
"description": "Provides an API for loading Unity asset files in multiple ways through a single interface.",
"unity": "2021.3",
Expand Down

0 comments on commit 6894c4e

Please sign in to comment.