Skip to content

Commit

Permalink
Merge pull request #126 from Corona-Studio/hotfix
Browse files Browse the repository at this point in the history
fix log encoding detect
  • Loading branch information
laolarou726 authored Mar 6, 2024
2 parents 9f3c5ac + 6d8b8e7 commit 6df92a2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 45 deletions.
6 changes: 6 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Helper/EncodingHelper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
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;
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

0 comments on commit 6df92a2

Please sign in to comment.