public void Initialize(string fileName) { try { HostNatvisProject.FindNatvisInSolution((s) => LoadFile(s)); } catch (FileNotFoundException) { // failed to find the VS Service } if (!string.IsNullOrEmpty(fileName)) { if (!Path.IsPathRooted(fileName)) { string globalVisualizersDirectory = _process.Engine.GetMetric("GlobalVisualizersDirectory") as string; string globalNatVisPath = null; if (!string.IsNullOrEmpty(globalVisualizersDirectory) && !string.IsNullOrEmpty(fileName)) { globalNatVisPath = Path.Combine(globalVisualizersDirectory, fileName); } // For local launch, try and load natvis next to the target exe if it exists and if // the exe is rooted. If the file doesn't exist, and also doesn't exist in the global folder fail. if (_process.LaunchOptions is LocalLaunchOptions) { string exePath = (_process.LaunchOptions as LocalLaunchOptions).ExePath; if (Path.IsPathRooted(exePath)) { string localNatvisPath = Path.Combine(Path.GetDirectoryName(exePath), fileName); if (File.Exists(localNatvisPath)) { LoadFile(localNatvisPath); return; } else if (globalNatVisPath == null || !File.Exists(globalNatVisPath)) { // Neither local or global path exists, report an error. _process.WriteOutput(String.Format(CultureInfo.CurrentCulture, ResourceStrings.FileNotFound, localNatvisPath)); return; } } } // Local wasn't supported or the file didn't exist. Try and load from globally registered visualizer directory if local didn't work // or wasn't supported by the launch options if (!string.IsNullOrEmpty(globalNatVisPath)) { LoadFile(globalNatVisPath); } } else { // Full path to the natvis file.. Just try the load LoadFile(fileName); } } }
private bool LoadFile(string path) { try { XmlSerializer serializer = new XmlSerializer(typeof(AutoVisualizer)); if (!File.Exists(path)) { _process.WriteOutput(String.Format(CultureInfo.CurrentCulture, ResourceStrings.FileNotFound, path)); return(false); } XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; settings.IgnoreProcessingInstructions = true; settings.IgnoreWhitespace = true; settings.XmlResolver = null; using (var stream = new System.IO.FileStream(path, FileMode.Open, FileAccess.Read)) using (var reader = XmlReader.Create(stream, settings)) { AutoVisualizer autoVis = null; autoVis = serializer.Deserialize(reader) as AutoVisualizer; if (autoVis != null) { FileInfo f = new FileInfo(autoVis); if (autoVis.Items == null) { return(false); } foreach (var o in autoVis.Items) { if (o is VisualizerType) { VisualizerType v = (VisualizerType)o; TypeName t = TypeName.Parse(v.Name); if (t != null) { lock (_typeVisualizers) { f.Visualizers.Add(new TypeInfo(t, v)); } } // add an entry for each alternative name too if (v.AlternativeType != null) { foreach (var a in v.AlternativeType) { t = TypeName.Parse(a.Name); if (t != null) { lock (_typeVisualizers) { f.Visualizers.Add(new TypeInfo(t, v)); } } } } } else if (o is AliasType) { AliasType a = (AliasType)o; TypeName t = TypeName.Parse(a.Name); if (t != null) { lock (_typeVisualizers) { f.Aliases.Add(new AliasInfo(t, a)); } } } } _typeVisualizers.Add(f); } return(autoVis != null); } } catch (Exception exception) { // don't allow natvis failures to stop debugging _process.WriteOutput(String.Format(CultureInfo.CurrentCulture, ResourceStrings.ErrorReadingFile, exception.Message, path)); return(false); } }
private bool LoadFile(string path) { try { XmlSerializer serializer = new XmlSerializer(typeof(AutoVisualizer)); if (!File.Exists(path)) { _process.WriteOutput(String.Format(CultureInfo.CurrentCulture, ResourceStrings.FileNotFound, path)); return(false); } XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; settings.IgnoreProcessingInstructions = true; settings.IgnoreWhitespace = true; // set XmlResolver via reflection, if it exists. This is required for desktop CLR, as otherwise the XML reader may // attempt to hit untrusted external resources. var xmlResolverProperty = settings.GetType().GetProperty("XmlResolver", BindingFlags.Public | BindingFlags.Instance); xmlResolverProperty?.SetValue(settings, null); using (var stream = new System.IO.FileStream(path, FileMode.Open, FileAccess.Read)) using (var reader = XmlReader.Create(stream, settings)) { AutoVisualizer autoVis = null; autoVis = serializer.Deserialize(reader) as AutoVisualizer; if (autoVis != null) { FileInfo f = new FileInfo(autoVis); if (autoVis.Items == null) { return(false); } foreach (var o in autoVis.Items) { if (o is VisualizerType) { VisualizerType v = (VisualizerType)o; TypeName t = TypeName.Parse(v.Name); if (t != null) { lock (_typeVisualizers) { f.Visualizers.Add(new TypeInfo(t, v)); } } // add an entry for each alternative name too if (v.AlternativeType != null) { foreach (var a in v.AlternativeType) { t = TypeName.Parse(a.Name); if (t != null) { lock (_typeVisualizers) { f.Visualizers.Add(new TypeInfo(t, v)); } } } } } else if (o is AliasType) { AliasType a = (AliasType)o; TypeName t = TypeName.Parse(a.Name); if (t != null) { lock (_typeVisualizers) { f.Aliases.Add(new AliasInfo(t, a)); } } } } _typeVisualizers.Add(f); } return(autoVis != null); } } catch (Exception exception) { // don't allow natvis failures to stop debugging _process.WriteOutput(String.Format(CultureInfo.CurrentCulture, ResourceStrings.ErrorReadingFile, exception.Message, path)); return(false); } }