Skip to content

Commit

Permalink
fix: in function signatures
Browse files Browse the repository at this point in the history
Co-authored-by: sargon64 <[email protected]>
  • Loading branch information
ChecksumDev and sargon64 committed Aug 27, 2023
1 parent 52eef0b commit 5399d2c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 60 deletions.
38 changes: 30 additions & 8 deletions GenericStripper/Modules/BeatSaber/BSAssemblyModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Runtime.InteropServices;
using Mono.Cecil;
using Mono.Cecil.Rocks;

namespace GenericStripper.Modules.BeatSaber;

Expand All @@ -8,6 +10,8 @@ public class BsAssemblyModule
private readonly FileInfo _file;
private readonly ModuleDefinition _module;

private TypeReference? _inasmref;

public BsAssemblyModule(string gamePath, string fileName, params string[] resolverDirs)
{
_bslibLoader = new BsLibLoader(gamePath);
Expand Down Expand Up @@ -44,7 +48,7 @@ public void Virtualize()
foreach (var type in _module.Types) VirtualizeType(type);
}

private static void VirtualizeType(TypeDefinition type)
private void VirtualizeType(TypeDefinition type)
{
if (type.IsSealed) type.IsSealed = false;

Expand All @@ -53,14 +57,32 @@ private static void VirtualizeType(TypeDefinition type)

foreach (var subType in type.NestedTypes) VirtualizeType(subType);

foreach (var m in type.Methods.Where(m => m.IsManaged
&& m is
{
IsIL: true, IsStatic: false, IsVirtual: false, IsAbstract: false,
IsAddOn: false, IsConstructor: false, IsSpecialName: false,
IsGenericInstance: false, HasOverrides: false
}))
foreach (var m in type.Methods.Where(m => m.IsManaged && m is
{
IsIL: true, IsStatic: false, IsVirtual: false, IsAbstract: false, IsAddOn: false,
IsConstructor: false, IsSpecialName: false, IsGenericInstance: false, HasOverrides: false
}))
{
foreach (var p in m.Parameters.Where(p => p.IsIn))
{
_inasmref ??= _module.ImportReference(typeof(InAttribute));
List<TypeReference> opt = new();
List<TypeReference> req = new();
while (_inasmref is IModifierType modType)
{
if (_inasmref.IsOptionalModifier) opt.Add(modType.ModifierType);
else req.Add(modType.ModifierType);
_inasmref = modType.ElementType;
}

if (!req.Contains(_inasmref)) req.Add(_inasmref);
foreach (var typeReference in req) _inasmref = _inasmref.MakeRequiredModifierType(typeReference);

foreach (var typeReference in opt) _inasmref = _inasmref.MakeOptionalModifierType(typeReference);

p.ParameterType = _inasmref;
}

m.IsVirtual = true;
m.IsPublic = true;
m.IsPrivate = false;
Expand Down
96 changes: 48 additions & 48 deletions GenericStripper/Modules/BeatSaber/BeatSaber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,51 @@ public BeatSaber(string gamePath)
public string GameName => "Beat Saber";
public string GamePath { get; }

public void StripDll(string file, string outDir, params string[] resolveDirs)
{
if (!File.Exists(file)) throw new FileNotFoundException("Failed to find assembly to strip!", file);
var fileInf = new FileInfo(file);

var bsAssemblyModule = new BsAssemblyModule(GamePath, file, resolveDirs);
bsAssemblyModule.Virtualize();
bsAssemblyModule.Strip();

Directory.CreateDirectory(Path.Combine(outDir, Path.GetRelativePath(GamePath, fileInf.Directory!.ToString())));
var outFile = Path.Combine(outDir, Path.GetRelativePath(GamePath, fileInf.FullName));
bsAssemblyModule.Write(outFile);
}

public async Task StripAllDlls(string outDir)
{
await InstallBsipa();

var bsLibsDir = Path.Combine(GamePath, "Libs");
var bsManagedDir = Path.Combine(GamePath, "Beat Saber_Data", "Managed");

var outputDir = Path.Combine(GamePath, outDir);
if (!Directory.Exists(outputDir)) Directory.CreateDirectory(outputDir);

var libAssemblies = Directory.GetFiles(bsLibsDir, "*.dll", SearchOption.AllDirectories);
var managedAssemblies = Directory.GetFiles(bsManagedDir, "*.dll", SearchOption.AllDirectories);

AnsiConsole.Progress()
.Start(ctx =>
{
var task = ctx.AddTask("[salmon1]Stripping assemblies...[/]", new ProgressTaskSettings
{
MaxValue = libAssemblies.Length + managedAssemblies.Length
});

foreach (var assembly in libAssemblies.Concat(managedAssemblies))
{
StripDll(assembly, outputDir, bsLibsDir, bsManagedDir);
task.Increment(1);
AnsiConsole.MarkupLine($"[teal]Stripped {assembly}[/]");
}
}
);
}


private async Task InstallBsipa()
{
Expand Down Expand Up @@ -65,60 +110,15 @@ await AnsiConsole.Status()
{
FileName = Path.Combine(GamePath, "IPA.exe"),
WorkingDirectory = GamePath,
Arguments = "--nowait",
Arguments = "--nowait"
}
};

bsipa.Start();
await bsipa.WaitForExitAsync();

if (bsipa.ExitCode != 0) throw new Exception("Failed to install BSIPA!");
ctx.Status("[green]BSIPA installed![/]");
});
}

