Skip to content

Commit

Permalink
improve mod resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
laolarou726 committed Jul 14, 2024
1 parent d7ca0b4 commit 2922e84
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
60 changes: 60 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Helper/GameResourcesResolveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -18,6 +19,58 @@ namespace ProjBobcat.Class.Helper;

public static class GameResourcesResolveHelper
{
static string ProcessJsonString(string json)
{
json = json.Replace("\"dependencies\": [mod_minecraftForge]", "\"dependencies\": \"\"");
var jsonSplit = json.Split('\n');
if (jsonSplit.Length < 2) return json;

var startIndex = 0;
var endIndex = 0;

for (var i = 0; i < jsonSplit.Length; i++)
{
if (jsonSplit[i].Trim().EndsWith("{") || jsonSplit[i].Trim() == "\n" ||
string.IsNullOrWhiteSpace(jsonSplit[i].Trim()))
{
continue;
}

if (startIndex == 0)
{
if ((!jsonSplit[i].Trim().EndsWith("\"") && !jsonSplit[i].Trim().EndsWith(",")) ||
(jsonSplit[i].Replace(" ", "").EndsWith(":\"")))
{
startIndex = i;
continue;
}
}

if (startIndex == 0) continue;
if ((!jsonSplit[i].Trim().StartsWith("\"") || !jsonSplit[i + 1].Trim().StartsWith("}")) &&
(!jsonSplit[i].Trim().StartsWith("\",")) && (!jsonSplit[i].Trim().EndsWith("\","))) continue;

endIndex = i;
break;
}

if (startIndex == 0 || endIndex == 0) return json;
{
var tempInt = endIndex - 1;
for (var i = endIndex; i > startIndex; i--)
{
jsonSplit[tempInt] = $"{jsonSplit[tempInt]}\\n{jsonSplit[tempInt + 1]}";
tempInt -= 1;
}

var newJsonArray = jsonSplit.Where((s, index) => index < startIndex + 1 || index > endIndex).ToArray();

var newJson = string.Join("\n", newJsonArray);
return newJson;
}
}


static async Task<GameModResolvedInfo?> GetLegacyModInfo(
IArchiveEntry entry,
string file,
Expand Down Expand Up @@ -62,6 +115,13 @@ public static class GameResourcesResolveHelper
List<GameModInfoModel>? model = null;

await using var stream = entry.OpenEntryStream();
using var sr = new StreamReader(stream, leaveOpen: true);

var json = await sr.ReadToEndAsync(ct);
var fixedJson = ProcessJsonString(json);

await using var fixedStream = new MemoryStream(Encoding.UTF8.GetBytes(fixedJson));

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

switch (doc.RootElement.ValueKind)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace ProjBobcat.Class.Model.Fabric;
Expand Down Expand Up @@ -34,7 +35,7 @@ public class FabricModInfoModel

[JsonPropertyName("custom")] public Custom? Custom { get; set; }

[JsonPropertyName("depends")] public IReadOnlyDictionary<string, string>? Depends { get; set; }
[JsonPropertyName("depends")] public IReadOnlyDictionary<string, JsonElement>? Depends { get; set; }

[JsonPropertyName("recommends")] public IReadOnlyDictionary<string, string>? Recommends { get; set; }

Expand All @@ -44,14 +45,12 @@ public class FabricModInfoModel

[JsonPropertyName("icon")] public string? Icon { get; set; }

[JsonPropertyName("authors")] public string[] Authors { get; set; } = [];
[JsonPropertyName("authors")] public JsonElement[] Authors { get; set; } = [];

[JsonPropertyName("contacts")] public IReadOnlyDictionary<string, string>? Contacts { get; set; }

[JsonPropertyName("jars")] public FabricFileInfo[]? Jars { get; set; }
}

[JsonSerializable(typeof(FabricModInfoModel))]
partial class FabricModInfoModelContext : JsonSerializerContext
{
}
partial class FabricModInfoModelContext : JsonSerializerContext;
2 changes: 1 addition & 1 deletion ProjBobcat/ProjBobcat/ProjBobcat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ resolved the issue that LaunchWrapper may not return the correct exit code
<!-- Dependencies -->
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="8.0.6" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="8.0.7" />
<PackageReference Include="SharpCompress">
<Version>0.37.2</Version>
</PackageReference>
Expand Down

0 comments on commit 2922e84

Please sign in to comment.