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

Binding generator safety, IMU component #30

Merged
merged 8 commits into from
May 1, 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
36 changes: 18 additions & 18 deletions ProjectObsidian.SourceGenerators/BindingGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private class OrderedCount
private readonly string MethodReturnType;
public string CountOverride => VariableNames.Count == 0
? ""
: $" public override int {CountVariableName} => base.{CountVariableName} + {VariableNames.Count};";
: $" public override int {CountVariableName} => base.{CountVariableName} + {VariableNames.Count};\n";

public void Add(string value) => VariableNames.Add(value);

Expand Down Expand Up @@ -117,7 +117,7 @@ public OrderedCount(string countVariableName, string methodName, string methodRe
private readonly OrderedCount _inputCount = new("NodeInputCount", "GetInputInternal", "ISyncRef");
private readonly OrderedCount _outputCount = new("NodeOutputCount", "GetOutputInternal", "INodeOutput");
private readonly OrderedCount _impulseCount = new("NodeImpulseCount", "GetImpulseInternal", "ISyncRef");
private readonly OrderedCount _operationCount = new("NodeImpulseCount", "GetImpulseInternal", "INodeOperation");
private readonly OrderedCount _operationCount = new("NodeOperationCount", "GetOperationInternal", "INodeOperation");

private readonly OrderedCount _inputListCount = new("NodeInputListCount", "GetInputListInternal", "ISyncList");
private readonly OrderedCount _outputListCount = new("NodeOutputListCount", "GetOutputListInternal", "ISyncList");
Expand Down Expand Up @@ -147,7 +147,7 @@ public string Result
namespace {BindingPrefix}{_currentNameSpace};

[Category(new string[] {{""ProtoFlux/Runtimes/Execution/Nodes/{_category}""}})]
public partial class {_fullName} : {_baseType}
public partial class {_fullName} : global::FrooxEngine.ProtoFlux.Runtimes.Execution.{_baseType}
{{
{Declarations}
{_nodeNameOverride}
Expand Down Expand Up @@ -203,37 +203,37 @@ public override void VisitFieldDeclaration(FieldDeclarationSyntax node)
var name = node.Declaration.Variables.First().ToString();

//inputs
TypedFieldDetection(type, name, "ObjectInput", "SyncRef<INodeObjectOutput<{1}>>", _inputCount);
TypedFieldDetection(type, name, "ObjectArgument", "SyncRef<INodeObjectOutput<{1}>>", _inputCount);
TypedFieldDetection(type, name, "ValueInput", "SyncRef<INodeValueOutput<{1}>>", _inputCount);
TypedFieldDetection(type, name, "ValueArgument", "SyncRef<INodeValueOutput<{1}>>", _inputCount);
TypedFieldDetection(type, name, "ObjectInput", "global::FrooxEngine.SyncRef<global::FrooxEngine.ProtoFlux.INodeObjectOutput<{1}>>", _inputCount);
TypedFieldDetection(type, name, "ObjectArgument", "global::FrooxEngine.SyncRef<global::FrooxEngine.ProtoFlux.INodeObjectOutput<{1}>>", _inputCount);
TypedFieldDetection(type, name, "ValueInput", "global::FrooxEngine.SyncRef<global::FrooxEngine.ProtoFlux.INodeValueOutput<{1}>>", _inputCount);
TypedFieldDetection(type, name, "ValueArgument", "global::FrooxEngine.SyncRef<global::FrooxEngine.ProtoFlux.INodeValueOutput<{1}>>", _inputCount);

//outputs
TypedFieldDetection(type, name, "ObjectOutput", "NodeObjectOutput<{1}>", _outputCount);
TypedFieldDetection(type, name, "ValueOutput", "NodeValueOutput<{1}>", _outputCount);
TypedFieldDetection(type, name, "ObjectOutput", "global::FrooxEngine.ProtoFlux.NodeObjectOutput<{1}>", _outputCount);
TypedFieldDetection(type, name, "ValueOutput", "global::FrooxEngine.ProtoFlux.NodeValueOutput<{1}>", _outputCount);

//impulses
if (!UntypedFieldDetection(type, name, "AsyncCall", "SyncRef<INodeOperation>", _impulseCount))
UntypedFieldDetection(type, name, "Call", "SyncRef<ISyncNodeOperation>", _impulseCount);
UntypedFieldDetection(type, name, "Continuation", "SyncRef<INodeOperation>", _impulseCount);
UntypedFieldDetection(type, name, "AsyncResumption", "SyncRef<INodeOperation>", _impulseCount);
if (!UntypedFieldDetection(type, name, "AsyncCall", "global::FrooxEngine.SyncRef<global::FrooxEngine.ProtoFlux.INodeOperation>", _impulseCount))
UntypedFieldDetection(type, name, "Call", "global::FrooxEngine.SyncRef<global::FrooxEngine.ProtoFlux.ISyncNodeOperation>", _impulseCount);
UntypedFieldDetection(type, name, "Continuation", "global::FrooxEngine.SyncRef<global::FrooxEngine.ProtoFlux.INodeOperation>", _impulseCount);
UntypedFieldDetection(type, name, "AsyncResumption", "global::FrooxEngine.SyncRef<global::FrooxEngine.ProtoFlux.INodeOperation>", _impulseCount);

//operations
UntypedFieldDetection(type, name, "Operation", "SyncNodeOperation", _operationCount);
UntypedFieldDetection(type, name, "Operation", "global::FrooxEngine.ProtoFlux.SyncNodeOperation", _operationCount);

//lists

//input lists
TypedFieldDetection(type, name, "ValueInputList", "SyncRefList<INodeValueOutput<{1}>>", _inputListCount);
TypedFieldDetection(type, name, "ValueInputList", "global::FrooxEngine.SyncRefList<global::FrooxEngine.ProtoFlux.INodeValueOutput<{1}>>", _inputListCount);

//output lists
TypedFieldDetection(type, name, "ObjectInputList", "SyncRefList<INodeObjectOutput<{1}>>", _outputListCount);
TypedFieldDetection(type, name, "ObjectInputList", "global::FrooxEngine.SyncRefList<global::FrooxEngine.ProtoFlux.INodeObjectOutput<{1}>>", _outputListCount);

//impulse lists
UntypedFieldDetection(type, name, "ContinuationList", "SyncRefList<INodeOperation>", _impulseListCount);
UntypedFieldDetection(type, name, "ContinuationList", "global::FrooxEngine.SyncRefList<global::FrooxEngine.ProtoFlux.INodeOperation>", _impulseListCount);

//operation lists
UntypedFieldDetection(type, name, "SyncOperationList", "SyncList<SyncNodeOperation>", _operationListCount);
UntypedFieldDetection(type, name, "SyncOperationList", "global::FrooxEngine.SyncList<global::FrooxEngine.ProtoFlux.SyncNodeOperation>", _operationListCount);

base.VisitFieldDeclaration(node);
}
Expand Down
93 changes: 93 additions & 0 deletions ProjectObsidian/Components/Users/ImuInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Runtime.InteropServices;
using Elements.Core;
using Valve.VR;

namespace FrooxEngine;

[Category("Obsidian/Devices")]
public class ImuInfo : Component
{
public readonly Sync<bool> Simulate;
public readonly SyncRef<User> SimulatingUser;
public readonly Sync<string> Path;
public readonly Sync<bool> Connected;
public readonly Sync<double> SampleTime;
public readonly Sync<double3> VAccel;
public readonly Sync<double3> VGyro;
public readonly Sync<Imu_OffScaleFlags> OffScaleFlags;
private ulong _buffer;
private string _previousPath;
private bool _previousSimulating;

~ImuInfo()
{
CloseBuffer();
}

private void CloseBuffer()
{
if (_buffer == 0) return;
OpenVR.IOBuffer.Close(_buffer);
_previousPath = null;
_buffer = 0;
Connected.Value = false;
}
protected override void OnCommonUpdate()
{
base.OnCommonUpdate();

if (Simulate.Value && LocalUser == SimulatingUser.Target)
{
if (!LocalUser.VR_Active || OpenVR.IOBuffer is null || string.IsNullOrEmpty(Path.Value)) CloseBuffer();
else if (_buffer == 0 || _previousPath != Path.Value)
{
CloseBuffer();
unsafe
{
ulong buffer = default;
//TODO: make this not run every frame if it fails
var errorCode = OpenVR.IOBuffer.Open(Path.Value, EIOBufferMode.Read, (uint)sizeof(ImuSample_t), 0u, ref buffer);
UniLog.Log($"{buffer}, {errorCode.ToString()}");
if (errorCode == EIOBufferError.IOBuffer_Success)
{
_buffer = buffer;
_previousPath = Path.Value;
}
}
}
if (_buffer != 0)
{
unsafe
{
var punRead = 0u;
var imuSample_t = default(ImuSample_t);

var error = OpenVR.IOBuffer.Read(_buffer, (IntPtr)(&imuSample_t), (uint)sizeof(ImuSample_t), ref punRead);

if (error == EIOBufferError.IOBuffer_Success && punRead == sizeof(ImuSample_t))
{
var fSampleTime = imuSample_t.fSampleTime;
var vAccel = new double3(imuSample_t.vAccel.v0, imuSample_t.vAccel.v1, imuSample_t.vAccel.v2);
var vGyro = new double3(imuSample_t.vGyro.v0, imuSample_t.vGyro.v1, imuSample_t.vGyro.v2);
var flags = (Imu_OffScaleFlags)imuSample_t.unOffScaleFlags;

SampleTime.Value = fSampleTime;
VAccel.Value = vAccel;
VGyro.Value = vGyro;
OffScaleFlags.Value = flags;
}
}
}
}
else
{
CloseBuffer();
}

if (LocalUser != SimulatingUser.Target) return;

var connectedValue = _buffer != 0;
if (connectedValue != Connected.Value) Connected.Value = connectedValue;
}
}