Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using TagTool.Cache;
  4. using TagTool.Commands.Common;
  5.  
  6. namespace TagTool.Commands.Tags
  7. {
  8.     class ListUnnamedTagsCommand : Command
  9.     {
  10.         public HaloOnlineCacheContext CacheContext { get; }
  11.  
  12.         public ListUnnamedTagsCommand(HaloOnlineCacheContext cacheContext)
  13.             : base(false,
  14.  
  15.                   "ListUnnamedTags",
  16.                   "Lists all unnamed tags in the current tag cache",
  17.  
  18.                   "ListUnnamedTags",
  19.  
  20.                   "Lists all unnamed tags in the current tag cache")
  21.         {
  22.             CacheContext = cacheContext;
  23.         }
  24.  
  25.         public override object Execute(List<string> args)
  26.         {
  27.             if (args.Count != 0)
  28.                 return false;
  29.  
  30.             var unnamedTags = new List<CachedTagInstance>();
  31.  
  32.             foreach (var tag in CacheContext.TagCache.Index)
  33.             {
  34.                 if (tag != null && (tag.Name == null || tag.Name == ""))
  35.                 {
  36.                     //Console.WriteLine($"0x{tag.Index:X4}.{tag.Group.Tag.ToString()}");
  37.                     unnamedTags.Add(tag);
  38.                 }
  39.             }
  40.  
  41.             Console.WriteLine($"Total unnamed tag count: {unnamedTags.Count}");
  42.  
  43.             var topmostTags = new List<CachedTagInstance>();
  44.  
  45.             foreach (var tag in unnamedTags)
  46.             {
  47.                 var topmost = FindTopmost(tag);
  48.  
  49.                 foreach (var topmostTag in topmost)
  50.                 {
  51.                     var topmostInstance = CacheContext.GetTag(topmostTag);
  52.  
  53.                     if (!topmostTags.Contains(topmostInstance))
  54.                         topmostTags.Add(topmostInstance);
  55.                 }
  56.             }
  57.  
  58.             Console.WriteLine("Topmost tags:");
  59.  
  60.             foreach (var tag in topmostTags)
  61.             {
  62.                 var tagName = (tag.Name == null || tag.Name == "") ?
  63.                     $"0x{tag.Index:X4}" :
  64.                     tag.Name;
  65.  
  66.                 Console.WriteLine($"{tagName}.{tag.Group.Tag}");
  67.             }
  68.  
  69.             Console.WriteLine($"Total topmost tag count: {topmostTags.Count}");
  70.  
  71.             return true;
  72.         }
  73.  
  74.         private List<int> FindTopmost(CachedTagInstance instance)
  75.         {
  76.             var result = new List<int>();
  77.  
  78.             if (instance == null)
  79.                 return result;
  80.  
  81.             var indexQueue = new List<int> { instance.Index };
  82.  
  83.             while (indexQueue.Count != 0)
  84.             {
  85.                 var nextIndexQueue = new List<int>();
  86.  
  87.                 foreach (var index in indexQueue)
  88.                 {
  89.                     if (result.Contains(index))
  90.                         continue;
  91.  
  92.                     foreach (var currentInstance in CacheContext.TagCache.Index)
  93.                     {
  94.                         if (currentInstance == null || currentInstance.Index == index)
  95.                             continue;
  96.  
  97.                         foreach (var dependentIndex in currentInstance.Dependencies)
  98.                         {
  99.                             var dependency = CacheContext.GetTag(dependentIndex);
  100.  
  101.                             if (dependency == null || dependency.Index != index)
  102.                                 continue;
  103.  
  104.                             if (!nextIndexQueue.Contains(currentInstance.Index))
  105.                                 nextIndexQueue.Add(currentInstance.Index);
  106.                         }
  107.                     }
  108.                 }
  109.  
  110.                 if (nextIndexQueue.Count == 0)
  111.                 {
  112.                     foreach (var entry in indexQueue)
  113.                     {
  114.                         if (!result.Contains(entry))
  115.                             result.Add(entry);
  116.                     }
  117.                     break;
  118.                 }
  119.                 else if (nextIndexQueue.Count == 1)
  120.                 {
  121.                     var dependentInstance = CacheContext.GetTag(nextIndexQueue[0]);
  122.  
  123.                     if (dependentInstance.IsInGroup("cfgt") ||
  124.                         dependentInstance.IsInGroup("matg") ||
  125.                         dependentInstance.IsInGroup("scnr"))
  126.                     {
  127.                         result.AddRange(indexQueue);
  128.                         break;
  129.                     }
  130.                 }
  131.                 else if (indexQueue.Count == nextIndexQueue.Count)
  132.                 {
  133.                     var equal = true;
  134.  
  135.                     foreach (var entry in nextIndexQueue)
  136.                     {
  137.                         if (!indexQueue.Contains(entry))
  138.                         {
  139.                             equal = false;
  140.                             break;
  141.                         }
  142.                     }
  143.  
  144.                     if (equal)
  145.                         break;
  146.                 }
  147.  
  148.                 indexQueue = nextIndexQueue;
  149.             }
  150.  
  151.             return result;
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement