Beispiel #1
0
        public bool AddScanToBestSystem(JournalScan je, int startindex, List <HistoryEntry> hl, out HistoryEntry he, out JournalLocOrJump jl)
        {
            he = null;
            jl = null;

            if (je?.BodyName == null)
            {
                return(false);
            }

            // go thru the list of history entries looking for a Loc

            for (int j = startindex; j >= 0; j--)   // same as FindBestSystem
            {
                he = hl[j];

                if (he.IsLocOrJump)
                {
                    jl = (JournalLocOrJump)he.journalEntry;

                    // get the body designation, given the je/system name

                    string designation = BodyDesignations.GetBodyDesignation(je, he.System.Name);
                    System.Diagnostics.Debug.Assert(designation != null);

                    // either the name/sys address matches, or the designation matches the star of the system name
                    if (je.IsStarNameRelated(he.System.Name, he.System.SystemAddress, designation))
                    {
                        je.BodyDesignation = designation;
                        return(ProcessJournalScan(je, he.System, true));
                    }
                    else if (jl.StarSystem != null && je.IsStarNameRelated(jl.StarSystem, jl.SystemAddress, designation)) // if we have a starsystem name, and its related, its a rename, ignore it
                    {
                        System.Diagnostics.Trace.WriteLine($"Rejecting body {designation} ({je.BodyName}) in system {he.System.Name} => {jl.StarSystem} due to system rename");
                        return(false);
                    }
                }
            }

            je.BodyDesignation = BodyDesignations.GetBodyDesignation(je, hl[startindex].System.Name);
            return(ProcessJournalScan(je, hl[startindex].System, true));         // no relationship, add..
        }
Beispiel #2
0
        public bool ReadNetLogSystem(out JObject jo, out JournalLocOrJump je, Func <bool> cancelRequested = null, Stream stream = null, bool ownstream = false)
        {
            if (cancelRequested == null)
            {
                cancelRequested = () => false;
            }

            string line;

            try
            {
                if (stream == null)
                {
                    stream    = File.Open(this.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    ownstream = true;
                }
                while (!cancelRequested() && this.ReadLine(out line, l => l, stream))
                {
                    ParseLineTime(line);

                    if (line.Contains("[PG]"))
                    {
                        if (line.Contains("[PG] [Notification] Left a playlist lobby"))
                        {
                            this.CQC = false;
                        }

                        if (line.Contains("[PG] Destroying playlist lobby."))
                        {
                            this.CQC = false;
                        }

                        if (line.Contains("[PG] [Notification] Joined a playlist lobby"))
                        {
                            this.CQC = true;
                        }
                        if (line.Contains("[PG] Created playlist lobby"))
                        {
                            this.CQC = true;
                        }
                        if (line.Contains("[PG] Found matchmaking lobby object"))
                        {
                            this.CQC = true;
                        }
                    }

                    int offset = line.IndexOf("} System:") - 8;
                    if (offset >= 1 && ParseTime(line.Substring(offset, 8)) && this.CQC == false)
                    {
                        //Console.WriteLine(" RD:" + line );
                        if (line.Contains("ProvingGround"))
                        {
                            continue;
                        }

                        if (ParseVisitedSystem(this.LastLogTime, this.TimeZoneOffset, line.Substring(offset + 10), out jo, out je))
                        {   // Remove some training systems
                            if (je.StarSystem.Equals("Training", StringComparison.CurrentCultureIgnoreCase))
                            {
                                continue;
                            }
                            if (je.StarSystem.Equals("Destination", StringComparison.CurrentCultureIgnoreCase))
                            {
                                continue;
                            }
                            if (je.StarSystem.Equals("Altiris", StringComparison.CurrentCultureIgnoreCase))
                            {
                                continue;
                            }
                            return(true);
                        }
                    }
                }
            }
            finally
            {
                if (ownstream)
                {
                    stream.Dispose();
                }
            }

            jo = null;
            je = null;
            return(false);
        }
Beispiel #3
0
        public IEnumerable <JObject> ReadSystems(Func <bool> cancelRequested = null, int cmdrid = -1)
        {
            if (cancelRequested == null)
            {
                cancelRequested = () => false;
            }

            if (cmdrid < 0)
            {
                cmdrid = EDCommander.CurrentCmdrID;
            }

            JournalLocOrJump 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;
                }
            }

            JObject          jo;
            JournalLocOrJump je;

            using (Stream stream = File.Open(this.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                System.Diagnostics.Debug.WriteLine("ReadData " + FileName + " from " + startpos + " to " + filePos);

                while (!cancelRequested() && ReadNetLogSystem(out jo, out je, cancelRequested, stream))
                {
                    if (last == null)
                    {
                        if (systems.Count == 0)
                        {
                            last = JournalEntry.GetLast <JournalLocOrJump>(cmdrid, je.EventTimeUTC);
                        }
                        else
                        {
                            last = systems[systems.Count - 1];
                        }
                    }

                    if (last != null && je.StarSystem.Equals(last.StarSystem, StringComparison.InvariantCultureIgnoreCase) &&
                        (!je.HasCoordinate || !last.HasCoordinate || (je.StarPos - last.StarPos).LengthSquared < 0.001))
                    {
                        continue;
                    }

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

                        last = je;
                    }
                }
            }

            if (startpos != filePos)
            {
                System.Diagnostics.Debug.WriteLine("Parse ReadData " + FileName + " from " + startpos + " to " + filePos);
            }
        }
        // used by historylist directly for a single update during play, in foreground..  Also used by above.. so can be either in fore/back
        public bool AddScanToBestSystem(JournalScan je, int startindex, List <HistoryEntry> hl, out HistoryEntry he, out JournalLocOrJump jl)
        {
            if (je?.BodyName == null)
            {
                he = null;
                jl = null;
                return(false);
            }

            for (int j = startindex; j >= 0; j--)
            {
                he = hl[j];

                if (he.IsLocOrJump)
                {
                    jl = (JournalLocOrJump)he.journalEntry;
                    string designation = GetBodyDesignation(je, he.System.Name);

                    if (je.IsStarNameRelated(he.System.Name, designation, he.System.SystemAddress))       // if its part of the name, use it
                    {
                        je.BodyDesignation = designation;
                        return(ProcessJournalScan(je, he.System, true));
                    }
                    else if (jl != null && je.IsStarNameRelated(jl.StarSystem, designation, jl.SystemAddress))
                    {
                        // Ignore scans where the system name has changed
                        return(false);
                    }
                }
            }

            jl = null;
            he = null;

            je.BodyDesignation = GetBodyDesignation(je, hl[startindex].System.Name);
            return(ProcessJournalScan(je, hl[startindex].System, true));         // no relationship, add..
        }
