public void readFile(string sfilename,string sObject) { if (File.Exists(Directory.GetCurrentDirectory() + slash+ "XML"+ slash + sfilename)) { var path = Directory.GetCurrentDirectory() + slash+"XML"+slash + sfilename; FileStream fs = new FileStream(path, FileMode.Open); System.Xml.Serialization.XmlSerializer xmlserializer; XmlReader reader; switch (sObject) { case "user": xmlserializer = new System.Xml.Serialization.XmlSerializer(lUsers.GetType()); reader = XmlReader.Create(fs); lUsers = (List<DataFiles.User>)xmlserializer.Deserialize(reader); fs.Close(); break; case "streams": try { xmlserializer = new System.Xml.Serialization.XmlSerializer(lStreams.GetType()); reader = XmlReader.Create(fs); lStreams = (List<DataFiles.internalStream>)xmlserializer.Deserialize(reader); fs.Close(); } catch(Exception ex) { Console.WriteLine(ex.ToString()); } break; case "userpicks": xmlserializer = new System.Xml.Serialization.XmlSerializer(lUserPickLists.GetType()); reader = XmlReader.Create(fs); lUserPickLists = (List<DataFiles.UserPickedList>)xmlserializer.Deserialize(reader); fs.Close(); break; case "counters": xmlserializer = new System.Xml.Serialization.XmlSerializer(lCounters.GetType()); reader = XmlReader.Create(fs); lCounters = (List<DataFiles.Counter>)xmlserializer.Deserialize(reader); fs.Close(); break; } } else { switch (sObject) { case "user": break; case "streams": lStreams = new List<DataFiles.internalStream>(); if(Streams!= null) { foreach (var stream in Streams.Element("Streams").Elements("Stream")) { string twitchrelay = ""; if(stream.Attribute("twitchchat") != null) { twitchrelay = stream.Attribute("twitchchat").Value; } bool twoway = false; if (stream.Attribute("twoway") != null) { if(stream.Attribute("twoway").Value == "1") { twoway = true; } } string targetchannel = ""; if (stream.Attribute("targetchannel") != null) { targetchannel = stream.Attribute("targetchannel").Value; } DataFiles.internalStream newstream = new DataFiles.internalStream(stream.Attribute("Channel").Value, twitchrelay, twoway, targetchannel); lStreams.Add(newstream); } } var path = Directory.GetCurrentDirectory() + slash+"XML"+ slash + sfilename; System.Xml.Serialization.XmlSerializer xmlserializer = new System.Xml.Serialization.XmlSerializer(lStreams.GetType()); System.IO.FileStream file = System.IO.File.Create(path); xmlserializer.Serialize(file, lStreams); file.Close(); break; case "userpicks": path = Directory.GetCurrentDirectory() + slash + "XML" + slash + "Userpicksv2.xml"; xmlserializer = new System.Xml.Serialization.XmlSerializer(lUserPickLists.GetType()); file = System.IO.File.Create(path); xmlserializer.Serialize(file, lUserPickLists); file.Close(); break; case "counters": IEnumerable<XElement> childlist = Counters.Root.Elements(); foreach(var child in childlist) { DataFiles.Counter newcounter = new DataFiles.Counter(); newcounter.Count = Int32.Parse(child.Attribute("Value").Value); newcounter.sName = child.Attribute("Name").Value; lCounters.Add(newcounter); } path = Directory.GetCurrentDirectory() + slash + "XML" + slash + sfilename; xmlserializer = new System.Xml.Serialization.XmlSerializer(lCounters.GetType()); file = System.IO.File.Create(path); xmlserializer.Serialize(file, lCounters); file.Close(); break; } } }
public string Counter(string counter,bool reset = false, bool read = false,int customincrease = 0) { int count = 0; IEnumerable<DataFiles.Counter> childlist = lCounters.Where(x => x.sName.ToLower() == counter.ToLower()); if (childlist.Count() > 0) { foreach (var _counter in childlist) { if(read) { count = _counter.Count; } else { if (reset) { _counter.Count = 0; } else { count = _counter.Count; if(customincrease > 0) { count += customincrease; } else { count++; } _counter.Count = count; } } } } else { try { if(customincrease>0) { DataFiles.Counter newcounter = new DataFiles.Counter(); newcounter.sName = counter; newcounter.Count = customincrease; count = customincrease; } else { DataFiles.Counter newcounter = new DataFiles.Counter(); newcounter.sName = counter; newcounter.Count = 1; count = 1; } } catch(Exception ex) { Console.WriteLine(ex.ToString()); } } saveFile("counters"); if (reset) { return "The Counter " + counter + " has been reset and is at " + count; } return "The Counter " + counter + " is at " + count; }