Skip to content

Commit

Permalink
Merge pull request #237 from UltraStar-Deluxe/anst/improvements
Browse files Browse the repository at this point in the history
Anst/improvements
  • Loading branch information
basisbit authored Apr 24, 2021
2 parents 2edb0dd + f4d693d commit 27df76a
Show file tree
Hide file tree
Showing 32 changed files with 257 additions and 213 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[Ll]ibrary/
[Uu]serSettings/
[Ll]ogs/
[Tt]emp/
[Oo]bj/
Expand Down
12 changes: 8 additions & 4 deletions UltraStar Play/Assets/Common/Network/ConnectedClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class ConnectedClientHandler : IDisposable

public IPEndPoint ClientIpEndPoint { get; private set; }
public string ClientName { get; private set; }
public string ClientId { get; private set; }
public TcpListener MicTcpListener { get; private set; }
public string ClientId => ServerSideConnectRequestManager.GetClientId(ClientIpEndPoint);
public int SampleRateHz => micSampleBuffer.Length;

private bool isDisposed;
Expand All @@ -31,15 +31,19 @@ public class ConnectedClientHandler : IDisposable
private readonly Thread receiveDataThread;
private readonly Thread clientStillAliveCheckThread;

public ConnectedClientHandler(ServerSideConnectRequestManager serverSideConnectRequestManager, IPEndPoint clientIpEndPoint, string clientName, int microphoneSampleRate)
public ConnectedClientHandler(ServerSideConnectRequestManager serverSideConnectRequestManager, IPEndPoint clientIpEndPoint, string clientName, string clientId, int microphoneSampleRate)
{
this.serverSideConnectRequestManager = serverSideConnectRequestManager;
ClientIpEndPoint = clientIpEndPoint;
ClientName = clientName;
ClientId = clientId;
if (ClientId.IsNullOrEmpty())
{
throw new ArgumentException("Attempt to create ConnectedClientHandler without ClientId");
}
if (microphoneSampleRate <= 0)
{
Debug.LogWarning("Attempt to create ConnectedClientHandler without microphoneSampleRate");
return;
throw new ArgumentException("Attempt to create ConnectedClientHandler without microphoneSampleRate");
}

micSampleBuffer = new float[microphoneSampleRate];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
{
public int ProtocolVersion { get; set; }
public string ClientName { get; set; }
public string ClientId { get; set; }
public int MicrophoneSampleRate { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
public class ConnectResponseDto : JsonSerializable
{
public string ClientName { get; set; }
public string ClientId { get; set; }
public int MicrophonePort { get; set; }
public string ErrorMessage { get; set; }
public int HttpServerPort { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static ServerSideConnectRequestManager Instance
/**
* This version number must to be increased when introducing breaking changes.
*/
public const int ProtocolVersion = 1;
public const int ProtocolVersion = 2;

private UdpClient serverUdpClient;
private const int ConnectPortOnServer = 34567;
Expand Down Expand Up @@ -155,7 +155,11 @@ private void HandleClientMessage(IPEndPoint clientIpEndPoint, string message)
}
if (connectRequestDto.ClientName.IsNullOrEmpty())
{
throw new ConnectRequestException("Malformed ConnectRequest: missing clientName.");
throw new ConnectRequestException("Malformed ConnectRequest: missing ClientName.");
}
if (connectRequestDto.ClientId.IsNullOrEmpty())
{
throw new ConnectRequestException("Malformed ConnectRequest: missing ClientId.");
}

if (connectRequestDto.MicrophoneSampleRate > 0)
Expand All @@ -182,19 +186,21 @@ private void HandleClientMessageWithNoMicrophone(IPEndPoint clientIpEndPoint, Co
ConnectResponseDto connectResponseDto = new ConnectResponseDto
{
ClientName = connectRequestDto.ClientName,
ClientId = connectRequestDto.ClientId,
HttpServerPort = httpServer.port,
};
serverUdpClient.Send(connectResponseDto.ToJson(), clientIpEndPoint);
}

private void HandleClientMessageWithMicrophone(IPEndPoint clientIpEndPoint, ConnectRequestDto connectRequestDto)
{
ConnectedClientHandler newConnectedClientHandler = RegisterClient(clientIpEndPoint, connectRequestDto.ClientName, connectRequestDto.MicrophoneSampleRate);
ConnectedClientHandler newConnectedClientHandler = RegisterClient(clientIpEndPoint, connectRequestDto.ClientName, connectRequestDto.ClientId, connectRequestDto.MicrophoneSampleRate);
clientConnectedEventQueue.Enqueue(new ClientConnectionEvent(newConnectedClientHandler, true));

ConnectResponseDto connectResponseDto = new ConnectResponseDto
{
ClientName = connectRequestDto.ClientName,
ClientId = connectRequestDto.ClientId,
HttpServerPort = httpServer.port,
MicrophonePort = newConnectedClientHandler.MicTcpListener.GetPort(),
};
Expand Down Expand Up @@ -235,16 +241,17 @@ public void RemoveConnectedClientHandler(ConnectedClientHandler connectedClientH
private ConnectedClientHandler RegisterClient(
IPEndPoint clientIpEndPoint,
string clientName,
string clientId,
int microphoneSampleRate)
{
// Dispose any currently registered client with the same IP-Address.
if (idToConnectedClientMap.TryGetValue(GetClientId(clientIpEndPoint), out ConnectedClientHandler existingConnectedClientHandler))
if (idToConnectedClientMap.TryGetValue(clientId, out ConnectedClientHandler existingConnectedClientHandler))
{
existingConnectedClientHandler.Dispose();
}

ConnectedClientHandler connectedClientHandler = new ConnectedClientHandler(this, clientIpEndPoint, clientName, microphoneSampleRate);
idToConnectedClientMap[GetClientId(clientIpEndPoint)] = connectedClientHandler;
ConnectedClientHandler connectedClientHandler = new ConnectedClientHandler(this, clientIpEndPoint, clientName, clientId, microphoneSampleRate);
idToConnectedClientMap[clientId] = connectedClientHandler;

Debug.Log("New number of connected clients: " + idToConnectedClientMap.Count);

Expand All @@ -260,9 +267,4 @@ public static bool TryGetConnectedClientHandler(string clientIpEndPointId, out C
{
return idToConnectedClientMap.TryGetValue(clientIpEndPointId, out connectedClientHandler);
}

public static string GetClientId(IPEndPoint clientIpEndPoint)
{
return clientIpEndPoint.Address.ToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.NetworkInformation;
using SimpleHttpServerForUnity;

public class UltraStarPlayHttpServer : HttpServer
Expand All @@ -19,7 +20,7 @@ protected override void Awake()
return;
}

host = IpAddressUtils.GetIpAddress(AddressFamily.IPv4);
host = IpAddressUtils.GetIpAddress(AddressFamily.IPv4, NetworkInterfaceType.Wireless80211);
NoEndpointFoundCallback = SendNoEndpointFound;
StartHttpListener();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ private RectTransform RectTransform

protected abstract void FillContextMenu(ContextMenu contextMenu);

private readonly List<IDisposable> disposables = new List<IDisposable>();

public bool IsDrag { get; private set; }
private Vector2 dragStartPosition;

protected void Start()
{
disposables.Add(InputManager.GetInputAction(R.InputActions.usplay_openContextMenu).PerformedAsObservable()
.Subscribe(CheckOpenContextMenuFromInputAction));
InputManager.GetInputAction(R.InputActions.usplay_openContextMenu).PerformedAsObservable()
.Subscribe(CheckOpenContextMenuFromInputAction)
.AddTo(gameObject);
}

protected virtual void CheckOpenContextMenuFromInputAction(InputAction.CallbackContext context)
Expand Down Expand Up @@ -106,11 +105,6 @@ private ContextMenu GetContextMenuPrefab()
return UiManager.Instance.contextMenuPrefab;
}

private void OnDestroy()
{
disposables.ForEach(it => it.Dispose());
}

public void OnBeginDrag(PointerEventData eventData)
{
dragStartPosition = eventData.position;
Expand Down
9 changes: 3 additions & 6 deletions UltraStar Play/Assets/Common/UI/ContextMenu/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ static void Init()
public ContextMenuItem contextMenuItemPrefab;
public ContextMenuSeparator contextMenuSeparatorPrefab;

private readonly List<IDisposable> disposables = new List<IDisposable>();

private bool wasNoButtonOrTouchPressed;

public static List<ContextMenu> OpenContextMenus { get; private set; } = new List<ContextMenu>();
Expand Down Expand Up @@ -48,7 +46,7 @@ protected override void Awake()
transform.DestroyAllDirectChildren();

// Close with next click or tap
disposables.Add(InputManager.GetInputAction(R.InputActions.usplay_closeContextMenu).PerformedAsObservable()
InputManager.GetInputAction(R.InputActions.usplay_closeContextMenu).PerformedAsObservable()
.Subscribe(context =>
{
// Only close when the mouse / touchscreen has been fully released in the mean time.
Expand All @@ -66,7 +64,8 @@ protected override void Awake()
}
CloseContextMenu();
}));
})
.AddTo(gameObject);
}

private void Start()
Expand Down Expand Up @@ -121,8 +120,6 @@ public void CloseContextMenu()

private void OnDestroy()
{
disposables.ForEach(it => it.Dispose());

// Remove this ContextMenu from the list of opened ContextMenus only after all Input has been released
// to avoid triggering additional actions (e.g. onClick of button).
if (CoroutineManager.Instance != null)
Expand Down
8 changes: 3 additions & 5 deletions UltraStar Play/Assets/Common/UI/Dialogs/QuestionDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public class QuestionDialog : Dialog
public bool noOnEscape = true;
public bool focusYesOnStart = true;

private readonly List<IDisposable> disposables = new List<IDisposable>();

private void Start()
{
yesButton.OnClickAsObservable().Subscribe(_ =>
Expand All @@ -38,14 +36,14 @@ private void Start()

if (noOnEscape)
{
disposables.Add(InputManager.GetInputAction(R.InputActions.usplay_back).PerformedAsObservable(5)
.Subscribe(OnBack));
InputManager.GetInputAction(R.InputActions.usplay_back).PerformedAsObservable(5)
.Subscribe(OnBack)
.AddTo(gameObject);
}
}

public void Close()
{
disposables.ForEach(it => it.Dispose());
Destroy(gameObject);
}

Expand Down
8 changes: 3 additions & 5 deletions UltraStar Play/Assets/Common/UI/Dialogs/WarningDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public class WarningDialog : Dialog

public bool focusOkButtonOnStart = true;

private readonly List<IDisposable> disposables = new List<IDisposable>();

void Start()
{
okButton.OnClickAsObservable().Subscribe(_ => Close());
Expand All @@ -20,13 +18,13 @@ void Start()
okButton.Select();
}

disposables.Add(InputManager.GetInputAction(R.InputActions.usplay_back).PerformedAsObservable(6)
.Subscribe(OnBack));
InputManager.GetInputAction(R.InputActions.usplay_back).PerformedAsObservable(6)
.Subscribe(OnBack)
.AddTo(gameObject);
}

public void Close()
{
disposables.ForEach(it => it.Dispose());
Destroy(gameObject);
}

Expand Down
12 changes: 3 additions & 9 deletions UltraStar Play/Assets/Common/UI/Drag/AbstractDragHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ abstract public class AbstractDragHandler<EVENT> : MonoBehaviour, INeedInjection

public RectTransform targetRectTransform;

private readonly List<IDisposable> disposables = new List<IDisposable>();

protected virtual void Start()
{
disposables.Add(InputManager.GetInputAction(R.InputActions.usplay_back).PerformedAsObservable(10)
InputManager.GetInputAction(R.InputActions.usplay_back).PerformedAsObservable(10)
.Where(_ => IsDragging)
.Subscribe(_ =>
{
CancelDrag();
// Cancel other callbacks. To do so, this subscription has a higher priority.
InputManager.GetInputAction(R.InputActions.usplay_back).CancelNotifyForThisFrame();
}));
})
.AddTo(gameObject);
}

public void AddListener(IDragListener<EVENT> listener)
Expand Down Expand Up @@ -121,11 +120,6 @@ private void NotifyListeners(Action<IDragListener<EVENT>> action, bool includeCa
}
}

private void OnDestroy()
{
disposables.ForEach(it => it.Dispose());
}

abstract protected EVENT CreateDragEventStart(PointerEventData eventData);
abstract protected EVENT CreateDragEvent(PointerEventData eventData, EVENT dragStartEvent);

Expand Down
15 changes: 14 additions & 1 deletion UltraStar Play/Assets/Editor/BuildInfoGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;

// Fills a file with build information before Unity performs the actual build.
public class BuildInfoGenerator : IPreprocessBuildWithReport
{
public static readonly string versionPropertyName = "release";
public static readonly string timeStampPropertyName = "build_timestamp";
public static readonly string commitShortHashPropertyName = "commit_hash";
public static readonly string versionFile = "Assets/VERSION.txt";

public int callbackOrder
Expand All @@ -28,14 +29,26 @@ public void OnPreprocessBuild(BuildReport report)
private void UpdateVersionFile()
{
string timeStamp = DateTime.Now.ToString("yyMMddHHmm", CultureInfo.InvariantCulture);
string commitShortHash = GitUtils.GetCurrentCommitShortHash();

string[] versionFileLines = File.ReadAllLines(versionFile);
for (int i = 0; i < versionFileLines.Length; i++)
{
string line = versionFileLines[i];
if (line.StartsWith(versionPropertyName, true, CultureInfo.InvariantCulture))
{
versionFileLines[i] = $"{versionPropertyName} = {PlayerSettings.bundleVersion}";
}

if (line.StartsWith(timeStampPropertyName, true, CultureInfo.InvariantCulture))
{
versionFileLines[i] = $"{timeStampPropertyName} = {timeStamp}";
}

if (line.StartsWith(commitShortHashPropertyName, true, CultureInfo.InvariantCulture))
{
versionFileLines[i] = $"{commitShortHashPropertyName} = {commitShortHash}";
}
}
File.WriteAllLines(versionFile, versionFileLines);
// Unity needs a hint that this asset has changed.
Expand Down
Loading

0 comments on commit 27df76a

Please sign in to comment.