This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring: dedicated SongListRequestor with SongListEvent
- Loading branch information
1 parent
a29667d
commit 7d864a4
Showing
11 changed files
with
241 additions
and
70 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
UltraStar Play Companion/Assets/Common/Network/AbstractHttpRequestor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Net; | ||
using UniInject; | ||
using UniRx; | ||
using UnityEngine; | ||
|
||
// Disable warning about fields that are never assigned, their values are injected. | ||
#pragma warning disable CS0649 | ||
|
||
public abstract class AbstractHttpRequestor : MonoBehaviour, INeedInjection | ||
{ | ||
protected IPEndPoint serverIPEndPoint; | ||
protected int httpServerPort; | ||
|
||
[Inject] | ||
protected ClientSideConnectRequestManager clientSideConnectRequestManager; | ||
|
||
protected void Start() | ||
{ | ||
clientSideConnectRequestManager.ConnectEventStream | ||
.Where(connectEvent => connectEvent.IsSuccess) | ||
.Subscribe(connectEvent => | ||
{ | ||
serverIPEndPoint = connectEvent.ServerIpEndPoint; | ||
httpServerPort = connectEvent.HttpServerPort; | ||
}); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
UltraStar Play Companion/Assets/Common/Network/AbstractHttpRequestor.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
UltraStar Play Companion/Assets/Common/Network/SongListEvent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class SongListEvent | ||
{ | ||
public string ErrorMessage { get; set; } | ||
public LoadedSongsDto LoadedSongsDto { get; set; } | ||
} |
3 changes: 3 additions & 0 deletions
3
UltraStar Play Companion/Assets/Common/Network/SongListEvent.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
UltraStar Play Companion/Assets/Common/Network/SongListRequestor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UniInject; | ||
using UnityEngine; | ||
using UniRx; | ||
|
||
// Disable warning about fields that are never assigned, their values are injected. | ||
#pragma warning disable CS0649 | ||
|
||
public class SongListRequestor : AbstractHttpRequestor | ||
{ | ||
private Subject<SongListEvent> songListEventStream = new Subject<SongListEvent>(); | ||
public IObservable<SongListEvent> SongListEventStream => songListEventStream; | ||
|
||
public bool SuccessfullyLoadedAllSongs { get; private set; } | ||
|
||
public LoadedSongsDto LoadedSongsDto { get; private set; } | ||
|
||
public void RequestSongList() | ||
{ | ||
if (serverIPEndPoint == null | ||
|| httpServerPort == 0) | ||
{ | ||
FireErrorMessageEvent("Can not get song list. Not yet connected to main game."); | ||
return; | ||
} | ||
|
||
string uri = $"http://{serverIPEndPoint.Address}:{httpServerPort}/api/rest/songs"; | ||
Debug.Log("GET song list from URI: " + uri); | ||
|
||
StartCoroutine(WebRequestUtils.LoadTextFromUriCoroutine(uri, | ||
HandleSongListResponse, | ||
_ => FireErrorMessageEvent("Ups, something went wrong getting the song list."))); | ||
} | ||
|
||
private void HandleSongListResponse(string downloadHandlerText) | ||
{ | ||
try | ||
{ | ||
LoadedSongsDto = JsonConverter.FromJson<LoadedSongsDto>(downloadHandlerText); | ||
if (!LoadedSongsDto.IsSongScanFinished | ||
&& LoadedSongsDto.SongCount == 0) | ||
{ | ||
SuccessfullyLoadedAllSongs = false; | ||
FireErrorMessageEvent("No songs loaded yet."); | ||
return; | ||
} | ||
|
||
if (LoadedSongsDto.IsSongScanFinished) | ||
{ | ||
SuccessfullyLoadedAllSongs = true; | ||
} | ||
|
||
songListEventStream.OnNext(new SongListEvent | ||
{ | ||
LoadedSongsDto = LoadedSongsDto, | ||
}); | ||
} | ||
catch (Exception e) | ||
{ | ||
Debug.LogException(e); | ||
SuccessfullyLoadedAllSongs = false; | ||
FireErrorMessageEvent("Ups, something went wrong reading the song list response."); | ||
} | ||
} | ||
|
||
private void FireErrorMessageEvent(string errorMessage) | ||
{ | ||
songListEventStream.OnNext(new SongListEvent | ||
{ | ||
ErrorMessage = errorMessage, | ||
}); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
UltraStar Play Companion/Assets/Common/Network/SongListRequestor.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
UltraStar Play Companion/Assets/Common/Util/WebRequestUtils.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections; | ||
using UnityEngine; | ||
using UnityEngine.Networking; | ||
|
||
public static class WebRequestUtils | ||
{ | ||
public static IEnumerator LoadTextFromUriCoroutine(string uri, Action<string> onSuccess, Action<UnityWebRequest> onFailure = null) | ||
{ | ||
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri)) | ||
{ | ||
webRequest.SendWebRequest(); | ||
|
||
while (!webRequest.isDone) | ||
{ | ||
yield return null; | ||
} | ||
|
||
if (webRequest.isNetworkError || webRequest.isHttpError) | ||
{ | ||
Debug.LogError("Error loading text from: " + uri); | ||
Debug.LogError(webRequest.error); | ||
if (onFailure != null) | ||
{ | ||
onFailure(webRequest); | ||
} | ||
yield break; | ||
} | ||
|
||
onSuccess(webRequest.downloadHandler.text); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
UltraStar Play Companion/Assets/Common/Util/WebRequestUtils.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.