Skip to content
This repository has been archived by the owner on Jul 26, 2021. It is now read-only.

Commit

Permalink
Replace netstandard workaround with better method
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak committed Jan 2, 2021
1 parent 0015ee9 commit e6f5afb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<UsingTask TaskName="Reobfuscate" AssemblyFile="$(TaskAssembly)" />
<UsingTask TaskName="Deobfuscate" AssemblyFile="$(TaskAssembly)" />

<Target Name="WarnAboutTargetFramework" BeforeTargets="PrepareForBuild" Condition=" '$(TargetFramework)' != 'netstandard2.1' ">
<!-- TODO link to docs explaining why netstandard is better -->
<Warning Text="Your project is not targetting netstandard2.1, you probably want to change that." File="$(MSBuildProjectFullPath)" />
</Target>

<Target Name="LoadMappings">
<LoadMappings TargetGamePlatform="$(TargetGamePlatform)" Mappings="$(Mappings)">
<Output TaskParameter="MappingsJson" PropertyName="MappingsJson" />
Expand Down Expand Up @@ -43,6 +48,6 @@
</Target>

<Target Name="Reobfuscate" AfterTargets="Build">
<Reobfuscate Input="$(ProjectDir)$(OutputPath)$(AssemblyName).dll" ReferencedAssemblies="@(ReferencePath)" />
<Reobfuscate AmongUs="$(AmongUs)" Input="$(ProjectDir)$(OutputPath)$(AssemblyName).dll" ReferencedAssemblies="@(ReferencePath)" />
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<LangVersion>latest</LangVersion>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

<Version>0.1.3</Version>
<Version>0.1.4</Version>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/NuclearPowered/Reactor.OxygenFilter</RepositoryUrl>
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>
Expand Down
31 changes: 22 additions & 9 deletions Reactor.OxygenFilter.MSBuild/Reobfuscate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace Reactor.OxygenFilter.MSBuild
{
public class Reobfuscate : Task
{
[Required]
public string AmongUs { get; set; }

[Required]
public string Input { get; set; }

Expand All @@ -30,13 +33,23 @@ public override bool Execute()
using var stream = File.Open(Input, FileMode.Open, FileAccess.ReadWrite);
var resolver = new DefaultAssemblyResolver();

foreach (var directory in ReferencedAssemblies
.Select(Path.GetDirectoryName)
.Distinct()
.OrderBy(x => x.EndsWith("unhollowed"))) // workaround for mono.cecil picking netstandard.dll from unhollowed instead of sdk
resolver.ResolveFailure += (_, reference) =>
{
resolver.AddSearchDirectory(directory);
}
foreach (var referencedAssembly in ReferencedAssemblies)
{
if (Path.GetFileNameWithoutExtension(referencedAssembly) == reference.Name)
{
var assemblyDefinition = AssemblyDefinition.ReadAssembly(referencedAssembly);

if (assemblyDefinition.Name.FullName == reference.FullName)
{
return assemblyDefinition;
}
}
}

return null;
};

using var moduleDefinition = ModuleDefinition.ReadModule(stream, new ReaderParameters { AssemblyResolver = resolver });
var toObfuscate = new Dictionary<MemberReference, string>();
Expand Down Expand Up @@ -153,6 +166,8 @@ public override bool Execute()
}
}

var obfuscatedAssembly = ModuleDefinition.ReadModule(Path.Combine(AmongUs, "BepInEx", "unhollowed", "Assembly-CSharp.dll"));

// fix generic methods
foreach (var methodDefinition in moduleDefinition.GetAllTypes().SelectMany(x => x.Methods))
{
Expand All @@ -172,8 +187,6 @@ public override bool Execute()
{
// get same type from obfuscated assembly

var assemblyDefinition = resolver.Resolve(new AssemblyNameDefinition("Assembly-CSharp", null));

var hierarchy = new List<TypeDefinition>();
var type = deobfuscatedCall.DeclaringType;

Expand All @@ -183,7 +196,7 @@ public override bool Execute()
type = type.DeclaringType;
}

var typeDefinition = assemblyDefinition.MainModule.GetType(GetObfuscated(hierarchy.First()));
var typeDefinition = obfuscatedAssembly.GetType(GetObfuscated(hierarchy.First()));

foreach (var element in hierarchy.Skip(1))
{
Expand Down

0 comments on commit e6f5afb

Please sign in to comment.