diff --git a/Suto/Processor.cs b/Suto/Processor.cs index dab4e06..27555e4 100644 --- a/Suto/Processor.cs +++ b/Suto/Processor.cs @@ -1,10 +1,8 @@ using Mono.Cecil; -using Mono.Cecil.Rocks; -using System.Runtime.InteropServices; namespace Suto; -internal class Processor +internal class Processor : IDisposable { public static Processor Load(string fileName, params string[] resolverDirs) { @@ -12,7 +10,6 @@ public static Processor Load(string fileName, params string[] resolverDirs) } private readonly FileInfo _file; - private TypeReference? _inModreqRef; private ModuleDefinition _module = null!; internal Processor(string fileName, params string[] resolverDirs) @@ -43,80 +40,6 @@ private void LoadModules(string[] directories) _module = ModuleDefinition.ReadModule(_file.FullName, parameters); } - public void Virtualize() - { - foreach (TypeDefinition type in _module.Types) - { - VirtualizeType(type); - } - } - - private void VirtualizeType(TypeDefinition type) - { - if (type.IsSealed) type.IsSealed = false; - - if (type.IsNestedPrivate) - { - type.IsNestedPrivate = false; - type.IsNestedPublic = true; - } - - if (type.IsInterface) return; - if (type.IsAbstract) return; - - foreach (var subType in type.NestedTypes) - { - VirtualizeType(subType); - } - - foreach (var m in type.Methods) - { - if (m.IsManaged - && m.IsIL - && !m.IsStatic - && (!m.IsVirtual || m.IsFinal) - && !m.IsAbstract - && !m.IsAddOn - && !m.IsConstructor - && !m.IsSpecialName - && !m.IsGenericInstance - && !m.HasOverrides) - { - - foreach (var param in m.Parameters) - { - if (param.IsIn) - { - _inModreqRef ??= _module.ImportReference(typeof(InAttribute)); - param.ParameterType = AddModreqIfNotExist(param.ParameterType, _inModreqRef); - } - } - - m.IsVirtual = true; - m.IsFinal = false; - m.IsPublic = true; - m.IsPrivate = false; - m.IsNewSlot = true; - m.IsHideBySig = true; - } - } - - foreach (var field in type.Fields) - { - if (field.IsPrivate) field.IsFamily = true; - } - } - - private TypeReference AddModreqIfNotExist(TypeReference type, TypeReference mod) - { - var (element, opt, req) = GetDecomposedModifiers(type); - if (!req.Contains(mod)) - { - req.Add(mod); - } - return BuildModifiedType(element, opt, req); - } - public void Strip() { foreach (TypeDefinition type in _module.Types) @@ -125,40 +48,7 @@ public void Strip() } } - private (TypeReference Element, List ModOpt, List ModReq) GetDecomposedModifiers(TypeReference type) - { - var opt = new List(); - var req = new List(); - - while (type is IModifierType modif) - { - if (type.IsOptionalModifier) - opt.Add(modif.ModifierType); - if (type.IsRequiredModifier) - req.Add(modif.ModifierType); - - type = modif.ElementType; - } - - return (type, opt, req); - } - - private TypeReference BuildModifiedType(TypeReference type, IEnumerable opt, IEnumerable req) - { - foreach (var mod in req) - { - type = type.MakeRequiredModifierType(mod); - } - - foreach (var mod in opt) - { - type = type.MakeOptionalModifierType(mod); - } - - return type; - } - - private void StripType(TypeDefinition type) + private static void StripType(TypeDefinition type) { foreach (var m in type.Methods) { @@ -180,4 +70,9 @@ public void Write(string outFile) { _module.Write(outFile); } + + public void Dispose() + { + _module.Dispose(); + } } \ No newline at end of file diff --git a/Suto/Program.cs b/Suto/Program.cs index c394cd3..29c1fa2 100644 --- a/Suto/Program.cs +++ b/Suto/Program.cs @@ -84,9 +84,11 @@ ZipFile.CreateFromDirectory(outDirectory, zipSavePath); -ProcessStartInfo info = new(); -info.FileName = "explorer"; -info.Arguments = string.Format("/select, \"{0}\"", zipSavePath); +ProcessStartInfo info = new() +{ + FileName = "explorer", + Arguments = $"/select, \"{zipSavePath}\"" +}; Process.Start(info); Log.Logger.Information("Completed. Enjoy your files!"); \ No newline at end of file diff --git a/Suto/Stripper.cs b/Suto/Stripper.cs index 77ef00e..bcd7660 100644 --- a/Suto/Stripper.cs +++ b/Suto/Stripper.cs @@ -12,8 +12,7 @@ internal static void DLL(string f, string outDir, params string[] resolverDirs) FileInfo file = new(f); Log.Logger.Debug($"Stripping {file.Name}"); - Processor mod = Processor.Load(file.FullName, resolverDirs); - mod.Virtualize(); + using Processor mod = Processor.Load(file.FullName, resolverDirs); mod.Strip(); string outFile = Path.Combine(outDir, file.Name);