Skip to content

Commit

Permalink
Improvements for ResourcesPath; Cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
g0dzZz-coder committed Dec 16, 2023
1 parent 55ec265 commit 9125749
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 40 deletions.
10 changes: 10 additions & 0 deletions Runtime/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -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('\\', '/');
}
}
3 changes: 3 additions & 0 deletions Runtime/Extensions/StringExtensions.cs.meta

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

6 changes: 3 additions & 3 deletions Runtime/Files.Bundles/Sources/AssetBundleFromStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ AssetBundle IAssetBundleSource.Load(string by)
return AssetBundle.LoadFromStream(fileStream);
}

Task<AssetBundle> IAssetBundleSource.LoadAsync(string by, Action<float> onProgress,
async Task<AssetBundle> IAssetBundleSource.LoadAsync(string by, Action<float> 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);
}
Expand Down
70 changes: 35 additions & 35 deletions Runtime/Files.Resource/ResourcesPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand All @@ -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('\\', '/');
}
}
}
3 changes: 1 addition & 2 deletions Runtime/Files.Resource/ResourcesReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 9125749

Please sign in to comment.