Hey everyone, I'm a Python programmer and recently decided to start a project with AI and the Rage Plugin Hook V / LSPDFR API but I'm having a bit of troubles getting it to work, especially because of named pipes and server-client connections between Python and C#, anyone has any idea why the game immediately freezes upon connection? How can I prevent it? Thanks!
using System;
using System.IO.Pipes;
using System.Text;
using Newtonsoft.Json;
using Rage;
using LSPD_First_Response.Mod.API;
using LSPD_First_Response;
namespace LASO
{
class DispatcherClient
{
private string pipeName;
public DispatcherClient(string pipeName)
{
this.pipeName = pipeName;
}
public void ConnectAndListen()
{
GameFiber.StartNew(() =>
{
using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", this.pipeName, PipeDirection.InOut))
{
try
{
Game.Console.Print("LASO: Connecting to dispatcher...");
pipeClient.Connect(5000); // Wait up to 5 seconds to connect
Game.Console.Print("LASO: Connected to dispatcher, starting main loop...");
// Main loop
while (pipeClient.IsConnected)
{
GameFiber.Yield();
try
{
if (pipeClient.CanRead)
{
// Receive the method to execute from Python
byte[] buffer = new byte[1024];
int bytesRead = pipeClient.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
throw new Exception("LASO: Received 0 bytes from the server");
}
string commandJson = Encoding.UTF8.GetString(buffer, 0, bytesRead);
dynamic command = JsonConvert.DeserializeObject(commandJson);
HandleCommand(command);
}
}
catch (TimeoutException)
{
// Prevent game freeze
GameFiber.Sleep(100);
}
catch (Exception e)
{
Game.Console.Print("LASO: Exception in ConnectAndListen: " + e.Message);
break; // Break the loop
}
}
}
catch (Exception e)
{
Game.Console.Print("LASO: Exception in ConnectAndListen: " + e.Message);
}
}
});
}
private void HandleCommand(dynamic command)
{
// Handle the command
switch ((string)command.function)
{
case "call_backup_to_player_position":
RequestBackup(command);
break;
default:
UknonwnCommand(command);
break;
}
}
private void RequestBackup(dynamic command)
{
Game.DisplayNotification("LASO: Requesting backup to your position");
Vector3 playerPosition = Game.LocalPlayer.Character.Position;
EBackupResponseType code = (EBackupResponseType)Enum.Parse(typeof(EBackupResponseType), command.code);
EBackupUnitType unitType = (EBackupUnitType)Enum.Parse(typeof(EBackupUnitType), command.unitType);
Functions.RequestBackup(playerPosition, code, unitType);
}
private void UknonwnCommand(dynamic command)
{
Game.DisplayNotification("LASO: Unknown command received from dispatcher" + command.function);
}
}
}