Unofficial CardLife revival project, pronounced like "celery"
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.

114 lines
4.0KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace CLre.Fixes
  7. {
  8. [AttributeUsage(AttributeTargets.Class)]
  9. public class BugfixAttribute : Attribute
  10. {
  11. public string name { get; set; }
  12. public string description { get; set; }
  13. public Type target { get; set; }
  14. public string more { get; set; }
  15. public uint id { get; set; }
  16. public BugfixType component { get; set; }
  17. }
  18. public enum BugfixType : byte
  19. {
  20. Miscellaneous = 0x00,
  21. HarmonyPatch,
  22. Initialiser,
  23. Workaround,
  24. Debug
  25. }
  26. internal static class BugfixAttributeUtility
  27. {
  28. public static void LogBugfixes()
  29. {
  30. List<uint> keys = new List<uint>();
  31. Dictionary<uint, BugfixStruct> fixes = new Dictionary<uint, BugfixStruct>();
  32. foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
  33. {
  34. BugfixAttribute b = t.GetCustomAttribute<BugfixAttribute>(true);
  35. if (b != null)
  36. {
  37. if (!fixes.ContainsKey(b.id))
  38. {
  39. BugfixStruct bugfixStruct = new BugfixStruct{id = b.id};
  40. bugfixStruct.Merge(b);
  41. fixes[b.id] = bugfixStruct;
  42. keys.Add(b.id);
  43. }
  44. else
  45. {
  46. BugfixStruct bugfixStruct = fixes[b.id];
  47. bugfixStruct.Merge(b);
  48. fixes[b.id] = bugfixStruct;
  49. }
  50. }
  51. }
  52. keys.Sort();
  53. //keys.Sort((u, u1) => u.CompareTo(u1));
  54. StringBuilder sb = new StringBuilder();
  55. sb.AppendFormat("Applying {0} CLre fixes\n", keys.Count);
  56. sb.AppendFormat("-----------------------------\n");
  57. foreach (uint i in keys)
  58. {
  59. sb.Append(fixes[i].ToString());
  60. sb.Append("\n");
  61. }
  62. sb.AppendFormat("-----------------------------\n");
  63. API.Utility.Logging.Log(sb.ToString());
  64. }
  65. private struct BugfixStruct
  66. {
  67. public string name;
  68. public string description;
  69. public Type target;
  70. public string more;
  71. public uint id;
  72. private uint total;
  73. private uint[] bugfixTypeCount;
  74. public void Merge(BugfixAttribute b)
  75. {
  76. if (name == null && b.name != null) name = b.name;
  77. if (description == null && b.description != null) description = b.description;
  78. if (target == null && b.target != null) target = b.target;
  79. if (more == null && b.more != null) more = b.more;
  80. total++;
  81. if (bugfixTypeCount == null) bugfixTypeCount = new uint[Enum.GetNames(typeof(BugfixType)).Length];
  82. bugfixTypeCount[(byte) b.component]++;
  83. }
  84. public override string ToString()
  85. {
  86. StringBuilder sb = new StringBuilder();
  87. sb.AppendFormat(" {0}: ", name);
  88. if (more != null)
  89. {
  90. sb.AppendFormat("[MORE: {0}] ", more);
  91. }
  92. else if (description != null)
  93. {
  94. sb.Append(description);
  95. sb.Append(" ");
  96. }
  97. if (target != null)
  98. {
  99. sb.AppendFormat("[TARGET: {0}] ", target.FullName);
  100. }
  101. sb.AppendFormat("[ID: {0}] ", id);
  102. sb.AppendFormat("({0}M/{1}P/{2}I/{3}W/{4}D/{5}T)", bugfixTypeCount[(byte) BugfixType.Miscellaneous], bugfixTypeCount[(byte) BugfixType.HarmonyPatch], bugfixTypeCount[(byte) BugfixType.Initialiser], bugfixTypeCount[(byte) BugfixType.Workaround], bugfixTypeCount[(byte) BugfixType.Debug], total);
  103. return sb.ToString();
  104. }
  105. }
  106. }
  107. }