Beispiel #1
0
        public IEnumerable <VisitedSystemsClass> ReadSystems(Func <bool> cancelRequested = null)
        {
            if (cancelRequested == null)
            {
                cancelRequested = () => false;
            }

            VisitedSystemsClass last = null;
            long startpos            = filePos;

            if (TimeZone == null)
            {
                if (!ReadHeader())  // may be empty if we read it too fast.. don't worry, monitor will pick it up
                {
                    System.Diagnostics.Trace.WriteLine("File was empty (for now) " + FileName);
                    yield break;
                }
            }

            VisitedSystemsClass ps;

            using (Stream stream = File.Open(this.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                while (!cancelRequested() && ReadNetLogSystem(out ps, cancelRequested, stream))
                {
                    if (last == null)
                    {
                        if (systems.Count == 0)
                        {
                            last = VisitedSystemsClass.GetLast(EDDConfig.Instance.CurrentCmdrID, ps.Time);
                        }
                        else
                        {
                            last = systems[systems.Count - 1];
                        }
                    }

                    if (last != null && ps.Name.Equals(last.Name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    if (ps.Time.Subtract(gammastart).TotalMinutes > 0)  // Ta bara med efter gamma.
                    {
                        systems.Add(ps);
                        yield return(ps);

                        last = ps;
                    }
                }
            }

            if (startpos != filePos)
            {
                Console.WriteLine("Parse ReadData " + FileName + " from " + startpos + " to " + filePos);
            }
        }
Beispiel #2
0
        private void ScanTickWorker(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            var worker  = sender as System.ComponentModel.BackgroundWorker;
            var entries = new List <VisitedSystemsClass>();

            e.Result = entries;
            int netlogpos        = 0;
            NetLogFileReader nfi = null;

            try
            {
                if (EDDConfig.Instance.NetLogDirAutoMode)
                {
                    EliteDangerous.CheckED();
                }

                string filename = null;

                if (m_netLogFileQueue.TryDequeue(out filename))      // if a new one queued, we swap to using it
                {
                    nfi     = OpenFileReader(new FileInfo(filename));
                    lastnfi = nfi;
                }
                else if (!File.Exists(lastnfi.FileName) || lastnfi.filePos >= new FileInfo(lastnfi.FileName).Length)
                {
                    HashSet <string> tlunames  = new HashSet <string>(TravelLogUnit.GetAllNames());
                    string[]         filenames = Directory.EnumerateFiles(GetNetLogPath(), "netLog.*.log", SearchOption.AllDirectories)
                                                 .Select(s => new { name = Path.GetFileName(s), fullname = s })
                                                 .Where(s => !tlunames.Contains(s.name))
                                                 .OrderBy(s => s.name)
                                                 .Select(s => s.fullname)
                                                 .ToArray();
                    foreach (var name in filenames)
                    {
                        nfi     = OpenFileReader(new FileInfo(name));
                        lastnfi = nfi;
                        break;
                    }
                }
                else
                {
                    nfi = lastnfi;
                }

                if (nfi != null)
                {
                    if (nfi.TimeZone == null)
                    {
                        nfi.ReadHeader();
                        if (nfi.TravelLogUnit.id == 0)
                        {
                            nfi.TravelLogUnit.Add();
                        }
                    }

                    netlogpos = nfi.TravelLogUnit.Size;

                    foreach (VisitedSystemsClass dbsys in nfi.ReadSystems())
                    {
                        dbsys.EDSM_sync = false;
                        dbsys.MapColour = EDDConfig.Instance.DefaultMapColour;
                        dbsys.Commander = EDDConfig.Instance.CurrentCmdrID;
                        dbsys.Add();

                        // here we need to make sure the cursystem is set up.. need to do it here because OnNewPosition expects all cursystems to be non null..

                        VisitedSystemsClass item2 = VisitedSystemsClass.GetLast(dbsys.Commander, dbsys.Time);
                        VisitedSystemsClass.UpdateVisitedSystemsEntries(dbsys, item2, EDDiscoveryForm.EDDConfig.UseDistances);       // ensure they have system classes behind them..
                        entries.Add(dbsys);

                        if (worker.CancellationPending)
                        {
                            break;
                        }
                    }
                    nfi.TravelLogUnit.Update();
                }

                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                }
            }
            catch (Exception ex)
            {
                // Revert and re-read the failed entries
                if (nfi != null && nfi.TravelLogUnit != null)
                {
                    nfi.TravelLogUnit.Size = netlogpos;
                }

                System.Diagnostics.Trace.WriteLine("Net tick exception : " + ex.Message);
                System.Diagnostics.Trace.WriteLine(ex.StackTrace);
                throw;
            }
        }
Beispiel #3
0
        public List <SystemPosition> ParseFiles(RichTextBox richTextBox_History, int defaultMapColour)
        {
            string        datapath;
            DirectoryInfo dirInfo;

            datapath = GetNetLogPath();

            if (datapath == null)
            {
                AppendText(richTextBox_History, "Netlog directory not found!" + Environment.NewLine + "Specify location in settings tab" + Environment.NewLine, Color.Red);
                return(null);
            }


            if (!Directory.Exists(datapath))   // if logfiles directory is not found
            {
                if (richTextBox_History != null)
                {
                    richTextBox_History.Clear();
                    AppendText(richTextBox_History, "Netlog directory not found!" + Environment.NewLine + "Specify location in settings tab" + Environment.NewLine, Color.Red);
                    //MessageBox.Show("Netlog directory not found!" + Environment.NewLine + "Specify location in settings tab", "EDDiscovery Error", MessageBoxButtons.OK);
                }
                return(null);
            }
            try
            {
                dirInfo = new DirectoryInfo(datapath);
            }
            catch (Exception ex)
            {
                AppendText(richTextBox_History, "Could not create Directory info: " + ex.Message + Environment.NewLine, Color.Red);
                return(null);
            }

            // Get TravelLogUnits;

            tlUnits = TravelLogUnit.GetAll();

            List <VisitedSystemsClass> vsSystemsList = VisitedSystemsClass.GetAll(ActiveCommander);

            visitedSystems.Clear();
            // Add systems in local DB.
            if (vsSystemsList != null)
            {
                foreach (VisitedSystemsClass vs in vsSystemsList)
                {
                    if (visitedSystems.Count == 0)
                    {
                        visitedSystems.Add(new SystemPosition(vs));
                    }
                    else if (!visitedSystems.Last <SystemPosition>().Name.Equals(vs.Name))  // Avoid duplicate if times exist in same system from different files.
                    {
                        visitedSystems.Add(new SystemPosition(vs));
                    }
                    else
                    {
                        VisitedSystemsClass vs2 = (VisitedSystemsClass)visitedSystems.Last <SystemPosition>().vs;
                        vs.Commander = -2; // Move to dupe user
                        vs.Update();
                    }
                }
            }

            FileInfo[] allFiles = dirInfo.GetFiles("netLog.*.log", SearchOption.AllDirectories).OrderBy(p => p.Name).ToArray();

            NoEvents = true;

            foreach (FileInfo fi in allFiles)
            {
                TravelLogUnit lu        = null;
                bool          parsefile = true;

                if (fi.Name.Equals("netLog.1510280152.01.log"))
                {
                    parsefile = true;
                }

                // Check if we alreade have parse the file and stored in DB.
                if (tlUnits != null)
                {
                    lu = (from c in tlUnits where c.Name == fi.Name select c).FirstOrDefault <TravelLogUnit>();
                }

                if (lu != null)
                {
                    if (lu.Size == fi.Length)  // File is already in DB:
                    {
                        parsefile = false;
                    }
                }
                else
                {
                    lu      = new TravelLogUnit();
                    lu.Name = fi.Name;
                    lu.Path = Path.GetDirectoryName(fi.FullName);
                    lu.Size = 0;  // Add real size after data is in DB //;(int)fi.Length;
                    lu.type = 1;
                    lu.Add();
                }


                if (parsefile)
                {
                    int nr = 0;
                    List <SystemPosition> tempVisitedSystems = new List <SystemPosition>();
                    ParseFile(fi, tempVisitedSystems);


                    foreach (SystemPosition ps in tempVisitedSystems)
                    {
                        SystemPosition ps2;
                        ps2 = (from c in visitedSystems where c.Name == ps.Name && c.time == ps.time select c).FirstOrDefault <SystemPosition>();
                        if (ps2 == null)
                        {
                            VisitedSystemsClass dbsys = new VisitedSystemsClass();

                            dbsys.Name      = ps.Name;
                            dbsys.Time      = ps.time;
                            dbsys.Source    = lu.id;
                            dbsys.EDSM_sync = false;
                            dbsys.Unit      = fi.Name;
                            dbsys.MapColour = defaultMapColour;
                            dbsys.Commander = ActiveCommander;

                            if (!lu.Beta)  // dont store  history in DB for beta (YET)
                            {
                                VisitedSystemsClass last = VisitedSystemsClass.GetLast();

                                if (last == null || !last.Name.Equals(dbsys.Name))  // If same name as last system. Dont Add.  otherwise we get a duplet with last from logfile before with different time.
                                {
                                    if (!VisitedSystemsClass.Exist(dbsys.Name, dbsys.Time))
                                    {
                                        dbsys.Add();
                                        visitedSystems.Add(ps);
                                        nr++;
                                    }
                                }
                            }
                        }
                    }

                    lu.Size = (int)fi.Length;
                    lu.Update();
                    AppendText(richTextBox_History, fi.Name + " " + nr.ToString() + " added to local database." + Environment.NewLine, Color.Black);
                }
            }
            NoEvents = false;

            //var result = visitedSystems.OrderByDescending(a => a.time).ToList<SystemPosition>();

            return(visitedSystems);
        }