/// <summary> /// Reads an instance from a file. /// </summary> /// <param name="instancePath">The path to either the instance or a layout configuration.</param> /// <param name="settingConfigPath">The path to the file specifying the setting.</param> /// <param name="controlConfigPath">The path to the file supplying the configuration for all controlling mechanisms.</param> /// <param name="overrideVisualizationAttached">Indicates whether a visualization shall be attached.</param> /// <param name="visualizationOnly">If this is enabled most of the initialization will be skipped.</param> /// <param name="logAction">A action that will be used for logging some lines.</param> /// <returns></returns> public static Instance ReadInstance( string instancePath, string settingConfigPath, string controlConfigPath, bool overrideVisualizationAttached = false, bool visualizationOnly = false, Action <string> logAction = null) { // Test for layout / instance file XmlDocument doc = new XmlDocument(); doc.Load(instancePath); string rootName = doc.SelectSingleNode("/*").Name; bool layoutConfigurationGiven = false; if (rootName == nameof(Instance)) { layoutConfigurationGiven = false; } else if (rootName == nameof(LayoutConfiguration)) { layoutConfigurationGiven = true; } else { throw new ArgumentException("No valid instance or layout file given!"); } logAction?.Invoke(rootName + " recognized!"); // --> Read configurations SettingConfiguration settingConfig = null; ControlConfiguration controlConfig = null; LayoutConfiguration layoutConfig = null; if (!visualizationOnly) { // Read the setting configuration logAction?.Invoke("Parsing setting config ..."); using (StreamReader sr = new StreamReader(settingConfigPath)) { // Deserialize the xml-file settingConfig = (SettingConfiguration)_settingConfigSerializer.Deserialize(sr); // If it contains a path to a word-file that is not leading to a wordlist file try the default wordlist locations if (settingConfig.InventoryConfiguration.ColoredWordConfiguration != null && !File.Exists(settingConfig.InventoryConfiguration.ColoredWordConfiguration.WordFile)) { settingConfig.InventoryConfiguration.ColoredWordConfiguration.WordFile = IOHelper.FindResourceFile(settingConfig.InventoryConfiguration.ColoredWordConfiguration.WordFile, instancePath); } // If it contains a path to an order-file that is not leading to a orderlist file try the default orderlist locations if (settingConfig.InventoryConfiguration.FixedInventoryConfiguration != null && !string.IsNullOrWhiteSpace(settingConfig.InventoryConfiguration.FixedInventoryConfiguration.OrderFile) && !File.Exists(settingConfig.InventoryConfiguration.FixedInventoryConfiguration.OrderFile)) { settingConfig.InventoryConfiguration.FixedInventoryConfiguration.OrderFile = IOHelper.FindResourceFile(settingConfig.InventoryConfiguration.FixedInventoryConfiguration.OrderFile, instancePath); } // If it contains a path to an simple-item-file that is not leading to a generator config file try the default locations if (settingConfig.InventoryConfiguration.SimpleItemConfiguration != null && !string.IsNullOrWhiteSpace(settingConfig.InventoryConfiguration.SimpleItemConfiguration.GeneratorConfigFile) && !File.Exists(settingConfig.InventoryConfiguration.SimpleItemConfiguration.GeneratorConfigFile)) { settingConfig.InventoryConfiguration.SimpleItemConfiguration.GeneratorConfigFile = IOHelper.FindResourceFile(settingConfig.InventoryConfiguration.SimpleItemConfiguration.GeneratorConfigFile, instancePath); } } // Read the control configuration logAction?.Invoke("Parsing control config ..."); using (StreamReader sr = new StreamReader(controlConfigPath)) // Deserialize the xml-file controlConfig = (ControlConfiguration)_controlConfigSerializer.Deserialize(sr); } // --> Init or generate instance Instance instance = null; if (layoutConfigurationGiven) { // Read the layout configuration logAction?.Invoke("Parsing layout config ..."); using (StreamReader sr = new StreamReader(instancePath)) // Deserialize the xml-file layoutConfig = (LayoutConfiguration)_layoutConfigSerializer.Deserialize(sr); // Apply override config, if available if (settingConfig != null && settingConfig.OverrideConfig != null) { layoutConfig.ApplyOverrideConfig(settingConfig.OverrideConfig); } // Generate instance logAction?.Invoke("Generating instance..."); instance = InstanceGenerator.GenerateLayout(layoutConfig, new RandomizerSimple(0), settingConfig, controlConfig, logAction); } else { // Init the instance object instance = new Instance(); } // Check whether the config is required if (!visualizationOnly) { // Submit config first to the instance object instance.SettingConfig = settingConfig; instance.ControllerConfig = controlConfig; } else { // Add default config (none required though) instance.SettingConfig = new SettingConfiguration(); instance.SettingConfig.VisualizationOnly = true; instance.ControllerConfig = new ControlConfiguration(); } // If a visualization is already present set it to true instance.SettingConfig.VisualizationAttached = overrideVisualizationAttached; // --> Parse the instance from a file, if no layout was given but a specific instance if (!layoutConfigurationGiven) { // Read the instance logAction?.Invoke("Parsing instance ..."); using (StreamReader sr = new StreamReader(instancePath)) { // Deserialize the xml-file DTOInstance dtoInstance = (DTOInstance)_instanceSerializer.Deserialize(sr); // Submit the data to an instance object dtoInstance.Submit(instance); } } // Return it return(instance); }