Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource Manager impl #4

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Hypercube.Shared/Resources/Manager/IResourceManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Hypercube.Shared.Resources.Manager;

public class IResourceManager
{

}
6 changes: 6 additions & 0 deletions Hypercube.Shared/Resources/Manager/ResourceManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Hypercube.Shared.Resources.Manager;

public class ResourceManager : IResourceManager
{

}
143 changes: 143 additions & 0 deletions Hypercube.Shared/Resources/ResourcePath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System.Diagnostics.CodeAnalysis;

namespace Hypercube.Shared.Resources;

public struct ResourcePath
{
public const char Separator = '/';
public const string SeparatorStr = "/";
public const char WinSeparator = '\\';
public const string WinSeparatorStr = @"\";

public static readonly ResourcePath Self = ".";


public ResourcePath(string path)
{
if (OperatingSystem.IsWindows())
{
Path = path.Replace('\\', '/');
return;
}

Path = path;
}

public readonly string Path { get; }

public bool Rooted => Path.Length > 0 && Path[0] == Separator;
public bool Relative => !Rooted;
public bool IsSelf => Path == Self.Path;

public string FilenameWithExt
{
get
{
var sepIndex = Path.LastIndexOf('/') + 1;
return sepIndex == -1 ? "" : Path[sepIndex..];
}
}

public string Extension
{
get
{
var filename = FilenameWithExt;

var ind = filename.LastIndexOf('.');
return ind <= 1
? string.Empty
: filename[ind..];
}
}

public string Filename
{
get
{
var filename = FilenameWithExt;

var ind = filename.LastIndexOf('.');
return ind <= 0
? filename
: filename[..ind];
}
}

public ResourcePath ParentDirectory
{
get
{
if (IsSelf)
{
return Self;
}

var idx = Path.Length > 1 && Path[^1] == '/'
? Path[..^1].LastIndexOf('/')
: Path.LastIndexOf('/');

return idx switch
{
-1 => Self,
0 => new ResourcePath(Path[..1]),
_ => new ResourcePath(Path[..idx])
};
}
}

public static implicit operator ResourcePath(string path)
{
return new ResourcePath(path);
}

public static ResourcePath operator +(ResourcePath l, ResourcePath r)
{
if (r.IsSelf)
return l;
if (r.Rooted)
return r;
if (l.Path == "")
return new ResourcePath("/" + r.Path);

if (l.Path.EndsWith("/"))
{
return new ResourcePath(l.Path + r.Path);
}

return new ResourcePath(l.Path + "/" + r.Path);
}

public static bool Equals(ResourcePath a, ResourcePath b)
{
return a.Path == b.Path;
}

public bool Equals(ResourcePath b)
{
return Path == b.Path;
}

public override bool Equals(object? other)
{
if (other is null || other is not ResourcePath b)
return false;

return Path == b.Path;
}

public override int GetHashCode()
{
return Path.GetHashCode();
}

public static bool operator ==(ResourcePath a, ResourcePath b)
{
return a.Equals(b);
}

public static bool operator !=(ResourcePath a, ResourcePath b)
{
return !a.Equals(b);
}
}
1 change: 1 addition & 0 deletions Hypercube.UnitTests/Hypercube.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<ItemGroup>
<ProjectReference Include="..\Hypercube.Shared.Math\Hypercube.Shared.Math.csproj" />
<ProjectReference Include="..\Hypercube.Shared\Hypercube.Shared.csproj" />
</ItemGroup>

</Project>
56 changes: 56 additions & 0 deletions Hypercube.UnitTests/ResourceManager/ResourcePathTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Hypercube.Shared.Resources;

namespace Hypercube.UnitTests.ResourceManager;

public class ResourcePathTest
{
[Test]
public void GetFilenameTest()
{
var resPath = new ResourcePath("/Rooted/path.txt");
Assert.That(resPath.Filename == "path");

Assert.Pass($"ResPath file name is {resPath.Filename}");
}

[Test]
public void GetExtensionTest()
{
var resPath = new ResourcePath("/Rooted/path.txt");
Assert.That(resPath.Extension == ".txt");
Assert.Pass($"ResPath ext is {resPath.Extension}");
}

[Test]
public void RootedTest()
{
var resPath = new ResourcePath("/Rooted/path.txt");
Assert.That(resPath.Rooted);
Assert.Pass("Res path is rooted");
}

[Test]
public void PathConcatTest()
{
var resPath = new ResourcePath("/Rooted/");
var resPath2 = new ResourcePath("path.txt");

var concated = resPath + resPath2;
Assert.That(concated.Path == "/Rooted/path.txt");

Assert.Pass($"Concatenated path is equal to {concated.Path}");
}

[Test]
public void PathEqualityTest()
{
var resPath = new ResourcePath("/Rooted/");
var resPath2 = new ResourcePath("/Rooted/");
var resPath3 = new ResourcePath("path.txt");

Assert.That(resPath != resPath3);
Assert.That(resPath == resPath2);

Assert.Pass("Passed equality test");
}
}
Loading