/// <summary> /// Loads a component description from the stream and adds it to the component descriptions store. /// </summary> /// <param name="stream">The stream to load from.</param> /// <param name="type">The type to find within the description.</param> /// <returns>A configuration with the loaded component description if it was available, null if it could not be loaded.</returns> private static ComponentIdentifier LoadDescription(EmbedComponentData data, IOComponentType type) { if (data.ContentType == IO.CDDX.ContentTypeNames.BinaryComponent) { // Binary component var reader = new CircuitDiagram.IO.Descriptions.BinaryDescriptionReader(); if (reader.Read(data.Stream)) { var descriptions = reader.ComponentDescriptions; if (descriptions.Count > 0) { return(FindIdentifier(type, descriptions[0])); } else { return(null); } } else { // Load failed return(null); } } else if (data.ContentType == "application/xml") { // XML component XmlLoader loader = new XmlLoader(); loader.Load(data.Stream); if (loader.LoadErrors.Count() == 0) { var descriptions = loader.GetDescriptions(); if (descriptions.Length > 0) { return(FindIdentifier(type, descriptions[0])); } else { return(null); } } else { // Load failed return(null); } } else { // Unknown type return(null); } }
private static bool CompileComponent(string inputPath, CompileOptions compileOptions) { List <ComponentDescription> componentDescriptions = new List <ComponentDescription>(); List <BinaryResource> binaryResources = new List <BinaryResource>(); XmlLoader loader = new XmlLoader(); loader.Load(new FileStream(inputPath, FileMode.Open)); if (loader.LoadErrors.Count(e => e.Category == LoadErrorCategory.Error) > 0) { foreach (var error in loader.LoadErrors) { Console.WriteLine(error.ToString()); } return(false); } ComponentDescription description = loader.GetDescriptions()[0]; description.ID = "C0"; componentDescriptions.Add(description); SetIcons(compileOptions, description); string output = compileOptions.Output; if (!Path.HasExtension(output)) { if (!Directory.Exists(output)) { Directory.CreateDirectory(output); } output += "\\" + description.ComponentName.ToLowerInvariant() + ".cdcom"; } FileStream stream = new FileStream(output, FileMode.Create, FileAccess.Write); X509Certificate2 certificate = SelectCertificate(compileOptions); CircuitDiagram.IO.BinaryWriter.BinaryWriterSettings settings = new CircuitDiagram.IO.BinaryWriter.BinaryWriterSettings(); settings.Certificate = certificate; CircuitDiagram.IO.BinaryWriter writer = new CircuitDiagram.IO.BinaryWriter(stream, settings); writer.Descriptions.AddRange(componentDescriptions); writer.Resources.AddRange(binaryResources); writer.Write(); stream.Flush(); return(true); }
static CompileResult Run(string inputFile, IResourceProvider resourceProvider, PreviewGenerationOptions previewOptions, IDictionary <IOutputGenerator, string> formats) { Log.LogInformation(inputFile); var loader = new XmlLoader(); using (var fs = File.OpenRead(inputFile)) { loader.Load(fs); if (loader.LoadErrors.Any()) { foreach (var error in loader.LoadErrors) { switch (error.Category) { case LoadErrorCategory.Error: Log.LogError(error.Message); break; } } if (loader.LoadErrors.Any(x => x.Category == LoadErrorCategory.Error)) { Environment.Exit(1); } } var description = loader.GetDescriptions()[0]; var outputs = Generate(fs, description, Path.GetFileNameWithoutExtension(inputFile), resourceProvider, formats, previewOptions); return(new CompileResult(description.Metadata.Author, description.ComponentName, description.Metadata.GUID, true, description.Metadata.AdditionalInformation, inputFile, description.Metadata.Entries.ToImmutableDictionary(), outputs.ToImmutableDictionary())); } }
public void Run(CompileContext context) { XmlLoader loader = new XmlLoader(); loader.Load(context.Input); context.Errors.AddRange(loader.LoadErrors.Select(e => new CompileError(e))); if (loader.LoadErrors.Count(e => e.Category == LoadErrorCategory.Error) > 0) { return; } var description = loader.GetDescriptions()[0]; // The component XML format doesn't provide an ID, so make one now description.ID = "C0"; context.Description = description; }
private static bool LoadXmlComponents() { bool conflictingGuid = false; var xmlLoader = new XmlLoader(); foreach (string location in componentDirectories) { foreach (string file in System.IO.Directory.GetFiles(location, "*.xml", SearchOption.TopDirectoryOnly)) { using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { xmlLoader.Load(fs); if (xmlLoader.LoadErrors.Count() == 0) { ComponentDescription description = xmlLoader.GetDescriptions()[0]; description.Metadata.Location = ComponentDescriptionMetadata.LocationType.Installed; description.Source = new ComponentDescriptionSource(file, new System.Collections.ObjectModel.ReadOnlyCollection <ComponentDescription>(new ComponentDescription[] { description })); // Check if duplicate GUID if (!conflictingGuid && description.Metadata.GUID != Guid.Empty) { foreach (ComponentDescription compareDescription in ComponentHelper.ComponentDescriptions) { if (compareDescription.Metadata.GUID == description.Metadata.GUID) { conflictingGuid = true; } } } ComponentHelper.AddDescription(description); if (ComponentHelper.WireDescription == null && description.ComponentName.ToLowerInvariant() == "wire" && description.Metadata.GUID == new Guid("6353882b-5208-4f88-a83b-2271cc82b94f")) { ComponentHelper.WireDescription = description; } } } } } return(conflictingGuid); }
private void TestComponent(string path) { using (var xml = File.OpenRead(path)) { var loader = new XmlLoader(); loader.Load(xml); // Make sure there were no errors Assert.AreEqual(0, loader.LoadErrors.Count()); var testDescription = loader.GetDescriptions()[0]; // Check component data AssertMetadata(testDescription); AssertProperties(testDescription); AssertFlags(testDescription); AssertConnections(testDescription); AssertRender(testDescription); } }
private void LoadXmlComponents(string[] directories) { var xmlLoader = new XmlLoader(); foreach (string location in directories) { foreach (string file in Directory.GetFiles(location, "*.xml", SearchOption.TopDirectoryOnly)) { using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { xmlLoader.Load(fs); if (!xmlLoader.LoadErrors.Any()) { ComponentDescription description = xmlLoader.GetDescriptions()[0]; description.Metadata.Location = ComponentDescriptionMetadata.LocationType.Installed; description.Source = new ComponentDescriptionSource(file, new ReadOnlyCollection <ComponentDescription>(new[] { description })); var type = GetTypeFromDescription(description); internalLookup.AddDescription(type, description); } } } } }
private void TestComponent(string resource) { var assembly = Assembly.GetExecutingAssembly(); var resources = assembly.GetManifestResourceNames(); var resourceName = resources.First(r => r.EndsWith(resource)); using (var xml = assembly.GetManifestResourceStream(resourceName)) { var loader = new XmlLoader(); loader.Load(xml); // Make sure there were no errors Assert.AreEqual(0, loader.LoadErrors.Count()); var testDescription = loader.GetDescriptions()[0]; // Check component data AssertMetadata(testDescription); AssertProperties(testDescription); AssertFlags(testDescription); AssertConnections(testDescription); AssertRender(testDescription); } }