Pro_Unit

DebugExtensions

Mar 10th, 2022 (edited)
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6.  
  7. namespace Supernova.Extensions
  8. {
  9.     public class HexColors
  10.     {
  11.         public const string Shamrock = "45CEA2";
  12.         public const string DarkSlateGray = "236C45";
  13.  
  14.         public const string SandyBrown = "F4A460";
  15.  
  16.         public const string LightSkyBlue = "87CEFA";
  17.  
  18.         public const string CornflowerBlue = "6495ED";
  19.     }
  20.  
  21.    
  22.  
  23.     public static class DebugExtensions
  24.     {
  25.         private static readonly Dictionary<Type, IDebugColorDecorator> DebugColorDecorators =
  26.             new Dictionary<Type, IDebugColorDecorator>()
  27.             {
  28.                 [typeof(bool)] = new BoolDebugColorDecorator(),
  29.                 [typeof(int)] = new IntDebugColorDecorator(),
  30.                 [typeof(float)] = new FloatDebugColorDecorator(),
  31.                 [typeof(double)] = new DoubleDebugColorDecorator(),
  32.                 [typeof(long)] = new LongDebugColorDecorator(),
  33.                 [typeof(string)] = new StringDebugColorDecorator(),
  34.                 [typeof(Color)] = new ColorDebugColorDecorator(),
  35.             };
  36.  
  37.         public static T Log<T>(this T value) =>
  38.             Log(value, string.Empty);
  39.  
  40.         public static T Log<T>(this T value, string name)
  41.         {
  42.             Type type = typeof(T);
  43.  
  44.             if (name == string.Empty)
  45.                 name = type.Name;
  46.  
  47.             bool decoratorExist = DebugColorDecorators.ContainsKey(type);
  48.  
  49.             string logName = decoratorExist
  50.                 ? DebugColorDecorators[type]
  51.                     .DecorateName(name)
  52.                 : $"{name}";
  53.             string logValue = decoratorExist
  54.                 ? DebugColorDecorators[type]
  55.                     .DecorateValue(value)
  56.                 : $"{value}";
  57.  
  58.             Debug.Log($"{logName} : {logValue}", value as Object);
  59.  
  60.             return value;
  61.         }
  62.  
  63.         public static T LogAsJson<T>(this T value, string name = "")
  64.         {
  65.             Type type = typeof(T);
  66.             if (name.IsNullOrEmpty())
  67.                 name = type.Name;
  68.  
  69.             string prettyJson = value.ToPrettyJson(name);
  70.             Debug.Log(prettyJson);
  71.             return value;
  72.         }
  73.  
  74.         public static IEnumerable<T> Log<T>(this IEnumerable<T> source)
  75.         {
  76.             IEnumerable<T> log = source as T[] ?? source.ToArray();
  77.  
  78.             Debug.Log(log.Count());
  79.  
  80.             IEnumerable<string> enumerable = log.Select(
  81.                 arg => arg != null
  82.                     ? arg.ToString()
  83.                     : "null");
  84.  
  85.             Debug.Log(string.Join("\n", enumerable));
  86.  
  87.             return log;
  88.         }
  89.  
  90.         public static IEnumerable<T> LogAsJson<T>(this IEnumerable<T> source, string name = null)
  91.         {
  92.             IEnumerable<T> log = source as T[] ?? source.ToArray();
  93.  
  94.             Debug.Log(log.Count());
  95.  
  96.             if (name.IsNullOrEmpty())
  97.                 name = typeof(T).Name;
  98.  
  99.             Debug.Log(log.ToPrettyJson(name));
  100.  
  101.             return log;
  102.         }
  103.  
  104.         public static List<T> LogEvery<T>(this List<T> source) =>
  105.             LogEvery(source as IEnumerable<T>)
  106.                 .ToList();
  107.  
  108.         public static T[] LogEvery<T>(this T[] source) =>
  109.             LogEvery(source as IEnumerable<T>)
  110.                 .ToArray();
  111.  
  112.         public static IEnumerable<T> LogEvery<T>(this IEnumerable<T> source)
  113.         {
  114.             IEnumerable<T> logEvery = source as T[] ?? source.ToArray();
  115.             IEnumerable<T> log = source as T[] ?? logEvery.ToArray();
  116.  
  117.             Type type = source.GetType();
  118.             bool isGenericType = type.IsGenericType;
  119.  
  120.             string typeName = type.Name;
  121.             typeName = typeName.ToHexColor(HexColors.SandyBrown);
  122.  
  123.             if (isGenericType)
  124.             {
  125.                 Type argument = type.GenericTypeArguments.FirstOrDefault();
  126.  
  127.                 string argumentName = argument == null ? string.Empty : argument.Name;
  128.  
  129.                 typeName = typeName.Replace("`1", $"<{argumentName.ToHexColor(HexColors.DarkSlateGray)}>");
  130.             }
  131.  
  132.             string name = typeName;
  133.  
  134.             string count = log.Count().ToString().ToHexColor(HexColors.LightSkyBlue);
  135.  
  136.             var message = $"{name} Count : {count}";
  137.  
  138.             Debug.Log(message);
  139.  
  140.             foreach (T s in log)
  141.             {
  142.                 message = s.ToString().ToHexColor(HexColors.CornflowerBlue);
  143.  
  144.                 Debug.Log(message, s as Object);
  145.             }
  146.  
  147.             return logEvery;
  148.         }
  149.  
  150.         public static void LogMethod<T>(this T instance, string methodName) =>
  151.             Debug.Log(
  152.                 $"{typeof(T).Name.ToHexColor(HexColors.Shamrock)}({instance.GetHashCode()}).{methodName.ToHexColor(HexColors.SandyBrown)}",
  153.                 instance as Object);
  154.  
  155.         public static void LogConstructor<T>(this T instance) =>
  156.             Debug.Log($"{typeof(T).Name.ToHexColor(HexColors.Shamrock)}({instance.GetHashCode()})", instance as Object);
  157.     }
  158. }
Add Comment
Please, Sign In to add comment