// Called by EDJournalClass periodically to scan for journal entries

        public List <JournalEntry> ScanForNewEntries()
        {
            var entries = new List <JournalEntry>();

            try
            {
                string filename = null;

                if (lastnfi != null)                            // always give old file another go, even if we are going to change
                {
                    if (!File.Exists(lastnfi.FileName))         // if its been removed, null
                    {
                        lastnfi = null;
                    }
                    else
                    {
                        ScanReader(lastnfi, entries);

                        if (entries.Count > 0)
                        {
                            ticksNoActivity = 0;
                            return(entries);     // feed back now don't change file
                        }
                    }
                }

                if (m_netLogFileQueue.TryDequeue(out filename))      // if a new one queued, we swap to using it
                {
                    lastnfi = OpenFileReader(new FileInfo(filename));
                    System.Diagnostics.Debug.WriteLine(string.Format("Change to scan {0}", lastnfi.FileName));
                    if (lastnfi != null)
                    {
                        ScanReader(lastnfi, entries);   // scan new one
                    }
                }
                // every few goes, if its not there or filepos is greater equal to length (so only done when fully up to date)
                else if (ticksNoActivity >= 30 && (lastnfi == null || lastnfi.filePos >= new FileInfo(lastnfi.FileName).Length))
                {
                    HashSet <string> tlunames  = new HashSet <string>(TravelLogUnit.GetAllNames());
                    string[]         filenames = Directory.EnumerateFiles(WatcherFolder, journalfilematch, SearchOption.AllDirectories)
                                                 .Select(s => new { name = Path.GetFileName(s), fullname = s })
                                                 .Where(s => !tlunames.Contains(s.name))            // find any new ones..
                                                 .OrderBy(s => s.name)
                                                 .Select(s => s.fullname)
                                                 .ToArray();

                    foreach (var name in filenames)         // for any new filenames..
                    {
                        System.Diagnostics.Debug.WriteLine("No Activity but found new file " + name);
                        lastnfi = OpenFileReader(new FileInfo(name));
                        break;      // stop on first found
                    }

                    if (lastnfi != null)
                    {
                        ScanReader(lastnfi, entries);   // scan new one
                    }
                    ticksNoActivity = 0;
                }

                ticksNoActivity++;

                return(entries);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Net tick exception : " + ex.Message);
                System.Diagnostics.Trace.WriteLine(ex.StackTrace);
                return(new List <JournalEntry>());
            }
        }
