diff --git a/Runtime/Extensions/StringExtensions.cs b/Runtime/Extensions/StringExtensions.cs new file mode 100644 index 0000000..276f2ff --- /dev/null +++ b/Runtime/Extensions/StringExtensions.cs @@ -0,0 +1,10 @@ +using System.Runtime.CompilerServices; + +namespace Depra.Assets.Extensions +{ + public static class StringExtensions + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string ToUnixPath(this string path) => path.Replace('\\', '/'); + } +} \ No newline at end of file diff --git a/Runtime/Extensions/StringExtensions.cs.meta b/Runtime/Extensions/StringExtensions.cs.meta new file mode 100644 index 0000000..af9d8f2 --- /dev/null +++ b/Runtime/Extensions/StringExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6072be06380544b19402e69ed23dfe68 +timeCreated: 1702728893 \ No newline at end of file diff --git a/Runtime/Files.Bundles/Sources/AssetBundleFromStream.cs b/Runtime/Files.Bundles/Sources/AssetBundleFromStream.cs index f764cf4..edfa718 100644 --- a/Runtime/Files.Bundles/Sources/AssetBundleFromStream.cs +++ b/Runtime/Files.Bundles/Sources/AssetBundleFromStream.cs @@ -30,14 +30,14 @@ AssetBundle IAssetBundleSource.Load(string by) return AssetBundle.LoadFromStream(fileStream); } - Task IAssetBundleSource.LoadAsync(string by, Action onProgress, + async Task IAssetBundleSource.LoadAsync(string by, Action onProgress, CancellationToken cancellationToken) { Guard.AgainstFileNotFound(by); - using var stream = OpenStream(by); + await using var stream = OpenStream(by); - return AssetBundle + return await AssetBundle .LoadFromStreamAsync(stream) .ToTask(onProgress, cancellationToken); } diff --git a/Runtime/Files.Resource/ResourcesPath.cs b/Runtime/Files.Resource/ResourcesPath.cs index f0e6b07..8f4035b 100644 --- a/Runtime/Files.Resource/ResourcesPath.cs +++ b/Runtime/Files.Resource/ResourcesPath.cs @@ -5,9 +5,9 @@ using System.IO; using System.Runtime.CompilerServices; using Depra.Assets.Exceptions; +using Depra.Assets.Extensions; using Depra.Assets.Files.Resource.Exceptions; using Depra.Assets.ValueObjects; -using JetBrains.Annotations; using static Depra.Assets.Common.UnityProject; namespace Depra.Assets.Files.Resource @@ -16,51 +16,54 @@ public sealed record ResourcesPath : IAssetUri { private static readonly string RESOURCES_FOLDER_PATH = RESOURCES_FOLDER_NAME + Path.AltDirectorySeparatorChar; - public static ResourcesPath Empty => new(string.Empty); - public static ResourcesPath Invalid => new(nameof(Invalid)); - public static implicit operator ResourcesPath(string relativePath) => new(relativePath); + public static ResourcesPath Empty => new(string.Empty, string.Empty); + public static ResourcesPath Invalid => new(nameof(Invalid), string.Empty); + public static implicit operator ResourcesPath(string relativePath) => new(relativePath, string.Empty); - public ResourcesPath(string relativePath) => - Initialize(Utility.CombineProjectPath(relativePath)); - - public ResourcesPath(string name, string relativeDirectory = null, string extension = null) => - Initialize(Utility.CombineProjectPath(relativeDirectory, name, extension)); - - public string ProjectPath { get; private set; } - public string RelativePath { get; private set; } + public ResourcesPath(string projectPath) + { + Project = projectPath; + Absolute = Path.GetFullPath(Project); + Relative = Utility.FindRelativePath(Project); + Directory = new DirectoryInfo(Path.GetDirectoryName(Absolute)!); + } - [UsedImplicitly] - public string AbsolutePath { get; private set; } - public DirectoryInfo Directory { get; private set; } + public ResourcesPath(string relativePath, string resourcesLocation = null) + { + Relative = relativePath; + Project = string.IsNullOrEmpty(resourcesLocation) + ? Path.Combine(ASSETS_FOLDER_NAME, RESOURCES_FOLDER_NAME, Relative) + : Path.Combine(ASSETS_FOLDER_NAME, RESOURCES_FOLDER_NAME, resourcesLocation, Relative); - string IAssetUri.Relative => RelativePath; - string IAssetUri.Absolute => AbsolutePath; + Absolute = Path.GetFullPath(Project); + Directory = new DirectoryInfo(Path.GetDirectoryName(Absolute)!); + } - private void Initialize(string projectPath) + public ResourcesPath(string name, string extension = null, string relativeDirectory = null, + string resourcesLocation = null) { - ProjectPath = projectPath; - RelativePath = Utility.FindRelativePath(ProjectPath); - AbsolutePath = Path.GetFullPath(ProjectPath); - var absoluteDirectoryPath = Path.GetDirectoryName(AbsolutePath); - Directory = new DirectoryInfo(absoluteDirectoryPath!); + Relative = string.IsNullOrEmpty(relativeDirectory) ? name : Path.Combine(relativeDirectory, name); + Project = string.IsNullOrEmpty(resourcesLocation) + ? Path.Combine(ASSETS_FOLDER_NAME, RESOURCES_FOLDER_NAME, Relative + extension) + : Path.Combine(ASSETS_FOLDER_NAME, RESOURCES_FOLDER_NAME, resourcesLocation, Relative + extension); + + Absolute = Path.GetFullPath(Project); + Directory = new DirectoryInfo(Path.GetDirectoryName(Absolute)!); } + public string Relative { get; } + public string Absolute { get; } + public string Project { get; } + public DirectoryInfo Directory { get; } + internal static class Utility { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string CombineProjectPath(string relativePath) => - Path.Combine(ASSETS_FOLDER_NAME, RESOURCES_FOLDER_NAME, relativePath); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string CombineProjectPath(string directory, string name, string extension = null) => - CombineProjectPath(Path.Combine(directory ?? string.Empty, name + extension)); - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string FindRelativePath(string projectPath) { Guard.AgainstEmptyString(projectPath, () => new NullReferenceException(nameof(projectPath))); - projectPath = ToUnixPath(projectPath); + projectPath = projectPath.ToUnixPath(); var folderIndex = projectPath.IndexOf(RESOURCES_FOLDER_PATH, StringComparison.OrdinalIgnoreCase); Guard.AgainstEqual(folderIndex, -1, () => new PathDoesNotContainResourcesFolder(projectPath)); @@ -71,9 +74,6 @@ public static string FindRelativePath(string projectPath) return projectPath.Substring(folderIndex, length); } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static string ToUnixPath(string path) => path.Replace('\\', '/'); } } } \ No newline at end of file diff --git a/Runtime/Files.Resource/ResourcesReference.cs b/Runtime/Files.Resource/ResourcesReference.cs index 51d03a3..265408d 100644 --- a/Runtime/Files.Resource/ResourcesReference.cs +++ b/Runtime/Files.Resource/ResourcesReference.cs @@ -24,9 +24,8 @@ public class ResourcesReference : ISerializationCallbackReceiver internal protected ResourcesReference() { } + public ResourcesPath Path => new(GetProjectPath()); public bool IsNull => string.IsNullOrEmpty(_projectPath); - public string ProjectPath => GetProjectPath(); - public string ResourcePath => ResourcesPath.Utility.FindRelativePath(ProjectPath); private string GetProjectPath() {