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.

BugfixAttribute.cs 4.1KB

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