From 1242478731591431a214a884ce2bc1147ab6e3e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 13 May 2009 14:35:59 +0200 Subject: [PATCH] Add support for a GTypeName attribute to get correct native/managed type mappings This requires yet another patch to Gtk# trunk, also update the list of patches again. --- README | 4 ++ gstreamer-sharp/Application.cs | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/README b/README index ef2cd6624b..ba66d60440 100644 --- a/README +++ b/README @@ -2,6 +2,10 @@ To build this Gtk# from trunk is required and the following patches: http://bugzilla.novell.com/show_bug.cgi?id=323372 http://bugzilla.novell.com/show_bug.cgi?id=501685 +http://bugzilla.novell.com/show_bug.cgi?id=497667 +http://bugzilla.novell.com/show_bug.cgi?id=503048 +http://bugzilla.novell.com/show_bug.cgi?id=503060 +http://bugzilla.novell.com/show_bug.cgi?id=499900 If you're using Mono < 2.4 you also need to apply: http://bugzilla.novell.com/show_bug.cgi?id=477396 diff --git a/gstreamer-sharp/Application.cs b/gstreamer-sharp/Application.cs index e1d1eeb61d..c61d5f5be0 100644 --- a/gstreamer-sharp/Application.cs +++ b/gstreamer-sharp/Application.cs @@ -7,12 +7,34 @@ // // Copyright (C) 2002 Alp Toker // Copyright (C) 2006 Novell, Inc. +// Copyright (C) 2009 Sebastian Dröge // using System; +using System.Reflection; +using System.IO; using System.Runtime.InteropServices; namespace Gst { + + [AttributeUsage (AttributeTargets.Enum | AttributeTargets.Class | AttributeTargets.Struct) ] + public sealed class GTypeNameAttribute : Attribute { + string type_name; + + public GTypeNameAttribute (string gtype_name) { + this.type_name = gtype_name; + } + + public string TypeName { + get { + return type_name; + } + set { + type_name = value; + } + } + } + public static class Application { public static void Init() { IntPtr argv = new IntPtr (0); @@ -34,7 +56,53 @@ namespace Gst { gst_deinit(); } + private static System.Type GstTypeMapping (GLib.GType gtype, string gtype_name) { + Assembly[] assemblies = (Assembly[]) AppDomain.CurrentDomain.GetAssemblies ().Clone (); + + foreach (Assembly asm in assemblies) { + Type[] ts = asm.GetTypes (); + foreach (Type t in ts) { + if (t.IsDefined (typeof (Gst.GTypeNameAttribute), false)) { + GTypeNameAttribute gattr = (GTypeNameAttribute) Attribute.GetCustomAttribute (t, typeof (GTypeNameAttribute), false); + if (gtype_name.Equals (gattr.TypeName)) { + return t; + } + } + } + } + + foreach (Assembly asm in assemblies) { + foreach (AssemblyName ref_name in asm.GetReferencedAssemblies ()) { + string asm_dir = Path.GetDirectoryName (asm.Location); + try { + Assembly ref_asm; + if (File.Exists (Path.Combine (asm_dir, ref_name.Name + ".dll"))) + ref_asm = Assembly.LoadFrom (Path.Combine (asm_dir, ref_name.Name + ".dll")); + else + ref_asm = Assembly.Load (ref_name); + + Type[] ts = asm.GetTypes (); + foreach (Type t in ts) { + if (t.IsDefined (typeof (Gst.GTypeNameAttribute), false)) { + GTypeNameAttribute gattr = (GTypeNameAttribute) Attribute.GetCustomAttribute (t, typeof (GTypeNameAttribute), false); + if (gtype_name.Equals (gattr.TypeName)) { + return t; + } + } + } + + } catch (Exception) { + /* Failure to load a referenced assembly is not an error */ + } + } + } + + return null; + } + private static void RegisterManagedTypes() { + GLib.GType.GTypeMapping += GstTypeMapping; + GLib.GType t; t = Gst.Fraction.GType;