Merge pull request #115 from cra0zy/fixcall

Improve library loading code
This commit is contained in:
Harry 2019-11-04 21:09:29 +01:00 committed by GitHub
commit 13d3e7ac74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 48 additions and 73 deletions

View file

@ -6,7 +6,7 @@
Please DO NOT MODIFY THIS FILE, modify .metadata files instead.
-->
<namespace name="Atk" library="libatk-1.0-0.dll">
<namespace name="Atk" library="Library.Atk">
<enum name="CoordType" cname="AtkCoordType" gtype="atk_coord_type_get_type" type="enum">
<member cname="ATK_XY_SCREEN" name="Screen" />
<member cname="ATK_XY_WINDOW" name="Window" />

View file

@ -1,6 +1,6 @@
<?xml version="1.0"?>
<api>
<namespace name="GLib" library="libglib-2.0-0.dll">
<namespace name="GLib" library="Library.GLib">
<enum name="ConnectFlags" cname="GConnectFlags" type="flags" />
<enum name="IOCondition" cname="GIOCondition" type="enum" />
<enum name="SeekType" cname="GSeekType" type="enum" />

View file

@ -6,7 +6,7 @@
Please DO NOT MODIFY THIS FILE, modify .metadata files instead.
-->
<namespace name="Gdk" library="libgdk-3-0.dll">
<namespace name="Gdk" library="Library.Gdk">
<enum name="AnchorHints" cname="GdkAnchorHints" gtype="gdk_anchor_hints_get_type" type="flags">
<member cname="GDK_ANCHOR_FLIP_X" name="FlipX" value="1 &lt;&lt; 0" />
<member cname="GDK_ANCHOR_FLIP_Y" name="FlipY" value="1 &lt;&lt; 1" />
@ -5301,7 +5301,7 @@
</method>
</class>
</namespace>
<namespace name="Gdk" library="libgdk_pixbuf-2.0-0.dll">
<namespace name="Gdk" library="Library.GdkPixbuf">
<enum name="Colorspace" cname="GdkColorspace" gtype="gdk_colorspace_get_type" type="enum">
<member cname="GDK_COLORSPACE_RGB" name="Rgb" />
</enum>

View file

@ -6,7 +6,7 @@
Please DO NOT MODIFY THIS FILE, modify .metadata files instead.
-->
<namespace name="G" library="libgio-2.0-0.dll">
<namespace name="G" library="Library.Gio">
<enum name="AppInfoCreateFlags" cname="GAppInfoCreateFlags" gtype="g_app_info_create_flags_get_type" type="flags">
<member cname="G_APP_INFO_CREATE_NONE" name="None" />
<member cname="G_APP_INFO_CREATE_NEEDS_TERMINAL" name="NeedsTerminal" value="1 &lt;&lt; 0" />

View file

