Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 7 additions & 112 deletions Suto/Processor.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
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)
{
return new Processor(fileName, resolverDirs);
}

private readonly FileInfo _file;
private TypeReference? _inModreqRef;
private ModuleDefinition _module = null!;

internal Processor(string fileName, params string[] resolverDirs)
Expand Down Expand Up @@ -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)
Expand All @@ -125,40 +48,7 @@ public void Strip()
}
}

private (TypeReference Element, List<TypeReference> ModOpt, List<TypeReference> ModReq) GetDecomposedModifiers(TypeReference type)
{
var opt = new List<TypeReference>();
var req = new List<TypeReference>();

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<TypeReference> opt, IEnumerable<TypeReference> 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)
{
Expand All @@ -180,4 +70,9 @@ public void Write(string outFile)
{
_module.Write(outFile);
}

public void Dispose()
{
_module.Dispose();
}
}
8 changes: 5 additions & 3 deletions Suto/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
3 changes: 1 addition & 2 deletions Suto/Stripper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down