Skip to content

Commit

Permalink
Fix startup crash duo to serialization issues
Browse files Browse the repository at this point in the history
Increase HttpClient timeout
Migrate some serialization fron Newtonsoft to .Net
  • Loading branch information
floh22 committed Nov 18, 2022
1 parent 1067472 commit 9417d88
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 32 deletions.
31 changes: 29 additions & 2 deletions LeagueBroadcast.Common/Data/DTO/SummonerSpell.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@

using LeagueBroadcast.Common.Utils;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace LeagueBroadcast.Common.Data.DTO
{

//This is dumb but its a workaround
public class SummonerSpell
{
public static HashSet<SummonerSpell> All { get; set; } = new();

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

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

[JsonPropertyName("name")]
public string Name { get; set; } = "";
Expand All @@ -22,5 +26,28 @@ public class SummonerSpell

[JsonPropertyName("iconPath")]
public string IconPath { get; set; } = "";

public FrontEndSummonerSpell AsFrontEndSummonerSpell()
{
return new FrontEndSummonerSpell
{
id = ID,
name = Name,
iconPath = IconPath,
};
}
}

public class FrontEndSummonerSpell
{
public string id { get; set; } = "";

public string key => id;

public string name { get; set; } = "";

public string icon => iconPath;

public string iconPath { get; set; } = "";
}
}
34 changes: 25 additions & 9 deletions LeagueBroadcast.Common/Http/RestRequester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
using System.Net.Http;
using System.Threading.Tasks;
using System.Text.Json;
using System.Text.Json.Serialization;
using LeagueBroadcast.Common;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace LeagueBroadcast.Update.Http
{
//https://github.com/Johannes-Schneider/GoldDiff/blob/master/GoldDiff.Shared/Http/RestRequester.cs
public class RestRequester : IDisposable
{
public static TimeSpan DefaultRequestTimeout { get; } = TimeSpan.FromMilliseconds(500);
public static TimeSpan DefaultRequestTimeout { get; } = TimeSpan.FromMilliseconds(2000);

private HttpClient Client { get; }

Expand All @@ -21,25 +25,24 @@ private static RestRequester GetInstance()
{
if (_instance == null)
{
_instance = new RestRequester(TimeSpan.FromMilliseconds(500), null);
_instance = new RestRequester(TimeSpan.FromMilliseconds(2000), null);
}
return _instance;
}

private RestRequester(TimeSpan requestTimeout, HttpClientHandler? clientHandler = null)
{

Client = new HttpClient(clientHandler ?? new HttpClientHandler())
{
Timeout = requestTimeout,
DefaultRequestHeaders =
{
Accept = {MediaTypeWithQualityHeaderValue.Parse("application/json")},
UserAgent = {ProductInfoHeaderValue.Parse("request")},
},
};

Client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
Client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("LeagueBroadcast", "2.0"));
}

public static async Task<TResultType?> GetAsync<TResultType>(string url)
public static async Task<TResultType?> GetAsync<TResultType>(string url, ICollection<JsonConverter> converters = null)
{
try
{
Expand All @@ -50,7 +53,20 @@ private RestRequester(TimeSpan requestTimeout, HttpClientHandler? clientHandler
return default;
}
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<TResultType>(json)!;


var serializationOptions = new JsonSerializerOptions
{
};

if (converters != null)
{
foreach(var converter in converters)
{
serializationOptions.Converters.Add(converter);
}
}
return JsonSerializer.Deserialize<TResultType>(json, serializationOptions)!;
}
catch (TaskCanceledException)
{
Expand Down
2 changes: 1 addition & 1 deletion LeagueBroadcast.Common/LeagueBroadcast.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<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" />
<PackageReference Include="System.Text.Json" Version="7.0.0" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions LeagueBroadcast.Common/Utils/NumberToStringJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace LeagueBroadcast.Common.Utils
{
public sealed class NumberToStringJsonConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return $"{reader.GetDecimal()}";
}

public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
writer.WriteNumberValue(decimal.Parse(value));
}
}
}
4 changes: 2 additions & 2 deletions LeagueBroadcast/ChampSelect/Data/DTO/Pick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace LeagueBroadcast.ChampSelect.Data.DTO
public class Pick : PickBan
{
public int id;
public SummonerSpell spell1;
public SummonerSpell spell2;
public FrontEndSummonerSpell spell1;
public FrontEndSummonerSpell spell2;
public bool isActive = false;
public string displayName = "";

Expand Down
4 changes: 2 additions & 2 deletions LeagueBroadcast/ChampSelect/Data/LCU/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class Cell
public int cellId;
public int championId;
public int summonerId;
public int spell1Id;
public int spell2Id;
public string spell1Id;
public string spell2Id;
}
}
4 changes: 2 additions & 2 deletions LeagueBroadcast/ChampSelect/StateInfo/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public static Team ConvertTeam(ConversionInput kwargs)
var pick = new Pick(cell.cellId);

