Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
- [CLI] Added feature to search prebuild AssetMap to loaded filtede…
Browse files Browse the repository at this point in the history
…d files only.
  • Loading branch information
Razmoth committed Feb 6, 2024
1 parent 5597235 commit 61ff13f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions AssetStudio.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public static void Run(Options o)
{
if (o.MapOp.HasFlag(MapOpType.Load))
{
files = AssetsHelper.ParseAssetMap(o.MapName, o.MapType, classTypeFilter, o.NameFilter, o.ContainerFilter);
}
else
{
Expand Down
90 changes: 90 additions & 0 deletions AssetStudio/AssetsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,96 @@ private static void BuildAssetMap(string file, List<AssetEntry> assets, ClassIDT
}
}

public static string[] ParseAssetMap(string mapName, ExportListType mapType, ClassIDType[] typeFilter, Regex[] nameFilter, Regex[] containerFilter)
{
var matches = new HashSet<string>();

switch (mapType)
{
case ExportListType.MessagePack:
{
using var stream = File.OpenRead(mapName);
var assetMap = MessagePackSerializer.Deserialize<AssetMap>(stream, MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray));
foreach(var entry in assetMap.AssetEntries)
{
var isNameMatch = nameFilter.Length == 0 || nameFilter.Any(x => x.IsMatch(entry.Name));
var isContainerMatch = containerFilter.Length == 0 || containerFilter.Any(x => x.IsMatch(entry.Container));
var isTypeMatch = typeFilter.Length == 0 || typeFilter.Any(x => x == entry.Type);
if (isNameMatch && isContainerMatch && isTypeMatch)
{
matches.Add(entry.Source);
}
}
}

break;
case ExportListType.XML:
{
using var stream = File.OpenRead(mapName);
using var reader = XmlReader.Create(stream);
reader.ReadToFollowing("Assets");
reader.ReadToFollowing("Asset");
do
{
reader.ReadToFollowing("Name");
var name = reader.ReadInnerXml();

var isNameMatch = nameFilter.Length == 0 || nameFilter.Any(x => x.IsMatch(name));

reader.ReadToFollowing("Container");
var container = reader.ReadInnerXml();

var isContainerMatch = containerFilter.Length == 0 || containerFilter.Any(x => x.IsMatch(container));

reader.ReadToFollowing("Type");
var type = reader.ReadInnerXml();

var isTypeMatch = typeFilter.Length == 0 || typeFilter.Any(x => x.ToString().Equals(type, StringComparison.OrdinalIgnoreCase));

reader.ReadToFollowing("PathID");
var pathID = reader.ReadInnerXml();

reader.ReadToFollowing("Source");
var source = reader.ReadInnerXml();

if (isNameMatch && isContainerMatch && isTypeMatch)
{
matches.Add(source);
}

reader.ReadEndElement();
} while (reader.ReadToNextSibling("Asset"));
}

break;
case ExportListType.JSON:
{
using var stream = File.OpenRead(mapName);
using var file = new StreamReader(stream);
using var reader = new JsonTextReader(file);

var serializer = new JsonSerializer() { Formatting = Newtonsoft.Json.Formatting.Indented };
serializer.Converters.Add(new StringEnumConverter());

var entries = serializer.Deserialize<List<AssetEntry>>(reader);
foreach (var entry in entries)
{
var isNameMatch = nameFilter.Length == 0 || nameFilter.Any(x => x.IsMatch(entry.Name));
var isContainerMatch = containerFilter.Length == 0 || containerFilter.Any(x => x.IsMatch(entry.Container));
var isTypeMatch = typeFilter.Length == 0 || typeFilter.Any(x => x == entry.Type);
if (isNameMatch && isContainerMatch && isTypeMatch)
{
matches.Add(entry.Source);
}
}
}

break;
}

return matches.ToArray();
}

private static void UpdateContainers(List<AssetEntry> assets, Game game)
{
if (game.Type.IsGISubGroup() && assets.Count > 0)
Expand Down
12 changes: 11 additions & 1 deletion AssetStudio/TypeFlags.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace AssetStudio;
public static class TypeFlags
Expand Down Expand Up @@ -44,3 +45,12 @@ public static bool CanExport(this ClassIDType type)
return false;
}
}

[Flags]
public enum TypeFlag
{
None,
Parse,
Export,
Both = Parse | Export,
}

0 comments on commit 61ff13f

Please sign in to comment.