Exemplo n.º 1
0
 public NetLogFileReader(TravelLogUnit tlu, List <VisitedSystemsClass> vsclist = null) : base(tlu)
 {
     if (vsclist != null)
     {
         systems = vsclist;
     }
     else
     {
         systems = VisitedSystemsClass.GetAll(tlu);
     }
 }
Exemplo n.º 2
0
        private void dEBUGResetAllHistoryToFirstCommandeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List <VisitedSystemsClass> vsall = VisitedSystemsClass.GetAll();

            foreach (VisitedSystemsClass vs in vsall)
            {
                if (vs.Commander != 0)
                {
                    vs.Commander = 0;
                    vs.Update();
                }
            }
        }
Exemplo n.º 3
0
        // called during start up and if refresh history is pressed

        public List <VisitedSystemsClass> ParseFiles(out string error, int defaultMapColour, Func <bool> cancelRequested, Action <int, string> updateProgress)
        {
            error = null;

            string datapath = GetNetLogPath();

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

            if (!Directory.Exists(datapath))   // if logfiles directory is not found
            {
                error = "Netlog directory is not present!" + Environment.NewLine + "Specify location in settings tab";
                return(null);
            }

            List <VisitedSystemsClass> vsSystemsList = VisitedSystemsClass.GetAll(EDDConfig.Instance.CurrentCmdrID);

            List <VisitedSystemsClass>         visitedSystems           = new List <VisitedSystemsClass>();
            Dictionary <string, TravelLogUnit> m_travelogUnits          = TravelLogUnit.GetAll().Where(t => t.type == 1).GroupBy(t => t.Name).Select(g => g.First()).ToDictionary(t => t.Name);
            Dictionary <string, List <VisitedSystemsClass> > vsc_lookup = VisitedSystemsClass.GetAll().GroupBy(v => v.Unit).ToDictionary(g => g.Key, g => g.ToList());

            if (vsSystemsList != null)
            {
                foreach (VisitedSystemsClass vs in vsSystemsList)
                {
                    if (visitedSystems.Count == 0)
                    {
                        visitedSystems.Add(vs);
                    }
                    else if (!visitedSystems.Last <VisitedSystemsClass>().Name.Equals(vs.Name))  // Avoid duplicate if times exist in same system from different files.
                    {
                        visitedSystems.Add(vs);
                    }
                    else
                    {
                        VisitedSystemsClass vs2 = (VisitedSystemsClass)visitedSystems.Last <VisitedSystemsClass>();
                        if (vs2.id != vs.id)
                        {
                            vs.Commander = -2; // Move to dupe user
                            vs.Update();
                        }
                    }
                }
            }
            // order by file write time so we end up on the last one written
            FileInfo[] allFiles = Directory.EnumerateFiles(datapath, "netLog.*.log", SearchOption.AllDirectories).Select(f => new FileInfo(f)).OrderBy(p => p.LastWriteTime).ToArray();

            List <NetLogFileReader> readersToUpdate = new List <NetLogFileReader>();

            for (int i = 0; i < allFiles.Length; i++)
            {
                FileInfo fi = allFiles[i];

                var reader = OpenFileReader(fi, m_travelogUnits, vsc_lookup);

                if (!m_travelogUnits.ContainsKey(reader.TravelLogUnit.Name))
                {
                    m_travelogUnits[reader.TravelLogUnit.Name] = reader.TravelLogUnit;
                    reader.TravelLogUnit.Add();
                }

                if (!netlogreaders.ContainsKey(reader.TravelLogUnit.Name))
                {
                    netlogreaders[reader.TravelLogUnit.Name] = lastnfi;
                }

                if (reader.filePos != fi.Length || i == allFiles.Length - 1)  // File not already in DB, or is the last one
                {
                    readersToUpdate.Add(reader);
                }
            }

            using (SQLiteConnectionED cn = new SQLiteConnectionED())
            {
                for (int i = 0; i < readersToUpdate.Count; i++)
                {
                    NetLogFileReader reader = readersToUpdate[i];
                    updateProgress(i * 100 / readersToUpdate.Count, reader.TravelLogUnit.Name);

                    using (DbTransaction tn = cn.BeginTransaction())
                    {
                        foreach (VisitedSystemsClass ps in reader.ReadSystems(cancelRequested))
                        {
                            ps.EDSM_sync = false;
                            ps.MapColour = defaultMapColour;
                            ps.Commander = EDDConfig.Instance.CurrentCmdrID;

                            ps.Add(cn, tn);
                            visitedSystems.Add(ps);
                        }

                        reader.TravelLogUnit.Update(cn, tn);

                        tn.Commit();
                    }

                    if (updateProgress != null)
                    {
                        updateProgress((i + 1) * 100 / readersToUpdate.Count, reader.TravelLogUnit.Name);
                    }

                    lastnfi = reader;
                }
            }

            return(visitedSystems);
        }
Exemplo n.º 4
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);
        }