Skip to content

Extends pr #336 by adding command line arguments #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions GameMod/GameMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Xml.Schema;
using GameMod.VersionHandling;
using HarmonyLib;
using Overload;
Expand Down Expand Up @@ -88,6 +89,41 @@ public static void Initialize()
{
uConsole.RegisterCommand("export-spawns", "Exports spawnpoints from the editor to a .json file in the OLmod directory", new uConsole.DebugCommand(MPSpawnExtensionVis.Export));
}

if (FindArg("-telemetry"))
TelemetryMod.telemetry_enabled = true;

if (FindArgVal("-telemetry-ip", out string ip_string)){
if (!String.IsNullOrEmpty(ip_string))
{
string[] ip_string_parts = ip_string.Split('.');
if (ip_string_parts.Length == 4)
{
bool valid = true;
for (int i = 0; i < 3; i++)
if (int.TryParse(ip_string_parts[i], out int value))
if (value < 0 | value > 255)
valid = false;

if (valid)
TelemetryMod.telemetry_ip = ip_string;
else
Debug.Log("Invalid Input. Telemetry IP contains subvalues outside of the [0,255] range: " + ip_string);
}

}
}

if (FindArgVal("-telemetry-port", out string port_string))
{
if (int.TryParse(port_string, out int port))
if (port >= 0 && port < 65535)
TelemetryMod.telemetry_port = port;
else
Debug.Log("Invalid Input. Telemetry Port is outside of the allowed [0, 65534] range: " + port_string);
else
Debug.Log("Invalid Input. Telemetry Port is not a whole number: " + port_string);
}
}

public static bool FindArg(string arg)
Expand Down
1 change: 1 addition & 0 deletions GameMod/GameMod.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
<Compile Include="Menus.cs" />
<Compile Include="Shaders.cs" />
<Compile Include="SPThunderboltShakeFix.cs" />
<Compile Include="TelemetryMod.cs" />
<Compile Include="WarperOrientation.cs" />
<Compile Include="ExitCutscene.cs" />
<Compile Include="UIMeshColliderNoRender.cs" />
Expand Down
294 changes: 294 additions & 0 deletions GameMod/TelemetryMod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
using HarmonyLib;

using Overload;

using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;

using UnityEngine;
using UnityEngine.Networking;