Beispiel #5
0
        protected bool ParseVisitedSystem(DateTime time, TimeSpan tzoffset, string line, out JObject jo, out JournalLocOrJump je)
        {
            jo = new JObject();
            jo["timestamp"] = time.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'");
            jo["event"]     = "FSDJump";
            je = null;

            try
            {
                Regex pattern;

                /* MKW: Use regular expressions to parse the log; much more readable and robust.
                 * Example log entry:
                 *
                 * From  ED  2.1 /1.6
                 * {19:21:15} System:"Ooscs Fraae JR-L d8-112" StarPos:(-11609.469,639.594,20141.875)ly  NormalFlight
                 * string rgexpstr = "{(?<Hour>\\d+):(?<Minute>\\d+):(?<Second>\\d+)} System:\"(?<SystemName>[^\"]+)\" StarPos:\\((?<Pos>.*?)\\)ly( +(?<TravelMode>\\w+))?";
                 *
                 * new from beta3?
                 * {18:15:14} System:"Pleiades Sector HR-W d1-41" StarPos:(-83.969,-146.156,-334.219)ly Body:0 RelPos:(-1.19887e+07,-9.95573e+06,2.55124e+06)km Supercruise
                 * string rgexpstr = "{(?<Hour>\\d+):(?<Minute>\\d+):(?<Second>\\d+)} System:\"(?<SystemName>[^\"]+)\" StarPos:\\((?<Pos>.*?)\\)ly Body:(?<Body>\d+) StarPos:\\((?<Pos>.*?)\\)ly( +(?<TravelMode>\\w+))?";
                 *
                 *
                 * Pre ED 2.1/1.6
                 *  {09:36:16} System:0(Thuechea JE-O b11-0) Body:1 Pos:(-6.67432e+009,7.3151e+009,-1.19125e+010) Supercruise
                 *
                 * Also, please note that due to E:D bugs, these entries can be at the end of a line as well, not just on a line of their own.
                 * The RegExp below actually just finds the pattern somewhere in the line, so it caters for rubbish at the end too.
                 */

                if (line.Contains("StarPos:")) // new  ED 2.1 format
                {
                    //{(?<Hour>\d+):(?<Minute>\d+):(?<Second>\d+)} System:"(?<SystemName>[^"]+)" StarPos:\((?<Pos>.*?)\)ly( +(?<TravelMode>\w+))?
                    //{(?<Hour>\d+):(?<Minute>\d+):(?<Second>\d+)} System:"(?<SystemName>[^"]+)" StarPos:\((?<Pos>.*?)\)ly( +(?<TravelMode>\w+))?
                    //string rgexpstr = "{(?<Hour>\\d+):(?<Minute>\\d+):(?<Second>\\d+)} System:\"(?<SystemName>[^\"]+)\" StarPos:\\((?<Pos>.*?)\\)ly( +(?<TravelMode>\\w+))?";
                    string rgexpstr;

                    if (line.Contains("Body:"))
                    {
                        rgexpstr = "System:\"(?<SystemName>[^\"]+)\" StarPos:\\((?<Pos>.*?)\\)ly Body:(?<Body>\\d+) RelPos:\\((?<RelPos>.*?)\\)km( +(?<TravelMode>\\w+))?";
                    }
                    else
                    {
                        rgexpstr = "System:\"(?<SystemName>[^\"]+)\" StarPos:\\((?<Pos>.*?)\\)ly( +(?<TravelMode>\\w+))?";
                    }

                    pattern = new Regex(rgexpstr);

                    Match match = pattern.Match(line);

                    if (match != null && match.Success)
                    {
                        //sp.Nr = int.Parse(match.Groups["Body"].Value);
                        jo["StarSystem"] = match.Groups["SystemName"].Value;
                        string pos = match.Groups["Pos"].Value;
                        try
                        {
                            string[] xyzpos = pos.Split(',');
                            jo["StarPos"] = new JArray(
                                double.Parse(xyzpos[0], CultureInfo.InvariantCulture),
                                double.Parse(xyzpos[1], CultureInfo.InvariantCulture),
                                double.Parse(xyzpos[2], CultureInfo.InvariantCulture)
                                );
                        }
                        catch
                        {
                            System.Diagnostics.Trace.WriteLine("System parse error 1:" + line);
                            return(false);
                        }
                    }
                    else
                    {
                        System.Diagnostics.Trace.WriteLine("System parse error 1:" + line);
                        return(false);
                    }
                }
                else
                {
                    pattern = new Regex(@"System:\d+\((?<SystemName>.*?)\) Body:(?<Body>\d+) Pos:\(.*?\)( (?<TravelMode>\w+))?");
                    Match match = pattern.Match(line);

                    if (match != null && match.Success)
                    {
                        //sp.Nr = int.Parse(match.Groups["Body"].Value);

                        jo["StarSystem"] = match.Groups["SystemName"].Value;
                    }
                    else
                    {
                        System.Diagnostics.Trace.WriteLine("System parse error 2:" + line);
                        return(false);
                    }
                }

                je = new JournalFSDJump(jo);

                return(true);
            }
            catch
            {
                // MKW TODO: should we log bad lines?
                return(false);
            }
        }
