Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

2008-05-13 Mike Kestner <mkestner@novell.com>

	* libsteticui/AssemblyResolver.cs: update to JBs latest Linker version.
	* libsteticui/CecilWidgetLibrary.cs: use new cache API.
	* libsteticui/LibraryCache.cs: rewritten to use instances, 
	XmlSerialization of the index and keep the index in memory, and to add
	HasWidgets capability.  Should be able to remove the other cache from
	Application now.

svn path=/trunk/stetic/; revision=103101
  • Loading branch information...
commit e3639c0831628b59848b62e7c4f79f9c15272654 1 parent 23606bb
@mkestner mkestner authored
View
9 ChangeLog
@@ -1,3 +1,12 @@
+2008-05-13 Mike Kestner <mkestner@novell.com>
+
+ * libsteticui/AssemblyResolver.cs: update to JBs latest Linker version.
+ * libsteticui/CecilWidgetLibrary.cs: use new cache API.
+ * libsteticui/LibraryCache.cs: rewritten to use instances,
+ XmlSerialization of the index and keep the index in memory, and to add
+ HasWidgets capability. Should be able to remove the other cache from
+ Application now.
+
2008-05-11 Mike Kestner <mkestner@novell.com>
* libsteticui/SignalsEditor.cs: some Disposal bulletproofing.
View
167 libsteticui/AssemblyResolver.cs
@@ -4,7 +4,7 @@
// Author:
// Jb Evain (jbevain@gmail.com)
//
-// (C) 2005 Jb Evain
+// (C) 2007 Novell, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
@@ -30,29 +30,160 @@
// Keep in synch as much as possible.
using System;
+using System.Collections;
using System.Collections.Specialized;
using System.IO;
-using SR = System.Reflection;
using System.Text;
using Mono.Cecil;
-namespace Stetic
-{
- internal class AssemblyResolver
- {
- public virtual string Resolve (string fullName)
+namespace Stetic {
+
+ internal class AssemblyResolver : BaseAssemblyResolver {
+
+ Hashtable _assemblies;
+
+ public IDictionary AssemblyCache {
+ get { return _assemblies; }
+ }
+
+ public AssemblyResolver ()
{
- return Resolve (fullName, null);
+ _assemblies = new Hashtable ();
}
-
- public virtual string Resolve (AssemblyNameReference name)
+
+ public override AssemblyDefinition Resolve (AssemblyNameReference name)
{
- return Resolve (name, null);
+ AssemblyDefinition asm = (AssemblyDefinition) _assemblies [name.Name];
+ if (asm == null) {
+ asm = base.Resolve (name);
+ asm.Resolver = this;
+ _assemblies [name.Name] = asm;
+ }
+
+ return asm;
}
-
- public string Resolve (string fullName, StringCollection basePaths)
+
+ public TypeDefinition Resolve (TypeReference type)
{
- return Resolve (AssemblyNameReference.Parse (fullName), basePaths);
+ if (type is TypeDefinition)
+ return (TypeDefinition) type;
+
+ AssemblyNameReference reference = type.Scope as AssemblyNameReference;
+ if (reference != null) {
+ AssemblyDefinition assembly = Resolve (reference);
+ return assembly.MainModule.Types [type.FullName];
+ }
+
+ ModuleDefinition module = type.Scope as ModuleDefinition;
+ if (module != null)
+ return module.Types [type.FullName];
+
+ throw new NotImplementedException ();
+ }
+
+ public FieldDefinition Resolve (FieldReference field)
+ {
+ TypeDefinition type = Resolve (field.DeclaringType);
+ return GetField (type.Fields, field);
+ }
+
+ static FieldDefinition GetField (ICollection collection, FieldReference reference)
+ {
+ foreach (FieldDefinition field in collection) {
+ if (field.Name != reference.Name)
+ continue;
+
+ if (!AreSame (field.FieldType, reference.FieldType))
+ continue;
+
+ return field;
+ }
+
+ return null;
+ }
+
+ public MethodDefinition Resolve (MethodReference method)
+ {
+ TypeDefinition type = Resolve (method.DeclaringType);
+ if (method.Name == MethodDefinition.Cctor || method.Name == MethodDefinition.Ctor)
+ return GetMethod (type.Constructors, method);
+ else
+ return GetMethod (type, method);
+ }
+
+ MethodDefinition GetMethod (TypeDefinition type, MethodReference reference)
+ {
+ while (type != null) {
+ MethodDefinition method = GetMethod (type.Methods, reference);
+ if (method == null)
+ type = Resolve (type.BaseType);
+ else
+ return method;
+ }
+
+ return null;
+ }
+
+ static MethodDefinition GetMethod (ICollection collection, MethodReference reference)
+ {
+ foreach (MethodDefinition meth in collection) {
+ if (meth.Name != reference.Name)
+ continue;
+
+ if (!AreSame (meth.ReturnType.ReturnType, reference.ReturnType.ReturnType))
+ continue;
+
+ if (!AreSame (meth.Parameters, reference.Parameters))
+ continue;
+
+ return meth;
+ }
+
+ return null;
+ }
+
+ static bool AreSame (ParameterDefinitionCollection a, ParameterDefinitionCollection b)
+ {
+ if (a.Count != b.Count)
+ return false;
+
+ if (a.Count == 0)
+ return true;
+
+ for (int i = 0; i < a.Count; i++)
+ if (!AreSame (a [i].ParameterType, b [i].ParameterType))
+ return false;
+
+ return true;
+ }
+
+ static bool AreSame (TypeReference a, TypeReference b)
+ {
+ while (a is TypeSpecification || b is TypeSpecification) {
+ if (a.GetType () != b.GetType ())
+ return false;
+
+ a = ((TypeSpecification) a).ElementType;
+ b = ((TypeSpecification) b).ElementType;
+ }
+
+ if (a is GenericParameter || b is GenericParameter) {
+ if (a.GetType() != b.GetType())
+ return false;
+
+ GenericParameter pa = (GenericParameter) a;
+ GenericParameter pb = (GenericParameter) b;
+
+ return pa.Position == pb.Position;
+ }
+
+ return a.FullName == b.FullName;
+ }
+
+ public void CacheAssembly (AssemblyDefinition assembly)
+ {
+ _assemblies [assembly.Name.FullName] = assembly;
+ assembly.Resolver = this;
}
public string Resolve (AssemblyNameReference name, StringCollection basePaths)
@@ -81,8 +212,7 @@ public string Resolve (AssemblyNameReference name, StringCollection basePaths)
string GetCorlib (AssemblyNameReference reference)
{
- SR.AssemblyName corlib = typeof (object).Assembly.GetName ();
- if (corlib.Version == reference.Version)
+ if (typeof (object).Assembly.GetName ().Version == reference.Version)
return typeof (object).Assembly.Location;
string path = Directory.GetParent (
@@ -111,11 +241,6 @@ string GetCorlib (AssemblyNameReference reference)
return Path.Combine (path, "mscorlib.dll");
}
- public static bool OnMono ()
- {
- return typeof (object).Assembly.GetType ("System.MonoType", false) != null;
- }
-
static string GetAssemblyInGac (AssemblyNameReference reference)
{
if (reference.PublicKeyToken == null || reference.PublicKeyToken.Length == 0)
View
53 libsteticui/CecilWidgetLibrary.cs
@@ -11,6 +11,8 @@ namespace Stetic
{
internal class CecilWidgetLibrary: WidgetLibrary
{
+ static LibraryCache cache = LibraryCache.Load ();
+
AssemblyDefinition assembly;
DateTime timestamp;
string name;
@@ -48,16 +50,16 @@ public CecilWidgetLibrary (ImportContext importContext, string assemblyPath)
void ReadCachedDescription (string assemblyPath)
{
- string path = LibraryCache.GetCachedFilePath (assemblyPath);
- if (path == null)
+ if (!cache.IsCurrent (assemblyPath))
return;
-
- if (!File.Exists (path + ".objects"))
+
+ LibraryCache.LibraryInfo info = cache [assemblyPath];
+ if (!File.Exists (info.ObjectsPath))
return;
try {
objects = new XmlDocument ();
- objects.Load (path + ".objects");
+ objects.Load (info.ObjectsPath);
}
catch (Exception ex) {
Console.WriteLine (ex);
@@ -65,10 +67,10 @@ void ReadCachedDescription (string assemblyPath)
return;
}
- if (File.Exists (path + ".steticGui")) {
+ if (File.Exists (info.GuiPath)) {
try {
steticGui = new XmlDocument ();
- steticGui.Load (path + ".steticGui");
+ steticGui.Load (info.GuiPath);
}
catch (Exception ex) {
Console.WriteLine (ex);
@@ -80,19 +82,14 @@ void ReadCachedDescription (string assemblyPath)
void StoreCachedDescription ()
{
- string path = LibraryCache.UpdateCachedFile (fileName);
- if (path == null)
- return;
+ LibraryCache.LibraryInfo info = cache [fileName];
try {
- string objfile = path + ".objects";
- string guifile = path + ".steticGui";
-
- objects.Save (objfile);
+ objects.Save (info.ObjectsPath);
if (steticGui != null)
- steticGui.Save (guifile);
- else if (File.Exists (guifile))
- File.Delete (guifile);
+ steticGui.Save (info.GuiPath);
+ else if (File.Exists (info.GuiPath))
+ File.Delete (info.GuiPath);
}
catch (Exception ex) {
Console.WriteLine (ex);
@@ -362,18 +359,7 @@ void ScanDependencies (ArrayList list, AssemblyDefinition asm)
public static bool IsWidgetLibrary (string path)
{
- try {
- AssemblyDefinition adef = AssemblyFactory.GetAssembly (path);
-
- foreach (Resource res in adef.MainModule.Resources) {
- EmbeddedResource eres = res as EmbeddedResource;
- if (eres == null) continue;
- if (eres.Name == "objects.xml")
- return true;
- }
- } catch {
- }
- return false;
+ return cache [path].HasWidgets;
}
public static string FindAssembly (ImportContext importContext, string assemblyName, string basePath)
@@ -393,7 +379,7 @@ public static string FindAssembly (ImportContext importContext, string assemblyN
AssemblyResolver res = new AssemblyResolver ();
try {
- return res.Resolve (assemblyName, col);
+ return res.Resolve (AssemblyNameReference.Parse (assemblyName), col);
} catch {
}
return null;
@@ -426,7 +412,14 @@ public static List<ComponentType> GetComponentTypes (Application app, string fil
EmbeddedResource res = GetResource (asm, "objects.xml");
if (res == null)
return list;
+ else
+ return GetComponentsFromResource (app, asm, res, fileName);
+ }
+
+ static List<ComponentType> GetComponentsFromResource (Application app, AssemblyDefinition asm, EmbeddedResource res, string fileName)
+ {
+ List<ComponentType> list = new List<ComponentType> ();
MemoryStream ms = new MemoryStream (res.Data);
XmlDocument objects = new XmlDocument ();
objects.Load (ms);
View
350 libsteticui/LibraryCache.cs
@@ -1,138 +1,270 @@
+// LibraryCache.cs : Assembly file caching class
+//
+// Author: Mike Kestner <mkestner@novell.com>
+//
+// Copyright (c) 2008 Novell, Inc
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
using System;
using System.Collections;
+using System.Collections.Generic;
+using System.Collections.Specialized;
using System.IO;
using System.Xml;
+using System.Xml.Serialization;
+using ENV = System.Environment;
+using Mono.Cecil;
-namespace Stetic
-{
- static class LibraryCache
- {
- static string cachePath;
- static XmlDocument indexDoc;
-
- static LibraryCache ()
+namespace Stetic {
+
+ public class LibraryCache {
+
+ public class LibraryInfo {
+
+ public LibraryInfo () {}
+
+ string file;
+ Guid guid;
+ bool has_widgets;
+ DateTime timestamp;
+
+ string CacheDirectory {
+ get {
+ string path = Path.Combine (dir, Guid.ToString ());
+ if (!Directory.Exists (path))
+ Directory.CreateDirectory (path);
+ return path;
+ }
+ }
+
+ [XmlAttribute]
+ public string File {
+ get { return file; }
+ set { file = value; }
+ }
+
+ [XmlAttribute]
+ public Guid Guid {
+ get { return guid; }
+ set {
+ string path = Path.Combine (dir, guid.ToString ());
+ if (Directory.Exists (path))
+ Directory.Delete (path);
+ guid = value;
+ }
+ }
+
+ public string GuiPath {
+ get { return Path.Combine (CacheDirectory, "steticGui"); }
+ }
+
+ [XmlAttribute]
+ public bool HasWidgets {
+ get { return has_widgets; }
+ set { has_widgets = value; }
+ }
+
+ public string ObjectsPath {
+ get { return Path.Combine (CacheDirectory, "objects"); }
+ }
+
+ [XmlAttribute]
+ public DateTime Timestamp {
+ get { return timestamp; }
+ set { timestamp = value; }
+ }
+ }
+
+ static string dir = Path.Combine (Path.Combine (ENV.GetFolderPath (ENV.SpecialFolder.ApplicationData), "stetic"), "library-cache");
+
+ public class LibraryInfoCollection : IEnumerable {
+
+ Dictionary<string, LibraryInfo> libs = new Dictionary<string, LibraryInfo> ();
+
+ public LibraryInfo this [string path] {
+ get {
+ path = Path.GetFullPath (path);
+ if (libs.ContainsKey (path))
+ return libs [path];
+ return null;
+ }
+ }
+
+ public void Add (object obj)
+ {
+ Add (obj as LibraryInfo);
+ }
+
+ public void Add (LibraryInfo info)
+ {
+ libs [info.File] = info;
+ }
+
+ public IEnumerator GetEnumerator ()
+ {
+ return libs.Values.GetEnumerator ();
+ }
+
+ public void Remove (string file)
+ {
+ file = Path.GetFullPath (file);
+ libs.Remove (file);
+ }
+ }
+
+ [XmlArray]
+ [XmlArrayItem (ElementName="LibraryInfo", Type=typeof(LibraryInfo))]
+ public LibraryInfoCollection Members = new LibraryInfoCollection ();
+
+ public LibraryCache () {}
+
+ public LibraryInfo this [string file] {
+ get {
+ file = Path.GetFullPath (file);
+ if (IsCurrent (file))
+ return Members [file];
+
+ RefreshFile (file);
+ return Members [file];
+ }
+ }
+
+ public bool IsCurrent (string file)
{
- cachePath = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), ".config");
- cachePath = Path.Combine (cachePath, "stetic");
- cachePath = Path.Combine (cachePath, "desc-cache");
+ file = Path.GetFullPath (file);
+ LibraryInfo info = Members [file];
+ return info != null && info.Timestamp == File.GetLastWriteTime (file).ToUniversalTime ();
}
-
- static XmlDocument GetIndex ()
+
+ AssemblyResolver resolver = new AssemblyResolver ();
+
+ EmbeddedResource GetResource (AssemblyDefinition asm, string name)
{
- if (indexDoc != null)
- return indexDoc;
-
- string index = Path.Combine (cachePath, "index.xml");
- if (!File.Exists (index))
- return null;
-
- try {
- XmlDocument doc = new XmlDocument ();
- doc.Load (index);
- return doc;
- } catch {
- return null;
+ foreach (Resource res in asm.MainModule.Resources) {
+ EmbeddedResource eres = res as EmbeddedResource;
+ if (eres != null && eres.Name == name)
+ return eres;
}
+ return null;
}
-
- static void SaveIndex (XmlDocument doc)
+
+ bool HasGtkReference (AssemblyDefinition assm, StringCollection visited)
{
- if (!Directory.Exists (cachePath))
- Directory.CreateDirectory (cachePath);
-
- // Purge the file list
-
- ArrayList todel = new ArrayList ();
- foreach (XmlElement elem in doc.DocumentElement.SelectNodes ("lib")) {
- string file = elem.GetAttribute ("file");
- if (!File.Exists (file)) {
- todel.Add (elem);
- string path = elem.GetAttribute ("cached");
- foreach (string cf in Directory.GetFiles (cachePath, path + "*")) {
- try {
- File.Delete (cf);
- } catch (Exception ex) {
- // Ignore exception
- Console.WriteLine (ex);
+ visited.Add (assm.Name.Name);
+
+ foreach (AssemblyNameReference nameRef in assm.MainModule.AssemblyReferences) {
+ if (visited.Contains (nameRef.Name))
+ continue;
+ else if (nameRef.Name == "gtk-sharp" || HasGtkReference (resolver.Resolve (nameRef), visited))
+ return true;
+ }
+
+ return false;
+ }
+
+ bool CheckForWidgets (string path)
+ {
+ try {
+ AssemblyDefinition adef = AssemblyFactory.GetAssembly (path);
+ if (GetResource (adef, "objects.xml") != null)
+ return true;
+
+ if (adef.Name.Name == "gtk-sharp")
+ return false; // Gtk is special-cased, so ignore it.
+
+ if (!HasGtkReference (adef, new StringCollection ()))
+ return false;
+
+ foreach (TypeDefinition type in adef.MainModule.Types) {
+ TypeReference tref = type.BaseType;
+ while (tref != null) {
+ if (tref.FullName == "Gtk.Window") {
+ break;
+ } else if (tref.FullName == "Gtk.Widget") {
+ return true;
}
+ tref = resolver.Resolve (tref).BaseType;
}
}
+ } catch {
}
-
- foreach (XmlElement elem in todel)
- doc.DocumentElement.RemoveChild (elem);
-
- string index = Path.Combine (cachePath, "index.xml");
- doc.Save (index);
- indexDoc = doc;
+ return false;
}
- public static string GetCachedFilePath (string assemblyPath)
+ void RefreshFile (string assembly)
{
- assemblyPath = Path.GetFullPath (assemblyPath);
-
- XmlDocument doc = GetIndex ();
- if (doc == null)
- return null;
-
- try {
- XmlElement elem = (XmlElement) doc.SelectSingleNode ("index/lib[@file='" + assemblyPath + "']");
- if (elem == null)
- return null;
-
- DateTime ts = XmlConvert.ToDateTime (elem.GetAttribute ("timestamp"), XmlDateTimeSerializationMode.Local);
- DateTime lastWrite = File.GetLastWriteTime (assemblyPath);
- if (ts != lastWrite)
- return null;
-
- return Path.Combine (cachePath, elem.GetAttribute ("cached"));
- }
- catch (Exception ex) {
- Console.WriteLine (ex);
- return null;
+ assembly = Path.GetFullPath (assembly);
+ LibraryInfo info = Members [assembly];
+ if (info == null) {
+ info = new LibraryInfo ();
+ info.File = assembly;
+ Members.Add (info);
}
+ info.Timestamp = File.GetLastWriteTime (assembly).ToUniversalTime ();
+ info.Guid = Guid.NewGuid ();
+ info.HasWidgets = CheckForWidgets (assembly);
+ Save ();
}
-
- public static string UpdateCachedFile (string assemblyPath)
+
+ void Save ()
{
- assemblyPath = Path.GetFullPath (assemblyPath);
-
- XmlDocument doc = GetIndex ();
- if (doc == null) {
- doc = new XmlDocument ();
- doc.AppendChild (doc.CreateElement ("index"));
+ if (!Directory.Exists (dir))
+ Directory.CreateDirectory (dir);
+
+ // remove any dead assemblies from the cache
+ StringCollection zombies = new StringCollection ();
+ foreach (LibraryInfo info in Members) {
+ if (File.Exists (info.File))
+ continue;
+ zombies.Add (info.File);
+ string zombie_dir = Path.Combine (dir, info.Guid.ToString ());
+ if (Directory.Exists (zombie_dir))
+ Directory.Delete (Path.Combine (dir, info.Guid.ToString ()), true);
}
- try {
- XmlElement elem = (XmlElement) doc.DocumentElement.SelectSingleNode ("lib[@file='" + assemblyPath + "']");
- if (elem == null) {
- elem = doc.CreateElement ("lib");
- elem.SetAttribute ("file", assemblyPath);
- doc.DocumentElement.AppendChild (elem);
- }
-
- DateTime lastWrite = File.GetLastWriteTime (assemblyPath);
- elem.SetAttribute ("timestamp", XmlConvert.ToString (lastWrite, XmlDateTimeSerializationMode.Local));
-
- string s = doc.DocumentElement.GetAttribute ("cindex");
- if (s.Length == 0) s = "0";
- else s = (int.Parse (s) + 1).ToString ();
- doc.DocumentElement.SetAttribute ("cindex", s);
-
- string cached = elem.GetAttribute ("cached");
- if (cached.Length == 0) {
- cached = Path.GetFileNameWithoutExtension (assemblyPath) + "_" + s;
- elem.SetAttribute ("cached", cached);
+ foreach (string file in zombies)
+ Members.Remove (file);
+
+ XmlSerializer serializer = new XmlSerializer (typeof (LibraryCache));
+ using (FileStream fs = File.Create (Path.Combine (dir, "index.xml")))
+ serializer.Serialize (fs, this);
+ }
+
+ public static LibraryCache Load ()
+ {
+ string index_path = Path.Combine (dir, "index.xml");
+ if (File.Exists (index_path)) {
+ try {
+ LibraryCache result;
+ XmlSerializer serializer = new XmlSerializer (typeof (LibraryCache));
+ using (XmlTextReader rdr = new XmlTextReader (index_path))
+ result = (LibraryCache) serializer.Deserialize (rdr);
+ return result;
+ } catch (Exception e) {
+ Console.WriteLine ("Cache index serialization failed " + e);
}
-
- SaveIndex (doc);
- return Path.Combine (cachePath, cached);
- }
- catch (Exception ex) {
- Console.WriteLine (ex);
- return null;
}
+
+ return new LibraryCache ();
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.