public void SaveSetting(string settingName, string value) { if (File.Exists(m_pathToSettings) == false) { FactorioXmlHelper.CreateXml(m_pathToSettings, XmlMainElement); } XDocument settings = XDocument.Load(m_pathToSettings); settings.Element(settingName).FirstAttribute.SetValue(value); }
/// <summary> /// Create new <see cref="FactorioItem"/> from data in the XElement /// </summary> /// <param name="xmlData">Xml data for the item</param> /// <returns>New <see cref="FactorioItem"/></returns> public static FactorioItem GetFactorioItemFromXmlData(XElement xmlData, List <FactorioItem> knownItems, List <FactorioItem> unknownItems) { FactorioItem item; try { int id = Convert.ToInt32(xmlData.Attribute(XmlItemAttributeId).Value); if (unknownItems.Exists(i => i.Id == id)) { item = unknownItems.Find(i => i.Id == id); item.Name = xmlData.Attribute(XmlItemAttributeName).Value; item.CraftingOutput = Convert.ToInt32(xmlData.Attribute(XmlItemAttributeOutput).Value); item.CraftingTime = Convert.ToDouble(xmlData.Attribute(XmlItemAttributeTime).Value); item.DefaultCraftingType = (CraftingType)Enum.Parse(typeof(CraftingType), xmlData.Attribute(XmlItemAttributeCraftingStation).Value); item.ImagePath = xmlData.Attribute(XmlItemAttributePicture).Value; unknownItems.Remove(item); } else { item = new FactorioItem(id) { Name = xmlData.Attribute(XmlItemAttributeName).Value, CraftingOutput = Convert.ToInt32(xmlData.Attribute(XmlItemAttributeOutput).Value), CraftingTime = Convert.ToDouble(xmlData.Attribute(XmlItemAttributeTime).Value), DefaultCraftingType = (CraftingType)Enum.Parse(typeof(CraftingType), xmlData.Attribute(XmlItemAttributeCraftingStation).Value), }; if (xmlData.Attribute(XmlItemAttributePicture) != null) { item.ImagePath = xmlData.Attribute(XmlItemAttributePicture).Value; } } if (FactorioXmlHelper.IsImagePathValid(item.ImagePath) == false) { item.ImagePath = String.Empty; } } catch (Exception) { throw; } return(item); }
public string LoadSetting(string settingName) { if (File.Exists(m_pathToSettings) == false) { FactorioXmlHelper.CreateXml(m_pathToSettings, XmlMainElement); } XDocument settings = XDocument.Load(m_pathToSettings); if (settings.Element(settingName).FirstAttribute == null) { FactorioXmlHelper.AddAttribute(m_pathToSettings, settingName); } return(settings.Element(settingName).FirstAttribute.ToString()); }
public ObservableCollection <FactorioItem> ReadItems(string path) { // Contains all items that are currently known var knownItems = new List <FactorioItem>(); // Contains items that are found in recipes but aren't loaded yet var unknownItems = new List <FactorioItem>(); try { // Check if file exists, create one if it doesn't if (!File.Exists(path)) { Directory.CreateDirectory(Path.GetDirectoryName(path)); FactorioXmlHelper.CreateXml(path, XmlMainElement); } XDocument itemsFile = XDocument.Load(path); foreach (var xmlItemData in itemsFile.Descendants(XmlItemElement)) { FactorioItem newItem = GetFactorioItemFromXmlData(xmlItemData, knownItems, unknownItems); knownItems.Add(newItem); TryAddRecipeData(newItem, xmlItemData, knownItems, unknownItems); } } catch (FactorioException) { // pass through throw; } catch (Exception ex) { // create a new exception to add the event code throw new FactorioException(DiagnosticEvents.DalXmlRead, "An error occurred while reading a xml file with the message: " + ex.Message, ex); } return(new ObservableCollection <FactorioItem>(knownItems)); }