@ -31,35 +31,12 @@ namespace Gtk {
const int WS_EX_TOOLWINDOW = 0x00000080;
const int WS_OVERLAPPEDWINDOW = 0x00CF0000;
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
delegate IntPtr d_Win32CreateWindow(int dwExStyle, string lpClassName, string lpWindowName,int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lParam);
static d_Win32CreateWindow Win32CreateWindow;
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
delegate bool d_Win32DestroyWindow(IntPtr window);
static d_Win32DestroyWindow Win32DestroyWindow;
static Application ()
{
if (!GLib.Thread.Supported)
GLib.Thread.Init ();
switch (Environment.OSVersion.Platform) {
case PlatformID.Win32NT:
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.WinCE:
Win32CreateWindow = FuncLoader.LoadFunction<d_Win32CreateWindow>(FuncLoader.GetProcAddress(GLibrary.Load("user32.dll"), "CreateWindowExW"));
Win32DestroyWindow = FuncLoader.LoadFunction<d_Win32DestroyWindow>(FuncLoader.GetProcAddress(GLibrary.Load("user32.dll"), "DestroyWindow"));
// No idea why we need to create that window, but it enables visual styles on the Windows platform
IntPtr window = Win32CreateWindow (WS_EX_TOOLWINDOW, "static", "gtk-sharp visual styles window", WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
Win32DestroyWindow (window);
break;
default:
break;
}
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate void d_gtk_init(ref int argc, ref IntPtr argv);
static d_gtk_init gtk_init = FuncLoader.LoadFunction<d_gtk_init>(FuncLoader.GetProcAddress(GLibrary.Load(Library.Gtk), "gtk_init"));

View file

@ -6,7 +6,7 @@
Please DO NOT MODIFY THIS FILE, modify .metadata files instead.
-->
<namespace name="Gtk" library="libgtk-3-0.dll">
<namespace name="Gtk" library="Library.Gtk">
<enum name="AccelFlags" cname="GtkAccelFlags" gtype="gtk_accel_flags_get_type" type="flags">
<member cname="GTK_ACCEL_VISIBLE" name="Visible" value="1 &lt;&lt; 0" />
<member cname="GTK_ACCEL_LOCKED" name="Locked" value="1 &lt;&lt; 1" />

View file

@ -6,7 +6,7 @@
Please DO NOT MODIFY THIS FILE, modify .metadata files instead.
-->
<namespace name="Pango" library="libpango-1.0-0.dll">
<namespace name="Pango" library="Library.Pango">
<enum name="Alignment" cname="PangoAlignment" gtype="pango_alignment_get_type" type="enum">
<member cname="PANGO_ALIGN_LEFT" name="Left" />
<member cname="PANGO_ALIGN_CENTER" name="Center" />

View file

@ -36,7 +36,7 @@
<attr path="/api/namespace/class/method[@cname='pango_version_string']" name="name">VersionString</attr>
<attr path="/api/namespace/class[@cname='PangoAttr_']" name="hidden">1</attr>
<attr path="/api/namespace/class[@cname='PangoCairo_']" name="name">CairoHelper</attr>
<attr path="/api/namespace/class[@cname='PangoCairo_']/method" name="library">libpangocairo-1.0-0.dll</attr>
<attr path="/api/namespace/class[@cname='PangoCairo_']/method" name="library">Library.PangoCairo</attr>
<attr path="/api/namespace/class[@cname='PangoCairo_']/method[@name='ContextGetFontOptions']" name="hidden">1</attr>
<attr path="/api/namespace/class[@cname='PangoCairo_']/method[@name='ContextSetFontOptions']" name="hidden">1</attr>
<attr path="/api/namespace/class[@cname='PangoGlobal']/method[@name='ExtentsToPixels']/*/*[@type='PangoRectangle*']" name="pass_as">ref</attr>

View file

@ -6,58 +6,56 @@ class GLibrary
{
private static Dictionary<Library, IntPtr> _libraries;
private static Dictionary<string, IntPtr> _customlibraries;
private static List<(Library Library, string WindowsLib, string LinuxLib, string OSXLib)> _libdict;
private static Dictionary <Library, string[]> _libraryDefinitions;
static GLibrary()
{
_customlibraries = new Dictionary<string, IntPtr>();
_libraries = new Dictionary<Library, IntPtr>();
_libdict = new List<(Library, string, string, string)>();
_libdict.Add((Library.GLib, "libglib-2.0-0.dll", "libglib-2.0.so.0", "libglib-2.0.0.dylib"));
_libdict.Add((Library.GObject, "libgobject-2.0-0.dll", "libgobject-2.0.so.0", "libgobject-2.0.0.dylib"));
_libdict.Add((Library.Cairo, "libcairo-2.dll", "libcairo.so.2", "libcairo.2.dylib"));
_libdict.Add((Library.Gio, "libgio-2.0-0.dll", "libgio-2.0.so.0", "libgio-2.0.0.dylib"));
_libdict.Add((Library.Atk, "libatk-1.0-0.dll", "libatk-1.0.so.0", "libatk-1.0.0.dylib"));
_libdict.Add((Library.Pango, "libpango-1.0-0.dll", "libpango-1.0.so.0", "libpango-1.0.0.dylib"));
_libdict.Add((Library.Gdk, "libgdk-3-0.dll", "libgdk-3.so.0", "libgdk-3.0.dylib"));
_libdict.Add((Library.GdkPixbuf, "libgdk_pixbuf-2.0-0.dll", "libgdk_pixbuf-2.0.so.0", "libgdk_pixbuf-2.0.dylib"));
_libdict.Add((Library.Gtk, "libgtk-3-0.dll", "libgtk-3.so.0", "libgtk-3.0.dylib"));
_libdict.Add((Library.PangoCairo, "libpangocairo-1.0-0.dll", "libpangocairo-1.0.so.0", "libpangocairo-1.0.0.dylib"));
}
public static IntPtr Load(string libname)
{
var index = _libdict.FindIndex((e) => (e.WindowsLib == libname || e.LinuxLib == libname || e.OSXLib == libname));
if (index != -1)
return Load(_libdict[index].Library);
var ret = IntPtr.Zero;
if (!_customlibraries.TryGetValue(libname, out ret))
_customlibraries[libname] = ret = FuncLoader.LoadLibrary(libname);
return ret;
_libraryDefinitions = new Dictionary<Library, string[]>();
_libraryDefinitions[Library.GLib] = new[] { "libglib-2.0-0.dll", "libglib-2.0.so.0", "libglib-2.0.0.dylib", "glib-2.dll" };
_libraryDefinitions[Library.GObject] = new[] { "libgobject-2.0-0.dll", "libgobject-2.0.so.0", "libgobject-2.0.0.dylib", "gobject-2.dll" };
_libraryDefinitions[Library.Cairo] = new[] { "libcairo-2.dll", "libcairo.so.2", "libcairo.2.dylib", "cairo.dll" };
_libraryDefinitions[Library.Gio] = new[] { "libgio-2.0-0.dll", "libgio-2.0.so.0", "libgio-2.0.0.dylib", "gio-2.dll" };
_libraryDefinitions[Library.Atk] = new[] { "libatk-1.0-0.dll", "libatk-1.0.so.0", "libatk-1.0.0.dylib", "atk-1.dll" };
_libraryDefinitions[Library.Pango] = new[] { "libpango-1.0-0.dll", "libpango-1.0.so.0", "libpango-1.0.0.dylib", "pango-1.dll" };
_libraryDefinitions[Library.Gdk] = new[] { "libgdk-3-0.dll", "libgdk-3.so.0", "libgdk-3.0.dylib", "gdk-3.dll" };
_libraryDefinitions[Library.GdkPixbuf] = new[] { "libgdk_pixbuf-2.0-0.dll", "libgdk_pixbuf-2.0.so.0", "libgdk_pixbuf-2.0.dylib", "gdk_pixbuf-2.dll" };
_libraryDefinitions[Library.Gtk] = new[] { "libgtk-3-0.dll", "libgtk-3.so.0", "libgtk-3.0.dylib", "gtk-3.dll" };
_libraryDefinitions[Library.PangoCairo] = new[] { "libpangocairo-1.0-0.dll", "libpangocairo-1.0.so.0", "libpangocairo-1.0.0.dylib", "pangocairo-1.dll" };
}
public static IntPtr Load(Library library)
{
IntPtr ret = IntPtr.Zero;
if (!_libraries.TryGetValue(library, out ret))
var ret = IntPtr.Zero;
if (_libraries.TryGetValue(library, out ret))
return ret;
if (FuncLoader.IsWindows)
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][0]);
else if (FuncLoader.IsOSX)
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][2]);
else
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][1]);
if (ret == IntPtr.Zero)
{
var i = _libdict.Find((e) => e.Library == library);
var s = i.LinuxLib;
for (int i = 0; i < _libraryDefinitions[library].Length; i++)
{
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][i]);
if (FuncLoader.IsWindows)
s = i.WindowsLib;
else if (FuncLoader.IsOSX)
s = i.OSXLib;
_libraries[library] = ret = FuncLoader.LoadLibrary(s);
if (ret != IntPtr.Zero)
break;
}
}
if (ret == IntPtr.Zero)
throw new DllNotFoundException(library.ToString());
{
var err = library + ": " + string.Join(", ", _libraryDefinitions);
throw new DllNotFoundException(err);
}
_libraries[library] = ret;
return ret;
}
}

