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

Barclah/multiplayer integration #957

Closed
wants to merge 18 commits into from
Closed
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
499 changes: 341 additions & 158 deletions api/Api/Aether/Lobby/LobbyClient.cs

Large diffs are not rendered by default.

265 changes: 210 additions & 55 deletions api/Api/Aether/Lobby/LobbyServer.cs

Large diffs are not rendered by default.

36 changes: 19 additions & 17 deletions api/Api/Aether/Proto/ServerTransformData.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
using UnityEngine;

public partial class ServerTransformData {
public static explicit operator ServerTransformData(Matrix4x4 m)
=> new ServerTransformData() {
MatrixData = {
m[0, 0], m[0, 1], m[0, 2], m[0, 3],
m[1, 0], m[1, 1], m[1, 2], m[1, 3],
m[2, 0], m[2, 1], m[2, 2], m[2, 3],
m[3, 0], m[3, 1], m[3, 2], m[3, 3]
}
};
namespace SynthesisAPI.Aether {
public partial class ServerTransformData {
public static explicit operator ServerTransformData(Matrix4x4 m)
=> new ServerTransformData() {
MatrixData = {
m[0, 0], m[0, 1], m[0, 2], m[0, 3],
m[1, 0], m[1, 1], m[1, 2], m[1, 3],
m[2, 0], m[2, 1], m[2, 2], m[2, 3],
m[3, 0], m[3, 1], m[3, 2], m[3, 3]
}
};

public static explicit operator Matrix4x4(ServerTransformData transform)
=> new Matrix4x4(
new Vector4(transform.MatrixData[0], transform.MatrixData[4], transform.MatrixData[8], transform.MatrixData[12]),
new Vector4(transform.MatrixData[1], transform.MatrixData[5], transform.MatrixData[9], transform.MatrixData[13]),
new Vector4(transform.MatrixData[2], transform.MatrixData[6], transform.MatrixData[10], transform.MatrixData[14]),
new Vector4(transform.MatrixData[3], transform.MatrixData[7], transform.MatrixData[11], transform.MatrixData[15])
);
public static explicit operator Matrix4x4(ServerTransformData transform)
=> new Matrix4x4(
new Vector4(transform.MatrixData[0], transform.MatrixData[4], transform.MatrixData[8], transform.MatrixData[12]),
new Vector4(transform.MatrixData[1], transform.MatrixData[5], transform.MatrixData[9], transform.MatrixData[13]),
new Vector4(transform.MatrixData[2], transform.MatrixData[6], transform.MatrixData[10], transform.MatrixData[14]),
new Vector4(transform.MatrixData[3], transform.MatrixData[7], transform.MatrixData[11], transform.MatrixData[15])
);
}
}
5 changes: 5 additions & 0 deletions api/Api/Aether/Proto/SynthesisDataDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace SynthesisAPI.Aether {
public partial class SynthesisDataDescriptor {

}
}
9 changes: 9 additions & 0 deletions api/Api/Simulation/SimObject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using SynthesisAPI.Aether;
using SynthesisAPI.Controller;
using Mirabuf.Signal;

Expand Down Expand Up @@ -47,6 +48,14 @@ public SimObject(string name, ControllableState state) {
_state = state;
}

public virtual void SetRemoteTransformData(ServerTransforms transformData) {

}

public virtual ServerTransforms GetRemoteTransformData() {
return new ServerTransforms();
}

public virtual void Destroy() { }

public List<(string key, string displayName)> GetAllReservedInputs() {
Expand Down
60 changes: 52 additions & 8 deletions api/Api/Utilities/Atomic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,73 @@

namespace SynthesisAPI.Utilities {

public class Atomic<T> where T: struct {
private readonly ReaderWriterLockSlim _lock;
private T _value;
/// <summary>
/// Multiple reads, one write allowed. Threadsafe value-type data. ReadOnly
/// </summary>
/// <typeparam name="T">Value Type</typeparam>
public class AtomicReadOnly<T> where T : struct {
protected readonly ReaderWriterLockSlim _lock;
protected T _value;

/// <summary>
/// Threadsafe Data Accessor. Data is copied on get
/// </summary>
public T Value {
get {
_lock.EnterReadLock();
var result = _value;
_lock.ExitReadLock();
return result;
}
}

/// <summary>
/// Constructs an Atomic with read only access to the data
/// </summary>
/// <param name="val"></param>
public AtomicReadOnly(T val) {
_value = val;
_lock = new ReaderWriterLockSlim();
}

public static implicit operator T(AtomicReadOnly<T> atomic) => atomic.Value;
}

/// <summary>
/// Multiple reads, one write allowed. Threadsafe value-type data
/// </summary>
/// <typeparam name="T">Value Type</typeparam>
public class Atomic<T> : AtomicReadOnly<T> where T : struct {

/// <summary>
/// Threadsafe Data Accessor. Data is copied on get
/// </summary>
public new T Value {
get {
_lock.EnterReadLock();
var result = _value;
_lock.ExitReadLock();
return result;
}
set {
_lock.EnterWriteLock();
_value = value;
_lock.ExitWriteLock();
}
}

public Atomic(T val) {
_value = val;
_lock = new ReaderWriterLockSlim();
}
/// <summary>
/// Constructs a new Atomic with read and write access to the stored data
/// </summary>
/// <param name="val">Data to store in Atomic</param>
/// <returns></returns>
public Atomic(T val) : base(val) { }

public static implicit operator T(Atomic<T> atomic) => atomic.Value;
/// <summary>
/// Converts Atomic to a ReadOnlyAtomic
/// </summary>
/// <returns><see cref="SynthesisAPI.Utilities.AtomicReadOnly{T}" /></returns>
public AtomicReadOnly<T> AsReadOnly() => this;
}

}
56 changes: 56 additions & 0 deletions api/Api/Utilities/ByteStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace SynthesisAPI.Utilities {

/// <summary>
/// Creates a read-only stream from a byte enumerator
/// </summary>
public class ByteStream : Stream {
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;

private readonly long _length;
public override long Length => _length;

private long _alreadyRead = 0;
public override long Position { get => _alreadyRead; set => throw new System.NotImplementedException(); }

private readonly IEnumerator<byte> _enumer;

/// <summary>
/// Constructs a stream from a byte enumerator
/// </summary>
/// <param name="enumer">Enumerator or byte data</param>
/// <param name="length">Length of byte data</param>
public ByteStream(IEnumerator<byte> enumer, long length) {
_enumer = enumer;
_length = length;
}

public override void Flush() { }

public override int Read(byte[] buffer, int offset, int count) {
int position = offset;
while (position < buffer.Length && position < offset + count && _enumer.MoveNext()) {
buffer[position] = _enumer.Current;
_alreadyRead++;
position++;
}
return position - offset;
}

public override long Seek(long offset, SeekOrigin origin) {
throw new NotImplementedException();
}
public override void SetLength(long value) {
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count) {
throw new NotImplementedException();
}
}

}
14 changes: 14 additions & 0 deletions api/Api/Utilities/GlobalUtil.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using SynthesisAPI.Utilities;

internal static class GlobalUtil {

Expand All @@ -14,4 +16,16 @@ public static V TryGetDefault<K, V>(this Dictionary<K, V> dict, K key, V defa) {
return success ? res : defa;
}

public static Task<U> CastTask<T, U>(this Task<T> task, int timeout = -1) where U : class
=> Task<U>.Factory.StartNew(() => {
if (timeout < 0) {
while (!task.IsCompleted) {
task.Wait();
}
} else {
task.Wait(timeout);
}
return task.Result as U;
});

}
31 changes: 31 additions & 0 deletions api/Api/Utilities/NetworkTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Threading.Tasks;

#nullable enable

namespace SynthesisAPI.Utilities {
public class NetworkTask<T> {
private Task<T> _task;
private AtomicReadOnly<NetworkTaskStatus> _status;
public NetworkTaskStatus Status => _status.Value;

public bool IsCompleted => _task.IsCompleted;

public NetworkTask(Task<T> task, AtomicReadOnly<NetworkTaskStatus> status) {
_task = task;
_status = status;
}

public static NetworkTask<T> FromResult(T val, string? msg = null)
=> new NetworkTask<T>(Task.FromResult(val), new AtomicReadOnly<NetworkTaskStatus>(
new NetworkTaskStatus { Progress = 1f, Message = msg ?? string.Empty }));
}

public struct NetworkTaskStatus {
public float Progress;
public string Message;

public override string ToString() {
return $"[{System.Math.Round(Progress * 100f, 1)}%] {Message}";
}
}
}
17 changes: 17 additions & 0 deletions api/Api/Utilities/Ref.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace SynthesisAPI.Utilities {
public class Ref<T> where T : struct {
private T _value;
public T Value {
get => _value;
set => _value = value;
}

public Ref(T val) {
_value = val;
}

public static Ref<T> Default => new Ref<T>(default);

public static implicit operator Ref<T>(T val) => new Ref<T>(val);
}
}
23 changes: 23 additions & 0 deletions api/Api/Utilities/StringValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace SynthesisAPI.Utilities {
/// <summary>
/// Turns a String reference type into a immutable String value type. Created for use in SynthesisAPI.Utlities.Atomic
/// </summary>
public struct StringValue {
private string _str;

public int Length => _str.Length;

public char this[int i] => _str[i];

public StringValue(string str) {
_str = str.Substring(0);
}

public static implicit operator string(StringValue s) => s._str.Substring(0);
public static implicit operator StringValue(string s) => new StringValue(s);

public override string ToString() => this;
public override int GetHashCode() => _str.GetHashCode();
public override bool Equals(object obj) => _str.Equals(obj);
}
}
Loading
Loading