var spell1 = DataDragon.Instance.GetSummonerById(cell.spell1Id);
pick.spell1 = new SummonerSpell() { ID = cell.spell1Id , IconPath = spell1 != null ? spell1.Icon : "" };
pick.spell1 = new SummonerSpell() { ID = cell.spell1Id , IconPath = spell1 != null ? spell1.Icon : "" }.AsFrontEndSummonerSpell();
var spell2 = DataDragon.Instance.GetSummonerById(cell.spell2Id);
pick.spell2 = new SummonerSpell() { ID = cell.spell2Id , IconPath = spell2 != null ? spell2.Icon : "" };
pick.spell2 = new SummonerSpell() { ID = cell.spell2Id , IconPath = spell2 != null ? spell2.Icon : "" }.AsFrontEndSummonerSpell();

var champion = DataDragon.Instance.GetChampionById(cell.championId);
pick.champion = champion;
Expand Down
11 changes: 3 additions & 8 deletions LeagueBroadcast/Common/Data/Provider/DataDragon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using static LeagueBroadcast.Common.Data.Provider.DataDragon;
using LeagueBroadcast.Common.Controllers;
using LeagueBroadcast.MVVM.Core;
using LeagueBroadcast.MVVM.ViewModel;
using Newtonsoft.Json;
using LeagueBroadcast.Common.Data.DTO;
using LeagueBroadcast.Common.Data.RIOT;
using System.Threading;
using Swan.Logging;
using LeagueBroadcast.Common.Utils;
using LeagueBroadcast.ChampSelect.Data.DTO;
using LeagueBroadcast.Update.Http;
using System.Text.Json;
using LeagueBroadcast.Common.Data.Config;
using System.Collections.Concurrent;
using System.Diagnostics;

Expand Down Expand Up @@ -403,12 +398,12 @@ public static void ExtendChampionLocal(CDragonChampion champion, StringVersion v

public static void ExtendSummonerLocal(SummonerSpell summoner, StringVersion version)
{
summoner.IconPath = Path.Combine("cache", $"{version.ToString(2)}.1", "spell", $"{summoner.Name}.png");
summoner.IconPath = $"cache/{version.ToString(2)}.1/spell/{summoner.ID}.png";
}

public static void ExtendItemLocal(CDragonItem item, StringVersion version)
{
item.IconPath = Path.Combine("cache", $"{version.ToString(2)}.1", "item", item.ID + ".png");
item.IconPath = $"cache/{version.ToString(2)}.1/item/{item.ID}.png";
}

#endregion
Expand All @@ -431,7 +426,7 @@ public Champion GetChampionById(int champID)
};
}

public SummonerSpell GetSummonerById(int summonerID)
public SummonerSpell GetSummonerById(string summonerID)
{
return SummonerSpell.All.SingleOrDefault(s => s.ID == summonerID);
}
Expand Down
6 changes: 3 additions & 3 deletions LeagueBroadcast/Ingame/State/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
using LeagueBroadcast.MVVM.View;
using LeagueBroadcast.MVVM.ViewModel;
using LeagueBroadcast.OperatingSystem;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.Json;

namespace LeagueBroadcast.Ingame.State
{
Expand Down Expand Up @@ -105,7 +105,7 @@ public void UpdateTeams(List<Player> PlayerData, Snapshot gameSnap)
if(!ShowedChampionMemoryError)
{
Log.Warn(e.Message);
Log.Verbose($"Players:\n{JsonConvert.SerializeObject(GetAllPlayers())}\nSnapshot:\n{JsonConvert.SerializeObject(gameSnap.Champions)}");
Log.Verbose($"Players:\n{JsonSerializer.Serialize(GetAllPlayers())}\nSnapshot:\n{JsonSerializer.Serialize(gameSnap.Champions)}");
MessageBoxUtils.ShowErrorBox("Could not read all champion data from memory. Please submit an issue on github containing the current log and replay.");
ShowedChampionMemoryError = true;
}
Expand Down Expand Up @@ -193,7 +193,7 @@ public void UpdateEvents(List<RiotEvent> allEvents, Snapshot gameSnap)
{
newEvents.Add(e);
if (Log.Instance.Level == Log.LogLevel.Verbose)
Log.Verbose($"New Event: {JsonConvert.SerializeObject(e)}");
Log.Verbose($"New Event: {JsonSerializer.Serialize(e)}");
}
});

Expand Down
5 changes: 2 additions & 3 deletions LeagueBroadcast/LeagueBroadcast.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<RepositoryUrl>https://github.com/floh22/LeagueBroadcast</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageIcon>BE_icon.png</PackageIcon>
<AssemblyVersion>1.5.24.0</AssemblyVersion>
<FileVersion>1.5.24.0</FileVersion>
<AssemblyVersion>1.5.25.0</AssemblyVersion>
<FileVersion>1.5.25.0</FileVersion>
<RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
</PropertyGroup>

Expand Down Expand Up @@ -88,7 +88,6 @@

<ItemGroup>
<PackageReference Include="EmbedIO" Version="3.5.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Management" Version="6.0.0" />
</ItemGroup>

Expand Down

0 comments on commit 9417d88

Please sign in to comment.