Exemplo n.º 1
0
        static public List <JournalEntry> GetAll(int commander = -999)
        {
            Dictionary <long, TravelLogUnit> tlus = TravelLogUnit.GetAll().ToDictionary(t => t.id);

            List <JournalEntry> list = new List <JournalEntry>();

            using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true))
            {
                using (DbCommand cmd = cn.CreateCommand("select * from JournalEntries where CommanderID=@commander Order by EventTime ASC"))
                {
                    if (commander == -999)
                    {
                        cmd.CommandText = "select * from JournalEntries Order by EventTime ";
                    }

                    cmd.AddParameterWithValue("@commander", commander);

                    DataSet ds = SQLiteDBClass.SQLQueryText(cn, cmd);

                    if (ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
                    {
                        return(list);
                    }

                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        JournalEntry sys = JournalEntry.CreateJournalEntry(dr);
                        sys.beta = tlus.ContainsKey(sys.TLUId) ? tlus[sys.TLUId].Beta : false;
                        list.Add(sys);
                    }

                    return(list);
                }
            }
        }
Exemplo n.º 2
0
        static public List <JournalEntry> GetAll(int commander = -999)
        {
            List <JournalEntry> list = new List <JournalEntry>();

            using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true))
            {
                using (DbCommand cmd = cn.CreateCommand("select * from JournalEntries where CommanderID=@commander Order by EventTime ASC"))
                {
                    if (commander == -999)
                    {
                        cmd.CommandText = "select * from JournalEntries Order by EventTime ";
                    }

                    cmd.AddParameterWithValue("@commander", commander);

                    DataSet ds = SQLiteDBClass.SQLQueryText(cn, cmd);

                    if (ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
                    {
                        return(list);
                    }

                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        JournalEntry sys = JournalEntry.CreateJournalEntry(dr);
                        list.Add(sys);
                    }

                    return(list);
                }
            }
        }
Exemplo n.º 3
0
        static protected JournalEntry CreateJournalEntryFixedPos(DbDataReader dr)       // table is Id,TravelLogId,CommanderId,EventData,Sycned
        {
            string       json = (string)dr[3];
            JournalEntry jr   = JournalEntry.CreateJournalEntry(json);

            jr.Id          = (int)(long)dr[0];
            jr.TLUId       = (int)(long)dr[1];
            jr.CommanderId = (int)(long)dr[2];
            jr.Synced      = (int)(long)dr[4];
            return(jr);
        }
Exemplo n.º 4
0
        static protected JournalEntry CreateJournalEntry(DbDataReader dr)
        {
            string json = (string)dr["EventData"];

            JournalEntry jr = JournalEntry.CreateJournalEntry(json);

            jr.Id          = (int)(long)dr["Id"];
            jr.TLUId       = (int)(long)dr["TravelLogId"];
            jr.CommanderId = (int)(long)dr["CommanderId"];
            jr.Synced      = (int)(long)dr["Synced"];
            return(jr);
        }
Exemplo n.º 5
0
        static protected JournalEntry CreateJournalEntry(DbDataReader dr)
        {
            string EDataString = (string)dr["EventData"];

            JournalEntry jr = JournalEntry.CreateJournalEntry(EDataString);

            jr.Id          = (int)(long)dr["Id"];
            jr.TLUId       = (int)(long)dr["TravelLogId"];
            jr.CommanderId = (int)(long)dr["CommanderId"];
            if (jr.EventTimeUTC == default(DateTime))
            {
                jr.EventTimeUTC = ((DateTime)dr["EventTime"]).ToUniversalTime();
            }
            if (jr.EventTypeID == JournalTypeEnum.Unknown)
            {
                jr.EventTypeID = (JournalTypeEnum)(long)dr["eventTypeID"];
            }
            jr.Synced = (int)(long)dr["Synced"];
            return(jr);
        }
Exemplo n.º 6
0
        public static List <JournalEntry> GetAllByTLU(long tluid)
        {
            List <JournalEntry> vsc = new List <JournalEntry>();

            using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true))
            {
                using (DbCommand cmd = cn.CreateCommand("SELECT * FROM JournalEntries WHERE TravelLogId = @source ORDER BY EventTime ASC"))
                {
                    cmd.AddParameterWithValue("@source", tluid);
                    using (DbDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            vsc.Add(JournalEntry.CreateJournalEntry(reader));
                        }
                    }
                }
            }
            return(vsc);
        }
Exemplo n.º 7
0
        static public JournalEntry CreateJournalEntry(DataRow dr)
        {
            string EDataString = (string)dr["EventData"];

            JournalEntry jr = JournalEntry.CreateJournalEntry(EDataString);     // this sets EventTypeId, EventTypeStr and UTC via constructor above..

            jr.Id          = (int)(long)dr["Id"];
            jr.TLUId       = (int)(long)dr["TravelLogId"];
            jr.CommanderId = (int)(long)dr["CommanderId"];
            if (jr.EventTimeUTC == default(DateTime))
            {
                jr.EventTimeUTC = (DateTime)dr["EventTime"];
            }
            if (jr.EventTypeID == JournalTypeEnum.Unknown)
            {
                jr.EventTypeID = (JournalTypeEnum)(long)dr["eventTypeID"];
            }
            jr.EdsmID = (long)dr["EdsmID"];
            jr.Synced = (int)(long)dr["Synced"];
            return(jr);
        }
