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

[WIP] Remote #29

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
13 changes: 13 additions & 0 deletions Brio.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@ VisualStudioVersion = 17.8.34316.72
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Brio", "Brio\Brio.csproj", "{6E14631E-8223-427D-8A03-550EEE66B842}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WpfRemote", "WpfRemote\WpfRemote.csproj", "{7A54ABF2-1053-4446-AA79-4ADB666184D5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6E14631E-8223-427D-8A03-550EEE66B842}.Debug|Any CPU.ActiveCfg = Debug|x64
{6E14631E-8223-427D-8A03-550EEE66B842}.Debug|Any CPU.Build.0 = Debug|x64
{6E14631E-8223-427D-8A03-550EEE66B842}.Debug|x64.ActiveCfg = Debug|x64
{6E14631E-8223-427D-8A03-550EEE66B842}.Debug|x64.Build.0 = Debug|x64
{6E14631E-8223-427D-8A03-550EEE66B842}.Release|Any CPU.ActiveCfg = Release|x64
{6E14631E-8223-427D-8A03-550EEE66B842}.Release|Any CPU.Build.0 = Release|x64
{6E14631E-8223-427D-8A03-550EEE66B842}.Release|x64.ActiveCfg = Release|x64
{6E14631E-8223-427D-8A03-550EEE66B842}.Release|x64.Build.0 = Release|x64
{7A54ABF2-1053-4446-AA79-4ADB666184D5}.Debug|Any CPU.ActiveCfg = Debug|x64
{7A54ABF2-1053-4446-AA79-4ADB666184D5}.Debug|x64.ActiveCfg = Debug|x64
{7A54ABF2-1053-4446-AA79-4ADB666184D5}.Debug|x64.Build.0 = Debug|x64
{7A54ABF2-1053-4446-AA79-4ADB666184D5}.Release|Any CPU.ActiveCfg = Release|x64
{7A54ABF2-1053-4446-AA79-4ADB666184D5}.Release|x64.ActiveCfg = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 4 additions & 0 deletions Brio/Brio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Brio.Game.Posing;
using Brio.Game.World;
using Brio.IPC;
using Brio.Remote;
using Brio.Resources;
using Brio.UI;
using Brio.UI.Windows;
Expand Down Expand Up @@ -137,6 +138,9 @@ private IServiceCollection SetupServices(DalamudServices dalamudServices)
serviceCollection.AddSingleton<CameraService>();
serviceCollection.AddSingleton<ObjectMonitorService>();

// Remote
serviceCollection.AddSingleton<RemoteService>();

// UI
serviceCollection.AddSingleton<UIManager>();
serviceCollection.AddSingleton<MainWindow>();
Expand Down
2 changes: 2 additions & 0 deletions Brio/Brio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

<ItemGroup>
<PackageReference Include="DalamudPackager" Version="2.1.12" />
<PackageReference Include="EasyTcp" Version="4.0.2" />
<PackageReference Include="EmbedIO" Version="3.5.2" />
<PackageReference Include="MessagePack" Version="2.5.140" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="OneOf" Version="3.0.263" />
<PackageReference Include="OneOf.SourceGenerator" Version="3.0.263" />
Expand Down
38 changes: 38 additions & 0 deletions Brio/Remote/BoneMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Brio.Game.Posing.Skeletons;
using MessagePack;

namespace Brio.Remote;

[MessagePackObject]
public class BoneMessage
{
[Key(00)] public string? Name { get; set; }
[Key(01)] public string? DisplayName { get; set; }
[Key(02)] public float PositionX { get; set; }
[Key(03)] public float PositionY { get; set; }
[Key(04)] public float PositionZ { get; set; }
[Key(05)] public float ScaleX { get; set; }
[Key(06)] public float ScaleY { get; set; }
[Key(07)] public float ScaleZ { get; set; }
[Key(08)] public float RotationX { get; set; }
[Key(09)] public float RotationY { get; set; }
[Key(10)] public float RotationZ { get; set; }
[Key(11)] public float RotationW { get; set; }

internal void FromBone(Bone bone)
{
this.Name = bone.Name;
this.DisplayName = bone.FriendlyName;

this.PositionX = bone.LastTransform.Position.X;
this.PositionY = bone.LastTransform.Position.Y;
this.PositionZ = bone.LastTransform.Position.Z;
this.ScaleX = bone.LastTransform.Scale.X;
this.ScaleY = bone.LastTransform.Scale.Y;
this.ScaleZ = bone.LastTransform.Scale.Z;
this.RotationX = bone.LastTransform.Rotation.X;
this.RotationY = bone.LastTransform.Rotation.Y;
this.RotationZ = bone.LastTransform.Rotation.Z;
this.RotationW = bone.LastTransform.Rotation.W;
}
}
6 changes: 6 additions & 0 deletions Brio/Remote/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Brio.Remote;

public static class Configuration
{
public const int Port = 1200;
}
9 changes: 9 additions & 0 deletions Brio/Remote/Heartbeat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MessagePack;

namespace Brio.Remote;

[MessagePackObject]
public class Heartbeat
{
[Key(00)] public int Count { get; set; }
}
103 changes: 103 additions & 0 deletions Brio/Remote/RemoteService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using Brio.Capabilities.Posing;
using Brio.Entities;
using Brio.Entities.Actor;
using Brio.Game.Posing;
using Brio.Game.Posing.Skeletons;
using EasyTcp4;
using EasyTcp4.ServerUtils;
using MessagePack;
using System;
using System.Threading.Tasks;

namespace Brio.Remote;
internal class RemoteService : IDisposable
{
public const int SyncMs = 33;

private readonly EntityManager _entityManager;
private EasyTcpServer? _server;

public RemoteService(EntityManager entityManager)
{
_entityManager = entityManager;

StartServer();
}

public bool StartServer()
{
if(_server != null)
throw new Exception("Attempt to start IPC server while it is already running");

_server = new();
_server.EnableServerKeepAlive();
_server.OnDataReceive += OnDataReceived;
_server.OnError += (s, e) => Brio.Log.Error(e, "Remote error");
_server.OnConnect += (s, e) => Brio.Log.Info("Remote client connected");
_server.OnDisconnect += (s, e) => Brio.Log.Info("Remote client disconnected");

_server.Start(Configuration.Port);

Task.Run(Synchronizer);

return _server.IsRunning;
}

public void Dispose()
{
_server?.Dispose();
_server = null;
}

public void Send(object obj)
{
byte[] data = MessagePackSerializer.Typeless.Serialize(obj);
_server.SendAll(data);
}

private void OnDataReceived(object? sender, Message e)
{
object? obj = MessagePackSerializer.Typeless.Deserialize(e.Data);
}

private async Task Synchronizer()
{
while(_server != null)
{
await Task.Delay(SyncMs);

if (_entityManager.SelectedEntity is ActorEntity actor)
{
PosingCapability? posing;
if (actor.TryGetCapability<PosingCapability>(out posing))
{
SynchronizePosing(posing);
}
}
}
}

private void SynchronizePosing(PosingCapability posing)
{
posing.Selected.Switch(
bone =>
{
Bone? realBone = posing.SkeletonPosing.GetBone(bone);
if(realBone != null && realBone.Skeleton.IsValid)
{
BoneMessage boneMessage = new();
boneMessage.FromBone(realBone);
Send(boneMessage);
}
},
_ =>
{
// Model
},
_ =>
{
// Model
}
);
}
}
Loading
Loading