Beispiel #6
0
        public static HistoryEntry FromJournalEntry(JournalEntry je, HistoryEntry prev, bool checkedsm, out bool journalupdate, SQLiteConnectionSystem conn = null, EDCommander cmdr = null)
        {
            ISystem isys    = prev == null ? new SystemClass("Unknown") : prev.System;
            int     indexno = prev == null ? 1 : prev.Indexno + 1;

            int mapcolour = 0;

            journalupdate = false;
            bool starposfromedsm = false;
            bool firstdiscover   = false;

            if (je.EventTypeID == JournalTypeEnum.Location || je.EventTypeID == JournalTypeEnum.FSDJump ||
                (je.EventTypeID == JournalTypeEnum.StartJump && (je as JournalStartJump)?.JumpType == "Hyperspace"))
            {
                JournalLocOrJump jl   = je as JournalLocOrJump;
                JournalFSDJump   jfsd = je as JournalFSDJump;
                JournalStartJump js   = je as JournalStartJump;

                ISystem newsys;

                if (jl != null && jl.HasCoordinate)       // LAZY LOAD IF it has a co-ord.. the front end will when it needs it
                {
                    newsys = new SystemClass(jl.StarSystem, jl.StarPos.X, jl.StarPos.Y, jl.StarPos.Z)
                    {
                        id_edsm         = jl.EdsmID < 0 ? 0 : jl.EdsmID, // pass across the EDSMID for the lazy load process.
                        faction         = jl.Faction,
                        government      = jl.EDGovernment,
                        primary_economy = jl.EDEconomy,
                        security        = jl.EDSecurity,
                        population      = jl.Population ?? 0,
                        state           = jl.EDState,
                        allegiance      = jl.EDAllegiance,
                        UpdateDate      = jl.EventTimeUTC,
                        status          = SystemStatusEnum.EDDiscovery,
                    };

                    if (jfsd != null && jfsd.JumpDist <= 0 && isys.HasCoordinate) // if we don't have a jump distance (pre 2.2) but the last sys does have pos, we can compute distance and update entry
                    {
                        jfsd.JumpDist = SystemClassDB.Distance(isys, newsys);     // fill it out here
                        journalupdate = true;
                    }

                    // If it was a new system, pass the coords back to the StartJump
                    if (prev != null && prev.System.name == newsys.name && !prev.System.HasCoordinate)
                    {
                        prev.System.x = newsys.x;
                        prev.System.y = newsys.y;
                        prev.System.z = newsys.z;
                    }
                }
                else
                {                           // Default one
                    string sysname = jl?.StarSystem ?? js?.StarSystem;

                    newsys         = new SystemClass(sysname);
                    newsys.id_edsm = je.EdsmID;

                    if (checkedsm)                                                                        // see if we can find the right system
                    {
                        ISystem s = SystemCache.FindEDSM(newsys, conn: conn, usedb: true, useedsm: true); // has no co-ord, did we find it?

                        if (s != null)                                                                    // yes, use, and update the journal with the esdmid, and also the position if we have a co-ord
                        {                                                                                 // so next time we don't have to do this again..
                            if (jl != null && jl.HasCoordinate)
                            {
                                s.x = Math.Round(jl.StarPos.X * 32.0) / 32.0;
                                s.y = Math.Round(jl.StarPos.Y * 32.0) / 32.0;
                                s.z = Math.Round(jl.StarPos.Z * 32.0) / 32.0;
                            }

                            newsys = s;

                            if (jfsd != null && jfsd.JumpDist <= 0 && newsys.HasCoordinate && isys.HasCoordinate) // if we don't have a jump distance (pre 2.2) but the last sys does, we can compute
                            {
                                jfsd.JumpDist = SystemClassDB.Distance(isys, newsys);                             // fill it out here.  EDSM systems always have co-ords, but we should check anyway
                                journalupdate = true;
                            }

                            if (je.EdsmID <= 0 && newsys.id_edsm > 0)
                            {
                                journalupdate = true;
                            }
                        }
                    }
                }

                if (jfsd != null)
                {
                    if (jfsd.JumpDist <= 0 && isys.HasCoordinate && newsys.HasCoordinate) // if no JDist, its a really old entry, and if previous has a co-ord
                    {
                        jfsd.JumpDist = SystemClassDB.Distance(isys, newsys);             // fill it out here
                        journalupdate = true;
                    }

                    mapcolour = jfsd.MapColor;
                }

                isys            = newsys;
                starposfromedsm = (jl != null && jl.HasCoordinate) ? jl.StarPosFromEDSM : newsys.HasCoordinate;
                firstdiscover   = jl == null ? false : jl.EDSMFirstDiscover;
            }

            string summary, info, detailed;

            je.FillInformation(out summary, out info, out detailed);

            HistoryEntry he = new HistoryEntry
            {
                Indexno             = indexno,
                EntryType           = je.EventTypeID,
                Journalid           = je.Id,
                journalEntry        = je,
                System              = isys,
                EventTimeUTC        = je.EventTimeUTC,
                MapColour           = mapcolour,
                EdsmSync            = je.SyncedEDSM,
                EDDNSync            = je.SyncedEDDN,
                EGOSync             = je.SyncedEGO,
                StartMarker         = je.StartMarker,
                StopMarker          = je.StopMarker,
                EventSummary        = summary,
                EventDescription    = info,
                EventDetailedInfo   = detailed,
                IsStarPosFromEDSM   = starposfromedsm,
                IsEDSMFirstDiscover = firstdiscover,
                Commander           = cmdr ?? EDCommander.GetCommander(je.CommanderId)
            };


            // WORK out docked/landed state

            if (prev != null)
            {
                if (prev.docked.HasValue)                   // copy docked..
                {
                    he.docked = prev.docked;
                }
                if (prev.landed.HasValue)
                {
                    he.landed = prev.landed;
                }
                if (prev.hyperspace.HasValue)
                {
                    he.hyperspace = prev.hyperspace;
                }

                he.shiptype          = prev.shiptype;
                he.shipid            = prev.shipid;
                he.whereami          = prev.whereami;
                he.onCrewWithCaptain = prev.onCrewWithCaptain;
                he.gamemode          = prev.gamemode;
                he.group             = prev.group;
            }

            if (je.EventTypeID == JournalTypeEnum.Location)
            {
                JournalLocation jl = je as JournalLocation;
                he.docked     = jl.Docked;
                he.whereami   = jl.Docked ? jl.StationName : jl.Body;
                he.hyperspace = false;
            }
            else if (je.EventTypeID == JournalTypeEnum.Docked)
            {
                JournalDocked jl = je as JournalDocked;
                he.docked   = true;
                he.whereami = jl.StationName;
            }
            else if (je.EventTypeID == JournalTypeEnum.Undocked)
            {
                he.docked = false;
            }
            else if (je.EventTypeID == JournalTypeEnum.Touchdown)
            {
                he.landed = true;
            }
            else if (je.EventTypeID == JournalTypeEnum.Liftoff)
            {
                he.landed = false;
            }
            else if (je.EventTypeID == JournalTypeEnum.SupercruiseEntry)
            {
                he.whereami   = (je as JournalSupercruiseEntry).StarSystem;
                he.hyperspace = true;
            }
            else if (je.EventTypeID == JournalTypeEnum.SupercruiseExit)
            {
                he.whereami   = (je as JournalSupercruiseExit).Body;
                he.hyperspace = false;
            }
            else if (je.EventTypeID == JournalTypeEnum.FSDJump)
            {
                he.whereami   = (je as JournalFSDJump).StarSystem;
                he.hyperspace = true;
            }
            else if (je.EventTypeID == JournalTypeEnum.StartJump)
            {
                he.hyperspace = true;   // some of these are just to make sure, as FSDJump will also set it
            }
            else if (je.EventTypeID == JournalTypeEnum.LoadGame)
            {
                JournalLoadGame jl = je as JournalLoadGame;

                he.onCrewWithCaptain = null;        // can't be in a crew at this point
                he.gamemode          = jl.GameMode; // set game mode
                he.group             = jl.Group;    // and group, may be empty
                he.landed            = jl.StartLanded;
                he.hyperspace        = false;

                if (jl.Ship.IndexOf("buggy", StringComparison.InvariantCultureIgnoreCase) == -1)        // load game with buggy, can't tell what ship we get back into, so ignore
                {
                    he.shiptype = (je as JournalLoadGame).Ship;
                    he.shipid   = (je as JournalLoadGame).ShipId;
                }
            }
            else if (je.EventTypeID == JournalTypeEnum.ShipyardBuy)         // BUY does not have ship id, but the new entry will that is written later - journals 8.34
            {
                he.shiptype = (je as JournalShipyardBuy).ShipType;
            }
            else if (je.EventTypeID == JournalTypeEnum.ShipyardNew)
            {
                he.shiptype = (je as JournalShipyardNew).ShipType;
                he.shipid   = (je as JournalShipyardNew).ShipId;
            }
            else if (je.EventTypeID == JournalTypeEnum.ShipyardSwap)
            {
                he.shiptype = (je as JournalShipyardSwap).ShipType;
                he.shipid   = (je as JournalShipyardSwap).ShipId;
            }
            else if (je.EventTypeID == JournalTypeEnum.JoinACrew)
            {
                he.onCrewWithCaptain = (je as JournalJoinACrew).Captain;
            }
            else if (je.EventTypeID == JournalTypeEnum.QuitACrew)
            {
                he.onCrewWithCaptain = null;
            }

            if (prev != null && prev.travelling)      // if we are travelling..
            {
                he.travelled_distance    = prev.travelled_distance;
                he.travelled_missingjump = prev.travelled_missingjump;
                he.travelled_jumps       = prev.travelled_jumps;

                if (he.IsFSDJump && !he.MultiPlayer)   // if jump, and not multiplayer..
                {
                    double dist = ((JournalFSDJump)je).JumpDist;
                    if (dist <= 0)
                    {
                        he.travelled_missingjump++;
                    }
                    else
                    {
                        he.travelled_distance += dist;
                        he.travelled_jumps++;
                    }
                }

                he.travelled_seconds = prev.travelled_seconds;
                TimeSpan diff = he.EventTimeUTC.Subtract(prev.EventTimeUTC);

                if (he.EntryType != JournalTypeEnum.LoadGame && diff < new TimeSpan(2, 0, 0))   // time between last entry and load game is not real time
                {
                    he.travelled_seconds += diff;
                }

                if (he.StopMarker || he.StartMarker)
                {
                    //Debug.WriteLine("Travelling stop at " + he.Indexno);
                    he.travelling         = false;
                    he.EventDetailedInfo += ((he.EventDetailedInfo.Length > 0) ? Environment.NewLine : "") + "Travelled " + he.travelled_distance.ToStringInvariant("0.0") + " LY"
                                            + ", " + he.travelled_jumps + " jumps"
                                            + ((he.travelled_missingjump > 0) ? ", " + he.travelled_missingjump + " unknown distance jumps" : "") +
                                            ", time " + he.travelled_seconds;

                    he.travelled_distance = 0;
                    he.travelled_seconds  = new TimeSpan(0);
                }
                else
                {
                    he.travelling = true;

                    if (he.IsFSDJump)
                    {
                        he.EventDetailedInfo += ((he.EventDetailedInfo.Length > 0) ? Environment.NewLine : "") + "Travelling" +
                                                " distance " + he.travelled_distance.ToString("0.0") + " LY"
                                                + ", " + he.travelled_jumps + " jumps"
                                                + ((he.travelled_missingjump > 0) ? ", " + he.travelled_missingjump + " unknown distance jumps" : "") +
                                                ", time " + he.travelled_seconds;
                    }
                }
            }

            if (he.StartMarker)
            {
                //Debug.WriteLine("Travelling start at " + he.Indexno);
                he.travelling = true;
            }

            return(he);
        }
