[gst-cvs] gstreamer-sharp: Add support for a GTypeName attribute to get correct native/ managed type mappings

Sebastian Dröge slomo at kemper.freedesktop.org
Thu May 14 08:16:04 PDT 2009


Module: gstreamer-sharp
Branch: master
Commit: 1242478731591431a214a884ce2bc1147ab6e3e2
URL:    http://cgit.freedesktop.org/gstreamer/gstreamer-sharp/commit/?id=1242478731591431a214a884ce2bc1147ab6e3e2

Author: Sebastian Dröge <sebastian.droege at collabora.co.uk>
Date:   Wed May 13 14:35:59 2009 +0200

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(+), 0 deletions(-)

diff --git a/README b/README
index ef2cd66..ba66d60 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 e1d1eeb..c61d5f5 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 <sebastian.droege at collabora.co.uk>
 //
 
 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;





More information about the Gstreamer-commits mailing list