Skip to content

Commit

Permalink
async & profiles window search bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
weedeej committed Feb 7, 2022
1 parent 5d3023c commit ceac5de
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 76 deletions.
11 changes: 7 additions & 4 deletions ValorantCC/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ValorantCC" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:Controls="clr-namespace:Xceed.Wpf.AvalonDock.Themes.Controls;assembly=Xceed.Wpf.AvalonDock.Themes.Aero" x:Class="ValorantCC.MainWindow"
xmlns:fa="http://schemas.fontawesome.io/icons/"
mc:Ignorable="d"
Title="vTools - ValorantCC" Height="423 " Width="500" SizeToContent="Manual" AllowsTransparency="True" WindowStartupLocation="CenterScreen" WindowState="Normal" ResizeMode="CanMinimize" WindowStyle="None" Icon="Resources/vtools-colored.ico"
Title="vTools - ValorantCC" Height="0" Width="500" SizeToContent="Height" AllowsTransparency="True" WindowStartupLocation="CenterScreen" WindowState="Normal" ResizeMode="CanMinimize" WindowStyle="None" Icon="Resources/vtools-colored.ico"
Background="Transparent"
RenderOptions.BitmapScalingMode="Fant">
<Window.Resources>
Expand Down Expand Up @@ -64,7 +65,9 @@
</TextBlock>
<ComboBox x:Name="profiles" Grid.Column="2" SelectionChanged="profiles_SelectionChanged" SelectedIndex="0" IsEditable="True" IsReadOnly="True" Text="Profiles" TextBoxBase.TextChanged="profiles_TextChanged" Background="#FF232429" Foreground="White" Margin="0,-1,2,1" FontFamily="Quicksand" TextBlock.FontFamily="Segoe UI Semibold" TextBlock.FontWeight="Bold"/>
</Grid>
<Grid MaxWidth="500" MaxHeight="411">
<fa:ImageAwesome Icon="CircleOutlineNotch" Spin="True" Height="48" Width="48" Foreground="White" Name="spinner" Margin="0,0,0,5"/>
<Grid Name="ch_display" MaxWidth="500" MaxHeight="411">

<Image x:Name="CrosshairBG" Source="{StaticResource crosshairBG0}" Margin="1,3,-1,3"/>

<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right">
Expand Down Expand Up @@ -177,7 +180,7 @@

<Path Data="M28,378 L573,378" MaxWidth="500" MaxHeight="411" Fill="#16171B" Height="1" Stretch="Fill" Stroke="White" Width="500"/>

<Grid Margin="3" Height="26" RenderTransformOrigin="0.5,0.5" Width="494" MaxWidth="500" MaxHeight="411">
<Grid Name="buttons_group" Margin="3" Height="26" RenderTransformOrigin="0.5,0.5" Width="494" MaxWidth="500" MaxHeight="411">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="1"/>
Expand All @@ -200,7 +203,7 @@
<Button x:Name="btnShare" Style="{DynamicResource RoundButton}" Content="Share" HorizontalContentAlignment="Left" Click="btnShare_Click" Cursor="Hand" Height="25" VerticalContentAlignment="Top" HorizontalAlignment="Left" VerticalAlignment="Center" Width="124" FontFamily="Quicksand" FontSize="11" FontWeight="DemiBold" TextBlock.FontFamily="Segoe UI Semibold" TextBlock.FontWeight="Bold" Margin="0,0,0,0"/>

</Grid>
<Grid Width="495" MaxWidth="495" MaxHeight="411">
<Grid Name="controls_group" Width="495" MaxWidth="495" MaxHeight="411">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="119*"/>
<ColumnDefinition Width="46*"/>
Expand Down
10 changes: 5 additions & 5 deletions ValorantCC/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private async void Grid_Loaded(object sender, RoutedEventArgs e)
}
}