Exemplo n.º 8
0
        public static List <JournalEntry> GetByEventType(JournalTypeEnum eventtype, int commanderid, DateTime start, DateTime stop)
        {
            List <JournalEntry> vsc = new List <JournalEntry>();

            using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true))
            {
                using (DbCommand cmd = cn.CreateCommand("SELECT * FROM JournalEntries WHERE EventTypeID = @eventtype and  CommanderID=@commander and  EventTime >=@start and EventTime<=@Stop ORDER BY EventTime ASC"))
                {
                    cmd.AddParameterWithValue("@eventtype", (int)eventtype);
                    cmd.AddParameterWithValue("@commander", (int)commanderid);
                    cmd.AddParameterWithValue("@start", start);
                    cmd.AddParameterWithValue("@stop", stop);
                    using (DbDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            vsc.Add(JournalEntry.CreateJournalEntry(reader));
                        }
                    }
                }
            }
            return(vsc);
        }
Exemplo n.º 9
0
        private JournalReaderEntry ProcessLine(string line, bool resetOnError)
        {
            int cmdrid = -2;        //-1 is hidden, -2 is never shown

            if (TravelLogUnit.CommanderId.HasValue)
            {
                cmdrid = TravelLogUnit.CommanderId.Value;
                // System.Diagnostics.Trace.WriteLine(string.Format("TLU says commander {0} at {1}", cmdrid, TravelLogUnit.Name));
            }

            if (line.Length == 0)
            {
                return(null);
            }

            JObject      jo = null;
            JournalEntry je = null;

            try
            {
                jo = JObject.Parse(line);
                je = JournalEntry.CreateJournalEntry(jo);
            }
            catch
            {
                System.Diagnostics.Trace.WriteLine($"Bad journal line:\n{line}");

                if (resetOnError)
                {
                    throw;
                }
                else
                {
                    return(null);
                }
            }

            if (je == null)
            {
                System.Diagnostics.Trace.WriteLine($"Bad journal line:\n{line}");
                return(null);
            }

            bool toosoon = false;

            if (je.EventTypeID == JournalTypeEnum.Fileheader)
            {
                JournalEvents.JournalFileheader header = (JournalEvents.JournalFileheader)je;

                if ((header.Beta && !EliteConfigInstance.InstanceOptions.DisableBetaCommanderCheck) || EliteConfigInstance.InstanceOptions.ForceBetaOnCommander) // if beta, and not disabled, or force beta
                {
                    TravelLogUnit.type |= 0x8000;
                }

                if (header.Part > 1)
                {
                    JournalEvents.JournalContinued contd = JournalEntry.GetLast <JournalEvents.JournalContinued>(je.EventTimeUTC.AddSeconds(1), e => e.Part == header.Part);

                    // Carry commander over from previous log if it ends with a Continued event.
                    if (contd != null && Math.Abs(header.EventTimeUTC.Subtract(contd.EventTimeUTC).TotalSeconds) < 5 && contd.CommanderId >= 0)
                    {
                        TravelLogUnit.CommanderId = contd.CommanderId;
                    }
                }
            }
            else if (je.EventTypeID == JournalTypeEnum.LoadGame)
            {
                string newname = (je as JournalEvents.JournalLoadGame).LoadGameCommander;

                if ((TravelLogUnit.type & 0x8000) == 0x8000)
                {
                    newname = "[BETA] " + newname;
                }

                EDCommander commander = EDCommander.GetCommander(newname);

                if (commander == null)
                {
                    commander = EDCommander.GetListCommanders().FirstOrDefault();
                    if (EDCommander.NumberOfCommanders == 1 && commander != null && commander.Name == "Jameson (Default)")
                    {
                        commander.Name     = newname;
                        commander.EdsmName = newname;
                        EDCommander.Update(new List <EDCommander> {
                            commander
                        }, false);
                    }
                    else
                    {
                        commander = EDCommander.Create(newname, null, EDJournalClass.GetDefaultJournalDir().Equals(TravelLogUnit.Path) ? "" : TravelLogUnit.Path);
                    }
                }

                cmdrid = commander.Nr;

                if (!TravelLogUnit.CommanderId.HasValue)
                {
                    TravelLogUnit.CommanderId = cmdrid;
                    TravelLogUnit.Update();
                    System.Diagnostics.Trace.WriteLine(string.Format("TLU {0} updated with commander {1}", TravelLogUnit.Path, cmdrid));
                }
            }
            else if (je is ISystemStationEntry && ((ISystemStationEntry)je).IsTrainingEvent)
            {
                System.Diagnostics.Trace.WriteLine($"Training detected:\n{line}");
                return(null);
            }

            if (je is IAdditionalFiles)
            {
                if ((je as IAdditionalFiles).ReadAdditionalFiles(Path.GetDirectoryName(FileName), ref jo) == false)     // if failed
                {
                    return(null);
                }
            }

            if (je is JournalEvents.JournalShipyard)                // when going into shipyard
            {
                toosoon      = lastshipyard != null && lastshipyard.Yard.Equals((je as JournalEvents.JournalShipyard).Yard);
                lastshipyard = je as JournalEvents.JournalShipyard;
            }
            else if (je is JournalEvents.JournalStoredShips)        // when going into shipyard
            {
                toosoon = laststoredships != null && CollectionStaticHelpers.Equals(laststoredships.ShipsHere, (je as JournalEvents.JournalStoredShips).ShipsHere) &&
                          CollectionStaticHelpers.Equals(laststoredships.ShipsRemote, (je as JournalEvents.JournalStoredShips).ShipsRemote);
                laststoredships = je as JournalEvents.JournalStoredShips;
            }
            else if (je is JournalEvents.JournalStoredModules)      // when going into outfitting
            {
                toosoon           = laststoredmodules != null && CollectionStaticHelpers.Equals(laststoredmodules.ModuleItems, (je as JournalEvents.JournalStoredModules).ModuleItems);
                laststoredmodules = je as JournalEvents.JournalStoredModules;
            }
            else if (je is JournalEvents.JournalOutfitting)         // when doing into outfitting
            {
                toosoon        = lastoutfitting != null && lastoutfitting.ItemList.Equals((je as JournalEvents.JournalOutfitting).ItemList);
                lastoutfitting = je as JournalEvents.JournalOutfitting;
            }
            else if (je is JournalEvents.JournalMarket)
            {
                toosoon    = lastmarket != null && lastmarket.Equals(je as JournalEvents.JournalMarket);
                lastmarket = je as JournalEvents.JournalMarket;
            }
            else if (je is JournalEvents.JournalUndocked || je is JournalEvents.JournalLoadGame)             // undocked, Load Game, repeats are cleared
            {
                lastshipyard      = null;
                laststoredmodules = null;
                lastoutfitting    = null;
                laststoredmodules = null;
                laststoredships   = null;
            }

            if (toosoon)                                                // if seeing repeats, remove
            {
                System.Diagnostics.Debug.WriteLine("**** Remove as dup " + je.EventTypeStr);
                return(null);
            }

            je.TLUId       = (int)TravelLogUnit.id;
            je.CommanderId = cmdrid;

            return(new JournalReaderEntry {
                JournalEntry = je, Json = jo
            });
        }
