/// <summary> /// OnChanged indicates that an existing journal file was changed, and triggers /// reading the new data from the journal. /// </summary> private void OnChanged(object source, FileSystemEventArgs e) { System.IO.FileInfo newJournalFile = new System.IO.FileInfo(journalFile.FullName); if (newJournalFile.Length > journalFile.Length) { JournalParser.ProcessJournal(newJournalFile, journalFile.Length, false); journalFile = newJournalFile; } }
/// <summary> /// OnCreated indicates that a new journal file was created in the directory. /// </summary> private void OnCreated(object source, FileSystemEventArgs e) { System.IO.FileInfo newJournalFile = JournalDirectory.GetFiles("Journal.*").OrderByDescending(x => x.LastWriteTime).First(); if (journalFile.FullName != newJournalFile.FullName) { Logger.Log(Severity.Debug, $"Switched to '{newJournalFile}'."); JournalParser.processedLogs.Clear(); } journalFile = newJournalFile; JournalParser.ProcessJournal(newJournalFile, 0, false); }
/// <summary> /// Resets the API. /// </summary> public void Reset() { //Reset services. try { Events = new Events.EventHandler(); } catch (Exception ex) { Logger.Log(Severity.Warning, "Couldn't instantiate service 'Events'.", ex); } try { Commander = new CommanderStatus(this); } catch (Exception ex) { Logger.Log(Severity.Warning, "Couldn't instantiate service 'Commander'.", ex); } try { Location = new LocationStatus(this); } catch (Exception ex) { Logger.Log(Severity.Warning, "Couldn't instantiate service 'Location'.", ex); } try { DiscordRichPresence = new RichPresenceClient(this); } catch (Exception ex) { Logger.Log(Severity.Warning, "Couldn't instantiate service 'DiscordRichPresence'.", ex); } try { StatusWatcher = new StatusWatcher(this); } catch (Exception ex) { Logger.Log(Severity.Warning, "Couldn't instantiate service 'StatusWatcher'.", ex); } try { CargoWatcher = new CargoWatcher(this); } catch (Exception ex) { Logger.Log(Severity.Warning, "Couldn't instantiate service 'CargoWatcher'.", ex); } try { Status = EliteAPI.Status.GameStatus.FromFile(new FileInfo(JournalDirectory + "//Status.json"), this); } catch (Exception ex) { Logger.Log(Severity.Warning, "Couldn't instantiate service 'Status'.", ex); } try { JournalParser = new JournalParser(this); } catch (Exception ex) { Logger.Log(Severity.Warning, "Couldn't instantiate service 'JournalParser'.", ex); } try { MaterialWatcher = new MaterialWatcher(this); } catch (Exception ex) { Logger.Log(Severity.Warning, "Couldn't instantiate service 'MaterialWatcher'.", ex); } JournalParser.processedLogs = new List <string>(); }
/// <summary> /// Starts the API. /// </summary> public void Start(bool runRichPresence = true) { OnError += (sender, e) => { Logger.Log(Severity.Error, e.Item1, e.Item2); Logger.Log(Severity.Warning, "EliteAPI stumbled upon a critical error and cannot continue.", new Exception("ELITEAPI TERMINATED")); }; OnReady += (sender, e) => Logger.Success("EliteAPI is ready."); Stopwatch s = new Stopwatch(); s.Start(); Logger.Log("Starting EliteAPI."); Logger.Log(Severity.Debug, "EliteAPI by CMDR Somfic (discord.gg/jwpFUPZ) (github.com/EliteAPI/EliteAPI)."); Logger.Log(Severity.Debug, "EliteAPI v" + Version + "."); //Check for updates. CheckForUpdate(); Logger.Log(Severity.Debug, "Checking journal directory."); if (!Directory.Exists(JournalDirectory.FullName)) { if (JournalDirectory.FullName != StandardDirectory.FullName) { Logger.Log(Severity.Warning, $"{JournalDirectory.Name} does not exist.", new DirectoryNotFoundException($"'{JournalDirectory.FullName}' could not be found.")); Logger.Log(Severity.Debug, "Trying standard journal directory instead."); } if (!Directory.Exists(EliteDangerousAPI.StandardDirectory.FullName)) { OnError?.Invoke(this, new Tuple <string, Exception>( "The default journal directory does not exist on this machine. This error usually occurs when Elite: Dangerous hasn't been run on this machine yet.", new DirectoryNotFoundException($"'{StandardDirectory.FullName}' could not be found."))); return; } JournalDirectory = StandardDirectory; } Logger.Log($"Journal directory set to '{JournalDirectory}'."); //Mark the API as running. IsRunning = true; //We'll process the journal one time first, to catch up. //Select the last edited Journal file. FileInfo journalFile; //Find the last edited Journal file. try { Logger.Log(Severity.Debug, $"Searching for 'Journal.*.log' files."); journalFile = JournalDirectory.GetFiles("Journal.*").OrderByDescending(x => x.LastWriteTime).First(); Logger.Log(Severity.Debug, $"Found '{journalFile}'."); } catch (Exception ex) { IsRunning = false; OnError?.Invoke(this, new Tuple <string, Exception>($"Could not find Journal files in '{JournalDirectory}'.", ex)); return; } //Check for the support JSON files. bool foundStatus = false; try { //Status.json. if (File.Exists(JournalDirectory.FullName + "\\Status.json")) { Logger.Log(Severity.Debug, "Found 'Status.json'."); foundStatus = true; } else { Logger.Log(Severity.Warning, $"Could not find 'Status.json' file."); foundStatus = false; } //Cargo.json. if (File.Exists(JournalDirectory.FullName + "\\Cargo.json")) { Logger.Log(Severity.Debug, "Found 'Cargo.json'."); } else { Logger.Log(Severity.Warning, $"Could not find 'Cargo.json' file."); } //Shipyard.json. Logger.Log(Severity.Debug, File.Exists(JournalDirectory.FullName + "\\Shipyard.json") ? "Found 'Shipyard.json'." : $"Could not find 'Shipyard.json' file."); //Outfitting.json. Logger.Log(Severity.Debug, File.Exists(JournalDirectory.FullName + "\\Outfitting.json") ? "Found 'Outfitting.json'." : $"Could not find 'Outfitting.json' file."); //Market.json. Logger.Log(Severity.Debug, File.Exists(JournalDirectory.FullName + "\\Market.json") ? "Found 'Market.json'." : $"Could not find 'Market.json' file."); //ModulesInfo.json. if (File.Exists(JournalDirectory.FullName + "\\ModulesInfo.json")) { Logger.Log(Severity.Debug, "Found 'ModulesInfo.json'."); } else { Logger.Log(Severity.Debug, $"Could not find 'ModulesInfo.json' file."); } } catch { } if (foundStatus) { Logger.Log("Found Journal and Status files."); } else { Logger.Log(Severity.Error, "Could not find Status.json.", new FileNotFoundException("This error usually occurs when Elite: Dangerous hasn't been run on this machine yet.", $"{JournalDirectory.FullName}\\Status.json")); Logger.Log("Live updates, such as the landing gear & hardpoints, are not supported without access to 'Status.json'. The Status file is only created after the first run of Elite: Dangerous. If this is not the first time you're running Elite: Dangerous on this machine, change the journal directory."); Logger.Log(Severity.Warning, "A critical part of EliteAPI will be offline.", new Exception("PROCEEDING WITH LIMITED FUNCTIONALITY")); Logger.Log("Proceeding in 20 seconds ..."); Thread.Sleep(20000); } Reset(); //Check if Elite: Dangerous is running. if (!Status.IsRunning) { Logger.Log(Severity.Warning, "Elite: Dangerous is not in-game."); } //Process the journal file. if (!SkipCatchUp) { Logger.Log(Severity.Debug, "Catching up with past events from this session."); } JournalParser.ProcessJournal(journalFile, SkipCatchUp, false, true); if (!SkipCatchUp) { Logger.Log(Severity.Debug, "Catchup on past events completed."); } //Go async. Task.Run(() => { //Run for as long as we're running. while (IsRunning) { //Select the last edited Journal file. FileInfo newJournalFile = JournalDirectory.GetFiles("Journal.*").OrderByDescending(x => x.LastWriteTime).First(); if (journalFile.FullName != newJournalFile.FullName) { Logger.Log(Severity.Info, $"Switched to '{newJournalFile}'."); JournalParser.processedLogs.Clear(); } journalFile = newJournalFile; //Process the journal file. JournalParser.ProcessJournal(journalFile, false); //Wait half a second to avoid overusing the CPU. Thread.Sleep(500); } }); s.Stop(); Logger.Log(Severity.Debug, $"Finished in {s.ElapsedMilliseconds}ms."); IsReady = true; OnReady?.Invoke(this, EventArgs.Empty); if (runRichPresence) { //Start the Rich Presence. DiscordRichPresence.TurnOn(); } }
/// <summary> /// Starts the API. /// </summary> public void Start() { Stopwatch s = new Stopwatch(); s.Start(); Logger.LogInfo("Starting EliteAPI."); Logger.LogDebug("EliteAPI by CMDR Somfic (discord.gg/jwpFUPZ) (github.com/EliteAPI/EliteAPI)."); Logger.LogDebug("EliteAPI v" + Version + "."); //Check for updates. CheckForUpdate(); Logger.LogInfo($"Journal directory set to '{JournalDirectory}'."); //Mark the API as running. IsRunning = true; //We'll process the journal one time first, to catch up. //Select the last edited Journal file. FileInfo journalFile = null; //Find the last edited Journal file. try { Logger.LogDebug($"Searching for 'Journal.*.log' files."); journalFile = JournalDirectory.GetFiles("Journal.*").OrderByDescending(x => x.LastWriteTime).First(); Logger.LogDebug($"Found '{journalFile}'."); } catch (Exception ex) { IsRunning = false; OnError?.Invoke(this, new Tuple <string, Exception>($"Could not find Journal files in '{JournalDirectory}'", ex)); return; } //Check for the support JSON files. bool foundStatus = false; try { //Status.json. if (File.Exists(JournalDirectory.FullName + "\\Status.json")) { Logger.LogDebug("Found 'Status.json'."); foundStatus = true; } else { Logger.LogWarning($"Could not find 'Status.json' file."); foundStatus = false; } //Cargo.json. if (File.Exists(JournalDirectory.FullName + "\\Cargo.json")) { Logger.LogDebug("Found 'Cargo.json'."); } else { Logger.LogWarning($"Could not find 'Cargo.json' file."); } //Shipyard.json. if (File.Exists(JournalDirectory.FullName + "\\Shipyard.json")) { Logger.LogDebug("Found 'Shipyard.json'."); } else { Logger.LogDebug($"Could not find 'Shipyard.json' file."); } //Outfitting.json. if (File.Exists(JournalDirectory.FullName + "\\Outfitting.json")) { Logger.LogDebug("Found 'Outfitting.json'."); } else { Logger.LogDebug($"Could not find 'Outfitting.json' file."); } //Market.json. if (File.Exists(JournalDirectory.FullName + "\\Market.json")) { Logger.LogDebug("Found 'Market.json'."); } else { Logger.LogDebug($"Could not find 'Market.json' file."); } //ModulesInfo.json. if (File.Exists(JournalDirectory.FullName + "\\ModulesInfo.json")) { Logger.LogDebug("Found 'ModulesInfo.json'."); } else { Logger.LogDebug($"Could not find 'ModulesInfo.json' file."); } } catch { } if (foundStatus) { Logger.LogInfo("Found Journal and Status files."); } //Check if Elite: Dangerous is running. if (!Status.IsRunning) { Logger.LogWarning("Elite: Dangerous is not in-game."); } //Process the journal file. if (!SkipCatchUp) { Logger.LogDebug("Catching up with past events from this session."); } JournalParser.ProcessJournal(journalFile, SkipCatchUp); if (!SkipCatchUp) { Logger.LogDebug("Catchup on past events completed."); } //Go async. Task.Run(() => { //Run for as long as we're running. while (IsRunning) { //Select the last edited Journal file. FileInfo newJournalFile = JournalDirectory.GetFiles("Journal.*").OrderByDescending(x => x.LastWriteTime).First(); if (journalFile.FullName != newJournalFile.FullName) { Logger.LogDebug($"Switched to '{newJournalFile}'."); JournalParser.processedLogs.Clear(); } journalFile = newJournalFile; //Process the journal file. JournalParser.ProcessJournal(journalFile, false); //Wait half a second to avoid overusing the CPU. Thread.Sleep(500); } }); s.Stop(); Logger.LogDebug($"Finished in {s.ElapsedMilliseconds}ms."); IsReady = true; OnReady?.Invoke(this, EventArgs.Empty); }