using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Abstractions.Extractors.Unity.Services;
namespace Abstractions.Extractors.Unity;
///
/// Catch-all extractor: dumps the full typetree as JSON for any asset type with no dedicated
/// extractor. Also used internally by MeshExtractor/MaterialExtractor/AnimationExtractor/
/// MonoBehaviourExtractor as their own on-failure fallback.
///
public sealed class FallbackExtractor : IAssetExtractor
{
public IReadOnlyCollection HandledTypes { get; } = [];
public Task ExtractAsync(ExtractionContext context, CancellationToken cancellationToken = default) =>
WriteTypeTreeDumpAsync(context, cancellationToken);
/// Dumps the object's full typetree to "{output}.json"; if it can't be JSON-serialized
/// for some reason, falls back further to a plain-text dump ("{output}.txt").
public static async Task WriteTypeTreeDumpAsync(ExtractionContext context, CancellationToken cancellationToken = default)
{
var info = ObjectSerializer.ReadTypeTree(context.BaseField);
try
{
await ObjectSerializer.WriteJsonAsync(info, context.OutputPathNoExtension + ".json", cancellationToken);
return true;
}
catch
{
await ObjectSerializer.WriteTextAsync(info?.ToString() ?? "null", context.OutputPathNoExtension + ".txt", cancellationToken);
return true;
}
}
}