Changeset 750
- Timestamp:
- 11/13/08 16:50:18 (16 years ago)
- Location:
- trunk/sources
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Core/PersistenceManager.cs
r687 r750 26 26 using System.IO; 27 27 using System.IO.Compression; 28 using HeuristicLab.PluginInfrastructure; 28 29 29 30 namespace HeuristicLab.Core { … … 40 41 } 41 42 public static XmlNode Persist(string name, IStorable instance, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { 42 if 43 if(persistedObjects.ContainsKey(instance.Guid)) { 43 44 XmlNode node = document.CreateNode(XmlNodeType.Element, name, null); 44 45 XmlAttribute guidAttribute = document.CreateAttribute("GUID"); … … 52 53 } 53 54 } 54 public static IStorable Restore(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {55 public static IStorable Restore(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) { 55 56 Guid guid = new Guid(node.Attributes["GUID"].Value); 56 if 57 if(restoredObjects.ContainsKey(guid)) { 57 58 return restoredObjects[guid]; 58 59 } else { … … 72 73 public static void Save(IStorable instance, Stream stream) { 73 74 XmlDocument document = PersistenceManager.CreateXmlDocument(); 74 75 document.AppendChild(Persist(instance, document, new Dictionary<Guid, IStorable>())); 75 Dictionary<Guid, IStorable> dictionary = new Dictionary<Guid, IStorable>(); 76 XmlNode rootNode = document.CreateElement("Root"); 77 document.AppendChild(rootNode); 78 XmlNode necessaryPluginsNode = document.CreateElement("NecessaryPlugins"); 79 rootNode.AppendChild(necessaryPluginsNode); 80 rootNode.AppendChild(Persist(instance, document, dictionary)); 81 // determine the list of necessary plugins for this document 82 DiscoveryService service = new DiscoveryService(); 83 List<PluginInfo> plugins = new List<PluginInfo>(); 84 foreach(IStorable storeable in dictionary.Values) { 85 PluginInfo pluginInfo = service.GetDeclaringPlugin(storeable.GetType()); 86 if(!plugins.Contains(pluginInfo)) plugins.Add(pluginInfo); 87 } 88 foreach(PluginInfo uniquePlugin in plugins) { 89 XmlNode necessaryPluginNode = document.CreateElement("Plugin"); 90 XmlAttribute nameAttr = document.CreateAttribute("Name"); 91 nameAttr.Value = uniquePlugin.Name; 92 XmlAttribute versionAttr = document.CreateAttribute("Version"); 93 versionAttr.Value = uniquePlugin.Version.ToString(); 94 necessaryPluginNode.Attributes.Append(nameAttr); 95 necessaryPluginNode.Attributes.Append(versionAttr); 96 necessaryPluginsNode.AppendChild(necessaryPluginNode); 97 } 76 98 document.Save(stream); 77 99 } … … 86 108 XmlDocument doc = new XmlDocument(); 87 109 doc.Load(stream); 88 return PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>()); 110 XmlNode rootNode = doc.ChildNodes[1]; 111 if(rootNode.Name == "Root" && rootNode.ChildNodes.Count == 2) { 112 // load documents that have a list of necessary plugins at the top 113 return PersistenceManager.Restore(rootNode.ChildNodes[1], new Dictionary<Guid, IStorable>()); 114 } else { 115 // compatibility to load documents without list of necessary plugins 116 return PersistenceManager.Restore(rootNode, new Dictionary<Guid, IStorable>()); 117 } 89 118 } 90 119 … … 111 140 builder.Append(type.Name); 112 141 Type[] args = type.GetGenericArguments(); 113 if 142 if(args.Length > 0) { 114 143 builder.Append("[["); 115 144 builder.Append(BuildTypeString(args[0])); 116 145 builder.Append("]"); 117 for 146 for(int i = 1; i < args.Length; i++) { 118 147 builder.Append(",["); 119 148 builder.Append(BuildTypeString(args[i])); -
trunk/sources/HeuristicLab.PluginInfrastructure/DiscoveryService.cs
r29 r750 117 117 return types.ToArray(); 118 118 } 119 120 public PluginInfo GetDeclaringPlugin(Type type) { 121 IPlugin[] plugins = GetInstances<IPlugin>(type.Assembly); 122 if(plugins.Length != 1) return null; 123 IPlugin plugin = plugins[0]; 124 foreach(PluginInfo info in PluginManager.Manager.LoadedPlugins) { 125 if(info.Name == plugin.Name && info.Version == plugin.Version) return info; 126 } 127 return null; 128 } 119 129 } 120 130 }
Note: See TracChangeset
for help on using the changeset viewer.