Exemplo n.º 1
0
        private Boolean LoadXML(String path)
        {
            DebugLog.writeSeparator();
            DebugLog.writeString("Loading xml", path);

            XmlDocument xDoc = new XmlDocument();

            xDoc.Load(path);
            message = "";
            try
            {
                XmlNode root = null;
                foreach (XmlNode n in xDoc.ChildNodes)
                {
                    if (n.Name.ToLower() == "telemetry")
                    {
                        root = n;
                        break;
                    }
                }

                Decimal version     = 0;
                String  application = "";
                ReadXMLVersion(root, ref version, ref application);
                DebugLog.writeString("Version", version.ToString());
                if (version < 1 || application != "f1-tm")
                {
                    return(false);
                }

                XmlNode        raceNode     = FindXMLChildNode(root, "Race");
                List <XmlNode> laps         = FindXMLChildNodes(raceNode, "Lap");
                XmlAttribute   lapsCountAtt = FindXMLAttribute(raceNode, "Laps");
                DebugLog.writeString("LapsCount", laps.Count.ToString());
                if (laps.Count != Convert.ToInt32(lapsCountAtt.Value))
                {
                    return(false);
                }

                race   = new Race(laps.Count + 1);
                maxLap = laps.Count;

                Boolean loadedDriverTeams = false;
                foreach (XmlNode lap in laps)
                {
                    List <XmlNode> rows = FindXMLChildNodes(lap, "Row");
                    if (!loadedDriverTeams)
                    {
                        foreach (XmlNode Row in rows)
                        {
                            String Name = CleanUp(FindXMLChildNode(Row, "Driver").InnerText);
                            String Team = CleanUp(FindXMLChildNode(Row, "Team").InnerText);

                            race.driver_team.AddDriver(Name, Team);
                        }

                        //list1.Items.Clear();
                        //list2.Items.Clear();

                        //list2.Items.AddRange(race.driver_team.getDrivers());
                        //list1.Items.AddRange(race.driver_team.getTeams());

                        loadedDriverTeams = true;
                    }

                    foreach (XmlNode Row in rows)
                    {
                        int    lapNo    = Convert.ToInt32(FindXMLAttribute(lap, "Number").Value);
                        int    Position = Convert.ToInt32(CleanUp(FindXMLChildNode(Row, "Position").InnerText));
                        String Name     = CleanUp(FindXMLChildNode(Row, "Driver").InnerText);
                        String Team     = CleanUp(FindXMLChildNode(Row, "Team").InnerText);
                        String Time     = CleanUp(FindXMLChildNode(Row, "LapTime").InnerText);
                        String Distance = CleanUp(FindXMLChildNode(Row, "Distance").InnerText);
                        String PitTime  = CleanUp(FindXMLChildNode(Row, "PitStopTime").InnerText);
                        String Mistake  = CleanUp(FindXMLChildNode(Row, "Mistake").InnerText);
                        String Message  = CleanUp(FindXMLChildNode(Row, "Message").InnerText);
                        race.assign(lapNo, Position, Name, Team, Time, Distance, PitTime, Mistake, Message);
                    }
                }

                DebugLog.writeString("Laps read");

                XmlNode pitNode = FindXMLChildNode(root, "PitStops");
                if (pitNode != null)
                {
                    List <XmlNode> stops = FindXMLChildNodes(pitNode, "Stop");
                    if (stops.Count > 0)
                    {
                        race.pitstops.PitsPresent = true;
                    }

                    foreach (XmlNode stop in stops)
                    {
                        String LapNumber = CleanUp(FindXMLChildNode(stop, "LapNumber").InnerText);
                        String Driver    = CleanUp(FindXMLChildNode(stop, "Driver").InnerText);
                        String Team      = CleanUp(FindXMLChildNode(stop, "Team").InnerText);
                        String Tyres     = CleanUp(FindXMLChildNode(stop, "Tyres").InnerText);
                        String Fuel      = CleanUp(FindXMLChildNode(stop, "Fuel").InnerText);
                        String Mistake   = CleanUp(FindXMLChildNode(stop, "Mistake").InnerText);
                        String Total     = CleanUp(FindXMLChildNode(stop, "Total").InnerText);

                        if (race.AddPit(LapNumber, Driver, Team, Tyres, Fuel, Mistake, Total) == false)
                        {
                            message += "Non fatal error in file format in pits stop section. Pit stop information will be unavailable\n";
                            race.pitstops.PitsPresent = false;
                            break;
                        }
                    }
                }

                DebugLog.writeString("Pits read");

                XmlNode wearNode = FindXMLChildNode(root, "CarWear");
                if (wearNode != null)
                {
                    List <XmlNode> cars = FindXMLChildNodes(wearNode, "Car");
                    if (cars.Count > 0)
                    {
                        race.WearPresent = true;
                    }

                    for (int i = 0; i < cars.Count; i++)
                    {
                        List <XmlNode> parts = FindXMLChildNodes(cars[i], "Part");
                        int            carNo = Convert.ToInt32(FindXMLAttribute(cars[i], "DriverNumber").Value);

                        foreach (XmlNode part in parts)
                        {
                            String Name        = CleanUp(FindXMLChildNode(part, "Name").InnerText);
                            String Wear        = CleanUp(FindXMLChildNode(part, "Wear").InnerText);
                            String Reliability = CleanUp(FindXMLChildNode(part, "Reliability").InnerText);

                            if (race.AddCarWear(carNo, Name, Wear, Reliability) == false)
                            {
                                message         += ("Non fatal error in file format in pits stop section. Pit stop information will be unavailable\n");
                                race.WearPresent = false;
                                break;
                            }
                        }
                    }

                    DebugLog.writeString("Wear read");

                    race.Car1Wear.Sort(CompareCarWearNames);
                    race.Car2Wear.Sort(CompareCarWearNames);
                }

                DebugLog.writeString("Done reading");
                DebugLog.writeSeparator();
            }
            catch (Exception ex)
            {
                message += ("Error in file format:\n" + ex.Message);
                DebugLog.writeString("Message Returned", message);
                DebugLog.writeError(ex);
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        public String openWeb(string webPath, string path, string name, string ext, DoWorkEventArgs e)
        {
            DebugLog.writeString("Downloading", webPath);

            try
            {
                // used on each read operation
                byte[] buf = new byte[8192];

                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest)
                                         WebRequest.Create(webPath);

                //Change Timeout to 5 minutes
                request.Timeout = 300000;

                // execute the request
                HttpWebResponse response = (HttpWebResponse)
                                           request.GetResponse();



                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                int    count = 0;
                String s     = response.Headers.Get("Content-Disposition");
                String s2    = response.Headers.Get("Content-Length");

                if (s == null)
                {
                    name = "Download" + name;
                }
                else if (s.StartsWith("filename="))
                {
                    name = s.Replace("filename=", "").Replace(".xml;", "") + name;
                }
                else
                {
                    name = "Download" + name;
                }

                int totalSize = 100;
                if (s2 != null)
                {
                    totalSize = Convert.ToInt32(s2);
                }

                int    i    = 1;
                String temp = path + name + ext;
                while (File.Exists(temp))
                {
                    temp = path + name + "_" + i.ToString() + ext;
                    i++;
                }

                FileStream fs = new FileStream(temp, FileMode.Create);

                try
                {
                    int run = 0;
                    do
                    {
                        if (bw.CancellationPending)
                        {
                            e.Cancel = true;
                            fs.Close();
                            if (File.Exists(temp))
                            {
                                File.Delete(temp);
                            }
                            return("Cancelled");
                        }
                        else
                        {
                            // fill the buffer with data
                            count = resStream.Read(buf, 0, buf.Length);

                            //Check if invalid username error, only in the first run
                            if (run < 1 && Encoding.ASCII.GetString(buf, 0, count).Contains("No Username"))
                            {
                                fs.Close();
                                if (File.Exists(temp))
                                {
                                    File.Delete(temp);
                                }
                                return("Invalid Username");
                            }

                            //Replace invalid characters with an underscore
                            for (int x = 0; x < count; x++)
                            {
                                if (buf[x] > 126 || buf[x] < 32)
                                {
                                    buf[x] = 95;
                                }
                            }

                            //write it out to file
                            fs.Write(buf, 0, count);
                            run += count;

                            bw.ReportProgress(run * 100 / totalSize);
                        }
                    }while (count > 0); // any more data to read?
                }
                catch (Exception ew)
                {
                    fs.Close();
                    if (File.Exists(temp))
                    {
                        File.Delete(temp);
                    }

                    DebugLog.writeString("Download Write Error");
                    DebugLog.writeError(ew);
                    return("Error during file write/download:" + Environment.NewLine + ew.Message);
                }

                fs.Close();
                DebugLog.writeString("Download done");
                DebugLog.writeSeparator();

                //Select the downloaded file
                mObj.SelectedFile = temp;

                return("");
            }
            catch (Exception ex)
            {
                DebugLog.writeString("Download Error");
                DebugLog.writeError(ex);
                return(ex.Message);
            }
        }