CLre/CLre_server/Fixes/BugfixAttribute.cs

118 lines
No EOL
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace CLre_server.Fixes
{
[AttributeUsage(AttributeTargets.Class)]
public class BugfixAttribute : Attribute
{
public string name { get; set; }
public string description { get; set; }
public Type target { get; set; }
public string more { get; set; }
public uint id { get; set; }
public BugfixType component { get; set; }
}
public enum BugfixType : byte
{
Miscellaneous = 0x00,
HarmonyPatch,
Initialiser,
Workaround,
API,
Debug
}
internal static class BugfixAttributeUtility
{
public static void LogBugfixes()
{
List<uint> keys = new List<uint>();
Dictionary<uint, BugfixStruct> fixes = new Dictionary<uint, BugfixStruct>();
foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
{
BugfixAttribute b = t.GetCustomAttribute<BugfixAttribute>(true);
if (b != null)
{
if (!fixes.ContainsKey(b.id))
{
BugfixStruct bugfixStruct = new BugfixStruct{id = b.id};
bugfixStruct.Merge(b);
fixes[b.id] = bugfixStruct;
keys.Add(b.id);
}
else
{
BugfixStruct bugfixStruct = fixes[b.id];
bugfixStruct.Merge(b);
fixes[b.id] = bugfixStruct;
}
}
}
keys.Sort();
//keys.Sort((u, u1) => u.CompareTo(u1));
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Applying {0} CLre fixes\n", keys.Count);
sb.AppendFormat("-----------------------------\n");
foreach (uint i in keys)
{
sb.Append(fixes[i].ToString());
sb.Append("\n");
}
sb.AppendFormat("-----------------------------\n");
API.Utility.Logging.Log(sb.ToString());
}
private struct BugfixStruct
{
public string name;
public string description;
public Type target;
public string more;
public uint id;
private uint total;
private uint[] bugfixTypeCount;
public void Merge(BugfixAttribute b)
{
if (name == null && b.name != null) name = b.name;
if (description == null && b.description != null) description = b.description;
if (target == null && b.target != null) target = b.target;
if (more == null && b.more != null) more = b.more;
total++;
if (bugfixTypeCount == null) bugfixTypeCount = new uint[Enum.GetNames(typeof(BugfixType)).Length];
bugfixTypeCount[(byte) b.component]++;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat(" {0}: ", name);
if (more != null)
{
sb.AppendFormat("[MORE: {0}] ", more);
}
else if (description != null)
{
sb.Append(description);
sb.Append(" ");
}
if (target != null)
{
sb.AppendFormat("[TARGET: {0}] ", target.FullName);
}
sb.AppendFormat("[ID: {0}] ", id);
sb.AppendFormat("({0}M/{1}P/{2}I/{3}W/{4}A/{5}D/{6}T)",
bugfixTypeCount[(byte) BugfixType.Miscellaneous], bugfixTypeCount[(byte) BugfixType.HarmonyPatch],
bugfixTypeCount[(byte) BugfixType.Initialiser], bugfixTypeCount[(byte) BugfixType.Workaround],
bugfixTypeCount[(byte) BugfixType.API], bugfixTypeCount[(byte) BugfixType.Debug], total);
return sb.ToString();
}
}
}
}