Exemplo n.º 10
0
        static public List <JournalEntry> GetAll(int commander         = -999, DateTime?afterutc = null, DateTime?beforeutc = null,
                                                 JournalTypeEnum[] ids = null, DateTime?allidsafterutc = null)
        {
            Dictionary <long, TravelLogUnit> tlus = TravelLogUnit.GetAll().ToDictionary(t => t.id);
            DbCommand           cmd     = null;
            DbDataReader        reader  = null;
            List <JournalEntry> entries = new List <JournalEntry>();

            try
            {
                cmd    = UserDatabase.Instance.ExecuteWithDatabase(cn => cn.Connection.CreateCommand("select * from JournalEntries"));
                reader = UserDatabase.Instance.ExecuteWithDatabase(cn =>
                {
                    string cnd = "";
                    if (commander != -999)
                    {
                        cnd = cnd.AppendPrePad("CommanderID = @commander", " and ");
                        cmd.AddParameterWithValue("@commander", commander);
                    }
                    if (afterutc != null)
                    {
                        cnd = cnd.AppendPrePad("EventTime >= @after", " and ");
                        cmd.AddParameterWithValue("@after", afterutc.Value);
                    }
                    if (beforeutc != null)
                    {
                        cnd = cnd.AppendPrePad("EventTime <= @before", " and ");
                        cmd.AddParameterWithValue("@before", beforeutc.Value);
                    }
                    if (ids != null)
                    {
                        int[] array = Array.ConvertAll(ids, x => (int)x);
                        if (allidsafterutc != null)
                        {
                            cmd.AddParameterWithValue("@idafter", allidsafterutc.Value);
                            cnd = cnd.AppendPrePad("(EventTypeId in (" + string.Join(",", array) + ") Or EventTime>=@idafter)", " and ");
                        }
                        else
                        {
                            cnd = cnd.AppendPrePad("EventTypeId in (" + string.Join(",", array) + ")", " and ");
                        }
                    }

                    if (cnd.HasChars())
                    {
                        cmd.CommandText += " where " + cnd;
                    }

                    cmd.CommandText += " Order By EventTime ASC";

                    return(cmd.ExecuteReader());
                });

                List <JournalEntry> retlist = null;

                do
                {
                    retlist = UserDatabase.Instance.ExecuteWithDatabase(cn =>
                    {
                        List <JournalEntry> list = new List <JournalEntry>();

                        while (list.Count < 1000 && reader.Read())
                        {
                            JournalEntry sys = JournalEntry.CreateJournalEntry(reader);
                            sys.beta         = tlus.ContainsKey(sys.TLUId) ? tlus[sys.TLUId].Beta : false;
                            list.Add(sys);
                        }

                        return(list);
                    });

                    entries.AddRange(retlist);
                }while (retlist != null && retlist.Count != 0);

                return(entries);
            }
            finally
            {
                if (reader != null || cmd != null)
                {
                    UserDatabase.Instance.ExecuteWithDatabase(cn =>
                    {
                        reader?.Close();
                        cmd?.Dispose();
                    });
                }
            }
        }
