Merge remote-tracking branch 'refs/remotes/origin/devel'

# Conflicts:
#	SharpPhysFS/PhysFS.cs
pull/2/head
Francesco Bertolaccini 2017-04-12 20:59:11 +02:00
commit a0794f5ea2
2 changed files with 51 additions and 6 deletions

View File

@ -99,6 +99,7 @@ namespace SharpPhysFS
} }
class Interop class Interop
: IDisposable
{ {
InvalidOperationException initException = new InvalidOperationException("Callbacks not initialized yet"); InvalidOperationException initException = new InvalidOperationException("Callbacks not initialized yet");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@ -201,6 +202,11 @@ namespace SharpPhysFS
: this($"{libname}.dll", $"{libname}.dylib", $"{libname}.so") : this($"{libname}.dll", $"{libname}.dylib", $"{libname}.so")
{ } { }
Func<string, IntPtr> loadLibrary;
Func<IntPtr, string, IntPtr> loadSymbol;
Func<IntPtr, bool> freeLibrary;
IntPtr library;
public Interop(string winlib, string maclib, string unixlib) public Interop(string winlib, string maclib, string unixlib)
{ {
/* This method is used to dynamically load the physfs /* This method is used to dynamically load the physfs
@ -212,28 +218,27 @@ namespace SharpPhysFS
* pointers from the loaded library * pointers from the loaded library
*/ */
Func<string, IntPtr> loadLibrary;
Func<IntPtr, string, IntPtr> loadSymbol;
string libraryName; string libraryName;
IntPtr library;
if (Environment.OSVersion.Platform == PlatformID.Win32NT) if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{ {
loadLibrary = DynamicLoader.LoadLibrary; loadLibrary = DynamicLoader.LoadLibrary;
loadSymbol = DynamicLoader.GetProcAddress; loadSymbol = DynamicLoader.GetProcAddress;
freeLibrary = DynamicLoader.FreeLibrary;
libraryName = winlib; libraryName = winlib;
} }
else if(Environment.OSVersion.Platform == PlatformID.MacOSX) else if(Environment.OSVersion.Platform == PlatformID.MacOSX)
{ {
loadLibrary = n => DynamicLoader.osx_dlopen(n, 1); loadLibrary = n => DynamicLoader.osx_dlopen(n, 1);
loadSymbol = DynamicLoader.osx_dlsym; loadSymbol = DynamicLoader.osx_dlsym;
freeLibrary = DynamicLoader.osx_dlclose;
libraryName = maclib; libraryName = maclib;
} }
else else
{ {
loadLibrary = n => DynamicLoader.unix_dlopen(n, 1); loadLibrary = n => DynamicLoader.unix_dlopen(n, 1);
loadSymbol = DynamicLoader.unix_dlsym; loadSymbol = DynamicLoader.unix_dlsym;
freeLibrary = DynamicLoader.unix_dlclose;
libraryName = unixlib; libraryName = unixlib;
} }
@ -253,5 +258,29 @@ namespace SharpPhysFS
field.SetValue(this, del); field.SetValue(this, del);
} }
} }
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
/*if (disposing)
{
// TODO: dispose managed state (managed objects).
}*/
freeLibrary(library);
disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
#endregion
} }
} }

View File

@ -684,7 +684,7 @@ namespace SharpPhysFS
/// <see cref="EnumerateFilesCallback(string, Action{string, string})"/> if you don't need /// <see cref="EnumerateFilesCallback(string, Action{string, string})"/> if you don't need
/// to pass custom data to the callback. /// to pass custom data to the callback.
/// </remarks> /// </remarks>
/// <typeparam name="T">Type of data passed to callbakc</typeparam> /// <typeparam name="T">Type of data passed to callback</typeparam>
/// <param name="dir">Directory, in platform-independent notation, to enumerate.</param> /// <param name="dir">Directory, in platform-independent notation, to enumerate.</param>
/// <param name="c">Callback function to notify about search path elements.</param> /// <param name="c">Callback function to notify about search path elements.</param>
/// <param name="data">Application-defined data passed to callback. Can be null.</param> /// <param name="data">Application-defined data passed to callback. Can be null.</param>
@ -725,9 +725,25 @@ namespace SharpPhysFS
return new PhysFSStream(this, handle, false); return new PhysFSStream(this, handle, false);
} }
public void Dispose() bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{ {
Deinit(); Deinit();
interop.Dispose();
}
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
} }
} }
} }