Beispiel #7
0
        public static HistoryEntry FromJournalEntry(JournalEntry je, HistoryEntry prev, out bool journalupdate, SQLiteConnectionSystem conn = null)
        {
            ISystem isys    = prev == null ? new SystemClass("Unknown") : prev.System;
            int     indexno = prev == null ? 1 : prev.Indexno + 1;

            journalupdate = false;

            if (je.EventTypeID == JournalTypeEnum.Location || je.EventTypeID == JournalTypeEnum.FSDJump)
            {
                JournalLocOrJump jl = je as JournalLocOrJump;

                ISystem newsys;

                if (jl != null && jl.HasCoordinate)       // LAZY LOAD IF it has a co-ord.. the front end will when it needs it
                {
                    newsys = new SystemClass(jl.StarSystem, jl.StarPos.X, jl.StarPos.Y, jl.StarPos.Z)
                    {
                        EDSMID         = jl.EdsmID < 0 ? 0 : jl.EdsmID, // pass across the EDSMID for the lazy load process.
                        SystemAddress  = jl.SystemAddress,
                        Population     = jl.Population ?? 0,
                        Faction        = jl.Faction,
                        Government     = jl.EDGovernment,
                        Allegiance     = jl.EDAllegiance,
                        State          = jl.EDState,
                        Security       = jl.EDSecurity,
                        PrimaryEconomy = jl.EDEconomy,
                        Power          = jl.PowerList,
                        PowerState     = jl.PowerplayState,
                        status         = SystemStatusEnum.EDDiscovery,
                    };

                    // If it was a new system, pass the coords back to the StartJump
                    if (prev != null && prev.journalEntry is JournalStartJump)
                    {
                        prev.System = newsys;       // give the previous startjump our system..
                    }
                }
                else
                {
                    // NOTE Rob: 09-JAN-2018 I've removed the Jumpstart looking up a system by name since they were using up lots of lookup time during history reading.
                    // This is used for pre 2.2 systems without co-ords, which now should be limited.
                    // JumpStart still gets the system when the FSD loc is processed, see above.
                    // Jumpstart was also screwing about with the EDSM ID fill in which was broken.  This is now working again.

                    // Default one
                    newsys        = new SystemClass(jl.StarSystem);
                    newsys.EDSMID = je.EdsmID;

                    ISystem s = SystemCache.FindSystem(newsys, conn); // has no co-ord, did we find it?

                    if (s != null)                                    // found a system..
                    {
                        if (jl != null && jl.HasCoordinate)           // if journal Loc, and journal has a star position, use that instead of EDSM..
                        {
                            s.X = Math.Round(jl.StarPos.X * 32.0) / 32.0;
                            s.Y = Math.Round(jl.StarPos.Y * 32.0) / 32.0;
                            s.Z = Math.Round(jl.StarPos.Z * 32.0) / 32.0;
                        }

                        //Debug.WriteLine("HistoryList found system {0} {1}", s.id_edsm, s.name);
                        newsys = s;

                        if (jl != null && je.EdsmID <= 0 && newsys.EDSMID > 0) // only update on a JL..
                        {
                            journalupdate = true;
                            Debug.WriteLine("HE EDSM ID update requested {0} {1}", newsys.EDSMID, newsys.Name);
                        }
                    }
                    else
                    {
                        newsys.EDSMID = -1;        // mark as checked but not found
                    }
                }

                JournalFSDJump jfsd = je as JournalFSDJump;

                if (jfsd != null)
                {
                    if (jfsd.JumpDist <= 0 && isys.HasCoordinate && newsys.HasCoordinate) // if no JDist, its a really old entry, and if previous has a co-ord
                    {
                        jfsd.JumpDist = isys.Distance(newsys);                            // fill it out here

                        if (jfsd.JumpDist > 0)
                        {
                            journalupdate = true;
                            Debug.WriteLine("Je Jump distance update(3) requested {0} {1} {2}", newsys.EDSMID, newsys.Name, jfsd.JumpDist);
                        }
                    }
                }

                isys = newsys;
            }

            HistoryEntry he = new HistoryEntry
            {
                Indexno      = indexno,
                journalEntry = je,
                System       = isys,
                EntryStatus  = HistoryEntryStatus.Update(prev?.EntryStatus, je, isys.Name)
            };

            he.TravelStatus = HistoryTravelStatus.Update(prev?.TravelStatus, prev, he);     // need a real he so can't do that as part of the constructor.

            return(he);
        }
Beispiel #8
0
        // used by historylist directly for a single update during play, in foreground..  Also used by above.. so can be either in fore/back
        public bool AddBodyToBestSystem(IBodyNameAndID je, int startindex, List <HistoryEntry> hl, out HistoryEntry he, out JournalLocOrJump jl)
        {
            if (je.Body == null || je.BodyType == "Station" || je.BodyType == "PlanetaryRing" || je.BodyType == "SmallBody")
            {
                he = null;
                jl = null;
                return(false);
            }

            for (int j = startindex; j >= 0; j--)
            {
                he = hl[j];

                if (he.IsLocOrJump && he.System.Name == je.StarSystem && (he.System.SystemAddress == null || je.SystemAddress == null || he.System.SystemAddress == je.SystemAddress))
                {
                    jl = (JournalLocOrJump)he.journalEntry;
                    string designation = GetBodyDesignation(je);

                    if (IsStarNameRelated(he.System.Name, je.Body, designation))       // if its part of the name, use it
                    {
                        je.BodyDesignation = designation;
                        return(Process(je, he.System, true));
                    }
                    else if (jl != null && IsStarNameRelated(jl.StarSystem, je.Body, designation))
                    {
                        // Ignore scans where the system name has changed
                        return(false);
                    }
                }
            }

            jl = null;
            he = null;

            je.BodyDesignation = GetBodyDesignation(je);
            return(Process(je, hl[startindex].System, true));         // no relationship, add..
        }