View file

@ -67,7 +67,7 @@ namespace GtkSharp.Generation {
{
sw.WriteLine("\t\t[UnmanagedFunctionPointer (CallingConvention.Cdecl)]");
sw.WriteLine("\t\tdelegate IntPtr d_{0}({1});", CName, Parameters.ImportSignature);
sw.WriteLine("\t\tstatic d_{0} {0} = FuncLoader.LoadFunction<d_{0}>(FuncLoader.GetProcAddress(GLibrary.Load(\"{1}\"), \"{0}\"));", CName, LibraryName);
sw.WriteLine("\t\tstatic d_{0} {0} = FuncLoader.LoadFunction<d_{0}>(FuncLoader.GetProcAddress(GLibrary.Load({1}), \"{0}\"));", CName, LibraryName);
sw.WriteLine();
}

View file

@ -121,7 +121,7 @@ namespace GtkSharp.Generation {
var funcname = Elem.GetAttribute("gtype");
sw.WriteLine ("\t\t[UnmanagedFunctionPointer (CallingConvention.Cdecl)]");
sw.WriteLine ("\t\tdelegate IntPtr d_" + funcname + "();");
sw.WriteLine ("\t\tstatic d_" + funcname + " " + funcname + " = FuncLoader.LoadFunction<d_" + funcname + ">(FuncLoader.GetProcAddress(GLibrary.Load(\"" + LibraryName + "\"), \"" + funcname + "\"));");
sw.WriteLine ("\t\tstatic d_" + funcname + " " + funcname + " = FuncLoader.LoadFunction<d_" + funcname + ">(FuncLoader.GetProcAddress(GLibrary.Load(" + LibraryName + "), \"" + funcname + "\"));");
sw.WriteLine ();
sw.WriteLine ("\t\tpublic static GLib.GType GType {");
sw.WriteLine ("\t\t\tget {");

View file

@ -198,7 +198,7 @@ namespace GtkSharp.Generation {
sw.WriteLine("\t\tdelegate " + retval.CSType + " d_" + CName + "(" + import_sig + ");");
else
sw.WriteLine("\t\tdelegate " + retval.MarshalType + " d_" + CName + "(" + import_sig + ");");
sw.WriteLine("\t\tstatic d_" + CName + " " + CName + " = FuncLoader.LoadFunction<d_" + CName + ">(FuncLoader.GetProcAddress(GLibrary.Load(\"" + LibraryName + "\"), \"" + CName + "\"));");
sw.WriteLine("\t\tstatic d_" + CName + " " + CName + " = FuncLoader.LoadFunction<d_" + CName + ">(FuncLoader.GetProcAddress(GLibrary.Load(" + LibraryName + "), \"" + CName + "\"));");
sw.WriteLine();
}

View file

@ -186,7 +186,7 @@ namespace GtkSharp.Generation {
#endif
if (set_gvalue != null) {
sw.WriteLine("\t\tdelegate IntPtr d_{0}(ref GLib.Value val, IntPtr obj);", set_gvalue.CName);
sw.WriteLine("\t\tstatic d_{0} {0} = FuncLoader.LoadFunction<d_{0}>(FuncLoader.GetProcAddress(GLibrary.Load(\"{1}\"), \"{0}\"));", set_gvalue.CName, LibraryName);
sw.WriteLine("\t\tstatic d_{0} {0} = FuncLoader.LoadFunction<d_{0}>(FuncLoader.GetProcAddress(GLibrary.Load({1}), \"{0}\"));", set_gvalue.CName, LibraryName);
sw.WriteLine ();
sw.WriteLine ("\t\tpublic void SetGValue (ref GLib.Value val)");
sw.WriteLine ("\t\t{");