Skip to content

Commit

Permalink
Fix to .net 8 AOT, related fixes
Browse files Browse the repository at this point in the history
Json.Net first deserializes arrays as lists and the list type is trimmed. Use list type directly to avoid it being trimmed.
  • Loading branch information
sttz committed May 22, 2024
1 parent 5a82e4f commit e320993
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Command/Command.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<PublishSingleFile>true</PublishSingleFile>
<PublishAot>true</PublishAot>
<PublishReadyToRun>true</PublishReadyToRun>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions Command/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ void DetailedModulesList(IEnumerable<Module> modules)
WriteField("Hidden", (module.hidden ? "yes" : null));
WriteField("Size", $"{Helpers.FormatSize(module.downloadSize.GetBytes())} ({Helpers.FormatSize(module.installedSize.GetBytes())} installed)");

if (module.eula?.Length > 0) {
if (module.eula?.Count > 0) {
WriteField("EULA", module.eula[0].message);
foreach (var eula in module.eula) {
WriteField("", eula.label + ": " + eula.url);
Expand Down Expand Up @@ -1189,7 +1189,7 @@ public async Task Install()
// Make user accept additional EULAs
var hasEula = false;
foreach (var module in resolved.OfType<Module>()) {
if (module.eula == null || module.eula.Length == 0) continue;
if (module.eula == null || module.eula.Count == 0) continue;
hasEula = true;
Console.WriteLine();
SetForeground(ConsoleColor.Yellow);
Expand Down
1 change: 1 addition & 0 deletions sttz.InstallUnity/Installer/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace sttz.InstallUnity
/// <summary>
/// Configuration of the Unity installer.
/// </summary>
[JsonObject(MemberSerialization.Fields)]
public class Configuration
{
[Description("After how many seconds the cache is considered to be outdated.")]
Expand Down
10 changes: 5 additions & 5 deletions sttz.InstallUnity/Installer/Scraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,20 +461,20 @@ void SetModuleKeys(Module download, IniParser.Model.SectionData section)
var eulaUrl2 = section.Keys["eulaurl2"];

var eulaCount = (eulaUrl2 != null ? 2 : 1);
download.eula = new Eula[eulaCount];
download.eula = new(eulaCount);

download.eula[0] = new Eula() {
download.eula.Add(new Eula() {
message = eulaMessage,
label = section.Keys["eulalabel1"],
url = eulaUrl1
};
});

if (eulaCount > 1) {
download.eula[1] = new Eula() {
download.eula.Add(new Eula() {
message = eulaMessage,
label = section.Keys["eulalabel2"],
url = eulaUrl2
};
});
}
}

Expand Down
32 changes: 14 additions & 18 deletions sttz.InstallUnity/Installer/UnityReleaseAPIClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -117,7 +118,7 @@ public class Response
/// <summary>
/// The release results.
/// </summary>
public Release[] results;
public List<Release> results;

// -------- Error fields --------

Expand Down Expand Up @@ -184,7 +185,7 @@ public class Release
/// <summary>
/// The Third Party Notices of the Unity Release.
/// </summary>
public ThirdPartyNotice[] thirdPartyNotices;
public List<ThirdPartyNotice> thirdPartyNotices;

[OnDeserialized]
internal void OnDeserializedMethod(StreamingContext context)
Expand Down Expand Up @@ -429,7 +430,7 @@ public class Module : Download
/// <summary>
/// EULAs the user should accept before installing.
/// </summary>
public Eula[] eula;
public List<Eula> eula;
/// <summary>
/// Sub-Modules of this module.
/// </summary>
Expand Down Expand Up @@ -653,30 +654,25 @@ public async Task<Response> Send(RequestParams request, CancellationToken cancel
/// <param name="maxResults">Limit returned results to not make too many requests</param>
/// <param name="cancellation">Cancellation token</param>
/// <returns>The results returned from the API</returns>
public async Task<Release[]> LoadAll(RequestParams request, int maxResults = 200, CancellationToken cancellation = default)
public async Task<IEnumerable<Release>> LoadAll(RequestParams request, int maxResults = 200, CancellationToken cancellation = default)
{
request.limit = 25;

int maxTotal = 0, currentOffset = 0;
Release[] releases = null;
Response response = null;
var releases = new List<Release>();
Response response;
do {
response = await Send(request, cancellation);
if (!response.IsSuccess) {
throw new Exception($"Unity Release API request failed: {response.title} - {response.detail}");
}

maxTotal = Math.Min(response.total, maxResults);
if (releases == null) {
releases = new Release[maxTotal];
}

Array.Copy(response.results, 0, releases, currentOffset, response.results.Length);
currentOffset += response.results.Length;
releases.AddRange(response.results);
currentOffset += response.results.Count;

request.offset += response.results.Length;
request.offset += response.results.Count;

} while (currentOffset < maxTotal && response.results.Length > 0);
} while (currentOffset < maxTotal && response.results.Count > 0);

return releases;
}
Expand All @@ -701,12 +697,12 @@ public async Task<IEnumerable<Release>> LoadLatest(RequestParams request, TimeSp
response = await Send(request, cancellation);
if (!response.IsSuccess) {
throw new Exception($"Unity Release API request failed: {response.title} - {response.detail}");
} else if (response.results.Length == 0) {
} else if (response.results.Count == 0) {
break;
}

releases.AddRange(response.results);
request.offset += response.results.Length;
request.offset += response.results.Count;

var oldestReleaseDate = response.results[^1].releaseDate;
var releasedSince = now - oldestReleaseDate;
Expand Down Expand Up @@ -758,7 +754,7 @@ public async Task<Release> FindRelease(UnityVersion version, Platform platform,
var result = await Send(req, cancellation);
if (!result.IsSuccess) {
throw new Exception($"Unity Release API request failed: {result.title} - {result.detail}");
} else if (result.results.Length == 0) {
} else if (result.results.Count == 0) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion sttz.InstallUnity/Installer/VirtualPackages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static IEnumerable<Module> Generator(UnityVersion version, EditorDownload editor
downloadSize = FileSize.FromMegaBytes(148),
installedSize = FileSize.FromMegaBytes(174),
parentModuleId = "Android",
eula = new Eula[] {
eula = new() {
new Eula() {
url = "https://dl.google.com/dl/android/repository/repository2-1.xml",
label = "Android SDK and NDK License Terms from Google",
Expand Down
1 change: 1 addition & 0 deletions sttz.InstallUnity/sttz.InstallUnity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<RootNamespace>sttz.InstallUnity</RootNamespace>
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

<PropertyGroup Label="Package">
Expand Down

0 comments on commit e320993

Please sign in to comment.