Skip to content

Commit

Permalink
Update to .NET 6. Fixes #195
Browse files Browse the repository at this point in the history
Switch from DDrag to CDrag. Fixes #196, #197
Fix loading indicator
Fix crash when offsets not yet updated
  • Loading branch information
floh22 committed Nov 2, 2022
1 parent 833d0b9 commit 81bfbce
Show file tree
Hide file tree
Showing 25 changed files with 1,502 additions and 1,085 deletions.
10 changes: 9 additions & 1 deletion LCUSharp/LCUSharp.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<AssemblyVersion>1.4.17.0</AssemblyVersion>
<FileVersion>1.4.17.21326</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Websocket.Client" Version="4.3.30" />
Expand Down
29 changes: 27 additions & 2 deletions LeagueBroadcast.Common/Data/DTO/Champion.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace LeagueBroadcast.Common.Data.DTO
{
public class Champion
{
public static List<Champion> Champions = new();

public string id;
public int key;
public string name;
Expand All @@ -16,4 +15,30 @@ public class Champion
public string loadingImg;
public string squareImg;
}

public class CDragonChampion
{
public static HashSet<CDragonChampion> All { get; set; } = new();

[JsonPropertyName("id")]
public int ID { get; set; } = 0;

[JsonPropertyName("alias")]
public string Alias { get; set; } = "";

[JsonPropertyName("name")]
public string Name { get; set; } = "";

[JsonPropertyName("splashImg")]
public string? SplashImg { get; set; }

[JsonPropertyName("splashCenteredImg")]
public string? SplashCenteredImg { get; set; }

[JsonPropertyName("loadingImg")]
public string? LoadingImg { get; set; }

[JsonPropertyName("squareImg")]
public string? SquareImg { get; set; }
}
}
21 changes: 16 additions & 5 deletions LeagueBroadcast.Common/Data/DTO/SummonerSpell.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace LeagueBroadcast.Common.Data.DTO
{
public class SummonerSpell
{
public static List<SummonerSpell> SummonerSpells = new();
public static HashSet<SummonerSpell> All { get; set; } = new();

public string id;
public int key;
public string name;
public string icon;
[JsonPropertyName("id")]
public int ID { get; set; } = -1;

[JsonPropertyName("key")]
public int Key => ID;

[JsonPropertyName("name")]
public string Name { get; set; } = "";

[JsonPropertyName("icon")]
public string Icon => IconPath;

[JsonPropertyName("iconPath")]
public string IconPath { get; set; } = "";
}
}
38 changes: 33 additions & 5 deletions LeagueBroadcast.Common/Data/RIOT/ItemData.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace LeagueBroadcast.Common.Data.RIOT
{
public class ItemData
{
public static List<ItemData> Items = new();

public int itemID;
public ItemCost gold;
public int specialRecipe;
Expand All @@ -20,3 +17,34 @@ public ItemData(int itemID)
}
}
}


