Skip to content

Commit

Permalink
Fix json serializer issues in trimmed mode (#28)
Browse files Browse the repository at this point in the history
* - Shorten logging to make it clearer
- Add more colors to logging to make it clearer
- Add more exe exclusion words
- Allow NonPublic DefaultConstructor (fixes #26)
- Add app version/runtime version at the top for future diagnostics
- Fix typos

* Fix json serializer reflection issues in trimmed mode
  • Loading branch information
JMTK authored Nov 4, 2024
1 parent 01b635e commit 66b4a31
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 37 deletions.
4 changes: 2 additions & 2 deletions SunshineGameFinder/FileWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using System.Text.Json;

namespace SunshineGameFinder
{
Expand All @@ -22,7 +22,7 @@ internal static bool UpdateConfig(string filePath, SunshineConfig config)
string backUpFilePath = Path.Combine(folderPath, $"{Path.GetFileNameWithoutExtension(filePath)}_{DateTime.Now.ToString("MMddyyyy_HHmmss")}.{backupFileExtension}");
File.Move(filePath, backUpFilePath);

File.WriteAllText(filePath, JsonConvert.SerializeObject(config, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }));
File.WriteAllText(filePath, JsonSerializer.Serialize<SunshineConfig>(config, SourceGenerationContext.Default.SunshineConfig));
}
catch (Exception e)
{
Expand Down
6 changes: 3 additions & 3 deletions SunshineGameFinder/ImageScraper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using System.Text.Json;

namespace SunshineGameFinder
{
Expand Down Expand Up @@ -127,7 +127,7 @@ private static double CalculateSimilarity(string source, string target)
private static async Task<int> GetIDForGame(string gameName)
{
var rawJson = await (await HttpClient.GetAsync(bucketTemplate.Replace("@FIRSTTWOLETTERS", string.Join("", gameName.Take(2)).ToLower()))).Content.ReadAsStringAsync();
var dict = JsonConvert.DeserializeObject<Dictionary<int, GamesForBucket>>(rawJson);
var dict = JsonSerializer.Deserialize<Dictionary<int, GamesForBucket>>(rawJson);
KeyValuePair<int, GamesForBucket>? FindGameFuzzy(double percentage)
{
return dict.FirstOrDefault(kvp => CalculateSimilarity(kvp.Value.name.ToLower(), gameName.ToLower()) > (percentage / 100));
Expand All @@ -147,7 +147,7 @@ public static async Task<string> SaveIGDBImageToCoversFolder(string gameName, st
{
int gameId = await GetIDForGame(gameName);
var rawJson = await (await HttpClient.GetAsync(gameTemplate.Replace("@ID", gameId.ToString()))).Content.ReadAsStringAsync();
var game = JsonConvert.DeserializeObject<Game>(rawJson);
var game = JsonSerializer.Deserialize<Game>(rawJson);
if (game == null) return null;
var coverUrl = game.cover.url;
var stream = await (await HttpClient.GetAsync("https:" + coverUrl.Replace("thumb", "cover_big"))).Content.ReadAsStreamAsync();
Expand Down
1 change: 1 addition & 0 deletions SunshineGameFinder/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static void Log(string message, LogLevel level, bool newline = true)
Console.Write(message);
}

Console.ForegroundColor = ConsoleColor.White;
}
}

Expand Down
8 changes: 3 additions & 5 deletions SunshineGameFinder/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// See https://aka.ms/new-console-template for more information
using Gameloop.Vdf;
using Gameloop.Vdf.Linq;
using Newtonsoft.Json;
using System.Text.Json;
using SunshineGameFinder;
using System.CommandLine;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -65,10 +65,8 @@
Logger.Log($"Could not find Sunshine Apps config at specified path: {sunshineAppsJson}", LogLevel.Error);
return;
}
var sunshineAppInstance = JsonConvert.DeserializeObject<SunshineConfig>(File.ReadAllText(sunshineAppsJson), new JsonSerializerSettings()
{
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
});
var sunshineAppInstance = JsonSerializer.Deserialize<SunshineConfig>(File.ReadAllText(sunshineAppsJson), SourceGenerationContext.Default.SunshineConfig);
var gamesAdded = 0;
var gamesRemoved = 0;
Expand Down
36 changes: 13 additions & 23 deletions SunshineGameFinder/SunshineAppsConfig.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,45 @@
using Newtonsoft.Json;
using System.Text.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json.Serialization;

namespace SunshineGameFinder
{
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(SunshineConfig))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
}
public class SunshineApp
{
public string name { get; set; }

[JsonProperty("image-path")]
[JsonPropertyName("image-path")]
public string imagepath { get; set; }
public List<string> detached { get; set; }
public string output { get; set; }
public string cmd { get; set; }

[JsonProperty("working-dir")]
[JsonPropertyName("working-dir")]
public string workingdir { get; set; }

[JsonProperty("exclude-global-prep-cmd")]
[JsonPropertyName("exclude-global-prep-cmd")]
public bool excludeglobalprepcmd { get; set; }
public bool elevated { get; set; }
[JsonProperty("auto-detach")]
[JsonPropertyName("auto-detach")]
public bool autodetach { get; set; }
[JsonProperty("wait-all")]
[JsonPropertyName("wait-all")]
public bool waitall { get; set; }

[JsonProperty("exit-timeout")]
[JsonPropertyName("exit-timeout")]
public int exittimeout { get; set; } = 5;
}

public class Env
{
public string PATH { get; set; }
}

public class SunshineConfig
{
public SunshineConfig()
{

}
public SunshineConfig(Env env, List<SunshineApp> apps)
{
this.env = env;
this.apps = apps;
}

public Env env { get; set; }
public List<SunshineApp> apps { get; set; }
}
}
9 changes: 5 additions & 4 deletions SunshineGameFinder/SunshineGameFinder.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PublishTrimmed>true</PublishTrimmed>
<PublishTrimmed>True</PublishTrimmed>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -13,8 +13,9 @@
<RepositoryUrl>https://github.com/JMTK/SunshineGameFinder</RepositoryUrl>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
<SignAssembly>False</SignAssembly>
<AssemblyVersion>1.8.0</AssemblyVersion>
<FileVersion>1.8.0</FileVersion>
<AssemblyVersion>1.8.1</AssemblyVersion>
<FileVersion>1.8.1</FileVersion>
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>

<ItemGroup>
Expand All @@ -26,8 +27,8 @@

<ItemGroup>
<PackageReference Include="Gameloop.Vdf" Version="0.6.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

</Project>

0 comments on commit 66b4a31

Please sign in to comment.