Exemplo n.º 11
0
        private JournalReaderEntry ProcessLine(string line, bool resetOnError)
        {
            int cmdrid = -2;        //-1 is hidden, -2 is never shown

            if (TravelLogUnit.CommanderId.HasValue)
            {
                cmdrid = TravelLogUnit.CommanderId.Value;
                // System.Diagnostics.Trace.WriteLine(string.Format("TLU says commander {0} at {1}", cmdrid, TravelLogUnit.Name));
            }

            if (line.Length == 0)
            {
                return(null);
            }

            JObject      jo = null;
            JournalEntry je = null;

            try
            {
                jo = JObject.Parse(line);
                je = JournalEntry.CreateJournalEntry(jo);
            }
            catch
            {
                System.Diagnostics.Trace.WriteLine($"Bad journal line:\n{line}");

                if (resetOnError)
                {
                    throw;
                }
                else
                {
                    return(null);
                }
            }

            if (je == null)
            {
                System.Diagnostics.Trace.WriteLine($"Bad journal line:\n{line}");
                return(null);
            }

            if (je.EventTypeID == JournalTypeEnum.Fileheader)
            {
                JournalEvents.JournalFileheader header = (JournalEvents.JournalFileheader)je;

                if (header.Beta && !disable_beta_commander_check)
                {
                    TravelLogUnit.type |= 0x8000;
                }

                if (header.Part > 1)
                {
                    JournalEvents.JournalContinued contd = JournalEntry.GetLast <JournalEvents.JournalContinued>(je.EventTimeUTC.AddSeconds(1), e => e.Part == header.Part);

                    // Carry commander over from previous log if it ends with a Continued event.
                    if (contd != null && Math.Abs(header.EventTimeUTC.Subtract(contd.EventTimeUTC).TotalSeconds) < 5 && contd.CommanderId >= 0)
                    {
                        TravelLogUnit.CommanderId = contd.CommanderId;
                    }
                }
            }
            else if (je.EventTypeID == JournalTypeEnum.LoadGame)
            {
                string newname = (je as JournalEvents.JournalLoadGame).LoadGameCommander;

                if ((TravelLogUnit.type & 0x8000) == 0x8000)
                {
                    newname = "[BETA] " + newname;
                }

                EDCommander _commander = EDCommander.GetCommander(newname);

                if (_commander == null)
                {
                    _commander = EDCommander.GetAll().FirstOrDefault();
                    if (EDCommander.NumberOfCommanders == 1 && _commander != null && _commander.Name == "Jameson (Default)")
                    {
                        _commander.Name     = newname;
                        _commander.EdsmName = newname;
                        EDCommander.Update(new List <EDCommander> {
                            _commander
                        }, false);
                    }
                    else
                    {
                        _commander = EDCommander.Create(newname, null, EDJournalClass.GetDefaultJournalDir().Equals(TravelLogUnit.Path) ? "" : TravelLogUnit.Path);
                    }
                }

                cmdrid = _commander.Nr;

                if (!TravelLogUnit.CommanderId.HasValue)
                {
                    TravelLogUnit.CommanderId = cmdrid;
                    TravelLogUnit.Update();
                    System.Diagnostics.Trace.WriteLine(string.Format("TLU {0} updated with commander {1}", TravelLogUnit.Path, cmdrid));
                }
            }
            else if (je is ISystemStationEntry && ((ISystemStationEntry)je).IsTrainingEvent)
            {
                System.Diagnostics.Trace.WriteLine($"Training detected:\n{line}");
                return(null);
            }

            if (je is IAdditionalFiles)
            {
                if ((je as IAdditionalFiles).ReadAdditionalFiles(Path.GetDirectoryName(FileName), ref jo) == false)     // if failed
                {
                    return(null);
                }
            }

            je.TLUId       = (int)TravelLogUnit.id;
            je.CommanderId = cmdrid;

            return(new JournalReaderEntry {
                JournalEntry = je, Json = jo
            });
        }
        // inhistoryrefreshparse = means reading history in batch mode
        // returns null if journal line is bad or its a repeat.. It does not throw
        private JournalEntry ProcessLine(string line, bool inhistoryrefreshparse)
        {
            //   System.Diagnostics.Debug.WriteLine("Line in '" + line + "'");
            int cmdrid = TravelLogUnit.CommanderId.HasValue  ? TravelLogUnit.CommanderId.Value  : -2; //-1 is hidden, -2 is never shown

            if (line.Length == 0)
            {
                return(null);
            }

            JournalEntry je = null;

            try
            {                                                           // use a try block in case anything in the creation goes t**s up
                je = JournalEntry.CreateJournalEntry(line, true, true); // save JSON, save json, don't return if bad
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine($"{TravelLogUnit.FullName} Exception Bad journal line: {line} {ex.Message} {ex.StackTrace}");
                return(null);
            }

            if (je == null)
            {
                System.Diagnostics.Trace.WriteLine($"{TravelLogUnit.FullName} Bad journal line: {line}");
                return(null);
            }

            bool toosoon = false;

            if (je.EventTypeID == JournalTypeEnum.Fileheader)
            {
                JournalEvents.JournalFileheader header = (JournalEvents.JournalFileheader)je;

                if ((header.Beta && !EliteConfigInstance.InstanceOptions.DisableBetaCommanderCheck) || EliteConfigInstance.InstanceOptions.ForceBetaOnCommander) // if beta, and not disabled, or force beta
                {
                    TravelLogUnit.Type |= TravelLogUnit.BetaMarker;
                }

                if (header.Part > 1)
                {
                    // if we have a last continued, and its header parts match, and it has a commander, and its not too different in time..
                    if (lastcontinued != null && lastcontinued.Part == header.Part && lastcontinued.CommanderId >= 0 &&
                        Math.Abs(header.EventTimeUTC.Subtract(lastcontinued.EventTimeUTC).TotalSeconds) < 5)
                    {
                        cmdrid = lastcontinued.CommanderId;
                        TravelLogUnit.CommanderId = lastcontinued.CommanderId;      // copy commander across.
                    }
                    else
                    {           // this only works if you have a history... EDD does.
                        JournalEvents.JournalContinued contd = JournalEntry.GetLast <JournalEvents.JournalContinued>(je.EventTimeUTC.AddSeconds(1), e => e.Part == header.Part);

                        // Carry commander over from previous log if it ends with a Continued event.
                        if (contd != null && Math.Abs(header.EventTimeUTC.Subtract(contd.EventTimeUTC).TotalSeconds) < 5 && contd.CommanderId >= 0)
                        {
                            cmdrid = lastcontinued.CommanderId;
                            TravelLogUnit.CommanderId = contd.CommanderId;
                        }
                    }
                }
            }
            else if (je.EventTypeID == JournalTypeEnum.Continued)
            {
                lastcontinued = je as JournalEvents.JournalContinued;       // save.. we are getting a new file soon
            }
            else if (je.EventTypeID == JournalTypeEnum.LoadGame)
            {
                var    jlg     = je as JournalEvents.JournalLoadGame;
                string newname = jlg.LoadGameCommander;

                if ((TravelLogUnit.Type & TravelLogUnit.BetaMarker) == TravelLogUnit.BetaMarker)
                {
                    newname = "[BETA] " + newname;
                }

                EDCommander commander = EDCommander.GetCommander(newname);

                if (commander == null)
                {
                    // in the default condition, we have a hidden commander, and first Cmdr. Jameson.
                    commander = EDCommander.GetListCommanders().FirstOrDefault();
                    if (EDCommander.NumberOfCommanders == 2 && commander != null && commander.Name == "Jameson (Default)")
                    {
                        commander.Name       = newname;
                        commander.EdsmName   = newname;
                        commander.JournalDir = TravelLogUnit.Path;
                        EDCommander.Update(commander);
                    }
                    else
                    {
                        commander = EDCommander.Add(name: newname, journalpath: TravelLogUnit.Path);           // always add the path from now on

                        if (EDCommander.Current.Name.Contains("[BETA]") && !newname.Contains("[BETA]"))        // if current commander is beta, and we dont, swap to it
                        {
                            EDCommander.CurrentCmdrID = commander.Id;
                        }
                    }
                }

                commander.FID = jlg.FID;

                cmdrid = commander.Id;

                if (!TravelLogUnit.CommanderId.HasValue)        // we do not need to write to DB the TLU at this point, since we read something the upper layers will do that
                {
                    TravelLogUnit.CommanderId = cmdrid;
                    //System.Diagnostics.Trace.WriteLine(string.Format("TLU {0} updated with commander {1} at {2}", TravelLogUnit.Path, cmdrid, TravelLogUnit.Size));
                }
            }
            else if (je is ISystemStationEntry && ((ISystemStationEntry)je).IsTrainingEvent)
            {
                //System.Diagnostics.Trace.WriteLine($"{filename} Training detected:\n{line}");
                return(null);
            }

            // if additional files, and we are NOT a console commander, see if we can pick up extra info

            if (je is IAdditionalFiles && !(EDCommander.GetCommander(cmdrid)?.ConsoleCommander ?? false))
            {
                if ((je as IAdditionalFiles).ReadAdditionalFiles(TravelLogUnit.Path, inhistoryrefreshparse) == false)     // if failed
                {
                    return(null);
                }
            }

            if (je is JournalEvents.JournalShipyard)                // when going into shipyard
            {
                toosoon      = lastshipyard != null && lastshipyard.Yard.Equals((je as JournalEvents.JournalShipyard).Yard);
                lastshipyard = je as JournalEvents.JournalShipyard;
            }
            else if (je is JournalEvents.JournalStoredShips)        // when going into shipyard
            {
                toosoon = laststoredships != null && CollectionStaticHelpers.Equals(laststoredships.ShipsHere, (je as JournalEvents.JournalStoredShips).ShipsHere) &&
                          CollectionStaticHelpers.Equals(laststoredships.ShipsRemote, (je as JournalEvents.JournalStoredShips).ShipsRemote);
                laststoredships = je as JournalEvents.JournalStoredShips;
            }
            else if (je is JournalEvents.JournalStoredModules)      // when going into outfitting
            {
                toosoon           = laststoredmodules != null && CollectionStaticHelpers.Equals(laststoredmodules.ModuleItems, (je as JournalEvents.JournalStoredModules).ModuleItems);
                laststoredmodules = je as JournalEvents.JournalStoredModules;
            }
            else if (je is JournalEvents.JournalOutfitting)         // when doing into outfitting
            {
                toosoon        = lastoutfitting != null && lastoutfitting.ItemList.Equals((je as JournalEvents.JournalOutfitting).ItemList);
                lastoutfitting = je as JournalEvents.JournalOutfitting;
            }
            else if (je is JournalEvents.JournalMarket)
            {
                toosoon    = lastmarket != null && lastmarket.Equals(je as JournalEvents.JournalMarket);
                lastmarket = je as JournalEvents.JournalMarket;
            }
            else if (je is JournalEvents.JournalCargo)
            {
                var cargo = je as JournalEvents.JournalCargo;
                if (lastcargo != null)
                {
                    toosoon = lastcargo.SameAs(cargo);     // if exactly the same, swallow.
                    //System.Diagnostics.Debug.WriteLine("Cargo vs last " + toosoon);
                }
                lastcargo = cargo;
            }
            else if (je is JournalEvents.JournalUndocked || je is JournalEvents.JournalLoadGame)             // undocked, Load Game, repeats are cleared
            {
                lastshipyard      = null;
                laststoredmodules = null;
                lastoutfitting    = null;
                laststoredmodules = null;
                laststoredships   = null;
                lastcargo         = null;
                cqc = (je is JournalEvents.JournalLoadGame) && ((JournalEvents.JournalLoadGame)je).GameMode == null;
            }
            else if (je is JournalEvents.JournalMusic)
            {
                var music = je as JournalEvents.JournalMusic;

                if (music.MusicTrackID == JournalEvents.EDMusicTrackEnum.CQC || music.MusicTrackID == JournalEvents.EDMusicTrackEnum.CQCMenu)
                {
                    cqc = true;
                }
            }
            else if (je is JournalEvents.JournalNavRoute)
            {
                var route = je as JournalEvents.JournalNavRoute;

                if (lastnavroute != null && (route.EventTimeUTC == lastnavroute.EventTimeUTC || route.EventTimeUTC == lastnavroute.EventTimeUTC.AddSeconds(1)))
                {
                    toosoon = true;
                }

                lastnavroute = route;
            }

            if (toosoon)                                                // if seeing repeats, remove
            {
                // System.Diagnostics.Debug.WriteLine("**** Remove as dup " + je.EventTypeStr);
                return(null);
            }

            if (cqc)  // Ignore events if in CQC
            {
                return(null);
            }

            je.SetTLUCommander(TravelLogUnit.ID, cmdrid);

            return(je);
        }