private void btnSave_Click(object sender, RoutedEventArgs e)
private async void btnSave_Click(object sender, RoutedEventArgs e)
{
if (!LoggedIn)
{
Expand All @@ -67,9 +67,9 @@ private void btnSave_Click(object sender, RoutedEventArgs e)
{
SelectedColors = new List<Color> { (Color)primary_color.SelectedColor };
}
if (DataProcessor.SaveNewColor(SelectedColors, profiles.SelectedIndex, profiles.Text))
if (await DataProcessor.SaveNewColor(SelectedColors, profiles.SelectedIndex, profiles.Text))
{
DataProcessor.Construct();
await DataProcessor.Construct();
profiles.Items.Refresh();
profiles.SelectedIndex = DataProcessor.CurrentProfile;
MessageTxt.Foreground = Brushes.Lime;
Expand Down Expand Up @@ -110,15 +110,15 @@ private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
if (e.LeftButton == MouseButtonState.Pressed) DragMove();
}

private void btnReload_Click(object sender, RoutedEventArgs e)
private async void btnReload_Click(object sender, RoutedEventArgs e)
{
if (!LoggedIn)
{
MessageTxt.Text = "You are not logged in!";
return;
}
Utils.Log("Reload Clicked > Reconstructing Processor.");
DataProcessor.Construct();
await DataProcessor.Construct();
profiles.ItemsSource = DataProcessor.ProfileNames;
profiles.Items.Refresh();
profiles.SelectedIndex = DataProcessor.CurrentProfile;
Expand Down
2 changes: 1 addition & 1 deletion ValorantCC/SubWindow/ProfilesWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
<TextBox Style="{StaticResource LineTextBox}" Name="SearchCode" MinWidth="128" MaxWidth="128" Background="#FF393B44" Margin="65,25,5,0" Foreground="White" Height="22"/>
<Button Style="{DynamicResource SearchCodeButtonStyle}" Name="btnSearchCode" Content="Search Code" MinWidth="85" Background="#FF393B44" Margin="0,25,5,0" Foreground="White" Cursor="Hand" Click="btnSearchCode_Click" BorderBrush="#FF393B44"/>
</StackPanel>
<Image gif:ImageBehavior.AnimatedSource="../Resources/vtspinner.gif" Width="50" Height="52" Margin="0,155,0,0" x:Name="LoadingPlaceHolder"/>
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Margin="0,5,0,0" Height="388" Width="338">

<StackPanel Name="ShareablesContainer" HorizontalAlignment="Center">
<Image gif:ImageBehavior.AnimatedSource="../Resources/vtspinner.gif" Width="50" Height="52" Margin="0,155,0,0" x:Name="LoadingPlaceHolder"/>
</StackPanel>
</ScrollViewer>
</StackPanel>
Expand Down
11 changes: 5 additions & 6 deletions ValorantCC/SubWindow/ProfilesWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ private async void Border_Loaded(object sender, RoutedEventArgs e)
/// Returns the searched sharecode or the list of shareables depending if sharecode is provided or not.
/// </summary>
/// <param name="sharecode">Nullable. Searched Code</param>
private static async Task<bool> InitialFetch(String sharecode = null)
private async Task<bool> InitialFetch(String sharecode = null)
{
PublicProfiles.Clear();
ShareablesContainer.Children.Clear();
List<ShareableProfile> Shareables;
if (sharecode != null)
if (!string.IsNullOrEmpty(sharecode) || !string.IsNullOrWhiteSpace(sharecode))
{
ValCCApi.Action = 3;
Shareables = (await ValCCApi.Fetch(sharecode)).data;
Expand All @@ -81,9 +82,6 @@ private static async Task<bool> InitialFetch(String sharecode = null)
}
PublicProfiles.Add(new PublicProfile() { owner = currentShareable.displayName, settings = profile, sharecode = currentShareable.shareCode, ID = i });
}

//Change this delay for the async backend
await Task.Delay(1);
return true;
}

Expand All @@ -105,8 +103,9 @@ private async Task<bool> RenderProfiles()
}
private async void btnSearchCode_Click(object sender, RoutedEventArgs e)
{
if (SearchCode.Text == null || SearchCode.Text == "") return;
LoadingPlaceHolder.Visibility = Visibility.Visible;
await InitialFetch(SearchCode.Text);
LoadingPlaceHolder.Visibility = Visibility.Collapsed;
await RenderProfiles();
}

Expand Down
3 changes: 2 additions & 1 deletion ValorantCC/ValorantCC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@

<ItemGroup>
<PackageReference Include="EZ_Updater" Version="0.5.0.4" />
<PackageReference Include="FontAwesome.WPF" Version="4.7.0.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RestSharp" Version="106.15.0" />
<PackageReference Include="RestSharp" Version="107.2.1" />
<PackageReference Include="WpfAnimatedGif" Version="2.0.2" />
</ItemGroup>

Expand Down
13 changes: 6 additions & 7 deletions ValorantCC/src/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public async Task<FetchResponse> Fetch(String sharecode = null)
payload.sharecode = sharecode;
payload.action = Action;
}
RestRequest request = new RestRequest() { Method = Method.POST };
RestRequest request = new RestRequest() { Method = Method.Post };
request.AddJsonBody(payload);
RestResponse response = (RestResponse)await client.ExecuteAsync(request);
RestResponse response = await client.ExecuteAsync(request);
if (!response.IsSuccessful) return new FetchResponse() { success = false };

if (response.StatusCode != System.Net.HttpStatusCode.OK) return new FetchResponse() { success = false };
return JsonConvert.DeserializeObject<FetchResponse>(response.Content);
}

Expand All @@ -81,13 +81,12 @@ public async Task<VoidCallResponse> Set()
shareable = Shareable,
action = 1
};
RestRequest request = new RestRequest() { Method = Method.POST };
//client.Authenticator = new JwtAuthenticator(AuthTokens.AccessToken);
RestRequest request = new RestRequest() { Method = Method.Post };
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(payload);
request.AddOrUpdateHeader("Authorization", $"Bearer {AuthTokens.AccessToken}"); // Pass to server so nobody can set somebody's saved profile.
RestResponse response = (RestResponse)await client.ExecuteAsync(request);
if (response.StatusCode != System.Net.HttpStatusCode.OK) return new VoidCallResponse() { success = false };
RestResponse response = await client.ExecuteAsync(request);
if (!response.IsSuccessful) return new VoidCallResponse() { success = false };
return JsonConvert.DeserializeObject<VoidCallResponse>(response.Content);
}