Beispiel #9
0
        public static HistoryEntry FromJournalEntry(JournalEntry je, HistoryEntry prev, out bool journalupdate, SQLiteConnectionSystem conn = null)
        {
            ISystem isys    = prev == null ? new SystemClass("Unknown") : prev.System;
            int     indexno = prev == null ? 1 : prev.Indexno + 1;

            journalupdate = false;

            if (je.EventTypeID == JournalTypeEnum.Location || je.EventTypeID == JournalTypeEnum.FSDJump)
            {
                JournalLocOrJump jl = je as JournalLocOrJump;

                ISystem newsys;

                if (jl != null && jl.HasCoordinate)       // LAZY LOAD IF it has a co-ord.. the front end will when it needs it
                {
                    newsys = new SystemClass(jl.StarSystem, jl.StarPos.X, jl.StarPos.Y, jl.StarPos.Z)
                    {
                        EDSMID         = jl.EdsmID < 0 ? 0 : jl.EdsmID, // pass across the EDSMID for the lazy load process.
                        Faction        = jl.Faction,
                        Government     = jl.EDGovernment,
                        PrimaryEconomy = jl.EDEconomy,
                        Security       = jl.EDSecurity,
                        Population     = jl.Population ?? 0,
                        State          = jl.EDState,
                        Allegiance     = jl.EDAllegiance,
                        UpdateDate     = jl.EventTimeUTC,
                        status         = SystemStatusEnum.EDDiscovery,
                        SystemAddress  = jl.SystemAddress,
                    };

                    // If it was a new system, pass the coords back to the StartJump
                    if (prev != null && prev.journalEntry is JournalStartJump)
                    {
                        prev.System = newsys;       // give the previous startjump our system..
                    }
                }
                else
                {
                    // NOTE Rob: 09-JAN-2018 I've removed the Jumpstart looking up a system by name since they were using up lots of lookup time during history reading.
                    // This is used for pre 2.2 systems without co-ords, which now should be limited.
                    // JumpStart still gets the system when the FSD loc is processed, see above.
                    // Jumpstart was also screwing about with the EDSM ID fill in which was broken.  This is now working again.

                    // Default one
                    newsys        = new SystemClass(jl.StarSystem);
                    newsys.EDSMID = je.EdsmID;

                    ISystem s = SystemCache.FindSystem(newsys, conn); // has no co-ord, did we find it?

                    if (s != null)                                    // found a system..
                    {
                        if (jl != null && jl.HasCoordinate)           // if journal Loc, and journal has a star position, use that instead of EDSM..
                        {
                            s.X = Math.Round(jl.StarPos.X * 32.0) / 32.0;
                            s.Y = Math.Round(jl.StarPos.Y * 32.0) / 32.0;
                            s.Z = Math.Round(jl.StarPos.Z * 32.0) / 32.0;
                        }

                        //Debug.WriteLine("HistoryList found system {0} {1}", s.id_edsm, s.name);
                        newsys = s;

                        if (jl != null && je.EdsmID <= 0 && newsys.EDSMID > 0) // only update on a JL..
                        {
                            journalupdate = true;
                            Debug.WriteLine("HE EDSM ID update requested {0} {1}", newsys.EDSMID, newsys.Name);
                        }
                    }
                    else
                    {
                        newsys.EDSMID = -1;        // mark as checked but not found
                    }
                }

                JournalFSDJump jfsd = je as JournalFSDJump;

                if (jfsd != null)
                {
                    if (jfsd.JumpDist <= 0 && isys.HasCoordinate && newsys.HasCoordinate) // if no JDist, its a really old entry, and if previous has a co-ord
                    {
                        jfsd.JumpDist = isys.Distance(newsys);                            // fill it out here

                        if (jfsd.JumpDist > 0)
                        {
                            journalupdate = true;
                            Debug.WriteLine("Je Jump distance update(3) requested {0} {1} {2}", newsys.EDSMID, newsys.Name, jfsd.JumpDist);
                        }
                    }
                }

                isys = newsys;
            }

            HistoryEntry he = new HistoryEntry
            {
                Indexno      = indexno,
                journalEntry = je,
                System       = isys,
                EntryStatus  = HistoryEntryStatus.Update(prev?.EntryStatus, je, isys.Name)
            };

            if (prev != null)
            {
                if (prev.StopMarker)                 // if we had a stop marker previously, means the next one needs to clear the counters
                {
                    he.isTravelling         = false; // still travelling if its a start marker
                    he.TravelledDistance    = 0;
                    he.TravelledSeconds     = new TimeSpan(0);
                    he.TravelledMissingjump = 0;
                    he.Travelledjumps       = 0;
                }
                else
                {
                    he.isTravelling         = prev.isTravelling;
                    he.TravelledDistance    = prev.TravelledDistance;
                    he.TravelledSeconds     = prev.TravelledSeconds;
                    he.TravelledMissingjump = prev.TravelledMissingjump;
                    he.Travelledjumps       = prev.Travelledjumps;
                }
            }

            if (he.StartMarker)           // start marker, start travelling
            {
                he.isTravelling = true;
            }

            if (he.isTravelling)
            {
                if (he.IsFSDJump && !he.MultiPlayer)   // if jump, and not multiplayer..
                {
                    double dist = ((JournalFSDJump)je).JumpDist;
                    if (dist <= 0)
                    {
                        he.TravelledMissingjump++;
                    }
                    else
                    {
                        he.TravelledDistance += dist;
                        he.Travelledjumps++;
                    }
                }

                if (prev != null)
                {
                    TimeSpan diff = he.EventTimeUTC.Subtract(prev.EventTimeUTC);

                    if (he.EntryType != JournalTypeEnum.LoadGame && diff < new TimeSpan(2, 0, 0))   // time between last entry and load game is not real time
                    {
                        he.TravelledSeconds += diff;
                    }
                }
            }

            return(he);
        }
        // called thru CalLWithConverter in UI main class.. that pases a ImageConverter to us
        // sysname and or ss can be null if it was picked up by a watcher not the new journal screenshot entry

        private void ProcessScreenshot(string filename, string sysname, JournalScreenshot ss, int cmdrid, ScreenShotImageConverter cp)
        {
            System.Diagnostics.Debug.Assert(System.Windows.Forms.Application.MessageLoop);  // UI thread

            System.Threading.Timer timer = null;

            if (sysname == null)
            {
                if (LastJournalLoc != null)
                {
                    sysname = LastJournalLoc.StarSystem;
                }
                else if (cmdrid >= 0)
                {
                    LastJournalLoc = JournalEntry.GetLast <JournalLocOrJump>(cmdrid, DateTime.UtcNow);
                    if (LastJournalLoc != null)
                    {
                        sysname = LastJournalLoc.StarSystem;
                    }
                }
            }

            if (sysname == null)
            {
                HistoryEntry he = discoveryform.history.GetLast;
                sysname = (he != null) ? he.System.name : "Unknown System";
            }

            try
            {
                cp.InputFilename     = filename;
                cp.SystemName        = sysname;
                cp.JournalScreenShot = ss;
                cp.CommanderID       = cmdrid;

                bool converted = false;

                using (Bitmap bmp = cp.GetScreenshot(ref filename, discoveryform.LogLine))
                {
                    // Don't run if OnScreenshot has already run for this image
                    if ((ScreenshotTimers.TryGetValue(filename, out timer) && timer == null) || (!ScreenshotTimers.TryAdd(filename, null) && !ScreenshotTimers.TryUpdate(filename, null, timer)))
                    {
                        return;
                    }

                    if (timer != null)
                    {
                        timer.Dispose();
                    }

                    converted = cp.Convert(bmp, discoveryform.LogLine);
                }

                if (converted && cp.RemoveInputFile)         // if remove, delete original picture
                {
                    ScreenshotTimers.TryRemove(filename, out timer);

                    try
                    {
                        File.Delete(filename);
                    }
                    catch
                    {
                        System.Diagnostics.Trace.WriteLine($"Unable to remove file {filename}");
                    }
                }

                this.OnScreenshot?.Invoke(cp);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Exception watcher: " + ex.Message);
                System.Diagnostics.Trace.WriteLine("Trace: " + ex.StackTrace);

                discoveryform.LogLineHighlight("Error in executing image conversion, try another screenshot, check output path settings. (Exception " + ex.Message + ")");
            }
        }
