/// <summary> /// Parses a stream over a rulings file into a list of cards. /// Does not close the stream! /// </summary> /// <param name="sr"> /// A <see cref="StreamReader"/> /// </param> /// <returns> /// A List of CardRulings /// </returns> private static List<CardRuling> ParseRulings(StreamReader inputStream) { Stopwatch sw = new Stopwatch(); Stopwatch swTotal = new Stopwatch(); int index = 0; long readLineCount = 0; long trySetCount = 0; long parseTypesCount = 0; long addRulesTextCount = 0; List<CardRuling> parsedRulings = new List<CardRuling>(); List<string> lineBuf = new List<string>(10); string line; sw.Start(); swTotal.Start(); while((line = inputStream.ReadLine()) != null) { readLineCount += sw.ElapsedMilliseconds; sw.Reset(); index++; if (index % 1000 == 0) { Console.WriteLine("Action\t\t Avg. Time @ {5}\n" + "ReadLine\t\t {0}\n" + "TrySetCost\t\t {1}\n" + "ParseTypes\t\t {2}\n" + "AddRulesT\t\t {3}\n" + "TOTAL:\t\t {4}", new object[] { readLineCount / index, trySetCount / index, parseTypesCount / index, addRulesTextCount / index , (readLineCount+trySetCount+parseTypesCount+addRulesTextCount)/index, index}); Console.WriteLine("Time Elapsed @ {0}:\t{1}", new object[] { index, sw.ElapsedMilliseconds }); } // parse card by card if(line != "") lineBuf.Add(line); else if(lineBuf.Count >= 2) { int i = 0; CardRuling newCard = new CardRuling(); newCard.Name = lineBuf[i++]; sw.Start(); if(newCard.TrySetCostAndColor(lineBuf[i])) i++; trySetCount += sw.ElapsedMilliseconds; sw.Reset(); sw.Start(); newCard.Type = Card.ParseTypes(lineBuf[i++]); parseTypesCount += sw.ElapsedMilliseconds; sw.Reset(); sw.Start(); for( ; i<lineBuf.Count; i++) { newCard.RulesText.Add(lineBuf[i]); } addRulesTextCount += sw.ElapsedMilliseconds; sw.Reset(); parsedRulings.Add(newCard); lineBuf.Clear(); } sw.Start(); } return parsedRulings; }
private static void FastWriteCache(StreamReader inputStream) { using (XmlWriter xw = XmlWriter.Create( new XmlTextWriter(new StreamWriter(new FileStream(RulingsCache.AbsolutePath, FileMode.Create))), new XmlWriterSettings() { Indent = true })) { List<string> lineBuf = new List<string>(10); xw.WriteStartDocument(); xw.WriteElementString("Expiration",DateTime.Now.AddDays(ExpirationInterval).ToShortDateString()); xw.WriteStartElement("CardRulings", "Folio"); string line; while((line = inputStream.ReadLine()) != null) { // parse card by card if(line != "") lineBuf.Add(line); else { int i = 0; CardRuling newCard = new CardRuling(); newCard.Name = lineBuf[i++]; if(newCard.Cost.TrySetCost(lineBuf[i])) i++; newCard.Type = Card.ParseTypes(lineBuf[i++]); for( ; i<lineBuf.Count; i++) { newCard.RulesText.Add(lineBuf[i]); } xw.WriteStartElement("Card"); xw.WriteAttributeString("Name",newCard.Name); xw.WriteAttributeString("Cost",newCard.Cost.ToString()); xw.WriteAttributeString("Types",newCard.Type.ToString()); foreach(string l in newCard.RulesText) xw.WriteString(l); xw.WriteEndElement(); } } xw.WriteEndElement(); xw.WriteEndDocument(); } }