Expand Down
48 changes: 14 additions & 34 deletions ValorantCC/src/Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Threading.Tasks;
using Utilities;
using RestSharp;
namespace ValorantCC
{
public partial class AuthTokens
Expand Down Expand Up @@ -53,6 +54,7 @@ public partial class VersionData
public class AuthObj
{
LockfileData LocalCredentials;
static RestClient client = new RestClient(new RestClientOptions() { RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true });
public async Task<AuthResponse> StartAuth()
{
string LockfilePath = Environment.GetEnvironmentVariable("LocalAppData") + "\\Riot Games\\Riot Client\\Config\\lockfile";
Expand Down Expand Up @@ -104,49 +106,27 @@ public static LockfileData ObtainLockfileData(string LockfilePath)

private async Task<AuthTokens> ObtainAuthTokens()
{
await Task.Delay(1);
Utils.Log("Creating Auth Request");
RestRequest request = new RestRequest($"https://127.0.0.1:{LocalCredentials.Port}/entitlements/v1/token", Method.Get);
request.AddHeader("Authorization", $"Basic {LocalCredentials.Basic}");

HttpWebRequest request = HttpWebRequest.CreateHttp($"https://127.0.0.1:{LocalCredentials.Port}/entitlements/v1/token");
request.Method = "GET";
request.Headers.Add("Authorization", $"Basic {LocalCredentials.Basic}");
request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
WebResponse resp;
try
{
Utils.Log("Sending Auth Request");
Task<WebResponse> taskResp = Task.Factory.FromAsync(
request.BeginGetResponse, request.EndGetResponse,
(object)null);
resp = await taskResp;
}
catch (WebException ex)
{
Utils.Log($"Auth Error {ex}");
return new AuthTokens();
}
RestResponse response = await client.ExecuteAsync(request);
if (!response.IsSuccessful) return new AuthTokens();

StreamReader sr = new StreamReader(resp.GetResponseStream());
AuthTokens tokens = JsonConvert.DeserializeObject<AuthTokens>(sr.ReadToEnd());
AuthTokens tokens = JsonConvert.DeserializeObject<AuthTokens>(response.Content.ToString());
tokens.Success = true;
return tokens;
}

private async static Task<String> GetVersion()
{
await Task.Delay(1);
HttpWebRequest request = HttpWebRequest.CreateHttp("https://valorant-api.com/v1/version");
request.Method = "GET";
Task<WebResponse> taskResp = Task.Factory.FromAsync(
request.BeginGetResponse,
asyncResult => request.EndGetResponse(asyncResult),
(object)null);
VersionResponse RespData = await taskResp.ContinueWith(r =>
{
WebResponse response = (WebResponse)r.Result;
StreamReader sr = new StreamReader(response.GetResponseStream());
return JsonConvert.DeserializeObject<VersionResponse>(sr.ReadToEnd());
});
Utils.Log("Obtaining Client Version info");
RestRequest request = new RestRequest("https://valorant-api.com/v1/version", Method.Get);

RestResponse response = await client.ExecuteAsync(request);
if (!response.IsSuccessful) return "0.0.0";

VersionResponse RespData = JsonConvert.DeserializeObject<VersionResponse>(response.Content.ToString());
return RespData.Data.RiotClientVersion;
}
}
Expand Down
11 changes: 9 additions & 2 deletions ValorantCC/src/BackgroundAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public async void LoopCheck()
{
string LockfilePath = Environment.GetEnvironmentVariable("LocalAppData") + "\\Riot Games\\Riot Client\\Config\\lockfile"; //Copy pasted from Auth.cs because why not?
bool lockfilexists = false;
main.ch_display.Visibility = Visibility.Collapsed;
main.buttons_group.Visibility = Visibility.Collapsed;
main.controls_group.Visibility = Visibility.Collapsed;
while (true)
{
if (AuthObj.CheckLockFile(LockfilePath) && !lockfilexists)
Expand All @@ -36,8 +39,7 @@ public async void LoopCheck()
if (lockfilexists && AuthObj.ObtainLockfileData(LockfilePath).Success)
{
main.StatusTxt.Text = "Logging in. . .";
processor.Login();
AuthResponse AuthResponse = processor.Login();
AuthResponse AuthResponse = await processor.Login();
main.DataProcessor = processor;
main.LoggedIn = AuthResponse.Success;
if (!main.LoggedIn)
Expand All @@ -56,6 +58,11 @@ public async void LoopCheck()
main.MessageTxt.Foreground = Brushes.Lime;
main.MessageTxt.Text = Utils.LoginResponse(processor);
main.StatusTxt.Text = "Logged In!";

main.ch_display.Visibility = Visibility.Visible;
main.buttons_group.Visibility = Visibility.Visible;
main.controls_group.Visibility = Visibility.Visible;
main.spinner.Visibility = Visibility.Collapsed;
return;
}
await Task.Delay(1500);
Expand Down
Loading

0 comments on commit ceac5de

Please # to comment.