Exemplo n.º 13
0
        // ordered in time, id order, ascending, oldest first

        static public List <JournalEntry> GetAll(int commander         = -999, DateTime?afterutc = null, DateTime?beforeutc = null,
                                                 JournalTypeEnum[] ids = null, DateTime?allidsafterutc = null)
        {
            var tluslist = TravelLogUnit.GetAll();
            Dictionary <long, TravelLogUnit> tlus = tluslist.ToDictionary(t => t.ID);

            DbCommand           cmd     = null;
            DbDataReader        reader  = null;
            List <JournalEntry> entries = new List <JournalEntry>();

            try
            {
                cmd    = UserDatabase.Instance.ExecuteWithDatabase(cn => cn.Connection.CreateCommand("select * from JournalEntries"));
                reader = UserDatabase.Instance.ExecuteWithDatabase(cn =>
                {
                    string cnd = "";
                    if (commander != -999)
                    {
                        cnd = cnd.AppendPrePad("CommanderID = @commander", " and ");
                        cmd.AddParameterWithValue("@commander", commander);
                    }
                    if (afterutc != null)
                    {
                        cnd = cnd.AppendPrePad("EventTime >= @after", " and ");
                        cmd.AddParameterWithValue("@after", afterutc.Value);
                    }
                    if (beforeutc != null)
                    {
                        cnd = cnd.AppendPrePad("EventTime <= @before", " and ");
                        cmd.AddParameterWithValue("@before", beforeutc.Value);
                    }
                    if (ids != null)
                    {
                        int[] array = Array.ConvertAll(ids, x => (int)x);
                        if (allidsafterutc != null)
                        {
                            cmd.AddParameterWithValue("@idafter", allidsafterutc.Value);
                            cnd = cnd.AppendPrePad("(EventTypeId in (" + string.Join(",", array) + ") Or EventTime>=@idafter)", " and ");
                        }
                        else
                        {
                            cnd = cnd.AppendPrePad("EventTypeId in (" + string.Join(",", array) + ")", " and ");
                        }
                    }

                    if (cnd.HasChars())
                    {
                        cmd.CommandText += " where " + cnd;
                    }

                    cmd.CommandText += " Order By EventTime,Id ASC";

                    return(cmd.ExecuteReader());
                });

                List <JournalEntry> retlist = null;

                do
                {
                    // experiments state that reading the DL takes 270/4000ms, reading json -> 1250, then the rest is creating and decoding the fields
                    // not much scope to improve it outside of the core json speed.

                    retlist = UserDatabase.Instance.ExecuteWithDatabase(cn =>       // split into smaller chunks to allow other things access..
                    {
                        List <JournalEntry> list = new List <JournalEntry>();

                        while (list.Count < 1000 && reader.Read())
                        {
                            JournalEntry sys = JournalEntry.CreateJournalEntry(reader);
                            sys.beta         = tlus.ContainsKey(sys.TLUId) ? tlus[sys.TLUId].Beta : false;
                            list.Add(sys);
                        }

                        return(list);
                    });

                    entries.AddRange(retlist);
                }while (retlist != null && retlist.Count != 0);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Getall Exception " + ex);
            }
            finally
            {
                if (reader != null || cmd != null)
                {
                    UserDatabase.Instance.ExecuteWithDatabase(cn =>
                    {
                        reader?.Close();
                        cmd?.Dispose();
                    });
                }
            }

            return(entries);
        }
