Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix log encoding detect #126

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Helper/EncodingHelper.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
using System.Text;
using System;
using System.IO;

namespace ProjBobcat.Class.Helper;

public static class EncodingHelper
{
static EncodingHelper()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}

public static Encoding GuessEncoding(string input)
{
if (IsUtf8(input)) return Encoding.UTF8;
if (IsUtf32(input)) return Encoding.UTF32;
if (IsBigEndianUnicode(input)) return Encoding.BigEndianUnicode;
if (IsUnicode(input)) return Encoding.Unicode;
if (IsUtf7(input)) return Encoding.UTF7;

Check warning on line 20 in ProjBobcat/ProjBobcat/Class/Helper/EncodingHelper.cs

View workflow job for this annotation

GitHub Actions / Test build on ubuntu-latest .NET 8.0

'Encoding.UTF7' is obsolete: 'The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.' (https://aka.ms/dotnet-warnings/SYSLIB0001)

Check warning on line 20 in ProjBobcat/ProjBobcat/Class/Helper/EncodingHelper.cs

View workflow job for this annotation

GitHub Actions / Test build on ubuntu-latest .NET 8.0

'Encoding.UTF7' is obsolete: 'The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.' (https://aka.ms/dotnet-warnings/SYSLIB0001)

Check warning on line 20 in ProjBobcat/ProjBobcat/Class/Helper/EncodingHelper.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'Encoding.UTF7' is obsolete: 'The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.' (https://aka.ms/dotnet-warnings/SYSLIB0001)

Check warning on line 20 in ProjBobcat/ProjBobcat/Class/Helper/EncodingHelper.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'Encoding.UTF7' is obsolete: 'The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.' (https://aka.ms/dotnet-warnings/SYSLIB0001)

Check warning on line 20 in ProjBobcat/ProjBobcat/Class/Helper/EncodingHelper.cs

View workflow job for this annotation

GitHub Actions / Test build on windows-latest .NET 8.0

'Encoding.UTF7' is obsolete: 'The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.' (https://aka.ms/dotnet-warnings/SYSLIB0001)

Check warning on line 20 in ProjBobcat/ProjBobcat/Class/Helper/EncodingHelper.cs

View workflow job for this annotation

GitHub Actions / Test build on windows-latest .NET 8.0

'Encoding.UTF7' is obsolete: 'The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.' (https://aka.ms/dotnet-warnings/SYSLIB0001)

Check warning on line 20 in ProjBobcat/ProjBobcat/Class/Helper/EncodingHelper.cs

View workflow job for this annotation

GitHub Actions / Test build on macOS-latest .NET 8.0

'Encoding.UTF7' is obsolete: 'The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.' (https://aka.ms/dotnet-warnings/SYSLIB0001)

Check warning on line 20 in ProjBobcat/ProjBobcat/Class/Helper/EncodingHelper.cs

View workflow job for this annotation

GitHub Actions / Test build on macOS-latest .NET 8.0

'Encoding.UTF7' is obsolete: 'The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.' (https://aka.ms/dotnet-warnings/SYSLIB0001)
if (IsGb2312(input)) return Encoding.GetEncoding("GB2312");
if (IsGbk(input)) return Encoding.GetEncoding("GBK");
if (IsGb18030(input)) return Encoding.GetEncoding("GB18030");
Expand Down
77 changes: 42 additions & 35 deletions ProjBobcat/ProjBobcat/Class/Helper/GameResourcesResolveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,52 +56,59 @@ public static class GameResourcesResolveHelper
bool isEnabled,
CancellationToken ct)
{
List<GameModInfoModel>? model = null;
try
{
List<GameModInfoModel>? model = null;

await using var stream = entry.OpenEntryStream();
var doc = await JsonDocument.ParseAsync(stream, cancellationToken: ct);
await using var stream = entry.OpenEntryStream();
var doc = await JsonDocument.ParseAsync(stream, cancellationToken: ct);

switch (doc.RootElement.ValueKind)
{
case JsonValueKind.Object:
var val = doc.RootElement.Deserialize(GameModInfoModelContext.Default.GameModInfoModel);
switch (doc.RootElement.ValueKind)
{
case JsonValueKind.Object:
var val = doc.RootElement.Deserialize(GameModInfoModelContext.Default.GameModInfoModel);

if (val != null) model = [val];
if (val != null) model = [val];

break;
case JsonValueKind.Array:
model = doc.RootElement.Deserialize(GameModInfoModelContext.Default.ListGameModInfoModel) ?? [];
break;
}
break;
case JsonValueKind.Array:
model = doc.RootElement.Deserialize(GameModInfoModelContext.Default.ListGameModInfoModel) ?? [];
break;
}

if (model == null || model.Count == 0)
return null;
if (model == null || model.Count == 0)
return null;

var authors = new HashSet<string>();
foreach (var author in model.Where(m => m.AuthorList != null).SelectMany(m => m.AuthorList!))
authors.Add(author);
var authors = new HashSet<string>();
foreach (var author in model.Where(m => m.AuthorList != null).SelectMany(m => m.AuthorList!))
authors.Add(author);

var baseMod = model.FirstOrDefault(m => string.IsNullOrEmpty(m.Parent));
var baseMod = model.FirstOrDefault(m => string.IsNullOrEmpty(m.Parent));

if (baseMod == null)
{
baseMod = model.First();
model.RemoveAt(0);
}
else
{
model.Remove(baseMod);
}
if (baseMod == null)
{
baseMod = model.First();
model.RemoveAt(0);
}
else
{
model.Remove(baseMod);
}

var authorStr = string.Join(',', authors);
var authorResult = string.IsNullOrEmpty(authorStr) ? null : authorStr;
var modList = model.Where(m => !string.IsNullOrEmpty(m.Name)).Select(m => m.Name!).ToImmutableList();
var titleResult = string.IsNullOrEmpty(baseMod.Name) ? Path.GetFileName(file) : baseMod.Name;
var authorStr = string.Join(',', authors);
var authorResult = string.IsNullOrEmpty(authorStr) ? null : authorStr;
var modList = model.Where(m => !string.IsNullOrEmpty(m.Name)).Select(m => m.Name!).ToImmutableList();
var titleResult = string.IsNullOrEmpty(baseMod.Name) ? Path.GetFileName(file) : baseMod.Name;

var displayModel = new GameModResolvedInfo(authorResult, file, modList, titleResult, baseMod.Version,
"Forge *", isEnabled);
var displayModel = new GameModResolvedInfo(authorResult, file, modList, titleResult, baseMod.Version,
"Forge *", isEnabled);

return displayModel;
return displayModel;
}
catch (Exception)
{
return null;
}
}

static async Task<GameModResolvedInfo> GetFabricModInfo(
Expand Down
16 changes: 11 additions & 5 deletions ProjBobcat/ProjBobcat/Class/Helper/GameVersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static partial class GameVersionHelper
TryGetMcVersionByFabric(version) ??
TryGetMcVersionByOptifine(version) ??
TryGetMcVersionByInheritFrom(version) ??
TryGetMcVersionByClientVersion(version) ??
TryGetMcVersionById(version);

if (string.IsNullOrEmpty(mcVersion)) continue;
Expand All @@ -31,17 +32,22 @@ public static partial class GameVersionHelper
return null;
}

private static string TryGetMcVersionById(RawVersionModel version)
static string TryGetMcVersionById(RawVersionModel version)
{
return version.Id;
}

private static string? TryGetMcVersionByInheritFrom(RawVersionModel version)
static string? TryGetMcVersionByClientVersion(RawVersionModel version)
{
return version.ClientVersion;
}

static string? TryGetMcVersionByInheritFrom(RawVersionModel version)
{
return version.InheritsFrom;
}

private static string? TryGetMcVersionByForgeGameArgs(RawVersionModel version)
static string? TryGetMcVersionByForgeGameArgs(RawVersionModel version)
{
var gameArgs = version.Arguments?.Game?
.Where(arg => arg.ValueKind == JsonValueKind.String)
Expand All @@ -61,7 +67,7 @@ private static string TryGetMcVersionById(RawVersionModel version)
return mcVersion;
}

private static string? TryGetMcVersionByFabric(RawVersionModel version)
static string? TryGetMcVersionByFabric(RawVersionModel version)
{
const string fabricLibPrefix = "net.fabricmc:intermediary:";

Expand All @@ -73,7 +79,7 @@ private static string TryGetMcVersionById(RawVersionModel version)
return mcVersion;
}

private static string? TryGetMcVersionByOptifine(RawVersionModel version)
static string? TryGetMcVersionByOptifine(RawVersionModel version)
{
const string optifineLibPrefix = "optifine:OptiFine:";

Expand Down
6 changes: 6 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Model/RawVersionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ public class RawVersionModel
[JsonPropertyName("id")]
public required string Id { get; set; }

/// <summary>
/// ClientVersion(Reserved field for other third-party launchers)
/// </summary>
[JsonPropertyName("clientVersion")]
public string? ClientVersion { get; set; }

[JsonPropertyName("javaVersion")] public JavaVersionModel? JavaVersion { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ static bool IsValidLogFile(FileInfo fi, double minutesAgo = 3)
var logType = GetLogFileType(log);

if (logType == LogFileType.Unknown) continue;

var lines = File.ReadAllText(log.FullName);
var encoding = EncodingHelper.GuessEncoding(lines);

lines = encoding.GetString(encoding.GetBytes(lines));

var rawLines = File.ReadAllText(log.FullName);
var encoding = EncodingHelper.GuessEncoding(rawLines);
var lines = File.ReadAllText(log.FullName, encoding);

if (lines.Length == 0) continue;

Expand Down