public class CDragonItem
{

public static HashSet<CDragonItem> All { get; set; } = new();

public static HashSet<CDragonItem> Full { get; set; } = new();


[JsonPropertyName("id")]
public int ID { get; set; } = -1;

[JsonPropertyName("name")]
public string Name { get; set; } = "";

[JsonPropertyName("from")]
public List<int> Components { get; set; } = new();

[JsonPropertyName("price")]
public int Price { get; set; } = 0;

[JsonPropertyName("priceTotal")]
public int PriceTotal { get; set; } = 0;

[JsonPropertyName("specialRecipe")]
public int SpecialRecipe { get; set; } = 0;

[JsonPropertyName("iconPath")]
public string IconPath { get; set; } = "";
}
36 changes: 10 additions & 26 deletions LeagueBroadcast.Common/Http/FileDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace LeagueBroadcast.Update.Http
{
//https://github.com/Johannes-Schneider/GoldDiff/blob/master/GoldDiff.Shared/Http/FileDownloader.cs
//Adapted from https://github.com/Johannes-Schneider/GoldDiff/blob/master/GoldDiff.Shared/Http/FileDownloader.cs
public class FileDownloader
{
#nullable enable
public event EventHandler<DownloadProgressEventArguments>? DownloadProgressChanged;

public async Task DownloadAsync(string? remoteUrl, string? filePath)
public static async Task<HttpStatusCode> DownloadAsync(string? remoteUrl, string? filePath, IProgress<float>? progress = null, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(remoteUrl))
{
Expand All @@ -23,37 +22,22 @@ public async Task DownloadAsync(string? remoteUrl, string? filePath)
throw new ArgumentNullException(nameof(filePath));
}

var fileDirectory = Path.GetDirectoryName(filePath);
string? fileDirectory = Path.GetDirectoryName(filePath);
if (!Directory.Exists(fileDirectory))
{
Directory.CreateDirectory(fileDirectory!);
_ = Directory.CreateDirectory(fileDirectory!);
}

if (File.Exists(filePath))
{
File.Delete(filePath!);
}

using var webClient = new WebClient();
var downloadStartTime = DateTime.Now;
webClient.DownloadProgressChanged += (_, args) =>
{
if (DownloadProgressChanged == null)
{
return;
}

var elapsedTime = DateTime.Now - downloadStartTime;
var progress = args.BytesReceived / (double)args.TotalBytesToReceive;
var bytesPerSecond = args.BytesReceived / elapsedTime.TotalSeconds;
var estimatedRemainingTime = TimeSpan.FromSeconds((args.TotalBytesToReceive - args.BytesReceived) / bytesPerSecond);

DownloadProgressChanged.Invoke(this, new DownloadProgressEventArguments(progress,
bytesPerSecond / (1024.0d * 1024.0d),
estimatedRemainingTime));
};
await webClient.DownloadFileTaskAsync(remoteUrl!, filePath!);
using HttpClient httpClient = new();
httpClient.Timeout = TimeSpan.FromMinutes(5);
using FileStream file = new(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
return await httpClient.DownloadAsync(remoteUrl, file, progress, cancellationToken);
}
}
#nullable disable
}
51 changes: 41 additions & 10 deletions LeagueBroadcast.Common/Http/RestRequester.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using LeagueBroadcast.Common;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text.Json;
using LeagueBroadcast.Common;

namespace LeagueBroadcast.Update.Http
{
Expand All @@ -14,7 +14,19 @@ public class RestRequester : IDisposable

private HttpClient Client { get; }

public RestRequester(TimeSpan requestTimeout, HttpClientHandler? clientHandler = null)
private static RestRequester? _instance;
private static RestRequester Instance => GetInstance();

private static RestRequester GetInstance()
{
if (_instance == null)
{
_instance = new RestRequester(TimeSpan.FromMilliseconds(500), null);
}
return _instance;
}

private RestRequester(TimeSpan requestTimeout, HttpClientHandler? clientHandler = null)
{
Client = new HttpClient(clientHandler ?? new HttpClientHandler())
{
Expand All @@ -27,18 +39,37 @@ public RestRequester(TimeSpan requestTimeout, HttpClientHandler? clientHandler =
};
}

public async Task<TResultType> GetAsync<TResultType>(string url)
public static async Task<TResultType?> GetAsync<TResultType>(string url)
{
try
{
var response = await Client.GetAsync(url).ConfigureAwait(false);
var response = await Instance.Client.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
Log.Warn($"Request to {url} ({nameof(TResultType)} = {typeof(TResultType).Name}) returned status code {response.StatusCode}.");
return default!;
return default;
}
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<TResultType>(json)!;
}
catch (TaskCanceledException)
{
Log.Warn($"Request to {url} caused a {nameof(TaskCanceledException)}. This is usually an indicator for a timeout.");
return default;
}
}

public static async Task<string> GetRaw(string url)
{
try
{
HttpResponseMessage? response = await Instance.Client.GetAsync(url).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
Log.Warn($"Request to {url} returned status code {response.StatusCode}.");
throw new HttpRequestException(response.StatusCode + "");
}
var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<TResultType>(json);
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
catch (TaskCanceledException)
{
Expand Down
11 changes: 10 additions & 1 deletion LeagueBroadcast.Common/LeagueBroadcast.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<AssemblyVersion>1.4.23.0</AssemblyVersion>
<FileVersion>1.4.23.21326</FileVersion>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Text.Json" Version="6.0.6" />
</ItemGroup>

</Project>
Loading

0 comments on commit 81bfbce

Please sign in to comment.