Exemplo n.º 14
0
        // inhistoryrefreshparse = means reading history in batch mode
        private JournalEntry ProcessLine(string line, bool inhistoryrefreshparse, bool resetOnError)
        {
            int cmdrid = TravelLogUnit.CommanderId.HasValue  ? TravelLogUnit.CommanderId.Value  : -2; //-1 is hidden, -2 is never shown

            if (line.Length == 0)
            {
                return(null);
            }

            JObject      jo = null;
            JournalEntry je = null;

            try
            {
                jo = JObject.Parse(line);
                je = JournalEntry.CreateJournalEntry(jo, true);
            }
            catch
            {
                System.Diagnostics.Trace.WriteLine($"Bad journal line:\n{line}");

                if (resetOnError)
                {
                    throw;
                }
                else
                {
                    return(null);
                }
            }

            if (je == null)
            {
                System.Diagnostics.Trace.WriteLine($"Bad journal line:\n{line}");
                return(null);
            }

            bool toosoon = false;

            if (je.EventTypeID == JournalTypeEnum.Fileheader)
            {
                JournalEvents.JournalFileheader header = (JournalEvents.JournalFileheader)je;

                if ((header.Beta && !EliteConfigInstance.InstanceOptions.DisableBetaCommanderCheck) || EliteConfigInstance.InstanceOptions.ForceBetaOnCommander) // if beta, and not disabled, or force beta
                {
                    TravelLogUnit.type |= TravelLogUnit.BetaMarker;
                }

                if (header.Part > 1)
                {
                    // if we have a last continued, and its header parts match, and it has a commander, and its not too different in time..
                    if (lastcontinued != null && lastcontinued.Part == header.Part && lastcontinued.CommanderId >= 0 &&
                        Math.Abs(header.EventTimeUTC.Subtract(lastcontinued.EventTimeUTC).TotalSeconds) < 5)
                    {
                        cmdrid = lastcontinued.CommanderId;
                        TravelLogUnit.CommanderId = lastcontinued.CommanderId;      // copy commander across.
                    }
                    else
                    {           // this only works if you have a history... EDD does.
                        JournalEvents.JournalContinued contd = JournalEntry.GetLast <JournalEvents.JournalContinued>(je.EventTimeUTC.AddSeconds(1), e => e.Part == header.Part);

                        // Carry commander over from previous log if it ends with a Continued event.
                        if (contd != null && Math.Abs(header.EventTimeUTC.Subtract(contd.EventTimeUTC).TotalSeconds) < 5 && contd.CommanderId >= 0)
                        {
                            cmdrid = lastcontinued.CommanderId;
                            TravelLogUnit.CommanderId = contd.CommanderId;
                        }
                    }
                }
            }
            else if (je.EventTypeID == JournalTypeEnum.Continued)
            {
                lastcontinued = je as JournalEvents.JournalContinued;       // save.. we are getting a new file soon
            }
            else if (je.EventTypeID == JournalTypeEnum.LoadGame)
            {
                var    jlg     = je as JournalEvents.JournalLoadGame;
                string newname = jlg.LoadGameCommander;

                if ((TravelLogUnit.type & TravelLogUnit.BetaMarker) == TravelLogUnit.BetaMarker)
                {
                    newname = "[BETA] " + newname;
                }

                EDCommander commander = EDCommander.GetCommander(newname);

                if (commander == null)
                {
                    // in the default condition, we have a hidden commander, and first Cmdr. Jameson.
                    commander = EDCommander.GetListCommanders().FirstOrDefault();
                    if (EDCommander.NumberOfCommanders == 2 && commander != null && commander.Name == "Jameson (Default)")
                    {
                        commander.Name     = newname;
                        commander.EdsmName = newname;
                        EDCommander.Update(new List <EDCommander> {
                            commander
                        }, false);
                    }
                    else
                    {
                        commander = EDCommander.Create(name: newname, journalpath: EDJournalUIScanner.GetDefaultJournalDir().Equals(TravelLogUnit.Path) ? "" : TravelLogUnit.Path);
                    }
                }

                commander.FID = jlg.FID;

                cmdrid = commander.Nr;

                if (!TravelLogUnit.CommanderId.HasValue)        // we do not need to write to DB the TLU at this point, since we read something the upper layers will do that
                {
                    TravelLogUnit.CommanderId = cmdrid;
                    //System.Diagnostics.Trace.WriteLine(string.Format("TLU {0} updated with commander {1} at {2}", TravelLogUnit.Path, cmdrid, TravelLogUnit.Size));
                }
            }
            else if (je is ISystemStationEntry && ((ISystemStationEntry)je).IsTrainingEvent)
            {
                System.Diagnostics.Trace.WriteLine($"Training detected:\n{line}");
                return(null);
            }

            if (je is IAdditionalFiles)
            {
                if ((je as IAdditionalFiles).ReadAdditionalFiles(Path.GetDirectoryName(FileName), inhistoryrefreshparse, ref jo) == false)     // if failed
                {
                    return(null);
                }
            }

            if (je is JournalEvents.JournalShipyard)                // when going into shipyard
            {
                toosoon      = lastshipyard != null && lastshipyard.Yard.Equals((je as JournalEvents.JournalShipyard).Yard);
                lastshipyard = je as JournalEvents.JournalShipyard;
            }
            else if (je is JournalEvents.JournalStoredShips)        // when going into shipyard
            {
                toosoon = laststoredships != null && CollectionStaticHelpers.Equals(laststoredships.ShipsHere, (je as JournalEvents.JournalStoredShips).ShipsHere) &&
                          CollectionStaticHelpers.Equals(laststoredships.ShipsRemote, (je as JournalEvents.JournalStoredShips).ShipsRemote);
                laststoredships = je as JournalEvents.JournalStoredShips;
            }
            else if (je is JournalEvents.JournalStoredModules)      // when going into outfitting
            {
                toosoon           = laststoredmodules != null && CollectionStaticHelpers.Equals(laststoredmodules.ModuleItems, (je as JournalEvents.JournalStoredModules).ModuleItems);
                laststoredmodules = je as JournalEvents.JournalStoredModules;
            }
            else if (je is JournalEvents.JournalOutfitting)         // when doing into outfitting
            {
                toosoon        = lastoutfitting != null && lastoutfitting.ItemList.Equals((je as JournalEvents.JournalOutfitting).ItemList);
                lastoutfitting = je as JournalEvents.JournalOutfitting;
            }
            else if (je is JournalEvents.JournalMarket)
            {
                toosoon    = lastmarket != null && lastmarket.Equals(je as JournalEvents.JournalMarket);
                lastmarket = je as JournalEvents.JournalMarket;
            }
            else if (je is JournalEvents.JournalCargo)
            {
                var cargo = je as JournalEvents.JournalCargo;
                toosoon   = lastcargo != null && cargo.EventTimeUTC.Subtract(lastcargo.EventTimeUTC).Minutes < 15;
                lastcargo = cargo;
            }
            else if (je is JournalEvents.JournalUndocked || je is JournalEvents.JournalLoadGame)             // undocked, Load Game, repeats are cleared
            {
                lastshipyard      = null;
                laststoredmodules = null;
                lastoutfitting    = null;
                laststoredmodules = null;
                laststoredships   = null;
                lastcargo         = null;
                cqc = false;
            }
            else if (je is JournalEvents.JournalMusic)
            {
                var music = je as JournalEvents.JournalMusic;

                if (music.MusicTrackID == JournalEvents.EDMusicTrackEnum.CQC || music.MusicTrackID == JournalEvents.EDMusicTrackEnum.CQCMenu)
                {
                    cqc = true;
                }
            }
            else if (je is JournalEvents.JournalNavRoute)
            {
                var route = je as JournalEvents.JournalNavRoute;

                if (lastnavroute != null && (route.EventTimeUTC == lastnavroute.EventTimeUTC || route.EventTimeUTC == lastnavroute.EventTimeUTC.AddSeconds(1)))
                {
                    toosoon = true;
                }

                lastnavroute = route;
            }

            if (toosoon)                                                // if seeing repeats, remove
            {
                System.Diagnostics.Debug.WriteLine("**** Remove as dup " + je.EventTypeStr);
                return(null);
            }

            if (cqc)  // Ignore events if in CQC
            {
                return(null);
            }

            je.SetTLUCommander(TravelLogUnit.id, cmdrid);

            return(je);
        }