Exemplo n.º 2
0
        public List <JournalEntry> ScanForNewEntries()
        {
            var             entries = new List <JournalEntry>();
            EDJournalReader nfi     = null;

            try
            {
                string filename = null;

                if (lastnfi != null)
                {
                    ScanReader(lastnfi, entries);
                }

                if (entries.Count != 0)
                {
                    return(entries);
                }

                if (m_netLogFileQueue.TryDequeue(out filename))      // if a new one queued, we swap to using it
                {
                    nfi     = OpenFileReader(new FileInfo(filename));
                    lastnfi = nfi;
                    System.Diagnostics.Trace.WriteLine(string.Format("Change in file, scan {0}", lastnfi.FileName));
                }
                else if (ticksNoActivity >= 30 && (lastnfi == null || (!File.Exists(lastnfi.FileName) || lastnfi.filePos >= new FileInfo(lastnfi.FileName).Length)))
                {
                    if (lastnfi == null)
                    {
                        Trace.Write($"No last file - scanning for journals");
                    }
                    else if (!File.Exists(lastnfi.FileName))
                    {
                        Trace.WriteLine($"File {lastnfi.FileName} not found - scanning for journals");
                    }
                    else
                    {
//                        Trace.WriteLine($"No activity on {lastnfi.FileName} for 60 seconds ({lastnfi.filePos} >= {new FileInfo(lastnfi.FileName).Length} - scanning for new journals");
                    }

                    HashSet <string> tlunames  = new HashSet <string>(TravelLogUnit.GetAllNames());
                    string[]         filenames = Directory.EnumerateFiles(m_watcherfolder, "Journal.*.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();
                    ticksNoActivity = 0;
                    foreach (var name in filenames)
                    {
                        nfi     = OpenFileReader(new FileInfo(name));
                        lastnfi = nfi;
                        break;
                    }
                }
                else
                {
                    nfi = lastnfi;
                }

                ticksNoActivity++;

                if (nfi != null)
                {
                    ScanReader(nfi, entries);
                }

                return(entries);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Net tick exception : " + ex.Message);
                System.Diagnostics.Trace.WriteLine(ex.StackTrace);
                return(new List <JournalEntry>());
            }
        }
Exemplo n.º 3
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;
            }
        }
        // Called by EDScanner periodically to scan for journal entries

        public Tuple <List <JournalEntry>, List <UIEvent> > ScanForNewEntries()
        {
            var entries   = new List <JournalEntry>();
            var uientries = new List <UIEvent>();

            string filename = null;

            if (lastnfi != null)                            // always give old file another go, even if we are going to change
            {
                if (!File.Exists(lastnfi.FullName))         // if its been removed, null
                {
                    lastnfi = null;
                }
                else
                {
                    //var notdone = new FileInfo(lastnfi.FullName).Length != lastnfi.Pos ? "**********" : ""; System.Diagnostics.Debug.WriteLine($"Scan last nfi {lastnfi.FullName} from {lastnfi.Pos} Length file is {new FileInfo(lastnfi.FullName).Length} {notdone} ");

                    ScanReader(entries, uientries);

                    if (entries.Count > 0 || uientries.Count > 0)
                    {
                        // System.Diagnostics.Debug.WriteLine("ScanFornew read " + entries.Count() + " ui " + uientries.Count());
                        ticksNoActivity = 0;
                        return(new Tuple <List <JournalEntry>, List <UIEvent> >(entries, uientries));     // feed back now don't change file
                    }
                }
            }

            if (m_netLogFileQueue.TryDequeue(out filename))      // if a new one queued, we swap to using it
            {
                lastnfi = OpenFileReader(filename);
                System.Diagnostics.Debug.WriteLine(string.Format("Change to scan {0}", lastnfi.FullName));
                ScanReader(entries, uientries);
            }
            else if (ticksNoActivity >= 30 && (lastnfi == null || lastnfi.Pos >= new FileInfo(lastnfi.FullName).Length))
            {
                // every few goes, if its not there or filepos is greater equal to length (so only done when fully up to date)
                // scan all files in the folder, pick out any new logs, and process the first that is found.

                try
                {
                    HashSet <string> tlunames  = new HashSet <string>(TravelLogUnit.GetAllNames());
                    string[]         filenames = Directory.EnumerateFiles(WatcherFolder, journalfilematch, SearchOption.AllDirectories)
                                                 .Select(s => new { name = Path.GetFileName(s), fullname = s })
                                                 .Where(s => !tlunames.Contains(s.fullname))              // find any new ones..
                                                 .Where(g => new FileInfo(g.fullname).LastWriteTime >= mindateutc)
                                                 .OrderBy(s => s.name)
                                                 .Select(s => s.fullname)
                                                 .ToArray();

                    if (filenames.Length > 0)
                    {
                        lastnfi = OpenFileReader(filenames[0]);     // open first one
                        System.Diagnostics.Debug.WriteLine(string.Format("Found new file {0}", lastnfi.FullName));
                        ScanReader(entries, uientries);
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Exception during monitor watch file found " + ex);
                }
                finally
                {
                    ticksNoActivity = 0;
                }
            }

            ticksNoActivity++;

            return(new Tuple <List <JournalEntry>, List <UIEvent> >(entries, uientries));     // feed back now don't change file
        }
Exemplo n.º 5
0
        public List <JournalEntry> ScanForNewEntries()
        {
            var             entries   = new List <JournalEntry>();
            int             netlogpos = 0;
            EDJournalReader nfi       = null;

            try
            {
                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;
                    System.Diagnostics.Trace.WriteLine(string.Format("Change in file, scan {0}", lastnfi.FileName));
                }
                else if (ticksNoActivity >= 30 && (lastnfi == null || (!File.Exists(lastnfi.FileName) || lastnfi.filePos >= new FileInfo(lastnfi.FileName).Length)))
                {
                    if (lastnfi == null)
                    {
                        Trace.Write($"No last file - scanning for journals");
                    }
                    else if (!File.Exists(lastnfi.FileName))
                    {
                        Trace.WriteLine($"File {lastnfi.FileName} not found - scanning for journals");
                    }
                    else
                    {
                        Trace.WriteLine($"No activity on {lastnfi.FileName} for 60 seconds ({lastnfi.filePos} >= {new FileInfo(lastnfi.FileName).Length} - scanning for new journals");
                    }

                    HashSet <string> tlunames  = new HashSet <string>(TravelLogUnit.GetAllNames());
                    string[]         filenames = Directory.EnumerateFiles(m_watcherfolder, "Journal.*.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();
                    ticksNoActivity = 0;
                    foreach (var name in filenames)
                    {
                        nfi     = OpenFileReader(new FileInfo(name));
                        lastnfi = nfi;
                        break;
                    }
                }
                else
                {
                    nfi = lastnfi;
                }

                ticksNoActivity++;

                if (nfi != null)
                {
                    if (nfi.TravelLogUnit.id == 0)
                    {
                        nfi.TravelLogUnit.type = 3;
                        nfi.TravelLogUnit.Add();
                    }

                    netlogpos = nfi.TravelLogUnit.Size;
                    List <JournalEntry> ents = nfi.ReadJournalLog().ToList();

                    if (ents.Count > 0)
                    {
                        using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true))
                        {
                            using (DbTransaction txn = cn.BeginTransaction())
                            {
                                ents = ents.Where(je => JournalEntry.FindEntry(je).Count == 0).ToList();

                                foreach (JournalEntry je in ents)
                                {
                                    entries.Add(je);
                                    je.Add(cn, txn);
                                    ticksNoActivity = 0;
                                }

                                nfi.TravelLogUnit.Update(cn);

                                txn.Commit();
                            }
                        }
                    }
                }

                return(entries);
            }
            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;
            }
        }