Beispiel #11
0
        public static HistoryEntry FromJournalEntry(JournalEntry je, HistoryEntry prev, out bool journalupdate, SQLiteConnectionSystem conn = null, EDCommander cmdr = null)
        {
            ISystem isys    = prev == null ? new SystemClass("Unknown") : prev.System;
            int     indexno = prev == null ? 1 : prev.Indexno + 1;

            int mapcolour = 0;

            journalupdate = false;
            bool starposfromedsm = false;
            bool firstdiscover   = false;

            if (je.EventTypeID == JournalTypeEnum.Location || je.EventTypeID == JournalTypeEnum.FSDJump)
            {
                JournalLocOrJump jl = je as JournalLocOrJump;

                ISystem newsys;

                if (jl != null && jl.HasCoordinate)       // LAZY LOAD IF it has a co-ord.. the front end will when it needs it
                {
                    newsys = new SystemClass(jl.StarSystem, jl.StarPos.X, jl.StarPos.Y, jl.StarPos.Z)
                    {
                        EDSMID         = jl.EdsmID < 0 ? 0 : jl.EdsmID, // pass across the EDSMID for the lazy load process.
                        Faction        = jl.Faction,
                        Government     = jl.EDGovernment,
                        PrimaryEconomy = jl.EDEconomy,
                        Security       = jl.EDSecurity,
                        Population     = jl.Population ?? 0,
                        State          = jl.EDState,
                        Allegiance     = jl.EDAllegiance,
                        UpdateDate     = jl.EventTimeUTC,
                        status         = SystemStatusEnum.EDDiscovery,
                        SystemAddress  = jl.SystemAddress,
                    };

                    // If it was a new system, pass the coords back to the StartJump
                    if (prev != null && prev.journalEntry is JournalStartJump)
                    {
                        prev.System = newsys;       // give the previous startjump our system..
                    }
                }
                else
                {
                    // NOTE Rob: 09-JAN-2018 I've removed the Jumpstart looking up a system by name since they were using up lots of lookup time during history reading.
                    // This is used for pre 2.2 systems without co-ords, which now should be limited.
                    // JumpStart still gets the system when the FSD loc is processed, see above.
                    // Jumpstart was also screwing about with the EDSM ID fill in which was broken.  This is now working again.

                    // Default one
                    newsys        = new SystemClass(jl.StarSystem);
                    newsys.EDSMID = je.EdsmID;

                    ISystem s = SystemCache.FindSystem(newsys, conn); // has no co-ord, did we find it?

                    if (s != null)                                    // found a system..
                    {
                        if (jl != null && jl.HasCoordinate)           // if journal Loc, and journal has a star position, use that instead of EDSM..
                        {
                            s.X = Math.Round(jl.StarPos.X * 32.0) / 32.0;
                            s.Y = Math.Round(jl.StarPos.Y * 32.0) / 32.0;
                            s.Z = Math.Round(jl.StarPos.Z * 32.0) / 32.0;
                        }

                        //Debug.WriteLine("HistoryList found system {0} {1}", s.id_edsm, s.name);
                        newsys = s;

                        if (jl != null && je.EdsmID <= 0 && newsys.EDSMID > 0) // only update on a JL..
                        {
                            journalupdate = true;
                            Debug.WriteLine("HE EDSM ID update requested {0} {1}", newsys.EDSMID, newsys.Name);
                        }
                    }
                    else
                    {
                        newsys.EDSMID = -1;        // mark as checked but not found
                    }
                }

                JournalFSDJump jfsd = je as JournalFSDJump;

                if (jfsd != null)
                {
                    if (jfsd.JumpDist <= 0 && isys.HasCoordinate && newsys.HasCoordinate) // if no JDist, its a really old entry, and if previous has a co-ord
                    {
                        jfsd.JumpDist = isys.Distance(newsys);                            // fill it out here

                        if (jfsd.JumpDist > 0)
                        {
                            journalupdate = true;
                            Debug.WriteLine("Je Jump distance update(3) requested {0} {1} {2}", newsys.EDSMID, newsys.Name, jfsd.JumpDist);
                        }
                    }

                    mapcolour = jfsd.MapColor;
                }

                isys            = newsys;
                starposfromedsm = (jl != null && jl.HasCoordinate) ? jl.StarPosFromEDSM : newsys.HasCoordinate;
                firstdiscover   = jl == null ? false : jl.EDSMFirstDiscover;
            }

            string summary, info, detailed;

            je.FillInformation(out summary, out info, out detailed);

            HistoryEntry he = new HistoryEntry
            {
                Indexno             = indexno,
                EntryType           = je.EventTypeID,
                Journalid           = je.Id,
                journalEntry        = je,
                System              = isys,
                EventTimeUTC        = je.EventTimeUTC,
                MapColour           = mapcolour,
                EdsmSync            = je.SyncedEDSM,
                EDDNSync            = je.SyncedEDDN,
                EGOSync             = je.SyncedEGO,
                StartMarker         = je.StartMarker,
                StopMarker          = je.StopMarker,
                EventSummary        = summary,
                EventDescription    = info,
                EventDetailedInfo   = detailed,
                IsStarPosFromEDSM   = starposfromedsm,
                IsEDSMFirstDiscover = firstdiscover,
                Commander           = cmdr ?? EDCommander.GetCommander(je.CommanderId)
            };


            // WORK out docked/landed state

            if (prev != null)
            {
                if (prev.docked.HasValue)                   // copy docked..
                {
                    he.docked = prev.docked;
                }
                if (prev.landed.HasValue)
                {
                    he.landed = prev.landed;
                }
                if (prev.hyperspace.HasValue)
                {
                    he.hyperspace = prev.hyperspace;
                }
                if (prev.marketId != null)
                {
                    he.marketId = prev.marketId;
                }
                if (prev.wanted.HasValue)
                {
                    he.wanted = prev.wanted;
                }

                he.stationName       = prev.stationName;
                he.shiptype          = prev.shiptype;
                he.shipid            = prev.shipid;
                he.whereami          = prev.whereami;
                he.onCrewWithCaptain = prev.onCrewWithCaptain;
                he.gamemode          = prev.gamemode;
                he.group             = prev.group;
            }

            if (je.EventTypeID == JournalTypeEnum.Location)
            {
                JournalLocation jl = je as JournalLocation;
                he.docked     = jl.Docked;
                he.landed     = jl.Latitude.HasValue;
                he.whereami   = jl.Docked ? jl.StationName : jl.Body;
                he.hyperspace = false;
                he.wanted     = jl.Wanted;
            }
            else if (je.EventTypeID == JournalTypeEnum.Docked)
            {
                JournalDocked jl = je as JournalDocked;
                he.docked      = true;
                he.whereami    = jl.StationName;
                he.stationName = jl.StationName;
                he.marketId    = jl.MarketID;
            }
            else if (je.EventTypeID == JournalTypeEnum.Undocked)
            {
                he.docked      = false;
                he.stationName = null;
                he.marketId    = null;
            }
            else if (je.EventTypeID == JournalTypeEnum.Touchdown)
            {
                he.landed = true;
            }
            else if (je.EventTypeID == JournalTypeEnum.Liftoff)
            {
                he.landed = !(je as JournalLiftoff).PlayerControlled;
            }
            else if (je.EventTypeID == JournalTypeEnum.SupercruiseEntry)
            {
                he.whereami   = (je as JournalSupercruiseEntry).StarSystem;
                he.hyperspace = true;
            }
            else if (je.EventTypeID == JournalTypeEnum.SupercruiseExit)
            {
                he.whereami   = (je as JournalSupercruiseExit).Body;
                he.hyperspace = false;
            }
            else if (je.EventTypeID == JournalTypeEnum.FSDJump)
            {
                JournalFSDJump ju = (je as JournalFSDJump);
                he.whereami   = ju.StarSystem;
                he.hyperspace = true;
                he.wanted     = ju.Wanted;
            }
            else if (je.EventTypeID == JournalTypeEnum.StartJump)
            {
                he.hyperspace = true;   // some of these are just to make sure, as FSDJump will also set it
            }
            else if (je.EventTypeID == JournalTypeEnum.LoadGame)
            {
                JournalLoadGame jl = je as JournalLoadGame;

                he.onCrewWithCaptain = null;        // can't be in a crew at this point
                he.gamemode          = jl.GameMode; // set game mode
                he.group             = jl.Group;    // and group, may be empty
                he.landed            = jl.StartLanded;
                he.hyperspace        = false;

                if (jl.Ship.IndexOf("buggy", StringComparison.InvariantCultureIgnoreCase) == -1)        // load game with buggy, can't tell what ship we get back into, so ignore
                {
                    he.shiptype = (je as JournalLoadGame).Ship;
                    he.shipid   = (je as JournalLoadGame).ShipId;
                }
            }
            else if (je.EventTypeID == JournalTypeEnum.ShipyardBuy)         // BUY does not have ship id, but the new entry will that is written later - journals 8.34
            {
                he.shiptype = (je as JournalShipyardBuy).ShipType;
            }
            else if (je.EventTypeID == JournalTypeEnum.ShipyardNew)
            {
                he.shiptype = (je as JournalShipyardNew).ShipType;
                he.shipid   = (je as JournalShipyardNew).ShipId;
            }
            else if (je.EventTypeID == JournalTypeEnum.ShipyardSwap)
            {
                he.shiptype = (je as JournalShipyardSwap).ShipType;
                he.shipid   = (je as JournalShipyardSwap).ShipId;
            }
            else if (je.EventTypeID == JournalTypeEnum.JoinACrew)
            {
                he.onCrewWithCaptain = (je as JournalJoinACrew).Captain;
            }
            else if (je.EventTypeID == JournalTypeEnum.QuitACrew)
            {
                he.onCrewWithCaptain = null;
            }

            if (prev != null && prev.travelling)      // if we are travelling..
            {
                he.travelled_distance    = prev.travelled_distance;
                he.travelled_missingjump = prev.travelled_missingjump;
                he.travelled_jumps       = prev.travelled_jumps;

                if (he.IsFSDJump && !he.MultiPlayer)   // if jump, and not multiplayer..
                {
                    double dist = ((JournalFSDJump)je).JumpDist;
                    if (dist <= 0)
                    {
                        he.travelled_missingjump++;
                    }
                    else
                    {
                        he.travelled_distance += dist;
                        he.travelled_jumps++;
                    }
                }

                he.travelled_seconds = prev.travelled_seconds;
                TimeSpan diff = he.EventTimeUTC.Subtract(prev.EventTimeUTC);

                if (he.EntryType != JournalTypeEnum.LoadGame && diff < new TimeSpan(2, 0, 0))   // time between last entry and load game is not real time
                {
                    he.travelled_seconds += diff;
                }

                if (he.StopMarker || he.StartMarker)
                {
                    //Debug.WriteLine("Travelling stop at " + he.Indexno);
                    he.travelling         = false;
                    he.EventDetailedInfo += ((he.EventDetailedInfo.Length > 0) ? Environment.NewLine : "") + "Travelled " + he.travelled_distance.ToStringInvariant("0.0") + " LY"
                                            + ", " + he.travelled_jumps + " jumps"
                                            + ((he.travelled_missingjump > 0) ? ", " + he.travelled_missingjump + " unknown distance jumps" : "") +
                                            ", time " + he.travelled_seconds;

                    he.travelled_distance = 0;
                    he.travelled_seconds  = new TimeSpan(0);
                }
                else
                {
                    he.travelling = true;

                    if (he.IsFSDJump)
                    {
                        he.EventDetailedInfo += ((he.EventDetailedInfo.Length > 0) ? Environment.NewLine : "") + "Travelling" +
                                                " distance " + he.travelled_distance.ToString("0.0") + " LY"
                                                + ", " + he.travelled_jumps + " jumps"
                                                + ((he.travelled_missingjump > 0) ? ", " + he.travelled_missingjump + " unknown distance jumps" : "") +
                                                ", time " + he.travelled_seconds;
                    }
                }
            }

            if (he.StartMarker)
            {
                //Debug.WriteLine("Travelling start at " + he.Indexno);
                he.travelling = true;
            }

            return(he);
        }
