diff --git a/Editor/BuildHooks.cs b/Editor/BuildHooks.cs index 6bec07c00e9d995a3209be171979e0eeaf548011..8d6b52c40b3151b9b4ccacf07d4bb572d64a02a2 100644 --- a/Editor/BuildHooks.cs +++ b/Editor/BuildHooks.cs @@ -18,16 +18,14 @@ public class BuildHooks /// <param name="path"> The build path. </param> [PostProcessBuild(1)] public static void OnPostprocessBuild(BuildTarget target, string path) { - if (path != null) - { + if (path != null) { var buildDir = Path.GetFileNameWithoutExtension(path); var buildHostDir = path.Replace(Path.GetFileName(path), ""); var dataDir = buildDir + "_Data"; var dataPath = Path.Combine(buildHostDir, dataDir); var pluginDir = Path.Combine(dataPath, PLUGIN_DIR); - switch (target) - { + switch (target) { case BuildTarget.StandaloneWindows: RenameLibFile(pluginDir, X86_FOLDER, LSLEditorIntegration.LIB32_NAME, LSLEditorIntegration.LIB64_NAME, LSLEditorIntegration.DLL_ENDING); break; diff --git a/Editor/LSLEditorWindow.cs b/Editor/LSLEditorWindow.cs index bdd312bf5f83b9e6185d73fb7e7e4c781646ea27..490b222f18a712d85c5172fa07652493c9a107ed 100644 --- a/Editor/LSLEditorWindow.cs +++ b/Editor/LSLEditorWindow.cs @@ -20,7 +20,7 @@ public class LSLShowStreamsWindow : EditorWindow private string streamLookUpResult; private ContinuousResolver resolver; - private string lslVersionInfos; + private string lslVersionInfos; public void Init() { @@ -79,8 +79,7 @@ public class LSLShowStreamsWindow : EditorWindow // EditorGUILayout.EndHorizontal(); //} //Replace by LINQ Expression - foreach (string[] s in namesOfStreams.Select(item => item.Split(' '))) - { + foreach (string[] s in namesOfStreams.Select(item => item.Split(' '))) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(new GUIContent(s[0], s[0]), fieldWidth); EditorGUILayout.LabelField(new GUIContent(s[1], s[1]), fieldWidth); @@ -99,8 +98,7 @@ public class LSLShowStreamsWindow : EditorWindow streamInfos = resolver.Results(); if (streamInfos.Length == 0) { streamLookUpResult = NO_STREAMS_FOUND; } - else - { + else { foreach (StreamInfo item in streamInfos) { namesOfStreams.Add($"{item.Name()} {item.Type()} {item.Hostname()} {item.Sampling()}"); } streamLookUpResult = namesOfStreams.Count + N_STREAMS_FOUND; } diff --git a/Editor/ScriptOrderManagement.cs b/Editor/ScriptOrderManagement.cs index 1d5e936b8c102dfce3e5ce9ce190f7edbbef9445..f4c7b70e9189eb3f9ec38ca2263644461aa2f5a4 100644 --- a/Editor/ScriptOrderManagement.cs +++ b/Editor/ScriptOrderManagement.cs @@ -7,12 +7,9 @@ namespace LSL4Unity.Editor { static ScriptOrderManagement() { - foreach (MonoScript monoScript in MonoImporter.GetAllRuntimeMonoScripts()) - { - if (monoScript.GetClass() != null) - { - foreach (Attribute a in Attribute.GetCustomAttributes(monoScript.GetClass(), typeof(ScriptOrder))) - { + foreach (MonoScript monoScript in MonoImporter.GetAllRuntimeMonoScripts()) { + if (monoScript.GetClass() != null) { + foreach (Attribute a in Attribute.GetCustomAttributes(monoScript.GetClass(), typeof(ScriptOrder))) { int currentOrder = MonoImporter.GetExecutionOrder(monoScript); int newOrder = ((ScriptOrder) a).order; if (currentOrder != newOrder) { MonoImporter.SetExecutionOrder(monoScript, newOrder); } diff --git a/LSL.cs b/LSL.cs index b87215c4e842f9395200546b875b1cef65ac8856..768274b9414f1c27a21ee1eac69431c48df323bd 100644 --- a/LSL.cs +++ b/LSL.cs @@ -101,10 +101,7 @@ public static class LSL /// <summary> Helper method to dispose a StreamInfo[] array </summary> /// <param name="arr">Array to be disposed</param> - public static void DisposeArray(this StreamInfo[] array) - { - foreach (var si in array) { si.Dispose(); } - } + public static void DisposeArray(this StreamInfo[] array) { foreach (var si in array) { si.Dispose(); } } /// <summary> Version of the lsl protocol. /// @@ -201,8 +198,7 @@ public static class LSL public static void CheckError(int code) { if (code >= 0) { return; } - switch (code) - { + switch (code) { case -1: throw new System.TimeoutException("The operation failed due to a timeout."); case -2: throw new LostException("The stream has been lost."); case -3: throw new System.ArgumentException("An argument was incorrectly specified (e.g., wrong format or wrong length)."); @@ -280,9 +276,7 @@ public class StreamInfo : LSLObject /// Therefore, it is highly recommended to always try to provide whatever information can uniquely identify the data source itself. </param> public StreamInfo(string name, string type, int channelCount = 1, double sampling = LSL.IRREGULAR_RATE, ChannelFormat channelFormat = LSL4Unity.ChannelFormat.Float32, string sourceId = "") : - base(Dll.lsl_create_streaminfo(name, type, channelCount, sampling, channelFormat, sourceId)) - { - } + base(Dll.lsl_create_streaminfo(name, type, channelCount, sampling, channelFormat, sourceId)) { } /// <summary> Initializes a new instance of the <see cref="StreamInfo"/> class. </summary> /// <param name="handle"> The handle. </param> @@ -423,10 +417,8 @@ public class StreamOutlet : LSLObject /// If unspecified, each push operation yields one chunk. Inlets can override this setting. </param> /// <param name="maxBuffered">Optionally the maximum amount of data to buffer (in seconds if there is a nominal sampling rate, otherwise x100 in samples). /// The default is 6 minutes of data. </param> - public StreamOutlet(StreamInfo info, int chunkSize = 0, int maxBuffered = 360) : base(Dll.lsl_create_outlet(info.DangerousGetHandle(), chunkSize, - maxBuffered)) - { - } + public StreamOutlet(StreamInfo info, int chunkSize = 0, int maxBuffered = 360) : + base(Dll.lsl_create_outlet(info.DangerousGetHandle(), chunkSize, maxBuffered)) { } protected override void DestroyLSLObject(IntPtr obj) { Dll.lsl_destroy_outlet(obj); } @@ -605,9 +597,9 @@ public class StreamInlet : LSLObject /// but this is strongly discouraged and should only ever be done if there is no time to resolve the stream up-front /// (e.g., due to limitations in the client program). </remarks> public StreamInlet(StreamInfo info, int maxBuflen = 360, int maxChunklen = 0, bool recover = true, - ProcessingOptions postproc_flags = ProcessingOptions.None) : base(Dll.lsl_create_inlet(info.DangerousGetHandle(), maxBuflen, maxChunklen, - recover ? 1 : 0)) - { + ProcessingOptions postproc_flags = ProcessingOptions.None) : + base(Dll.lsl_create_inlet(info.DangerousGetHandle(), maxBuflen, maxChunklen, recover ? 1 : 0)) + { Dll.lsl_set_postprocessing(Obj, postproc_flags); } @@ -735,12 +727,10 @@ public class StreamInlet : LSLObject var tmp = new IntPtr[sample.Length]; var res = Dll.lsl_pull_sample_str(Obj, tmp, tmp.Length, timeout, ref ec); LSL.CheckError(ec); - try - { + try { for (var k = 0; k < tmp.Length; k++) { sample[k] = Marshal.PtrToStringAnsi(tmp[k]); } } - finally - { + finally { foreach (var t in tmp) { Dll.lsl_destroy_string(t); } } return res; @@ -815,17 +805,13 @@ public class StreamInlet : LSLObject var tmp = new IntPtr[buffer.GetLength(0), buffer.GetLength(1)]; var res = Dll.lsl_pull_chunk_str(Obj, tmp, times, (uint) tmp.Length, (uint) times.Length, timeout, ref ec); LSL.CheckError(ec); - try - { - for (var s = 0; s < tmp.GetLength(0); s++) - { + try { + for (var s = 0; s < tmp.GetLength(0); s++) { for (var c = 0; c < tmp.GetLength(1); c++) { buffer[s, c] = Marshal.PtrToStringAnsi(tmp[s, c]); } } } - finally - { - for (var s = 0; s < tmp.GetLength(0); s++) - { + finally { + for (var s = 0; s < tmp.GetLength(0); s++) { for (var c = 0; c < tmp.GetLength(1); c++) { Dll.lsl_destroy_string(tmp[s, c]); } } } @@ -870,9 +856,8 @@ public class ContinuousResolver : LSLObject /// <param name="value"> The string value that the property should have (e.g., "EEG" as the type property). </param> /// <param name="forgetAfter"> When a stream is no longer visible on the network (e.g., because it was shut down), /// this is the time in seconds after which it is no longer reported by the resolver. </param> - public ContinuousResolver(string prop, string value, double forgetAfter = 5.0) : base(Dll.lsl_create_continuous_resolver_byprop(prop, value, forgetAfter)) - { - } + public ContinuousResolver(string prop, string value, double forgetAfter = 5.0) : + base(Dll.lsl_create_continuous_resolver_byprop(prop, value, forgetAfter)) { } /// <summary> Initializes a new instance of the <see cref="ContinuousResolver"/> class that resolves all streams that match a given XPath 1.0 predicate. /// diff --git a/OV/OVInlets.cs b/OV/OVInlets.cs index 75fef6fe33bc26aa8dd5ee077def447b542c53ea..22ba63ecae31c865714f32bd16dc53f8ef5309ab 100644 --- a/OV/OVInlets.cs +++ b/OV/OVInlets.cs @@ -30,8 +30,7 @@ public abstract class OVInlet<T> : MonoBehaviour { bool hasAName = streamName.Length != 0; - if (!hasAName) - { + if (!hasAName) { Debug.LogError("Inlet has to specify a name or a type before it is able to lookup a stream."); enabled = false; return; @@ -43,32 +42,22 @@ public abstract class OVInlet<T> : MonoBehaviour } /// <summary> Fixupdate is called once per physics framerate. </summary> - private void FixedUpdate() - { - if (moment == UpdateMoment.FixedUpdate && inlet != null) { PullSamples(); } - } + private void FixedUpdate() { if (moment == UpdateMoment.FixedUpdate && inlet != null) { PullSamples(); } } /// <summary> Update is called once per frame. </summary> - private void Update() - { - if (moment == UpdateMoment.Update && inlet != null) { PullSamples(); } - } + private void Update() { if (moment == UpdateMoment.Update && inlet != null) { PullSamples(); } } /// <summary> ForceUpdate is called when it's needed. </summary> public void ForceUpdate() { PullSamples(); } /// <summary> Start coroutine to resolve stream. </summary> - public void ResolveStream() - { - if (inlet == null) { StartCoroutine(ResolveExpectedStream()); } - } + public void ResolveStream() { if (inlet == null) { StartCoroutine(ResolveExpectedStream()); } } /// <summary> Check if inlet is created. </summary> public bool IsSolved() { return (inlet != null); } public void PopSamples() { samples = null; } - private void CreateInlet(StreamInfo result) { //Debug.Log($"Resolving Stream : Name = {streamName}, Steam Info Name = {result.Name()}, Stream Info Type = ({result.Type()}"); @@ -109,20 +98,17 @@ public abstract class OVFloatInlet : OVInlet<float> { samples = new float[expectedChannels]; - try - { + try { double lastTimeStamp = inlet.PullSample(samples, 0.0f); - if (Math.Abs(lastTimeStamp) > Constants.TOLERANCE) - { + if (Math.Abs(lastTimeStamp) > Constants.TOLERANCE) { // do not miss the first one found Process(samples, lastTimeStamp); // pull as long samples are available while (Math.Abs(lastTimeStamp = inlet.PullSample(samples, 0.0f)) > Constants.TOLERANCE) { Process(samples, lastTimeStamp); } } } - catch (ArgumentException e) - { + catch (ArgumentException e) { Debug.LogError("An Error on pulling samples deactivating LSL inlet on...", this); enabled = false; Debug.LogException(e, this); @@ -139,20 +125,17 @@ public abstract class OVDoubleInlet : OVInlet<double> { samples = new double[expectedChannels]; - try - { + try { double lastTimeStamp = inlet.PullSample(samples, 0.0f); - if (Math.Abs(lastTimeStamp) > Constants.TOLERANCE) - { + if (Math.Abs(lastTimeStamp) > Constants.TOLERANCE) { // do not miss the first one found Process(samples, lastTimeStamp); // pull as long samples are available while (Math.Abs(lastTimeStamp = inlet.PullSample(samples, 0.0f)) > Constants.TOLERANCE) { Process(samples, lastTimeStamp); } } } - catch (ArgumentException e) - { + catch (ArgumentException e) { Debug.LogError("An Error on pulling samples deactivating LSL inlet on...", this); enabled = false; Debug.LogException(e, this); @@ -169,20 +152,17 @@ public abstract class OVIntInlet : OVInlet<int> { samples = new int[expectedChannels]; - try - { + try { double lastTimeStamp = inlet.PullSample(samples, 0.0f); - if (Math.Abs(lastTimeStamp) > Constants.TOLERANCE) - { + if (Math.Abs(lastTimeStamp) > Constants.TOLERANCE) { // do not miss the first one found Process(samples, lastTimeStamp); // pull as long samples are available while (Math.Abs(lastTimeStamp = inlet.PullSample(samples, 0.0f)) > Constants.TOLERANCE) { Process(samples, lastTimeStamp); } } } - catch (ArgumentException e) - { + catch (ArgumentException e) { Debug.LogError("An Error on pulling samples deactivating LSL inlet on...", this); enabled = false; Debug.LogException(e, this); diff --git a/OV/OVOutlets.cs b/OV/OVOutlets.cs index fba454c10fc62d90db496deadb0f35fdc7fe3526..694e18513e8e4247eafcd90a832d8f2cfe0dbcfa 100644 --- a/OV/OVOutlets.cs +++ b/OV/OVOutlets.cs @@ -9,10 +9,7 @@ public abstract class OVOutlet<T> : MonoBehaviour { private enum UpdateMoment { FixedUpdate, Update, OnDemand } - protected enum StreamTypes - { - /* Matrix, Stimulation,*/ Double, Float, Int - } + protected enum StreamTypes { /* Matrix, Stimulation,*/ Double, Float, Int } [SerializeField] private UpdateMoment moment = UpdateMoment.OnDemand; [SerializeField] private string streamName = "ovSignal"; @@ -23,14 +20,13 @@ public abstract class OVOutlet<T> : MonoBehaviour protected StreamOutlet outlet; protected StreamInfo info; - protected T[] samples; + protected T[] samples; protected StreamTypes streamType = StreamTypes.Int; private new string GetType() { - switch (streamType) - { + switch (streamType) { //case StreamTypes.Matrix: return ""; //case StreamTypes.Stimulation: return "Markers"; case StreamTypes.Double: return "signal"; @@ -42,8 +38,7 @@ public abstract class OVOutlet<T> : MonoBehaviour private ChannelFormat GetFormat() { - switch (streamType) - { + switch (streamType) { case StreamTypes.Double: return ChannelFormat.Double64; case StreamTypes.Float: return ChannelFormat.Float32; case StreamTypes.Int: return ChannelFormat.Int32; @@ -62,23 +57,16 @@ public abstract class OVOutlet<T> : MonoBehaviour } /// <summary> Fixupdate is called once per physics framerate. </summary> - private void FixedUpdate() - { - if (moment == UpdateMoment.FixedUpdate && outlet != null) { PushSamples(); } - } + private void FixedUpdate() { if (moment == UpdateMoment.FixedUpdate && outlet != null) { PushSamples(); } } /// <summary> Update is called once per frame. </summary> - private void Update() - { - if (moment == UpdateMoment.Update && outlet != null) { PushSamples(); } - } + private void Update() { if (moment == UpdateMoment.Update && outlet != null) { PushSamples(); } } /// <summary> ForceUpdate is called when it's needed. </summary> /// <param name="input"> The samples to push. </param> public void ForceUpdate(T[] input) { - if (outlet != null) - { + if (outlet != null) { Process(input); PushSamples(); } diff --git a/OV/Template/MatrixInlet.cs b/OV/Template/MatrixInlet.cs index e0910b47fcf807a8d98be35c82685f7f483526b7..05ece5ca2605cb1ce409e204483956ec7b23e986 100644 --- a/OV/Template/MatrixInlet.cs +++ b/OV/Template/MatrixInlet.cs @@ -15,10 +15,7 @@ public class MatrixInlet : OVDoubleInlet private void ResetMatrix() { - for (int i = 0; i < nChannel; i++) - { - for (int j = 0; j < nSample; j++) { matrix[i, j] = 0; } - } + for (int i = 0; i < nChannel; i++) { for (int j = 0; j < nSample; j++) { matrix[i, j] = 0; } } curChannel = 0; curSample = 0; } @@ -26,25 +23,21 @@ public class MatrixInlet : OVDoubleInlet protected override void Process(double[] input, double time) { if (nChannel == -1) { nChannel = (int) input[0]; } - else if (nSample == -1) - { + else if (nSample == -1) { nSample = (int) input[0]; matrix = new double[nChannel, nSample]; ResetMatrix(); } - else - { + else { // If We have complete the matrix if (curChannel == nChannel && curSample == nSample) { ResetMatrix(); } // Update Row and column - if (curSample == nSample) - { + if (curSample == nSample) { curChannel++; curSample = 0; } else { curSample++; } - // If Now the matrix is completed if (curChannel == nChannel && curSample == nSample) { readyToSend = true; } } diff --git a/Scripts/LSLCommon.cs b/Scripts/LSLCommon.cs index 029ab44d894e4f75e3632ea61782b0416e03c691..57cc91092f0783f2bc5ff873ae2a4fa06294503b 100644 --- a/Scripts/LSLCommon.cs +++ b/Scripts/LSLCommon.cs @@ -14,18 +14,15 @@ public static class LSLUtils /// <returns></returns> public static float GetSamplingRateFor(MomentForSampling moment, bool setRefreshRateToDisplay = true) { - switch (moment) - { + switch (moment) { case MomentForSampling.FixedUpdate: return 1000 / (1000 * Time.fixedDeltaTime); case MomentForSampling.Update: case MomentForSampling.LateUpdate: { - if (Application.targetFrameRate == DEFAULT_PLATTFORM_SPECIFIC_FRAMERATE && !setRefreshRateToDisplay) - { + if (Application.targetFrameRate == DEFAULT_PLATTFORM_SPECIFIC_FRAMERATE && !setRefreshRateToDisplay) { throw new InvalidOperationException("When using Update or LateUpdate as sampling moment - specify a target frameRate"); } - if (setRefreshRateToDisplay) - { + if (setRefreshRateToDisplay) { Debug.LogWarning("Application.targetFrameRate get set to Screen.currentResolution.refreshRate!"); Application.targetFrameRate = Screen.currentResolution.refreshRate; } diff --git a/Scripts/LSLOutlet.cs b/Scripts/LSLOutlet.cs index 392a9720b88fb4319a829b89bf608f5cc7351062..676235afc8fbf4f2fc89ccb47f7e2dfa68146e8b 100644 --- a/Scripts/LSLOutlet.cs +++ b/Scripts/LSLOutlet.cs @@ -9,7 +9,7 @@ public class LSLOutlet : MonoBehaviour { private StreamOutlet outlet; private StreamInfo info; - private float[] sample; + private float[] sample; public string streamName = "Unity.ExampleStream"; public string streamType = "Unity.FixedUpdateTime"; diff --git a/Scripts/LSLTransformOutlet.cs b/Scripts/LSLTransformOutlet.cs index 73ab5576e94c54d9bd67b8d09b640903e3068ca9..89ad37030e0242f75f8d27efd2d3ae2f65edcf0e 100644 --- a/Scripts/LSLTransformOutlet.cs +++ b/Scripts/LSLTransformOutlet.cs @@ -46,8 +46,7 @@ public class LSLTransformOutlet : MonoBehaviour // it's not possible to create a XMLElement before and append it. var chns = streamInfo.Desc().AppendChild("channels"); // so this workaround has been introduced. - foreach (var def in channelDefinitions) - { + foreach (var def in channelDefinitions) { chns.AppendChild("channel").AppendChildValue("label", def.label).AppendChildValue("unit", def.unit).AppendChildValue("type", def.type); } @@ -67,8 +66,7 @@ public class LSLTransformOutlet : MonoBehaviour { int offset = -1; - if (rotationAsQuaternion) - { + if (rotationAsQuaternion) { var rotation = sampleSource.rotation; sample[++offset] = rotation.x; @@ -76,16 +74,14 @@ public class LSLTransformOutlet : MonoBehaviour sample[++offset] = rotation.z; sample[++offset] = rotation.w; } - if (rotationAsEuler) - { + if (rotationAsEuler) { var rotation = sampleSource.rotation.eulerAngles; sample[++offset] = rotation.x; sample[++offset] = rotation.y; sample[++offset] = rotation.z; } - if (position) - { + if (position) { var pos = sampleSource.position; sample[++offset] = pos.x; @@ -103,23 +99,20 @@ public class LSLTransformOutlet : MonoBehaviour { var list = new List<ChannelDefinition>(); - if (rotationAsQuaternion) - { + if (rotationAsQuaternion) { string[] quatlabels = { "x", "y", "z", "w" }; list.AddRange(quatlabels.Select(item => new ChannelDefinition { label = item, unit = "unit quaternion", type = "quaternion component" })); } - if (rotationAsEuler) - { + if (rotationAsEuler) { string[] eulerLabels = { "x", "y", "z" }; list.AddRange(eulerLabels.Select(item => new ChannelDefinition { label = item, unit = "degree", type = "axis angle" })); } - if (position) - { + if (position) { string[] eulerLabels = { "x", "y", "z" }; list.AddRange(eulerLabels.Select(item => new ChannelDefinition { label = item, unit = "meter", type = "position in world space" })); diff --git a/Scripts/Resolver.cs b/Scripts/Resolver.cs index 956b85e6ac55d1fe4539f9ed87708fa6e074f98f..d30b69d3cd60c7fcce4501488beaeee026540741 100644 --- a/Scripts/Resolver.cs +++ b/Scripts/Resolver.cs @@ -16,7 +16,7 @@ public class Resolver : MonoBehaviour, IEventSystemHandler public float forgetStreamAfter = 1.0f; public List<LSLStreamInfoWrapper> streams; - private ContinuousResolver resolver; + private ContinuousResolver resolver; /// <summary> Use this for initialization. </summary> private void Start() @@ -30,8 +30,7 @@ public class Resolver : MonoBehaviour, IEventSystemHandler var result = streams.Where(i => (streamName.Length == 0 || i.name.Equals(streamName)) && (streamType.Length == 0 || i.type.Equals(streamType)) && (hostName.Length == 0 || i.type.Equals(hostName))).ToList(); - if (result.Any()) - { + if (result.Any()) { info = result.First(); return true; } @@ -41,8 +40,7 @@ public class Resolver : MonoBehaviour, IEventSystemHandler private IEnumerator ResolveContinuously() { - while (true) - { + while (true) { var results = resolver.Results(); //foreach (var item in streams) { @@ -57,10 +55,8 @@ public class Resolver : MonoBehaviour, IEventSystemHandler streams.RemoveAll(s => !results.Any(r => r.Name().Equals(s.name))); // add new found streams to the cache - foreach (var item in results) - { - if (!streams.Any(s => s.name == item.Name() && s.type == item.Type())) - { + foreach (var item in results) { + if (!streams.Any(s => s.name == item.Name() && s.type == item.Type())) { Debug.Log($"Found new Stream {item.Name()}"); var newStreamInfo = new LSLStreamInfoWrapper(item);