Exemplo n.º 15
0
        static public List <JournalEntry> GetAll(int commander         = -999, DateTime?after = null, DateTime?before = null,
                                                 JournalTypeEnum[] ids = null, DateTime?allidsafter = null)
        {
            Dictionary <long, TravelLogUnit> tlus = TravelLogUnit.GetAll().ToDictionary(t => t.id);

            List <JournalEntry> list = new List <JournalEntry>();

            using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true))
            {
                using (DbCommand cmd = cn.CreateCommand("select * from JournalEntries"))
                {
                    string cnd = "";
                    if (commander != -999)
                    {
                        cnd = cnd.AppendPrePad("CommanderID = @commander", " and ");
                        cmd.AddParameterWithValue("@commander", commander);
                    }
                    if (after != null)
                    {
                        cnd = cnd.AppendPrePad("EventTime >= @after", " and ");
                        cmd.AddParameterWithValue("@after", after.Value);
                    }
                    if (before != null)
                    {
                        cnd = cnd.AppendPrePad("EventTime <= @before", " and ");
                        cmd.AddParameterWithValue("@before", before.Value);
                    }
                    if (ids != null)
                    {
                        int[] array = Array.ConvertAll(ids, x => (int)x);
                        if (allidsafter != null)
                        {
                            cmd.AddParameterWithValue("@idafter", allidsafter.Value);
                            cnd = cnd.AppendPrePad("(EventTypeId in (" + string.Join(",", array) + ") Or EventTime>=@idafter)", " and ");
                        }
                        else
                        {
                            cnd = cnd.AppendPrePad("EventTypeId in (" + string.Join(",", array) + ")", " and ");
                        }
                    }

                    if (cnd.HasChars())
                    {
                        cmd.CommandText += " where " + cnd;
                    }

                    cmd.CommandText += " Order By EventTime ASC";

                    using (DbDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            JournalEntry sys = JournalEntry.CreateJournalEntry(reader);
                            sys.beta = tlus.ContainsKey(sys.TLUId) ? tlus[sys.TLUId].Beta : false;
                            list.Add(sys);
                        }
                    }

                    return(list);
                }
            }
        }