diff --git a/CHANGELOG.md b/CHANGELOG.md index 06d8732..ba48690 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,4 +10,7 @@ ## Version 0.2.0 - New dataless enumeration functions are available now, which do not cause GC havoc -- Unsafe enumeration functions are now marked obsolete \ No newline at end of file +- Unsafe enumeration functions are now marked obsolete + +## Version 1.0.0 +- DLL configuration on platforms other than Windows is done via app.config now, as per http://www.mono-project.com/docs/advanced/pinvoke/dllmap/ \ No newline at end of file diff --git a/SharpPhysFS/Interop.cs b/SharpPhysFS/Interop.cs index ba0e206..d4dbfbb 100644 --- a/SharpPhysFS/Interop.cs +++ b/SharpPhysFS/Interop.cs @@ -4,19 +4,19 @@ using System.Runtime.InteropServices; namespace SharpPhysFS { - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] public delegate int InitDelegate(); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] public delegate void DeinitDelegate(); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] public delegate IntPtr MallocDelegate(ulong size); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] public delegate IntPtr ReallocDelegate(IntPtr ptr, ulong size); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] public delegate void FreeDelegate(IntPtr ptr); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] public delegate void StringCallback(IntPtr data, string str); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] public delegate void EnumFilesCallback(IntPtr data, string origdir, string fname); [StructLayout(LayoutKind.Sequential)] @@ -62,224 +62,141 @@ namespace SharpPhysFS public FreeDelegate Free; } - static class DynamicLoader + static class Interop { - #region Windows - [DllImport("kernel32.dll", SetLastError = true)] - public static extern IntPtr LoadLibrary(string lpFileName); + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern void PHYSFS_getLinkedVersion(ref Version v); - [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] - public static extern IntPtr GetProcAddress(IntPtr hModule, string procName); + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_init(string argv0); - [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] - public static extern bool FreeLibrary(IntPtr hModule); - #endregion + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_deinit(); - #region Unix - [DllImport("libdl.so", CallingConvention = CallingConvention.Cdecl, EntryPoint = "dlopen")] - public static extern IntPtr unix_dlopen(string filename, int flags); + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_supportedArchiveTypes(); - [DllImport("libdl.so", CallingConvention = CallingConvention.Cdecl, EntryPoint = "dlsym")] - public static extern IntPtr unix_dlsym(IntPtr handle, string symbol); + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern void PHYSFS_freeList(IntPtr h); - [DllImport("libdl.so", CallingConvention = CallingConvention.Cdecl, EntryPoint = "dlclose")] - public static extern bool unix_dlclose(IntPtr handle); - #endregion + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_getLastError(); - #region OSX - [DllImport("libdyld.dylib", CallingConvention = CallingConvention.Cdecl, EntryPoint = "dlopen")] - public static extern IntPtr osx_dlopen(string filename, int flags); + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_getDirSeparator(); - [DllImport("libdyld.dylib", CallingConvention = CallingConvention.Cdecl, EntryPoint = "dlsym")] - public static extern IntPtr osx_dlsym(IntPtr handle, string symbol); + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern void PHYSFS_permitSymbolicLinks(int permit); - [DllImport("libdyld.dylib", CallingConvention = CallingConvention.Cdecl, EntryPoint = "dlclose")] - public static extern bool osx_dlclose(IntPtr handle); - #endregion - } + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_getCdRomDirs(); - class Interop - : IDisposable - { - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void FnGetLinkedVersion(ref Version v); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate int FnInit(string argv0); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate int FnDeinit(); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate int FnClose(IntPtr ptr); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate IntPtr FnSupportedArchiveTypes(); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void FnFreeList(IntPtr h); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate IntPtr FnGetLastError(); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void FnPermitSymbolicLinks(int permit); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate int FnSetWriteDir(string s); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate int FnAddToSearchPath(string s, int i); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate int FnSetSaneConfig(string s1, string s2, string s3, int i1, int i2); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate IntPtr FnEnumerateFiles(string s); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate long FnGetLastModTime(string s); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate long FnRead(IntPtr ptr1, IntPtr ptr2, uint i1, uint i2); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate long FnTell(IntPtr ptr); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate int FnSeek(IntPtr ptr, ulong u); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate long FnFileLength(IntPtr ptr); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate int FnSetAllocator(Allocator alloc); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate int FnMount(string s1, string s2, int i); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void FnGetCdRomDirsCallback(StringCallback c, IntPtr ptr); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate void FnEnumerateFilesCallback(string s, EnumFilesCallback c, IntPtr ptr); + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_getBaseDir(); - // When the callbacks are not yet initialized, instead of throwing a - // null reference exception we explain the problem. - // I think it makes for a more graceful failure. - public FnGetLinkedVersion PHYSFS_getLinkedVersion = (ref Version v) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnInit PHYSFS_init = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnDeinit PHYSFS_deinit = () => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSupportedArchiveTypes PHYSFS_supportedArchiveTypes = () => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnFreeList PHYSFS_freeList = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnGetLastError PHYSFS_getLastError = () => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnGetLastError PHYSFS_getDirSeparator = () => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnPermitSymbolicLinks PHYSFS_permitSymbolicLinks = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSupportedArchiveTypes PHYSFS_getCdRomDirs = () => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnGetLastError PHYSFS_getBaseDir = () => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnGetLastError PHYSFS_getUserDir = () => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnGetLastError PHYSFS_getWriteDir = () => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSetWriteDir PHYSFS_setWriteDir = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnAddToSearchPath PHYSFS_addToSearchPath = (a, b) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSetWriteDir PHYSFS_removeFromSearchPath = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSupportedArchiveTypes PHYSFS_getSearchPath = () => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSetSaneConfig PHYSFS_setSaneConfig = (a, b, c, d, e) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSetWriteDir PHYSFS_mkdir = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSetWriteDir PHYSFS_delete = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnEnumerateFiles PHYSFS_getRealDir = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnEnumerateFiles PHYSFS_enumerateFiles = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSetWriteDir PHYSFS_exists = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSetWriteDir PHYSFS_isDirectory = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSetWriteDir PHYSFS_isSymbolicLink = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnGetLastModTime PHYSFS_getLastModTime = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnEnumerateFiles PHYSFS_openWrite = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnEnumerateFiles PHYSFS_openAppend = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnEnumerateFiles PHYSFS_openRead = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnClose PHYSFS_close = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnRead PHYSFS_read = (a, b, c, d) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnRead PHYSFS_write = (a, b, c, d) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnClose PHYSFS_eof = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnTell PHYSFS_tell = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSeek PHYSFS_seek = (a, b) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnFileLength PHYSFS_fileLength = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSeek PHYSFS_setBuffer = (a, b) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnClose PHYSFS_flush = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnDeinit PHYSFS_isInit = () => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnDeinit PHYSFS_symbolicLinksPermitted = () => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnSetAllocator PHYSFS_setAllocator = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnMount PHYSFS_mount = (a, b, c) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnEnumerateFiles PHYSFS_getMountPoint = (a) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnGetCdRomDirsCallback PHYSFS_getCdRomDirsCallback = (a, b) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnGetCdRomDirsCallback PHYSFS_getSearchPathCallback = (a, b) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; - public FnEnumerateFilesCallback PHYSFS_enumerateFilesCallback = (a, b, c) => { throw new InvalidOperationException("Callbacks not initialized yet"); }; + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_getUserDir(); - public Interop() - : this("physfs.dll", "libphysfs.dylib", "libphysfs.so") - { } + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_getWriteDir(); - public Interop(string libname) - : this($"{libname}.dll", $"{libname}.dylib", $"{libname}.so") - { } + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_setWriteDir(string s); - Func loadLibrary; - Func loadSymbol; - Func freeLibrary; - IntPtr library; + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_addToSearchPath(string s, int i); - public Interop(string winlib, string maclib, string unixlib) - { - /* This method is used to dynamically load the physfs - * library. It works by detecting the current platform - * and deciding whether to use LoadLibrary/GetProcAddr - * on Windows or dlopen/dlsym on Linux and OSX. - * The the class is then scanned using reflection - * to populate all the callbacks with the right function - * pointers from the loaded library - */ + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_removeFromSearchPath(string s); - string libraryName; + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_getSearchPath(); - if (Environment.OSVersion.Platform == PlatformID.Win32NT) - { - loadLibrary = DynamicLoader.LoadLibrary; - loadSymbol = DynamicLoader.GetProcAddress; - freeLibrary = DynamicLoader.FreeLibrary; - libraryName = winlib; - } - else if(Environment.OSVersion.Platform == PlatformID.MacOSX) - { - loadLibrary = n => DynamicLoader.osx_dlopen(n, 1); - loadSymbol = DynamicLoader.osx_dlsym; - freeLibrary = DynamicLoader.osx_dlclose; - libraryName = maclib; - } - else - { - loadLibrary = n => DynamicLoader.unix_dlopen(n, 1); - loadSymbol = DynamicLoader.unix_dlsym; - freeLibrary = DynamicLoader.unix_dlclose; - libraryName = unixlib; - } + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_setSaneConfig(string s1, string s2, string s3, int i1, int i2); - library = loadLibrary(libraryName); - if (library == IntPtr.Zero) - { - throw new PhysFSLibNotFound(); - } + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_mkdir(string s); - var fields = typeof(Interop).GetFields(); + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_delete(string s); - foreach(var field in fields.Where(x => x.Name.StartsWith("PHYSFS_"))) - { - var funcPtr = loadSymbol(library, field.Name); - var del = Marshal.GetDelegateForFunctionPointer(funcPtr, field.FieldType); + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_getRealDir(string s); - field.SetValue(this, del); - } - } + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_enumerateFiles(string s); - #region IDisposable Support - private bool disposedValue = false; // To detect redundant calls + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_exists(string s); - protected virtual void Dispose(bool disposing) - { - if (!disposedValue) - { - /*if (disposing) - { - // TODO: dispose managed state (managed objects). - }*/ + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_isDirectory(string s); - freeLibrary(library); + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_isSymbolicLink(string s); - disposedValue = true; - } - } + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern long PHYSFS_getLastModTime(string s); - public void Dispose() - { - Dispose(true); - } - #endregion + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_openWrite(string s); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_openAppend(string s); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_openRead(string s); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_close(IntPtr ptr); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern long PHYSFS_read(IntPtr ptr1, IntPtr ptr2, uint i1, uint i2); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern long PHYSFS_write(IntPtr ptr1, IntPtr ptr2, uint i1, uint i2); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_eof(IntPtr ptr); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern long PHYSFS_tell(IntPtr ptr); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_seek(IntPtr ptr, ulong u); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern long PHYSFS_fileLength(IntPtr ptr); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_setBuffer(IntPtr ptr, ulong u); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_flush(IntPtr ptr); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_isInit(); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_symbolicLinksPermitted(); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_setAllocator(Allocator alloc); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern int PHYSFS_mount(string s1, string s2, int i); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr PHYSFS_getMountPoint(string s); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern void PHYSFS_getCdRomDirsCallback(StringCallback c, IntPtr p); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern void PHYSFS_getSearchPathCallback(StringCallback c, IntPtr p); + + [DllImport("physfs.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern void PHYSFS_enumerateFilesCallback(string s, EnumFilesCallback c, IntPtr p); } } diff --git a/SharpPhysFS/PhysFS.LowLevel.cs b/SharpPhysFS/PhysFS.LowLevel.cs index 2c847db..e8f4dd9 100644 --- a/SharpPhysFS/PhysFS.LowLevel.cs +++ b/SharpPhysFS/PhysFS.LowLevel.cs @@ -8,7 +8,7 @@ namespace SharpPhysFS { public static IntPtr OpenWrite(string filename, PhysFS physFS) { - var val = physFS.interop.PHYSFS_openWrite(filename); + var val = Interop.PHYSFS_openWrite(filename); if (val == null) throw new PhysFSException(physFS); return val; @@ -16,7 +16,7 @@ namespace SharpPhysFS public static IntPtr OpenAppend(string filename, PhysFS physFS) { - var val = physFS.interop.PHYSFS_openAppend(filename); + var val = Interop.PHYSFS_openAppend(filename); if (val == null) throw new PhysFSException(physFS); return val; @@ -24,7 +24,7 @@ namespace SharpPhysFS public static IntPtr OpenRead(string filename, PhysFS physFS) { - var val = physFS.interop.PHYSFS_openRead(filename); + var val = Interop.PHYSFS_openRead(filename); if (val == null) throw new PhysFSException(physFS); return val; @@ -32,62 +32,62 @@ namespace SharpPhysFS public static void Close(IntPtr file, PhysFS physFS) { - int err = physFS.interop.PHYSFS_close(file); + int err = Interop.PHYSFS_close(file); physFS.ThrowException(err); } - public static long Read(IntPtr file, byte[] buffer, uint objSize, uint objCount, PhysFS physFS) + public static long Read(IntPtr file, byte[] buffer, uint objSize, uint objCount) { unsafe { fixed (void* ptr = buffer) { - return physFS.interop.PHYSFS_read(file, (IntPtr)ptr, objSize, objCount); + return Interop.PHYSFS_read(file, (IntPtr)ptr, objSize, objCount); } } } - public static long Write(IntPtr file, byte[] buffer, uint objSize, uint objCount, PhysFS physFS) + public static long Write(IntPtr file, byte[] buffer, uint objSize, uint objCount) { unsafe { fixed (void* ptr = buffer) { - return physFS.interop.PHYSFS_write(file, (IntPtr)ptr, objSize, objCount); + return Interop.PHYSFS_write(file, (IntPtr)ptr, objSize, objCount); } } } - public static bool EOF(IntPtr file, PhysFS physFS) + public static bool EOF(IntPtr file) { - return physFS.interop.PHYSFS_eof(file) != 0; + return Interop.PHYSFS_eof(file) != 0; } - public static long Tell(IntPtr file, PhysFS physFS) + public static long Tell(IntPtr file) { - return physFS.interop.PHYSFS_tell(file); + return Interop.PHYSFS_tell(file); } public static void Seek(IntPtr file, ulong pos, PhysFS physFS) { - int err = physFS.interop.PHYSFS_seek(file, pos); + int err = Interop.PHYSFS_seek(file, pos); physFS.ThrowException(err); } - public static long FileLength(IntPtr file, PhysFS physFS) + public static long FileLength(IntPtr file) { - return physFS.interop.PHYSFS_fileLength(file); + return Interop.PHYSFS_fileLength(file); } public static void SetBuffer(IntPtr file, ulong bufSize, PhysFS physFS) { - int err = physFS.interop.PHYSFS_setBuffer(file, bufSize); + int err = Interop.PHYSFS_setBuffer(file, bufSize); physFS.ThrowException(err); } public static void Flush(IntPtr file, PhysFS physFS) { - int err = physFS.interop.PHYSFS_flush(file); + int err = Interop.PHYSFS_flush(file); physFS.ThrowException(err); } } diff --git a/SharpPhysFS/PhysFS.cs b/SharpPhysFS/PhysFS.cs index 5331f5f..f01a5b0 100644 --- a/SharpPhysFS/PhysFS.cs +++ b/SharpPhysFS/PhysFS.cs @@ -24,17 +24,13 @@ namespace SharpPhysFS { } } - Interop interop; - public PhysFS(string argv0) { - interop = new Interop(); Init(argv0); } public PhysFS(string argv0, string libname) { - interop = new Interop(libname); Init(argv0); } @@ -58,7 +54,7 @@ namespace SharpPhysFS public Version GetLinkedVersion() { Version v = new Version(); - interop.PHYSFS_getLinkedVersion(ref v); + Interop.PHYSFS_getLinkedVersion(ref v); return v; } @@ -73,7 +69,7 @@ namespace SharpPhysFS /// This should be the path of the executable (first arguments passed to main function in C programs) public void Init(string argv0) { - int err = interop.PHYSFS_init(argv0); + int err = Interop.PHYSFS_init(argv0); ThrowException(err); } @@ -96,7 +92,7 @@ namespace SharpPhysFS /// public void Deinit() { - int err = interop.PHYSFS_deinit(); + int err = Interop.PHYSFS_deinit(); ThrowException(err); } @@ -123,7 +119,7 @@ namespace SharpPhysFS /// True to append to search path, false to prepend. public void Mount(string dir, string mountPoint, bool appendToPath) { - int err = interop.PHYSFS_mount(dir, mountPoint, appendToPath ? 1 : 0); + int err = Interop.PHYSFS_mount(dir, mountPoint, appendToPath ? 1 : 0); ThrowException(err); } @@ -140,7 +136,7 @@ namespace SharpPhysFS /// String of last error message. public string GetLastError() { - return Marshal.PtrToStringAnsi(interop.PHYSFS_getLastError()); + return Marshal.PtrToStringAnsi(Interop.PHYSFS_getLastError()); } /// @@ -156,7 +152,7 @@ namespace SharpPhysFS /// Platform-dependent dir separator string public string GetDirSeparator() { - return Marshal.PtrToStringAnsi(interop.PHYSFS_getDirSeparator()); + return Marshal.PtrToStringAnsi(Interop.PHYSFS_getDirSeparator()); } /// @@ -202,7 +198,7 @@ namespace SharpPhysFS /// An enumeration of supported archive types public IEnumerable SupportedArchiveTypes() { - IntPtr archives = interop.PHYSFS_supportedArchiveTypes(); + IntPtr archives = Interop.PHYSFS_supportedArchiveTypes(); IntPtr i = archives; for (i = archives; Marshal.ReadIntPtr(i) != IntPtr.Zero; i = IntPtr.Add(i, IntPtr.Size)) { @@ -237,7 +233,7 @@ namespace SharpPhysFS /// true to permit symlinks, false to deny linking. public void PermitSymbolicLinks(bool permit) { - interop.PHYSFS_permitSymbolicLinks(permit ? 1 : 0); + Interop.PHYSFS_permitSymbolicLinks(permit ? 1 : 0); } /// @@ -274,7 +270,7 @@ namespace SharpPhysFS /// public string GetBaseDir() { - return Marshal.PtrToStringAnsi(interop.PHYSFS_getBaseDir()); + return Marshal.PtrToStringAnsi(Interop.PHYSFS_getBaseDir()); } /// @@ -292,7 +288,7 @@ namespace SharpPhysFS /// String of user dir in platform-dependent notation. public string GetUserDir() { - return Marshal.PtrToStringAnsi(interop.PHYSFS_getUserDir()); + return Marshal.PtrToStringAnsi(Interop.PHYSFS_getUserDir()); } /// @@ -304,7 +300,7 @@ namespace SharpPhysFS /// String of write dir in platform-dependent notation, OR null IF NO WRITE PATH IS CURRENTLY SET public string GetWriteDir() { - return Marshal.PtrToStringAnsi(interop.PHYSFS_getWriteDir()); + return Marshal.PtrToStringAnsi(Interop.PHYSFS_getWriteDir()); } /// @@ -322,7 +318,7 @@ namespace SharpPhysFS /// public void SetWriteDir(string path) { - int err = interop.PHYSFS_setWriteDir(path); + int err = Interop.PHYSFS_setWriteDir(path); ThrowException(err); } @@ -334,7 +330,7 @@ namespace SharpPhysFS [Obsolete("AddToSearchPath is deprecated, please use Mount instead")] public void AddToSearchPath(string newDir, bool appendToPath) { - int err = interop.PHYSFS_addToSearchPath(newDir, appendToPath ? 1 : 0); + int err = Interop.PHYSFS_addToSearchPath(newDir, appendToPath ? 1 : 0); ThrowException(err); } @@ -350,7 +346,7 @@ namespace SharpPhysFS /// dir/archive to remove. public void RemoveFromSearchPath(string oldDir) { - int err = interop.PHYSFS_removeFromSearchPath(oldDir); + int err = Interop.PHYSFS_removeFromSearchPath(oldDir); ThrowException(err); } @@ -420,7 +416,7 @@ namespace SharpPhysFS /// True to prepend the archives to the search path. False to append them. Ignored if !. public void SetSaneConfig(string organization, string appName, string archiveExt, bool includeCdRoms, bool archivesFirst) { - int err = interop.PHYSFS_setSaneConfig(organization, appName, archiveExt, includeCdRoms ? 1 : 0, archivesFirst ? 1 : 0); + int err = Interop.PHYSFS_setSaneConfig(organization, appName, archiveExt, includeCdRoms ? 1 : 0, archivesFirst ? 1 : 0); ThrowException(err); } @@ -440,7 +436,7 @@ namespace SharpPhysFS /// New dir to create. public void Mkdir(string dirName) { - int err = interop.PHYSFS_mkdir(dirName); + int err = Interop.PHYSFS_mkdir(dirName); ThrowException(err); } @@ -467,7 +463,7 @@ namespace SharpPhysFS /// Filename to delete. public void Delete(string filename) { - int err = interop.PHYSFS_delete(filename); + int err = Interop.PHYSFS_delete(filename); ThrowException(err); } @@ -491,7 +487,7 @@ namespace SharpPhysFS /// String of element of search path containing the the file in question. null if not found. public string GetRealDir(string filename) { - return Marshal.PtrToStringAnsi(interop.PHYSFS_getRealDir(filename)); + return Marshal.PtrToStringAnsi(Interop.PHYSFS_getRealDir(filename)); } /// @@ -506,7 +502,7 @@ namespace SharpPhysFS /// True if filename exists. false otherwise. public bool Exists(string fname) { - return interop.PHYSFS_exists(fname) != 0; + return Interop.PHYSFS_exists(fname) != 0; } /// @@ -518,7 +514,7 @@ namespace SharpPhysFS /// True if filename exists and is a directory. False otherwise. public bool IsDirectory(string fname) { - return interop.PHYSFS_isDirectory(fname) != 0; + return Interop.PHYSFS_isDirectory(fname) != 0; } /// @@ -530,7 +526,7 @@ namespace SharpPhysFS /// True if filename exists and is a symlink. False otherwise. public bool IsSymbolicLink(string fname) { - return interop.PHYSFS_isSymbolicLink(fname) != 0; + return Interop.PHYSFS_isSymbolicLink(fname) != 0; } /// @@ -545,7 +541,7 @@ namespace SharpPhysFS /// Last modified time of the file. -1 if it can't be determined. public long GetLastModTime(string fname) { - return interop.PHYSFS_getLastModTime(fname); + return Interop.PHYSFS_getLastModTime(fname); } /// @@ -558,7 +554,7 @@ namespace SharpPhysFS /// True if library is initialized, false if library is not. public bool IsInit() { - return interop.PHYSFS_isInit() != 0; + return Interop.PHYSFS_isInit() != 0; } /// @@ -571,12 +567,12 @@ namespace SharpPhysFS /// True if symlinks are permitted, false if not. public bool SymbolicLinksPermitted() { - return interop.PHYSFS_symbolicLinksPermitted() != 0; + return Interop.PHYSFS_symbolicLinksPermitted() != 0; } public void SetAllocator(Allocator allocator) { - int err = interop.PHYSFS_setAllocator(allocator); + int err = Interop.PHYSFS_setAllocator(allocator); ThrowException(err); } @@ -594,7 +590,7 @@ namespace SharpPhysFS /// String of mount point if added to path public string GetMountPoint(string dir) { - var s = Marshal.PtrToStringAnsi(interop.PHYSFS_getMountPoint(dir)); + var s = Marshal.PtrToStringAnsi(Interop.PHYSFS_getMountPoint(dir)); if(s == null) { throw new PhysFSException(this); @@ -615,7 +611,7 @@ namespace SharpPhysFS public void GetCdRomDirsCallback(StringCallback c, object data) { GCHandle objHandle = GCHandle.Alloc(data); - interop.PHYSFS_getCdRomDirsCallback(c, GCHandle.ToIntPtr(objHandle)); + Interop.PHYSFS_getCdRomDirsCallback(c, GCHandle.ToIntPtr(objHandle)); objHandle.Free(); } @@ -640,14 +636,14 @@ namespace SharpPhysFS /// Callback function to notify about detected drives. public void GetCdRomDirsCallback(Action c) { - interop.PHYSFS_getCdRomDirsCallback((p, s) => c(s), IntPtr.Zero); + Interop.PHYSFS_getCdRomDirsCallback((p, s) => c(s), IntPtr.Zero); } [Obsolete("The non-generic variant of GetSearchPathCallback is meant for internal use only. Consider using one of the generic alternatives.")] public void GetSearchPathCallback(StringCallback c, object data) { GCHandle objHandle = GCHandle.Alloc(data); - interop.PHYSFS_getSearchPathCallback(c, GCHandle.ToIntPtr(objHandle)); + Interop.PHYSFS_getSearchPathCallback(c, GCHandle.ToIntPtr(objHandle)); objHandle.Free(); } @@ -672,14 +668,14 @@ namespace SharpPhysFS /// Callback function to notify about search path elements. public void GetSearchPathCallback(Action c) { - interop.PHYSFS_getSearchPathCallback((p, s) => c(s), IntPtr.Zero); + Interop.PHYSFS_getSearchPathCallback((p, s) => c(s), IntPtr.Zero); } [Obsolete("The non-generic variant of EnumerateFilesCallback is meant for internal use only. Consider using one of the generic alternatives.")] public void EnumerateFilesCallback(string dir, EnumFilesCallback c, object data) { GCHandle objHandle = GCHandle.Alloc(data); - interop.PHYSFS_enumerateFilesCallback(dir, c, GCHandle.ToIntPtr(objHandle)); + Interop.PHYSFS_enumerateFilesCallback(dir, c, GCHandle.ToIntPtr(objHandle)); objHandle.Free(); } @@ -710,7 +706,7 @@ namespace SharpPhysFS /// Callback function to notify about search path elements. public void EnumerateFilesCallback(string dir, Action c) { - interop.PHYSFS_enumerateFilesCallback(dir, (data, origdir, fname) => c(origdir, fname), IntPtr.Zero); + Interop.PHYSFS_enumerateFilesCallback(dir, (data, origdir, fname) => c(origdir, fname), IntPtr.Zero); } public PhysFSStream OpenAppend(string file) @@ -740,7 +736,6 @@ namespace SharpPhysFS if (disposing) { Deinit(); - interop.Dispose(); } disposed = true; diff --git a/SharpPhysFS/PhysFSStream.cs b/SharpPhysFS/PhysFSStream.cs index d77e742..b231cc5 100644 --- a/SharpPhysFS/PhysFSStream.cs +++ b/SharpPhysFS/PhysFSStream.cs @@ -49,7 +49,7 @@ namespace SharpPhysFS { get { - return PhysFS.LowLevel.FileLength(handle, physFS); + return PhysFS.LowLevel.FileLength(handle); } } @@ -57,7 +57,7 @@ namespace SharpPhysFS { get { - return PhysFS.LowLevel.Tell(handle, physFS); + return PhysFS.LowLevel.Tell(handle); } set { @@ -67,7 +67,7 @@ namespace SharpPhysFS public long Read(byte[] buffer, uint offset, uint count) { - return PhysFS.LowLevel.Read(handle, buffer, 1, count, physFS); + return PhysFS.LowLevel.Read(handle, buffer, 1, count); } public override int Read(byte[] buffer, int offset, int count) @@ -81,9 +81,9 @@ namespace SharpPhysFS if (origin == SeekOrigin.Begin) pos = 0; else if (origin == SeekOrigin.Current) - pos = PhysFS.LowLevel.Tell(handle, physFS); + pos = PhysFS.LowLevel.Tell(handle); else - pos = PhysFS.LowLevel.FileLength(handle, physFS); + pos = PhysFS.LowLevel.FileLength(handle); PhysFS.LowLevel.Seek(handle, (ulong)(pos + offset), physFS); return pos + offset; @@ -91,7 +91,7 @@ namespace SharpPhysFS public long Write(byte[] buffer, uint offset, uint count) { - return PhysFS.LowLevel.Write(handle, buffer, 1, count, physFS); + return PhysFS.LowLevel.Write(handle, buffer, 1, count); } public override void Write(byte[] buffer, int offset, int count) diff --git a/SharpPhysFS/Properties/AssemblyInfo.cs b/SharpPhysFS/Properties/AssemblyInfo.cs index fafc200..c96917e 100644 --- a/SharpPhysFS/Properties/AssemblyInfo.cs +++ b/SharpPhysFS/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Francesco Bertolaccini")] [assembly: AssemblyProduct("SharpPhysFS")] -[assembly: AssemblyCopyright("Copyright © 2016 Francesco Bertolaccini")] +[assembly: AssemblyCopyright("")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.0.2.1")] -[assembly: AssemblyFileVersion("0.0.2.1")] +[assembly: AssemblyVersion("0.1.0.0")] +[assembly: AssemblyFileVersion("0.1.0.0")] diff --git a/Test/App.config b/Test/App.config index 88fa402..24fcfab 100644 --- a/Test/App.config +++ b/Test/App.config @@ -3,4 +3,5 @@ + \ No newline at end of file