Skip to content

Commit

Permalink
add icon for the mod resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
laolarou726 committed Jan 10, 2025
1 parent 43a085f commit cc00c9c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
44 changes: 43 additions & 1 deletion ProjBobcat/ProjBobcat/Class/Helper/GameResourcesResolveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,44 @@ public static ModLoaderType GetModLoaderType(IArchive archive)
return ModLoaderType.Unknown;
}

private static async Task<byte[]?> TryResolveModIcon(IArchive archive)
{
static bool IsInRootPath(IArchiveEntry entry)
{
var path = entry.Key?.Split('/', StringSplitOptions.RemoveEmptyEntries);

if (path == null || path.Length == 0) return false;

return path.Length == 1;
}

static bool IsImageFile(IArchiveEntry entry)
{
var ext = Path.GetExtension(entry.Key ?? string.Empty);

return !string.IsNullOrEmpty(ext) &&
(ext.Equals(".png", StringComparison.OrdinalIgnoreCase) ||
ext.Equals(".jpg", StringComparison.OrdinalIgnoreCase) ||
ext.Equals(".jpeg", StringComparison.OrdinalIgnoreCase) ||
ext.Equals(".gif", StringComparison.OrdinalIgnoreCase) ||
ext.Equals(".bmp", StringComparison.OrdinalIgnoreCase) ||
ext.Equals(".webp", StringComparison.OrdinalIgnoreCase));
}

var iconEntry = archive.Entries
.FirstOrDefault(e => IsImageFile(e) && IsInRootPath(e));

if (iconEntry == null)
return null;

await using var entryStream = iconEntry.OpenEntryStream();
await using var ms = new MemoryStream();

await entryStream.CopyToAsync(ms);

return ms.ToArray();
}

public static async IAsyncEnumerable<GameModResolvedInfo> ResolveModListAsync(
IEnumerable<string> files,
[EnumeratorCancellation] CancellationToken ct)
Expand Down Expand Up @@ -286,7 +324,11 @@ public static async IAsyncEnumerable<GameModResolvedInfo> ResolveModListAsync(
isEnabled);

ReturnResult:
result = result! with { LoaderType = GetModLoaderType(archive) };
result = result! with
{
LoaderType = GetModLoaderType(archive),
IconBytes = await TryResolveModIcon(archive)
};
yield return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public record GameModResolvedInfo(
bool IsEnabled)
{
public ModLoaderType LoaderType { get; init; }
public byte[]? IconBytes { get; init; }
}

0 comments on commit cc00c9c

Please # to comment.