Beispiel #12
0
        static public void ParseFiles(string datapath, out string error, int defaultMapColour,
                                      Func <bool> cancelRequested, Action <int, string> updateProgress,
                                      bool forceReload, int currentcmdrid)
        {
            error = null;

            if (datapath == null)
            {
                error = "Netlog directory not set!";
                return;
            }

            if (!Directory.Exists(datapath))   // if logfiles directory is not found
            {
                error = "Netlog directory is not present!";
                return;
            }

            // list of systems in journal, sorted by time
            List <JournalLocOrJump> vsSystemsEnts = JournalEntry.GetAll(currentcmdrid).OfType <JournalLocOrJump>().OrderBy(j => j.EventTimeUTC).ToList();

            // 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>();
            List <TravelLogUnit>    tlutoadd        = new List <TravelLogUnit>();

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

                var reader = OpenFileReader(fi.FullName, currentcmdrid);

                if (reader.ID == 0)            // if not present, add to commit add list
                {
                    tlutoadd.Add(reader.TravelLogUnit);
                }

                if (forceReload)        // Force a reload of the travel log
                {
                    reader.Pos = 0;
                }

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

            if (tlutoadd.Count > 0)                      // now, on spinning rust, this takes ages for 600+ log files first time, so transaction it
            {
                UserDatabase.Instance.ExecuteWithDatabase(cn =>
                {
                    using (DbTransaction txn = cn.Connection.BeginTransaction())
                    {
                        foreach (var tlu in tlutoadd)
                        {
                            tlu.Add(cn.Connection, txn);
                        }

                        txn.Commit();
                    }
                });
            }

            for (int i = 0; i < readersToUpdate.Count; i++)
            {
                UserDatabase.Instance.ExecuteWithDatabase(cn =>
                {
                    int ji = 0;

                    NetLogFileReader reader = readersToUpdate[i];
                    updateProgress(i * 100 / readersToUpdate.Count, reader.TravelLogUnit.FullName);

                    var systems = JournalEntry.GetAllByTLU(reader.ID, cn.Connection).OfType <JournalLocOrJump>().ToList();
                    var last    = systems.LastOrDefault();  // find last system recorded for this TLU, may be null if no systems..

                    using (DbTransaction tn = cn.Connection.BeginTransaction())
                    {
                        var ienum = reader.ReadSystems(last, cancelRequested, currentcmdrid);
                        System.Diagnostics.Debug.WriteLine("Scanning TLU " + reader.ID + " " + reader.FullName);

                        foreach (JObject jo in ienum)
                        {
                            jo["EDDMapColor"] = defaultMapColour;

                            JournalLocOrJump je = new JournalFSDJump(jo);
                            je.SetTLUCommander(reader.TravelLogUnit.ID, currentcmdrid);

                            while (ji < vsSystemsEnts.Count && vsSystemsEnts[ji].EventTimeUTC < je.EventTimeUTC)
                            {
                                ji++;   // move to next entry which is bigger in time or equal to ours.
                            }

                            JournalLocOrJump prev = (ji > 0 && (ji - 1) < vsSystemsEnts.Count) ? vsSystemsEnts[ji - 1] : null;
                            JournalLocOrJump next = ji < vsSystemsEnts.Count ? vsSystemsEnts[ji] : null;

                            bool previssame = (prev != null && prev.StarSystem.Equals(je.StarSystem, StringComparison.CurrentCultureIgnoreCase) && (!prev.HasCoordinate || !je.HasCoordinate || (prev.StarPos - je.StarPos).LengthSquared < 0.01));
                            bool nextissame = (next != null && next.StarSystem.Equals(je.StarSystem, StringComparison.CurrentCultureIgnoreCase) && (!next.HasCoordinate || !je.HasCoordinate || (next.StarPos - je.StarPos).LengthSquared < 0.01));

                            // System.Diagnostics.Debug.WriteLine("{0} {1} {2}", ji, vsSystemsEnts[ji].EventTimeUTC, je.EventTimeUTC);

                            if (!(previssame || nextissame))
                            {
                                je.Add(jo, cn.Connection, tn);
                                System.Diagnostics.Debug.WriteLine("Add {0} {1}", je.EventTimeUTC, jo.ToString());
                            }
                        }

                        reader.TravelLogUnit.Update(cn.Connection, tn);

                        tn.Commit();
                    }

                    if (updateProgress != null)
                    {
                        updateProgress((i + 1) * 100 / readersToUpdate.Count, reader.TravelLogUnit.FullName);
                    }
                });
            }
        }