public void StripDll(string file, string outDir, params string[] resolveDirs)
{
if (!File.Exists(file)) throw new FileNotFoundException("Failed to find assembly to strip!", file);
var fileInf = new FileInfo(file);

var bsAssemblyModule = new BsAssemblyModule(GamePath, file, resolveDirs);
bsAssemblyModule.Virtualize();
bsAssemblyModule.Strip();

Directory.CreateDirectory(Path.Combine(outDir, Path.GetRelativePath(GamePath, fileInf.Directory!.ToString())));
var outFile = Path.Combine(outDir, Path.GetRelativePath(GamePath, fileInf.FullName));
bsAssemblyModule.Write(outFile);
}

public async Task StripAllDlls(string outDir)
{
await InstallBsipa();

var bsLibsDir = Path.Combine(GamePath, "Libs");
var bsManagedDir = Path.Combine(GamePath, "Beat Saber_Data", "Managed");

var outputDir = Path.Combine(GamePath, outDir);
if (!Directory.Exists(outputDir)) Directory.CreateDirectory(outputDir);

var libAssemblies = Directory.GetFiles(bsLibsDir, "*.dll", SearchOption.AllDirectories);
var managedAssemblies = Directory.GetFiles(bsManagedDir, "*.dll", SearchOption.AllDirectories);

AnsiConsole.Progress()
.Start(ctx =>
{
var task = ctx.AddTask("[salmon1]Stripping assemblies...[/]", new ProgressTaskSettings
{
MaxValue = libAssemblies.Length + managedAssemblies.Length
});

foreach (var assembly in libAssemblies.Concat(managedAssemblies))
{
StripDll(assembly, outputDir, bsLibsDir, bsManagedDir);
task.Increment(1);
AnsiConsole.MarkupLine($"[teal]Stripped {assembly}[/]");
}
}
);
}
}
2 changes: 1 addition & 1 deletion GenericStripper/Modules/IModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public interface IModule
public string GamePath { get; }

protected void StripDll(string file, string outDir, params string[] resolveDirs);

public Task StripAllDlls(string outDir);
}
6 changes: 3 additions & 3 deletions GenericStripper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
var module = settings.Module.ToLower();
var path = settings.Path;
var outDir = settings.Out;

IModule? mod = module switch
{
"beatsaber" => new BeatSaber(path),
_ => null
};

if (mod == null)
{
AnsiConsole.MarkupLine("[red]Invalid module specified![/]");
return 1;
}

await mod.StripAllDlls(outDir);
return 0;
}
Expand Down

0 comments on commit 5399d2c

Please sign in to comment.