using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
namespace AutoGroupGenerator
{
///
/// Output rule that renames group layouts using simple heuristics.
///
[CreateAssetMenu(menuName = Constants.ContextMenus.OutputRulesMenu + nameof(ImprovedNamesOutputRule))]
public class ImprovedNamesOutputRule : OutputRule
{
#region Methods
///
protected override bool DoesMatchSelectionCriteria(GroupLayout groupLayout)
{
return true;
}
///
/// Renames group layouts based on heuristics derived from their contents.
///
public override void Refine()
{
HashSet inputAssets = m_DataContainer.InputAssets;
foreach (var groupLayout in m_Selection)
{
var currentName = groupLayout.Name;
var groupLayoutAssets = groupLayout.Nodes.Select(node => node.AssetPath).ToHashSet();
var intersection = new HashSet(inputAssets);
intersection.IntersectWith(groupLayoutAssets);
if (intersection.Count == 1)
{
Rename(groupLayout, $"Hierarchy of {Path.GetFileName(intersection.ToList()[0])}_{currentName}");
continue;
}
if (groupLayout.Sources.Count == 1)
{
Rename(groupLayout, $"Dependencies of {groupLayout.Sources.ToList()[0].FileName}_{currentName}");
continue;
}
if (groupLayout.Nodes.Count == 1)
{
Rename(groupLayout, $"{groupLayout.Nodes.ToList()[0].FileName}_{currentName}");
continue;
}
if (groupLayout.IsShared)
{
var newName = $"Shared_{currentName}";
Rename(groupLayout, newName);
continue;
}
}
}
#endregion
}
}