-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJournalMonitor.cs
More file actions
80 lines (68 loc) · 1.73 KB
/
JournalMonitor.cs
File metadata and controls
80 lines (68 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using Newtonsoft.Json.Linq;
using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace ED_Router
{
public class JournalMonitor : LogMonitor
{
private static Regex JsonRegex = new Regex(@"^{.*}$");
public JournalMonitor() : base(GetSavedGamesDir(), @"^Journal.*\.[0-9\.]+\.log$")
{ }
public event Action<string> NewLocation;
public override void EventCallback(string data)
{
string location = GetLocation(data);
if(!string.IsNullOrEmpty(location))
{
//raise event
NewLocation(location);
}
}
public static string GetLocation(string line)
{
string location = "";
try
{
Match match = JsonRegex.Match(line);
if (match.Success)
{
JObject o = JObject.Parse(line);
/*dynamic ev = JObject.Parse(line);
string loca = ev.StarSystem;*/
string eventType = (string)o["event"];
switch (eventType)
{
case "StartJump":
case "FSDJump":
case "Location":
location = (string)o["StarSystem"];
break;
}
}
}
catch (Exception)
{
}
return location;
}
private static string GetSavedGamesDir()
{
IntPtr path;
int result = NativeMethods.SHGetKnownFolderPath(new Guid("4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4"), 0, new IntPtr(0), out path);
if (result >= 0)
{
return Marshal.PtrToStringUni(path) + @"\Frontier Developments\Elite Dangerous";
}
else
{
throw new ExternalException("Failed to find the saved games directory.", result);
}
}
internal class NativeMethods
{
[DllImport("Shell32.dll")]
internal static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)]Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr ppszPath);
}
}
}