public string SaveProjectContent(ReflectionProjectContent pc) { try { // create cache directory, if necessary Directory.CreateDirectory(cacheDirectory); string assemblyFullName = pc.AssemblyFullName; int pos = assemblyFullName.IndexOf(','); string fileName = Path.Combine(cacheDirectory, assemblyFullName.Substring(0, pos) + "." + pc.AssemblyLocation.GetHashCode().ToString("x", CultureInfo.InvariantCulture) + ".dat"); AddFileNameToCacheIndex(Path.GetFileName(fileName), pc); using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { WriteProjectContent(pc, fs); } return(fileName); } catch (IOException) { return(null); } catch (UnauthorizedAccessException) { return(null); } }
public string SaveProjectContent(ReflectionProjectContent pc) { try { // create cache directory, if necessary Directory.CreateDirectory(cacheDirectory); string assemblyFullName = pc.AssemblyFullName; int pos = assemblyFullName.IndexOf(','); string fileName = Path.Combine(cacheDirectory, assemblyFullName.Substring(0, pos) + "." + pc.AssemblyLocation.GetHashCode().ToString("x", CultureInfo.InvariantCulture) + ".dat"); AddFileNameToCacheIndex(Path.GetFileName(fileName), pc); using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { WriteProjectContent(pc, fs); } return fileName; } catch (IOException) { return null; } catch (UnauthorizedAccessException) { return null; } }
/// <summary> /// Saves the project content to the stream. /// </summary> public static void WriteProjectContent(ReflectionProjectContent pc, Stream stream) { BinaryWriter writer = new BinaryWriter(stream); new ReadWriteHelper(writer).WriteProjectContent(pc); // do not close the stream }
public virtual IProjectContent GetProjectContentForReference(string itemInclude, string itemFileName) { lock (contents) { IProjectContent pc = GetExistingProjectContent(itemFileName); if (pc != null) { "Loaded Code Completion Reference (from Cache): {0}".debug(itemFileName); return(pc); } //LoggingService.Debug("Loading PC for " + itemInclude); string shortName = itemInclude; int pos = shortName.IndexOf(','); //if (pos > 0) // shortName = shortName.Substring(0, pos); #if DEBUG int time = Environment.TickCount; #endif try { pc = LoadProjectContent(itemInclude, itemFileName); } catch (Exception ex) { HostCallback.ShowAssemblyLoadErrorInternal(itemFileName, itemInclude, "Error loading assembly:\n" + ex.ToString()); ex.log(); } finally { #if DEBUG LoggingService.Debug(string.Format("Loaded {0} in {1}ms", itemInclude, Environment.TickCount - time)); #endif } if (pc != null) { ReflectionProjectContent reflectionProjectContent = pc as ReflectionProjectContent; if (reflectionProjectContent != null) { reflectionProjectContent.InitializeReferences(); if (reflectionProjectContent.AssemblyFullName != null) { contents[reflectionProjectContent.AssemblyFullName] = pc; } } contents[itemInclude] = pc; contents[itemFileName] = pc; "Loaded Code Completion Reference for: {0}".info(itemFileName); } return(pc); } }
public static string GetAssemblyLocation(ReflectionProjectContent rpc) { if (rpc == null) throw new ArgumentNullException("rpc"); // prefer GAC assemblies over reference assemblies: string assemblyLocation = FindAssemblyInNetGac(new DomAssemblyName(rpc.AssemblyFullName)); if (string.IsNullOrEmpty(assemblyLocation)) { // use file only if assembly isn't in GAC: assemblyLocation = rpc.AssemblyLocation; } return assemblyLocation; }
public virtual IProjectContent GetProjectContentForReference(string itemInclude, string itemFileName) { lock (contents) { IProjectContent pc = GetExistingProjectContent(itemFileName); if (pc != null) { return(pc); } LoggingService.Debug("Loading PC for " + itemInclude); string shortName = itemInclude; int pos = shortName.IndexOf(','); if (pos > 0) { shortName = shortName.Substring(0, pos); } #if DEBUG int time = Environment.TickCount; #endif try { pc = LoadProjectContent(itemInclude, itemFileName); } catch (BadImageFormatException ex) { HostCallback.ShowAssemblyLoadErrorInternal(itemFileName, itemInclude, ex.Message); } catch (Exception ex) { HostCallback.ShowError("Error loading assembly " + itemFileName, ex); } finally { #if DEBUG LoggingService.Debug(string.Format("Loaded {0} in {1}ms", itemInclude, Environment.TickCount - time)); #endif } if (pc != null) { ReflectionProjectContent reflectionProjectContent = pc as ReflectionProjectContent; if (reflectionProjectContent != null) { reflectionProjectContent.InitializeReferences(); if (reflectionProjectContent.AssemblyFullName != null) { contents[reflectionProjectContent.AssemblyFullName] = pc; } } contents[itemInclude] = pc; contents[itemFileName] = pc; } return(pc); } }
public ReflectionProjectContent ReadProjectContent(ProjectContentRegistry registry) { Int64 bigIntFileMagic = reader.ReadInt64(); if (bigIntFileMagic != FileMagic) { LoggingService.Warn("Read dom: wrong magic"); return(null); } if (reader.ReadInt16() != FileVersion) { LoggingService.Warn("Read dom: wrong version"); return(null); } string assemblyName = reader.ReadString(); string assemblyLocation = reader.ReadString(); long time = 0; try { time = File.GetLastWriteTimeUtc(assemblyLocation).ToFileTime(); } catch {} Int64 bigIntTime = reader.ReadInt64(); //if (bigIntTime != time) //{ // LoggingService.Warn("Read dom: assembly changed since cache was created"); // return null; //} DomAssemblyName[] referencedAssemblies = new DomAssemblyName[reader.ReadInt32()]; for (int i = 0; i < referencedAssemblies.Length; i++) { referencedAssemblies[i] = new DomAssemblyName(reader.ReadString()); } this.pc = new ReflectionProjectContent(assemblyName, assemblyLocation, referencedAssemblies, registry); if (ReadClasses()) { return(pc); } else { LoggingService.Warn("Read dom: error in file (invalid control mark)"); return(null); } }
public string SaveProjectContent(ReflectionProjectContent pc) { string assemblyFullName = pc.AssemblyFullName; int pos = assemblyFullName.IndexOf(','); string fileName = Path.Combine(cacheDirectory, assemblyFullName.Substring(0, pos) + "." + assemblyFullName.GetHashCode().ToString("x", CultureInfo.InvariantCulture) + "." + pc.AssemblyLocation.GetHashCode().ToString("x", CultureInfo.InvariantCulture) + ".dat"); AddFileNameToCacheIndex(Path.GetFileName(fileName), pc); using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { WriteProjectContent(pc, fs); } return fileName; }
public string SaveProjectContent(ReflectionProjectContent pc) { string assemblyFullName = pc.AssemblyFullName; int pos = assemblyFullName.IndexOf(','); string fileName = Path.Combine(cacheDirectory, assemblyFullName.Substring(0, pos) + "." + assemblyFullName.GetHashCode().ToString("x", CultureInfo.InvariantCulture) + "." + pc.AssemblyLocation.GetHashCode().ToString("x", CultureInfo.InvariantCulture) + ".dat"); AddFileNameToCacheIndex(Path.GetFileName(fileName), pc); using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { WriteProjectContent(pc, fs); } return(fileName); }
public void WriteProjectContent(ReflectionProjectContent pc) { this.pc = pc; writer.Write(FileMagic); writer.Write(FileVersion); writer.Write(pc.AssemblyFullName); writer.Write(pc.AssemblyLocation); long time = 0; try { time = File.GetLastWriteTimeUtc(pc.AssemblyLocation).ToFileTime(); } catch {} writer.Write(time); writer.Write(pc.ReferencedAssemblies.Length); foreach (AssemblyName name in pc.ReferencedAssemblies) { writer.Write(name.FullName); } WriteClasses(); }
public string LoadAndCreateDatabase(string fileName, string include, string cacheDirectory) { try { ReflectionProjectContent content = LoadProjectContent(fileName, include, new ProjectContentRegistry()); if (content == null) { return(null); } return(new DomPersistence(cacheDirectory, null).SaveProjectContent(content)); } catch (Exception ex) { if (ex is FileLoadException) { LoggingService.Info(ex); } else { LoggingService.Error(ex); } throw; } }
void AddFileNameToCacheIndex(string cacheFile, ReflectionProjectContent pc) { Dictionary <string, string> l = LoadCacheIndex(); l[pc.AssemblyLocation] = cacheFile; string txt = pc.AssemblyFullName; l[txt] = cacheFile; int pos = txt.LastIndexOf(','); do { txt = txt.Substring(0, pos); if (l.ContainsKey(txt)) { break; } l[txt] = cacheFile; pos = txt.LastIndexOf(','); } while (pos >= 0); SaveCacheIndex(l); cacheIndex = l; }
protected virtual IProjectContent LoadProjectContent(string itemInclude, string itemFileName) { string shortName = itemInclude; int pos = shortName.IndexOf(','); if (pos > 0) shortName = shortName.Substring(0, pos); try { Assembly assembly = File.Exists(shortName) ? Assembly.LoadFrom(shortName) : GetDefaultAssembly(shortName); ReflectionProjectContent pc = null; if (assembly != null) { if (persistence != null) { pc = persistence.LoadProjectContentByAssemblyName(assembly.FullName); } if (pc == null) { pc = new ReflectionProjectContent(assembly, this); if (persistence != null) { persistence.SaveProjectContent(pc); } } } else { // find real file name for cecil: if (File.Exists(itemFileName)) { if (persistence != null) { pc = persistence.LoadProjectContentByAssemblyName(itemFileName); } if (pc == null) { pc = CecilReader.LoadAssembly(itemFileName, this); if (persistence != null) { persistence.SaveProjectContent(pc); } } } else { DomAssemblyName asmName = GacInterop.FindBestMatchingAssemblyName(itemInclude); if (persistence != null && asmName != null) { //LoggingService.Debug("Looking up in DOM cache: " + asmName.FullName); pc = persistence.LoadProjectContentByAssemblyName(asmName.FullName); } if (pc == null && asmName != null) { string subPath = Path.Combine(asmName.ShortName, GetVersion__Token(asmName)); subPath = Path.Combine(subPath, asmName.ShortName + ".dll"); foreach (string dir in Directory.GetDirectories(GacInterop.GacRootPath, "GAC*")) { itemFileName = Path.Combine(dir, subPath); if (File.Exists(itemFileName)) { pc = CecilReader.LoadAssembly(itemFileName, this); if (persistence != null) { persistence.SaveProjectContent(pc); } break; } } } if (pc == null) { HostCallback.ShowAssemblyLoadErrorInternal(itemFileName, itemInclude, "Could not find assembly file."); } } } return pc; } catch (Exception ex) { PublicDI.log.ex(ex); return null; } }
public ReflectionProjectContent ReadProjectContent(ProjectContentRegistry registry) { if (reader.ReadInt64() != FileMagic) { LoggingService.Warn("Read dom: wrong magic"); return null; } if (reader.ReadInt16() != FileVersion) { LoggingService.Warn("Read dom: wrong version"); return null; } string assemblyName = reader.ReadString(); string assemblyLocation = reader.ReadString(); long time = 0; try { time = File.GetLastWriteTimeUtc(assemblyLocation).ToFileTime(); } catch {} if (reader.ReadInt64() != time) { LoggingService.Warn("Read dom: assembly changed since cache was created"); return null; } DomAssemblyName[] referencedAssemblies = new DomAssemblyName[reader.ReadInt32()]; for (int i = 0; i < referencedAssemblies.Length; i++) { referencedAssemblies[i] = new DomAssemblyName(reader.ReadString()); } this.pc = new ReflectionProjectContent(assemblyName, assemblyLocation, referencedAssemblies, registry); if (ReadClasses()) { return pc; } else { LoggingService.Warn("Read dom: error in file (invalid control mark)"); return null; } }
public void WriteProjectContent(ReflectionProjectContent pc) { this.pc = pc; writer.Write(FileMagic); writer.Write(FileVersion); writer.Write(pc.AssemblyFullName); writer.Write(pc.AssemblyLocation); long time = 0; try { time = File.GetLastWriteTimeUtc(pc.AssemblyLocation).ToFileTime(); } catch {} writer.Write(time); writer.Write(pc.ReferencedAssemblyNames.Count); foreach (DomAssemblyName name in pc.ReferencedAssemblyNames) { writer.Write(name.FullName); } WriteClasses(); }
void AddFileNameToCacheIndex(string cacheFile, ReflectionProjectContent pc) { Dictionary<string, string> l = LoadCacheIndex(); l[pc.AssemblyLocation] = cacheFile; string txt = pc.AssemblyFullName; l[txt] = cacheFile; int pos = txt.LastIndexOf(','); do { txt = txt.Substring(0, pos); if (l.ContainsKey(txt)) break; l[txt] = cacheFile; pos = txt.LastIndexOf(','); } while (pos >= 0); SaveCacheIndex(l); cacheIndex = l; }
/// <summary> /// Load a project content using Reflection in a separate AppDomain. /// This method first tries to load the assembly from the disk cache, if it exists there. /// If it does not exist in disk cache, it creates a new AppDomain, instanciates a ReflectionLoader in it /// and loads the assembly <paramref name="filename"/>. /// If the file does not exist, <paramref name="include"/> is loaded from GAC. /// </summary> public ReflectionProjectContent ReflectionLoadProjectContent(string filename, string include) { DomPersistence persistence; bool tempPersistence; if (this.persistence == null) { tempPersistence = true; persistence = new DomPersistence(Path.GetTempPath(), this); } else { // non-temp persistence tempPersistence = false; persistence = this.persistence; ReflectionProjectContent pc = persistence.LoadProjectContentByAssemblyName(filename); if (pc != null) { return(pc); } pc = persistence.LoadProjectContentByAssemblyName(include); if (pc != null) { return(pc); } } AppDomainSetup setup = new AppDomainSetup(); setup.DisallowCodeDownload = true; setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; AppDomain domain = AppDomain.CreateDomain("AssemblyLoadingDomain", AppDomain.CurrentDomain.Evidence, setup); string database; try { object o = domain.CreateInstanceAndUnwrap(typeof(ReflectionLoader).Assembly.FullName, typeof(ReflectionLoader).FullName); ReflectionLoader loader = (ReflectionLoader)o; database = loader.LoadAndCreateDatabase(filename, include, persistence.CacheDirectory); } catch (FileLoadException e) { database = null; HostCallback.ShowAssemblyLoadErrorInternal(filename, include, e.Message); } catch (Exception e) { database = null; HostCallback.ShowError("Error loading code-completion information for " + include + " from " + filename, e); } finally { AppDomain.Unload(domain); } if (database == null) { LoggingService.Debug("AppDomain finished but returned null..."); return(null); } else { LoggingService.Debug("AppDomain finished, loading cache..."); try { return(persistence.LoadProjectContent(database)); } finally { if (tempPersistence) { try { File.Delete(database); } catch {} } } } }
protected virtual IProjectContent LoadProjectContent(string itemInclude, string itemFileName) { string shortName = itemInclude; int pos = shortName.IndexOf(','); if (pos > 0) { shortName = shortName.Substring(0, pos); } Assembly assembly = GetDefaultAssembly(shortName); ReflectionProjectContent pc = null; if (assembly != null) { if (persistence != null) { pc = persistence.LoadProjectContentByAssemblyName(assembly.FullName); } if (pc == null) { pc = new ReflectionProjectContent(assembly, this); if (persistence != null) { persistence.SaveProjectContent(pc); } } } else { // find real file name for cecil: if (File.Exists(itemFileName)) { if (persistence != null) { pc = persistence.LoadProjectContentByAssemblyName(itemFileName); } if (pc == null) { pc = CecilReader.LoadAssembly(itemFileName, this); if (persistence != null) { persistence.SaveProjectContent(pc); } } } else { DomAssemblyName asmName = GacInterop.FindBestMatchingAssemblyName(itemInclude); if (persistence != null && asmName != null) { //LoggingService.Debug("Looking up in DOM cache: " + asmName.FullName); pc = persistence.LoadProjectContentByAssemblyName(asmName.FullName); } if (pc == null && asmName != null) { string subPath = Path.Combine(asmName.ShortName, GetVersion__Token(asmName)); subPath = Path.Combine(subPath, asmName.ShortName + ".dll"); foreach (string dir in Directory.GetDirectories(GacInterop.GacRootPath, "GAC*")) { itemFileName = Path.Combine(dir, subPath); if (File.Exists(itemFileName)) { pc = CecilReader.LoadAssembly(itemFileName, this); if (persistence != null) { persistence.SaveProjectContent(pc); } break; } } } if (pc == null) { HostCallback.ShowAssemblyLoadErrorInternal(itemFileName, itemInclude, "Could not find assembly file."); } } } return(pc); }
TextTemplatingReflectionProjectContent(ReflectionProjectContent projectContent) { if (projectContent != null) { AssemblyLocation = projectContent.AssemblyLocation; } }