using Mono.Cecil; using System; using System.IO; namespace ModTool.Shared { /// /// Extension methods for enums. /// public static class EnumExtensions { /// /// Unity's enum mask fields sets all bits to 1. This sets all unused bits to 0, so it can be converted to a string and serialized properly. /// /// An enum instance. /// A fixed enum. public static int FixEnum(this Enum self) { int bits = 0; foreach (var enumValue in Enum.GetValues(self.GetType())) { int checkBit = Convert.ToInt32(self) & (int)enumValue; if (checkBit != 0) { bits |= (int)enumValue; } } return bits; } } /// /// Extension methods for strings. /// public static class StringExtensions { /// /// Returns a normalized version of a path. /// /// A string. /// A normalized version of a path. public static string NormalizedPath(this string self) { string normalizedPath = Path.GetFullPath(new Uri(self).LocalPath); normalizedPath = normalizedPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); normalizedPath = normalizedPath.ToLowerInvariant(); return normalizedPath; } } /// /// Extension methods for Mono.Cecil. /// public static class CecilExtensions { /// /// Is this Type a subclass of the other Type? /// /// A TypeDefinition. /// A Type's full name. /// True if this TypeDefinition is a subclass of the Type. public static bool IsSubClassOf(this TypeDefinition self, string typeName) { TypeDefinition type = self; while (type != null) { if (type.BaseType != null) { try { type = type.BaseType.Resolve(); } catch (AssemblyResolutionException e) { LogUtility.LogWarning("Could not resolve " + e.AssemblyReference.Name + " in IsSubClassOf()."); return false; } } else type = null; if (type?.GetFullName() == typeName) return true; } return false; } /// /// Get the Type's name including the Type's namespace. /// /// /// public static string GetFullName(this TypeReference self) { string fullName = self.Name; if (string.IsNullOrEmpty(self.Namespace)) return fullName; return self.Namespace + "." + fullName; } /// /// Get the Member's name including the declaring Type's full name. /// /// /// public static string GetFullName(this MemberReference self) { string name = self.Name; if (self.DeclaringType == null) return name; return self.DeclaringType.GetFullName() + "." + name; } } }