From c3bcdeced659feb8dd94c17daa4e5dca389bd40a Mon Sep 17 00:00:00 2001 From: Evan Hemsley <2342303+ehemsley@users.noreply.github.com> Date: Fri, 17 Jan 2020 19:16:09 -0800 Subject: [PATCH] renaming + optimizations --- SharpPhysFS.sln => NETPhysFS.sln | 2 +- SharpPhysFS/Interop.cs | 3 +- .../{SharpPhysFS.csproj => NETPhysFS.csproj} | 8 +++--- SharpPhysFS/PhysFS.LowLevel.cs | 2 +- SharpPhysFS/PhysFS.cs | 28 +++++++++++++------ SharpPhysFS/PhysFSStream.cs | 8 +++--- UnitTests/Tests.cs | 11 +++----- UnitTests/UnitTests.csproj | 7 ++--- 8 files changed, 37 insertions(+), 32 deletions(-) rename SharpPhysFS.sln => NETPhysFS.sln (95%) rename SharpPhysFS/{SharpPhysFS.csproj => NETPhysFS.csproj} (65%) diff --git a/SharpPhysFS.sln b/NETPhysFS.sln similarity index 95% rename from SharpPhysFS.sln rename to NETPhysFS.sln index b837d7d..fa6ee3a 100644 --- a/SharpPhysFS.sln +++ b/NETPhysFS.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.29709.97 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpPhysFS", "SharpPhysFS\SharpPhysFS.csproj", "{AD6AA182-8C7F-4F3A-AAEF-7BD993D1D262}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NETPhysFS", "SharpPhysFS\NETPhysFS.csproj", "{AD6AA182-8C7F-4F3A-AAEF-7BD993D1D262}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{250EE88E-18B5-4433-ACF0-24A1D81429CF}" ProjectSection(SolutionItems) = preProject diff --git a/SharpPhysFS/Interop.cs b/SharpPhysFS/Interop.cs index 9c3ebcc..e6fa9ec 100644 --- a/SharpPhysFS/Interop.cs +++ b/SharpPhysFS/Interop.cs @@ -1,8 +1,7 @@ using System; -using System.Linq; using System.Runtime.InteropServices; -namespace SharpPhysFS +namespace MoonTools.NETPhysFS { [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] public delegate int InitDelegate(); diff --git a/SharpPhysFS/SharpPhysFS.csproj b/SharpPhysFS/NETPhysFS.csproj similarity index 65% rename from SharpPhysFS/SharpPhysFS.csproj rename to SharpPhysFS/NETPhysFS.csproj index e3cc209..ea4a4e7 100644 --- a/SharpPhysFS/SharpPhysFS.csproj +++ b/SharpPhysFS/NETPhysFS.csproj @@ -3,10 +3,10 @@ 1.0.0 netstandard2.0 .NET wrapper for PhysFS - SharpPhysFS - SharpPhysFS - SharpPhysFS - SharpPhysFS + MoonTools.NETPhysFS + MoonTools.NETPhysFS + MoonTools.NETPhysFS + MoonTools.NETPhysFS true true diff --git a/SharpPhysFS/PhysFS.LowLevel.cs b/SharpPhysFS/PhysFS.LowLevel.cs index 087461b..3064828 100644 --- a/SharpPhysFS/PhysFS.LowLevel.cs +++ b/SharpPhysFS/PhysFS.LowLevel.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.InteropServices; -namespace SharpPhysFS +namespace MoonTools.NETPhysFS { public sealed partial class PhysFS { diff --git a/SharpPhysFS/PhysFS.cs b/SharpPhysFS/PhysFS.cs index 579de0c..b3216f5 100644 --- a/SharpPhysFS/PhysFS.cs +++ b/SharpPhysFS/PhysFS.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; -namespace SharpPhysFS +namespace MoonTools.NETPhysFS { public class PhysFSLibNotFound : Exception { @@ -253,11 +253,16 @@ namespace SharpPhysFS /// This call may block while drives spin up. Be forewarned. /// /// An enumeration of paths to available CD-ROM drives. - public string[] GetCdRomDirs() + public IEnumerable GetCdRomDirs() { - var list = new List(); - GetCdRomDirsCallback((s) => list.Add(s)); - return list.ToArray(); + IntPtr files = Interop.PHYSFS_getCdRomDirs(); + for (IntPtr ptr = files; Marshal.ReadIntPtr(ptr) != IntPtr.Zero; ptr = IntPtr.Add(ptr, IntPtr.Size)) + { + var strPtr = (IntPtr)Marshal.PtrToStructure(ptr, typeof(IntPtr)); + var str = Marshal.PtrToStringAnsi(strPtr); + if (!IsDirectory(str)) { yield return str; } // the dll seems to be returning directories. boo! + } + Interop.PHYSFS_freeList(files); } /// @@ -340,11 +345,16 @@ namespace SharpPhysFS /// /// Get the current search path. /// - public string[] GetSearchPath() + public IEnumerable GetSearchPath() { - var list = new List(); - GetSearchPathCallback((s) => list.Add(s)); - return list.ToArray(); + IntPtr files = Interop.PHYSFS_getSearchPath(); + for (IntPtr ptr = files; Marshal.ReadIntPtr(ptr) != IntPtr.Zero; ptr = IntPtr.Add(ptr, IntPtr.Size)) + { + var strPtr = (IntPtr)Marshal.PtrToStructure(ptr, typeof(IntPtr)); + var str = Marshal.PtrToStringAnsi(strPtr); + if (!IsDirectory(str)) { yield return str; } // the dll seems to be returning directories. boo! + } + Interop.PHYSFS_freeList(files); } /// diff --git a/SharpPhysFS/PhysFSStream.cs b/SharpPhysFS/PhysFSStream.cs index adbcbf9..7aa373b 100644 --- a/SharpPhysFS/PhysFSStream.cs +++ b/SharpPhysFS/PhysFSStream.cs @@ -1,7 +1,7 @@ using System; using System.IO; -namespace SharpPhysFS +namespace MoonTools.NETPhysFS { public class PhysFSStream : Stream { @@ -89,19 +89,19 @@ namespace SharpPhysFS return pos + offset; } - public long Write(byte[] buffer, uint offset, uint count) + public long Write(byte[] buffer, uint count) { return PhysFS.LowLevel.Write(handle, buffer, 1, count); } public override void Write(byte[] buffer, int offset, int count) { - Write(buffer, (uint)offset, (uint)count); + Write(buffer, (uint)count); } public override void SetLength(long value) { - throw new NotImplementedException(); + throw new NotSupportedException(); } protected override void Dispose(bool disposing) diff --git a/UnitTests/Tests.cs b/UnitTests/Tests.cs index c301742..d844582 100644 --- a/UnitTests/Tests.cs +++ b/UnitTests/Tests.cs @@ -3,7 +3,7 @@ using System.IO; using System.Linq; using FluentAssertions; using NUnit.Framework; -using SharpPhysFS; +using MoonTools.NETPhysFS; namespace UnitTests { @@ -21,7 +21,7 @@ namespace UnitTests public void VersionCheck(byte major, byte minor, byte patch) { using var pfs = new PhysFS(""); - new SharpPhysFS.Version() { major = major, minor = minor, patch = patch }.Should().BeEquivalentTo(pfs.GetLinkedVersion()); + new MoonTools.NETPhysFS.Version() { major = major, minor = minor, patch = patch }.Should().BeEquivalentTo(pfs.GetLinkedVersion()); } [Test] @@ -89,13 +89,10 @@ namespace UnitTests var effectiveCdDrives = DriveInfo.GetDrives() .Where(x => x.DriveType == DriveType.CDRom) .Select(x => x.RootDirectory.FullName) + .OrderBy(s => s) .ToArray(); - var enumeratedCdDrives = pfs.GetCdRomDirs(); - - Array.Sort(effectiveCdDrives); - Array.Sort(enumeratedCdDrives); - + var enumeratedCdDrives = pfs.GetCdRomDirs().OrderBy(s => s); enumeratedCdDrives.Should().BeEquivalentTo(effectiveCdDrives); } diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index e92ab8e..f19a2a6 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -3,10 +3,6 @@ netcoreapp3.0 false - - - - @@ -16,4 +12,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + + +