Skip to content
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

Add FiveM -> Ped comms #9

Merged
merged 3 commits into from
May 18, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[submodule "fivem/cfx-server-data"]
path = fivem/cfx-server-data
url = https://github.com/citizenfx/cfx-server-data.git
ignore = dirty
2 changes: 1 addition & 1 deletion fivem/Start-Server.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ param(
# Build
Push-Location -Path "$PSScriptRoot\..\src"
try {
& dotnet build Missions.sln
& dotnet build PedGPT.sln
}
finally {
Pop-Location
Expand Down
4 changes: 4 additions & 0 deletions src/IntelliPed.Core/IntelliPed.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<PackageReference Include="Microsoft.SemanticKernel.Planners.Handlebars" Version="1.7.0-preview" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\IntelliPed.FiveM.Messages\IntelliPed.FiveM.Messages.csproj" />
</ItemGroup>

</Project>
18 changes: 17 additions & 1 deletion src/IntelliPed.Core/Plugins/NavigationPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.ComponentModel;
using System.Net.Http.Json;
using System.Numerics;
using IntelliPed.FiveM.Messages.Navigation;
using Microsoft.SemanticKernel;

namespace IntelliPed.Core.Plugins;
Expand All @@ -12,7 +14,21 @@ public async Task<string> NavigateTo(
Kernel kernel,
[Description("The co-ordinate.")] Vector3 coordinate)
{
await Task.Delay(1000);
HttpClient httpClient = new();

HttpResponseMessage response = await httpClient.PostAsJsonAsync("http://localhost:5000/api/navigation/move-to-position", new MoveToPositionRequest
{
X = coordinate.X,
Y = coordinate.Y,
Z = coordinate.Z,
});

if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"Failed to navigate to ({coordinate.X}, {coordinate.Y}, {coordinate.Z})");
return "Failed to navigate.";
}

Console.WriteLine($"Successfully navigated to ({coordinate.X}, {coordinate.Y}, {coordinate.Z})");
return "Successfully navigated.";
}
Expand Down
12 changes: 12 additions & 0 deletions src/IntelliPed.FiveM.Messages/IntelliPed.FiveM.Messages.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<Folder Include="Navigation\" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions src/IntelliPed.FiveM.Messages/Navigation/MoveToPosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace IntelliPed.FiveM.Messages.Navigation;

public record MoveToPositionRequest
{
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
}
27 changes: 27 additions & 0 deletions src/IntelliPed.FiveM.Server/Controllers/NavigationController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using CitizenFX.Core.Native;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using IntelliPed.FiveM.Messages.Navigation;

namespace IntelliPed.FiveM.Server.Controllers;

[Route("api/[controller]")]
public class NavigationController : Controller
{
[HttpPost("move-to-position")]
public async Task<IActionResult> MoveToPosition([FromBody] MoveToPositionRequest request)
{
string? resourceName = null;
TaskCompletionSource<bool> taskCompletionSource = new();

GameScript.Instance.ExecuteOnGameThread(() =>
{
resourceName = API.GetCurrentResourceName();
taskCompletionSource.SetResult(true);
});

await taskCompletionSource.Task;

return Ok();
}
}
34 changes: 34 additions & 0 deletions src/IntelliPed.FiveM.Server/GameScript.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using CitizenFX.Core;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System;

namespace IntelliPed.FiveM.Server;

public class GameScript : BaseScript
{
public static GameScript Instance { get; private set; } = null!;

private readonly ConcurrentQueue<Action> _actions = new ConcurrentQueue<Action>();

public GameScript()
{
Instance = this;
Tick += OnTick;
}

public void ExecuteOnGameThread(Action action)
{
_actions.Enqueue(action);
}

private async Task OnTick()
{
while (_actions.TryDequeue(out var action))
{
action();
}

await Task.FromResult(0); // Yield control back to the game loop
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<DebugType>portable</DebugType>
<TargetName>$(AssemblyName).net</TargetName>
<DefineConstants>SERVER</DefineConstants>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CitizenFX.Core.Server" Version="1.0.8202" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.15" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.15" />
</ItemGroup>

<ItemGroup>
<Folder Include="NewFolder\" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\..\fivem\cfx-server-data\resources\intelliped\server\</OutputPath>
</PropertyGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(ProjectDir)fxmanifest.lua&quot; &quot;$(OutDir)..\&quot; /Y" />
</Target>

</Project>
13 changes: 13 additions & 0 deletions src/IntelliPed.FiveM.Server/IntelliPed.FiveM.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@

<ItemGroup>
<PackageReference Include="CitizenFX.Core.Server" Version="1.0.8202" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.15" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.15" />
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\IntelliPed.FiveM.Messages\IntelliPed.FiveM.Messages.csproj" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
56 changes: 56 additions & 0 deletions src/IntelliPed.FiveM.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Diagnostics;
using System.Threading.Tasks;
using CitizenFX.Core;
using CitizenFX.Core.Native;
using IntelliPed.FiveM.Server.Controllers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
using Debug = CitizenFX.Core.Debug;

namespace IntelliPed.FiveM.Server;

public class Program : BaseScript
{
[EventHandler("onResourceStart")]
private async void OnResourceStart(string resourceName)
{
if (API.GetCurrentResourceName() != resourceName)
{
return;
}

IConfigurationRoot config = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.Build();

IWebHost host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.ConfigureServices(services =>
{
services
.AddMvc()
.AddApplicationPart(typeof(NavigationController).Assembly);

services.AddSingleton<ObjectPoolProvider>(new DefaultObjectPoolProvider());
services.AddSingleton<IHostingEnvironment>(new HostingEnvironment());
services.AddSingleton<DiagnosticSource>(new DiagnosticListener("IntelliPed"));
})
.Configure(app =>
{
app.UseCors();
app.UseMvcWithDefaultRoute();
})
.ConfigureLogging(_ => _.AddConsole())
.Build();

await Task.Run(host.Run);

Debug.WriteLine("IntelliPed.FiveM.Server has been started.");
}
}
12 changes: 9 additions & 3 deletions src/PedGPT.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33213.308
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntelliPed.Core", "IntelliPed.Core\IntelliPed.Core.csproj", "{C8547D29-E7DD-4617-9FB8-DFFDEBC391CE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntelliPed.Core", "IntelliPed.Core\IntelliPed.Core.csproj", "{C8547D29-E7DD-4617-9FB8-DFFDEBC391CE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntelliPed.ConsoleApp", "IntelliPed.ConsoleApp\IntelliPed.ConsoleApp.csproj", "{F16E4B1E-8D56-4414-94D7-08695692A7EF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntelliPed.ConsoleApp", "IntelliPed.ConsoleApp\IntelliPed.ConsoleApp.csproj", "{F16E4B1E-8D56-4414-94D7-08695692A7EF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntelliPed.FiveM.Client", "IntelliPed.FiveM.Client\IntelliPed.FiveM.Client.csproj", "{A8318136-1CB5-4B6E-99F3-25420ADE1331}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntelliPed.FiveM.Server", "IntelliPed.FiveM.Server\IntelliPed.FiveM.Server.csproj", "{3BB5874B-0236-4961-BECE-A6D38B92EACE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntelliPed.FiveM.Server", "IntelliPed.FiveM.Server\IntelliPed.FiveM.Server.csproj", "{3BB5874B-0236-4961-BECE-A6D38B92EACE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntelliPed.FiveM.Messages", "IntelliPed.FiveM.Messages\IntelliPed.FiveM.Messages.csproj", "{8C3E55A3-6518-45EF-A7E2-F84CBAB064BE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -33,6 +35,10 @@ Global
{3BB5874B-0236-4961-BECE-A6D38B92EACE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3BB5874B-0236-4961-BECE-A6D38B92EACE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3BB5874B-0236-4961-BECE-A6D38B92EACE}.Release|Any CPU.Build.0 = Release|Any CPU
{8C3E55A3-6518-45EF-A7E2-F84CBAB064BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C3E55A3-6518-45EF-A7E2-F84CBAB064BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C3E55A3-6518-45EF-A7E2-F84CBAB064BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C3E55A3-6518-45EF-A7E2-F84CBAB064BE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading