Skip to content

Commit

Permalink
OAuth Flow Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Neonalig committed May 16, 2020
1 parent f78ec05 commit c17f846
Show file tree
Hide file tree
Showing 23 changed files with 3,556 additions and 112 deletions.
Binary file added AltIcon.ico
Binary file not shown.
211 changes: 211 additions & 0 deletions AltIcon.pdn

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@
<setting name="OsuLazerInstallationPath" serializeAs="String">
<value />
</setting>
<setting name="GitHubUsername" serializeAs="String">
<value />
</setting>
<setting name="GitHubPassword" serializeAs="String">
<value />
</setting>
<setting name="LatestToken" serializeAs="String">
<value />
</setting>
<setting name="LatestTokenCreationTime" serializeAs="String">
<value />
</setting>
</OsuModeManager.Properties.Settings>
</userSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="x86"/>
</assemblyBinding>
</runtime>
</configuration>
Binary file added CustomIcon.ico
Binary file not shown.
2,467 changes: 2,467 additions & 0 deletions CustomIcon.pdn

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Diagnostics;
using System.Linq;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using Ookii.Dialogs.Wpf;

namespace OsuModeManager {
Expand Down Expand Up @@ -104,7 +106,43 @@ public static DirectoryInfo GetDirectory(Environment.SpecialFolder StartFolder =
return null;
}

public static FileInfo GetFile(string Title = "Pick a file", string Filter = "Any File (*.*)|*.*", DirectoryInfo InitialDirectory = null, FileInfo StartFile = null) {
VistaOpenFileDialog FileBrowser = new VistaOpenFileDialog {
ReadOnlyChecked = true,
ShowReadOnly = true,
CheckFileExists = true,
CheckPathExists = true,
Filter = Filter,
FilterIndex = 0,
InitialDirectory = InitialDirectory?.FullName ?? Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
RestoreDirectory = true,
Title = Title
};

if (StartFile != null) {
FileBrowser.FileName = StartFile.FullName;
}

if (FileBrowser.ShowDialog() == true && TryGetFile(FileBrowser.FileName, out FileInfo Result)) {
return Result;
}

return null;
}

public static int Clamp(this int Value, int Min = int.MinValue, int Max = int.MaxValue) => Value < Min ? Min : Value > Max ? Max : Value;

public static Task WaitForExitAsync(this Process Process,
CancellationToken CancellationToken = default) {
TaskCompletionSource<object> Tcs = new TaskCompletionSource<object>();
Process.EnableRaisingEvents = true;
Process.Exited += (Sender, Args) => Tcs.TrySetResult(null);
if (CancellationToken != default) {
CancellationToken.Register(Tcs.SetCanceled);
}

return Tcs.Task;
}

}
}
57 changes: 33 additions & 24 deletions GithubGamemode.cs → Gamemode.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Octokit;

namespace OsuModeManager {
public struct GitHubGamemode : IEquatable<GitHubGamemode> {
public struct Gamemode : IEquatable<Gamemode> {

public string GitHubUser;
public string GitHubRepo;
public string TagVersion;
public string GitHubTagVersion;
public string RulesetFilename;

public static GitHubGamemode CreateInstance(string GitHubUser = @"Altenhh", string GitHubRepo = "tau", string TagVersion = null, string RulesetFilename = "osu.Game.Rulesets.Tau.dll") => new GitHubGamemode(GitHubUser, GitHubRepo, TagVersion, RulesetFilename);
public GitHubGamemode(string GitHubUser = @"Altenhh", string GitHubRepo = "tau", string TagVersion = null, string RulesetFilename = "osu.Game.Rulesets.Tau.dll") {
public bool? UpdateRequired;

public System.Windows.Visibility DisplayUpdate => UpdateRequired == true ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
public System.Windows.Visibility DisplayUpToDate => UpdateRequired == false ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
public string DisplayName => ToString();

public static Gamemode CreateInstance(string GitHubUser = @"Altenhh", string GitHubRepo = "tau", string TagVersion = null, string RulesetFilename = null, bool? UpdateRequired = null) => new Gamemode(GitHubUser, GitHubRepo, TagVersion, RulesetFilename, UpdateRequired);

public Gamemode(string GitHubUser = @"Altenhh", string GitHubRepo = "tau", string TagVersion = null, string RulesetFilename = "osu.Game.Rulesets.Tau.dll", bool? UpdateRequired = null) {
this.GitHubUser = GitHubUser;
this.GitHubRepo = GitHubRepo;
this.TagVersion = TagVersion;
this.GitHubTagVersion = TagVersion;
this.RulesetFilename = RulesetFilename;
this.UpdateRequired = UpdateRequired;
}

#region GitHub
public async Task<(bool, Release)> TryGetLatestReleaseAsync() {
IEnumerable<Release> Releases = await MainWindow.GetRepositoryReleases(GitHubUser, GitHubRepo);
Releases = Releases.Reverse();
IEnumerable<Release> Releases = await MainWindow.Client.Repository.Release.GetAll(GitHubUser, GitHubRepo);
//Releases = Releases.Reverse();
return Releases.TryGetFirst(out Release LatestRelease) ? (true, LatestRelease) : (false, null);
}

Expand All @@ -41,8 +48,7 @@ public static bool TryGetRulesetAsync(Release Release, out ReleaseAsset FoundAss
public async Task<(bool, Release)> TryGetCurrentReleaseAsync() {
// ReSharper disable once LoopCanBePartlyConvertedToQuery
foreach (Release Release in await MainWindow.Client.Repository.Release.GetAll(GitHubUser, GitHubRepo)) {
if (Release.TagName.Equals(TagVersion, StringComparison.InvariantCultureIgnoreCase)) {
Debug.WriteLine(Release.TagName + "==" + TagVersion);
if (Release.TagName.Equals(GitHubTagVersion, StringComparison.InvariantCultureIgnoreCase)) {
return (true, Release);
}
}
Expand All @@ -53,8 +59,11 @@ public static bool TryGetRulesetAsync(Release Release, out ReleaseAsset FoundAss
(bool LatestSucess, Release Latest) = await TryGetLatestReleaseAsync();
(bool CurrentSucess, Release Current) = await TryGetCurrentReleaseAsync();

Debug.WriteLine("[ID Check; Latest: " + Latest.Id + " | Current: " + Current.Id + "]");
return LatestSucess && CurrentSucess ? ((bool ?, Release))(!Latest.Id.Equals(Current.Id), Latest) : (null, Latest);
if (LatestSucess && CurrentSucess) {
//Debug.WriteLine("Current: " + Current.TagName + " | Latest: " + Latest.TagName + " |== " + Current.TagName.Equals(Latest.TagName));
UpdateRequired = !Latest.TagName.Equals(Current.TagName, StringComparison.InvariantCultureIgnoreCase);
}
return (UpdateRequired, Latest);
}

#endregion
Expand All @@ -63,9 +72,9 @@ public static bool TryGetRulesetAsync(Release Release, out ReleaseAsset FoundAss
public const char CharData = '';
public const char CharLine = '';

public string ExportGamemode() => $"{GitHubUser}{CharData}{GitHubRepo}{CharData}{TagVersion}{CharData}{RulesetFilename}";
public string ExportGamemode() => $"{GitHubUser}{CharData}{GitHubRepo}{CharData}{GitHubTagVersion}{CharData}{RulesetFilename}";

public static GitHubGamemode? ImportGamemode(string ImportSection) {
public static Gamemode? ImportGamemode(string ImportSection) {
string[] Sections = ImportSection?.Split(CharData);
if (Sections.TryGetAt(0, out string User) && Sections.TryGetAt(1, out string Repo) && Sections.TryGetAt(2, out string Tag) && Sections.TryGetAt(3, out string RulesetFile)) {
return CreateInstance(User, Repo, Tag, RulesetFile);
Expand All @@ -75,18 +84,18 @@ public static bool TryGetRulesetAsync(Release Release, out ReleaseAsset FoundAss
#endregion

#region File Importing/Exporting
public static string ExportGamemodes(GitHubGamemode[] Gamemodes) {
public static string ExportGamemodes(Gamemode[] Gamemodes) {
string Result = "";
for (int G = 0; G < Gamemodes.Length; G++) {
Result += (G > 0 ? CharLine.ToString() : "") + Gamemodes[G].ExportGamemode();
}
return Result;
}

public static IEnumerable<GitHubGamemode> ImportGamemodes(FileInfo GamemodeFile) {
public static IEnumerable<Gamemode> ImportGamemodes(FileInfo GamemodeFile) {
// ReSharper disable once LoopCanBeConvertedToQuery
foreach(string Section in File.ReadAllText(GamemodeFile.FullName).Split(CharLine)) {
GitHubGamemode? SectionResult = ImportGamemode(Section);
Gamemode? SectionResult = ImportGamemode(Section);
if (SectionResult.HasValue) {
yield return SectionResult.Value;
}
Expand All @@ -95,31 +104,31 @@ public static IEnumerable<GitHubGamemode> ImportGamemodes(FileInfo GamemodeFile)
#endregion

#region Generic Overrides
public override string ToString() => $"{RulesetFilename} ({TagVersion})";
public override string ToString() => $"{RulesetFilename} ({GitHubTagVersion})";

public override bool Equals(object Obj) => Obj is GitHubGamemode Gamemode && Equals(Gamemode);
public override bool Equals(object Obj) => Obj is Gamemode Gamemode && Equals(Gamemode);

public override int GetHashCode() {
unchecked {
// ReSharper disable NonReadonlyMemberInGetHashCode
int HashCode = (GitHubUser != null ? GitHubUser.GetHashCode() : 0);
HashCode = (HashCode * 397) ^ (GitHubRepo != null ? GitHubRepo.GetHashCode() : 0);
HashCode = (HashCode * 397) ^ (TagVersion != null ? TagVersion.GetHashCode() : 0);
HashCode = (HashCode * 397) ^ (GitHubTagVersion != null ? GitHubTagVersion.GetHashCode() : 0);
HashCode = (HashCode * 397) ^ (RulesetFilename != null ? RulesetFilename.GetHashCode() : 0);
// ReSharper restore NonReadonlyMemberInGetHashCode
return HashCode;
}
}

public bool Equals(GitHubGamemode Other) =>
public bool Equals(Gamemode Other) =>
GitHubUser == Other.GitHubUser &&
GitHubRepo == Other.GitHubRepo &&
TagVersion == Other.TagVersion &&
GitHubTagVersion == Other.GitHubTagVersion &&
RulesetFilename == Other.RulesetFilename;

public static bool operator ==(GitHubGamemode Left, GitHubGamemode Right) => Left.Equals(Right);
public static bool operator ==(Gamemode Left, Gamemode Right) => Left.Equals(Right);

public static bool operator !=(GitHubGamemode Left, GitHubGamemode Right) => !(Left == Right);
public static bool operator !=(Gamemode Left, Gamemode Right) => !(Left == Right);
#endregion
}
}
10 changes: 5 additions & 5 deletions GamemodeViewer.xaml → GamemodeEditor.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<mah:MetroWindow x:Class="OsuModeManager.GamemodeViewer"
<mah:MetroWindow x:Class="OsuModeManager.GamemodeEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Expand All @@ -7,7 +7,7 @@
mc:Ignorable="d"
Closing="MetroWindow_Closing"
Background="#242424"
Title="GamemodeViewer" Height="280" Width="450">
Title="Editor" Height="280" Width="450" ResizeMode="CanMinimize">
<Grid Margin="10">
<StackPanel Orientation="Vertical">
<Grid Margin="5">
Expand All @@ -24,13 +24,13 @@
</Grid>
<Grid Margin="5,10,5,5">
<Label Content="Tag Version:" Width="150" HorizontalAlignment="Left"/>
<TextBox x:Name="TextBoxTagVersion" Margin="150,0,0,0" HorizontalAlignment="Stretch"/>
<TextBox x:Name="TextBoxTagVersion" Margin="150,0,0,0" HorizontalAlignment="Stretch" TextChanged="TextBox_InvalidateUpdateCheck"/>
</Grid>
<Grid Margin="5">
<Label Content="Ruleset Filename:" Width="150" HorizontalAlignment="Left"/>
<TextBox x:Name="TextBoxRulsesetFilename" Margin="150,0,0,0" HorizontalAlignment="Stretch"/>
<TextBox x:Name="TextBoxRulsesetFilename" Margin="150,0,0,0" HorizontalAlignment="Stretch" TextChanged="TextBox_InvalidateUpdateCheck"/>
</Grid>
<Rectangle Height="10"/>
<Rectangle Height="8"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="GetLatestButton" Content="Get Latest" Width="200" Height="40" Click="GetLatestButton_Click"/>
<Rectangle Width="20"/>
Expand Down
33 changes: 19 additions & 14 deletions GamemodeViewer.xaml.cs → GamemodeEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,42 @@
using Octokit;

namespace OsuModeManager {
public partial class GamemodeViewer {
public TaskCompletionSource<GitHubGamemode> Result;
public GitHubGamemode ResultantGamemode;
public partial class GamemodeEditor {
public TaskCompletionSource<Gamemode> Result;
public Gamemode ResultantGamemode;

public GamemodeViewer() {
public GamemodeEditor() {
InitializeComponent();
}

public static async Task<GitHubGamemode> GetGamemodeViewer(GitHubGamemode CurrentGamemode = default) {
GamemodeViewer Window = new GamemodeViewer();
public static async Task<Gamemode> GetGamemodeEditor(Gamemode CurrentGamemode = default) {
Debug.WriteLine("Update required? " + CurrentGamemode.UpdateRequired);
GamemodeEditor Window = new GamemodeEditor();
Window.Show();
return await Window.GetGamemode(CurrentGamemode);
}

public async Task<GitHubGamemode> GetGamemode(GitHubGamemode CurrentGamemode = default) {
public async Task<Gamemode> GetGamemode(Gamemode CurrentGamemode = default) {
if (Result != null) {
Debug.WriteLine("Please do not request multiple gamemodes at once.", "Warning");
return default;
}

if (CurrentGamemode == default) { CurrentGamemode = GitHubGamemode.CreateInstance(); }
if (CurrentGamemode == default) {
CurrentGamemode = Gamemode.CreateInstance();
}
TextBoxGitHubURL.Text = GitHubURLEncoder.Replace("%GitHubUser%", CurrentGamemode.GitHubUser).Replace("%GitHubRepo%", CurrentGamemode.GitHubRepo);
//TextBoxGitHubUser.Text = CurrentGamemode.GitHubUser;
//TextBoxGitHubRepo.Text = CurrentGamemode.GitHubRepo;
TextBoxTagVersion.Text = CurrentGamemode.TagVersion;
TextBoxTagVersion.Text = CurrentGamemode.GitHubTagVersion;
TextBoxRulsesetFilename.Text = CurrentGamemode.RulesetFilename;

ResultantGamemode = CurrentGamemode;

//await GetLatest();

Result = new TaskCompletionSource<GitHubGamemode>();
GitHubGamemode ResultGamemode = await Result.Task;
Result = new TaskCompletionSource<Gamemode>();
Gamemode ResultGamemode = await Result.Task;

Close();
return ResultGamemode;
Expand All @@ -51,7 +54,7 @@ void SaveButton_Click(object Sender, System.Windows.RoutedEventArgs E) {

if (User.IsNullOrEmpty() || Repo.IsNullOrEmpty() || Version.IsNullOrEmpty() || RulesetFile.IsNullOrEmpty()) { return; }

ResultantGamemode = new GitHubGamemode(User, Repo, Version, RulesetFile);
ResultantGamemode = new Gamemode(User, Repo, Version, RulesetFile, ResultantGamemode.UpdateRequired);

Result?.TrySetResult(ResultantGamemode);
}
Expand Down Expand Up @@ -83,16 +86,18 @@ void TextBoxGitHubURL_TextChanged(object Sender, System.Windows.Controls.TextCha
async Task GetLatest() {
string User = TextBoxGitHubUser.Text;
string Repo = TextBoxGitHubRepo.Text;

if ((await MainWindow.GetRepositoryReleases(User, Repo)).TryGetFirst(out Release Release)) {
if ((await MainWindow.Client.Repository.Release.GetAll(User, Repo)).TryGetFirst(out Release Release)) {
Dispatcher.Invoke(() => TextBoxTagVersion.Text = Release.TagName, System.Windows.Threading.DispatcherPriority.Normal);
foreach (ReleaseAsset Asset in Release.Assets.Where(Asset => Asset.Name.ToLowerInvariant().EndsWith(".dll"))) {
Dispatcher.Invoke(() => TextBoxRulsesetFilename.Text = Asset.Name, System.Windows.Threading.DispatcherPriority.Normal);
ResultantGamemode.UpdateRequired = false;
break;
}
}
}

void MetroWindow_Closing(object Sender, System.ComponentModel.CancelEventArgs E) => Result?.TrySetResult(ResultantGamemode);

void TextBox_InvalidateUpdateCheck(object Sender, System.Windows.Controls.TextChangedEventArgs E) => ResultantGamemode.UpdateRequired = null;
}
}
Loading

0 comments on commit c17f846

Please sign in to comment.