Skip to content

Commit

Permalink
SpanshAttack: moved EDTS stuff to plugin code
Browse files Browse the repository at this point in the history
see #62
  • Loading branch information
alterNERDtive committed Feb 22, 2021
1 parent fdccb3a commit 2d6c96f
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 50 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@

* Fixed RATSIGNAL parsing for “Sagittarius A*” landmark.

## SpanshAttack 7.2

### Changed

* Moved EDTS system coordinate estimation code from Python script to the
VoiceAttack plugin.

### Removed

* `edts.exe` Python script.

-----

# 4.1 (2021-02-19)
Expand Down
85 changes: 37 additions & 48 deletions plugins/SpanshAttack/SpanshAttack.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable

using alterNERDtive.util;
using alterNERDtive.edts;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -25,60 +26,48 @@ private static VoiceAttackCommands Commands

private static void Context_EDTS_GetCoordinates(dynamic vaProxy)
{
string system = vaProxy.GetText("~system") ?? throw new ArgumentNullException("~system");
string name = vaProxy.GetText("~system") ?? throw new ArgumentNullException("~system");

string path = $@"{vaProxy.SessionState["VA_SOUNDS"]}\Scripts\edts.exe";
string arguments = $@"coords ""{system}""";
bool success = false;
string? errorType = null;
string? errorMessage = null;

Process p = PythonProxy.SetupPythonScript(path, arguments);
try
{
StarSystem system = EdtsApi.GetCoordinates(name);

Dictionary<char, int> coords = new Dictionary<char, int> { { 'x', 0 }, { 'y', 0 }, { 'z', 0 } };
int precision = 0;
bool error = false;
string errorMessage = "";
if (system.Coords.Precision < 100)
{
Log.Info($@"Coordinates for ""{name}"": ({system.Coords.X}, {system.Coords.Y}, {system.Coords.Z}), precision: {system.Coords.Precision} ly");
}
else
{
Log.Warn($@"Coordinates with low precision for ""{name}"": ({system.Coords.X}, {system.Coords.Y}, {system.Coords.Z}), precision: {system.Coords.Precision} ly");
}

p.Start();
string stdout = p.StandardOutput.ReadToEnd();
string stderr = p.StandardError.ReadToEnd();
p.WaitForExit();
switch (p.ExitCode)
{
case 0:
string[] stdoutExploded = stdout.Split('|');
precision = int.Parse(stdoutExploded[1]);
string[] stdoutCoords = stdoutExploded[0].Split(',');
coords['x'] = int.Parse(stdoutCoords[0]);
coords['y'] = int.Parse(stdoutCoords[1]);
coords['z'] = int.Parse(stdoutCoords[2]);
if (precision < 100)
{
Log.Info($"Coordinates for {system}: ({coords['x']}, {coords['y']}, {coords['z']}), precision: {precision} ly");
}
else
{
Log.Warn($"Coordinates with low precision for {system}: ({coords['x']}, {coords['y']}, {coords['z']}), precision: {precision} ly");
}
break;
case 3:
errorMessage = $@"No coordinates could be determined for ""{system}"" with acceptable precision";
Log.Error(errorMessage);
break;
case 4:
errorMessage = $@"""{system}"" is not a valied proc gen system name";
Log.Error(errorMessage);
break;
default:
break;
vaProxy.SetInt("~x", system.Coords.X);
vaProxy.SetInt("~y", system.Coords.Y);
vaProxy.SetInt("~z", system.Coords.Z);
vaProxy.SetInt("~precision", system.Coords.Precision);

success = true;
} catch (ArgumentException e)
{
errorType = "invalid name";
errorMessage = e.Message;
} catch (Exception e)
{
errorType = "connection error";
errorMessage = e.Message;
}

vaProxy.SetInt("~x", coords['x']);
vaProxy.SetInt("~y", coords['y']);
vaProxy.SetInt("~z", coords['z']);
vaProxy.SetInt("~precision", precision);
vaProxy.SetBoolean("~error", error);
vaProxy.SetText("~errorMessage", errorMessage);
vaProxy.SetInt("~exitCode", p.ExitCode);
vaProxy.SetBoolean("~success", success);
if (!string.IsNullOrWhiteSpace(errorType))
{
Log.Error(errorMessage!);
vaProxy.SetText("~errorType", errorType);
vaProxy.SetText("~errorMessage", errorMessage);
}
}

private static void Context_Log(dynamic vaProxy)
Expand Down Expand Up @@ -214,7 +203,7 @@ private static void Context_Startup(dynamic vaProxy)
| required VoiceAttack plugin shenanigans |
\========================================*/

static readonly Version VERSION = new Version("7.1.0");
static readonly Version VERSION = new Version("7.2.0");

public static Guid VA_Id()
=> new Guid("{e722b29d-898e-47dd-a843-a409c87e0bd8}");
Expand Down
11 changes: 11 additions & 0 deletions plugins/VoiceAttack-base/VoiceAttack-base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -47,8 +54,12 @@
</ItemGroup>
<ItemGroup>
<Compile Include="base.cs" />
<Compile Include="edts.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="util.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
16 changes: 14 additions & 2 deletions plugins/VoiceAttack-base/base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -172,6 +173,17 @@ private static void Context_Config_SetVariables(dynamic vaProxy)

private static void Context_Config_VersionMigration(dynamic vaProxy)
{
// ===========
// === 4.2 ===
// ===========

// SpanshAttack
string edtsPath = $@"{vaProxy.SessionState["VA_SOUNDS"]}\scripts\edts.exe";
if (File.Exists(edtsPath))
{
File.Delete(edtsPath);
}

// ===========
// === 4.0 ===
// ===========
Expand Down Expand Up @@ -275,7 +287,7 @@ private static void Context_EDSM_BodyCount(dynamic vaProxy)
{
string system = vaProxy.GetText("~system") ?? throw new ArgumentNullException("~system");

string path = $@"{vaProxy.SessionState["VA_SOUNDS"]}\Scripts\explorationtools.exe";
string path = $@"{vaProxy.SessionState["VA_SOUNDS"]}\scripts\explorationtools.exe";
string arguments = $@"bodycount ""{system}""";

Process p = PythonProxy.SetupPythonScript(path, arguments);
Expand Down Expand Up @@ -451,7 +463,7 @@ private static void Context_Update_Check(dynamic vaProxy)
| required VoiceAttack plugin shenanigans |
\========================================*/

static readonly Version VERSION = new Version("4.1.1");
static readonly Version VERSION = new Version("4.2.0");

public static Guid VA_Id()
=> new Guid("{F7F59CFD-1AE2-4A7E-8F62-C62372418BAC}");
Expand Down
60 changes: 60 additions & 0 deletions plugins/VoiceAttack-base/edts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json.Linq;

namespace alterNERDtive.edts
{

public struct StarSystem
{
public string Name { get; set; }
public Position Coords { get; set; }
}
public struct Position
{
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
public int Precision { get; set; }
}

public class EdtsApi
{
private static readonly string APIURL = "http://edts.thargoid.space/api/v1/";
private static HttpClient ApiClient;

static EdtsApi()
{
ApiClient = new HttpClient
{
BaseAddress = new Uri(APIURL)
};
ApiClient.DefaultRequestHeaders.Accept.Clear();
ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}

public static StarSystem GetCoordinates(string name)
{
HttpResponseMessage response = ApiClient.GetAsync($"system_position/{name}").Result;

if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) // 400
{
throw new ArgumentException($"“{name}” is not a valid proc gen system name.", "~system");
}

response.EnsureSuccessStatusCode();
dynamic json = response.Content.ReadAsAsync<dynamic>().Result["result"];

int x = json["position"]["x"];
int y = json["position"]["y"];
int z = json["position"]["z"];
int uncertainty = json["uncertainty"];

return new StarSystem { Name=name, Coords=new Position { X=x, Y=y, Z=z, Precision=uncertainty } };
}
}
}
5 changes: 5 additions & 0 deletions plugins/VoiceAttack-base/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net48" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net48" />
</packages>
Binary file modified profiles/SpanshAttack-Profile.vap
Binary file not shown.

0 comments on commit 2d6c96f

Please sign in to comment.