namespace GameMod
{

// commandline arguments:
// telemetry = enables the telemetry output
// telemetry-ip = ip to send udp packets towards (optional)
// telemetry-port = port to send udp packets towards (optional)

/// <summary>
/// v 1.1.1
/// </summary>
internal class TelemetryMod
{
public static bool telemetry_enabled = false;
public static string telemetry_ip = "127.0.0.1";
public static int telemetry_port = 4123;
private static float event_boosting = 0.0f;
private static float event_primary_fire = 0.0f;
private static float event_secondary_fire = 0.0f;
private static float event_picked_up_item = 0.0f;
private static float event_damage_taken = 0.0f;
private static Telemetry telemetryComponent;
private static bool initialized = false;
private static GameObject udpSenderObject;
private static Vector3 previousVelocity = Vector3.zero;
private static Vector3 previousLocalVelocity = Vector3.zero;


[HarmonyPatch(typeof(PlayerShip), "FixedUpdateProcessControlsInternal")]
private class TelemetryMod_PlayerShip_FixedUpdateProcessControlsInternal
{
private static void Prefix()
{
if (!telemetry_enabled)
return;
event_primary_fire = GameManager.m_local_player.IsPressed((CCInput)14) ? 1f : 0.0f;
event_secondary_fire = GameManager.m_local_player.IsPressed((CCInput)15) ? 1f : 0.0f;
}
}

[HarmonyPatch(typeof(Item), "PlayItemPickupFX")]
private class TelemetryMod_Item_PlayItemPickupFX
{
private static void Postfix(Player player)
{
if (!telemetry_enabled || player == null || !player.isLocalPlayer)
return;
event_secondary_fire = 1f;
}
}

[HarmonyPatch(typeof(PlayerShip), "ApplyDamage")]
private class TelemetryMod_PlayerShip_ApplyDamage
{
private static void Postfix(DamageInfo di, PlayerShip __instance)
{
if (!telemetry_enabled || __instance == null || !__instance.isLocalPlayer)
return;
event_damage_taken += di.damage;
}
}

[HarmonyPatch(typeof(GameManager), "FixedUpdate")]
private class TelemetryMod_GameManager_FixedUpdate
{
private static void Postfix()
{
if (!initialized & GameManager.m_local_player != null)
{
initialized = true;
udpSenderObject = new GameObject("UdpTelemetrySender");
telemetryComponent = udpSenderObject.AddComponent<Telemetry>();
telemetryComponent.IP = telemetry_ip;
telemetryComponent.port = telemetry_port;
}
else
{
if (!initialized)
return;
event_boosting = GameManager.m_local_player.c_player_ship.m_boosting ? 1f : 0.0f;
if (GameplayManager.m_gameplay_state == 0)
{
Rigidbody cRigidbody = GameManager.m_local_player.c_player_ship.c_rigidbody;
Quaternion rotation = cRigidbody.rotation;
Vector3 eulerAngles = rotation.eulerAngles;
Vector3 angularVelocity = cRigidbody.angularVelocity;

// angular velocity relative to object
Vector3 localAngularVelocity = cRigidbody.transform.InverseTransformDirection(cRigidbody.angularVelocity);

// velocity relative to object
Vector3 localVelocity = cRigidbody.transform.InverseTransformDirection(cRigidbody.velocity);

Vector3 gforce = (cRigidbody.velocity - previousVelocity) / Time.fixedDeltaTime / 9.81f;
previousVelocity = cRigidbody.velocity;

Vector3 lgforce = (localVelocity - previousLocalVelocity) / Time.fixedDeltaTime / 9.81f;
previousLocalVelocity = localVelocity;


Telemetry.Telemetry_SendTelemetry( new Vector3(
eulerAngles.x > 180.0 ? eulerAngles.x - 360f : eulerAngles.x,
eulerAngles.y > 180.0 ? eulerAngles.y - 360f : eulerAngles.y,
eulerAngles.z > 180.0 ? eulerAngles.z - 360f : eulerAngles.z),
angularVelocity,
gforce,
event_boosting,
event_primary_fire,
event_secondary_fire,
event_picked_up_item,
event_damage_taken,
lgforce,
localAngularVelocity,
localVelocity);
}
else
Telemetry.Telemetry_SendTelemetry(Vector3.zero, Vector3.zero, Vector3.zero, 0f, 0f, 0f, 0f, 0f, Vector3.zero, Vector3.zero, Vector3.zero);

event_boosting = 0.0f;
event_primary_fire = 0.0f;
event_secondary_fire = 0.0f;
event_picked_up_item = 0.0f;
event_damage_taken = 0.0f;
}
}
}

private class PlayerData
{
/// <summary>
/// pitch (x), yaw (y), roll (z)
/// </summary>
public Vector3 Rotation;

/// <summary>
/// pitch (x), yaw (y), roll (z)
/// </summary>
public Vector3 AngularVelocity;

/// <summary>
/// sway (x), heave (y), surge (z)
/// </summary>
public Vector3 GForce;

/// <summary>
/// pitch (x), yaw (y), roll (z)
/// </summary>
public Vector3 LocalAngularVelocity;

/// <summary>
/// sway (x), heave (y), surge (z)
/// </summary>
public Vector3 LocalVelocity;

/// <summary>
/// sway (x), heave (y), surge (z)
/// </summary>
public Vector3 LocalGForce;

public float EventBoosting;
public float EventPrimaryFire;
public float EventSecondaryFire;
public float EventItemPickup;
public float EventDamageTaken;



public PlayerData()
{
}

/// <summary>
///
/// </summary>
/// <param name="rotation">pitch(x), yaw(y), roll(z)</param>
/// <param name="angularVelocity">pitch (x), yaw (y), roll (z)</param>
/// <param name="gforce">sway (x), heave (y), surge (z)</param>
/// <param name="boosting"></param>
/// <param name="primaryFire"></param>
/// <param name="secondaryFire"></param>
/// <param name="itemPickup"></param>
/// <param name="damageTaken"></param>
/// <param name="localgForce">sway (x), heave (y), surge (z)</param>
/// <param name="localAngularVelocity">pitch (x), yaw (y), roll (z)</param>
/// <param name="localVelocity">sway (x), heave (y), surge (z)</param>
public PlayerData(
Vector3 rotation,
Vector3 angularVelocity,
Vector3 gforce,
float boosting,
float primaryFire,
float secondaryFire,
float itemPickup,
float damageTaken,
Vector3 localgForce,
Vector3 localAngularVelocity,
Vector3 localVelocity
)
{
Rotation = rotation;
AngularVelocity = angularVelocity;
GForce = gforce;

EventBoosting = boosting;
EventPrimaryFire = primaryFire;
EventSecondaryFire = secondaryFire;
EventItemPickup = itemPickup;
EventDamageTaken = damageTaken;

LocalGForce = localgForce;
LocalAngularVelocity = localAngularVelocity;
LocalVelocity = localVelocity;

}
}

public class Telemetry : MonoBehaviour
{
public string IP = "127.0.0.1";
public int port = 4123;
private IPEndPoint remoteEndPoint;
private static UdpClient client;
private static PlayerData local_player_data;

private void Start()
{
DontDestroyOnLoad(gameObject);
this.remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
client = new UdpClient();
local_player_data = new PlayerData();
this.StartCoroutine("Telemetry_Start");
}

public static void Telemetry_SendTelemetry(
Vector3 rotation,
Vector3 angularVelocity,
Vector3 gforce,
float boosting,
float primaryFire, float secondaryFire,
float itemPickup,
float damageTaken,
Vector3 localgForce,
Vector3 localAngularVelocity,
Vector3 localVelocity)
{
local_player_data = new PlayerData(rotation,
angularVelocity,
gforce,
boosting,
primaryFire, secondaryFire,
itemPickup,
damageTaken,
localgForce,
localAngularVelocity,
localVelocity);
}

private IEnumerator Telemetry_Start()
{
while (true)
{
string info = string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};{10};{11};{12};{13};{14};{15};{16};{17};{18};{19};{20};{21};{22}",
local_player_data.Rotation.z, local_player_data.Rotation.x, local_player_data.Rotation.y, // out of order for backwards compatibility with simtools
local_player_data.AngularVelocity.z, local_player_data.AngularVelocity.x, local_player_data.AngularVelocity.y, // out of order for backwards compatibility with simtools
local_player_data.GForce.x, local_player_data.GForce.y, local_player_data.GForce.z,
local_player_data.EventBoosting,
local_player_data.EventPrimaryFire, local_player_data.EventSecondaryFire,
local_player_data.EventItemPickup, local_player_data.EventDamageTaken,
local_player_data.LocalGForce.x, local_player_data.LocalGForce.y, local_player_data.LocalGForce.z,
local_player_data.LocalAngularVelocity.x, local_player_data.LocalAngularVelocity.y, local_player_data.LocalAngularVelocity.z,
local_player_data.LocalVelocity.x, local_player_data.LocalVelocity.y, local_player_data.LocalVelocity.z);

byte[] data = Encoding.Default.GetBytes(info);
client.Send(data, data.Length, this.remoteEndPoint);
yield return null;
info = null;
data = null;
}
}
}
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ This is an unaffiliated, unsupported tool. Use at your own risk.

- Spawnpoint injection, by Fireball. See https://github.com/overload-development-community/olmod/wiki/Spawnpoint-Injection for details.

- Export UDP Telemetry , by luponix. Add localized sway,surge & heave, and angular velocity by fresh_ch.

##### Linux

Build the shared object library:
Expand Down