Skip to content

Commit

Permalink
Add service panel and OBS drag and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
Marakusa committed Aug 26, 2023
1 parent 1d3e493 commit 70b7e8d
Show file tree
Hide file tree
Showing 21 changed files with 702 additions and 379 deletions.
34 changes: 22 additions & 12 deletions CutOverlay/CutOverlay/App/OAuthOverlayApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public abstract class OAuthOverlayApp : OverlayApp

private static readonly Random Random = new();

private string? _clientId = "";
private string? _clientSecret = "";
protected string? ClientId = "";
protected string? ClientSecret = "";
private string _stateToken = "";
private protected string? AccessToken;

Expand All @@ -37,8 +37,10 @@ private string RegenerateStateToken()

private protected void SetupOAuth(string? clientId, string? clientSecret)
{
_clientId = clientId;
_clientSecret = clientSecret;
Status = ServiceStatusType.Starting;

ClientId = clientId;
ClientSecret = clientSecret;

AuthorizationTimer?.Stop();
AuthorizationTimer = new Timer { Interval = 120000 };
Expand All @@ -54,7 +56,7 @@ private protected void SetupOAuth(string? clientId, string? clientSecret)
"{0}?response_type={1}&client_id={2}&redirect_uri={3}&scope={4}&state={5}",
AuthorizationAddress,
ResponseType,
_clientId,
ClientId,
CallbackAddress,
Scopes,
state
Expand All @@ -80,8 +82,8 @@ private async Task UpdateAuthorizationAsync()
{
Content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string?>("client_id", _clientId),
new KeyValuePair<string, string?>("client_secret", _clientSecret),
new KeyValuePair<string, string?>("client_id", ClientId),
new KeyValuePair<string, string?>("client_secret", ClientSecret),
new KeyValuePair<string, string?>("grant_type", "authorization_code"),
new KeyValuePair<string, string?>("code", AccessToken),
new KeyValuePair<string, string?>("state", state),
Expand All @@ -92,16 +94,16 @@ private async Task UpdateAuthorizationAsync()
request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
Encoding.UTF8.GetBytes(
$"{_clientId}:{_clientSecret}")));
$"{ClientId}:{ClientSecret}")));
}
else
{
request = new HttpRequestMessage(HttpMethod.Post, AuthApiUri)
{
Content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string?>("client_id", _clientId),
new KeyValuePair<string, string?>("client_secret", _clientSecret),
new KeyValuePair<string, string?>("client_id", ClientId),
new KeyValuePair<string, string?>("client_secret", ClientSecret),
new KeyValuePair<string, string?>("grant_type", "refresh_token"),
new KeyValuePair<string, string?>("state", state),
new KeyValuePair<string, string?>("refresh_token", RefreshToken)
Expand All @@ -111,7 +113,7 @@ private async Task UpdateAuthorizationAsync()
request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
Encoding.UTF8.GetBytes(
$"{_clientId}:{_clientSecret}")));
$"{ClientId}:{ClientSecret}")));
}

HttpResponseMessage response = await HttpClient!.SendAsync(request);
Expand All @@ -120,28 +122,36 @@ private async Task UpdateAuthorizationAsync()
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"ERROR: {content}");
Status = ServiceStatusType.Error;
return;
}

SpotifyAuthenticationModel? authentication =
JsonConvert.DeserializeObject<SpotifyAuthenticationModel>(content);
AccessToken = authentication?.AccessToken;
if (!string.IsNullOrEmpty(authentication?.RefreshToken))
RefreshToken = authentication?.RefreshToken;
RefreshToken = authentication.RefreshToken;
Status = ServiceStatusType.Running;
}
catch (Exception ex)
{
Console.WriteLine($"ERROR: {ex}");
Status = ServiceStatusType.Error;
}
}

public void AuthCallback(string code, string state)
{
if (string.IsNullOrEmpty(state) || !state.Equals(state, StringComparison.InvariantCulture))
{
Status = ServiceStatusType.Error;
throw new Exception("Failed to verify the request");
}

AccessToken = code;

Status = ServiceStatusType.Running;

_ = UpdateAuthorizationAsync();
}

