public bool ReadJournalLog(out JournalEntry jent, bool resetOnError = false) { while (ReadLine(out jent, l => ProcessLine(l, resetOnError))) { if (jent == null) continue; //System.Diagnostics.Trace.WriteLine(string.Format("Read line {0} from {1}", line, this.FileName)); return true; } jent = null; return false; }
public static void ReadCmdLineJournal(string file) //DEBUG ONLY FROM COMMAND LINE { System.IO.StreamReader filejr = new System.IO.StreamReader(file); string line; string system = ""; StarScan ss = new StarScan(); Dictionary <string, string> items = new Dictionary <string, string>(); while ((line = filejr.ReadLine()) != null) { if (line.Equals("END")) { break; } //System.Diagnostics.Trace.WriteLine(line); if (line.Length > 0) { JObject jo = (JObject)JObject.Parse(line); //JSONPrettyPrint jpp = new JSONPrettyPrint(EliteDangerous.JournalFieldNaming.StandardConverters(), "event;timestamp", "_Localised", (string)jo["event"]); //string s = jpp.PrettyPrintStr(line, 80); //System.Diagnostics.Trace.WriteLine(s); EliteDangerous.JournalEntry je = EliteDangerous.JournalEntry.CreateJournalEntry(line); //System.Diagnostics.Trace.WriteLine(je.EventTypeStr); if (je.EventTypeID == JournalTypeEnum.Location) { EDDiscovery.EliteDangerous.JournalEvents.JournalLocOrJump jl = je as EDDiscovery.EliteDangerous.JournalEvents.JournalLocOrJump; system = jl.StarSystem; } else if (je.EventTypeID == JournalTypeEnum.FSDJump) { EDDiscovery.EliteDangerous.JournalEvents.JournalFSDJump jfsd = je as EDDiscovery.EliteDangerous.JournalEvents.JournalFSDJump; system = jfsd.StarSystem; } else if (je.EventTypeID == JournalTypeEnum.Scan) { ss.Process(je as EliteDangerous.JournalEvents.JournalScan, new SystemClassDB(system)); } } } }
public void NewEntry(EliteDangerous.JournalEntry je) // hooked into journal monitor and receives new entries.. Also call if you programatically add an entry { if (je.CommanderId == history.CommanderId) // we are only interested at this point accepting ones for the display commander { foreach (HistoryEntry he in history.AddJournalEntry(je, h => LogLineHighlight(h))) { { OnNewEntry?.Invoke(he, history); // major hook OnNewEntrySecond?.Invoke(he, history); // secondary hook.. } } } OnNewJournalEntry?.Invoke(je); if (je.EventTypeID == JournalTypeEnum.LoadGame) { OnRefreshCommanders?.Invoke(); } }
public void ProcessWithUserDb(EliteDangerous.JournalEntry je, HistoryEntry prev, SQLiteConnectionUser conn) // called after above with a USER connection { materialscommodities = MaterialCommoditiesList.Process(je, prev?.materialscommodities, conn, EDDiscoveryForm.EDDConfig.ClearMaterials, EDDiscoveryForm.EDDConfig.ClearCommodities); }
public static HistoryEntry FromJournalEntry(EliteDangerous.JournalEntry je, HistoryEntry prev, bool checkedsm, out bool journalupdate, SQLiteConnectionSystem conn = 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; if (je.EventTypeID == EliteDangerous.JournalTypeEnum.Location || je.EventTypeID == EliteDangerous.JournalTypeEnum.FSDJump) { EDDiscovery.EliteDangerous.JournalEvents.JournalLocOrJump jl = je as EDDiscovery.EliteDangerous.JournalEvents.JournalLocOrJump; EDDiscovery.EliteDangerous.JournalEvents.JournalFSDJump jfsd = je as EDDiscovery.EliteDangerous.JournalEvents.JournalFSDJump; ISystem newsys; if (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); newsys.id_edsm = jl.EdsmID < 0 ? 0 : jl.EdsmID; // pass across the EDSMID for the lazy load process. if (jfsd != null && jfsd.JumpDist <= 0 && isys.HasCoordinate) // if we don't have a jump distance (pre 2.2) but the last sys does, we can compute { jfsd.JumpDist = SystemClass.Distance(isys, newsys); // fill it out here journalupdate = true; } } else { // Default one newsys = new SystemClass(jl.StarSystem); newsys.id_edsm = jl.EdsmID; if (checkedsm) // see if we can find the right system { SystemClass s = SystemClass.FindEDSM(newsys, conn); // 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.. 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 = SystemClass.Distance(isys, newsys); // fill it out here. EDSM systems always have co-ords, but we should check anyway journalupdate = true; } if (jl.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 = SystemClass.Distance(isys, newsys); // fill it out here journalupdate = true; } mapcolour = jfsd.MapColor; } isys = newsys; starposfromedsm = jl.HasCoordinate ? jl.StarPosFromEDSM : newsys.HasCoordinate; } 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, StartMarker = je.StartMarker, StopMarker = je.StopMarker, EventSummary = summary, EventDescription = info, EventDetailedInfo = detailed, IsStarPosFromEDSM = starposfromedsm }; if (prev != null && prev.travelling) // if we are travelling.. { he.travelled_distance = prev.travelled_distance; he.travelled_missingjump = prev.travelled_missingjump; if (he.IsFSDJump) { double dist = ((EliteDangerous.JournalEvents.JournalFSDJump)je).JumpDist; if (dist <= 0) { he.travelled_missingjump++; } else { he.travelled_distance += dist; } } he.travelled_seconds = prev.travelled_seconds; TimeSpan diff = he.EventTimeUTC.Subtract(prev.EventTimeUTC); if (he.EntryType != EliteDangerous.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) { he.travelling = false; he.EventDetailedInfo += ((he.EventDetailedInfo.Length > 0) ? Environment.NewLine : "") + "Travelled " + he.travelled_distance.ToString("0.0") + ((he.travelled_missingjump > 0) ? " LY(" + he.travelled_missingjump + " unknown distance jumps)" : " LY") + " time " + he.travelled_seconds; he.travelled_distance = 0; he.travelled_seconds = new TimeSpan(0); } else { he.travelling = true; he.EventDetailedInfo += ((he.EventDetailedInfo.Length > 0) ? Environment.NewLine : "") + "Travelling"; if (he.IsFSDJump) { he.EventDetailedInfo += " distance " + he.travelled_distance.ToString("0.0") + ((he.travelled_missingjump > 0) ? " LY (*)" : " LY"); } he.EventDetailedInfo += " time " + he.travelled_seconds; } } if (he.StartMarker) { he.travelling = true; } // 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; } he.wheredocked = prev.wheredocked; } if (je.EventTypeID == JournalTypeEnum.Location) { EliteDangerous.JournalEvents.JournalLocation jl = je as EliteDangerous.JournalEvents.JournalLocation; he.docked = jl.Docked; he.wheredocked = jl.Docked ? jl.StationName : ""; } else if (je.EventTypeID == JournalTypeEnum.Docked) { EliteDangerous.JournalEvents.JournalDocked jl = je as EliteDangerous.JournalEvents.JournalDocked; he.docked = true; he.wheredocked = 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.LoadGame) { he.landed = (je as EliteDangerous.JournalEvents.JournalLoadGame).StartLanded; } return(he); }
protected 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) { EDCommander onlyc = EDCommander.GetAll().FirstOrDefault(); if (EDCommander.NumberOfCommanders == 1 && onlyc != null && onlyc.Name == "Jameson (Default)") { onlyc.Name = newname; onlyc.EdsmName = newname; EDCommander.Update(new List <EDCommander> { onlyc }, 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)); } } je.TLUId = (int)TravelLogUnit.id; je.CommanderId = cmdrid; return(new JournalReaderEntry { JournalEntry = je, Json = jo }); }
public void ParseJournalFiles(Func <bool> cancelRequested, Action <int, string> updateProgress, bool forceReload = false) { System.Diagnostics.Trace.WriteLine("Scanned " + m_watcherfolder); Dictionary <string, TravelLogUnit> m_travelogUnits = TravelLogUnit.GetAll().Where(t => (t.type & 0xFF) == 3).GroupBy(t => t.Name).Select(g => g.First()).ToDictionary(t => t.Name); // order by file write time so we end up on the last one written FileInfo[] allFiles = Directory.EnumerateFiles(m_watcherfolder, "Journal.*.log", SearchOption.AllDirectories).Select(f => new FileInfo(f)).OrderBy(p => p.LastWriteTime).ToArray(); List <EDJournalReader> readersToUpdate = new List <EDJournalReader>(); for (int i = 0; i < allFiles.Length; i++) { FileInfo fi = allFiles[i]; var reader = OpenFileReader(fi, m_travelogUnits); if (!m_travelogUnits.ContainsKey(reader.TravelLogUnit.Name)) { m_travelogUnits[reader.TravelLogUnit.Name] = reader.TravelLogUnit; reader.TravelLogUnit.type = 3; 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)) { EDJournalReader reader = readersToUpdate[i]; updateProgress(i * 100 / readersToUpdate.Count, reader.TravelLogUnit.Name); List <JournalReaderEntry> entries = reader.ReadJournalLog(true).ToList(); // this may create new commanders, and may write to the TLU db ILookup <DateTime, JournalEntry> existing = JournalEntry.GetAllByTLU(reader.TravelLogUnit.id).ToLookup(e => e.EventTimeUTC); using (DbTransaction tn = cn.BeginTransaction()) { foreach (JournalReaderEntry jre in entries) { if (!existing[jre.JournalEntry.EventTimeUTC].Any(e => JournalEntry.AreSameEntry(jre.JournalEntry, e, ent1jo: jre.Json))) { System.Diagnostics.Trace.WriteLine(string.Format("Write Journal to db {0} {1}", jre.JournalEntry.EventTimeUTC, jre.JournalEntry.EventTypeStr)); jre.JournalEntry.Add(jre.Json, cn, tn); } } tn.Commit(); } reader.TravelLogUnit.Update(cn); updateProgress((i + 1) * 100 / readersToUpdate.Count, reader.TravelLogUnit.Name); lastnfi = reader; } } updateProgress(-1, ""); }
private void FetcherThreadProc() { bool jupdate = false; LastEventTime = DateTime.UtcNow; FirstEventTime = LastEventTime; int waittime = 2000; // Max 1 request every 2 seconds, with a backoff if the rate limit is hit if (EDSMRequestBackoffTime > DateTime.UtcNow) { waittime = (int)Math.Min(EDSMMaxLogAgeMinutes * 60000, Math.Min(BackoffInterval.TotalSeconds * 1000, EDSMRequestBackoffTime.Subtract(DateTime.UtcNow).TotalSeconds * 1000)); } while (!ExitRequested.WaitOne(waittime)) { EDSMClass edsm = new EDSMClass { apiKey = Commander.APIKey, commanderName = Commander.EdsmName }; List <HistoryEntry> edsmlogs = null; DateTime logstarttime = DateTime.MinValue; DateTime logendtime = DateTime.MinValue; int res = -1; if (edsm.IsApiKeySet && Commander.SyncFromEdsm && DateTime.UtcNow > EDSMRequestBackoffTime) { if (DateTime.UtcNow.Subtract(LastEventTime).TotalMinutes >= EDSMMaxLogAgeMinutes) { Trace.WriteLine($"Retrieving logs starting {LastEventTime}"); res = edsm.GetLogs(LastEventTime, null, out edsmlogs, out logstarttime, out logendtime); } else if (FirstEventTime > GammaStart) { Trace.WriteLine($"Retrieving logs ending {FirstEventTime}"); res = edsm.GetLogs(null, FirstEventTime, out edsmlogs, out logstarttime, out logendtime); } } if (ExitRequested.WaitOne(0)) { return; } if (res == 429) // Rate Limit Exceeded { Trace.WriteLine($"EDSM Log request rate limit hit - backing off for {BackoffInterval.TotalSeconds}s"); EDSMRequestBackoffTime = DateTime.UtcNow + BackoffInterval; BackoffInterval = BackoffInterval + TimeSpan.FromSeconds(60); } else if (logstarttime > LastEventTime && logendtime < FirstEventTime) { Trace.WriteLine($"Bad start and/or end times returned by EDSM - backing off for {BackoffInterval.TotalSeconds}s"); EDSMRequestBackoffTime = DateTime.UtcNow + BackoffInterval; BackoffInterval = BackoffInterval + TimeSpan.FromSeconds(60); } else if (res == 100 && edsmlogs != null) { BackoffInterval = TimeSpan.FromSeconds(60); if (logendtime > DateTime.UtcNow) { logendtime = DateTime.UtcNow; } List <HistoryEntry> hlfsdlist = JournalEntry.GetAll(Commander.Nr).OfType <JournalLocOrJump>().OrderBy(je => je.EventTimeUTC).Select(je => HistoryEntry.FromJournalEntry(je, null, false, out jupdate)).ToList(); HistoryList hl = new HistoryList(hlfsdlist); List <DateTime> hlfsdtimes = hlfsdlist.Select(he => he.EventTimeUTC).ToList(); List <HistoryEntry> toadd = new List <HistoryEntry>(); int previdx = -1; foreach (HistoryEntry he in edsmlogs) // find out list of ones not present { int index = hlfsdlist.FindIndex(x => x.System.name.Equals(he.System.name, StringComparison.InvariantCultureIgnoreCase) && x.EventTimeUTC.Ticks == he.EventTimeUTC.Ticks); if (index < 0) { // Look for any entries where DST may have thrown off the time foreach (var vi in hlfsdlist.Select((v, i) => new { v = v, i = i }).Where(vi => vi.v.System.name.Equals(he.System.name, StringComparison.InvariantCultureIgnoreCase))) { if (vi.i > previdx) { double hdiff = vi.v.EventTimeUTC.Subtract(he.EventTimeUTC).TotalHours; if (hdiff >= -2 && hdiff <= 2 && hdiff == Math.Floor(hdiff)) { if (vi.v.System.id_edsm <= 0) { vi.v.System.id_edsm = 0; hl.FillEDSM(vi.v); } if (vi.v.System.id_edsm <= 0 || vi.v.System.id_edsm == he.System.id_edsm) { index = vi.i; break; } } } } } if (index < 0) { toadd.Add(he); } else { HistoryEntry lhe = hlfsdlist[index]; if (he.IsEDSMFirstDiscover && !lhe.IsEDSMFirstDiscover) { lhe.SetFirstDiscover(); } previdx = index; } } if (toadd.Count > 0) // if we have any, we can add { TravelLogUnit tlu = new TravelLogUnit(); // need a tlu for it tlu.type = 2; // EDSM tlu.Name = "EDSM-" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); tlu.Size = 0; tlu.Path = "EDSM"; tlu.CommanderId = EDCommander.CurrentCmdrID; tlu.Add(); // Add to Database using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true)) { foreach (HistoryEntry he in toadd) { JObject jo = EDDiscovery.EliteDangerous.JournalEntry.CreateFSDJournalEntryJson(he.EventTimeUTC, he.System.name, he.System.x, he.System.y, he.System.z, EDDConfig.Instance.DefaultMapColour); EDDiscovery.EliteDangerous.JournalEntry je = EDDiscovery.EliteDangerous.JournalEntry.CreateFSDJournalEntry(tlu.id, tlu.CommanderId.Value, (int)EDDiscovery.EliteDangerous.SyncFlags.EDSM, jo); System.Diagnostics.Trace.WriteLine(string.Format("Add {0} {1}", je.EventTimeUTC, he.System.name)); je.Add(jo, cn); } } LogLine($"Retrieved {toadd.Count} log entries from EDSM, from {logstarttime.ToLocalTime().ToString()} to {logendtime.ToLocalTime().ToString()}"); if (logendtime > LastEventTime || logstarttime <= GammaStart) { if (OnDownloadedSystems != null) { OnDownloadedSystems(); } } } if (logstarttime < FirstEventTime) { FirstEventTime = logstarttime; } if (logendtime > LastEventTime) { LastEventTime = logendtime; } } } }
public bool ReadJournalLog(out JournalEntry je) { 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)); } string line; while (this.ReadLine(out line)) { if (line.Length == 0) continue; //System.Diagnostics.Trace.WriteLine(string.Format("Read line {0} from {1}", line, this.FileName)); try { je = JournalEntry.CreateJournalEntry(line); if (je.EventTypeID == JournalTypeEnum.FileHeader) { JournalEvents.JournalFileHeader header = (JournalEvents.JournalFileHeader)je; if (header.Beta && !disable_beta_commander_check) { TravelLogUnit.type |= 0x8000; } } else if (je.EventTypeID == JournalTypeEnum.LoadGame) { string newname = (je as JournalEvents.JournalLoadGame).LoadGameCommander; if ((TravelLogUnit.type & 0x8000) == 0x8000) { newname = "[BETA] " + newname; } EDCommander _commander = EDDiscovery2.EDDConfig.Instance.ListOfCommanders.FirstOrDefault(c => c.Name.Equals(newname, StringComparison.InvariantCultureIgnoreCase)); if (_commander == null) _commander = EDDiscovery2.EDDConfig.Instance.GetNewCommander(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)); } } je.TLUId = (int)TravelLogUnit.id; je.CommanderId = cmdrid; return true; } catch ( Exception ) // CreateJournal Entry may except, in which case, the line is crap { } } je = null; return false; }
protected JournalEntry 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); } JournalEntry je = null; try { je = JournalEntry.CreateJournalEntry(line); } catch { System.Diagnostics.Trace.WriteLine($"Bad journal line:\n{line}"); if (resetOnError) { throw; } else { return(null); } } if (je.EventTypeID == JournalTypeEnum.Fileheader) { JournalEvents.JournalFileheader header = (JournalEvents.JournalFileheader)je; if (header.Beta && !disable_beta_commander_check) { TravelLogUnit.type |= 0x8000; } } else if (je.EventTypeID == JournalTypeEnum.LoadGame) { string newname = (je as JournalEvents.JournalLoadGame).LoadGameCommander; if ((TravelLogUnit.type & 0x8000) == 0x8000) { newname = "[BETA] " + newname; } EDCommander _commander = EDDiscovery2.EDDConfig.Instance.ListOfCommanders.FirstOrDefault(c => c.Name.Equals(newname, StringComparison.InvariantCultureIgnoreCase)); if (_commander == null) { if (EDDiscovery2.EDDConfig.Instance.ListOfCommanders.Count == 1 && EDDiscovery2.EDDConfig.Instance.ListOfCommanders[0].Name == "Jameson (Default)") { EDDiscovery2.EDDConfig.Instance.ListOfCommanders[0].Name = newname; EDDiscovery2.EDDConfig.Instance.ListOfCommanders[0].EdsmName = newname; EDDiscovery2.EDDConfig.Instance.UpdateCommanders(EDDiscovery2.EDDConfig.Instance.ListOfCommanders); // replaces it } else { _commander = EDDiscovery2.EDDConfig.Instance.GetNewCommander(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)); } } je.TLUId = (int)TravelLogUnit.id; je.CommanderId = cmdrid; return(je); }
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) { TLUId = (int)reader.TravelLogUnit.id, CommanderId = 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 List <JournalEntry> ScanForNewEntries() { var entries = new List <JournalEntry>(); int netlogpos = 0; EDJournalReader nfi = null; try { string filename = null; if (m_netLogFileQueue.TryDequeue(out filename)) // if a new one queued, we swap to using it { nfi = OpenFileReader(new FileInfo(filename)); lastnfi = nfi; System.Diagnostics.Trace.WriteLine(string.Format("Change in file, scan {0}", lastnfi.FileName)); } else if (ticksNoActivity >= 30 && (lastnfi == null || (!File.Exists(lastnfi.FileName) || lastnfi.filePos >= new FileInfo(lastnfi.FileName).Length))) { if (lastnfi == null) { Trace.Write($"No last file - scanning for journals"); } else if (!File.Exists(lastnfi.FileName)) { Trace.WriteLine($"File {lastnfi.FileName} not found - scanning for journals"); } else { Trace.WriteLine($"No activity on {lastnfi.FileName} for 60 seconds ({lastnfi.filePos} >= {new FileInfo(lastnfi.FileName).Length} - scanning for new journals"); } HashSet <string> tlunames = new HashSet <string>(TravelLogUnit.GetAllNames()); string[] filenames = Directory.EnumerateFiles(m_watcherfolder, "Journal.*.log", SearchOption.AllDirectories) .Select(s => new { name = Path.GetFileName(s), fullname = s }) .Where(s => !tlunames.Contains(s.name)) .OrderBy(s => s.name) .Select(s => s.fullname) .ToArray(); ticksNoActivity = 0; foreach (var name in filenames) { nfi = OpenFileReader(new FileInfo(name)); lastnfi = nfi; break; } } else { nfi = lastnfi; } ticksNoActivity++; if (nfi != null) { if (nfi.TravelLogUnit.id == 0) { nfi.TravelLogUnit.type = 3; nfi.TravelLogUnit.Add(); } netlogpos = nfi.TravelLogUnit.Size; List <JournalEntry> ents = nfi.ReadJournalLog().ToList(); if (ents.Count > 0) { using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true)) { using (DbTransaction txn = cn.BeginTransaction()) { ents = ents.Where(je => JournalEntry.FindEntry(je).Count == 0).ToList(); foreach (JournalEntry je in ents) { entries.Add(je); je.Add(cn, txn); ticksNoActivity = 0; } nfi.TravelLogUnit.Update(cn); txn.Commit(); } } } } return(entries); } catch (Exception ex) { // Revert and re-read the failed entries if (nfi != null && nfi.TravelLogUnit != null) { nfi.TravelLogUnit.Size = netlogpos; } System.Diagnostics.Trace.WriteLine("Net tick exception : " + ex.Message); System.Diagnostics.Trace.WriteLine(ex.StackTrace); throw; } }