Beispiel #13
0
        static public void ParseFiles(string datapath, out string error, int defaultMapColour, Func <bool> cancelRequested, Action <int, string> updateProgress, bool forceReload = false, Dictionary <string, NetLogFileReader> netlogreaders = null, int currentcmdrid = -1)
        {
            error = null;

            if (datapath == null)
            {
                error = "Netlog directory not set!";
                return;
            }

            if (!Directory.Exists(datapath))   // if logfiles directory is not found
            {
                error = "Netlog directory is not present!";
                return;
            }

            if (netlogreaders == null)
            {
                netlogreaders = new Dictionary <string, NetLogFileReader>();
            }

            if (currentcmdrid < 0)
            {
                currentcmdrid = EDCommander.CurrentCmdrID;
            }

            // TLUs
            List <TravelLogUnit> tlus = TravelLogUnit.GetAll();
            Dictionary <string, TravelLogUnit>            netlogtravelogUnits  = tlus.Where(t => t.type == 1).GroupBy(t => t.Name).Select(g => g.First()).ToDictionary(t => t.Name);
            Dictionary <long, string>                     travellogunitid2name = netlogtravelogUnits.Values.ToDictionary(t => t.id, t => t.Name);
            Dictionary <string, List <JournalLocOrJump> > vsc_lookup           = JournalEntry.GetAll().OfType <JournalLocOrJump>().GroupBy(v => v.TLUId).Where(g => travellogunitid2name.ContainsKey(g.Key)).ToDictionary(g => travellogunitid2name[g.Key], g => g.ToList());

            // list of systems in journal, sorted by time
            List <JournalLocOrJump> vsSystemsEnts = JournalEntry.GetAll(currentcmdrid).OfType <JournalLocOrJump>().OrderBy(j => j.EventTimeUTC).ToList();

            // 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, netlogtravelogUnits, vsc_lookup, netlogreaders);

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

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

                if (forceReload)
                {
                    // Force a reload of the travel log
                    reader.TravelLogUnit.Size = 0;
                }

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

            for (int i = 0; i < readersToUpdate.Count; i++)
            {
                using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true))
                {
                    int ji = 0;

                    NetLogFileReader reader = readersToUpdate[i];
                    updateProgress(i * 100 / readersToUpdate.Count, reader.TravelLogUnit.Name);

                    using (DbTransaction tn = cn.BeginTransaction())
                    {
                        foreach (JObject jo in reader.ReadSystems(cancelRequested, currentcmdrid))
                        {
                            jo["EDDMapColor"] = defaultMapColour;

                            JournalLocOrJump je = new JournalFSDJump(jo);
                            je.SetTLUCommander(reader.TravelLogUnit.id, currentcmdrid);

                            while (ji < vsSystemsEnts.Count && vsSystemsEnts[ji].EventTimeUTC < je.EventTimeUTC)
                            {
                                ji++;   // move to next entry which is bigger in time or equal to ours.
                            }

                            JournalLocOrJump prev = (ji > 0 && (ji - 1) < vsSystemsEnts.Count) ? vsSystemsEnts[ji - 1] : null;
                            JournalLocOrJump next = ji < vsSystemsEnts.Count ? vsSystemsEnts[ji] : null;

                            bool previssame = (prev != null && prev.StarSystem.Equals(je.StarSystem, StringComparison.CurrentCultureIgnoreCase) && (!prev.HasCoordinate || !je.HasCoordinate || (prev.StarPos - je.StarPos).LengthSquared < 0.01));
                            bool nextissame = (next != null && next.StarSystem.Equals(je.StarSystem, StringComparison.CurrentCultureIgnoreCase) && (!next.HasCoordinate || !je.HasCoordinate || (next.StarPos - je.StarPos).LengthSquared < 0.01));

                            // System.Diagnostics.Debug.WriteLine("{0} {1} {2}", ji, vsSystemsEnts[ji].EventTimeUTC, je.EventTimeUTC);

                            if (!(previssame || nextissame))
                            {
                                je.Add(jo, cn, tn);
                                System.Diagnostics.Debug.WriteLine("Add {0} {1}", je.EventTimeUTC, jo.ToString());
                            }
                        }

                        tn.Commit();

                        reader.TravelLogUnit.Update();
                    }

                    if (updateProgress != null)
                    {
                        updateProgress((i + 1) * 100 / readersToUpdate.Count, reader.TravelLogUnit.Name);
                    }
                }
            }
        }
        public static HistoryEntry FromJournalEntry(JournalEntry je, HistoryEntry prev)
        {
            ISystem isys    = prev == null ? new SystemClass("Unknown") : prev.System;
            int     entryno = prev == null ? 1 : prev.EntryNumber + 1;

            if (je.EventTypeID == JournalTypeEnum.Location || je.EventTypeID == JournalTypeEnum.FSDJump || je.EventTypeID == JournalTypeEnum.CarrierJump)
            {
                JournalLocOrJump jl = je as JournalLocOrJump;

                ISystem newsys;

                if (jl != null && jl.HasCoordinate)       // LAZY LOAD IF it has a co-ord.. the front end will when it needs it
                {
                    newsys = new SystemClass(jl.StarSystem, jl.StarPos.X, jl.StarPos.Y, jl.StarPos.Z)
                    {
                        EDSMID         = 0, // not an EDSM entry
                        SystemAddress  = jl.SystemAddress,
                        Population     = jl.Population ?? 0,
                        Faction        = jl.Faction,
                        Government     = jl.EDGovernment,
                        Allegiance     = jl.EDAllegiance,
                        State          = jl.EDState,
                        Security       = jl.EDSecurity,
                        PrimaryEconomy = jl.EDEconomy,
                        Power          = jl.PowerList,
                        PowerState     = jl.PowerplayState,
                        Source         = jl.StarPosFromEDSM ? SystemSource.FromEDSM : SystemSource.FromJournal,
                    };

                    SystemCache.FindCachedJournalSystem(newsys);        // this puts it in the cache

                    // If it was a new system, pass the coords back to the StartJump
                    if (prev != null && prev.journalEntry is JournalStartJump)
                    {
                        prev.System = newsys;       // give the previous startjump our system..
                    }
                }
                else
                {
                    // NOTE Rob: 09-JAN-2018 I've removed the Jumpstart looking up a system by name since they were using up lots of lookup time during history reading.
                    // This is used for pre 2.2 systems without co-ords, which now should be limited.
                    // JumpStart still gets the system when the FSD loc is processed, see above.
                    // Jumpstart was also screwing about with the EDSM ID fill in which was broken.  This is now working again.

                    newsys = new SystemClass(jl.StarSystem);         // this will be a synthesised one
                }

                isys = newsys;
            }

            HistoryEntry he = new HistoryEntry
            {
                EntryNumber  = entryno,
                journalEntry = je,
                System       = isys,
                EntryStatus  = HistoryEntryStatus.Update(prev?.EntryStatus, je, isys.Name)
            };

            he.TravelStatus = HistoryTravelStatus.Update(prev?.TravelStatus, prev, he);     // need a real he so can't do that as part of the constructor.

            return(he);
        }