Expand Down
8 changes: 8 additions & 0 deletions CutOverlay/CutOverlay/App/OverlayApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
public abstract class OverlayApp : IDisposable
{
private protected HttpClient? HttpClient = null;
private protected ServiceStatusType Status = ServiceStatusType.Stopped;

public void Dispose()
{
Unload();
}

public abstract void Unload();

public abstract ServiceStatusType GetStatus();
}

public enum ServiceStatusType
{
Running, Starting, Stopping, Stopped, Error
}
30 changes: 2 additions & 28 deletions CutOverlay/CutOverlay/Controllers/ConfigurationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ConfigurationController : ControllerBase
private readonly Pulsoid _pulsoid;
private readonly Spotify _spotify;
private readonly Twitch _twitch;

public ConfigurationController(IConfiguration configuration, Spotify spotify, Twitch twitch, Pulsoid pulsoid)
{
_spotify = spotify;
Expand Down Expand Up @@ -83,36 +83,10 @@ public async Task<IActionResult> SaveConfig([FromBody] Dictionary<string, string
{
EncryptAndSaveConfig(config);
DecryptAndReadConfig();

await _spotify.RefreshConfigurationsAsync();
await _twitch.RefreshConfigurationsAsync();
await _pulsoid.RefreshConfigurationsAsync();
/*if (spotifySettingsRefresh)
{
Spotify.Instance?.Unload();
Spotify.Instance?.Dispose();
Spotify.Instance = null;
_ = new Spotify();
await Spotify.Instance?.Start(Configurations)!;
}
if (twitchSettingsRefresh)
{
Twitch.Instance?.Unload();
Twitch.Instance?.Dispose();
Twitch.Instance = null;
_ = new Twitch();
await Twitch.Instance?.Start(Configurations)!;
}
if (pulsoidSettingsRefresh)
{
Pulsoid.Instance?.Unload();
Pulsoid.Instance?.Dispose();
Pulsoid.Instance = null;
_ = new Pulsoid();
await Pulsoid.Instance?.Start(Configurations)!;
}*/

return Ok();
}
Expand Down
2 changes: 1 addition & 1 deletion CutOverlay/CutOverlay/Controllers/SpotifyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public IActionResult SpotifyCallback([FromQuery(Name = "code")] string accessTok
return new ContentResult
{
Content =
"<div>Authorization successful! This tab can now be closed.</div><script>setTimeout(() => {close();},3000);</script>",
"<div>Authorization successful! This tab can now be closed.</div><script>setTimeout(() => {close();},100);</script>",
ContentType = "text/html",
StatusCode = 200
};
Expand Down
156 changes: 153 additions & 3 deletions CutOverlay/CutOverlay/Controllers/StatusController.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
using CutOverlay.Services;
using Microsoft.AspNetCore.Mvc;
using CutOverlay.App;
using CutOverlay.Models;
using Newtonsoft.Json;

namespace CutOverlay.Controllers;

[Route("status")]
[ApiController]
public class StatusController : ControllerBase
{
public StatusController(Spotify spotify, BeatSaberPlus beatSaberPlus)
private readonly Spotify _spotify;
private readonly BeatSaberPlus _beatSaberPlus;
private readonly Twitch _twitch;
private readonly Pulsoid _pulsoid;

public StatusController(Spotify spotify, BeatSaberPlus beatSaberPlus, Twitch twitch, Pulsoid pulsoid)
{
_ = spotify;
_ = beatSaberPlus;
_spotify = spotify;
_beatSaberPlus = beatSaberPlus;
_twitch = twitch;
_pulsoid = pulsoid;
}

[HttpGet("get")]
Expand Down Expand Up @@ -40,4 +50,144 @@ public IActionResult Image()
return BadRequest(ex.Message);
}
}

[HttpGet("service/twitch")]
public IActionResult ServiceTwitch()
{
try
{
ServiceStatusType status = _twitch.GetStatus();
ServiceStatusModel statusModel = new()
{
Color = status switch
{
ServiceStatusType.Running => "greenyellow",
ServiceStatusType.Stopped => "grey",
ServiceStatusType.Stopping => "coral",
ServiceStatusType.Starting => "darkorange",
ServiceStatusType.Error => "crimson",
_ => throw new ArgumentOutOfRangeException()
},
Status = status switch
{
ServiceStatusType.Running => "Running",
ServiceStatusType.Stopped => "Stopped",
ServiceStatusType.Stopping => "Stopping",
ServiceStatusType.Starting => "Starting",
ServiceStatusType.Error => "Error",
_ => throw new ArgumentOutOfRangeException()
}
};
return Ok(JsonConvert.SerializeObject(statusModel));
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}

[HttpGet("service/spotify")]
public IActionResult ServiceSpotify()
{
try
{
ServiceStatusType status = _spotify.GetStatus();
ServiceStatusModel statusModel = new()
{
Color = status switch
{
ServiceStatusType.Running => "greenyellow",
ServiceStatusType.Stopped => "grey",
ServiceStatusType.Stopping => "coral",
ServiceStatusType.Starting => "darkorange",
ServiceStatusType.Error => "crimson",
_ => throw new ArgumentOutOfRangeException()
},
Status = status switch
{
ServiceStatusType.Running => "Running",
ServiceStatusType.Stopped => "Stopped",
ServiceStatusType.Stopping => "Stopping",
ServiceStatusType.Starting => "Starting",
ServiceStatusType.Error => "Error",
_ => throw new ArgumentOutOfRangeException()
}
};
return Ok(JsonConvert.SerializeObject(statusModel));
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}

[HttpGet("service/bsp")]
public IActionResult ServiceBsp()
{
try
{
ServiceStatusType status = _beatSaberPlus.GetStatus();
ServiceStatusModel statusModel = new()
{
Color = status switch
{
ServiceStatusType.Running => "greenyellow",
ServiceStatusType.Stopped => "grey",
ServiceStatusType.Stopping => "coral",
ServiceStatusType.Starting => "darkorange",
ServiceStatusType.Error => "crimson",
_ => throw new ArgumentOutOfRangeException()
},
Status = status switch
{
ServiceStatusType.Running => "Running",
ServiceStatusType.Stopped => "Stopped",
ServiceStatusType.Stopping => "Stopping",
ServiceStatusType.Starting => "Starting",
ServiceStatusType.Error => "Error",
_ => throw new ArgumentOutOfRangeException()
}
};
return Ok(JsonConvert.SerializeObject(statusModel));
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}

[HttpGet("service/pulsoid")]
public IActionResult ServicePulsoid()
{
try
{
ServiceStatusType status = _pulsoid.GetStatus();
ServiceStatusModel statusModel = new()
{
Color = status switch
{
ServiceStatusType.Running => "greenyellow",
ServiceStatusType.Stopped => "grey",
ServiceStatusType.Stopping => "coral",
ServiceStatusType.Starting => "darkorange",
ServiceStatusType.Error => "crimson",
_ => throw new ArgumentOutOfRangeException()
},
Status = status switch
{
ServiceStatusType.Running => "Running",
ServiceStatusType.Stopped => "Stopped",
ServiceStatusType.Stopping => "Stopping",
ServiceStatusType.Starting => "Starting",
ServiceStatusType.Error => "Error",
_ => throw new ArgumentOutOfRangeException()
}
};
return Ok(JsonConvert.SerializeObject(statusModel));
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}
19 changes: 2 additions & 17 deletions CutOverlay/CutOverlay/Controllers/TwitchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,7 @@ public IActionResult ApiConnectionCallback()
return BadRequest(ex.Message);
}
}

[HttpGet("chat/debug")]
public IActionResult ChatDebugCallback()
{
try
{
// TODO: Debug
//_twitch.DebugMessages();
return Ok();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}


[HttpGet("callback")]
public IActionResult TwitchCallback()
{
Expand All @@ -68,7 +53,7 @@ public async Task<IActionResult> TwitchCallbackQuery([FromQuery(Name = "access_t
return new ContentResult
{
Content =
"<div>Authorization successful! This tab can now be closed.</div><script>setTimeout(() => {close();},3000);</script>",
"<div>Authorization successful! This tab can now be closed.</div><script>setTimeout(() => {close();},100);</script>",
ContentType = "text/html",
StatusCode = 200
};
Expand Down
Loading

0 comments on commit 70b7e8d

Please sign in to comment.