Skip to content

Commit bf55cdd

Browse files
committed
✨ voiceattack plugin
1 parent 8962126 commit bf55cdd

25 files changed

Lines changed: 249 additions & 94 deletions

EliteAPI/EliteAPI.csproj

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net10.0</TargetFramework>
5-
<ImplicitUsings>enable</ImplicitUsings>
4+
<TargetFrameworks>net8.0;net48</TargetFrameworks>
65
<Nullable>enable</Nullable>
7-
<OutputType>Exe</OutputType>
6+
<LangVersion>latest</LangVersion>
7+
</PropertyGroup>
8+
9+
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
10+
<ImplicitUsings>enable</ImplicitUsings>
811
</PropertyGroup>
912

1013
<ItemGroup>
14+
<PackageReference Include="IsExternalInit" Version="1.0.3">
15+
<PrivateAssets>all</PrivateAssets>
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
</PackageReference>
18+
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
1119
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
1220
</ItemGroup>
1321

EliteAPI/EliteDangerousApi.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using EliteAPI.Journals;
23
using EliteAPI.Watcher;
34

@@ -6,12 +7,14 @@ namespace EliteApi;
67
public class EliteDangerousApi
78
{
89
private readonly FileWatcher _journalWatcher;
10+
private readonly FileWatcher _statusWatcher;
911

1012
public EliteDangerousApi()
1113
{
1214
var journalDirectory = JournalUtils.GetJournalsDirectory();
1315

1416
_journalWatcher = FileWatcher.Create(journalDirectory, "Journal.*.log", FileWatchMode.LineByLine).watcher;
17+
_statusWatcher = FileWatcher.Create(journalDirectory, "Status.json", FileWatchMode.EntireFile).watcher;
1518
}
1619

1720
public void OnJournalEvent(Action<(string eventName, string json)> onEvent)
@@ -21,5 +24,10 @@ public void OnJournalEvent(Action<(string eventName, string json)> onEvent)
2124
var eventName = JournalUtils.GetEventName(json);
2225
onEvent((eventName, json));
2326
});
27+
28+
_statusWatcher.OnContentChanged(json =>
29+
{
30+
onEvent(("Status", json));
31+
});
2432
}
2533
}

EliteAPI/Journals/JournalUtils.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
using System;
2+
using System.IO;
13
using System.Runtime.InteropServices;
2-
using System.Text.Json;
4+
using Newtonsoft.Json.Linq;
35

46
namespace EliteAPI.Journals;
57

68
public static class JournalUtils
79
{
810
public static string GetEventName(string json)
911
{
10-
using var document = JsonDocument.Parse(json);
11-
if (document.RootElement.TryGetProperty("event", out var eventProperty))
12-
{
13-
return eventProperty.GetString() ?? string.Empty;
14-
}
12+
var obj = JObject.Parse(json);
13+
if (obj.TryGetValue("event", out var eventNameToken))
14+
return eventNameToken.ToString();
1515

1616
return string.Empty;
1717
}

EliteAPI/Json/JsonUtils.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System.Text.Json;
2-
using Newtonsoft;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
34
using Newtonsoft.Json.Linq;
45

56
namespace EliteAPI.Json;

EliteAPI/Main.cs

Lines changed: 0 additions & 1 deletion
This file was deleted.

EliteAPI/Watcher/FileWatcher.cs

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading;
6+
17
namespace EliteAPI.Watcher;
28

39
public class FileWatcher
@@ -68,8 +74,15 @@ private void StartWatching()
6874

6975
_fileWatcher.Changed += (sender, args) =>
7076
{
71-
Thread.Sleep(50);
72-
HandleFileChange();
77+
try
78+
{
79+
Thread.Sleep(50);
80+
HandleFileChange();
81+
}
82+
catch
83+
{
84+
// Silently ignore exceptions to prevent crashes
85+
}
7386
};
7487

7588
_fileWatcher.EnableRaisingEvents = true;
@@ -79,17 +92,33 @@ private void HandleFileChange()
7992
{
8093
if (_onContentChanged == null) return;
8194

82-
if (_mode == FileWatchMode.LineByLine)
95+
try
8396
{
84-
var lines = ReadFileLines(_file.FullName);
85-
for (int i = _lastLineCount; i < lines.Length; i++)
86-
_onContentChanged(lines[i]);
87-
_lastLineCount = lines.Length;
97+
if (_mode == FileWatchMode.LineByLine)
98+
{
99+
var lines = ReadFileLines(_file.FullName);
100+
for (int i = _lastLineCount; i < lines.Length; i++)
101+
{
102+
try
103+
{
104+
_onContentChanged(lines[i]);
105+
}
106+
catch
107+
{
108+
// Continue processing other lines even if one fails
109+
}
110+
}
111+
_lastLineCount = lines.Length;
112+
}
113+
else
114+
{
115+
var content = ReadFileContent(_file.FullName);
116+
_onContentChanged(content);
117+
}
88118
}
89-
else
119+
catch
90120
{
91-
var content = ReadFileContent(_file.FullName);
92-
_onContentChanged(content);
121+
// Silently ignore exceptions to prevent crashes
93122
}
94123
}
95124

@@ -108,17 +137,24 @@ private void SetupDirectoryWatcher()
108137

109138
private void OnFileInDirectoryChanged(object sender, FileSystemEventArgs e)
110139
{
111-
Thread.Sleep(100);
112-
113-
var changedFile = new FileInfo(e.FullPath);
114-
115-
// Only switch if the changed file matches the pattern, is newer than current, and is not the current file
116-
if (_filePattern != null &&
117-
MatchesPattern(changedFile.Name, _filePattern) &&
118-
changedFile.FullName != _file.FullName &&
119-
changedFile.LastWriteTimeUtc > _file.LastWriteTimeUtc)
140+
try
141+
{
142+
Thread.Sleep(100);
143+
144+
var changedFile = new FileInfo(e.FullPath);
145+
146+
// Only switch if the changed file matches the pattern, is newer than current, and is not the current file
147+
if (_filePattern != null &&
148+
MatchesPattern(changedFile.Name, _filePattern) &&
149+
changedFile.FullName != _file.FullName &&
150+
changedFile.LastWriteTimeUtc > _file.LastWriteTimeUtc)
151+
{
152+
SwitchToNewFile(changedFile);
153+
}
154+
}
155+
catch
120156
{
121-
SwitchToNewFile(changedFile);
157+
// Silently ignore exceptions to prevent crashes
122158
}
123159
}
124160

EliteVA/EliteVA.csproj

Lines changed: 0 additions & 20 deletions
This file was deleted.

EliteVA/Plugin.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

VoiceAttack/Abstractions/IProxy.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using VoiceAttack.Audio;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using VoiceAttack.Audio;
25
using VoiceAttack.Commands;
36
using VoiceAttack.Logging;
47
using VoiceAttack.Options;

VoiceAttack/Audio/VoiceAttackSpeech.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace VoiceAttack.Audio;
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
4+
namespace VoiceAttack.Audio;
25

36
public class VoiceAttackSpeech
47
{

0 commit comments

Comments
 (0)