Skip to content

Commit

Permalink
AutoTracking and macOS Fix
Browse files Browse the repository at this point in the history
- Improved logging and status indicators.
- Status indicator changes now properly run in the UI thread.
- macOS option set to use software rendering.
- Incremented version from 0.8.5 to 1.0.0 for first non-beta release.
  • Loading branch information
trippsc2 committed May 8, 2020
1 parent 3485412 commit b2b312a
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 100 deletions.
9 changes: 6 additions & 3 deletions OpenTracker.Models/AutoTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ private void OnPropertyChanged(string propertyName)
return;
}

public void Start(Action<string, LogLevel> messageHandler)
public void Start(string uriString, Action<string, LogLevel> messageHandler)
{
Connector = new USB2SNESConnector(messageHandler);
Connector = new USB2SNESConnector(uriString, messageHandler);

Connector.ConnectIfNecessary();

int i = 0;

Expand Down Expand Up @@ -122,7 +124,8 @@ public void Stop()

public bool IsInGame()
{
if (InGameStatus != null && InGameStatus.Value > 0x05 && InGameStatus.Value != 0x14)
if (InGameStatus.HasValue && InGameStatus.Value > 0x05 &&
InGameStatus.Value != 0x14)
return true;

return false;
Expand Down
3 changes: 2 additions & 1 deletion OpenTracker.Models/AutotrackerConnectors/ConnectionStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
{
public enum ConnectionStatus : byte
{
Closed,
NotConnected,
Connecting,
Open,
Error
}
}
68 changes: 50 additions & 18 deletions OpenTracker.Models/AutotrackerConnectors/USB2SNESConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace OpenTracker.Models.AutotrackerConnectors
{
public class USB2SNESConnector : INotifyPropertyChanging, INotifyPropertyChanged, IDisposable
{
private readonly string _webSocketURI;
private readonly Action<string, LogLevel> _messageHandler;
private bool _shutdown;
private volatile bool _open;
Expand Down Expand Up @@ -55,10 +56,10 @@ public WebSocket Socket
public Action<MessageEventArgs> PendingMessageHandler { get; private set; }
public string Usb2SnesApplicationName { get; set; } = "OpenTracker";

public USB2SNESConnector(Action<string, LogLevel> messageHandler = null)
public USB2SNESConnector(string webSocketURI, Action<string, LogLevel> messageHandler = null)
{
_webSocketURI = webSocketURI;
_messageHandler = messageHandler;
ConnectIfNecessary();
}

private void OnPropertyChanging(string propertyName)
Expand Down Expand Up @@ -107,7 +108,7 @@ private void Disconnect(bool shutdown = false)
_open = false;
}

ConnectionStatusChanged?.Invoke(this, (ConnectionStatus.Closed, "Disconnected from usb2snes websocket."));
ConnectionStatusChanged?.Invoke(this, (ConnectionStatus.NotConnected, "Disconnected from usb2snes websocket."));
}

private void Output(string message, LogLevel level, params object[] tokens)
Expand Down Expand Up @@ -154,6 +155,7 @@ public bool ReadByte(ulong address, out byte value)
public bool Read(ulong address, byte[] buffer)
{
bool success = false;

while (!success)
{
ConnectIfNecessary();
Expand Down Expand Up @@ -197,7 +199,7 @@ public bool Read(ulong address, byte[] buffer)
{
Output("ERROR: Connection to USB2SNES lost.", LogLevel.Error);

ConnectionStatusChanged?.Invoke(this, (ConnectionStatus.Closed,
ConnectionStatusChanged?.Invoke(this, (ConnectionStatus.Error,
"Connection to USB2SNES lost."));
}
else
Expand Down Expand Up @@ -263,14 +265,21 @@ public static bool MapAddressInRange(uint address, uint srcRangeBegin, uint srcR
return false;
}

private bool? ConnectIfNecessary()
public bool? ConnectIfNecessary()
{
Output("Attempting to connect to USB2SNES", LogLevel.Debug);

if (Connected || _shutdown)
{
Output("Already connected to USB2SNES or connector is shut down.", LogLevel.Debug);
return null;
}

Disconnect();

Socket = new WebSocket("ws://localhost:8080");
ConnectionStatusChanged?.Invoke(this, (ConnectionStatus.Connecting, "Connecting to USB2SNES websocket."));

Socket = new WebSocket(_webSocketURI);
Socket.Log.Output = (data, message) =>
{
if (!string.IsNullOrWhiteSpace(data.Message))
Expand All @@ -280,7 +289,7 @@ public static bool MapAddressInRange(uint address, uint srcRangeBegin, uint srcR
Socket.Connect();
Thread.Sleep(1000);

if (Socket.IsAlive)
if (Socket != null && Socket.IsAlive)
{
Output("Connected to WebSocket", LogLevel.Info);
string port = null;
Expand All @@ -289,19 +298,35 @@ public static bool MapAddressInRange(uint address, uint srcRangeBegin, uint srcR
{
try
{
Output("Message received from websocket.", LogLevel.Debug);
if (JsonConvert.DeserializeObject<Dictionary<string, string[]>>(e.Data) is Dictionary<string, string[]> dictionary &&
dictionary.TryGetValue("Results", out string[] results))
{
Output("Message successfully deserialized.", LogLevel.Debug);
foreach (string result in (results as IEnumerable<string>) ?? new string[0])
{
port = result as string;
if (!string.IsNullOrWhiteSpace(port))
{
Output(string.Format("SNES port {0} found.", port), LogLevel.Debug);
break;
}
else
Output("Received empty response.", LogLevel.Debug);
}
}
else
Output("Message could not be deserialized.", LogLevel.Warn);
}
catch (Exception ex)
{
Output(ex.Message, LogLevel.Fatal);
}
catch { }
finally { _memoryReadEvent.Set(); }
};

Expand All @@ -318,6 +343,9 @@ public static bool MapAddressInRange(uint address, uint srcRangeBegin, uint srcR
{
Output(string.Format("Connected to SNES via {0}", port), LogLevel.Info);

ConnectionStatusChanged?.Invoke(this, (ConnectionStatus.Open,
"Connection to USB2SNES socket service established."));

Socket.Send(JsonConvert.SerializeObject(new RequestType()
{
Opcode = OpcodeType.Attach.ToString(),
Expand All @@ -332,9 +360,6 @@ public static bool MapAddressInRange(uint address, uint srcRangeBegin, uint srcR
Operands = new List<string>() { Usb2SnesApplicationName }
}));

ConnectionStatusChanged?.Invoke(this, (ConnectionStatus.Open,
"Connection to USB2SNES socket service established."));

_open = true;

if (_keepAliveTask == null)
Expand All @@ -343,29 +368,36 @@ public static bool MapAddressInRange(uint address, uint srcRangeBegin, uint srcR
{
while (_open)
{
try { KeepAlive(); }
catch { }
try
{
Output("Keepalive sent.", LogLevel.Debug);
KeepAlive();
}
catch (Exception ex)
{
Output(ex.Message, LogLevel.Fatal);
}
Thread.Sleep(1500);
}
_messageHandler?.Invoke("USB2SNESConnector keepalive is quitting.", LogLevel.Fatal);
_messageHandler?.Invoke("USB2SNESConnector keepalive is quitting.", LogLevel.Error);
}, TaskCreationOptions.LongRunning);
}
return true;
}

Output("ERROR: No SNES was detected by the system.", LogLevel.Error);
Output("No SNES was detected by the system.", LogLevel.Error);

ConnectionStatusChanged?.Invoke(this, (ConnectionStatus.Closed,
ConnectionStatusChanged?.Invoke(this, (ConnectionStatus.Error,
"No SNES was detected by the system."));

return false;
}

Output("ERROR: Failed to connect to web socket; the USB2SNES websocket application may not be running.", LogLevel.Error);
Output("Failed to connect to web socket. The USB2SNES websocket application may not be running.", LogLevel.Error);

ConnectionStatusChanged?.Invoke(this, (ConnectionStatus.Closed,
ConnectionStatusChanged?.Invoke(this, (ConnectionStatus.Error,
"Failed to connect to websocket; the USB2SNES websocket application may not be running."));

return false;
Expand Down
2 changes: 1 addition & 1 deletion OpenTracker.Models/OpenTracker.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>0.8.5</Version>
<Version>1.0.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions OpenTracker.Setup/OpenTracker.Setup.vdproj
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,15 @@
{
"Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:OpenTracker"
"ProductCode" = "8:{CCCABE1F-A7AE-4F7D-9D8B-D128299252EC}"
"PackageCode" = "8:{DD582563-EE5B-4C12-AA6D-0E69667799D5}"
"ProductCode" = "8:{856A224D-493E-443D-8CB7-C54468589A7E}"
"PackageCode" = "8:{C29F17E6-0DB6-49FA-95BB-529F8CDB1F32}"
"UpgradeCode" = "8:{C98C2121-B4C5-473A-9B86-5407FEFFE8F1}"
"AspNetVersion" = "8:2.0.50727.0"
"RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:TRUE"
"ProductVersion" = "8:0.8.5"
"ProductVersion" = "8:1.0.0"
"Manufacturer" = "8:OpenTracker"
"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:"
Expand Down Expand Up @@ -834,7 +834,7 @@
{
"{5259A561-127C-4D43-A0A1-72F10C7B3BF8}:_9E6FFC26C1B94A308AE37383659B05F8"
{
"SourcePath" = "8:..\\OpenTracker\\obj\\Release\\netcoreapp3.1\\OpenTracker.exe"
"SourcePath" = "8:..\\OpenTracker\\obj\\Debug\\netcoreapp3.1\\OpenTracker.exe"
"TargetName" = "8:"
"Tag" = "8:"
"Folder" = "8:_B21EA2C01C014AC99A33B72F50C26B5D"
Expand Down
44 changes: 42 additions & 2 deletions OpenTracker/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@

<Style Selector="Border.AutoTrackerLog">
<Setter Property="Background" Value="#2D4848" />
<Setter Property="Margin" Value="5" />
<Setter Property="BorderBrush" Value="#081212" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Margin" Value="5" />
</Style>

<Style Selector="Border.MapLocation">
Expand Down Expand Up @@ -209,7 +211,23 @@
<Setter Property="VerticalAlignment" Value="Center" />
</Style>

<Style Selector="TextBlock.AutoTrackerLog">
<Style Selector="TextBlock.AutoTrackerLabel">
<Setter Property="FontFamily" Value="avares://OpenTracker/Assets/Fonts/#Open Sans" />
<Setter Property="FontSize" Value="12" />
<Setter Property="FontWeight" Value="SemiBold" />
<Setter Property="Foreground" Value="#FFFFFF" />
<Setter Property="Margin" Value="0,0,10,0" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>

<Style Selector="TextBlock.AutoTrackerStatus">
<Setter Property="FontFamily" Value="avares://OpenTracker/Assets/Fonts/#Open Sans" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Foreground" Value="#FFFFFF" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>

<Style Selector="TextBlock.AutoTrackerLog">
<Setter Property="FontFamily" Value="avares://OpenTracker/Assets/Fonts/#Open Sans" />
</Style>

Expand Down Expand Up @@ -324,6 +342,28 @@
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>

<Style Selector="TextBox.AutoTrackerURI">
<Setter Property="Background" Value="#2D4848" />
<Setter Property="FontFamily" Value="avares://OpenTracker/Assets/Fonts/#Open Sans" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Foreground" Value="#FFFFFF" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Width" Value="150" />
</Style>

<Style Selector="ComboBox.AutoTrackerLogLevel">
<Setter Property="Background" Value="#2D4848" />
<Setter Property="FontFamily" Value="avares://OpenTracker/Assets/Fonts/#Open Sans" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Foreground" Value="#FFFFFF" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Width" Value="100" />
</Style>

<Style Selector="ComboBox.AutoTrackerLogLevel > ComboBoxItem">
<Setter Property="Background" Value="#2D4848" />
</Style>

</Application.Styles>

</Application>
14 changes: 7 additions & 7 deletions OpenTracker/OpenTracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<ApplicationIcon>triforce.ico</ApplicationIcon>
<Win32Resource />
<Version>0.8.5</Version>
<Version>1.0.0</Version>
<Authors>Tripp</Authors>
<AssemblyVersion>0.8.5.0</AssemblyVersion>
<FileVersion>0.8.5.0</FileVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<RuntimeIdentifiers>osx-x64;linux-x64;debian-x64;rhel-x64;win-x64;win-x86;win7-x64;win7-x86</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
Expand Down Expand Up @@ -49,10 +49,10 @@
<None Remove="Views\SectionControl.xaml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.9.9" />
<PackageReference Include="Avalonia.Desktop" Version="0.9.9" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.9.9" />
<PackageReference Include="Dotnet.Bundle" Version="0.9.12" />
<PackageReference Include="Avalonia" Version="0.9.10" />
<PackageReference Include="Avalonia.Desktop" Version="0.9.10" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.9.10" />
<PackageReference Include="Dotnet.Bundle" Version="0.9.13" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="ThemeEditor.Controls.ColorPicker" Version="0.9.4" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion OpenTracker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToDebug()
.UseReactiveUI();
.UseReactiveUI()
.With(new AvaloniaNativePlatformOptions { UseGpu = false });
}
}
Loading

0 comments on commit b2b312a

Please sign in to comment.