You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
6.0 KiB

  1. using CustomUI.Utilities;
  2. using IPA.Loader;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Reflection;
  8. using UnityEngine;
  9. using System.Runtime.CompilerServices;
  10. using IPA.Utilities;
  11. namespace BSIPA_ModList
  12. {
  13. internal static class Utilities
  14. {
  15. private static Sprite _defaultBsipaIcon;
  16. public static Sprite DefaultBSIPAIcon
  17. {
  18. get
  19. {
  20. if (_defaultBsipaIcon == null)
  21. _defaultBsipaIcon = UIUtilities.LoadSpriteFromResources("BSIPA_ModList.Icons.mod_bsipa.png");
  22. return _defaultBsipaIcon;
  23. }
  24. }
  25. private static Sprite _defaultLibraryIcon;
  26. public static Sprite DefaultLibraryIcon
  27. {
  28. get
  29. {
  30. if (_defaultLibraryIcon == null)
  31. _defaultLibraryIcon = UIUtilities.LoadSpriteFromResources("BSIPA_ModList.Icons.library.png");
  32. return _defaultLibraryIcon;
  33. }
  34. }
  35. private static Sprite _defaultIpaIcon;
  36. public static Sprite DefaultIPAIcon
  37. {
  38. get
  39. {
  40. if (_defaultIpaIcon == null)
  41. _defaultIpaIcon = UIUtilities.LoadSpriteFromResources("BSIPA_ModList.Icons.mod_ipa.png");
  42. return _defaultIpaIcon;
  43. }
  44. }
  45. public static Sprite GetIcon(this PluginLoader.PluginMetadata meta)
  46. {
  47. if (meta == null) return DefaultBSIPAIcon;
  48. if (meta.IsBare) return DefaultLibraryIcon;
  49. else return GetEmbeddedIcon(meta) ?? DefaultBSIPAIcon;
  50. }
  51. private static Dictionary<PluginLoader.PluginMetadata, Sprite> embeddedIcons = new Dictionary<PluginLoader.PluginMetadata, Sprite>();
  52. public static Sprite GetEmbeddedIcon(this PluginLoader.PluginMetadata meta)
  53. {
  54. if (embeddedIcons.TryGetValue(meta, out var sprite)) return sprite;
  55. var icon = GetEmbeddedIconNoCache(meta);
  56. embeddedIcons.Add(meta, icon);
  57. return icon;
  58. }
  59. private static Sprite GetEmbeddedIconNoCache(PluginLoader.PluginMetadata meta)
  60. {
  61. if (meta.Assembly == null) return null;
  62. if (meta.Manifest.IconPath == null) return null;
  63. try
  64. {
  65. return UIUtilities.LoadSpriteRaw(UIUtilities.GetResource(meta.Assembly, meta.Manifest.IconPath));
  66. }
  67. catch (Exception e)
  68. {
  69. Logger.log.Error($"Error loading icon for {meta.Name}");
  70. Logger.log.Error(e);
  71. return null;
  72. }
  73. }
  74. public static void DebugPrintTo<T>(this T obj, Action<string> log, int maxDepth = -1) =>
  75. DebugPrintTo(obj?.GetType() ?? typeof(T), obj, log, "", new ConditionalWeakTable<object, Ref<bool>>(), maxDepth);
  76. private static void DebugPrintTo(Type type, object obj, Action<string> log, string indent, ConditionalWeakTable<object, Ref<bool>> table, int maxDepth)
  77. {
  78. if (maxDepth == 0)
  79. {
  80. log(indent + "<Max depth reached>");
  81. return;
  82. }
  83. if (obj == null)
  84. {
  85. log(indent + "null");
  86. return;
  87. }
  88. table.Add(obj, true);
  89. if (type.IsPrimitive)
  90. {
  91. log(indent + obj.ToString());
  92. return;
  93. }
  94. if (type.IsEnum)
  95. {
  96. log(indent + obj.ToString());
  97. return;
  98. }
  99. if (type == typeof(string))
  100. {
  101. log(indent + $"\"{obj.ToString()}\"");
  102. return;
  103. }
  104. if (type.IsArray)
  105. {
  106. log(indent + $"{type.GetElementType()} [");
  107. foreach (var o in obj as Array)
  108. {
  109. if (type.GetElementType().IsPrimitive)
  110. log(indent + "- " + o?.ToString() ?? "null");
  111. else if (type.GetElementType().IsEnum)
  112. log(indent + "- " + o?.ToString() ?? "null");
  113. else if (type.GetElementType() == typeof(string))
  114. log(indent + "- " + $"\"{o?.ToString()}\"");
  115. else
  116. {
  117. log(indent + $"- {o?.GetType()?.ToString() ?? "null"}");
  118. if (o != null)
  119. {
  120. if (!table.TryGetValue(o, out _))
  121. DebugPrintTo(o.GetType(), o, log, indent + " ", table, maxDepth - 1);
  122. else
  123. log(indent + " <Already printed>");
  124. }
  125. }
  126. }
  127. log(indent + "]");
  128. return;
  129. }
  130. var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  131. foreach (var field in fields)
  132. {
  133. var value = field.GetValue(obj);
  134. if (field.FieldType.IsPrimitive)
  135. log(indent + field.Name + ": " + value?.ToString() ?? "null");
  136. else if (field.FieldType.IsEnum)
  137. log(indent + field.Name + ": " + value?.ToString() ?? "null");
  138. else if (field.FieldType == typeof(string))
  139. log(indent + field.Name + ": " + $"\"{value?.ToString()}\"");
  140. else
  141. {
  142. log(indent + field.Name + ": " + value?.GetType()?.ToString() ?? "null");
  143. if (value != null)
  144. {
  145. if (!table.TryGetValue(value, out _))
  146. DebugPrintTo(value?.GetType() ?? field.FieldType, value, log, indent + " ", table, maxDepth - 1);
  147. else
  148. log(indent + " <Already printed>");
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }